Miner cleanup (#5456)

* Cleanup readability of firing events

* Minor cleanup of MinerUtils

* Cleanup fallback system for MinerUtils and inline fallbackGetSilkTouch

* restore block drops backwards compatibility

* handle shulker boxes in digital miner

* use fake player in a few more event spots & add helper for getting a block's state for placement

* Change default placement so that logs face upwards
This commit is contained in:
Richard Freimer authored and GitHub committed 2019-05-04 15:17:12 -04:00
1 parent 4c4e88dca5
commit e3161cf2db
6 files changed
+120 -51

No files matched your search

@@ -21,6 +21,7 @@ import net.minecraft.util.math.RayTraceResult;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.util.Constants.WorldEvents;
import net.minecraftforge.event.world.BlockEvent;
public class LaserManager {
@@ -94,7 +95,7 @@ public class LaserManager {
blockHit.breakBlock(world, blockCoord.getPos(), state);
world.setBlockToAir(blockCoord.getPos());
world.playEvent(2001, blockCoord.getPos(), Block.getStateId(state));
world.playEvent(WorldEvents.BREAK_BLOCK_EFFECTS, blockCoord.getPos(), Block.getStateId(state));
return ret;
}
@@ -27,6 +27,7 @@ import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.math.RayTraceResult.Type;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import net.minecraftforge.common.util.Constants.WorldEvents;
import net.minecraftforge.fml.common.registry.IEntityAdditionalSpawnData;
public class EntityFlame extends Entity implements IEntityAdditionalSpawnData {
@@ -239,7 +240,7 @@ public class EntityFlame extends Entity implements IEntityAdditionalSpawnData {
world.spawnEntity(item);
}
world.playEvent(null, 2001, block.getPos(), Block.getStateId(state));
world.playEvent(WorldEvents.BREAK_BLOCK_EFFECTS, block.getPos(), Block.getStateId(state));
}
spawnParticlesAt(new Pos3D(block).translate(0.5, 0.5, 0.5));
@@ -38,6 +38,7 @@ import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.world.World;
import net.minecraftforge.common.util.Constants.WorldEvents;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
@@ -157,7 +158,7 @@ public class ItemAtomicDisassembler extends ItemEnergized {
Block block2 = coord.getBlock(player.world);
block2.onBlockHarvested(player.world, coord.getPos(), state, player);
player.world.playEvent(null, 2001, coord.getPos(), Block.getStateId(state));
player.world.playEvent(WorldEvents.BREAK_BLOCK_EFFECTS, coord.getPos(), Block.getStateId(state));
player.world.setBlockToAir(coord.getPos());
block2.breakBlock(player.world, coord.getPos(), state);
block2.dropBlockAsItem(player.world, coord.getPos(), state, 0);
@@ -7,6 +7,7 @@ import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
@@ -52,6 +53,7 @@ import mekanism.common.util.StackUtils;
import mekanism.common.util.TransporterUtils;
import net.minecraft.block.Block;
import net.minecraft.block.BlockBush;
import net.minecraft.block.BlockShulkerBox;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
@@ -60,7 +62,9 @@ import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityShulkerBox;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.NonNullList;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
@@ -70,6 +74,7 @@ import net.minecraft.world.WorldServer;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.util.Constants.NBT;
import net.minecraftforge.common.util.Constants.WorldEvents;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.event.world.BlockEvent;
import net.minecraftforge.fml.common.FMLCommonHandler;
@@ -254,7 +259,7 @@ public class TileEntityDigitalMiner extends TileEntityElectricBlock implements I
continue;
}
List<ItemStack> drops = MinerUtils.getDrops(world, coord, silkTouch);
List<ItemStack> drops = MinerUtils.getDrops(world, coord, silkTouch, this.pos);
if (canInsert(drops) && setReplace(coord, index)) {
did = true;
@@ -265,7 +270,8 @@ public class TileEntityDigitalMiner extends TileEntityElectricBlock implements I
it.remove();
}
world.playEvent(null, 2001, coord.getPos(), Block.getStateId(state));
world.playEvent(WorldEvents.BREAK_BLOCK_EFFECTS, coord.getPos(),
Block.getStateId(state));
missingStack = ItemStack.EMPTY;
}
@@ -360,16 +366,25 @@ public class TileEntityDigitalMiner extends TileEntityElectricBlock implements I
*/
public boolean setReplace(Coord4D obj, int index) {
ItemStack stack = getReplace(index);
BlockPos pos = obj.getPos();
EntityPlayer fakePlayer = Objects.requireNonNull(Mekanism.proxy.getDummyPlayer((WorldServer) world, this.pos).get());
//if its a shulker box, remove it TE so it can't drop itself in breakBlock - we've already captured its itemblock
TileEntity te = world.getTileEntity(pos);
TileEntityShulkerBox tileEntityShulkerBox = null;
if (te instanceof TileEntityShulkerBox) {
tileEntityShulkerBox = (TileEntityShulkerBox) te;
world.removeTileEntity(pos);
}
if (!stack.isEmpty()) {
world.setBlockState(obj.getPos(),
Block.getBlockFromItem(stack.getItem()).getStateFromMeta(stack.getItemDamage()), 3);
world.setBlockState(pos, StackUtils.getStateForPlacement(stack, world, pos, fakePlayer), 3);
IBlockState s = obj.getBlockState(world);
if (s.getBlock() instanceof BlockBush && !((BlockBush) s.getBlock())
.canBlockStay(world, obj.getPos(), s)) {
s.getBlock().dropBlockAsItem(world, obj.getPos(), s, 1);
world.setBlockToAir(obj.getPos());
.canBlockStay(world, pos, s)) {
s.getBlock().dropBlockAsItem(world, pos, s, 1);
world.setBlockToAir(pos);
}
return true;
@@ -377,13 +392,19 @@ public class TileEntityDigitalMiner extends TileEntityElectricBlock implements I
MinerFilter filter = replaceMap.get(index);
if (filter == null || (filter.replaceStack.isEmpty() || !filter.requireStack)) {
world.setBlockToAir(obj.getPos());
world.setBlockToAir(pos);
return true;
}
missingStack = filter.replaceStack;
// something failed, so put that thing back where it came from
if (tileEntityShulkerBox != null) {
tileEntityShulkerBox.validate();
world.setTileEntity(pos, tileEntityShulkerBox);
}
return false;
}
}
@@ -391,7 +412,7 @@ public class TileEntityDigitalMiner extends TileEntityElectricBlock implements I
private boolean canMine(Coord4D coord) {
IBlockState state = coord.getBlockState(world);
EntityPlayer dummy = Mekanism.proxy.getDummyPlayer((WorldServer) world, pos).get();
EntityPlayer dummy = Objects.requireNonNull(Mekanism.proxy.getDummyPlayer((WorldServer) world, pos).get());
BlockEvent.BreakEvent event = new BlockEvent.BreakEvent(world, coord.getPos(), state, dummy);
MinecraftForge.EVENT_BUS.post(event);
@@ -9,18 +9,26 @@ import java.util.List;
import mekanism.api.Coord4D;
import mekanism.common.Mekanism;
import net.minecraft.block.Block;
import net.minecraft.block.BlockShulkerBox;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityShulkerBox;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;
import net.minecraftforge.event.ForgeEventFactory;
import net.minecraftforge.fml.relauncher.ReflectionHelper;
import net.minecraftforge.fml.relauncher.ReflectionHelper.UnableToFindMethodException;
public final class MinerUtils {
public static List<Block> specialSilkIDs = Arrays.asList(Blocks.ICE, Blocks.CHORUS_FLOWER);
public static final List<Block> specialSilkIDs = Arrays.asList(Blocks.ICE, Blocks.CHORUS_FLOWER);
private static Method getSilkTouchDrop = null;
@@ -28,54 +36,76 @@ public final class MinerUtils {
try {
getSilkTouchDrop = ReflectionHelper
.findMethod(Block.class, "getSilkTouchDrop", "func_180643_i", IBlockState.class);
} catch (ReflectionHelper.UnableToFindMethodException e) {
} catch (UnableToFindMethodException e) {
Mekanism.logger.error("Unable to find method Block.getSilkTouchDrop");
}
}
public static List<ItemStack> getDrops(World world, Coord4D obj, boolean silk) {
IBlockState state = obj.getBlockState(world);
public static List<ItemStack> getDrops(World world, Coord4D coord, boolean silk, BlockPos minerPosition) {
IBlockState state = coord.getBlockState(world);
Block block = state.getBlock();
EntityPlayer fakePlayer = Mekanism.proxy.getDummyPlayer((WorldServer) world, minerPosition).get();
if (block.isAir(state, world, obj.getPos())) {
return Collections.EMPTY_LIST;
if (block.isAir(state, world, coord.getPos())) {
return Collections.emptyList();
}
if (silk && (
block.canSilkHarvest(world, obj.getPos(), state, Mekanism.proxy.getDummyPlayer((WorldServer) world).get())
|| specialSilkIDs.contains(block))) {
List<ItemStack> ret = new ArrayList<>();
if (block instanceof BlockShulkerBox){
//special case Shulker Boxes because bad Mojang code / no forge patch
ItemStack shulkerBoxItem = new ItemStack(Item.getItemFromBlock(block));
TileEntity tileentity = world.getTileEntity(coord.getPos());
//copied from BlockShulkerBox.breakBlock
if (tileentity instanceof TileEntityShulkerBox)
{
TileEntityShulkerBox tileentityshulkerbox = (TileEntityShulkerBox)tileentity;
if (!tileentityshulkerbox.isCleared() && tileentityshulkerbox.shouldDrop())
{
NBTTagCompound itemTag = new NBTTagCompound();
NBTTagCompound nbtBlockEntity = new NBTTagCompound();
itemTag.setTag("BlockEntityTag", ((TileEntityShulkerBox)tileentity).saveToNbt(nbtBlockEntity));
shulkerBoxItem.setTagCompound(itemTag);
if (tileentityshulkerbox.hasCustomName())
{
shulkerBoxItem.setStackDisplayName(tileentityshulkerbox.getName());
}
}
}
return Collections.singletonList(shulkerBoxItem);
} else if (silk && (block.canSilkHarvest(world, coord.getPos(), state, fakePlayer) || specialSilkIDs.contains(block))) {
Object it = null;
if (getSilkTouchDrop != null) {
try {
Object it = getSilkTouchDrop.invoke(block, state);
if (it instanceof ItemStack && !((ItemStack) it).isEmpty()) {
ret.add((ItemStack) it);
} else if (it instanceof ItemStack && ((ItemStack) it)
.isEmpty()) {//silk touch drop is empty, fallback to grabbing an itemblock
fallbackGetSilkTouch(block, state, ret);
}
it = getSilkTouchDrop.invoke(block, state);
} catch (InvocationTargetException | IllegalAccessException e) {
Mekanism.logger.error("Block.getSilkTouchDrop errored", e);
fallbackGetSilkTouch(block, state, ret);
}
} else//fallback to old method
{
fallbackGetSilkTouch(block, state, ret);
}
List<ItemStack> ret = new ArrayList<>();
if (it instanceof ItemStack && !((ItemStack) it).isEmpty()) {
ret.add((ItemStack) it);
} else {
//silk touch drop is empty or failed to call/find getSilkTouchDrop method
// Fallback to grabbing an itemblock
Item item = Item.getItemFromBlock(block);
if (item != Items.AIR) {
ret.add(new ItemStack(item, 1, item.getHasSubtypes() ? block.getMetaFromState(state) : 0));
}
}
if (ret.size() > 0) {
net.minecraftforge.event.ForgeEventFactory
.fireBlockHarvesting(ret, world, obj.getPos(), state, 0, 1.0f, true, null);
ForgeEventFactory.fireBlockHarvesting(ret, world, coord.getPos(), state, 0, 1.0F, true, fakePlayer);
return ret;
}
} else {
@SuppressWarnings("deprecation")
List<ItemStack> blockDrops = block.getDrops(world, obj.getPos(), state, 0);
@SuppressWarnings("deprecation")//needed for backwards compatibility
List<ItemStack> blockDrops = block.getDrops(world, coord.getPos(), state, 0);
if (blockDrops.size() > 0) {
net.minecraftforge.event.ForgeEventFactory
.fireBlockHarvesting(blockDrops, world, obj.getPos(), state, 0, 1.0f, false, null);
} else if (block
== Blocks.CHORUS_FLOWER) {//Chorus flower returns AIR for itemDropped... and for silkTouchDrop.
ForgeEventFactory.fireBlockHarvesting(blockDrops, world, coord.getPos(), state, 0, 1.0F, false, fakePlayer);
} else if (block == Blocks.CHORUS_FLOWER) {
//Chorus flower returns AIR for itemDropped... and for silkTouchDrop.
blockDrops.add(new ItemStack(Blocks.CHORUS_FLOWER));
}
return blockDrops;
@@ -83,12 +113,4 @@ public final class MinerUtils {
return Collections.emptyList();
}
private static void fallbackGetSilkTouch(Block block, IBlockState state, List<ItemStack> ret) {
Item item = Item.getItemFromBlock(block);
if (item != Items.AIR) {
int meta = item.getHasSubtypes() ? block.getMetaFromState(state) : 0;
ret.add(new ItemStack(item, 1, meta));
}
}
}
}
@@ -2,9 +2,17 @@ package mekanism.common.util;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nonnull;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.NonNullList;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.oredict.OreDictionary;
public final class StackUtils {
@@ -126,4 +134,19 @@ public final class StackUtils {
int nameHash = registryName == null ? 0 : registryName.hashCode();
return nameHash << 8 | stack.getMetadata();
}
/**
* Get state for placement for a generic item, with our fake player
*
* @param stack the item to place
* @param world which universe
* @param pos where
* @param player our fake player, usually
* @return the result of {@link Block#getStateForPlacement(net.minecraft.world.World, net.minecraft.util.math.BlockPos, net.minecraft.util.EnumFacing, float, float, float, int, net.minecraft.entity.EntityLivingBase, net.minecraft.util.EnumHand)}
*/
@Nonnull
public static IBlockState getStateForPlacement(ItemStack stack, World world, BlockPos pos, EntityPlayer player) {
Block blockFromItem = Block.getBlockFromItem(stack.getItem());
return blockFromItem.getStateForPlacement(world, pos, EnumFacing.UP, 0,0,0, stack.getMetadata(), player, EnumHand.MAIN_HAND);
}
}