Jump to content

Recommended Posts

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.

Posted

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

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