From dc9abc918780d58f1c08c39de2ea0b0f8e86a794 Mon Sep 17 00:00:00 2001 From: "restyled-io[bot]" <32688539+restyled-io[bot]@users.noreply.github.com> Date: Tue, 11 Jan 2022 22:24:44 -0500 Subject: [PATCH] Restyle 1.7-final ItemFocusDislocation error handling (#1053) * Prevents crashing and block deletion when using Wand Focus: Dislocation on some poorly-coded blocks Previously, when Wand Focus: Dislocation was used to pick up certain blocks (mostly blocks whose breakBlock functions assume the block's tile entity exists and is valid), the game would crash and the block would be destroyed. Now, instead of crashing, the game logs the relevant information and otherwise "fails silently". This may have some purely-visual side-effects, but the crash has been fixed. * Restyled by clang-format Co-authored-by: SarahKirksey Co-authored-by: Restyled.io --- .../item/foci/ItemFocusDislocation.java | 455 ++++++++++-------- 1 file changed, 264 insertions(+), 191 deletions(-) diff --git a/src/main/java/thaumic/tinkerer/common/item/foci/ItemFocusDislocation.java b/src/main/java/thaumic/tinkerer/common/item/foci/ItemFocusDislocation.java index 1d43c3d5c..09f556572 100644 --- a/src/main/java/thaumic/tinkerer/common/item/foci/ItemFocusDislocation.java +++ b/src/main/java/thaumic/tinkerer/common/item/foci/ItemFocusDislocation.java @@ -16,6 +16,7 @@ import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; +import java.util.ArrayList; import net.minecraft.block.Block; import net.minecraft.client.renderer.texture.IIconRegister; import net.minecraft.entity.player.EntityPlayer; @@ -47,230 +48,302 @@ import thaumic.tinkerer.common.research.ResearchHelper; import thaumic.tinkerer.common.research.TTResearchItem; -import java.util.ArrayList; - public class ItemFocusDislocation extends ItemModFocus { - private static final String TAG_AVAILABLE = "available"; - private static final String TAG_TILE_CMP = "tileCmp"; - @Deprecated - private static final String TAG_BLOCK_ID = "blockID"; - private static final String TAG_BLOCK_NAME = "blockName"; - private static final String TAG_BLOCK_META = "blockMeta"; - private static final AspectList visUsage = new AspectList().add(Aspect.ENTROPY, 500).add(Aspect.ORDER, 500).add(Aspect.EARTH, 100); - private static final AspectList visUsageTile = new AspectList().add(Aspect.ENTROPY, 2500).add(Aspect.ORDER, 2500).add(Aspect.EARTH, 500); - private static final AspectList visUsageSpawner = new AspectList().add(Aspect.ENTROPY, 10000).add(Aspect.ORDER, 10000).add(Aspect.EARTH, 5000); - private static ArrayList blacklist = new ArrayList(); - private IIcon ornament; - - private static AspectList getCost(TileEntity tile) { - return tile == null ? visUsage : tile instanceof TileEntityMobSpawner ? visUsageSpawner : visUsageTile; - } - - @Override - @SideOnly(Side.CLIENT) - public void registerIcons(IIconRegister par1IconRegister) { - super.registerIcons(par1IconRegister); - ornament = IconHelper.forItem(par1IconRegister, this, "Orn"); - } + private static final String TAG_AVAILABLE = "available"; + private static final String TAG_TILE_CMP = "tileCmp"; + @Deprecated private static final String TAG_BLOCK_ID = "blockID"; + private static final String TAG_BLOCK_NAME = "blockName"; + private static final String TAG_BLOCK_META = "blockMeta"; + private static final AspectList visUsage = new AspectList() + .add(Aspect.ENTROPY, 500) + .add(Aspect.ORDER, 500) + .add(Aspect.EARTH, 100); + private static final AspectList visUsageTile = new AspectList() + .add(Aspect.ENTROPY, 2500) + .add(Aspect.ORDER, 2500) + .add(Aspect.EARTH, 500); + private static final AspectList visUsageSpawner = + new AspectList() + .add(Aspect.ENTROPY, 10000) + .add(Aspect.ORDER, 10000) + .add(Aspect.EARTH, 5000); + private static ArrayList blacklist = new ArrayList(); + private IIcon ornament; - @Override - public ItemStack onFocusRightClick(ItemStack itemstack, World world, EntityPlayer player, MovingObjectPosition mop) { - if (mop == null) - return itemstack; + private static AspectList getCost(TileEntity tile) { + return tile == null ? visUsage + : tile instanceof TileEntityMobSpawner ? visUsageSpawner + : visUsageTile; + } - Block block = world.getBlock(mop.blockX, mop.blockY, mop.blockZ); - int meta = world.getBlockMetadata(mop.blockX, mop.blockY, mop.blockZ); - TileEntity tile = world.getTileEntity(mop.blockX, mop.blockY, mop.blockZ); - ItemWandCasting wand = (ItemWandCasting) itemstack.getItem(); + @Override + @SideOnly(Side.CLIENT) + public void registerIcons(IIconRegister par1IconRegister) { + super.registerIcons(par1IconRegister); + ornament = IconHelper.forItem(par1IconRegister, this, "Orn"); + } - if (player.canPlayerEdit(mop.blockX, mop.blockY, mop.blockZ, mop.sideHit, itemstack)) { - ItemStack stack = getPickedBlock(itemstack); - if (stack != null) { - if (mop.sideHit == 0) - --mop.blockY; - if (mop.sideHit == 1) - ++mop.blockY; - if (mop.sideHit == 2) - --mop.blockZ; - if (mop.sideHit == 3) - ++mop.blockZ; - if (mop.sideHit == 4) - --mop.blockX; - if (mop.sideHit == 5) - ++mop.blockX; + @Override + public ItemStack onFocusRightClick(ItemStack itemstack, World world, + EntityPlayer player, + MovingObjectPosition mop) { + if (mop == null) + return itemstack; - if (block.canPlaceBlockOnSide(world, mop.blockX, mop.blockY, mop.blockZ, mop.sideHit)) { - if (!world.isRemote) { - world.setBlock(mop.blockX, mop.blockY, mop.blockZ, ((ItemBlock) stack.getItem()).field_150939_a, stack.getItemDamage(), 1 | 2); - ((ItemBlock) stack.getItem()).field_150939_a.onBlockPlacedBy(world, mop.blockX, mop.blockY, mop.blockZ, player, itemstack); - NBTTagCompound tileCmp = getStackTileEntity(itemstack); - if (tileCmp != null && !tileCmp.hasNoTags()) { - TileEntity tile1 = TileEntity.createAndLoadEntity(tileCmp); - tile1.xCoord = mop.blockX; - tile1.yCoord = mop.blockY; - tile1.zCoord = mop.blockZ; - world.setTileEntity(mop.blockX, mop.blockY, mop.blockZ, tile1); - } - } else player.swingItem(); - clearPickedBlock(itemstack); + Block block = world.getBlock(mop.blockX, mop.blockY, mop.blockZ); + int meta = world.getBlockMetadata(mop.blockX, mop.blockY, mop.blockZ); + TileEntity tile = world.getTileEntity(mop.blockX, mop.blockY, mop.blockZ); + ItemWandCasting wand = (ItemWandCasting)itemstack.getItem(); - for (int i = 0; i < 8; i++) { - float x = (float) (mop.blockX + Math.random()); - float y = (float) (mop.blockY + Math.random()) + 0.65F; - float z = (float) (mop.blockZ + Math.random()); - ThaumicTinkerer.tcProxy.burst(world, x, y, z, 0.2F); - } - world.playSoundAtEntity(player, "thaumcraft:wand", 0.5F, 1F); - } - } else if (!blacklist.contains(block) && !ThaumcraftApi.portableHoleBlackList.contains(block) && block != null && block.getBlockHardness(world, mop.blockX, mop.blockY, mop.blockZ) != -1F && wand.consumeAllVis(itemstack, player, getCost(tile), true, false)) { - if (!world.isRemote) { - world.removeTileEntity(mop.blockX, mop.blockY, mop.blockZ); - world.setBlock(mop.blockX, mop.blockY, mop.blockZ, Blocks.air, 0, 1 | 2); - storePickedBlock(itemstack, block, (short) meta, tile); - } + if (player.canPlayerEdit(mop.blockX, mop.blockY, mop.blockZ, mop.sideHit, + itemstack)) { + ItemStack stack = getPickedBlock(itemstack); + if (stack != null) { + if (mop.sideHit == 0) + --mop.blockY; + if (mop.sideHit == 1) + ++mop.blockY; + if (mop.sideHit == 2) + --mop.blockZ; + if (mop.sideHit == 3) + ++mop.blockZ; + if (mop.sideHit == 4) + --mop.blockX; + if (mop.sideHit == 5) + ++mop.blockX; - for (int i = 0; i < 8; i++) { - float x = (float) (mop.blockX + Math.random()); - float y = (float) (mop.blockY + Math.random()); - float z = (float) (mop.blockZ + Math.random()); - ThaumicTinkerer.tcProxy.burst(world, x, y, z, 0.2F); - } - world.playSoundAtEntity(player, block.stepSound.getBreakSound(), 1F, 1F); - world.playSoundAtEntity(player, "thaumcraft:wand", 0.5F, 1F); + if (block.canPlaceBlockOnSide(world, mop.blockX, mop.blockY, mop.blockZ, + mop.sideHit)) { + if (!world.isRemote) { + world.setBlock(mop.blockX, mop.blockY, mop.blockZ, + ((ItemBlock)stack.getItem()).field_150939_a, + stack.getItemDamage(), 1 | 2); + ((ItemBlock)stack.getItem()) + .field_150939_a.onBlockPlacedBy(world, mop.blockX, mop.blockY, + mop.blockZ, player, itemstack); + NBTTagCompound tileCmp = getStackTileEntity(itemstack); + if (tileCmp != null && !tileCmp.hasNoTags()) { + TileEntity tile1 = TileEntity.createAndLoadEntity(tileCmp); + tile1.xCoord = mop.blockX; + tile1.yCoord = mop.blockY; + tile1.zCoord = mop.blockZ; + world.setTileEntity(mop.blockX, mop.blockY, mop.blockZ, tile1); + } + } else + player.swingItem(); + clearPickedBlock(itemstack); - if (world.isRemote) - player.swingItem(); + for (int i = 0; i < 8; i++) { + float x = (float)(mop.blockX + Math.random()); + float y = (float)(mop.blockY + Math.random()) + 0.65F; + float z = (float)(mop.blockZ + Math.random()); + ThaumicTinkerer.tcProxy.burst(world, x, y, z, 0.2F); + } + world.playSoundAtEntity(player, "thaumcraft:wand", 0.5F, 1F); + } + } else if (!blacklist.contains(block) && + !ThaumcraftApi.portableHoleBlackList.contains(block) && + block != null && + block.getBlockHardness(world, mop.blockX, mop.blockY, + mop.blockZ) != -1F && + wand.consumeAllVis(itemstack, player, getCost(tile), true, + false)) { + if (!world.isRemote) { + try { + storePickedBlock(itemstack, block, (short)meta, tile); + world.removeTileEntity(mop.blockX, mop.blockY, mop.blockZ); + world.setBlock(mop.blockX, mop.blockY, mop.blockZ, Blocks.air, 0, + 1 | 2); + } catch (Exception e) { + ItemStack pickedBlock = getPickedBlock(itemstack); + String pickedBlockName = + (pickedBlock == null + ? "a null block (how?)" + : "'" + getPickedBlock(itemstack).getUnlocalizedName()) + + "'"; + ThaumicTinkerer.log.error( + "Attempting to use Wand Focus: Dislocation on " + + pickedBlockName + " resulted in an exception being thrown."); + if (tile != null) { + ThaumicTinkerer.log.debug( + "Tile entity's NBT is as follows: " + + getStackTileEntity(itemstack).toString()); + } + ThaumicTinkerer.log.debug("Exception details:"); + for (Object line : e.getStackTrace()) { + ThaumicTinkerer.log.debug(line.toString()); } + ThaumicTinkerer.log.debug("(End stack trace)"); + } } - return itemstack; - } + for (int i = 0; i < 8; i++) { + float x = (float)(mop.blockX + Math.random()); + float y = (float)(mop.blockY + Math.random()); + float z = (float)(mop.blockZ + Math.random()); + ThaumicTinkerer.tcProxy.burst(world, x, y, z, 0.2F); + } + world.playSoundAtEntity(player, block.stepSound.getBreakSound(), 1F, + 1F); + world.playSoundAtEntity(player, "thaumcraft:wand", 0.5F, 1F); - @Override - public String getSortingHelper(ItemStack itemstack) { - return "DISLOCATION" + getUniqueKey(itemstack); + if (world.isRemote) + player.swingItem(); + } } - public String getUniqueKey(ItemStack itemstack) { - ItemStack stack = getPickedBlock(itemstack); - if (stack == null) - return ""; - String name = stack.getUnlocalizedName(); - int datahash = 0; - if (stack.getTagCompound() != null) { - datahash = stack.getTagCompound().hashCode(); - } - return String.format("%s-%d", name, datahash); - } + return itemstack; + } - public ItemStack getPickedBlock(ItemStack stack) { - ItemStack focus; - if (stack.getItem() instanceof ItemWandCasting) { - ItemWandCasting wand = (ItemWandCasting) stack.getItem(); - focus = wand.getFocusItem(stack); - } else { - focus = stack; - } - return (ItemNBTHelper.getBoolean(focus, TAG_AVAILABLE, false)) ? getPickedBlockStack(stack) : null; - } + @Override + public String getSortingHelper(ItemStack itemstack) { + return "DISLOCATION" + getUniqueKey(itemstack); + } - public ItemStack getPickedBlockStack(ItemStack stack) { - ItemStack focus; - if (stack.getItem() instanceof ItemWandCasting) { - ItemWandCasting wand = (ItemWandCasting) stack.getItem(); - focus = wand.getFocusItem(stack); - } else { - focus = stack; - } - String name = ItemNBTHelper.getString(focus, TAG_BLOCK_NAME, ""); - Block block = Block.getBlockFromName(name); - if (block == Blocks.air) { - int id = ItemNBTHelper.getInt(focus, TAG_BLOCK_ID, 0); - block = Block.getBlockById(id); - } - int meta = ItemNBTHelper.getInt(focus, TAG_BLOCK_META, 0); - ItemStack stck; - //if(block instanceof BlockReed) - //{ - // stck=new ItemStack(Items.reeds,1,meta); - //} - stck = new ItemStack(new ItemBlock(block), 1, meta); - return stck; + public String getUniqueKey(ItemStack itemstack) { + ItemStack stack = getPickedBlock(itemstack); + if (stack == null) + return ""; + String name = stack.getUnlocalizedName(); + int datahash = 0; + if (stack.getTagCompound() != null) { + datahash = stack.getTagCompound().hashCode(); } + return String.format("%s-%d", name, datahash); + } - public NBTTagCompound getStackTileEntity(ItemStack stack) { - ItemStack focus; - if (stack.getItem() instanceof ItemWandCasting) { - ItemWandCasting wand = (ItemWandCasting) stack.getItem(); - focus = wand.getFocusItem(stack); - } else { - focus = stack; - } - return ItemNBTHelper.getCompound(focus, TAG_TILE_CMP, true); + public ItemStack getPickedBlock(ItemStack stack) { + ItemStack focus; + if (stack.getItem() instanceof ItemWandCasting) { + ItemWandCasting wand = (ItemWandCasting)stack.getItem(); + focus = wand.getFocusItem(stack); + } else { + focus = stack; } + return (ItemNBTHelper.getBoolean(focus, TAG_AVAILABLE, false)) + ? getPickedBlockStack(stack) + : null; + } - private void storePickedBlock(ItemStack stack, Block block, short meta, TileEntity tile) { - ItemWandCasting wand = (ItemWandCasting) stack.getItem(); - ItemStack focus = wand.getFocusItem(stack); - String blockName = Block.blockRegistry.getNameForObject(block); - ItemNBTHelper.setString(focus, TAG_BLOCK_NAME, blockName); - ItemNBTHelper.setInt(focus, TAG_BLOCK_META, meta); - NBTTagCompound cmp = new NBTTagCompound(); - if (tile != null) - tile.writeToNBT(cmp); - ItemNBTHelper.setCompound(focus, TAG_TILE_CMP, cmp); - ItemNBTHelper.setBoolean(focus, TAG_AVAILABLE, true); - wand.setFocus(stack, focus); + public ItemStack getPickedBlockStack(ItemStack stack) { + ItemStack focus; + if (stack.getItem() instanceof ItemWandCasting) { + ItemWandCasting wand = (ItemWandCasting)stack.getItem(); + focus = wand.getFocusItem(stack); + } else { + focus = stack; } - - private void clearPickedBlock(ItemStack stack) { - ItemWandCasting wand = (ItemWandCasting) stack.getItem(); - ItemStack focus = wand.getFocusItem(stack); - ItemNBTHelper.setBoolean(focus, TAG_AVAILABLE, false); - wand.setFocus(stack, focus); + String name = ItemNBTHelper.getString(focus, TAG_BLOCK_NAME, ""); + Block block = Block.getBlockFromName(name); + if (block == Blocks.air) { + int id = ItemNBTHelper.getInt(focus, TAG_BLOCK_ID, 0); + block = Block.getBlockById(id); } + int meta = ItemNBTHelper.getInt(focus, TAG_BLOCK_META, 0); + ItemStack stck; + // if(block instanceof BlockReed) + //{ + // stck=new ItemStack(Items.reeds,1,meta); + // } + stck = new ItemStack(new ItemBlock(block), 1, meta); + return stck; + } - @Override - public int getFocusColor(ItemStack stack) { - return 0xFFB200; + public NBTTagCompound getStackTileEntity(ItemStack stack) { + ItemStack focus; + if (stack.getItem() instanceof ItemWandCasting) { + ItemWandCasting wand = (ItemWandCasting)stack.getItem(); + focus = wand.getFocusItem(stack); + } else { + focus = stack; } + return ItemNBTHelper.getCompound(focus, TAG_TILE_CMP, true); + } - @Override - public IIcon getOrnament(ItemStack stack) { - return ornament; - } + private void storePickedBlock(ItemStack stack, Block block, short meta, + TileEntity tile) { + ItemWandCasting wand = (ItemWandCasting)stack.getItem(); + ItemStack focus = wand.getFocusItem(stack); + String blockName = Block.blockRegistry.getNameForObject(block); + ItemNBTHelper.setString(focus, TAG_BLOCK_NAME, blockName); + ItemNBTHelper.setInt(focus, TAG_BLOCK_META, meta); + NBTTagCompound cmp = new NBTTagCompound(); + if (tile != null) + tile.writeToNBT(cmp); + ItemNBTHelper.setCompound(focus, TAG_TILE_CMP, cmp); + ItemNBTHelper.setBoolean(focus, TAG_AVAILABLE, true); + wand.setFocus(stack, focus); + } - @Override - public AspectList getVisCost(ItemStack stack) { - return visUsage; - } + private void clearPickedBlock(ItemStack stack) { + ItemWandCasting wand = (ItemWandCasting)stack.getItem(); + ItemStack focus = wand.getFocusItem(stack); + ItemNBTHelper.setBoolean(focus, TAG_AVAILABLE, false); + wand.setFocus(stack, focus); + } + @Override + public int getFocusColor(ItemStack stack) { + return 0xFFB200; + } - static { - blacklist.add(Blocks.piston_extension); - blacklist.add(Blocks.piston_head); + @Override + public IIcon getOrnament(ItemStack stack) { + return ornament; + } - } + @Override + public AspectList getVisCost(ItemStack stack) { + return visUsage; + } - @Override - public String getItemName() { - return LibItemNames.FOCUS_DISLOCATION; - } + static { + blacklist.add(Blocks.piston_extension); + blacklist.add(Blocks.piston_head); + } - @Override - public IRegisterableResearch getResearchItem() { - if (!Config.allowMirrors) { - return null; - } - return (TTResearchItem) new TTResearchItem(LibResearch.KEY_FOCUS_DISLOCATION, new AspectList().add(Aspect.ELDRITCH, 2).add(Aspect.MAGIC, 1).add(Aspect.EXCHANGE, 1), -5, -5, 2, new ItemStack(this)).setSecondary().setParents(LibResearch.KEY_FOCUS_FLIGHT).setConcealed() - .setPages(new ResearchPage("0"), new ResearchPage("1"), ResearchHelper.infusionPage(LibResearch.KEY_FOCUS_DISLOCATION)); + @Override + public String getItemName() { + return LibItemNames.FOCUS_DISLOCATION; + } + @Override + public IRegisterableResearch getResearchItem() { + if (!Config.allowMirrors) { + return null; } + return (TTResearchItem) new TTResearchItem( + LibResearch.KEY_FOCUS_DISLOCATION, + new AspectList() + .add(Aspect.ELDRITCH, 2) + .add(Aspect.MAGIC, 1) + .add(Aspect.EXCHANGE, 1), + -5, -5, 2, new ItemStack(this)) + .setSecondary() + .setParents(LibResearch.KEY_FOCUS_FLIGHT) + .setConcealed() + .setPages( + new ResearchPage("0"), new ResearchPage("1"), + ResearchHelper.infusionPage(LibResearch.KEY_FOCUS_DISLOCATION)); + } - @Override - public ThaumicTinkererRecipe getRecipeItem() { - return new ThaumicTinkererInfusionRecipe(LibResearch.KEY_FOCUS_DISLOCATION, new ItemStack(this), 8, new AspectList().add(Aspect.ELDRITCH, 20).add(Aspect.DARKNESS, 10).add(Aspect.VOID, 25).add(Aspect.MAGIC, 20).add(Aspect.TAINT, 5), new ItemStack(Items.ender_pearl), - new ItemStack(Items.quartz), new ItemStack(Items.quartz), new ItemStack(Items.quartz), new ItemStack(Items.quartz), new ItemStack(ConfigItems.itemResource, 1, 6), new ItemStack(ConfigItems.itemResource, 1, 6), new ItemStack(ConfigItems.itemResource, 1, 6), new ItemStack(Items.diamond)); - } + @Override + public ThaumicTinkererRecipe getRecipeItem() { + return new ThaumicTinkererInfusionRecipe( + LibResearch.KEY_FOCUS_DISLOCATION, new ItemStack(this), 8, + new AspectList() + .add(Aspect.ELDRITCH, 20) + .add(Aspect.DARKNESS, 10) + .add(Aspect.VOID, 25) + .add(Aspect.MAGIC, 20) + .add(Aspect.TAINT, 5), + new ItemStack(Items.ender_pearl), new ItemStack(Items.quartz), + new ItemStack(Items.quartz), new ItemStack(Items.quartz), + new ItemStack(Items.quartz), + new ItemStack(ConfigItems.itemResource, 1, 6), + new ItemStack(ConfigItems.itemResource, 1, 6), + new ItemStack(ConfigItems.itemResource, 1, 6), + new ItemStack(Items.diamond)); + } }