aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorghostflyby <ghostflyby+git@outlook.com>2023-11-14 23:23:10 +0800
committerGitHub <noreply@github.com>2023-11-14 17:23:10 +0200
commitfa897185ef59eb13eff2e9cd1bae68e8a1996e82 (patch)
tree393d862613525d5b7da7eb41215e8d24cb883051 /src
parentebd1748addf3c0bdd8d25c040ccaf530b390a766 (diff)
downloadGT5-Unofficial-fa897185ef59eb13eff2e9cd1bae68e8a1996e82.tar.gz
GT5-Unofficial-fa897185ef59eb13eff2e9cd1bae68e8a1996e82.tar.bz2
GT5-Unofficial-fa897185ef59eb13eff2e9cd1bae68e8a1996e82.zip
Harvest IC2 machine with wrench left click (#2365)
* Harvest IC2 machine with wrench left click * right click functions are left unchanged
Diffstat (limited to 'src')
-rw-r--r--src/main/java/gregtech/api/interfaces/IToolStats.java18
-rw-r--r--src/main/java/gregtech/common/GT_Proxy.java161
-rw-r--r--src/main/java/gregtech/common/tools/GT_Tool_Wrench.java42
3 files changed, 149 insertions, 72 deletions
diff --git a/src/main/java/gregtech/api/interfaces/IToolStats.java b/src/main/java/gregtech/api/interfaces/IToolStats.java
index 4d9a4f0775..c25056d1b1 100644
--- a/src/main/java/gregtech/api/interfaces/IToolStats.java
+++ b/src/main/java/gregtech/api/interfaces/IToolStats.java
@@ -2,12 +2,16 @@ package gregtech.api.interfaces;
import java.util.List;
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
import net.minecraft.block.Block;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
+import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.DamageSource;
import net.minecraft.world.World;
import net.minecraftforge.event.world.BlockEvent;
@@ -32,6 +36,20 @@ public interface IToolStats {
void onStatsAddedToTool(GT_MetaGenerated_Tool aItem, int aID);
/**
+ *
+ * @param player The player
+ * @param x Block pos
+ * @param y Block pos
+ * @param z Block pos
+ * @param block the block
+ * @param metadata block metadata
+ * @param tile TileEntity of the block if exist
+ * @param event the event, cancel it to prevent the block from being broken
+ */
+ default void onBreakBlock(@Nonnull EntityPlayer player, int x, int y, int z, @Nonnull Block block, byte metadata,
+ @Nullable TileEntity tile, @Nonnull BlockEvent.BreakEvent event) {}
+
+ /**
* @return Damage the Tool receives when breaking a Block. 100 is one Damage Point (or 100 EU).
*/
int getToolDamagePerBlockBreak();
diff --git a/src/main/java/gregtech/common/GT_Proxy.java b/src/main/java/gregtech/common/GT_Proxy.java
index 1c9a515aad..a33e4571d2 100644
--- a/src/main/java/gregtech/common/GT_Proxy.java
+++ b/src/main/java/gregtech/common/GT_Proxy.java
@@ -140,6 +140,7 @@ import gregtech.api.fluid.GT_FluidFactory;
import gregtech.api.interfaces.IBlockOnWalkOver;
import gregtech.api.interfaces.IGlobalWirelessEnergy;
import gregtech.api.interfaces.IProjectileItem;
+import gregtech.api.interfaces.IToolStats;
import gregtech.api.interfaces.internal.IGT_Mod;
import gregtech.api.interfaces.internal.IThaumcraftCompat;
import gregtech.api.interfaces.metatileentity.IMetaTileEntity;
@@ -1580,59 +1581,79 @@ public abstract class GT_Proxy implements IGT_Mod, IGuiHandler, IFuelHandler, IG
}
@SubscribeEvent
+ public void onBlockBreakingEvent(BlockEvent.BreakEvent event) {
+ EntityPlayer player = event.getPlayer();
+ if (player == null) return;
+
+ ItemStack item = event.getPlayer()
+ .getCurrentEquippedItem();
+ if (item == null) return;
+
+ if (!(item.getItem() instanceof GT_MetaGenerated_Tool tool)) return;
+
+ IToolStats stats = tool.getToolStats(item);
+ if (stats == null) return;
+
+ TileEntity tile = event.world.getTileEntity(event.x, event.y, event.z);
+ stats.onBreakBlock(player, event.x, event.y, event.z, event.block, (byte) event.blockMetadata, tile, event);
+ }
+
+ @SubscribeEvent
public void onBlockHarvestingEvent(BlockEvent.HarvestDropsEvent aEvent) {
- if (aEvent.harvester != null) {
- if ((!aEvent.world.isRemote) && (GT_Log.pal != null)) {
- this.mBufferedPlayerActivity.offer(
- getDataAndTime() + ";HARVEST_BLOCK;"
- + aEvent.harvester.getDisplayName()
- + ";DIM:"
- + aEvent.world.provider.dimensionId
- + ";"
- + aEvent.x
- + ";"
- + aEvent.y
- + ";"
- + aEvent.z
- + ";|;"
- + aEvent.x / 10
- + ";"
- + aEvent.y / 10
- + ";"
- + aEvent.z / 10);
- }
- ItemStack aStack = aEvent.harvester.getCurrentEquippedItem();
- if (aStack != null) {
- if ((aStack.getItem() instanceof GT_MetaGenerated_Tool)) {
- ((GT_MetaGenerated_Tool) aStack.getItem()).onHarvestBlockEvent(
- aEvent.drops,
- aStack,
- aEvent.harvester,
- aEvent.block,
- aEvent.x,
- aEvent.y,
- aEvent.z,
- (byte) aEvent.blockMetadata,
- aEvent.fortuneLevel,
- aEvent.isSilkTouching,
- aEvent);
- }
- if (EnchantmentHelper.getEnchantmentLevel(Enchantment.fireAspect.effectId, aStack) > 2) {
- try {
- for (ItemStack tDrop : aEvent.drops) {
- ItemStack tSmeltingOutput = GT_ModHandler.getSmeltingOutput(tDrop, false, null);
- if (tSmeltingOutput != null) {
- tDrop.stackSize *= tSmeltingOutput.stackSize;
- tSmeltingOutput.stackSize = tDrop.stackSize;
- GT_Utility.setStack(tDrop, tSmeltingOutput);
- }
- }
- } catch (Throwable e) {
- e.printStackTrace(GT_Log.err);
+ if (aEvent.harvester == null) return;
+
+ if ((!aEvent.world.isRemote) && (GT_Log.pal != null)) {
+ this.mBufferedPlayerActivity.offer(
+ getDataAndTime() + ";HARVEST_BLOCK;"
+ + aEvent.harvester.getDisplayName()
+ + ";DIM:"
+ + aEvent.world.provider.dimensionId
+ + ";"
+ + aEvent.x
+ + ";"
+ + aEvent.y
+ + ";"
+ + aEvent.z
+ + ";|;"
+ + aEvent.x / 10
+ + ";"
+ + aEvent.y / 10
+ + ";"
+ + aEvent.z / 10);
+ }
+
+ ItemStack aStack = aEvent.harvester.getCurrentEquippedItem();
+ if (aStack == null) return;
+
+ if ((aStack.getItem() instanceof GT_MetaGenerated_Tool tool)) {
+ tool.onHarvestBlockEvent(
+ aEvent.drops,
+ aStack,
+ aEvent.harvester,
+ aEvent.block,
+ aEvent.x,
+ aEvent.y,
+ aEvent.z,
+ (byte) aEvent.blockMetadata,
+ aEvent.fortuneLevel,
+ aEvent.isSilkTouching,
+ aEvent);
+ }
+ if (EnchantmentHelper.getEnchantmentLevel(Enchantment.fireAspect.effectId, aStack) > 2) {
+ try {
+ for (ItemStack tDrop : aEvent.drops) {
+ ItemStack tSmeltingOutput = GT_ModHandler.getSmeltingOutput(tDrop, false, null);
+ if (tSmeltingOutput != null) {
+ tDrop.stackSize *= tSmeltingOutput.stackSize;
+ tSmeltingOutput.stackSize = tDrop.stackSize;
+ GT_Utility.setStack(tDrop, tSmeltingOutput);
}
}
+ } catch (Throwable e) {
+ e.printStackTrace(GT_Log.err);
}
}
+
}
@SubscribeEvent
@@ -2678,9 +2699,9 @@ public abstract class GT_Proxy implements IGT_Mod, IGuiHandler, IFuelHandler, IG
}
/**
- * @deprecated use {@link GT_FluidFactory#builder}
* @see GT_FluidFactory#of(String, String, Materials, FluidState, int)
* @see GT_FluidFactory#of(String, String, FluidState, int)
+ * @deprecated use {@link GT_FluidFactory#builder}
*/
@Deprecated
public Fluid addFluid(String aName, String aLocalized, Materials aMaterial, int aState, int aTemperatureK) {
@@ -2969,15 +2990,15 @@ public abstract class GT_Proxy implements IGT_Mod, IGuiHandler, IFuelHandler, IG
@Deprecated
public static final HashMap<Integer, HashMap<ChunkCoordIntPair, int[]>> dimensionWiseChunkData = new HashMap<>(16); // stores
- // chunk
- // data
- // that
- // is
- // loaded/saved
+ // chunk
+ // data
+ // that
+ // is
+ // loaded/saved
public static final HashMap<Integer, GT_Pollution> dimensionWisePollution = new HashMap<>(16); // stores
- // GT_Polluttors
- // objects
+ // GT_Polluttors
+ // objects
public static final byte GTOIL = 3, GTOILFLUID = 2, GTPOLLUTION = 1, GTMETADATA = 0, NOT_LOADED = 0, LOADED = 1; // consts
// TO get default's fast
@@ -2999,21 +3020,19 @@ public abstract class GT_Proxy implements IGT_Mod, IGuiHandler, IFuelHandler, IG
@SubscribeEvent
public void onBlockBreakSpeedEvent(PlayerEvent.BreakSpeed aEvent) {
- if (aEvent.newSpeed > 0.0F) {
- if (aEvent.entityPlayer != null) {
- ItemStack aStack = aEvent.entityPlayer.getCurrentEquippedItem();
- if ((aStack != null) && ((aStack.getItem() instanceof GT_MetaGenerated_Tool))) {
- aEvent.newSpeed = ((GT_MetaGenerated_Tool) aStack.getItem()).onBlockBreakSpeedEvent(
- aEvent.newSpeed,
- aStack,
- aEvent.entityPlayer,
- aEvent.block,
- aEvent.x,
- aEvent.y,
- aEvent.z,
- (byte) aEvent.metadata,
- aEvent);
- }
+ if (aEvent.entityPlayer != null) {
+ ItemStack aStack = aEvent.entityPlayer.getCurrentEquippedItem();
+ if ((aStack != null) && ((aStack.getItem() instanceof GT_MetaGenerated_Tool))) {
+ aEvent.newSpeed = ((GT_MetaGenerated_Tool) aStack.getItem()).onBlockBreakSpeedEvent(
+ aEvent.newSpeed,
+ aStack,
+ aEvent.entityPlayer,
+ aEvent.block,
+ aEvent.x,
+ aEvent.y,
+ aEvent.z,
+ (byte) aEvent.metadata,
+ aEvent);
}
}
}
diff --git a/src/main/java/gregtech/common/tools/GT_Tool_Wrench.java b/src/main/java/gregtech/common/tools/GT_Tool_Wrench.java
index 14206b26ee..0b27806160 100644
--- a/src/main/java/gregtech/common/tools/GT_Tool_Wrench.java
+++ b/src/main/java/gregtech/common/tools/GT_Tool_Wrench.java
@@ -1,26 +1,36 @@
package gregtech.common.tools;
+import static gregtech.api.items.GT_MetaGenerated_Tool.getPrimaryMaterial;
+
import java.util.Arrays;
import java.util.List;
+import javax.annotation.Nonnull;
+
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
+import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.monster.EntityIronGolem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
+import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.IChatComponent;
+import net.minecraft.world.World;
+import net.minecraftforge.event.world.BlockEvent;
import gregtech.api.enums.SoundResource;
import gregtech.api.enums.Textures;
import gregtech.api.interfaces.IIconContainer;
+import gregtech.api.interfaces.IToolStats;
import gregtech.api.items.GT_MetaGenerated_Tool;
import gregtech.api.util.GT_ToolHarvestHelper;
import gregtech.common.items.behaviors.Behaviour_Wrench;
+import ic2.api.tile.IWrenchable;
public class GT_Tool_Wrench extends GT_Tool {
@@ -130,7 +140,7 @@ public class GT_Tool_Wrench extends GT_Tool {
@Override
public short[] getRGBa(boolean aIsToolHead, ItemStack aStack) {
- return aIsToolHead ? GT_MetaGenerated_Tool.getPrimaryMaterial(aStack).mRGBa : null;
+ return aIsToolHead ? getPrimaryMaterial(aStack).mRGBa : null;
}
@Override
@@ -148,4 +158,34 @@ public class GT_Tool_Wrench extends GT_Tool {
+ aEntity.getCommandSenderName()
+ EnumChatFormatting.WHITE);
}
+
+ @Override
+ public float getMiningSpeed(Block block, byte metadata, float mineSpeed, EntityPlayer player, World world, int x,
+ int y, int z) {
+ ItemStack holding = player.getCurrentEquippedItem();
+ if (holding == null || !(holding.getItem() instanceof GT_MetaGenerated_Tool tool)) return mineSpeed;
+
+ IToolStats stats = tool.getToolStats(holding);
+ if (stats == null) return mineSpeed;
+
+ TileEntity tile = world.getTileEntity(x, y, z);
+ if (tile == null) return mineSpeed;
+
+ float newSpeed = Math.max(Float.MIN_NORMAL, getSpeedMultiplier() * getPrimaryMaterial(holding).mToolSpeed);
+
+ if (tile instanceof IWrenchable) return newSpeed;
+
+ return mineSpeed;
+ }
+
+ @Override
+ public void onBreakBlock(@Nonnull EntityPlayer player, int x, int y, int z, @Nonnull Block block, byte metadata,
+ TileEntity tile, @Nonnull BlockEvent.BreakEvent event) {
+ final World world = player.worldObj;
+ if (tile instanceof IWrenchable wrenchable) {
+ ItemStack drop = wrenchable.getWrenchDrop(player);
+ world.setBlockToAir(x, y, z);
+ world.spawnEntityInWorld(new EntityItem(world, x, y, z, drop));
+ }
+ }
}