Player Equipping Armor

Discussion in 'Spigot Plugin Help' started by bmac20, May 19, 2016.

  1. So this is the code I have so far. How would you make it so that it only sends the message when you put it on, and not take it off?
    Code (Text):
    package me.chromenized.custome;

    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;

    public class CustomEnchants extends JavaPlugin implements Listener{
       
        public void onEnable(){
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }

        @EventHandler
        public void playerEquipsArmour(InventoryClickEvent event) {
            Player p = (Player) event.getWhoClicked();
            ItemStack i = p.getInventory().getHelmet();

            if (i.getType() == Material.IRON_HELMET) {
                p.sendMessage("You are wearing an iron helmet.");
            }

        }
    }
     
    #1 bmac20, May 19, 2016
    Last edited: May 19, 2016
  2. wat code were
     
  3. Sorry. I am an idiot xD. There it is.
     
  4. Maybe add an simply 1-tick delay and check if it's null / air? If so, it was taken off, if not, something got equipped
     
  5. So
    Code (Text):
    if(i.getType() == Material.AIR){
                return;
            }
     
  6. After 1 tick
     
  7. So would I have to make a BukkitRunnable and then put that code inside?
     
  8. Yes, try
     
  9. I am getting kind of confused with this.
    So I have 2 classes in eclipse.
    The main one:
    Code (Text):
    package me.chromenized.custome;

    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;

    public class CustomEnchants extends JavaPlugin implements Listener {

        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }

        @EventHandler
        public void playerEquipsArmour(InventoryClickEvent event) {
            Player p = (Player) event.getWhoClicked();
            ItemStack i = p.getInventory().getHelmet();

            if (i.getType() == Material.AIR) {
                return;
            }

            if (i.getType() == Material.IRON_HELMET) {
                p.sendMessage("You are wearing an iron helmet.");
            }

        }

    }
     
    And the BukkitRunnable which for some reason isn't working properly:
    Code (Text):
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scheduler.BukkitRunnable;

    public class BukkitRunnables extends BukkitRunnable {

        private final JavaPlugin plugin;

        public BukkitRunnables(JavaPlugin plugin) {

            this.plugin = plugin;
            this.plugin.getServer().getPluginManager().registerEvents(this, plugin);
        }

        @EventHandler
        public void playerEquipsArmour(InventoryClickEvent event) {
            Player p = (Player) event.getWhoClicked();
            ItemStack e = p.getInventory().getHelmet();
        }

        @Override
        public void run() {
           

        }

    }
     
  10. You know how to make a runnable rite .-.
     
  11. Not really, I was just kind of going off of the tutorial one.
     
  12. Every time someone comes to this section and doesn't know anything, no one answers. But how do you suggest I learn in order to make good plugins coming from no Java background? I have "Head First Java". Will that help me? If you have any other suggestions on how I can learn, that would be great!
     
    • Friendly Friendly x 1
  13. https://docs.oracle.com/javase/tutorial/
     
    • Like Like x 1