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

I'm trying to set up a mode changing system wherein when the user presses a key, the mode int tag of an ItemStack's NBT is incremented by 1. 

@SubscribeEvent
public void keyDown(TickEvent.ClientTickEvent e) {
   if (e.phase == TickEvent.Phase.END) {
      Minecraft mc = Minecraft.getMinecraft();

      if (mc.world != null) {
         ItemStack stack = mc.player.getHeldItemMainhand();

         if (key.isPressed())
            if (stack != null) {
               IModeItem item = (IModeItem) stack.getItem();
               if (item instanceof ItemVoidVacuum) {
                  if (item.getMode(stack) != 0) {
                     int result = item.getMode(stack) == 1 ? 2 : 1;
                     item.setModeClient(stack, result);
                     ChromEngPacketHandler.INSTANCE.sendToServer(new MessageSetMode(stack, result));
                  }
               }
            }
      }
   }

 

default void setModeClient(ItemStack stack, int i) {
   ItemStackUtil.getNBT(stack).setInteger("Mode", i);
}

 

public class MessageSetMode  implements IMessage{

   private ItemStack stack;
   private int modeNo;

   public MessageSetMode() {
   }

   public MessageSetMode(ItemStack stack, int modeNo) {
      this.stack = stack;
      this.modeNo = modeNo;
   }

   @Override
   public void fromBytes(ByteBuf buf) {
      this.stack = ByteBufUtils.readItemStack(buf);
      this.modeNo = buf.readInt();
   }

   @Override
   public void toBytes(ByteBuf buf) {
      ByteBufUtils.writeItemStack(buf, this.stack);
      buf.writeInt(modeNo);
   }

   public static class MessageSetModeHandler implements IMessageHandler<MessageSetMode, IMessage> {

      @Override
      public IMessage onMessage(MessageSetMode message, MessageContext ctx) {
         ((IModeItem) message.stack.getItem()).setModeClient(message.stack, message.modeNo);
         return null;
      }
   }
}

 

However, when this conditional statement is called: 

if (getMode(player.getHeldItemMainhand()) == 0) {
   NBTUtil.writeBlockState(voidNBT, worldIn.getBlockState(pos));
   worldIn.setBlockToAir(pos);
   setModeClient(player.getHeldItemMainhand(), prefMode);
} else if (getMode(player.getHeldItemMainhand()) == 1) {
   worldIn.setBlockState(pos.offset(facing), NBTUtil.readBlockState(voidNBT));
   worldIn.spawnParticle(EnumParticleTypes.END_ROD, pos.offset(facing).getX(), pos.offset(facing).getY(), pos.offset(facing).getZ(), 5, 5, 5);
   setModeClient(player.getHeldItemMainhand(), 0);
   prefMode = 1;
} else if (getMode(player.getHeldItemMainhand()) == 2) {
   NBTUtil.writeBlockState(voidNBT, Blocks.AIR.getDefaultState());
   setModeClient(player.getHeldItemMainhand(), 0);
   prefMode = 2;
}

 

default int getMode(ItemStack stack) {
   return ItemStackUtil.getNBT(stack).getInteger("Mode");
}

 

On the client side, when the mode is equal to 2, the final else if is called, while on the server side, the first else if is called. I checked while debugging and when the mode is switched, the value on client and server side is 2, but when getMode is called in the Item class, the client's mode is 2 while the server's is 1. Sorry if the answer is painfully obvious, I've been trying the whole day and can't see the problem.

  • Author

Thanks, it worked. I tried passing 

ctx.getServerHandler().entityPlayer.getActiveItemStack()

for the ItemStack near the start but it didn't work, but calling getHeldItemMainhand() worked instead.

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.