ColdFox Posted September 21, 2013 Posted September 21, 2013 So I've this code: @ForgeSubscribe public void preventInvalidBlocks(PlayerInteractEvent event) { try { if (event.action == event.action.RIGHT_CLICK_BLOCK && Block.blocksList[event.entityPlayer.inventory.getCurrentItem().itemID].getFlammability(null, 0, 0, 0, 0, null)>0) { int x = event.x; int y = event.y; int z = event.z; int face = event.face; switch(face){ case 0: --y; break; case 1: ++y; break; case 2: --z; break; case 3: ++z; break; case 4: --x; break; case 5: ++x; break; } if(event.entityPlayer.inventory.getCurrentItem().stackSize>0) event.entityPlayer.inventory.getCurrentItem().stackSize--; //It seems to be necessary otherwise the player wouldn't catch on fire when walking over it for (int j = 0; j < MinecraftServer.getServer().worldServers.length; j++) { MinecraftServer.getServer().worldServers[j].setBlock(x, y, z, Block.fire.blockID); } event.setCanceled(true); System.out.println("Invalid block placed!"); } } catch (NullPointerException e) { System.out.println(e.getMessage()); } } and it places fire instead the block. this part works well, the only problem seems to be reducing the size of the itemstack. I tried many ways and didn't succeed. What happens is that the stacks loses an item but it have it back immediately. Example: I've 64 wooden planks. I place one on the ground and it's replaced by fire. The stacksize drops to 63 and goes back to 64. Quote
Alexiy Posted September 22, 2013 Posted September 22, 2013 You have to get EntityPlayerMP instance and use .inventory.setInventorySlotContents(slotNumber, itemStack). Quote
ColdFox Posted September 22, 2013 Author Posted September 22, 2013 On 9/22/2013 at 7:35 AM, Alexiy said: You have to get EntityPlayerMP instance and use .inventory.setInventorySlotContents(slotNumber, itemStack). How do I get the EntityPlayerMP from the event? Quote
ColdFox Posted September 24, 2013 Author Posted September 24, 2013 On 9/22/2013 at 1:58 PM, ColdFox said: Quote You have to get EntityPlayerMP instance and use .inventory.setInventorySlotContents(slotNumber, itemStack). How do I get the EntityPlayerMP from the event? I made it work like this @ForgeSubscribe public void preventInvalidBlocks(PlayerInteractEvent event) { try { if (event.action == event.action.RIGHT_CLICK_BLOCK && Block.blocksList[event.entityPlayer.inventory.getCurrentItem().itemID].getFlammability(null, 0, 0, 0, 0, null)>0) { int x = event.x; int y = event.y; int z = event.z; int face = event.face; switch(face){ case 0: --y; break; case 1: ++y; break; case 2: --z; break; case 3: ++z; break; case 4: --x; break; case 5: ++x; break; } if(event.entityLiving instanceof EntityPlayer) { EntityPlayer player = (EntityPlayer) event.entityLiving; //player.inventory.decrStackSize(player.inventory.currentItem, 1); int newStackSize = player.inventory.mainInventory[player.inventory.currentItem].stackSize - 1; for (int j = 0; j < MinecraftServer.getServer().worldServers.length; j++) { if(player.inventory.mainInventory[player.inventory.currentItem].stackSize != newStackSize){ MinecraftServer.getServer().worldServers[j].getPlayerEntityByName(player.username). inventory.decrStackSize(player.inventory.currentItem, 1); //It seems to be necessary otherwise the player wouldn't catch on fire when walking over it MinecraftServer.getServer().worldServers[j].setBlock(x, y, z, Block.fire.blockID); break; } } } event.setCanceled(true); System.out.println("Invalid block placed!"); } } catch (NullPointerException e) { System.out.println(e.getMessage()); } } Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.