aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/common/items
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/gregtech/common/items')
-rw-r--r--src/main/java/gregtech/common/items/behaviors/Behaviour_Wrench.java341
1 files changed, 216 insertions, 125 deletions
diff --git a/src/main/java/gregtech/common/items/behaviors/Behaviour_Wrench.java b/src/main/java/gregtech/common/items/behaviors/Behaviour_Wrench.java
index df6f3bb0d4..edafab779f 100644
--- a/src/main/java/gregtech/common/items/behaviors/Behaviour_Wrench.java
+++ b/src/main/java/gregtech/common/items/behaviors/Behaviour_Wrench.java
@@ -2,21 +2,24 @@ package gregtech.common.items.behaviors;
import java.util.Arrays;
import java.util.List;
+import java.util.function.BooleanSupplier;
import net.minecraft.block.Block;
-import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
+import net.minecraftforge.oredict.OreDictionary;
+import appeng.api.parts.IPartHost;
+import appeng.api.util.IOrientable;
+import appeng.tile.misc.TileInterface;
import gregtech.api.enums.SoundResource;
import gregtech.api.items.GT_MetaBase_Item;
import gregtech.api.items.GT_MetaGenerated_Tool;
import gregtech.api.util.GT_LanguageManager;
-import gregtech.api.util.GT_ModHandler;
import gregtech.api.util.GT_Utility;
import ic2.api.tile.IWrenchable;
@@ -33,150 +36,238 @@ public class Behaviour_Wrench extends Behaviour_None {
@Override
public boolean onItemUseFirst(GT_MetaBase_Item aItem, ItemStack aStack, EntityPlayer aPlayer, World aWorld, int aX,
int aY, int aZ, ForgeDirection side, float hitX, float hitY, float hitZ) {
- if (aWorld.isRemote || aPlayer.isSneaking()) {
- return false;
- }
final Block aBlock = aWorld.getBlock(aX, aY, aZ);
if (aBlock == null) {
return false;
}
- final byte aMeta = (byte) aWorld.getBlockMetadata(aX, aY, aZ);
+ final int aMeta = aWorld.getBlockMetadata(aX, aY, aZ);
final short targetSideOrdinal = (short) GT_Utility.determineWrenchingSide(side, hitX, hitY, hitZ)
.ordinal();
final TileEntity aTileEntity = aWorld.getTileEntity(aX, aY, aZ);
+
+ final WrenchHandler handler = new WrenchHandler(
+ aBlock,
+ aMeta,
+ targetSideOrdinal,
+ aTileEntity,
+ aPlayer,
+ aWorld,
+ aX,
+ aY,
+ aZ,
+ aStack,
+ (GT_MetaGenerated_Tool) aItem,
+ mCosts);
+
try {
- if (aTileEntity instanceof IWrenchable wrenchable) {
- if (wrenchable.wrenchCanSetFacing(aPlayer, targetSideOrdinal)) {
- if ((aPlayer.capabilities.isCreativeMode)
- || (((GT_MetaGenerated_Tool) aItem).doDamage(aStack, this.mCosts))) {
- wrenchable.setFacing(targetSideOrdinal);
- GT_Utility.sendSoundToPlayers(aWorld, SoundResource.IC2_TOOLS_WRENCH, 1.0F, -1.0F, aX, aY, aZ);
- }
- return true;
+ return handler.handle();
+ } catch (Throwable ignored) {}
+ return false;
+ }
+
+ /**
+ * <p>
+ * A class to simplify wrenching operation,
+ * stopping "checking creative", "trying to damage tool",
+ * "doing the logic" and "playing sound" again and again.
+ * This should have been a record, but it's not available in Java 8.
+ * </p>
+ * <p>
+ * {@link WrenchHandler#handle()} is the entry point of main logic.
+ * </p>
+ */
+ private static class WrenchHandler {
+
+ boolean handle() {
+ ForgeDirection direction = ForgeDirection.getOrientation(targetSideOrdinal);
+
+ // AE2 logic
+ // default to change the up facing
+ // sneak to change the forward facing
+ if (tileEntity instanceof IOrientable orientable) {
+ if (!orientable.canBeRotated()) return false;
+ ForgeDirection front = orientable.getForward();
+ ForgeDirection up = orientable.getUp();
+
+ // mainly for me-interfaces, whose initial orientation is UNKNOWN
+ if (front == ForgeDirection.UNKNOWN) {
+ if (direction == ForgeDirection.UP || direction == ForgeDirection.DOWN)
+ front = ForgeDirection.NORTH;
+ else front = ForgeDirection.UP;
}
- if (wrenchable.wrenchCanRemove(aPlayer)) {
- final int tDamage = wrenchable.getWrenchDropRate() < 1.0F ? 10 : 3;
- if ((aPlayer.capabilities.isCreativeMode)
- || (((GT_MetaGenerated_Tool) aItem).doDamage(aStack, (long) tDamage * this.mCosts))) {
- ItemStack tOutput = wrenchable.getWrenchDrop(aPlayer);
- for (final ItemStack tStack : aBlock.getDrops(aWorld, aX, aY, aZ, aMeta, 0)) {
- if (tOutput == null) {
- aWorld.spawnEntityInWorld(
- new EntityItem(aWorld, aX + 0.5D, aY + 0.5D, aZ + 0.5D, tStack));
- } else {
- aWorld.spawnEntityInWorld(
- new EntityItem(aWorld, aX + 0.5D, aY + 0.5D, aZ + 0.5D, tOutput));
- tOutput = null;
- }
- }
- aWorld.setBlockToAir(aX, aY, aZ);
- GT_Utility.sendSoundToPlayers(aWorld, SoundResource.IC2_TOOLS_WRENCH, 1.0F, -1.0F, aX, aY, aZ);
+
+ ForgeDirection back = front.getOpposite();
+ ForgeDirection down = up.getOpposite();
+
+ if (tileEntity instanceof TileInterface) {
+ if (player.isSneaking()) return false;
+ if (direction == down) {
+ return doWrenchOperation(costs, () -> {
+ orientable.setOrientation(ForgeDirection.UNKNOWN, ForgeDirection.UNKNOWN);
+ return true;
+ });
}
- return true;
+ // interface's up-side is opposite to the arrow on texture
+ // make it intuitive by rotating it to the opposite side.
+ direction = direction.getOpposite();
+ up = up.getOpposite();
+ } else if (direction == up || direction == front) {
+ // rotate around the direction axis
+ final var tempFront = front;
+ final var tempUp = up;
+ if (!player.isSneaking() && direction == up) return doWrenchOperation(costs, () -> {
+ orientable.setOrientation(tempFront.getRotation(tempUp), tempUp);
+ return true;
+ });
+ if (player.isSneaking() && direction == front) return doWrenchOperation(costs, () -> {
+ orientable.setOrientation(
+ tempFront,
+ tempUp.getRotation(tempFront)
+ .getRotation(tempFront));
+ return true;
+ });
+ }
+
+ if (player.isSneaking()) {
+ if (direction == up || direction == down) {
+ orientable.setOrientation(direction, down.getRotation(front.getRotation(direction)));
+ } else orientable.setOrientation(direction, up);
+ } else {
+ if (direction == front || direction == back) {
+ orientable.setOrientation(back.getRotation(up.getRotation(direction)), direction);
+ } else orientable.setOrientation(front, direction);
}
- return true;
+
+ return damageWrench(costs);
}
- } catch (Throwable ignored) {}
- if ((aBlock == Blocks.log) || (aBlock == Blocks.log2) || (aBlock == Blocks.hay_block)) {
- if ((aPlayer.capabilities.isCreativeMode)
- || (((GT_MetaGenerated_Tool) aItem).doDamage(aStack, this.mCosts))) {
- aWorld.setBlockMetadataWithNotify(aX, aY, aZ, (aMeta + 4) % 12, 3);
- GT_Utility.sendSoundToPlayers(aWorld, SoundResource.IC2_TOOLS_WRENCH, 1.0F, -1.0F, aX, aY, aZ);
+ if (world.isRemote) return false;
+ // IC2 Wrenchable
+ if (tileEntity instanceof IWrenchable wrenchable) {
+ if (wrenchable.wrenchCanSetFacing(player, targetSideOrdinal)) {
+ return doWrenchOperation(costs, () -> {
+ wrenchable.setFacing(targetSideOrdinal);
+ return true;
+ });
+ }
+ return false;
}
- return true;
- }
- if ((aBlock == Blocks.powered_repeater) || (aBlock == Blocks.unpowered_repeater)) {
- if ((aPlayer.capabilities.isCreativeMode)
- || (((GT_MetaGenerated_Tool) aItem).doDamage(aStack, this.mCosts))) {
- aWorld.setBlockMetadataWithNotify(aX, aY, aZ, aMeta / 4 * 4 + (aMeta % 4 + 1) % 4, 3);
- GT_Utility.sendSoundToPlayers(aWorld, SoundResource.IC2_TOOLS_WRENCH, 1.0F, -1.0F, aX, aY, aZ);
+
+ if (block == Blocks.powered_repeater || block == Blocks.unpowered_repeater
+ || block == Blocks.powered_comparator
+ || block == Blocks.unpowered_comparator) return setBlockMeta(costs, meta / 4 * 4 + (meta % 4 + 1) % 4);
+
+ // hopper cannot face sky
+ if (block == Blocks.hopper && targetSideOrdinal != 1) return setBlockMeta(costs, targetSideOrdinal);
+
+ if (isVanillaAllSideRotatable(block)) if (meta < 6) return setBlockMeta(costs, targetSideOrdinal);
+
+ // blocks like chests and furnaces have only four directions
+ if (isVanillaCantFaceAxisY(block)) {
+ if (targetSideOrdinal > 1) return setBlockMeta(costs, targetSideOrdinal);
+ else return false;
}
- return true;
- }
- if ((aBlock == Blocks.powered_comparator) || (aBlock == Blocks.unpowered_comparator)) {
- if ((aPlayer.capabilities.isCreativeMode)
- || (((GT_MetaGenerated_Tool) aItem).doDamage(aStack, this.mCosts))) {
- aWorld.setBlockMetadataWithNotify(aX, aY, aZ, aMeta / 4 * 4 + (aMeta % 4 + 1) % 4, 3);
- GT_Utility.sendSoundToPlayers(aWorld, SoundResource.IC2_TOOLS_WRENCH, 1.0F, -1.0F, aX, aY, aZ);
+ if (tileEntity instanceof IPartHost) return false;
+
+ final int logWoodId = OreDictionary.getOreID("logWood");
+ if (Arrays.stream(OreDictionary.getOreIDs(new ItemStack(block)))
+ .anyMatch(id -> id == logWoodId)) {
+ // The meta just work
+ return setBlockMeta(costs, (meta + 4) % 12);
}
- return true;
+
+ // vanilla block rotate logic
+ if ((Arrays.asList(block.getValidRotations(world, x, y, z))
+ .contains(direction))) return rotateBlock(costs, direction);
+ return false;
+
+ // GT blocks' rotations are done by blocks themselves after this returning false
}
- if ((aBlock == Blocks.crafting_table) || (aBlock == Blocks.bookshelf)) {
- if ((aPlayer.capabilities.isCreativeMode)
- || (((GT_MetaGenerated_Tool) aItem).doDamage(aStack, this.mCosts))) {
- aWorld.spawnEntityInWorld(
- new EntityItem(aWorld, aX + 0.5D, aY + 0.5D, aZ + 0.5D, new ItemStack(aBlock, 1, aMeta)));
- aWorld.setBlockToAir(aX, aY, aZ);
- GT_Utility.sendSoundToPlayers(aWorld, SoundResource.IC2_TOOLS_WRENCH, 1.0F, -1.0F, aX, aY, aZ);
- }
- return true;
+
+ private final Block block;
+ private final short targetSideOrdinal;
+ private final TileEntity tileEntity;
+ private final EntityPlayer player;
+ private final World world;
+ private final int x, y, z, meta;
+ private final ItemStack stack;
+
+ private final GT_MetaGenerated_Tool item;
+ private final int costs;
+
+ public WrenchHandler(Block block, int meta, short targetSideOrdinal, TileEntity tileEntity, EntityPlayer player,
+ World world, int x, int y, int z, ItemStack stack, GT_MetaGenerated_Tool item, int costs) {
+ this.block = block;
+ this.meta = meta;
+ this.targetSideOrdinal = targetSideOrdinal;
+ this.tileEntity = tileEntity;
+ this.player = player;
+ this.world = world;
+ this.x = x;
+ this.y = y;
+ this.z = z;
+ this.stack = stack;
+ this.item = item;
+ this.costs = costs;
}
- if (aMeta == targetSideOrdinal) {
- if ((aBlock == Blocks.pumpkin) || (aBlock == Blocks.lit_pumpkin)
- || (aBlock == Blocks.piston)
- || (aBlock == Blocks.sticky_piston)
- || (aBlock == Blocks.dispenser)
- || (aBlock == Blocks.dropper)
- || (aBlock == Blocks.furnace)
- || (aBlock == Blocks.lit_furnace)
- || (aBlock == Blocks.chest)
- || (aBlock == Blocks.trapped_chest)
- || (aBlock == Blocks.ender_chest)
- || (aBlock == Blocks.hopper)) {
- if ((aPlayer.capabilities.isCreativeMode)
- || (((GT_MetaGenerated_Tool) aItem).doDamage(aStack, this.mCosts))) {
- aWorld.spawnEntityInWorld(
- new EntityItem(aWorld, aX + 0.5D, aY + 0.5D, aZ + 0.5D, new ItemStack(aBlock, 1, 0)));
- aWorld.setBlockToAir(aX, aY, aZ);
- GT_Utility.sendSoundToPlayers(aWorld, SoundResource.IC2_TOOLS_WRENCH, 1.0F, -1.0F, aX, aY, aZ);
- }
- return true;
- }
- } else {
- if ((aBlock == Blocks.piston) || (aBlock == Blocks.sticky_piston)
- || (aBlock == Blocks.dispenser)
- || (aBlock == Blocks.dropper)) {
- if ((aMeta < 6) && ((aPlayer.capabilities.isCreativeMode)
- || (((GT_MetaGenerated_Tool) aItem).doDamage(aStack, this.mCosts)))) {
- aWorld.setBlockMetadataWithNotify(aX, aY, aZ, targetSideOrdinal, 3);
- GT_Utility.sendSoundToPlayers(aWorld, SoundResource.IC2_TOOLS_WRENCH, 1.0F, -1.0F, aX, aY, aZ);
- }
- return true;
- }
- if ((aBlock == Blocks.pumpkin) || (aBlock == Blocks.lit_pumpkin)
- || (aBlock == Blocks.furnace)
- || (aBlock == Blocks.lit_furnace)
- || (aBlock == Blocks.chest)
- || (aBlock == Blocks.ender_chest)
- || (aBlock == Blocks.trapped_chest)) {
- if ((targetSideOrdinal > 1) && ((aPlayer.capabilities.isCreativeMode)
- || (((GT_MetaGenerated_Tool) aItem).doDamage(aStack, this.mCosts)))) {
- aWorld.setBlockMetadataWithNotify(aX, aY, aZ, targetSideOrdinal, 3);
- GT_Utility.sendSoundToPlayers(aWorld, SoundResource.IC2_TOOLS_WRENCH, 1.0F, -1.0F, aX, aY, aZ);
- }
- return true;
- }
- if (aBlock == Blocks.hopper) {
- if ((targetSideOrdinal != 1) && ((aPlayer.capabilities.isCreativeMode)
- || (((GT_MetaGenerated_Tool) aItem).doDamage(aStack, this.mCosts)))) {
- aWorld.setBlockMetadataWithNotify(aX, aY, aZ, targetSideOrdinal, 3);
- GT_Utility.sendSoundToPlayers(aWorld, SoundResource.IC2_TOOLS_WRENCH, 1.0F, -1.0F, aX, aY, aZ);
+
+ /**
+ * this will run the operation, damage the tool and play the sound if possible (creative mode or
+ * {@link GT_MetaGenerated_Tool#canWrench(EntityPlayer, int, int, int)})
+ *
+ * @param damage damage to be applied to the wrench
+ * @param operation the real operation of the click
+ * @return true if the operation was successful
+ * @see #setBlockMeta(int, int)
+ * @see #rotateBlock(int, ForgeDirection)
+ * @see #rotateBlock(int, ForgeDirection)
+ */
+ boolean doWrenchOperation(int damage, BooleanSupplier operation) {
+ if (player.capabilities.isCreativeMode || item.canWrench(player, x, y, z)) {
+ if (operation.getAsBoolean()) {
+ item.doDamage(stack, damage);
+ GT_Utility.sendSoundToPlayers(world, SoundResource.IC2_TOOLS_WRENCH, 1.0F, -1.0F, x, y, z);
+ return true;
}
- return true;
}
+ return false;
}
- if ((Arrays.asList(aBlock.getValidRotations(aWorld, aX, aY, aZ))
- .contains(ForgeDirection.getOrientation(targetSideOrdinal)))
- && ((aPlayer.capabilities.isCreativeMode) || (!GT_ModHandler.isElectricItem(aStack))
- || (GT_ModHandler.canUseElectricItem(aStack, this.mCosts)))
- && (aBlock.rotateBlock(aWorld, aX, aY, aZ, ForgeDirection.getOrientation(targetSideOrdinal)))) {
- if (!aPlayer.capabilities.isCreativeMode) {
- ((GT_MetaGenerated_Tool) aItem).doDamage(aStack, this.mCosts);
- }
- GT_Utility.sendSoundToPlayers(aWorld, SoundResource.IC2_TOOLS_WRENCH, 1.0F, -1.0F, aX, aY, aZ);
- return true;
+
+ boolean setBlockMeta(int damage, int newMeta) {
+ return doWrenchOperation(damage, () -> setBlockMetadataWithNotify(newMeta));
}
- return false;
+
+ boolean rotateBlock(int damage, ForgeDirection direction) {
+ return doWrenchOperation(damage, () -> block.rotateBlock(world, x, y, z, direction));
+ }
+
+ boolean damageWrench(int damage) {
+ return doWrenchOperation(damage, () -> true);
+ }
+
+ private boolean setBlockMetadataWithNotify(int newMeta) {
+ return world.setBlockMetadataWithNotify(x, y, z, newMeta, 3);
+ }
+
+ }
+
+ public static boolean isVanillaRotatable(Block block) {
+ return isVanillaCantFaceAxisY(block) || isVanillaAllSideRotatable(block) || block == Blocks.hopper;
+ }
+
+ public static boolean isVanillaCantFaceAxisY(Block block) {
+ return GT_Utility.arrayContains(
+ block,
+ Blocks.pumpkin,
+ Blocks.lit_pumpkin,
+ Blocks.furnace,
+ Blocks.lit_furnace,
+ Blocks.chest,
+ Blocks.trapped_chest,
+ Blocks.ender_chest);
+ }
+
+ public static boolean isVanillaAllSideRotatable(Block block) {
+ return GT_Utility.arrayContains(block, Blocks.piston, Blocks.sticky_piston, Blocks.dispenser, Blocks.dropper);
}
@Override