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

Added property for specially registered tools #2693

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
63 changes: 43 additions & 20 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 @@ -150,6 +151,10 @@ default ItemStack getRaw() {
return stack;
}

/**
* @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 stack = new ItemStack(get());

Expand All @@ -170,35 +175,38 @@ default ItemStack get(Material material) {
AoESymmetrical aoeDefinition = getToolStats().getAoEDefinition(stack);

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

SimpleToolProperty finalToolProperty = getToolProperty(material);
// 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 = toolProperty.getToolDurability() * toolProperty.getDurabilityMultiplier();
// 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 *= toolStats.getDurabilityMultiplier(stack);
durability = (int) (durability * toolStats.getDurabilityMultiplier(stack));
} else {
durability += toolStats.getBaseDurability(stack) * toolStats.getDurabilityMultiplier(stack);
durability += (int) (toolStats.getBaseDurability(stack) * toolStats.getDurabilityMultiplier(stack));
}

toolTag.setInteger(MAX_DURABILITY_KEY, durability - 1);
durability -= 1; // Finally adjust for MC
toolTag.setInteger(MAX_DURABILITY_KEY, durability);
toolTag.setInteger(DURABILITY_KEY, 0);
if (toolProperty.getUnbreakable()) {

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

// Set tool and material enchantments
Object2IntMap<Enchantment> enchantments = new Object2IntOpenHashMap<>();
toolProperty.getEnchantments().forEach((enchantment, level) -> enchantments.put(enchantment,
level.getLevel(toolProperty.getToolHarvestLevel())));
finalToolProperty.getEnchantments().forEach((enchantment, level) -> enchantments.put(enchantment,
level.getLevel(finalToolProperty.getToolHarvestLevel())));
toolStats.getDefaultEnchantments(stack).forEach((enchantment, level) -> enchantments.put(enchantment,
level.getLevel(toolProperty.getToolHarvestLevel())));
level.getLevel(finalToolProperty.getToolHarvestLevel())));
enchantments.forEach((enchantment, level) -> {
if (stack.getItem().canApplyAtEnchantingTable(stack, enchantment)) {
stack.addEnchantment(enchantment, level);
Expand All @@ -218,7 +226,7 @@ default ItemStack get(Material material) {
behaviourTag.setInteger(AOE_LAYER_KEY, aoeDefinition.layer);
}

if (toolProperty.isMagnetic()) {
if (finalToolProperty.isMagnetic()) {
behaviourTag.setBoolean(RELOCATE_MINED_BLOCKS_KEY, true);
}

Expand Down Expand Up @@ -253,8 +261,23 @@ default Material getToolMaterial(ItemStack stack) {
}

@Nullable
default ToolProperty getToolProperty(ItemStack stack) {
return getToolMaterial(stack).getProperty(PropertyKey.TOOL);
default SimpleToolProperty getToolProperty(Material material) {
SimpleToolProperty finalToolProperty;
{
ToolProperty toolProperty = material.getProperty(PropertyKey.TOOL);
if (material.hasProperty(PropertyKey.EXTRATOOL)) {
finalToolProperty = material.getProperty(PropertyKey.EXTRATOOL)
.getOverriddenResult(this.getToolId(), toolProperty);
} else {
finalToolProperty = toolProperty;
}
}
return finalToolProperty;
}

@Nullable
default SimpleToolProperty getToolProperty(ItemStack stack) {
return getToolProperty(getToolMaterial(stack));
}

@Nullable
Expand All @@ -263,32 +286,32 @@ default DustProperty getDustProperty(ItemStack stack) {
}

default float getMaterialToolSpeed(ItemStack stack) {
ToolProperty toolProperty = getToolProperty(stack);
SimpleToolProperty toolProperty = getToolProperty(stack);
return toolProperty == null ? 0F : toolProperty.getToolSpeed();
}

default float getMaterialAttackDamage(ItemStack stack) {
ToolProperty toolProperty = getToolProperty(stack);
SimpleToolProperty toolProperty = getToolProperty(stack);
return toolProperty == null ? 0F : toolProperty.getToolAttackDamage();
}

default float getMaterialAttackSpeed(ItemStack stack) {
ToolProperty toolProperty = getToolProperty(stack);
SimpleToolProperty toolProperty = getToolProperty(stack);
return toolProperty == null ? 0F : toolProperty.getToolAttackSpeed();
}

default int getMaterialDurability(ItemStack stack) {
ToolProperty toolProperty = getToolProperty(stack);
SimpleToolProperty toolProperty = getToolProperty(stack);
return toolProperty == null ? 0 : toolProperty.getToolDurability() * toolProperty.getDurabilityMultiplier();
}

default int getMaterialEnchantability(ItemStack stack) {
ToolProperty toolProperty = getToolProperty(stack);
SimpleToolProperty toolProperty = getToolProperty(stack);
return toolProperty == null ? 0 : toolProperty.getToolEnchantability();
}

default int getMaterialHarvestLevel(ItemStack stack) {
ToolProperty toolProperty = getToolProperty(stack);
SimpleToolProperty toolProperty = getToolProperty(stack);
return toolProperty == null ? 0 : toolProperty.getToolHarvestLevel();
}

Expand Down Expand Up @@ -849,7 +872,7 @@ default AoESymmetrical getAoEDefinition(ItemStack stack) {
}
}

ToolProperty property = getToolProperty(stack);
SimpleToolProperty property = getToolProperty(stack);
if (property == null) return false;

// Check for any special enchantments specified by the material of this Tool
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/gregtech/api/unification/material/Material.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import gregtech.api.unification.material.info.MaterialIconSet;
import gregtech.api.unification.material.properties.BlastProperty;
import gregtech.api.unification.material.properties.DustProperty;
import gregtech.api.unification.material.properties.ExtraToolProperty;
import gregtech.api.unification.material.properties.FluidPipeProperties;
import gregtech.api.unification.material.properties.FluidProperty;
import gregtech.api.unification.material.properties.GemProperty;
Expand Down Expand Up @@ -936,6 +937,15 @@ public Builder toolStats(ToolProperty toolProperty) {
return this;
}

/**
* Use {@link ExtraToolProperty.Builder} to create a Tool Property Override.
*/
public Builder overrideToolStats(String toolId, ExtraToolProperty.OverrideToolProperty toolProperty) {
properties.ensureSet(PropertyKey.EXTRATOOL);
properties.getProperty(PropertyKey.EXTRATOOL).setOverrideProperty(toolId, toolProperty);
return this;
}

public Builder rotorStats(float speed, float damage, int durability) {
properties.setProperty(PropertyKey.ROTOR, new RotorProperty(speed, damage, durability));
return this;
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,39 @@
package gregtech.api.unification.material.materials;

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

public class SoftToolAddition {

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

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

Material material = softMaterials[i];

if (ModHandler.isMaterialWood(material)) {
material.getProperties().ensureSet(PropertyKey.EXTRATOOL);
ExtraToolProperty extraToolProperty = material.getProperty(PropertyKey.EXTRATOOL);

extraToolProperty.setOverrideProperty(ToolClasses.SOFT_MALLET,
ExtraToolProperty.Builder.of(4F, 1F, 48, 1).build());
} else {
material.getProperties().ensureSet(PropertyKey.EXTRATOOL);
ExtraToolProperty extraToolProperty = material.getProperty(PropertyKey.EXTRATOOL);

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
@@ -0,0 +1,164 @@
package gregtech.api.unification.material.properties;

import net.minecraft.enchantment.Enchantment;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.Map;

public class ExtraToolProperty implements IMaterialProperty {

/**
* Special override for certain kinds of tools.
*/
private final Map<String, OverrideToolProperty> overrideMap;

public static class OverrideToolProperty extends SimpleToolProperty {

public OverrideToolProperty() {
this.setToolSpeed(Float.NaN);
this.setToolAttackSpeed(Float.NaN);
this.setToolAttackDamage(Float.NaN);
this.setToolDurability(-1);
this.setToolHarvestLevel(-1);
this.setToolEnchantability(-1);
this.setDurabilityMultiplier(-1);
}

// It does not make much sense to set these overrides:
public void setShouldIgnoreCraftingTools(boolean ignore) {
throw new UnsupportedOperationException();
}

public void setUnbreakable(boolean isUnbreakable) {
throw new UnsupportedOperationException();
}

public boolean isMagnetic() {
throw new UnsupportedOperationException();
}

private SimpleToolProperty override(@NotNull SimpleToolProperty property) {
// copy to prevent the previous map is produced
SimpleToolProperty result = new SimpleToolProperty(property);

// Set the floating point number fields
if (!Float.isNaN(this.getToolSpeed()))
result.setToolSpeed(this.getToolSpeed());
if (!Float.isNaN(this.getToolAttackSpeed()))
result.setToolAttackSpeed(this.getToolAttackSpeed());
if (!Float.isNaN(this.getToolAttackDamage()))
result.setToolAttackDamage(this.getToolAttackDamage());

// Set the integer fields
if (this.getToolDurability() != -1)
result.setToolDurability(this.getToolDurability());
if (this.getToolHarvestLevel() != -1)
result.setToolHarvestLevel(this.getToolHarvestLevel());
if (this.getToolEnchantability() != -1)
result.setToolEnchantability(this.getToolEnchantability());
if (this.getDurabilityMultiplier() != -1)
result.setDurabilityMultiplier(this.getDurabilityMultiplier());

// Merge the enchantment map
result.getEnchantments().putAll(this.getEnchantments());
return result;
}
}

public ExtraToolProperty() {
this.overrideMap = new HashMap<>();
}

public void setOverrideProperty(String toolId, OverrideToolProperty overrideProperty) {
this.overrideMap.put(toolId, overrideProperty);
}

@Nullable
public OverrideToolProperty getOverrideProperty(String toolId) {
return this.overrideMap.get(toolId);
}

public boolean hasOverrideProperty(String toolId) {
return getOverrideProperty(toolId) != null;
}

public SimpleToolProperty getOverriddenResult(String toolId, @Nullable ToolProperty toolProperty) {
if (toolProperty == null) toolProperty = new ToolProperty();
return overrideMap.getOrDefault(toolId, new OverrideToolProperty())
.override(toolProperty);
}

@Override
public void verifyProperty(MaterialProperties properties) {
// No check here, since these recipes should be self-generated.
// If they are overriding ToolProperty, then it is already generated.
}

public static class Builder {

private final OverrideToolProperty toolProperty;

public static Builder of() {
return new Builder();
}

public static Builder of(int durability) {
Builder builder = new Builder();
builder.durability(durability);
return builder;
}

public static Builder of(float harvestSpeed, float attackDamage, int durability, int harvestLevel) {
Builder builder = new Builder();
builder.harvestSpeed(harvestSpeed).attackDamage(attackDamage).durability(durability)
.harvestLevel(harvestLevel);
return builder;
}

public Builder() {
this.toolProperty = new OverrideToolProperty();
}

public Builder harvestSpeed(float harvestSpeed) {
toolProperty.setToolSpeed(harvestSpeed);
return this;
}

public Builder attackDamage(float attackDamage) {
toolProperty.setToolAttackDamage(attackDamage);
return this;
}

public Builder durability(int durability) {
toolProperty.setToolDurability(durability);
return this;
}

public Builder attackSpeed(float attackSpeed) {
toolProperty.setToolAttackSpeed(attackSpeed);
return this;
}

public Builder harvestLevel(int harvestLevel) {
toolProperty.setToolHarvestLevel(harvestLevel);
return this;
}

public Builder enchantment(Enchantment enchantment, int level) {
toolProperty.addEnchantmentForTools(enchantment, level);
return this;
}

public Builder durabilityMultiplier(int multiplier) {
toolProperty.setDurabilityMultiplier(multiplier);
return this;
}

public OverrideToolProperty build() {
return this.toolProperty;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class PropertyKey<T extends IMaterialProperty> {
ItemPipeProperties.class);
public static final PropertyKey<OreProperty> ORE = new PropertyKey<>("ore", OreProperty.class);
public static final PropertyKey<ToolProperty> TOOL = new PropertyKey<>("tool", ToolProperty.class);
public static final PropertyKey<ExtraToolProperty> EXTRATOOL = new PropertyKey<>("extra_tool",
ExtraToolProperty.class);
public static final PropertyKey<RotorProperty> ROTOR = new PropertyKey<>("rotor", RotorProperty.class);
public static final PropertyKey<WireProperties> WIRE = new PropertyKey<>("wire", WireProperties.class);
public static final PropertyKey<WoodProperty> WOOD = new PropertyKey<>("wood", WoodProperty.class);
Expand Down
Loading