Solved Endgateway Exit location reverting to null

Discussion in 'Spigot Plugin Development' started by Cosmic_luck, Feb 8, 2020.

  1. This is an odd one, but I am trying to mess around with end gateways, possibly making a way for players to randomly teleport around the world (basically a fancier way than just a command to randomly teleport them places). I am setting the portals exit location, and I have a test message set up to repeat back what the location is.

    Code (Java):
        Random r = new Random();

        @EventHandler
        public void onUse(PlayerInteractEvent event){
            Block block = event.getClickedBlock();
            if(block == null) return;
            if(event.getAction() != Action.RIGHT_CLICK_BLOCK) return;
            if(event.getItem() == null) return;
            if(event.getItem().getType() != Material.BOOK) return;
            Block clone = block.getLocation().clone().add(0, 1, 0).getBlock();
            if(clone.getType() == Material.AIR){
                clone.setType(Material.END_GATEWAY);
                if(clone.getState() instanceof EndGateway){
                    EndGateway gateway = (EndGateway) clone.getState();
                    if(r.nextInt(2) == 1){
                        gateway.setExitLocation(new Location(event.getPlayer().getWorld(), gateway.getX() + r.nextInt(15000), gateway.getY(), gateway.getZ() + r.nextInt(15000)));
                    } else {
                        gateway.setExitLocation(new Location(event.getPlayer().getWorld(), gateway.getX() + r.nextInt(15000) * -1, gateway.getY(), gateway.getZ() + r.nextInt(15000) * -1));
                    }
                    clone.getState().update(true);
                    Bukkit.broadcastMessage("" + gateway.getExitLocation()); //Send a test message to the server to check the exit location
                }
            }
        }
    However, the gateway does not teleport when walked through, and upon further testing with debug code, the exitlocation is somehow null event after the event above is run. So I am thinking getState()#update(); is not working like I think it should? Let me know!
     
  2. Maybe that's the reason?
     
  3. Unsure, because my test code does show a perfectly reasonable exit location, the world IS there...
    [​IMG]
     
  4. Really not sure, but try gateway.update() instead of clone.getState().update().
     
    • Agree Agree x 1
  5. That worked! Thank you! I guess that makes sense really, I would have thought both methods did the same thing.