Solved Preventing the use of certain potion types

Discussion in 'Plugin Development' started by pairofsnipers, Apr 27, 2015.

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

    pairofsnipers

    I am currently trying to make a plugin that will prevent the use of certain types of potions while still allowing the use of potions that aren't being blocked. My current plugin is not allowing the use of anything (pretty much everytime you right click with an item in your hand it cancels the event). Can anyone help me?

    Here is the code (It is only one class):
    Code:
    public class nPotions extends JavaPlugin implements Listener
    {
        public void onEnable() {
            getLogger().info("Event Enabled!");
            getServer().getPluginManager().registerEvents(this, this);
       }
       public void onDisable(){
           getLogger().info("Event Disabled!");
       }
    @EventHandler(priority=EventPriority.HIGH)
       public void onPlayerInteractEvent(PlayerInteractEvent e)
       {
        Potion regen = new Potion(PotionType.REGEN);
               if(!(e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.RIGHT_CLICK_AIR)) return;
               if(e.getPlayer().getItemInHand().equals(regen));
               {
                   e.setCancelled(true);
                   e.getPlayer().sendMessage(ChatColor.RED + "You can't use that.");
               }
       }
    }    
     
  2. Offline

    mine-care

    Follow java naming conventions,
    You compare an itemstack with a potion object :/
     
    TheGamesHawk2001 likes this.
  3. Offline

    nverdier

    And no need to log enable/disable messages.
     
    mine-care likes this.
  4. Offline

    pairofsnipers

    Then how do I specify a specific potion type with an itemstack? The farthest I can go with itemstack is Material.POTION.
    Gotcha :)
     
  5. @pairofsnipers As you've probably figured out, Potion has lots of useful methods when dealing with potions. One of the methods it has is fromItemStack(), which will give you a Potion reference if you pass it the ItemStack :)
     
  6. Offline

    pairofsnipers

    Yes I was aware of of that method but I am failing to understand how that can be applied to what I am trying to do?
     
  7. @pairofsnipers Quite simple really: Get the item they're using, check that it's a potion, use the fromItemStack() method to get a Potion object, check if it's a potion you want to block, and block it if it is
     
  8. Offline

    pairofsnipers

    @AdamQpzm sorry I'm a fairly new plugin dev, I still don't under stand where your getting this from. If you showed me this in code it may make more since. Again sorry I:
     
  9. Offline

    Reynergodoy

    I'm interested in that too...
     
  10. Code:
        ItemStack stack = //Item\\
        if(stack != null) {
            if(stack.getType().equals(Material.POTION)) {
               if(stack.getDurability() == /Potion short\) {
                   event.setCancelled(true);
               }
            }
        }
    
    Link
     
    Last edited: Apr 29, 2015
  11. Offline

    SuperOriginal

  12. @MaTaMoR_ Yet again spoon-feeding code that doesn't even do what the OP is requesting. Disgraceful.
     
  13. Offline

    pairofsnipers

    I don't believe thats what I asked. I'm just simply asking how the fromItemStack() is applied to the code I already had and where.
     
  14. Offline

    Hex_27

    @pairofsnipers why not check ItemConsumeEvent instead of InteractEvent? It would work much better
     
  15. No ? you're asking for a system to block some potions... so you just have to put that on player interact event
     
  16. @MaTaMoR_ Then please explain why the hell you're making a Strength Potion and doing exactly nothing with it? You're just taking advice that you've seen said in this thread, and throwing it in there because you think it'll help somehow. Spoon-feeding is a bad thing to do. Spoon-feeding poor quality code, as you so often do, is terrible. Please just be helpful, or find something better to do with your time. It's much more enjoyable for everyone involved that way :)


    @pairofsnipers I would ignore any code that is spoon-fed to you if I were you. Chances are normally quite high that it's pretty bad code, just some advice for the future :p

    What you really want to do is adjust your code so that it's not checking for an exact ItemStack. You see where you're checking if the item in the Player's hand is equal to whatever "regen" is? You don't really want to be doing that. First, you want to check if the ItemStack is a potion:
    You apparently already know how to do this :) Then you'll want to convert that item they have in their hand into a Potion object. Like this:

    Code:
    Potion potion = Potion.fromItemStack(player's item);
    Once you've done that, the Potion class has methods relevant to potions - I believe you'll probably want to check out the getType() method, which will return what type of potion it is, for example a strength potion. It may also be worth considering the advice given by
    @Hex_27
     
  17. I forget about that.
    2) I didnt even spoon feed m8, is not like he don't even know what i did....
     
  18. @MaTaMoR_ That's why you spoon-feed: you're unwilling to put in any actual effort into helping people.
     
    mine-care likes this.
  19. Nah, you're wrong, i just didn't pay attention and i haven't done anything with potions.
     
  20. @MaTaMoR_ That code could be decreased by like 4 lines of code XD. Not only are you spoon feeding him but also spoon feeding him: in correct code / bad code and not even what OP wants.
     
  21. @MaTaMoR_ I don't buy that. I don't see why you'd make a potion reference, hard coded to a type chosen at random, and do nothing with it... No matter how unfamiliar with potions you are (hey, maybe that's a hint not to post anything about it!)

    So let's move on to the next problem... Why are you checking with a magic number? It's been a long time since the alternatives were around, and not using magic numbers where avoidable is a basic programming idea.
     
  22. That code could blablalbla, but im sure if i wasn't post anything you all will post a bukkit guide link or something like that, is really easy that shit about others players code, but you didnt anything, im pissed of from people like you guys, "spoon feed is bad, better post a java link..." i was at java some time ago and the thing what really don't help is a shity java link or somethign like that, so if you didnt been there just shut up, and i really don't care if a mod mute me or ban me GL .
     
  23. Offline

    mythbusterma

    @MaTaMoR_

    I think we all learned Java at some point. You know what doesn't help people learn Java? Handing them the code the need, already written. I don't expect someone like you to understand why, but if you think you can wrap your head around it...

    If someone didn't write the code themselves, they didn't learn anything from it.

    Continuing to just hand them the code creates a negative feedback loop that causes them to come back here for every little problem, expecting people to write the code for them.

    Then again, the code you post never works anyway, so I really don't see why we should be getting upset, as nobody could use it anyway.
     
  24. You can say what you didnt live, im sure of it, you don't know what is ask for some help and just get a java link, no ones here really do something, yes they post 1 - 2 lines of code and do it all by yourself, gl, if instead of that you all take sometime doing a good example where you explain everything it will be diferent, but that not happens, you all just post a link and gl, do it all by yourself, if im here asking for some help you really think i didnt search something by myself before ? If im here is because i didnt get it and im asking for some example about it not for java link, every day i learn something new and i try to do my best helping the people and if i don't do it better is because im not enough good to do it, and you all help when i posted something wrong, you all are really fast to say what i did wrong, but when somebody asks for somehelp...
     
    Last edited: Apr 29, 2015
  25. Offline

    pairofsnipers

    I wasn't aware that was event but you are completely right that would be much better thanks :D

    Thank you very much. I really didn't know how fromItemStack was applied since I am fairly inexperienced and haven't made many plugins. Again thanks :)
    Thanks for attempting to help me even though the code didn't work correctly, it did give me another way of looking at how to check if the item is a potion. :)
     
    AdamQpzm likes this.
Thread Status:
Not open for further replies.

Share This Page