From fd32bbf31ad1cb89644f56b1d723bb10b01a38e6 Mon Sep 17 00:00:00 2001 From: Tschipp Date: Mon, 14 Aug 2017 14:52:34 +0200 Subject: [PATCH] Added Two new Config Options --- .../carryon/common/config/Configs.java | 9 ++++ .../carryon/common/event/ItemEvents.java | 22 ++++---- .../tschipp/carryon/common/item/ItemTile.java | 50 ++++++++++--------- 3 files changed, 48 insertions(+), 33 deletions(-) diff --git a/src/main/java/tschipp/carryon/common/config/Configs.java b/src/main/java/tschipp/carryon/common/config/Configs.java index f7e817d..7aa345c 100644 --- a/src/main/java/tschipp/carryon/common/config/Configs.java +++ b/src/main/java/tschipp/carryon/common/config/Configs.java @@ -12,6 +12,12 @@ public class Configs { @Comment("More complex Tile Entities slow down the player more") public boolean heavyTiles = true; + + @Comment("Allow all blocks to be picked up, not just Tile Entites") + public boolean pickupAllBlocks = false; + + @Comment("Maximum distance from where Blocks can be picked up") + public double maxDistance = 2.5; } public static class ForbiddenTiles @@ -22,6 +28,8 @@ public class Configs { { "minecraft:end_portal", "minecraft:end_gateway", + "minecraft:double_plant", + "minecraft:bed", "animania:block_trough", "animania:block_invisiblock", "colossalchests:*", @@ -54,6 +62,7 @@ public class Configs { public String[] modelOverrides = new String[] { "minecraft:lit_furnace->minecraft:furnace", + "minecraft:bed->minecraft:bed", "quark:custom_chest{type:\"spruce\"}->quark:custom_chest;0", "quark:custom_chest{type:\"birch\"}->quark:custom_chest;1", "quark:custom_chest{type:\"jungle\"}->quark:custom_chest;2", diff --git a/src/main/java/tschipp/carryon/common/event/ItemEvents.java b/src/main/java/tschipp/carryon/common/event/ItemEvents.java index f119d98..af3e826 100644 --- a/src/main/java/tschipp/carryon/common/event/ItemEvents.java +++ b/src/main/java/tschipp/carryon/common/event/ItemEvents.java @@ -18,13 +18,14 @@ import net.minecraftforge.event.entity.player.PlayerInteractEvent; import net.minecraftforge.fml.common.eventhandler.Event.Result; import net.minecraftforge.fml.common.eventhandler.EventPriority; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; +import tschipp.carryon.common.config.CarryOnConfig; import tschipp.carryon.common.handler.ForbiddenTileHandler; import tschipp.carryon.common.handler.RegistrationHandler; import tschipp.carryon.common.item.ItemTile; public class ItemEvents { - + @SubscribeEvent(priority = EventPriority.HIGH) public void onBlockClick(PlayerInteractEvent.RightClickBlock event) { @@ -74,7 +75,6 @@ public class ItemEvents } } - @SubscribeEvent public void onBlockRightClick(PlayerInteractEvent.RightClickBlock event) { @@ -91,17 +91,21 @@ public class ItemEvents ItemStack stack = new ItemStack(RegistrationHandler.itemTile); TileEntity te = world.getTileEntity(pos); - if (te != null && (block.getBlockHardness(state, world, pos) != -1 || player.isCreative())) + if ((CarryOnConfig.settings.pickupAllBlocks ? true : te != null) && (block.getBlockHardness(state, world, pos) != -1 || player.isCreative())) { + double distance = pos.distanceSqToCenter(player.posX, player.posY + 0.5, player.posZ); - if (!ItemTile.isLocked(pos, world)) + if (distance < Math.pow(CarryOnConfig.settings.maxDistance, 2)) { - if (ItemTile.storeTileData(te, state.getActualState(world, pos), stack)) + if (!ItemTile.isLocked(pos, world)) { - world.removeTileEntity(pos); - world.setBlockToAir(pos); - player.setHeldItem(EnumHand.MAIN_HAND, stack); - event.setUseBlock(Result.DENY); + if (ItemTile.storeTileData(te, world, pos, state.getActualState(world, pos), stack)) + { + world.removeTileEntity(pos); + world.setBlockToAir(pos); + player.setHeldItem(EnumHand.MAIN_HAND, stack); + event.setUseBlock(Result.DENY); + } } } diff --git a/src/main/java/tschipp/carryon/common/item/ItemTile.java b/src/main/java/tschipp/carryon/common/item/ItemTile.java index c406e71..1361f2a 100644 --- a/src/main/java/tschipp/carryon/common/item/ItemTile.java +++ b/src/main/java/tschipp/carryon/common/item/ItemTile.java @@ -4,6 +4,8 @@ import java.util.Arrays; import java.util.Iterator; import java.util.List; +import javax.annotation.Nullable; + import net.minecraft.block.Block; import net.minecraft.block.BlockDirectional; import net.minecraft.block.BlockHorizontal; @@ -82,17 +84,17 @@ public class ItemTile extends Item { boolean hasDirection = false; boolean hasAllDirection = false; - + Iterator> iterator = containedblock.getDefaultState().getPropertyKeys().iterator(); while (iterator.hasNext()) { IProperty prop = iterator.next(); Object[] allowedValues = prop.getAllowedValues().toArray(); - + if (prop instanceof PropertyDirection && this.equal(allowedValues, EnumFacing.HORIZONTALS)) hasDirection = true; - - if(prop instanceof PropertyDirection && this.equal(allowedValues, EnumFacing.VALUES)) + + if (prop instanceof PropertyDirection && this.equal(allowedValues, EnumFacing.VALUES)) { hasAllDirection = true; facing2 = EnumFacing.getFacingFromVector((float) vec.xCoord, (float) vec.yCoord, (float) vec.zCoord); @@ -100,7 +102,7 @@ public class ItemTile extends Item } - if(hasAllDirection) + if (hasAllDirection) world.setBlockState(pos2, containedstate.withProperty(BlockDirectional.FACING, facing2.getOpposite())); else if (hasDirection) world.setBlockState(pos2, containedstate.withProperty(BlockHorizontal.FACING, facing2.getOpposite())); @@ -153,16 +155,17 @@ public class ItemTile extends Item return false; } - public static boolean storeTileData(TileEntity tile, IBlockState state, ItemStack stack) + public static boolean storeTileData(@Nullable TileEntity tile, World world, BlockPos pos, IBlockState state, ItemStack stack) { - if (tile == null) + if (CarryOnConfig.settings.pickupAllBlocks ? false : tile == null) return false; if (stack.isEmpty()) return false; NBTTagCompound chest = new NBTTagCompound(); - chest = tile.writeToNBT(chest); + if (tile != null) + chest = tile.writeToNBT(chest); NBTTagCompound tag = stack.hasTagCompound() ? stack.getTagCompound() : new NBTTagCompound(); if (tag.hasKey(TILE_DATA_KEY)) @@ -170,8 +173,8 @@ public class ItemTile extends Item tag.setTag(TILE_DATA_KEY, chest); - ItemStack drop = state.getBlock().getItem(tile.getWorld(), tile.getPos(), state); - + ItemStack drop = state.getBlock().getItem(world, pos, state); + tag.setString("block", state.getBlock().getRegistryName().toString()); Item item = Item.getItemFromBlock(state.getBlock()); tag.setInteger("meta", drop.getItemDamage()); @@ -228,7 +231,7 @@ public class ItemTile extends Item { return new ItemStack(getBlock(stack), 1, getMeta(stack)); } - + public static IBlockState getBlockState(ItemStack stack) { if (stack.hasTagCompound()) @@ -239,43 +242,42 @@ public class ItemTile extends Item } return Blocks.AIR.getDefaultState(); } - - + public static boolean isLocked(BlockPos pos, World world) - { + { TileEntity te = world.getTileEntity(pos); - if(te != null) + if (te != null) { NBTTagCompound tag = new NBTTagCompound(); te.writeToNBT(tag); return tag.hasKey("Lock") ? !tag.getString("Lock").equals("") : false; } - return true; + return false; } - + private boolean equal(Object[] a, Object[] b) { if (a.length != b.length) return false; - + List lA = Arrays.asList(a); List lB = Arrays.asList(b); return lA.containsAll(lB); } - + private int potionLevel(ItemStack stack) { String nbt = getTileData(stack).toString(); int i = nbt.length() / 500; - - if(i > 4) + + if (i > 4) i = 4; - - if(!CarryOnConfig.settings.heavyTiles) + + if (!CarryOnConfig.settings.heavyTiles) i = 1; - + return i; } }