Skip to content

Commit

Permalink
Integrated soft tools using the new ExtraToolProperty
Browse files Browse the repository at this point in the history
  • Loading branch information
littlecube8152 committed Jan 6, 2025
1 parent b180442 commit 05e56fc
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 66 deletions.
96 changes: 44 additions & 52 deletions src/main/java/gregtech/api/items/toolitem/IGTTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import gregtech.api.unification.material.Materials;
import gregtech.api.unification.material.properties.DustProperty;
import gregtech.api.unification.material.properties.PropertyKey;
import gregtech.api.unification.material.properties.SimpleToolProperty;
import gregtech.api.unification.material.properties.ToolProperty;
import gregtech.api.unification.ore.OrePrefix;
import gregtech.api.unification.stack.UnificationEntry;
Expand Down Expand Up @@ -151,19 +152,16 @@ default ItemStack getRaw() {
}

/**
* This is a safer wrapper for materials without toolProperty properly set. In this case, the missing data is
* inferred from the referenceStack. This method supports specially made tools that is registered separately.
*
* @return A tool made from the given material
* @return A tool made from the given material. The tool property (at least overriding) for the material must be
* set.
*/
default ItemStack get(Material material, ItemStack referenceStack) {
default ItemStack get(Material material) {
ItemStack stack = new ItemStack(get());

NBTTagCompound stackCompound = GTUtility.getOrCreateNbtCompound(stack);
stackCompound.setBoolean(DISALLOW_CONTAINER_ITEM_KEY, false);

NBTTagCompound toolTag = getToolTag(stack);
NBTTagCompound referenceToolTag = getToolTag(referenceStack);
IGTToolDefinition toolStats = getToolStats();

// don't show the normal vanilla damage and attack speed tooltips,
Expand All @@ -177,55 +175,56 @@ default ItemStack get(Material material, ItemStack referenceStack) {
AoESymmetrical aoeDefinition = getToolStats().getAoEDefinition(stack);

// Set other tool stats (durability)
ToolProperty toolProperty = material.getProperty(PropertyKey.TOOL);

System.out.println(this.getToolId());
SimpleToolProperty finalToolProperty;
{
ToolProperty toolProperty = material.getProperty(PropertyKey.TOOL);
if (material.hasProperty(PropertyKey.EXTRATOOL)) {
System.out.println("Find Override Property!");
System.out.println(material);
finalToolProperty = material.getProperty(PropertyKey.EXTRATOOL)
.getOverriddenResult(this.getToolId(), toolProperty);
} else {
finalToolProperty = material.getProperty(PropertyKey.TOOL);
}
}
// Durability formula we are working with:
// Final Durability = (material durability * material durability multiplier) + (tool definition durability *
// definition durability multiplier) - 1
// Subtracts 1 internally since Minecraft treats "0" as a valid durability, but we don't want to display this.

int durability;
boolean isUnbreakable;
if (toolProperty != null) {
// Tool material modifiers.
durability = toolProperty.getToolDurability() * toolProperty.getDurabilityMultiplier();

// Tool type modifiers.
// Most Tool Definitions do not set a base durability, which will lead to ignoring the multiplier if present. So
// apply the multiplier to the material durability if that would happen
if (toolStats.getBaseDurability(stack) == 0) {
durability = (int) (durability * toolStats.getDurabilityMultiplier(stack));
} else {
durability += (int) (toolStats.getBaseDurability(stack) * toolStats.getDurabilityMultiplier(stack));
}
durability--; // Adjust for MC

// Get unbreakability
isUnbreakable = toolProperty.getUnbreakable();

// Set tool and material enchantments
Object2IntMap<Enchantment> enchantments = new Object2IntOpenHashMap<>();
toolProperty.getEnchantments().forEach((enchantment, level) -> enchantments.put(enchantment,
level.getLevel(toolProperty.getToolHarvestLevel())));
toolStats.getDefaultEnchantments(stack).forEach((enchantment, level) -> enchantments.put(enchantment,
level.getLevel(toolProperty.getToolHarvestLevel())));
enchantments.forEach((enchantment, level) -> {
if (stack.getItem().canApplyAtEnchantingTable(stack, enchantment)) {
stack.addEnchantment(enchantment, level);
}
});
// Tool material modifiers.
int durability = finalToolProperty.getToolDurability() * finalToolProperty.getDurabilityMultiplier();

// Tool type modifiers.
// Most Tool Definitions do not set a base durability, which will lead to ignoring the multiplier if present. So
// apply the multiplier to the material durability if that would happen
if (toolStats.getBaseDurability(stack) == 0) {
durability = (int) (durability * toolStats.getDurabilityMultiplier(stack));
} else {
// In this case, simply get from the reference tool
durability = referenceToolTag.getInteger(MAX_DURABILITY_KEY);
isUnbreakable = referenceToolTag.getBoolean(UNBREAKABLE_KEY);
durability += (int) (toolStats.getBaseDurability(stack) * toolStats.getDurabilityMultiplier(stack));
}

if (isUnbreakable) {
stackCompound.setBoolean(UNBREAKABLE_KEY, true);
}
durability -= 1; // Finally adjust for MC
toolTag.setInteger(MAX_DURABILITY_KEY, durability);
toolTag.setInteger(DURABILITY_KEY, 0);

if (finalToolProperty.getUnbreakable()) {
stackCompound.setBoolean(UNBREAKABLE_KEY, true);
}

// Set tool and material enchantments
Object2IntMap<Enchantment> enchantments = new Object2IntOpenHashMap<>();
finalToolProperty.getEnchantments().forEach((enchantment, level) -> enchantments.put(enchantment,
level.getLevel(finalToolProperty.getToolHarvestLevel())));
toolStats.getDefaultEnchantments(stack).forEach((enchantment, level) -> enchantments.put(enchantment,
level.getLevel(finalToolProperty.getToolHarvestLevel())));
enchantments.forEach((enchantment, level) -> {
if (stack.getItem().canApplyAtEnchantingTable(stack, enchantment)) {
stack.addEnchantment(enchantment, level);
}
});

// Set behaviours
NBTTagCompound behaviourTag = getBehaviorsTag(stack);
getToolStats().getBehaviors().forEach(behavior -> behavior.addBehaviorNBT(stack, behaviourTag));
Expand All @@ -239,20 +238,13 @@ default ItemStack get(Material material, ItemStack referenceStack) {
behaviourTag.setInteger(AOE_LAYER_KEY, aoeDefinition.layer);
}

if (toolProperty != null && toolProperty.isMagnetic()) {
if (finalToolProperty.isMagnetic()) {
behaviourTag.setBoolean(RELOCATE_MINED_BLOCKS_KEY, true);
}

return stack;
}

/**
* @return A tool made from the given material. The tool property for the material must be set.
*/
default ItemStack get(Material material) {
return get(material, ItemStack.EMPTY);
}

default ItemStack get(Material material, long defaultCharge, long defaultMaxCharge) {
ItemStack stack = get(material);
if (isElectric()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ public static void register() {
Materials.DyeRed, Materials.DyeBlack
};

// Register soft tools
SoftToolAddition.register();

OrePrefix.init();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package gregtech.api.unification.material.materials;

import gregtech.api.items.toolitem.ToolClasses;
import gregtech.api.recipes.ModHandler;
import gregtech.api.unification.material.properties.ExtraToolProperty;
import gregtech.api.unification.material.properties.PropertyKey;

import gregtech.api.unification.material.Material;
import gregtech.api.unification.material.Materials;

public class SoftToolAddition {

public static final Material[] softMaterials = new Material[] {
Materials.Wood, Materials.Rubber, Materials.Polyethylene,
Materials.Polytetrafluoroethylene, Materials.Polybenzimidazole
};

private static ExtraToolProperty ensureExtraToolProperty(Material material) {
if (material.getProperty(PropertyKey.EXTRATOOL) == null)
material.setProperty(PropertyKey.EXTRATOOL, new ExtraToolProperty());
return material.getProperty(PropertyKey.EXTRATOOL);
}


public static void register() {
for (int i = 0; i < softMaterials.length; i++) {

Material material = softMaterials[i];

if (ModHandler.isMaterialWood(material)) {
ensureExtraToolProperty(material).setOverrideProperty(ToolClasses.SOFT_MALLET,
ExtraToolProperty.Builder.of(4F, 1F, 48, 1).build());
} else {
ExtraToolProperty extraToolProperty = ensureExtraToolProperty(material);
extraToolProperty.setOverrideProperty(ToolClasses.SOFT_MALLET,
ExtraToolProperty.Builder.of(4F, 1F, 128 * (1 << i), 1).build());
extraToolProperty.setOverrideProperty(ToolClasses.PLUNGER,
ExtraToolProperty.Builder.of(4F, 0F, 128 * (1 << i), 1).build());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import gregtech.api.capability.IElectricItem;
import gregtech.api.items.metaitem.MetaItem.MetaValueItem;
import gregtech.api.items.toolitem.IGTTool;
import gregtech.api.items.toolitem.ToolHelper;
import gregtech.api.recipes.ModHandler;
import gregtech.api.unification.OreDictUnifier;
import gregtech.api.unification.material.MarkerMaterials;
import gregtech.api.unification.material.Material;
import gregtech.api.unification.material.Materials;
import gregtech.api.unification.material.materials.SoftToolAddition;
import gregtech.api.unification.material.properties.PropertyKey;
import gregtech.api.unification.material.properties.ToolProperty;
import gregtech.api.unification.ore.OrePrefix;
Expand Down Expand Up @@ -406,31 +406,23 @@ private static void registerMortarRecipes() {
}

private static void registerSoftToolRecipes() {
final Material[] softMaterials = new Material[] {
Materials.Wood, Materials.Rubber, Materials.Polyethylene,
Materials.Polytetrafluoroethylene, Materials.Polybenzimidazole
};
final Material[] softMaterials = SoftToolAddition.softMaterials;

final UnificationEntry stick = new UnificationEntry(OrePrefix.stick, Materials.Wood);

for (int i = 0; i < softMaterials.length; i++) {
Material material = softMaterials[i];

for (Material material : softMaterials) {
if (ModHandler.isMaterialWood(material)) {
ModHandler.addMirroredShapedRecipe(String.format("soft_mallet_%s", material),
ToolHelper.getAndSetToolData(ToolItems.SOFT_MALLET, material, 47, 1, 4F, 1F),
addToolRecipe(material, ToolItems.SOFT_MALLET, true,
"II ", "IIS", "II ",
'I', new UnificationEntry(OrePrefix.plank, material),
'S', stick);
} else {
ModHandler.addMirroredShapedRecipe(String.format("soft_mallet_%s", material),
ToolHelper.getAndSetToolData(ToolItems.SOFT_MALLET, material, 128 * (1 << i) - 1, 1, 4F, 1F),
addToolRecipe(material, ToolItems.SOFT_MALLET, true,
"II ", "IIS", "II ",
'I', new UnificationEntry(OrePrefix.ingot, material),
'S', stick);

ModHandler.addMirroredShapedRecipe(String.format("plunger_%s", material),
ToolHelper.getAndSetToolData(ToolItems.PLUNGER, material, 128 * (i << 1) - 1, 1, 4F, 0F),
addToolRecipe(material, ToolItems.PLUNGER, true,
"xPP", " SP", "S f",
'P', new UnificationEntry(OrePrefix.plate, material),
'S', stick);
Expand Down

0 comments on commit 05e56fc

Please sign in to comment.