Get the location of block

Discussion in 'Plugin Development' started by _ionut_, Nov 8, 2020.

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

    _ionut_

    how can i get the position of a block, for now i can only get the position of the player

    Main:
    Code:
    package parck;
    
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    public class Main extends JavaPlugin {
      
        public static Main plugin;
      
        public void onEnable() {
            System.out.println("plugin Abilitato");
            plugin = this;
            saveDefaultConfig();
            Bukkit.getPluginManager().registerEvents(new Evento(), this);
      
        }
        public void OnDisable() {
            System.out.println("Plugin Disabilitato");
      
        }
      
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
          
            Player p = (Player) sender;
          
          
              
                if(commandLabel.equalsIgnoreCase("setlocation")) {
                    p.sendMessage("Set");
                    getConfig().set("loc.world", p.getLocation().getWorld().getName());
                    getConfig().set("loc.x", p.getLocation().getBlockX());
                    getConfig().set("loc.y", p.getLocation().getBlockY());
                    getConfig().set("loc.z", p.getLocation().getBlockZ());
                    saveConfig();
                  
                  
                    return true;
                }
                if(commandLabel.equalsIgnoreCase("setlocation1")) {
                    p.sendMessage("Set");
                  
                    getConfig().set("loc1.world", p.getLocation().getWorld().getName());
                    getConfig().set("loc1.x", p.getLocation().getBlockX());
                    getConfig().set("loc1.y", p.getLocation().getBlockY());
                    getConfig().set("loc1.z", p.getLocation().getBlockZ());
                    saveConfig();
                  
                  
                    return true;
                }
      
    
    
            if (commandLabel.equalsIgnoreCase("Tpa")) {
              
                p.sendMessage(ChatColor.RED + "The loc has not yet been set!");
              
                World w = Bukkit.getServer().getWorld(getConfig().getString("loc.world"));
                double x = getConfig().getDouble("loc.x");
                double y = getConfig().getDouble("loc.y");
                double z = getConfig().getDouble("loc.z");
                w.getBlockAt(new Location(w, x, y, z)).setType(Material.BEDROCK);
              
              
                return true;
              
            }
                  
                  
              
            if (commandLabel.equalsIgnoreCase("Tpa1")) {
              
                p.sendMessage(ChatColor.BLUE + "The loc has not yet been set!");
              
                World w = Bukkit.getServer().getWorld(getConfig().getString("loc1.world"));
                double x = getConfig().getDouble("loc1.x");
                double y = getConfig().getDouble("loc1.y");
                double z = getConfig().getDouble("loc1.z");
                p.teleport(new Location(w, x, y, z));
              
            }
              
              
                return true;
    }
    }  
      
    Event:
    Code:
    package parck;
    
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Evento extends JavaPlugin implements Listener {
        @EventHandler
        public void onPlayerClicks(PlayerInteractEvent event) {
            Player p = event.getPlayer();
            Action action = event.getAction();
    
             if (action.equals(Action.RIGHT_CLICK_AIR) || action.equals(Action.RIGHT_CLICK_BLOCK)) {
                 if (p.getItemInHand().getType() == Material.WOOD_AXE) {
                     p.sendMessage("location salvata");
                     p.performCommand("setlocation");
                  
              
                 }
             }
    
        }
    }
     
    Last edited by a moderator: Nov 8, 2020
  2. Offline

    timtower Administrator Administrator Moderator

  3. Offline

    _ionut_

    where
     
  4. Offline

    KingOfMars4

    What block do you want to get loc?
     
  5. Offline

    _ionut_

    in reality, I would like to obtain the position of a block already positioned, by the wood axe
     
  6. Offline

    Kars

    @_ionut_ do what tim told you. Use getLocation on the block.
     
  7. Offline

    _ionut_

    here??



    getConfig (). set ("loc.world", p.getLocation (). getWorld (). getName ());
    getConfig (). set ("loc.x", p.getLocation (). getBlockX ());
    getConfig (). set ("loc.y", p.getLocation (). getBlockY ());
    getConfig (). set ("loc.z", p.getLocation (). getBlockZ ());
    saveConfig ();
     
  8. Offline

    CraftCreeper6

  9. Offline

    _ionut_

    error
     

    Attached Files:

  10. Offline

    CraftCreeper6

    @_ionut_
    You need to get the block first. You can't just use the Block class.

    When you do your player interact event, get the blocks location, I linked in my previous post how to do that, then pass that information to the command in the form of args and use the args to set the values in the config.
     
  11. Offline

    _ionut_

    I don't understand
     
  12. Offline

    CraftCreeper6

    @_ionut_
    Please use the Tahg User button under my posts so I get a notification when you reply.

    In your player interact event, get the block that the player clicked (getClickedBlock()). Then get the location of that block (getLocation()) and use that as the location instead of the player.
     
  13. Offline

    _ionut_

    @ CraftCreeper6
     

    Attached Files:

  14. Offline

    CraftCreeper6

    @_ionut_
    You missed my first step. Get the block the player clicked.
     
  15. Offline

    _ionut_

    how?
    sorry, I'm a beginner if I disturb you


    @ CraftCreeper6
     
  16. Offline

    CraftCreeper6

    @_ionut_
    Use the PlayerInteractEvent.

    It's recommended that you know some basic Java knowledge before you start making plugins.
     
  17. Offline

    _ionut_

    @CraftCreeper6
    ok , how do i get the position in the config

    Code:
    package parck;
    
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    
    
    public class Evento implements Listener {
        @EventHandler
        public void onBlockClick(PlayerInteractEvent event) {
            Player p = event.getPlayer();
            Block block = event.getClickedBlock();
    
             if (event.getAction() != Action.LEFT_CLICK_BLOCK) {
                 if (p.getItemInHand().getType() == Material.WOOD_AXE) {
                     p.sendMessage("location salvata");
                     block.getLocation();
                  
                  
              
                 }
             }
            
          
                  }    
            
          
    
        }
     
  18. Offline

    timtower Administrator Administrator Moderator

    @_ionut_ Please surround your code with [code] <actual code here> [/code]
     
  19. Offline

    _ionut_

    Code:
     
    package parck;
    
    
    
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    
    
    public class Evento implements Listener {
        @EventHandler
        public void onBlockClick(PlayerInteractEvent event) {
            Player p = event.getPlayer();
            Block block = event.getClickedBlock();
    
             if (event.getAction() != Action.LEFT_CLICK_BLOCK) {
                 if (p.getItemInHand().getType() == Material.WOOD_AXE) {
                     p.sendMessage("location salvata");
                     
                     block.getLocation();
                   
                   
               
                 }
             }
             
           
                  }     
             
           
    
        }
    
                               
     
  20. Offline

    timtower Administrator Administrator Moderator

    @_ionut_ What do you want to do? Save the position?
    If so: you need to have access to the main class for that.
     
  21. Offline

    _ionut_

    @timtower
    I would like you to save the position of the clicked block. as in main only the player's position saves it
     
  22. Offline

    timtower Administrator Administrator Moderator

    @_ionut_ Block has a getLocation as well, you can save that just fine.
     
  23. Offline

    _ionut_

    ma come faccio a salvarlo nel file config
    @timtower
     
  24. Offline

    timtower Administrator Administrator Moderator

    @_ionut_ No idea which language that is but please translate to English.
     
  25. Offline

    _ionut_

    @timtower
    sorry
    but how do I save it in the config
     
  26. Offline

    timtower Administrator Administrator Moderator

  27. Offline

    _ionut_

    Attached Files:

  28. Offline

    timtower Administrator Administrator Moderator

    @_ionut_ Because those functions need the main class.
    Pass it along using a constructor.
     
  29. Offline

    _ionut_

  30. Offline

    timtower Administrator Administrator Moderator

    @_ionut_ That is basic java. Not helping with that.
     
Thread Status:
Not open for further replies.

Share This Page