Solved Close anvil when item is in inventory

Discussion in 'Plugin Development' started by tovd1234, Sep 8, 2015.

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

    tovd1234

    Hey I'm trying to get an anvil to not open when a player has a TripWire hook in his inventory. And when he gets one while in an anvil, it should close. IDK how to do it.... I tried this:
    Code:
    package com.piggeh.antiglitchkey;
    
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener {
       
        double timer = 0.2;
       
        @Override
        public void onEnable() {
            getServer().getPluginManager().registerEvents(this, this);
            getLogger().info("I'm like hey what's up hello, this plugin works (I know that doesn't rhyme o-o)");
        }
       
        public void antiAnvil(Player player){
            this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    
                @Override
                public void run() {
                   
                    if (timer != 0) {
                        if (timer != 0) {
                            timer--;
                        }
                    }
                   
                    if (timer == 0) {
                        timer = 0.2;
                        if(player.getInventory().contains(Material.TRIPWIRE_HOOK)) {
                            player.closeInventory();
                        }
                    }
                   
                    if(timer < 0) {
                        timer = 0.2;
                       
                    }
                }
    
               
            }, 0L, 20L);
        }
    
    }
    

    Thanks, Tim

    Please Ignore the onEnable xD I was bored...

    <Edited by bwfcwalshy: Merged posts, please use the edit button rather than double posting.>
     
    Last edited by a moderator: Sep 9, 2015
    teej107 likes this.
  2. Offline

    Petersoj

    When ever a player right clicks an anvil, check if they have a tripwire hook in their inventory. If they do, close the inventory.
     
  3. Offline

    Ruptur

    @tovd1234
    1. Register a InventoryClickEvent;
    2. Check if the inventory type is anvil using event.getView().getType() and the item clicked is restricted
    3. Then cancel the event
     
  4. Offline

    tovd1234

    But how do I check it when they get a tripwire hook in their inventory when the anvil uis already opened?

    And do I put everything in the clock or make it just somewhere in the public void

    <Edited by bwfcwalshy: Merged posts, please use the edit button rather than double posting.>
     
    Last edited by a moderator: Sep 9, 2015
  5. Offline

    Ruptur

    @tovd1234
    Like I said above ...

    You don't need to block opening the anvil if they have a tripwire hook in thier inventory. Just check once they do try to click on a tripwire in an anvil, and stop the event
     
  6. Offline

    tovd1234

    Yea I get that, but you could drop a tripwire hook, right click the anvil before you pick it up, and then pick it up to bypass it
     
  7. Offline

    Petersoj

    Make an Inventory Click Event and if the player clicks the tripwire hook or tries to drop it (use PlayerDropItemEvent) then set it cancelled.
     
  8. Offline

    ImpaTheImpaler

    You could make a PlayerPickupItemEvent, check if the player is in the anvil and check the item being picked up(if its a tripwire hook set it to cancelled)
     
  9. Offline

    tovd1234

    I think I'm on the right way but I still can't figure it out. Can one of you maybe send the code for it?

    I did this:
    Code:
    public void antiAnvil(InventoryClickEvent e){
            Player p = Bukkit.getPlayer(getName());
                if(e.getView().getType() == e.getView().getType().ANVIL) {
                    if(p.getItemOnCursor().equals(Material.TRIPWIRE_HOOK)) {
                    p.closeInventory();
                    p.getInventory().remove(Material.TRIPWIRE_HOOK);
                    e.setCancelled(true);
                    p.sendMessage("§5Rekt");
                    }
            }
        }
     
  10. Offline

    RoboticPlayer

    Does anything show in the console when you are in an anvil and you click a tripwire hook?
     
  11. Offline

    tovd1234

    Yea.. a lot of text. Errors
     
  12. Offline

    Petersoj

    Did you add an @EventHandler? And register your events? What are the errors?
     
  13. Offline

    tovd1234

    I have @EventHandler, I registered events, and the error is [16:21:39 ERROR]: Could not pass event InventoryClickEvent to Anti-GlitchKey v1.0
     
  14. Offline

    Petersoj

    Can you post the whole stack trace?
     
  15. Offline

    tovd1234

    Idk what that means but I got it to work now, but it only closes the anvil once you take out the tripwire hook ...

    Code:
    @EventHandler
        public void AntiAnvilGlitch(InventoryClickEvent e) {
            Player p = (Player) e.getWhoClicked();
            Inventory inventory = e.getInventory();
            if (e.getInventory().getType() == InventoryType.ANVIL) {
                if(e.getInventory().contains(Material.TRIPWIRE_HOOK)) {
                    Bukkit.broadcastMessage(p.getDisplayName() + " §ctried to duplicate keys!");
                    p.closeInventory();
                    e.setCancelled(true);
                }   
            }
        }

    The same goes for the message I made it broadcast....

    Never mind! I fixed it with this!
    Code:
    @EventHandler
        public void AntiAnvilGlitch(InventoryClickEvent e) {
            Player p = (Player) e.getWhoClicked();
            Inventory inventory = e.getInventory();   
                if(e.getClickedInventory().contains(Material.TRIPWIRE_HOOK)) {
                Bukkit.broadcastMessage("FIRSHASKDJHAKSJ");   
                p.sendMessage("§9[§eStealthFactions§9] §7You can't use anvils with a Voting Key in your inventory!");
                p.closeInventory();
                e.setCancelled(true);
                    }
            }


    Thanks for helping all :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 11, 2016
  16. Offline

    Ruptur

    @tovd1234
    Try not to directly use the color symbol in code.
    I understand using ChatColor.translateAltenativeColorCode each time you want to send a message might be tedious but its better than putting \u00A7 in code.
    I usually create a method that sends a message for me so i dont have to keep trying up ChatColor.tra...
    Code:
    public void sendMessage(CommandSender reciever, String message) {
        reciever.sendMessage(ChatColor.translateAlternativeColorCodes('you char', message);
    }
     
  17. Offline

    tovd1234

    But why?
     
  18. Offline

    Ruptur

    @tovd1234
    Its bad practise.
    If the color code ever change the old code wont still work and you would have to go through all your past plugins to make changes.
    In terms of readibility, ChatColor is also easier to read.
     
  19. Offline

    tovd1234

    oh ok sure, thanks!
     
Thread Status:
Not open for further replies.

Share This Page