Jump to content

Recommended Posts

Posted

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.

Posted

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());
}
}

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.