Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into pr/592
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/client/java/minicraft/item/Items.java
#	src/client/java/minicraft/item/Recipes.java
#	src/client/java/minicraft/saveload/Load.java
  • Loading branch information
BenCheung0422 committed Oct 10, 2024
2 parents 65435f2 + b197319 commit 0cf3270
Show file tree
Hide file tree
Showing 160 changed files with 2,912 additions and 3,590 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
allprojects {
apply plugin: "java"

version = "2.2.1-dev1"
version = "2.2.1-dev2"

sourceCompatibility = 8
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
Expand Down
2 changes: 1 addition & 1 deletion src/client/java/minicraft/core/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protected Game() {

public static final String NAME = "Minicraft Plus"; // This is the name on the application window.

public static final Version VERSION = new Version("2.2.1-dev1");
public static final Version VERSION = new Version("2.2.1-dev2");

public static InputHandler input; // Input used in Game, Player, and just about all the *Menu classes.
public static Player player;
Expand Down
6 changes: 3 additions & 3 deletions src/client/java/minicraft/core/io/Localization.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.regex.Pattern;

public class Localization {

Expand All @@ -18,6 +19,7 @@ public class Localization {
public static boolean isDebugLocaleEnabled = false;
public static boolean unlocalizedStringTracing = false;

private static final Pattern NUMBER_REGEX = Pattern.compile("^[+-]?((\\d+(\\.\\d*)?)|(\\.\\d+))$");
private static final HashMap<Locale, HashSet<String>> knownUnlocalizedStrings = new HashMap<>();
private static final HashMap<String, String> localization = new HashMap<>();

Expand All @@ -36,10 +38,8 @@ public static String getLocalized(String key, Object... arguments) {
if (key.matches("^ *$")) return key; // Blank, or just whitespace
if (selectedLocale == DEBUG_LOCALE) return key;

try {
Double.parseDouble(key);
if (NUMBER_REGEX.matcher(key).matches()) {
return key; // This is a number; don't try to localize it
} catch (NumberFormatException ignored) {
}

String localString = localization.get(key);
Expand Down
27 changes: 25 additions & 2 deletions src/client/java/minicraft/entity/furniture/Bed.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,45 @@
import minicraft.core.Updater;
import minicraft.core.io.Localization;
import minicraft.entity.mob.Player;
import minicraft.gfx.SpriteAnimation;
import minicraft.gfx.SpriteLinker.LinkedSprite;
import minicraft.gfx.SpriteLinker.SpriteType;
import minicraft.item.DyeItem;
import minicraft.level.Level;
import minicraft.util.MyUtils;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;

public class Bed extends Furniture {

private static final HashMap<DyeItem.DyeColor, LinkedSprite> sprites = new HashMap<>();
private static final HashMap<DyeItem.DyeColor, LinkedSprite> itemSprites = new HashMap<>();

@Override
public @NotNull Furniture copy() {
return new Bed(color);
}

static {
for (DyeItem.DyeColor color : DyeItem.DyeColor.values()) {
sprites.put(color, new LinkedSprite(SpriteType.Entity, color.toString().toLowerCase() + "_bed"));
itemSprites.put(color, new LinkedSprite(SpriteType.Item, color.toString().toLowerCase() + "_bed"));
}
}

private static int playersAwake = 1;
private static final HashMap<Player, Bed> sleepingPlayers = new HashMap<>();

public final DyeItem.DyeColor color;

/**
* Creates a new furniture with the name Bed and the bed sprite and color.
*/
public Bed() {
super("Bed", new LinkedSprite(SpriteType.Entity, "bed"), new LinkedSprite(SpriteType.Item, "bed"), 3, 2);
public Bed() { this(DyeItem.DyeColor.RED); }
public Bed(DyeItem.DyeColor color) {
super(MyUtils.capitalizeFully(color.toString().replace('_', ' ')) + " Bed", sprites.get(color), itemSprites.get(color), 3, 2);
this.color = color;
}

/**
Expand Down
9 changes: 5 additions & 4 deletions src/client/java/minicraft/entity/furniture/Crafter.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public enum Type {
Furnace(new LinkedSprite(SpriteType.Entity, "furnace"), new LinkedSprite(SpriteType.Item, "furnace"), 3, 2, Recipes.furnaceRecipes),
Anvil(new LinkedSprite(SpriteType.Entity, "anvil"), new LinkedSprite(SpriteType.Item, "anvil"), 3, 2, Recipes.anvilRecipes),
Enchanter(new LinkedSprite(SpriteType.Entity, "enchanter"), new LinkedSprite(SpriteType.Item, "enchanter"), 7, 2, Recipes.enchantRecipes),
Loom(new LinkedSprite(SpriteType.Entity, "loom"), new LinkedSprite(SpriteType.Item, "loom"), 7, 2, Recipes.loomRecipes);
Loom(new LinkedSprite(SpriteType.Entity, "loom"), new LinkedSprite(SpriteType.Item, "loom"), 7, 2, Recipes.loomRecipes),
DyeVat(new LinkedSprite(SpriteType.Entity, "dyevat"), new LinkedSprite(SpriteType.Item, "dyevat"), 0, 0, Recipes.dyeVatRecipes);

public ArrayList<Recipe> recipes;
protected LinkedSprite sprite;
Expand All @@ -45,12 +46,12 @@ public enum Type {
* @param type What type of crafter this is.
*/
public Crafter(Crafter.Type type) {
super(type.name(), type.sprite, type.itemSprite, type.xr, type.yr);
super((type.name().equalsIgnoreCase("DyeVat") ? "Dye Vat" : type.name()), type.sprite, type.itemSprite, type.xr, type.yr);
this.type = type;
}

public boolean use(Player player) {
Game.setDisplay(new CraftingDisplay(type.recipes, type.name(), player));
Game.setDisplay(new CraftingDisplay(type.recipes, (type.name().equalsIgnoreCase("DyeVat") ? "Dye Vat" : type.name()), player));
return true;
}

Expand All @@ -61,6 +62,6 @@ public boolean use(Player player) {

@Override
public String toString() {
return type.name() + getDataPrints();
return (type.name().equalsIgnoreCase("DyeVat") ? "Dye Vat" : type.name()) + getDataPrints();
}
}
2 changes: 1 addition & 1 deletion src/client/java/minicraft/entity/mob/Mob.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public static LinkedSprite[][] compileMobSpriteAnimations(int sheetX, int sheetY
private boolean isWooling() { // supposed to walk at half speed on wool
if (level == null) return false;
Tile tile = level.getTile(x >> 4, y >> 4);
return tile == Tiles.get("wool");
return tile == Tiles.get("white wool");
}

/**
Expand Down
50 changes: 39 additions & 11 deletions src/client/java/minicraft/entity/mob/Sheep.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package minicraft.entity.mob;

import minicraft.item.DyeItem;
import org.jetbrains.annotations.Nullable;

import minicraft.core.io.Settings;
import minicraft.entity.Direction;
import minicraft.gfx.Screen;
Expand All @@ -11,27 +14,50 @@
import minicraft.level.tile.GrassTile;
import minicraft.level.tile.Tile;
import minicraft.level.tile.Tiles;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;

public class Sheep extends PassiveMob {
private static final LinkedSprite[][] sprites = Mob.compileMobSpriteAnimations(0, 0, "sheep");
private static final LinkedSprite[][] cutSprites = Mob.compileMobSpriteAnimations(0, 2, "sheep");
private static final HashMap<DyeItem.DyeColor, LinkedSprite[][]> sprites = new HashMap<>();
private static final HashMap<DyeItem.DyeColor, LinkedSprite[][]> cutSprites = new HashMap<>();

static {
for (DyeItem.DyeColor color : DyeItem.DyeColor.values()) {
LinkedSprite[][] mobSprites = Mob.compileMobSpriteAnimations(0, 0, "sheep");
for (LinkedSprite[] mobSprite : mobSprites) {
for (LinkedSprite linkedSprite : mobSprite) {
linkedSprite.setColor(color.color);
}
}
sprites.put(color, mobSprites);
mobSprites = Mob.compileMobSpriteAnimations(0, 2, "sheep");
for (LinkedSprite[] mobSprite : mobSprites) {
for (LinkedSprite linkedSprite : mobSprite) {
linkedSprite.setColor(color.color);
}
}
cutSprites.put(color, mobSprites);
}
}

public boolean cut = false;
public DyeItem.DyeColor color;

/**
* Creates a sheep entity.
*/
public Sheep() {
super(sprites);
public Sheep() { this(DyeItem.DyeColor.WHITE); }
public Sheep(DyeItem.DyeColor color) {
super(null);
this.color = color;
}

@Override
public void render(Screen screen) {
int xo = x - 8;
int yo = y - 11;

LinkedSprite[][] curAnim = cut ? cutSprites : sprites;
LinkedSprite[][] curAnim = cut ? cutSprites.get(color) : sprites.get(color);

LinkedSprite curSprite = curAnim[dir.getDir()][(walkDist >> 3) % curAnim[dir.getDir()].length];
if (hurtTime > 0) {
Expand All @@ -52,15 +78,17 @@ public void tick() {
}

public boolean interact(Player player, @Nullable Item item, Direction attackDir) {
if (cut) return false;

if (item instanceof ToolItem) {
if (((ToolItem) item).type == ToolType.Shears) {
if (!cut && ((ToolItem) item).type == ToolType.Shears) {
cut = true;
dropItem(1, 3, Items.get("Wool"));
dropItem(1, 3, Items.get(color.toString().replace('_', ' ') + " Wool"));
((ToolItem) item).payDurability();
return true;
}
} else if (item instanceof DyeItem) {
color = ((DyeItem) item).color;
((DyeItem) item).count--;
return true;
}
return false;
}
Expand All @@ -80,7 +108,7 @@ public void die() {
max = 2;
}

if (!cut) dropItem(min, max, Items.get("wool"));
if (!cut) dropItem(min, max, Items.get(color.toString().replace('_', ' ') + " Wool"));
dropItem(min, max, Items.get("Raw Beef"));

super.die();
Expand Down
18 changes: 17 additions & 1 deletion src/client/java/minicraft/gfx/Font.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import minicraft.core.Renderer;
import minicraft.gfx.SpriteLinker.SpriteType;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -82,7 +83,22 @@ public static void drawBackground(String msg, Screen screen, int x, int y, int w
}

public static int textWidth(String text) { // Filtering out coloring codes.
return (int) (Math.max(text.length() - text.chars().filter(ch -> ch == Color.COLOR_CHAR).count() * 5, 0) * 8);
return Math.max(text.length() - countMatches(text, Color.COLOR_CHAR) * 5, 0) * 8;
}

// Source: Apache commons-lang lang3 StringUtils
private static int countMatches(@NotNull final CharSequence str, final char ch) {
if (str.length() == 0) {
return 0;
}
int count = 0;
// We could also call str.toCharArray() for faster lookups but that would generate more garbage.
for (int i = 0; i < str.length(); i++) {
if (ch == str.charAt(i)) {
count++;
}
}
return count;
}

public static int textWidth(String[] para) {
Expand Down
2 changes: 1 addition & 1 deletion src/client/java/minicraft/gfx/Screen.java
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ public double getOverlayOpacity(int currentLevel, double darkFactor) {
return 1;
} else {
// Outside the caves, not being lit simply means being darker.
return darkFactor / 128; // darkens the color one shade.
return darkFactor / 160; // darkens the color one shade.
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/client/java/minicraft/item/ClothingItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protected static ArrayList<Item> getAllInstances() {
items.add(new ClothingItem("Orange Clothes", new LinkedSprite(SpriteType.Item, "orange_clothes"), Color.get(1, 255, 102, 0)));
items.add(new ClothingItem("Purple Clothes", new LinkedSprite(SpriteType.Item, "purple_clothes"), Color.get(1, 102, 0, 153)));
items.add(new ClothingItem("Cyan Clothes", new LinkedSprite(SpriteType.Item, "cyan_clothes"), Color.get(1, 0, 102, 153)));
items.add(new ClothingItem("Reg Clothes", new LinkedSprite(SpriteType.Item, "reg_clothes"), Color.get(1, 51, 51, 0)));
items.add(new ClothingItem("Reg Clothes", new LinkedSprite(SpriteType.Item, "reg_clothes"), Color.get(1, 51, 51, 0))); // Dark Green

return items;
}
Expand Down
58 changes: 58 additions & 0 deletions src/client/java/minicraft/item/DyeItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package minicraft.item;

import minicraft.gfx.SpriteLinker;
import minicraft.util.MyUtils;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

public class DyeItem extends StackableItem {

protected static ArrayList<Item> getAllInstances() {
ArrayList<Item> items = new ArrayList<>();

for (DyeColor color : DyeColor.values()) {
items.add(new DyeItem(MyUtils.capitalizeFully(color.toString().replace('_', ' ')) + " Dye", new SpriteLinker.LinkedSprite(
SpriteLinker.SpriteType.Item, color.toString().toLowerCase() + "_dye"), color));
}

return items;
}

public final DyeColor color;

protected DyeItem(String name, SpriteLinker.LinkedSprite sprite, DyeColor color) {
super(name, sprite);
this.color = color;
}

public enum DyeColor {
BLACK(0x1_1D1D21),
RED(0x1_B02E26),
GREEN(0x1_5E7C16),
BROWN(0x1_835432),
BLUE(0x1_3C44AA),
PURPLE(0x1_8932B8),
CYAN(0x1_169C9C),
LIGHT_GRAY(0x1_9D9D97),
GRAY(0x1_474F52),
PINK(0x1_F38BAA),
LIME(0x1_80C71F),
YELLOW(0x1_FED83D),
LIGHT_BLUE(0x1_3AB3DA),
MAGENTA(0x1_C74EBD),
ORANGE(0x1_F9801D),
WHITE(0x1_F9FFFE);

public final int color;

DyeColor(int color) {
this.color = color;
}
}

@Override
public @NotNull DyeItem copy() {
return new DyeItem(getName(), sprite, color);
}
}
7 changes: 6 additions & 1 deletion src/client/java/minicraft/item/FurnitureItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,18 @@ protected static ArrayList<Item> getAllInstances() {
for (Crafter.Type type : Crafter.Type.values()) {
items.add(new FurnitureItem(new Crafter(type)));
}

// Add the various lanterns
for (Lantern.Type type : Lantern.Type.values()) {
items.add(new FurnitureItem(new Lantern(type)));
}

// Add the various colors of bed
for (DyeItem.DyeColor color : DyeItem.DyeColor.values()) {
items.add(new FurnitureItem(new Bed(color)));
}

items.add(new FurnitureItem(new Tnt()));
items.add(new FurnitureItem(new Bed()));
items.add(new FurnitureItem(new Composter()));

return items;
Expand Down
2 changes: 2 additions & 0 deletions src/client/java/minicraft/item/Items.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ private static void addAll(ArrayList<Item> items) {
addAll(SummonItem.getAllInstances());
addAll(HeartItem.getAllInstances());
addAll(WateringCanItem.getAllInstances());
addAll(DyeItem.getAllInstances());
addAll(WoolItem.getAllInstances());
addAll(EntitySummonItem.getAllInstances());
}

Expand Down
7 changes: 7 additions & 0 deletions src/client/java/minicraft/item/Recipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import minicraft.entity.mob.Player;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

Expand Down Expand Up @@ -114,4 +115,10 @@ public int hashCode() {
result = 31 * result + amount;
return result;
}

@Override
public String toString() {
return product + ":" + amount +
"[" + String.join(";", costs.entrySet().stream().<CharSequence>map(e -> e.getKey() + ":" + e.getValue())::iterator) + "]";
}
}
Loading

0 comments on commit 0cf3270

Please sign in to comment.