Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

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.

You have to get EntityPlayerMP instance and use .inventory.setInventorySlotContents(slotNumber, itemStack).

  • Author

You have to get EntityPlayerMP instance and use .inventory.setInventorySlotContents(slotNumber, itemStack).

 

How do I get the EntityPlayerMP from the event?

  • Author

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.