Solved Disabling all potions except fire resistance

Discussion in 'Plugin Development' started by MooshViolet, Aug 1, 2016.

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

    MooshViolet

    Hello all. I am trying to disable all potions except fire resistance. Here is my code. So far I have disabled every potion. how would I see if the item event is a fire resistance potion? I tried event.getItem().getType().equals(PotionType.FIRERESISTANCE) but it did not work.
    Code:
        @EventHandler
        public void onPlayerInteractEvent(PlayerInteractEvent event){
            Player p = event.getPlayer();
            if(event.getItem() == null){
                return;
            }
            if((event.getItem().getType().equals(Material.POTION))){
                p.sendMessage(ChatColor.GRAY + "You cannot use this.");
                event.setCancelled(true);
    Edit: I tried this, however it says I cannot use the fire resistance potion:
    Code:
    Code:
        @EventHandler
        public void onPlayerInteractEvent(PlayerInteractEvent event){
            Player p = event.getPlayer();
            if(event.getItem() == null){
                return;
            }
            if((event.getItem().getType().equals(Material.POTION))){
                if(event.getItem().getType().equals(PotionType.FIRE_RESISTANCE)){
                    return;
                }
                p.sendMessage(ChatColor.GRAY + "You cannot use this.");
                event.setCancelled(true);
     
  2. Offline

    Zombie_Striker

    1. Do not compare enums using ".equals"
    2. The type cannot both be a Material and a potion. How would that work?
    3. To check a potions type, you will need to get the potions data using "Itemstack#getDurability" and compare that ID to the id of the potion type.
     
  3. Offline

    Oxyorum

    @MooshViolet What @Zombie_Striker said is correct. Your code is failing because you cannot compare a Material object to a PotionType object. To do what you want to do change:

    Code:
    if(event.getItem().getType().equals(PotionType.FIRE_RESISTANCE))
    
    to

    Code:
    Potion fireResist = new Potion(PotionType.FIRE_RESISTANCE)
    if(event.getItem().getDurability() == fireResist.toDamageValue())
    
     
    Last edited: Aug 1, 2016
  4. Offline

    MooshViolet

    I replaced, however it is saying that you cannot use this potion when I try to drink a fire res.
    Code:
    @EventHandler
        public void onPlayerInteractEvent(PlayerInteractEvent event){
            Player p = event.getPlayer();
            if(event.getItem() == null){
                return;
            }
            if((event.getItem().getType().equals(Material.POTION))){
                Potion fireResist = new Potion(PotionType.FIRE_RESISTANCE);
                if(event.getItem().getDurability() == fireResist.toDamageValue()){
                    return;
                }
              
                p.sendMessage(ChatColor.GRAY + "This potion is not allowed on the Elite server.");
                event.setCancelled(true);
        }
        }
        }    
    @Zombie_Striker
     
  5. Offline

    ipodtouch0218

    @Oxyorum

    From the javadocs:


    @MooshViolet

    Try using Potion.fromItemStack(<ItemStack>).getEffectType();
    and comparing it to a PotionType.FIRE_RESISTANCE
     
    Last edited: Aug 1, 2016
  6. Offline

    MooshViolet

    could you help me out a little more i think I am on the right track.
     
  7. Offline

    ipodtouch0218

    @MooshViolet

    Potion <name> = Potion.fromItemStack( event#getItem )
    if <name>#getEffectType() == PotionType#FIRE_RESISTANCE) { return }
     
  8. Offline

    MooshViolet

    Thanks. After some troubleshooting it ended up like this:
    Code:
    @EventHandler
        public void onPlayerInteractEvent(PlayerInteractEvent event){
            Player p = event.getPlayer();
            if(event.getItem() == null){
                return;
            }
            if((event.getItem().getType().equals(Material.POTION))){
                Potion fireResist = Potion.fromItemStack(event.getItem());
                if(fireResist.getType()==PotionType.FIRE_RESISTANCE){
                    return;
                }
               
                p.sendMessage(ChatColor.GRAY + "This potion is not allowed on the Elite server.");
                event.setCancelled(true);
        }
        }
        }    
     
  9. Offline

    ipodtouch0218

    @MooshViolet

    If it works, make sure you set the title to Solved

    Thread Tools > Edit Title
     
  10. Offline

    Oxyorum

Thread Status:
Not open for further replies.

Share This Page