Solved Making compass work on nether

Discussion in 'Plugin Development' started by FR0DG3R, Aug 17, 2020.

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

    FR0DG3R

    My objective is to make a player's compass point to a specific location when he right clicks with a compass in hand. I started with:
    Code:
    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent e) {
        if (e.getItem().getType() != Material.COMPASS) return;
        if (e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK) {
            player.setCompassTarget(locationToPoint);
        }
    }
    This works fine in the overworld, but I also want it to work on Nether where this code doesn't even move the compass pointer, nothing happens.

    So I read some stuff in the docs and tried this:

    Code:
    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent e) {
        if (e.getItem().getType() != Material.COMPASS) return;
        if (e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK) {
            if (world.getEnvironment() != World.Environment.NORMAL) {
                CompassMeta meta = (CompassMeta) e.getItem().getItemMeta();
                meta.setLodestoneTracked(true);
                meta.setLodestone(l);
                e.getItem().setItemMeta(meta);
                // line X
            } else {
                p.setCompassTarget(l);
            }
        }
    }
    And what I get when testing this in the Nether is: it quickly becomes "enchanted" (like it's paired with a lodestone) and point somewhere, too fast to see. So I tried putting e.setCancelled(true) on the "line X" but it didn't have any effect. Are compasses updated every tick in the Nether? Is there a way to stop it? Or am I just using setLodestone wrong (since there are no actual lodestones)?

    EDIT: I figured out a solution, the problem was indeed that no lodestone existed, so I placed a lodestone at y=0 where I wanted the compass to point to. Here is what worked for me. The lodestone must be placed before you get the CompassMeta otherwise it won't work (no idea why, this messed me up for days)

    Code:
    l.getBlock().setType(Material.LODESTONE);
    CompassMeta meta = (CompassMeta) e.getItem().getItemMeta();
    meta.setLodestone(l);
    meta.setLodestoneTracked(true);
    e.getItem().setItemMeta(meta);
    Remember, this technically gives a free lodestone to players but only if they know they can dig to y=0 and get it. I wouldn't recommend to use this in a large server.
     
    Last edited: Aug 29, 2020
  2. Offline

    TerroDoor

    @FR0DG3R I believe you can check a World the player is in by using ‘if (player.getWorld().getName().equalsIgnireCase(“yourNetherWorld”)){}’


    Sent from my iPhone using Tapatalk
     
  3. Offline

    FR0DG3R

    Does "player.getWorld().getName()" it work differently than "player.getWorld().getEnvironment()"? The documentation says getName "Gets the unique name of this world", not sure what it means though, getEnvironment seems more safe and more performant since I only compare Enums.

    But regardless, this is not my main issue, from my tests I can always check sucessfully the player's current world. My problem is actually just making the compass work in any world.
     
Thread Status:
Not open for further replies.

Share This Page