Skip to content

Commit

Permalink
Tools can be repaired, various code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
wothers committed May 22, 2021
1 parent cdf28f5 commit ca55b7d
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 42 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ Optional for all items: "isFireproof" field (true or false) - is the item immune

Optional for food items: "isSnack" field (true or false) - is the food consumed quickly.

Optional for tool items: "repairItem" field - the item that can repair the tool with an Anvil.

## Basic Items:

```
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx1G
loader_version=0.11.2

# Mod Properties
mod_version = 1.6.0
mod_version = 1.6.1
maven_group = wothers.ift
archives_base_name = items-from-text

Expand Down
13 changes: 12 additions & 1 deletion src/main/java/wothers/hr/items/HyperFood.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,25 @@
import net.minecraft.item.ItemGroup;

public class HyperFood extends HyperItem {
private final int hunger;
private final float saturation;
private final boolean isSnack;

public HyperFood(int maxStackSize, int hunger, float saturation, boolean isSnack, boolean isHandheld, boolean isFireproof) {
super(maxStackSize, isHandheld, isFireproof);
this.hunger = hunger;
this.saturation = saturation;
this.isSnack = isSnack;
}

@Override
public Item getItem() {
Item.Settings settings = new Item.Settings().group(ItemGroup.FOOD).maxCount(maxStackSize);
if (isFireproof)
settings = settings.fireproof();
FoodComponent.Builder builder = new FoodComponent.Builder().hunger(hunger).saturationModifier(saturation);
if (isSnack)
builder = builder.snack();
item = new Item(settings.food(builder.build()));
return new Item(settings.food(builder.build()));
}
}
16 changes: 8 additions & 8 deletions src/main/java/wothers/hr/items/HyperItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@

public class HyperItem {
public final boolean isHandheld;
protected Item item;
protected final int maxStackSize;
protected final boolean isFireproof;

public HyperItem(int maxStackSize, boolean isHandheld, boolean isFireproof) {
this.isHandheld = isHandheld;
if (maxStackSize < 1 || maxStackSize > 64)
throw new RuntimeException("Invalid item stack size - should be at least 1 and no more than 64");
else {
Item.Settings settings = new Item.Settings().group(ItemGroup.MISC).maxCount(maxStackSize);
if (isFireproof)
settings = settings.fireproof();
item = new Item(settings);
}
this.maxStackSize = maxStackSize;
this.isFireproof = isFireproof;
}

public Item getItem() {
return item;
Item.Settings settings = new Item.Settings().group(ItemGroup.MISC).maxCount(maxStackSize);
if (isFireproof)
settings = settings.fireproof();
return new Item(settings);
}
}
34 changes: 21 additions & 13 deletions src/main/java/wothers/hr/items/HyperTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,23 @@
import net.minecraft.item.SwordItem;
import net.minecraft.item.ToolMaterial;
import net.minecraft.recipe.Ingredient;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;

public class HyperTool extends HyperItem {
public HyperTool(String toolType, float miningSpeed, int miningLevel, float attackSpeed, int attackDamage, int durability, int enchantability, boolean isFireproof) {
private final String toolType;
private final int attackDamage;
private final float attackSpeed;
private final ToolMaterial customToolMaterial;

public HyperTool(String toolType, float miningSpeed, int miningLevel, float attackSpeed, int attackDamage, int durability, int enchantability, String repairItem, boolean isFireproof) {
super(1, true, isFireproof);
this.toolType = toolType;
this.attackDamage = attackDamage;
attackSpeed -= 4;
this.attackSpeed = attackSpeed;

ToolMaterial customToolMaterial = new ToolMaterial() {
customToolMaterial = new ToolMaterial() {
@Override
public int getDurability() {
return durability;
Expand Down Expand Up @@ -43,31 +53,29 @@ public int getEnchantability() {

@Override
public Ingredient getRepairIngredient() {
return null;
return repairItem != null ? Ingredient.ofItems(Registry.ITEM.get(new Identifier(repairItem))) : Ingredient.EMPTY;
}
};
}

@Override
public Item getItem() {
Item.Settings settings = new Item.Settings().group(ItemGroup.TOOLS);
if (isFireproof)
settings = settings.fireproof();

switch (toolType) {
case "pickaxe":
item = new CustomPickaxeItem(customToolMaterial, attackDamage / 2, attackSpeed, settings);
break;
return new CustomPickaxeItem(customToolMaterial, attackDamage / 2, attackSpeed, settings);
case "axe":
item = new CustomAxeItem(customToolMaterial, attackDamage / 2, attackSpeed, settings);
break;
return new CustomAxeItem(customToolMaterial, attackDamage / 2, attackSpeed, settings);
case "shovel":
item = new ShovelItem(customToolMaterial, attackDamage / 2, attackSpeed, settings);
break;
return new ShovelItem(customToolMaterial, attackDamage / 2, attackSpeed, settings);
case "hoe":
item = new CustomHoeItem(customToolMaterial, attackDamage / 2, attackSpeed, settings);
break;
return new CustomHoeItem(customToolMaterial, attackDamage / 2, attackSpeed, settings);
case "sword":
settings = settings.group(ItemGroup.COMBAT);
item = new SwordItem(customToolMaterial, attackDamage / 2, attackSpeed, settings);
break;
return new SwordItem(customToolMaterial, attackDamage / 2, attackSpeed, settings);
default:
throw new RuntimeException("Invalid tool type - only pickaxe, axe, shovel, hoe or sword are permitted");
}
Expand Down
24 changes: 5 additions & 19 deletions src/main/java/wothers/ift/ItemsFromText.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package wothers.ift;

import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand All @@ -24,14 +22,8 @@ public class ItemsFromText implements ModInitializer {
@Override
public void onInitialize() {
File[] subDirectories = {};
if (MAIN_FOLDER.toFile().exists()) {
subDirectories = MAIN_FOLDER.toFile().listFiles(new FileFilter() {
@Override
public boolean accept(File file) {
return file.isDirectory();
}
});
}
if (MAIN_FOLDER.toFile().exists())
subDirectories = MAIN_FOLDER.toFile().listFiles(File::isDirectory);

try {
parseItems(MAIN_FOLDER);
Expand All @@ -47,14 +39,8 @@ private void parseItems(Path path) throws IOException {
String namespaceName = path.toFile().getName();

File[] txtFiles = {};
if (path.toFile().exists()) {
txtFiles = path.toFile().listFiles(new FilenameFilter() {
@Override
public boolean accept(File f, String s) {
return s.endsWith(".txt");
}
});
}
if (path.toFile().exists())
txtFiles = path.toFile().listFiles((file, string) -> string.endsWith(".txt"));

for (File file : txtFiles) {
Properties properties = new Properties();
Expand All @@ -69,7 +55,7 @@ public boolean accept(File f, String s) {
item = new HyperFood(Integer.parseInt(properties.getProperty("stack")), Integer.parseInt(properties.getProperty("hunger")), Float.parseFloat(properties.getProperty("saturation")), Boolean.parseBoolean(properties.getProperty("isSnack")), Boolean.parseBoolean(properties.getProperty("isHandheld")), Boolean.parseBoolean(properties.getProperty("isFireproof")));
break;
case "tool":
item = new HyperTool(properties.getProperty("toolType"), Float.parseFloat(properties.getProperty("miningSpeed")), Integer.parseInt(properties.getProperty("miningLevel")), Float.parseFloat(properties.getProperty("attackSpeed")), Integer.parseInt(properties.getProperty("attackDamage")), Integer.parseInt(properties.getProperty("durability")), Integer.parseInt(properties.getProperty("enchantability")), Boolean.parseBoolean(properties.getProperty("isFireproof")));
item = new HyperTool(properties.getProperty("toolType"), Float.parseFloat(properties.getProperty("miningSpeed")), Integer.parseInt(properties.getProperty("miningLevel")), Float.parseFloat(properties.getProperty("attackSpeed")), Integer.parseInt(properties.getProperty("attackDamage")), Integer.parseInt(properties.getProperty("durability")), Integer.parseInt(properties.getProperty("enchantability")), properties.getProperty("repairItem"), Boolean.parseBoolean(properties.getProperty("isFireproof")));
break;
}
} else {
Expand Down

0 comments on commit ca55b7d

Please sign in to comment.