Solved Help for my code

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

Thread Status:
Not open for further replies.
  1. I wish that if one player opens the chest, another player cannot open that chest, I tried this code but it doesn't work, advice?

    Code (Text):


    package it.FarBack.chest;

    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.block.Chest;
    import org.bukkit.block.DoubleChest;
    import org.bukkit.entity.Player;
    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.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin implements Listener {
        @Override
        public void onEnable() {
            System.out.println("Plugin delle Chest by FarBack attivato correttamente");
            Bukkit.getPluginManager().registerEvents(this, this);
        }
        private boolean baule = true;

        @EventHandler

        public void AperturaChest(InventoryOpenEvent evento) {
            if (evento.getInventory().getType().equals(Material.CHEST)) {
                if (baule == false) {
                    evento.getPlayer().sendMessage("§cQuesto baule e' gia' aperto");
                    evento.setCancelled(true);
                } else {
                    if (baule = true) {
                        Player p = (Player) evento.getPlayer();
                        if (evento.getInventory().getType().equals(Material.CHEST)) {
                            baule = false;
                            p.sendMessage("§cHai aperto un baule");

                        }
                    }
                }
            }
        }

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

    }
    }

    }
     
  2. SteelPhoenix

    Moderator

    if (baule = true) I don't think you want to give baule a new value.
     
  3. Some ideas to replace it?
     
  4. Either use baule == true or just if (baule) { since an == true check is implicit
     
  5. I want to set baule to true and false
     
  6. if (evento.getInventory().getType().equals(Material.CHEST)) {

    ?? what's this
    You need to InventoryType
     
  7. Code (Java):
                  if (baule = true) { // <------ HERE
                        Player p = (Player) evento.getPlayer();
                        if (evento.getInventory().getType().equals(Material.CHEST)) {
                            baule = false;
                            p.sendMessage("§cHai aperto un baule");

                        }
                    }
    @SteelPhoenix is referring to where you set baule to true instead of checking whether it is true in the if statement marked above. You also have some redundant checks, such as checking if baule is true after discovering it isn't false. You also have a second inventory check, which as @HarleySwtfus noted is incorrect in both places.
     
  8. NOW WORK!!! VERY THANKS
    Thanks Dadatags & Harl!
     
Thread Status:
Not open for further replies.