Solved I want to do.. Help with my code

Discussion in 'Spigot Plugin Development' started by FutureMcServer, Aug 1, 2020.

Thread Status:
Not open for further replies.
  1. I'm trying to add this pseudo:
    Code (Text):

    if the player is 1 block from the chest
       continue
    Else: sendMessage U are too far
     
    This is my code:

    Code (Text):


    package it.Drago.chest;

    import org.bukkit.*;
    import org.bukkit.block.Block;
    import org.bukkit.block.Chest;
    import org.bukkit.block.DoubleChest;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Event;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryCloseEvent;
    import org.bukkit.event.inventory.InventoryOpenEvent;
    import org.bukkit.event.inventory.InventoryType;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Virusjar extends JavaPlugin implements Listener {
        @Override
        public void onEnable() {
            System.out.println("Plugin delle Chest by Drago903 attivato correttamente");
            Bukkit.getPluginManager().registerEvents(this, this);
            this.saveDefaultConfig();





        }
        private boolean baule = true;

        @EventHandler

        public void AperturaChest(InventoryOpenEvent evento) {
            if (evento.getInventory().getType().equals(InventoryType.CHEST)) {
                if (baule == false) {
                    Player player = (Player) evento.getPlayer();
                    ((Player) evento.getPlayer()).playSound(player.getLocation(), Sound.BLOCK_ANVIL_FALL, 10, 1);
                    player.sendMessage(this.getConfig().getString("Baule_Già_Aperto"));
                    evento.setCancelled(true);
                } else {
                    Player p = (Player) evento.getPlayer();
                    if (evento.getInventory().getType().equals(InventoryType.CHEST)) {
                        baule = false;
                        p.sendMessage(this.getConfig().getString("Apertura_Baule"));

                        }
                    }
                }
            }


        @EventHandler
        public void ChiusuraChest(InventoryCloseEvent evento){
            Player p = (Player) evento.getPlayer();
            if(evento.getInventory().getType().equals(InventoryType.CHEST)) {
                baule = true;

    }
    }

    }
     
  2. Code (Java):
        public void onOpenInventory(InventoryOpenEvent event) {
            Inventory inv = event.getInventory();
            Block b = null;
            if (inv.getHolder() instanceof Chest) {
                b = ((Chest) inv.getHolder()).getBlock();
            }
            if (inv.getHolder() instanceof DoubleChest) {
                b = ((DoubleChest) inv.getHolder()).getLocation().getBlock();
            }
            double distance = b.getLocation().distance(event.getPlayer().getLocation());
            if(distance > 1) {
                event.setCancelled(true);
                event.getPlayer().sendMessage("You are too far!");
            }
        }
     
     
  3. I tryed with this code but my code don't work


    Code (Text):

    package it.Drago.chest;

    import org.bukkit.*;
    import org.bukkit.block.Block;
    import org.bukkit.block.Chest;
    import org.bukkit.block.DoubleChest;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Event;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryCloseEvent;
    import org.bukkit.event.inventory.InventoryOpenEvent;
    import org.bukkit.event.inventory.InventoryType;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin implements Listener {
        @Override
        public void onEnable() {
            System.out.println("Plugin delle Chest by Drago903 attivato correttamente");
            Bukkit.getPluginManager().registerEvents(this, this);
            this.saveDefaultConfig();





        }
        private boolean baule = true;

        @EventHandler

        public void AperturaChest(InventoryOpenEvent evento) {
            if (evento.getInventory().getType().equals(InventoryType.CHEST)) {
                if (baule == false) {
                    Player player = (Player) evento.getPlayer();
                    ((Player) evento.getPlayer()).playSound(player.getLocation(), Sound.BLOCK_ANVIL_FALL, 10, 1);
                    player.sendMessage(this.getConfig().getString("Baule_Già_Aperto"));
                    evento.setCancelled(true);
                } else {
                    Player p = (Player) evento.getPlayer();
                    if (evento.getInventory().getType().equals(InventoryType.CHEST)) {
                        Inventory inv = evento.getInventory();
                        Block b = null;
                        if (inv.getHolder() instanceof Chest) {
                            b = ((Chest) inv.getHolder()).getBlock();
                        }
                        if (inv.getHolder() instanceof DoubleChest) {
                            b = ((DoubleChest) inv.getHolder()).getLocation().getBlock();
                        }
                        double distance = b.getLocation().distance(evento.getPlayer().getLocation());
                        if(distance > 1) {
                            evento.setCancelled(true);
                            evento.getPlayer().sendMessage("aYHHH");
                            baule = false;
                        }
                    }



                }
                    }
                }



        @EventHandler
        public void ChiusuraChest(InventoryCloseEvent evento){
            Player p = (Player) evento.getPlayer();
            if(evento.getInventory().getType().equals(InventoryType.CHEST)) {
                baule = true;

    }
    }

    }
     
Thread Status:
Not open for further replies.