Cancelling custom item placing

Discussion in 'Plugin Development' started by GumbyExe, Nov 23, 2020.

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

    GumbyExe

    New to java so it's probably a really simple mistake but I've been looking through the forums and trying things for a few days and countless hours with no success. I am making a plugin with a lightning rod custom item and am trying to stop the player from being able to place the item.
    Here is my code:

    Main:

    Code:
    package me.harry.GodVersusGod;
    
    import java.util.List;
    
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.metadata.MetadataValue;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scheduler.BukkitRunnable;
    
    import me.harry.GodVersusGod.Commands.Ascend;
    import me.harry.GodVersusGod.Events.Events;
    import me.harry.GodVersusGod.ItemManager.ItemManager;
    import me.harry.GodVersusGod.Listeners.BlockPlaceListener;
    import me.harry.GodVersusGod.Listeners.InventoryClickListener;
    import me.harry.GodVersusGod.Listeners.StopDropListener;
    import me.harry.GodVersusGod.UI.UI;
    
    public class Main extends JavaPlugin implements Listener {
    
        @Override
        public void onEnable() {
          
            getServer().getConsoleSender().sendMessage("§6The Gods Welcome us");
            new Ascend(this);
            new InventoryClickListener(this);
            UI.initialize(this);
            ItemManager.init();
            runnable();
            getServer().getPluginManager().registerEvents(new Events(), this);
            new StopDropListener(this);
            new BlockPlaceListener();
        }
    
    
        public void runnable() {
            new BukkitRunnable() {
    
            @Override
                public void run() {
                    for(Player p : Bukkit.getOnlinePlayers()) {
                        int slot = 1;
                        List<MetadataValue> values = p.getMetadata("playerType");
                        if (!values.isEmpty() && values.get(0).value().equals(1) && p.getInventory() != ItemManager.zeus) {
                            p.getInventory().setItem(0, ItemManager.zeus);
                        }
                    }
                }
            }.runTaskTimer(this, 0, 10);
    
        }
    
    }
    ItemManager:

    Code:
    package me.harry.GodVersusGod.ItemManager;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.Material;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.inventory.ItemFlag;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    
    
    public class ItemManager {
    
        public static ItemStack zeus;
        public static String item_name;
    
        public static void init() {
            item_name = "ItemManager";
            createZeus();
        }
    
        private static void createZeus() {
            ItemStack item = new ItemStack(Material.HORN_CORAL, 1);
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName("§6Zeus's Lightning");
            List<String> lore = new ArrayList<String>();
            lore.add("Strike lightning down upon your foes with the power of Zeus's Lightning!");
            meta.setLore(lore);
            meta.addEnchant(Enchantment.LUCK,  1, false);
            meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
            item.setItemMeta(meta);
            zeus = item;
        }
    
    
        }
    BlockPlaceListener:

    Code:
    package me.harry.GodVersusGod.Listeners;
    
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockPlaceEvent;
    
    import me.harry.GodVersusGod.ItemManager.ItemManager;
    
    public class BlockPlaceListener implements Listener {
      
            @EventHandler
            public void onBlockPlace(final BlockPlaceEvent e) {
                if(e.getBlockPlaced().getType().equals(ItemManager.zeus)) {
                    e.setCancelled(true);
                  
            }
    
    }
    }
    Any help is much appreciated and thanks in advance!
    P.S. Code is in Spigot 1.16.3 if that helps.
     
  2. Offline

    KarimAKL

    You are comparing a Material with an ItemStack.

    What you want to do is:
    Code:Java
    1. @EventHandler
    2. public void onBlockPlace(final BlockPlaceEvent e) {
    3. // Get the item that the player is trying to place
    4. ItemStack item = e.getItemInHand();
    5.  
    6. // Compare the item with your custom item
    7. // I am using #isSimilar instead of #equals because that ignores the stack amount; in case the player has more than one "zeus" in their hand
    8. if (!item.isSimilar(ItemManager.zeus)) return;
    9.  
    10. // Cancel the event because it is the custom item
    11. e.setCancelled(true);
    12. }
     
    Last edited by a moderator: Nov 24, 2020
    GumbyExe likes this.
  3. Offline

    GumbyExe

    Thanks so much!
    One more question -
    Why doesn't eclipse like it when I add (this) to its constructor in the main? It's not working because of this.
     
  4. Online

    timtower Administrator Administrator Moderator

    @GumbyExe Hover your mouse over it. It will tell you.
     
  5. Offline

    GumbyExe

    Yes,
    It says:
    The constructor BlockPlaceListener(Main) is undefined

    I've tried both ways it says to fix it (adding a main constructor to BlockPlaceListener or removing the (this))
    But, neither way fixed it and allowed it to run. I don't know why this one, in particular, is like that because I have built plenty of other Listeners in this plugin the exact same way and imported them the exact same and it worked just fine.

    Edit: fixed the issue. needed to register it as an event in my BlockListener class.
     
    Last edited: Nov 24, 2020
Thread Status:
Not open for further replies.

Share This Page