From 8dc5a0e5d677b4fdec6f38714d8d91d4ebe21898 Mon Sep 17 00:00:00 2001 From: Nicolas BARBOTIN Date: Tue, 30 Jan 2018 20:59:36 +0100 Subject: [PATCH] * Upgrades are now dropped when the screen is destroyed --- README.md | 1 - .../net/montoyo/wd/block/BlockScreen.java | 27 +++++++---- .../montoyo/wd/entity/TileEntityScreen.java | 45 ++++++++++++------- 3 files changed, 46 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index a9af664..4c02918 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,6 @@ This is the unfinished port of the WebDisplays mod for Minecraft 1.12.2. The tex ### TODO * VideoType doesn't seem to be used... -* DROP UPGRADES WHEN SCREEN IS DESTROYED * Achievements (minePad 2 and all that stuff) * Top/bottom screen orientation * GuiSetURL2 missing buttons diff --git a/src/main/java/net/montoyo/wd/block/BlockScreen.java b/src/main/java/net/montoyo/wd/block/BlockScreen.java index 84bbbb2..354a43f 100644 --- a/src/main/java/net/montoyo/wd/block/BlockScreen.java +++ b/src/main/java/net/montoyo/wd/block/BlockScreen.java @@ -227,34 +227,38 @@ public class BlockScreen extends WDBlockContainer { return meta == 0 ? null : new TileEntityScreen(); } - @Override - public void onBlockDestroyedByPlayer(World world, BlockPos pos, IBlockState dontCare) { + /************************************************* DESTRUCTION HANDLING *************************************************/ + + private void onDestroy(World world, BlockPos pos, EntityPlayer ply) { if(!world.isRemote) { Vector3i bp = new Vector3i(pos); Multiblock.BlockOverride override = new Multiblock.BlockOverride(bp, Multiblock.OverrideAction.SIMULATE); for(BlockSide bs: BlockSide.values()) - destroySide(world, bp.clone(), bs, override); + destroySide(world, bp.clone(), bs, override, ply); } } - private void destroySide(World world, Vector3i pos, BlockSide side, Multiblock.BlockOverride override) { + private void destroySide(World world, Vector3i pos, BlockSide side, Multiblock.BlockOverride override, EntityPlayer source) { Multiblock.findOrigin(world, pos, side, override); BlockPos bp = pos.toBlock(); TileEntity te = world.getTileEntity(bp); - if(te != null && te instanceof TileEntityScreen) + if(te != null && te instanceof TileEntityScreen) { + ((TileEntityScreen) te).onDestroy(source); world.setBlockState(bp, getDefaultState().withProperty(hasTE, false)); //Destroy tile entity + } } @Override - public EnumPushReaction getMobilityFlag(IBlockState state) { - return EnumPushReaction.IGNORE; + public boolean removedByPlayer(IBlockState state, World world, BlockPos pos, EntityPlayer ply, boolean willHarvest) { + onDestroy(world, pos, ply); + return super.removedByPlayer(state, world, pos, ply, willHarvest); } @Override public void onBlockDestroyedByExplosion(World world, BlockPos pos, Explosion explosion) { - onBlockDestroyedByPlayer(world, pos, null); + onDestroy(world, pos, null); } @Override @@ -275,9 +279,14 @@ public class BlockScreen extends WDBlockContainer { for(Vector3i neighbor: neighbors) { if(world.getBlockState(neighbor.toBlock()).getBlock() instanceof BlockScreen) { for(BlockSide bs: BlockSide.values()) - destroySide(world, neighbor.clone(), bs, override); + destroySide(world, neighbor.clone(), bs, override, (whoDidThisShit instanceof EntityPlayer) ? ((EntityPlayer) whoDidThisShit) : null); } } } + @Override + public EnumPushReaction getMobilityFlag(IBlockState state) { + return EnumPushReaction.IGNORE; + } + } diff --git a/src/main/java/net/montoyo/wd/entity/TileEntityScreen.java b/src/main/java/net/montoyo/wd/entity/TileEntityScreen.java index dfac781..b36ba8c 100644 --- a/src/main/java/net/montoyo/wd/entity/TileEntityScreen.java +++ b/src/main/java/net/montoyo/wd/entity/TileEntityScreen.java @@ -719,23 +719,7 @@ public class TileEntityScreen extends TileEntity { } if(idxToRemove >= 0) { - if(!itemAsUpgrade.onRemove(this, side, player, scr.upgrades.get(idxToRemove))) { //Drop upgrade item - ItemStack toDrop = scr.upgrades.get(idxToRemove); - boolean spawnDrop = true; - - if(player != null) { - if(player.isCreative() || player.addItemStackToInventory(toDrop)) - spawnDrop = false; //If in creative or if the item was added to the player's inventory, don't spawn drop entity - } - - if(spawnDrop) { - Vector3f pos = new Vector3f((float) this.pos.getX(), (float) this.pos.getY(), (float) this.pos.getZ()); - pos.addMul(side.backward.toFloat(), 1.5f); - - world.spawnEntity(new EntityItem(world, (double) pos.x, (double) pos.y, (double) pos.z, toDrop)); - } - } - + dropUpgrade(scr.upgrades.get(idxToRemove), side, player); scr.upgrades.remove(idxToRemove); WebDisplays.NET_HANDLER.sendToAllAround(CMessageScreenUpdate.upgrade(this, side), point()); playSoundAt(WebDisplays.INSTANCE.soundUpgradeDel, pos, 1.0f, 1.0f); @@ -743,6 +727,24 @@ public class TileEntityScreen extends TileEntity { Log.warning("Tried to remove non-existing upgrade %s to screen %s at %s", safeName(is), side.toString(), pos.toString()); } + private void dropUpgrade(ItemStack is, BlockSide side, @Nullable EntityPlayer ply) { + if(!((IUpgrade) is.getItem()).onRemove(this, side, ply, is)) { //Drop upgrade item + boolean spawnDrop = true; + + if(ply != null) { + if(ply.isCreative() || ply.addItemStackToInventory(is)) + spawnDrop = false; //If in creative or if the item was added to the player's inventory, don't spawn drop entity + } + + if(spawnDrop) { + Vector3f pos = new Vector3f((float) this.pos.getX(), (float) this.pos.getY(), (float) this.pos.getZ()); + pos.addMul(side.backward.toFloat(), 1.5f); + + world.spawnEntity(new EntityItem(world, (double) pos.x, (double) pos.y, (double) pos.z, is)); + } + } + } + private Screen getScreenForLaserOp(BlockSide side, EntityPlayer ply) { if(world.isRemote) return null; @@ -795,4 +797,13 @@ public class TileEntityScreen extends TileEntity { } } + public void onDestroy(@Nullable EntityPlayer ply) { + for(Screen scr: screens) { + for(ItemStack is: scr.upgrades) + dropUpgrade(is, scr.side, ply); + + scr.upgrades.clear(); + } + } + }