Why do I have to create a new compass ItemStack to point to a Lodestone?

Discussion in 'Plugin Development' started by PalmLP, Sep 25, 2020.

Thread Status:
Not open for further replies.
  1. Offline

    PalmLP

    Hey there,

    I am a beginner and probably, this is a stupid question. I can circumvent this error by creating a new ItemStack for every execution, but I am curious how this works.
    My current project is a 1.16.2 manhunt plugin containing a compass to track players in different dimensions. The hunters should be able to track their target player in all dimensions, so I had to use the Lodestone block (at least in the nether and end). If you know a better way to track players in the nether/end, let me know :D

    Currently, my test command tries to place a Lodestone block at the location of your target (a specified player) and sets the compass you have to hold in your hand to this Lodestone block.

    Code:
    if(!player.getInventory().getItemInMainHand().getType().equals(Material.COMPASS)) return true;
            Block block = player.getLocation().getBlock();
            block.setType(Material.LODESTONE);
            ItemStack compass = new ItemStack(Material.COMPASS);
            CompassMeta compassMeta = (CompassMeta) compass.getItemMeta();
            compassMeta.setLodestoneTracked(true);
            compassMeta.setLodestone(block.getLocation());
            compass.setItemMeta(compassMeta);
            player.getInventory().setItemInMainHand(compass);
    Unfortunately, the compass starts spinning randomly if I donĀ“t actually place a Lodestone block at the set location (later, this block will be at y=0).

    However - for some reason, I have to create a new compass ItemStack to set the location of the Lodestone block. If I reuse the compass the player already holds in his hand (ItemStack compass = player.getInventory.getItemInMainHand()), the compass gets enchanted and starts spinning around randomly. After this failed first execution, executing this command a second time sets the location accurately and now, it even works when I hold a new normal compass. I tried to switch setLoadstoneTracked and setLodestone, but this does not make a difference.

    Why do I have to create a new compass ItemStack so that the compass directly points to the Lodestone?

    Thanks in advance :D
    Max
     
  2. Offline

    Mathias Eklund

    Alternatively you can make the compass lock to the target players nether coordinates * 8, as that is how the coordinates work between nether & overworld.

    Not to sure what the rules are for the end though as I haven't really messed around with it a lot.
     
Thread Status:
Not open for further replies.

Share This Page