Disable Certain Potions

Discussion in 'Plugin Requests' started by mbogacz, May 30, 2016.

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

    mbogacz

    How long do you think it will take
     
  2. Offline

    MrGeneralQ

    @mbogacz

    There is going something wrong to get he potionData()
     
  3. Offline

    I Al Istannen

  4. Offline

    MrGeneralQ

    Somehow I still don't get it on how to use it. I know you shouldn't spoongive me the code, but I don't get. I simply don't.
    I got this:

    Potion potion = (Potion) potion.fromItemStack(e.getPotion().getItem().getItemMeta());
     
    Last edited: Jun 19, 2016
  5. Offline

    I Al Istannen

    @MrGeneralQ
    Code:
        private boolean isBad(ItemStack item) {
            if(item.getType() != Material.POTION) {
                return false;
            }
            List<PotionType> blocked = <your blocked list>.
            return blocked.contains(Potion.fromItemStack(item).getType());
        }
     
  6. Offline

    MrGeneralQ

    I think I've found a code that does work. Didn't test yet. So I used this:

    Code:
        public void PotionSplash (PotionSplashEvent e){
       
            Player player = (Player) e.getPotion().getShooter();
            player.sendMessage("Thrown, " + player.getName());
           
            Potion potion = Potion.fromItemStack(e.getEntity().getItem());
    
       
            PotionMeta meta = (PotionMeta) e.getPotion().getItem().getItemMeta();
            PotionType potionType = potion.getType();
            if(main.getConfig().getBoolean("potions." + potionType.toString())== true){
                e.setCancelled(true);
                player.sendMessage(ChatColor.RED + "You are not allowed to throw that potion!");   
           
        }
           
           
           
           
           
        }
    What do you think?
     
  7. Offline

    I Al Istannen

    @MrGeneralQ
    It is a bit confusing, as you don't use all the variables. Also start the method with a lowercase letter.
    You can also leave out the "== true", the method already retuens a boolean.

    Further I would save it as a StringList in the config. It is easier for you and the user with a string list.
    Then you would do config.getStringList(path).contains(type.name())

    And, I would change type.toString() with type.name(), as the former could change. name() won't.

    But the part with potion and potionType should work :)
     
  8. Offline

    MrGeneralQ


    Ah yes, I see what you mean :) And the stringList, should look something like this?

    Code:
    Restricted:
      - type1
      - type2
      - type3
      - type4
    correct?
     
  9. Offline

    I Al Istannen

    @MrGeneralQ
    Correct. But I believe the entries are not indented, so it looks like this:
    Code:
    path:
    - type
    - type2
    I think used the one above and it worked so far, but maybe you can use both.
     
  10. Offline

    MrGeneralQ

    Okay thnx! I'll have to recode a bith but it should work :) I'll give it a try!
     
  11. Offline

    I Al Istannen

  12. Offline

    CeramicTitan

    This won't work for drinkable potions, you should use the PlayerInteractEvent, that way you can limit all types of potions.

    EDIT: Can you release the source when you are done? Thanks :)
     
  13. Offline

    MrGeneralQ

    I know :) I got different classes. This one was for the splash and Lingerin potion.
    And sure I will :)

    But where do I release it? :s
     
  14. Offline

    timtower Administrator Administrator Moderator

    Github, or pastebin will do if it is a single class.
     
  15. Offline

    MrGeneralQ

    @mbogacz

    It is done!

    Okay so what I did. Since the potion type names are changing between the versions, I've added 2 commands to the plugin. The first one:
    • /potionname
      just hold the potion/splahspotion in your hand and type the command.
      the plugin will then tell you what you need to add to your config.


      [​IMG]

    • /potionreload
      this will reload the config, so you don't have to reload the whole server.
    That's it! Thanks to @timtower & @I Al Istannen for the support in order to make this work.

    The permissions are different. Since I don't see any staffmember or player to need any of these commands there are no permissions for this plugin. You need to be an OP in order to use any of them.


    NOTE: Downloadlink: Sorry it took that long!

    Please give some feedback :)
     
    timtower likes this.
  16. Offline

    CeramicTitan

    I decompiled your plugin and I must commend you on your code structure, it is very good - clean and easy to read. However, logistically, there are some things that aren't too good;
    • Firstly, the enable/disable messages are kinda redundant, because I'm pretty sure Bukkit sends enable and disable messages by default - This isn't really a big issue and shouldn't impede too much on performance.
    • Secondly, programming is all about sequence; therefore in your onCommand handler, you check multiple times if the sender is op. I'd recommend that you move the op check to the start of the onCommand handler, so the sender cannot execute any commands unless they're op - Additionally, another great alternative is the use of permissions, where you just replace the op check with a player.hasPermission("permission.node") check.
    • Lastly, you listen for two events; the ItemConsumeEvent and the PotionSplashEvent. I mentioned in my previous post that you should use the PlayerInteractEvent(Called when a player interacts with an object or air - this means you can check the item in hand and cancel the event, stoppping anything from happening before it can happen i.e. a potion splashing - if that makes sense :p), as it could eliminate the use of unnecessary event handling(The less you do the more efficient your software).
    Check the javadocs for all things Bukkit - It is a developers favourite tool if used right!

    NOTE: I'd like to say I'm not trying to be that mean guy on here that rips apart your code; I'm just trying to help you understand and learn.

    If you have any questions of queries, don't hesitate to contact me :)
     
    Last edited: Jun 19, 2016
    timtower likes this.
  17. Offline

    MrGeneralQ

    Alright, first thnx!

    I understand that very well! However the only difference now is that it is giving the message when the player actually consumed an item. And your suggestion (which is good) is preventing it to even right click and so cancel the event before the player start consuming the item.


    I am aware of that option. But in this case I didn't saw any possible method why any other player except the owner needs one of these commands. The commands are only usefull to the owner since he/she is probably the only one that has access to the server files (config file). It is still an option ofcourse, but in this case, I thought it was not nessecary.

    I am not sure what I am about to say now. But bukkit tells you indeed for ex this message: "Enabling potionrestrict". However, It will try to enable it but if it fails somehow then nothing apears. If I use the onEnable() and put it on the last line in there, then it should execute when everything loaded just fine. (Again: not sure about this)


    I am glad that you gave me such feedback. I got still much to learn so I will :)
     
  18. Offline

    CeramicTitan

    The permissions option was merely an alternative to your current solution and something that is used in most plugins - which I'm sure you've noticed :p

    And in regards to the onEnable messages; if I recall correctly, you'll be prompted with a stacktrace if something is wrong or if there is a logic error, your plugin will still run it just won't work as expected :)

    Anyway, Glad I could help :)
     
    MrGeneralQ likes this.
  19. Offline

    I Al Istannen

    @MrGeneralQ
    Looks good :)

    I also found another, subtile, thing:
    and
    Code:
    @EventHandler
    public void PotionSplash(final PotionSplashEvent e) {
      final Player player = (Player)e.getPotion().getShooter();
    won't really work together ;)
     
  20. Offline

    MrGeneralQ

    So making it final will make it better? And the first thing you said, I don't understand :s
     
  21. Offline

    I Al Istannen

    @MrGeneralQ
    Ah, no. Sorry, that was added by the decompiler.
    What I wanted to say is:
    Not every shooter of a Potion is a Player. You cast it directly to a player ((Player)e.getPotion().getShooter()), but that will throw an exception if a Witch throws a potion, or an dispenser disposes one. You need to check that, before you cast it to a player.
     
  22. Offline

    MrGeneralQ

    So basicly

    if(e.getShooter instanceof Player){
    // code goes here?
    }
     
  23. Offline

    I Al Istannen

    @MrGeneralQ
    You (probably) want to cancel the event everytime. But as you never need the player instance except for sending a message, just put the code for sending the message and the casting in the if you described.

    Hope this made sense... :p
     
  24. Offline

    MrGeneralQ

    Yup I understand. thnx
     
  25. Offline

    mbogacz

    @MrGeneralQ
    I just started my server with the new new jar file, I don't know why but the config file is not popping up?

    It's like not loading or anything.
     
  26. Offline

    I Al Istannen

    @mbogacz
    Any errors in the console? If you type "/pl" (or "pl" in the console), does it show up in green or red?
     
  27. Offline

    MrGeneralQ


    I might know what the problem is. Hold on a second :)
    @mbogacz

    Try this: download
     
  28. Offline

    mbogacz

    Nope, still doesn't work.

    Green.
     
  29. Offline

    MrGeneralQ

    @mbogacz

    Remove the folder, then reload server. Should fix it
     
  30. Offline

    mbogacz

    Not working :(
    Nothing really happens, it just doesn't load at all.
     
Thread Status:
Not open for further replies.

Share This Page