Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Repair Bench #532

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/client/java/minicraft/core/Renderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ private static void renderGui() {
if (player.activeItem instanceof ToolItem) {
// Draws the text
ToolItem tool = (ToolItem) player.activeItem;
int dura = tool.dur * 100 / (tool.type.durability * (tool.level + 1));
int dura = tool.dur * 100 / tool.MAX_DUR;
int green = (int) (dura * 2.55f); // Let duration show as normal.
Font.drawBackground(dura + "%", screen, 164, Screen.h - 16, Color.get(1, 255 - green, green, 0));
}
Expand Down
125 changes: 125 additions & 0 deletions src/client/java/minicraft/entity/furniture/DarkAnvil.java
BenCheung0422 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package minicraft.entity.furniture;

import minicraft.core.Game;
import minicraft.entity.mob.Player;
import minicraft.gfx.SpriteLinker;
import minicraft.item.Item;
import minicraft.item.Items;
import minicraft.item.StackableItem;
import minicraft.screen.DarkAnvilDisplay;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Range;

public class DarkAnvil extends Furniture {
private static final SpriteLinker.LinkedSprite sprite = new SpriteLinker.LinkedSprite(SpriteLinker.SpriteType.Entity, "dark_anvil");
private static final SpriteLinker.LinkedSprite itemSprite = new SpriteLinker.LinkedSprite(SpriteLinker.SpriteType.Item, "dark_anvil");

public static final int MAX_ENERGY = 8;

private int energy = 0;

private StackableItem fuelStore = null;

public DarkAnvil() {
super("Dark Anvil", sprite, itemSprite);
}
public DarkAnvil(int store, int energy) {
this();
this.energy = energy;
this.fuelStore = (StackableItem) Items.get("Cloud Ore");
fuelStore.count = store;
}

@Range(from = 0, to = MAX_ENERGY)
public int getEnergy() {
return energy;
}

/** Try to refill energy using cloud ore in storage.
* @return {@code true} if there was no energy left and successfully refilled */
public boolean tryRefillEnergy() {
if (energy == 0 && fuelStore != null) {
boolean success = false;
if (fuelStore.count > 0) {
fuelStore.count--;
energy = MAX_ENERGY;
success = true;
}

if (fuelStore.count == 0) fuelStore = null;
return success;
}

return false;
}

/** @see minicraft.screen.entry.SlotEntry.SynchronizedSlotEntry#withdrawSlot(boolean, int) */
public @Nullable StackableItem withdrawStore(boolean whole, @Range(from = 0, to = Integer.MAX_VALUE) int maxStackSize) {
return withdrawStore(whole ? maxStackSize : 1);
}
/** @see minicraft.screen.entry.SlotEntry.SynchronizedSlotEntry#withdrawSlot(int) */
public @Nullable StackableItem withdrawStore(int count) {
if (fuelStore == null) return null;
StackableItem item;
if (count >= fuelStore.count) {
item = fuelStore;
fuelStore = null;
} else {
item = fuelStore.copy();
item.count = Math.min(count, fuelStore.count);
fuelStore.count -= item.count;
if (fuelStore.isDepleted()) fuelStore = null;
}
return item;
}

@Range(from = 0, to = Integer.MAX_VALUE)
public int getStore() {
if (fuelStore == null) return 0;
return fuelStore.count;
}

/** @return {@code true} if 1 energy point is successfully deducted */
public boolean deduceEnergy() {
if (energy > 0) {
energy--;
return true;
}

return false;
}

/** @return {@code true} if the provided {@code item} is not depleted
* @see minicraft.screen.entry.SlotEntry.SynchronizedSlotEntry#depositSlot(Item) */
public boolean depositStore(StackableItem item) {
if (item.getName().equalsIgnoreCase("Cloud Ore")) {
if (!item.isDepleted()) {
if (fuelStore == null) {
fuelStore = item.copy();
fuelStore.count = Math.min(item.count, fuelStore.maxCount);
item.count -= fuelStore.count;
} else {
int toAdd = Math.min(item.count, fuelStore.maxCount - fuelStore.count);
fuelStore.count += toAdd;
item.count -= toAdd;
}
}

return !item.isDepleted();
}

return true;
}

@Override
public boolean use(Player player) {
Game.setDisplay(new DarkAnvilDisplay(this, player));
return true;
}

@Override
public @NotNull Furniture copy() {
return new DarkAnvil();
}
}
27 changes: 27 additions & 0 deletions src/client/java/minicraft/entity/furniture/RepairBench.java
BenCheung0422 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package minicraft.entity.furniture;

import minicraft.core.Game;
import minicraft.entity.mob.Player;
import minicraft.gfx.SpriteLinker;
import minicraft.screen.RepairBenchDisplay;
import org.jetbrains.annotations.NotNull;

public class RepairBench extends Furniture {
private static final SpriteLinker.LinkedSprite sprite = new SpriteLinker.LinkedSprite(SpriteLinker.SpriteType.Entity, "repair_bench");
private static final SpriteLinker.LinkedSprite itemSprite = new SpriteLinker.LinkedSprite(SpriteLinker.SpriteType.Item, "repair_bench");

public RepairBench() {
super("Repair Bench", sprite, itemSprite);
}

@Override
public boolean use(Player player) {
Game.setDisplay(new RepairBenchDisplay(this, player));
return true;
}

@Override
public @NotNull Furniture copy() {
return new RepairBench();
}
}
8 changes: 8 additions & 0 deletions src/client/java/minicraft/entity/mob/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,10 @@ protected void attack() {
activeItem.interactOn(Tiles.get("rock"), level, 0, 0, this, attackDir);
if (activeItem.isDepleted()) {
activeItem = null;
if (isFishing) {
isFishing = false;
fishingTicks = maxFishingTicks;
}
}
return;
}
Expand Down Expand Up @@ -663,6 +667,10 @@ protected void attack() {
if (activeItem.isDepleted()) {
// If the activeItem has 0 items left, then "destroy" it.
activeItem = null;
if (isFishing) {
isFishing = false;
fishingTicks = maxFishingTicks;
}
}
}
if (done) return; // Skip the rest if interaction was handled
Expand Down
4 changes: 2 additions & 2 deletions src/client/java/minicraft/item/FishingRodItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected static ArrayList<Item> getAllInstances() {

return items;
}
private int uses = 0; // The more uses, the higher the chance of breaking
public int uses = 0; // The more uses, the higher the chance of breaking
public int level; // The higher the level the lower the chance of breaking

private Random random = new Random();
Expand All @@ -39,7 +39,7 @@ protected static ArrayList<Item> getAllInstances() {
{79, 69, 59, 4} // Gem has very high chance of rare items
};

private static final String[] LEVEL_NAMES = {
public static final String[] LEVEL_NAMES = {
"Wood",
"Iron",
"Gold",
Expand Down
4 changes: 4 additions & 0 deletions src/client/java/minicraft/item/FurnitureItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import minicraft.entity.furniture.Bed;
import minicraft.entity.furniture.Chest;
import minicraft.entity.furniture.Crafter;
import minicraft.entity.furniture.DarkAnvil;
import minicraft.entity.furniture.DungeonChest;
import minicraft.entity.furniture.Furniture;
import minicraft.entity.furniture.Lantern;
import minicraft.entity.furniture.RepairBench;
import minicraft.entity.furniture.Spawner;
import minicraft.entity.furniture.Tnt;
import minicraft.entity.mob.Cow;
Expand Down Expand Up @@ -57,6 +59,8 @@ protected static ArrayList<Item> getAllInstances() {

items.add(new FurnitureItem(new Tnt()));
items.add(new FurnitureItem(new Bed()));
items.add(new FurnitureItem(new DarkAnvil()));
items.add(new FurnitureItem(new RepairBench()));

return items;
}
Expand Down
2 changes: 2 additions & 0 deletions src/client/java/minicraft/item/Recipes.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ public class Recipes {
anvilRecipes.add(new Recipe("Gem Shovel_1", "Wood_5", "gem_50"));
anvilRecipes.add(new Recipe("Gem Bow_1", "Wood_5", "gem_50", "string_2"));
anvilRecipes.add(new Recipe("Shears_1", "Iron_4"));
anvilRecipes.add(new Recipe("Dark Anvil_1", "Cloud Ore_5", "Shard_10", "Iron_5", "Raw Obsidian_4"));
anvilRecipes.add(new Recipe("Repair Bench_1", "Wood_12", "Iron_8", "Lapis_2", "Stone_4"));

furnaceRecipes.add(new Recipe("iron_1", "iron Ore_4", "coal_1"));
furnaceRecipes.add(new Recipe("gold_1", "gold Ore_4", "coal_1"));
Expand Down
5 changes: 3 additions & 2 deletions src/client/java/minicraft/item/ToolItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ protected static ArrayList<Item> getAllInstances() {
private Random random = new Random();

public static final String[] LEVEL_NAMES = {"Wood", "Rock", "Iron", "Gold", "Gem"}; // The names of the different levels. A later level means a stronger tool.
public final int MAX_DUR;

public ToolType type; // Type of tool (Sword, hoe, axe, pickaxe, shovel)
public int level; // Level of said tool
Expand All @@ -50,14 +51,14 @@ public ToolItem(ToolType type, int level) {
this.level = level;
this.damage = level * 5 + 10;

dur = type.durability * (level + 1); // Initial durability fetched from the ToolType
dur = MAX_DUR = type.durability * (level + 1); // Initial durability fetched from the ToolType
}

public ToolItem(ToolType type) {
super(type.name(), new LinkedSprite(SpriteType.Item, getSpriteName(type.toString(), "")));

this.type = type;
dur = type.durability;
dur = MAX_DUR = type.durability;
}

/** Gets the name of this tool (and it's type) as a display string. */
Expand Down
6 changes: 6 additions & 0 deletions src/client/java/minicraft/saveload/Load.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
import minicraft.entity.furniture.Bed;
import minicraft.entity.furniture.Chest;
import minicraft.entity.furniture.Crafter;
import minicraft.entity.furniture.DarkAnvil;
import minicraft.entity.furniture.DeathChest;
import minicraft.entity.furniture.DungeonChest;
import minicraft.entity.furniture.KnightStatue;
import minicraft.entity.furniture.Lantern;
import minicraft.entity.furniture.RepairBench;
import minicraft.entity.furniture.Spawner;
import minicraft.entity.furniture.Tnt;
import minicraft.entity.mob.AirWizard;
Expand Down Expand Up @@ -1067,6 +1069,8 @@ public static Entity loadEntity(String entityData, Version worldVer, boolean isL
} else if (newEntity instanceof KnightStatue) {
int health = Integer.parseInt(info.get(2));
newEntity = new KnightStatue(health);
} else if (newEntity instanceof DarkAnvil) {
newEntity = new DarkAnvil(Integer.parseInt(info.get(2)), Integer.parseInt(info.get(3)));
}

if (!isLocalSave) {
Expand Down Expand Up @@ -1145,6 +1149,8 @@ private static Entity getEntity(String string, int mobLevel) {
case "TextParticle": return new TextParticle("", 0, 0, 0);
case "KnightStatue": return new KnightStatue(0);
case "ObsidianKnight": return new ObsidianKnight(0);
case "DarkAnvil": return new DarkAnvil();
case "RepairBench": return new RepairBench();
default : Logging.SAVELOAD.error("LOAD ERROR: Unknown or outdated entity requested: " + string);
return null;
}
Expand Down
6 changes: 6 additions & 0 deletions src/client/java/minicraft/saveload/Save.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import minicraft.entity.Spark;
import minicraft.entity.furniture.Chest;
import minicraft.entity.furniture.Crafter;
import minicraft.entity.furniture.DarkAnvil;
import minicraft.entity.furniture.DeathChest;
import minicraft.entity.furniture.DungeonChest;
import minicraft.entity.furniture.KnightStatue;
Expand Down Expand Up @@ -396,6 +397,11 @@ else if (e instanceof Sheep)
extradata.append(":").append(((KnightStatue) e).getBossHealth());
}

if (e instanceof DarkAnvil) {
extradata.append(":").append(((DarkAnvil) e).getEnergy());
extradata.append(":").append(((DarkAnvil) e).getStore());
}

if (!isLocalSave) {
if (e instanceof ItemEntity) extradata.append(":").append(((ItemEntity) e).getData());
if (e instanceof Arrow) extradata.append(":").append(((Arrow) e).getData());
Expand Down
Loading