From 4613dcb55f181a5fc6a0b9822361890f13c105ee Mon Sep 17 00:00:00 2001 From: aidancbrady Date: Sat, 23 May 2015 11:36:53 -0400 Subject: [PATCH] Implemented BuildCraft style of dummy player, integrated with BlockEvent.BreakEvent in Digital Miner and Laser logic --- .../java/mekanism/common/CommonProxy.java | 51 ++++++++++++++ .../java/mekanism/common/LaserManager.java | 16 ++++- src/main/java/mekanism/common/Mekanism.java | 7 ++ .../common/tile/TileEntityDigitalMiner.java | 66 +++++++++++-------- 4 files changed, 111 insertions(+), 29 deletions(-) diff --git a/src/main/java/mekanism/common/CommonProxy.java b/src/main/java/mekanism/common/CommonProxy.java index 6d34ec33a..78533327f 100644 --- a/src/main/java/mekanism/common/CommonProxy.java +++ b/src/main/java/mekanism/common/CommonProxy.java @@ -1,6 +1,7 @@ package mekanism.common; import java.io.File; +import java.lang.ref.WeakReference; import mekanism.api.Coord4D; import mekanism.api.MekanismAPI; @@ -113,7 +114,9 @@ import net.minecraft.item.crafting.CraftingManager; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.MovingObjectPosition; import net.minecraft.world.World; +import net.minecraft.world.WorldServer; import net.minecraftforge.common.config.Configuration; +import net.minecraftforge.common.util.FakePlayerFactory; import net.minecraftforge.common.util.ForgeDirection; import cpw.mods.fml.client.registry.RenderingRegistry; import cpw.mods.fml.common.FMLCommonHandler; @@ -133,6 +136,7 @@ public class CommonProxy public static int PLASTIC_RENDER_ID = RenderingRegistry.getNextAvailableRenderId(); public static int CTM_RENDER_ID = RenderingRegistry.getNextAvailableRenderId(); + protected static WeakReference dummyPlayer = new WeakReference(null); /** * Register tile entities that have special models. Overwritten in client to register TESRs. @@ -590,6 +594,53 @@ public class CommonProxy Mekanism.logger.info("Received config from server."); } } + + private WeakReference createNewPlayer(WorldServer world) + { + EntityPlayer player = FakePlayerFactory.get(world, Mekanism.gameProfile); + + return new WeakReference(player); + } + + private WeakReference createNewPlayer(WorldServer world, double x, double y, double z) + { + EntityPlayer player = FakePlayerFactory.get(world, Mekanism.gameProfile); + + player.posX = x; + player.posY = y; + player.posZ = z; + + return new WeakReference(player); + } + + public final WeakReference getDummyPlayer(WorldServer world) + { + if(dummyPlayer.get() == null) + { + dummyPlayer = createNewPlayer(world); + } + else { + dummyPlayer.get().worldObj = world; + } + + return dummyPlayer; + } + + public final WeakReference getDummyPlayer(WorldServer world, double x, double y, double z) + { + if(dummyPlayer.get() == null) + { + dummyPlayer = createNewPlayer(world, x, y, z); + } + else { + dummyPlayer.get().worldObj = world; + dummyPlayer.get().posX = x; + dummyPlayer.get().posY = y; + dummyPlayer.get().posZ = z; + } + + return dummyPlayer; + } public EntityPlayer getPlayer(MessageContext context) { diff --git a/src/main/java/mekanism/common/LaserManager.java b/src/main/java/mekanism/common/LaserManager.java index 2822e45bb..77b3b4b39 100644 --- a/src/main/java/mekanism/common/LaserManager.java +++ b/src/main/java/mekanism/common/LaserManager.java @@ -2,21 +2,23 @@ package mekanism.common; import java.util.List; -import com.mojang.realmsclient.util.Pair; - import mekanism.api.Coord4D; import mekanism.api.MekanismConfig.general; import mekanism.api.Pos3D; import mekanism.api.lasers.ILaserReceptor; import net.minecraft.block.Block; import net.minecraft.entity.Entity; +import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.DamageSource; import net.minecraft.util.MovingObjectPosition; import net.minecraft.util.Vec3; import net.minecraft.world.World; +import net.minecraft.world.WorldServer; +import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.common.util.ForgeDirection; +import net.minecraftforge.event.world.BlockEvent; public class LaserManager { @@ -78,6 +80,16 @@ public class LaserManager List ret = null; Block blockHit = blockCoord.getBlock(world); + int meta = blockCoord.getMetadata(world); + + EntityPlayer dummy = Mekanism.proxy.getDummyPlayer((WorldServer)world, blockCoord.xCoord, blockCoord.yCoord, blockCoord.zCoord).get(); + BlockEvent.BreakEvent event = new BlockEvent.BreakEvent(blockCoord.xCoord, blockCoord.yCoord, blockCoord.zCoord, world, blockHit, meta, dummy); + MinecraftForge.EVENT_BUS.post(event); + + if(event.isCanceled()) + { + return null; + } if(dropAtBlock) { diff --git a/src/main/java/mekanism/common/Mekanism.java b/src/main/java/mekanism/common/Mekanism.java index 05bf5381a..3e87f1243 100644 --- a/src/main/java/mekanism/common/Mekanism.java +++ b/src/main/java/mekanism/common/Mekanism.java @@ -8,6 +8,7 @@ import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.UUID; import mekanism.api.Coord4D; import mekanism.api.EnumColor; @@ -111,6 +112,9 @@ import org.apache.logging.log4j.Logger; import rebelkeithy.mods.metallurgy.api.IOreInfo; import rebelkeithy.mods.metallurgy.api.MetallurgyAPI; import codechicken.multipart.handler.MultipartProxy; + +import com.mojang.authlib.GameProfile; + import cpw.mods.fml.client.event.ConfigChangedEvent; import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.IFuelHandler; @@ -203,6 +207,9 @@ public class Mekanism /** The version of ore generation in this version of Mekanism. Increment this every time the default ore generation changes. */ public static int baseWorldGenVersion = 0; + /** The GameProfile used by the dummy Mekanism player */ + public static GameProfile gameProfile = new GameProfile(UUID.nameUUIDFromBytes("mekanism.common".getBytes()), "[Mekanism]"); + public static KeySync keyMap = new KeySync(); public static Set jetpackOn = new HashSet(); diff --git a/src/main/java/mekanism/common/tile/TileEntityDigitalMiner.java b/src/main/java/mekanism/common/tile/TileEntityDigitalMiner.java index 97cf94bdc..46194a69d 100644 --- a/src/main/java/mekanism/common/tile/TileEntityDigitalMiner.java +++ b/src/main/java/mekanism/common/tile/TileEntityDigitalMiner.java @@ -1,5 +1,7 @@ package mekanism.common.tile; +import io.netty.buffer.ByteBuf; + import java.util.ArrayList; import java.util.BitSet; import java.util.HashMap; @@ -18,7 +20,6 @@ import mekanism.common.Mekanism; import mekanism.common.Upgrade; import mekanism.common.base.IActiveState; import mekanism.common.base.IAdvancedBoundingBlock; -import mekanism.common.base.ILogisticalTransporter; import mekanism.common.base.IRedstoneControl; import mekanism.common.base.ISustainedData; import mekanism.common.base.ITransporterTile; @@ -40,7 +41,6 @@ import mekanism.common.util.InventoryUtils; import mekanism.common.util.MekanismUtils; import mekanism.common.util.MinerUtils; import mekanism.common.util.TransporterUtils; - import net.minecraft.block.Block; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; @@ -51,15 +51,15 @@ import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.AxisAlignedBB; +import net.minecraft.world.WorldServer; +import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.common.util.Constants.NBT; import net.minecraftforge.common.util.ForgeDirection; +import net.minecraftforge.event.world.BlockEvent; import cpw.mods.fml.common.Optional.Interface; import cpw.mods.fml.common.Optional.Method; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; - -import io.netty.buffer.ByteBuf; - import dan200.computercraft.api.lua.ILuaContext; import dan200.computercraft.api.lua.LuaException; import dan200.computercraft.api.peripheral.IComputerAccess; @@ -358,34 +358,46 @@ public class TileEntityDigitalMiner extends TileEntityElectricBlock implements I */ public boolean setReplace(Coord4D obj, int index) { - ItemStack stack = getReplace(index); - - if(stack != null) + Block block = obj.getBlock(worldObj); + int meta = obj.getMetadata(worldObj); + + EntityPlayer dummy = Mekanism.proxy.getDummyPlayer((WorldServer)worldObj, obj.xCoord, obj.yCoord, obj.zCoord).get(); + BlockEvent.BreakEvent event = new BlockEvent.BreakEvent(obj.xCoord, obj.yCoord, obj.zCoord, worldObj, block, meta, dummy); + MinecraftForge.EVENT_BUS.post(event); + + if(!event.isCanceled()) { - worldObj.setBlock(obj.xCoord, obj.yCoord, obj.zCoord, Block.getBlockFromItem(stack.getItem()), stack.getItemDamage(), 3); - - if(obj.getBlock(worldObj) != null && !obj.getBlock(worldObj).canBlockStay(worldObj, obj.xCoord, obj.yCoord, obj.zCoord)) + ItemStack stack = getReplace(index); + + if(stack != null) { - obj.getBlock(worldObj).dropBlockAsItem(worldObj, obj.xCoord, obj.yCoord, obj.zCoord, obj.getMetadata(worldObj), 1); - worldObj.setBlockToAir(obj.xCoord, obj.yCoord, obj.zCoord); - } - - return true; - } - else { - MinerFilter filter = replaceMap.get(index); - - if(filter == null || (filter.replaceStack == null || !filter.requireStack)) - { - worldObj.setBlockToAir(obj.xCoord, obj.yCoord, obj.zCoord); + worldObj.setBlock(obj.xCoord, obj.yCoord, obj.zCoord, Block.getBlockFromItem(stack.getItem()), stack.getItemDamage(), 3); + + if(obj.getBlock(worldObj) != null && !obj.getBlock(worldObj).canBlockStay(worldObj, obj.xCoord, obj.yCoord, obj.zCoord)) + { + obj.getBlock(worldObj).dropBlockAsItem(worldObj, obj.xCoord, obj.yCoord, obj.zCoord, obj.getMetadata(worldObj), 1); + worldObj.setBlockToAir(obj.xCoord, obj.yCoord, obj.zCoord); + } return true; } - - missingStack = filter.replaceStack; - - return false; + else { + MinerFilter filter = replaceMap.get(index); + + if(filter == null || (filter.replaceStack == null || !filter.requireStack)) + { + worldObj.setBlockToAir(obj.xCoord, obj.yCoord, obj.zCoord); + + return true; + } + + missingStack = filter.replaceStack; + + return false; + } } + + return false; } public ItemStack getReplace(int index)