Implemented BuildCraft style of dummy player, integrated with BlockEvent.BreakEvent in Digital Miner and Laser logic

This commit is contained in:
aidancbrady 2015-05-23 11:36:53 -04:00
parent 173be49a4a
commit 4613dcb55f
4 changed files with 111 additions and 29 deletions

View file

@ -1,6 +1,7 @@
package mekanism.common; package mekanism.common;
import java.io.File; import java.io.File;
import java.lang.ref.WeakReference;
import mekanism.api.Coord4D; import mekanism.api.Coord4D;
import mekanism.api.MekanismAPI; import mekanism.api.MekanismAPI;
@ -113,7 +114,9 @@ import net.minecraft.item.crafting.CraftingManager;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.MovingObjectPosition; import net.minecraft.util.MovingObjectPosition;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraft.world.WorldServer;
import net.minecraftforge.common.config.Configuration; import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.common.util.FakePlayerFactory;
import net.minecraftforge.common.util.ForgeDirection; import net.minecraftforge.common.util.ForgeDirection;
import cpw.mods.fml.client.registry.RenderingRegistry; import cpw.mods.fml.client.registry.RenderingRegistry;
import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.FMLCommonHandler;
@ -133,6 +136,7 @@ public class CommonProxy
public static int PLASTIC_RENDER_ID = RenderingRegistry.getNextAvailableRenderId(); public static int PLASTIC_RENDER_ID = RenderingRegistry.getNextAvailableRenderId();
public static int CTM_RENDER_ID = RenderingRegistry.getNextAvailableRenderId(); public static int CTM_RENDER_ID = RenderingRegistry.getNextAvailableRenderId();
protected static WeakReference<EntityPlayer> dummyPlayer = new WeakReference<EntityPlayer>(null);
/** /**
* Register tile entities that have special models. Overwritten in client to register TESRs. * 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."); Mekanism.logger.info("Received config from server.");
} }
} }
private WeakReference<EntityPlayer> createNewPlayer(WorldServer world)
{
EntityPlayer player = FakePlayerFactory.get(world, Mekanism.gameProfile);
return new WeakReference<EntityPlayer>(player);
}
private WeakReference<EntityPlayer> 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<EntityPlayer>(player);
}
public final WeakReference<EntityPlayer> getDummyPlayer(WorldServer world)
{
if(dummyPlayer.get() == null)
{
dummyPlayer = createNewPlayer(world);
}
else {
dummyPlayer.get().worldObj = world;
}
return dummyPlayer;
}
public final WeakReference<EntityPlayer> 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) public EntityPlayer getPlayer(MessageContext context)
{ {

View file

@ -2,21 +2,23 @@ package mekanism.common;
import java.util.List; import java.util.List;
import com.mojang.realmsclient.util.Pair;
import mekanism.api.Coord4D; import mekanism.api.Coord4D;
import mekanism.api.MekanismConfig.general; import mekanism.api.MekanismConfig.general;
import mekanism.api.Pos3D; import mekanism.api.Pos3D;
import mekanism.api.lasers.ILaserReceptor; import mekanism.api.lasers.ILaserReceptor;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.DamageSource; import net.minecraft.util.DamageSource;
import net.minecraft.util.MovingObjectPosition; import net.minecraft.util.MovingObjectPosition;
import net.minecraft.util.Vec3; import net.minecraft.util.Vec3;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraft.world.WorldServer;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.util.ForgeDirection; import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.event.world.BlockEvent;
public class LaserManager public class LaserManager
{ {
@ -78,6 +80,16 @@ public class LaserManager
List<ItemStack> ret = null; List<ItemStack> ret = null;
Block blockHit = blockCoord.getBlock(world); 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) if(dropAtBlock)
{ {

View file

@ -8,6 +8,7 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.UUID;
import mekanism.api.Coord4D; import mekanism.api.Coord4D;
import mekanism.api.EnumColor; 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.IOreInfo;
import rebelkeithy.mods.metallurgy.api.MetallurgyAPI; import rebelkeithy.mods.metallurgy.api.MetallurgyAPI;
import codechicken.multipart.handler.MultipartProxy; import codechicken.multipart.handler.MultipartProxy;
import com.mojang.authlib.GameProfile;
import cpw.mods.fml.client.event.ConfigChangedEvent; import cpw.mods.fml.client.event.ConfigChangedEvent;
import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.IFuelHandler; 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. */ /** The version of ore generation in this version of Mekanism. Increment this every time the default ore generation changes. */
public static int baseWorldGenVersion = 0; 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 KeySync keyMap = new KeySync();
public static Set<String> jetpackOn = new HashSet<String>(); public static Set<String> jetpackOn = new HashSet<String>();

View file

@ -1,5 +1,7 @@
package mekanism.common.tile; package mekanism.common.tile;
import io.netty.buffer.ByteBuf;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.BitSet; import java.util.BitSet;
import java.util.HashMap; import java.util.HashMap;
@ -18,7 +20,6 @@ import mekanism.common.Mekanism;
import mekanism.common.Upgrade; import mekanism.common.Upgrade;
import mekanism.common.base.IActiveState; import mekanism.common.base.IActiveState;
import mekanism.common.base.IAdvancedBoundingBlock; import mekanism.common.base.IAdvancedBoundingBlock;
import mekanism.common.base.ILogisticalTransporter;
import mekanism.common.base.IRedstoneControl; import mekanism.common.base.IRedstoneControl;
import mekanism.common.base.ISustainedData; import mekanism.common.base.ISustainedData;
import mekanism.common.base.ITransporterTile; import mekanism.common.base.ITransporterTile;
@ -40,7 +41,6 @@ import mekanism.common.util.InventoryUtils;
import mekanism.common.util.MekanismUtils; import mekanism.common.util.MekanismUtils;
import mekanism.common.util.MinerUtils; import mekanism.common.util.MinerUtils;
import mekanism.common.util.TransporterUtils; import mekanism.common.util.TransporterUtils;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.entity.player.EntityPlayerMP;
@ -51,15 +51,15 @@ import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList; import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB; 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.Constants.NBT;
import net.minecraftforge.common.util.ForgeDirection; 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.Interface;
import cpw.mods.fml.common.Optional.Method; import cpw.mods.fml.common.Optional.Method;
import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly; import cpw.mods.fml.relauncher.SideOnly;
import io.netty.buffer.ByteBuf;
import dan200.computercraft.api.lua.ILuaContext; import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.lua.LuaException; import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.peripheral.IComputerAccess; import dan200.computercraft.api.peripheral.IComputerAccess;
@ -358,34 +358,46 @@ public class TileEntityDigitalMiner extends TileEntityElectricBlock implements I
*/ */
public boolean setReplace(Coord4D obj, int index) public boolean setReplace(Coord4D obj, int index)
{ {
ItemStack stack = getReplace(index); Block block = obj.getBlock(worldObj);
int meta = obj.getMetadata(worldObj);
if(stack != null)
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); ItemStack stack = getReplace(index);
if(obj.getBlock(worldObj) != null && !obj.getBlock(worldObj).canBlockStay(worldObj, obj.xCoord, obj.yCoord, obj.zCoord)) if(stack != null)
{ {
obj.getBlock(worldObj).dropBlockAsItem(worldObj, obj.xCoord, obj.yCoord, obj.zCoord, obj.getMetadata(worldObj), 1); worldObj.setBlock(obj.xCoord, obj.yCoord, obj.zCoord, Block.getBlockFromItem(stack.getItem()), stack.getItemDamage(), 3);
worldObj.setBlockToAir(obj.xCoord, obj.yCoord, obj.zCoord);
} if(obj.getBlock(worldObj) != null && !obj.getBlock(worldObj).canBlockStay(worldObj, obj.xCoord, obj.yCoord, obj.zCoord))
{
return true; obj.getBlock(worldObj).dropBlockAsItem(worldObj, obj.xCoord, obj.yCoord, obj.zCoord, obj.getMetadata(worldObj), 1);
} worldObj.setBlockToAir(obj.xCoord, obj.yCoord, obj.zCoord);
else { }
MinerFilter filter = replaceMap.get(index);
if(filter == null || (filter.replaceStack == null || !filter.requireStack))
{
worldObj.setBlockToAir(obj.xCoord, obj.yCoord, obj.zCoord);
return true; return true;
} }
else {
missingStack = filter.replaceStack; MinerFilter filter = replaceMap.get(index);
return false; 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) public ItemStack getReplace(int index)