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

Fix GT tools not working for some mod actions #1706

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
11 changes: 10 additions & 1 deletion src/main/java/gregtech/api/items/toolitem/IToolStats.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
* The Stats for GT Tools. Not including any Material Modifiers.
Expand Down Expand Up @@ -161,4 +162,12 @@ default int getColor(ItemStack stack, int tintIndex) {
SolidMaterial primaryMaterial = ToolMetaItem.getToolMaterial(stack);
return tintIndex % 2 == 1 ? primaryMaterial.materialRGB : 0xFFFFFF;
}
}

/**
* @return The MC tool classes for cross-mod compatibility.
* Default: no tool classes.
*/
default Set<String> getToolClasses(ItemStack stack) {
return Collections.emptySet();
}
}
11 changes: 10 additions & 1 deletion src/main/java/gregtech/api/items/toolitem/ToolMetaItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,16 @@ private Map<Enchantment, Integer> bakeEnchantmentsMap(ItemStack itemStack, Colle
enchantments.keySet().removeIf(enchantment -> !enchantment.canApply(itemStack));
return enchantments;
}

}

@Override
@Nonnull
public Set<String> getToolClasses(@Nonnull ItemStack stack) {
T metaToolValueItem = getItem(stack);
if (metaToolValueItem != null) {
IToolStats toolStats = metaToolValueItem.getToolStats();
return toolStats.getToolClasses(stack);
}
return Collections.emptySet();
}
}
10 changes: 10 additions & 0 deletions src/main/java/gregtech/common/tools/ToolAxe.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;

import java.util.Collections;
import java.util.Set;

public class ToolAxe extends ToolBase {

private static final Set<String> AXE_TOOL_CLASSES = Collections.singleton("axe");

@Override
public boolean canApplyEnchantment(ItemStack stack, Enchantment enchantment) {
return enchantment.type.canEnchantItem(Items.IRON_AXE);
Expand Down Expand Up @@ -59,4 +64,9 @@ public boolean onBlockPreBreak(ItemStack stack, BlockPos blockPos, EntityPlayer
}
return false;
}

@Override
public Set<String> getToolClasses(ItemStack stack) {
return AXE_TOOL_CLASSES;
}
}
10 changes: 10 additions & 0 deletions src/main/java/gregtech/common/tools/ToolCrowbar.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@
import net.minecraft.block.state.IBlockState;
import net.minecraft.item.ItemStack;

import java.util.Collections;
import java.util.Set;

public class ToolCrowbar extends ToolBase {

private static final Set<String> CROWBAR_TOOL_CLASSES = Collections.singleton("crowbar");

@Override
public int getToolDamagePerBlockBreak(ItemStack stack) {
return 1;
Expand Down Expand Up @@ -39,4 +44,9 @@ public boolean canMineBlock(IBlockState block, ItemStack stack) {
return (tool != null && tool.equals("crowbar")) ||
block.getMaterial() == Material.CIRCUITS;
}

@Override
public Set<String> getToolClasses(ItemStack stack) {
return CROWBAR_TOOL_CLASSES;
}
}
9 changes: 9 additions & 0 deletions src/main/java/gregtech/common/tools/ToolFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@
import net.minecraft.block.state.IBlockState;
import net.minecraft.item.ItemStack;

import java.util.Collections;
import java.util.Set;

public class ToolFile extends ToolBase {

private static final Set<String> FILE_TOOL_CLASSES = Collections.singleton("file");

@Override
public int getToolDamagePerBlockBreak(ItemStack stack) {
return 1;
Expand All @@ -28,4 +33,8 @@ public boolean canMineBlock(IBlockState block, ItemStack stack) {
block.getMaterial() == Material.CIRCUITS;
}

@Override
public Set<String> getToolClasses(ItemStack stack) {
return FILE_TOOL_CLASSES;
}
}
11 changes: 11 additions & 0 deletions src/main/java/gregtech/common/tools/ToolHardHammer.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,16 @@
import net.minecraft.world.World;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class ToolHardHammer extends ToolBase {

private static final Set<String> HAMMER_TOOL_CLASSES = new HashSet<String>() {{
add("hammer"); add("pickaxe");
}};
Comment on lines +24 to +26
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably better to use ImmutableSet.of("hammer", "pickaxe") or similar here. If the Set itself is mutable then a caller could modify it, and the double-brace approach will also create an anonymous inner class which is probably not desirable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also think these should be immutable from our side to keep them safe. Regarding double braces they have to be addressed too.


@Override
public boolean canApplyEnchantment(ItemStack stack, Enchantment enchantment) {
return enchantment.type.canEnchantItem(Items.IRON_PICKAXE);
Expand Down Expand Up @@ -87,4 +93,9 @@ public void convertBlockDrops(World world, BlockPos blockPos, IBlockState blockS
public void addInformation(ItemStack stack, List<String> lines, boolean isAdvanced) {
lines.add(I18n.format("metaitem.tool.tooltip.hammer.extra_drop"));
}

@Override
public Set<String> getToolClasses(ItemStack stack) {
return HAMMER_TOOL_CLASSES;
}
}
9 changes: 9 additions & 0 deletions src/main/java/gregtech/common/tools/ToolHoe.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@
import net.minecraft.block.state.IBlockState;
import net.minecraft.item.ItemStack;

import java.util.Collections;
import java.util.Set;

public class ToolHoe extends ToolBase {

private static final Set<String> HOE_TOOL_CLASSES = Collections.singleton("hoe");

@Override
public int getToolDamagePerBlockBreak(ItemStack stack) {
return 1;
Expand All @@ -30,4 +35,8 @@ public void onStatsAddedToTool(MetaValueItem item) {
item.addComponents(new HoeBehaviour(DamageValues.DAMAGE_FOR_HOE));
}

@Override
public Set<String> getToolClasses(ItemStack stack) {
return HOE_TOOL_CLASSES;
}
}
13 changes: 10 additions & 3 deletions src/main/java/gregtech/common/tools/ToolJackHammer.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
import net.minecraft.world.World;
import net.minecraftforge.common.util.FakePlayer;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.*;

public class ToolJackHammer extends ToolDrillLV {

private static final Set<String> HAMMER_TOOL_CLASSES = new HashSet<String>() {{
add("pickaxe"); add("hammer");
}};
Comment on lines +28 to +30
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing as other comment, perhaps also should be called JACKHAMMER_[...] rather than HAMMER_[...]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It uses "hammer" in canMineBlock, so it should be consistent:

return (tool != null && (tool.equals("hammer") || tool.equals("pickaxe"))) ||

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All these if statements could be changed to something like:

return HAMMER_TOOL_CLASSES.contains(tool) ||

It will be slightly less efficient, but easier to maintain in future.
i.e. you won't have subtle bugs introduced by inconsistencies between getToolClasses() and canMineBlock()

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It uses "hammer" in canMineBlock, so it should be consistent:

I clearly meant the name of the constant, not the value of the strings in the Set.

All these if statements could be changed to something like:

Not sure what this has to do with this block of code; perhaps create a separate "conversation" linked to the code you're talking about so we have the appropriate context.


private static final ModeSwitchBehavior<JackHammerMode> MODE_SWITCH_BEHAVIOR = new ModeSwitchBehavior<>(JackHammerMode.class);

public enum JackHammerMode implements ILocalizationKey {
Expand Down Expand Up @@ -202,4 +204,9 @@ private static BlockPos rotateVertical(BlockPos origin, int x, int y, EnumFacing
default: return BlockPos.ORIGIN;
}
}

@Override
public Set<String> getToolClasses(ItemStack stack) {
return HAMMER_TOOL_CLASSES;
}
}
9 changes: 9 additions & 0 deletions src/main/java/gregtech/common/tools/ToolPickaxe.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;

import java.util.Collections;
import java.util.Set;

public class ToolPickaxe extends ToolBase {

private static final Set<String> PICK_TOOL_CLASSES = Collections.singleton("pickaxe");

@Override
public boolean canApplyEnchantment(ItemStack stack, Enchantment enchantment) {
return enchantment.type.canEnchantItem(Items.IRON_PICKAXE);
Expand Down Expand Up @@ -38,4 +43,8 @@ public boolean canMineBlock(IBlockState block, ItemStack stack) {
block.getMaterial() == Material.GLASS;
}

@Override
public Set<String> getToolClasses(ItemStack stack) {
return PICK_TOOL_CLASSES;
}
}
11 changes: 11 additions & 0 deletions src/main/java/gregtech/common/tools/ToolSaw.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@

import gregtech.api.items.toolitem.ToolMetaItem;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class ToolSaw extends ToolBase {

private static final Set<String> SAW_TOOL_CLASSES = new HashSet<String>() {{
add("axe"); add("saw");
}};

@Override
public boolean canApplyEnchantment(ItemStack stack, Enchantment enchantment) {
return enchantment.type.canEnchantItem(Items.IRON_AXE);
Expand Down Expand Up @@ -82,6 +88,11 @@ public void convertBlockDrops(World world, BlockPos blockPos, IBlockState blockS
}
}

@Override
public Set<String> getToolClasses(ItemStack stack) {
return SAW_TOOL_CLASSES;
}

/**
* Added to the world event listeners to hopefully catch and revert the ice-to-water conversion.
*
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/gregtech/common/tools/ToolScoop.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@
import net.minecraft.block.state.IBlockState;
import net.minecraft.item.ItemStack;

import java.util.Collections;
import java.util.Set;

public class ToolScoop extends ToolBase {

private static final Set<String> SCOOP_TOOL_CLASSES = Collections.singleton("scoop");

@Override
public int getToolDamagePerBlockBreak(ItemStack stack) {
return 2;
Expand All @@ -23,4 +28,8 @@ public void onStatsAddedToTool(MetaValueItem item) {
item.addComponents(new ScoopBehaviour(DamageValues.DAMAGE_FOR_SCOOP));
}

@Override
public Set<String> getToolClasses(ItemStack stack) {
return SCOOP_TOOL_CLASSES;
}
}
10 changes: 10 additions & 0 deletions src/main/java/gregtech/common/tools/ToolScrewdriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.item.ItemStack;

import java.util.Collections;
import java.util.Set;

public class ToolScrewdriver extends ToolBase {

private static final Set<String> DRIVER_TOOL_CLASSES = Collections.singleton("screwdriver");

@Override
public float getNormalDamageBonus(EntityLivingBase entity, ItemStack stack, EntityLivingBase attacker) {
String name = entity.getClass().getName();
Expand Down Expand Up @@ -35,4 +40,9 @@ public boolean canMineBlock(IBlockState block, ItemStack stack) {
return (tool != null && tool.equals("screwdriver")) ||
block.getMaterial() == Material.CIRCUITS;
}

@Override
public Set<String> getToolClasses(ItemStack stack) {
return DRIVER_TOOL_CLASSES;
}
}
9 changes: 9 additions & 0 deletions src/main/java/gregtech/common/tools/ToolShovel.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;

import java.util.Collections;
import java.util.Set;

public class ToolShovel extends ToolBase {

private static final Set<String> SHOVEL_TOOL_CLASSES = Collections.singleton("shovel");

@Override
public boolean canApplyEnchantment(ItemStack stack, Enchantment enchantment) {
return enchantment.type.canEnchantItem(Items.IRON_SHOVEL);
Expand Down Expand Up @@ -44,4 +49,8 @@ public boolean canMineBlock(IBlockState block, ItemStack stack) {
block.getMaterial() == Material.CLAY;
}

@Override
public Set<String> getToolClasses(ItemStack stack) {
return SHOVEL_TOOL_CLASSES;
}
}
9 changes: 9 additions & 0 deletions src/main/java/gregtech/common/tools/ToolSword.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;

import java.util.Collections;
import java.util.Set;

public class ToolSword extends ToolBase {

private static final Set<String> SWORD_TOOL_CLASSES = Collections.singleton("sword");

@Override
public boolean canApplyEnchantment(ItemStack stack, Enchantment enchantment) {
return enchantment.type.canEnchantItem(Items.IRON_SWORD);
Expand Down Expand Up @@ -60,4 +65,8 @@ public boolean canMineBlock(IBlockState block, ItemStack stack) {
block.getMaterial() == Material.SPONGE;
}

@Override
public Set<String> getToolClasses(ItemStack stack) {
return SWORD_TOOL_CLASSES;
}
}
11 changes: 11 additions & 0 deletions src/main/java/gregtech/common/tools/ToolUniversalSpade.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@
import net.minecraft.block.state.IBlockState;
import net.minecraft.item.ItemStack;

import java.util.HashSet;
import java.util.Set;

public class ToolUniversalSpade extends ToolBase {

private static final Set<String> SPADE_TOOL_CLASSES = new HashSet<String>() {{
add("shovel"); add("axe"); add("saw"); add("sword"); add("crowbar");
}};

@Override
public int getToolDamagePerBlockBreak(ItemStack stack) {
return 1;
Expand Down Expand Up @@ -67,4 +74,8 @@ public void onStatsAddedToTool(MetaValueItem item) {
item.addComponents(new CrowbarBehaviour(DamageValues.DAMAGE_FOR_UNIVERSAL_SPADE));
}

@Override
public Set<String> getToolClasses(ItemStack stack) {
return SPADE_TOOL_CLASSES;
}
}
9 changes: 9 additions & 0 deletions src/main/java/gregtech/common/tools/ToolWireCutter.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
import net.minecraft.block.state.IBlockState;
import net.minecraft.item.ItemStack;

import java.util.Collections;
import java.util.Set;

public class ToolWireCutter extends ToolBase {

private static final Set<String> CUTTER_TOOL_CLASSES = Collections.singleton("cutter");

@Override
public int getToolDamagePerBlockBreak(ItemStack stack) {
return 1;
Expand All @@ -26,4 +31,8 @@ public boolean canMineBlock(IBlockState block, ItemStack stack) {
return tool != null && tool.equals("cutter");
}

@Override
public Set<String> getToolClasses(ItemStack stack) {
return CUTTER_TOOL_CLASSES;
}
}
Loading