Get the location of block

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

  1. Offline

    _ionut_

    @timtower
    event
    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 {
       
        private final Main main;
        public Evento()
        {
        this.main = main;    here are an erro
        }
       
        @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");
                     main.getConfig (). set ("loc.world", block.getLocation (). getWorld (). getName ());
                     main.getConfig (). set ("loc.x", block.getLocation (). getBlockX ());
                     main.getConfig (). set ("loc.y", block.getLocation (). getBlockY ());
                     main.getConfig (). set ("loc.z", block.getLocation (). getBlockZ ());
                     main.saveConfig ();
                   
                   
                   
               
                 }
             }
             
           
                  }     
             
           
    
        }
    
    
    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.getServer().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.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;
               
               
               
               
    
       
       
    }
       
       
       
       
       
       
       
       
       
       
    }
    
     
  2. Offline

    timtower Moderator Moderator

    @_ionut_ Because you are not giving it the main class using the arguments.
     
  3. Offline

    _ionut_

  4. Offline

    CraftCreeper6

  5. Offline

    _ionut_

    @CraftCreeper6
    I solved the problem by implementing the Listener in main but I would still like to know what I was wrong before
     
    Last edited: Nov 9, 2020
  6. Offline

    KingOfMars4

Share This Page