Solved Need Help Writing Code

Discussion in 'Plugin Development' started by jasondorson, Jan 17, 2021.

  1. Offline

    jasondorson

    What code could I write to teleport a player to a certain location once their coordinates match a certain coordinate?
     
  2. Offline

    timtower Administrator Administrator Moderator

  3. Offline

    Kars

    If you don't care about performance, you could check the players location on every PlayerMoveEvent, and teleport them if their coords are as expected.
     
  4. Offline

    jasondorson

    Code:
    package me.test.HelloWorld;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin {
        @Override
        public void onEnable() {
            getLogger().info("Teleportation plugin launched");
        }
       
        @Override
        public void onDisable() {
        }
    
       
        public void onPlayerMove(PlayerMoveEvent e) {
            getLogger().info("player moved");
            if((int) e.getPlayer().getLocation().getZ() == 6){
                getLogger().info("got location 6");
                e.getPlayer().teleport(new Location(Bukkit.getWorld("world"), -11.5, 100, -9.5));
                }
            
            if((int) e.getPlayer().getLocation().getZ() == 5) {
                getLogger().info("got location 5");
                e.getPlayer().teleport(new Location(Bukkit.getWorld("world"), -11.5, 100, -9.5));
                }
          
            if((int) e.getPlayer().getLocation().getZ() == 4)    {
                getLogger().info("got location 4");
                e.getPlayer().teleport(new Location(Bukkit.getWorld("world"), -11.5, 100, -9.5));
              }
          
        }
    }
    I made it so that when a player moves and their z axis matches 4 5 or 6 it teleports them to -11.5, 100, -9.5 but it's not working.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jan 17, 2021
  5. Offline

    Strahan

    If you do that, filter the PlayerMoveEvent so you aren't processing anything unless they actually moved. PME gets called even when you move your head.
     
  6. Offline

    timtower Administrator Administrator Moderator

  7. Offline

    jasondorson

    How would I do that?
     
  8. Offline

    timtower Administrator Administrator Moderator

  9. Offline

    jasondorson

    Code:
    package me.test.HelloWorld;
    
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.event.Listener;
    
    public class Main extends JavaPlugin implements Listener{
        @Override
        public void onEnable() {
            getLogger().info("Teleportation plugin launched");
        }
       
        @Override
        public void onDisable() {
        }
        @EventHandler(priority = EventPriority.HIGH)
        public void onPlayerMove(PlayerMoveEvent e) {
            getLogger().info("player moved");
            if((int) e.getPlayer().getLocation().getZ() == 6){
                getLogger().info("got location 6");
                e.getPlayer().teleport(new Location(Bukkit.getWorld("world"), -11.5, 100, -9.5));
                }
            if((int) e.getPlayer().getLocation().getZ() == 5) {
                getLogger().info("got location 5");
                e.getPlayer().teleport(new Location(Bukkit.getWorld("world"), -11.5, 100, -9.5));
                }
            if((int) e.getPlayer().getLocation().getZ() == 4)    {
                getLogger().info("got location 4");
                e.getPlayer().teleport(new Location(Bukkit.getWorld("world"), -11.5, 100, -9.5));
              }
        }
    }
    
    I think I did it but it's still not working. I have two warnings in my IDE but I don't think they are the problem. Sorry if I'm being dumb. upload_2021-1-18_0-41-7.png
     
  10. Offline

    timtower Administrator Administrator Moderator

    @jasondorson You still do not have the correct line in your onEnable.
    And no need to log your own plugin, Bukkit does that for you.
     
  11. Offline

    jasondorson

    Thank you so much it finally works
     

Share This Page