Help with @SuppressWarnings

Discussion in 'Plugin Development' started by nateracecar5, Jun 8, 2013.

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

    nateracecar5

    Okay, so I have some code that will give a player a potion effect when you right click a sign. The problem is, I can't figure out how to tell if the player right clicked a sign or not, so I am trying to use @SuppressWarnings but it isn't working. How can I fix this? Help would be nice.

    Code:
    package me.redstonefreak589.warp;
     
    import org.bukkit.ChatColor;
    import org.bukkit.block.Block;
    import org.bukkit.block.BlockState;
    import org.bukkit.block.Sign;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
     
    @SuppressWarnings("deprecation")
    public class SignWarpListener implements Listener{
       
        public Main plugin;
       
        public SignWarpListener(Main instance){
            plugin = instance;
        }
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event){
            Player player = event.getPlayer();
            Block block = event.getClickedBlock();
            BlockState state = block.getState();
            Sign sign = (Sign)state;
            String signline1 = sign.getLine(0);
            String signline2 = sign.getLine(1);
            String sle = signline2;
           
            if(event.getAction() == Action.RIGHT_CLICK_BLOCK){
                if(signline1.equalsIgnoreCase("[SignEffects]")){
                    if(sle.equals("CONFUSION") || sle.equals("BLINDNESS") || sle.equals("DAMAGE_RESISTANCE") || sle.equals("FAST_DIGGING") || sle.equals("FIRE_RESISTANCE") || sle.equals("HARM")
                            || sle.equals("HEAL") || sle.equals("HUNGER") || sle.equals("INCREASE_DAMAGE") || sle.equals("INVISIBILITY") || sle.equals("JUMP") || sle.equals("NIGHT_VISION") ||
                            sle.equals("POISON") || sle.equals("REGENERATION") || sle.equals("SLOW") || sle.equals("SLOW_DIGGING") || sle.equals("SPEED") || sle.equals("WATER_BREATHING") ||
                            sle.equals("WEAKNESS") || sle.equals("WITHER")){
                        if(player.hasPermission(plugin.getConfig().getString("permissions.add")) || player.isOp()){
                            player.addPotionEffect(new PotionEffect(PotionEffectType.getByName(signline2), 500, 1));
                            player.sendMessage(ChatColor.AQUA + "[SignEffects] " + ChatColor.LIGHT_PURPLE + "I just gave you the " + ChatColor.RED + signline2 + ChatColor.LIGHT_PURPLE + " effect!");
                        }else{
                            player.sendMessage(ChatColor.RED + "You don't have permission!");
                        }
                    }else{
                        player.sendMessage(ChatColor.RED + "This potion effect doesn't exist!");
                    }
                }else{}
            }else{}
        }
    }
    I know it isn't the cleanest code, but keep in mind, my plugins are supposed to be simple, not way out there like Survival Games. ;) I also know that the package says "me.redstonefreak589.warp". That is because I WAS going to make this a warp plugin, but got sidetracked. Also, the name is my ingame name.

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

    Drkmaster83

    This code worked great for me! However, I didn't have your main class to generate the config, so it threw an error with the .hasPermission() method... so I removed it and checked for OP. It sent me the NoPerms message when I didn't have OP, so we're good!
    Code:
    package me.Drkmaster83.ClassName;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.block.Sign;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    public class SignWarpListener implements Listener
    {
    
        public Main plugin;
    
        public SignWarpListener(Main instance)
        {
            plugin = instance;
        }
        
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event)
        {
            Player player = event.getPlayer();
            
            if(event.getAction() == Action.RIGHT_CLICK_BLOCK)
            {
                if (event.getClickedBlock().getType() == Material.SIGN ||
                    event.getClickedBlock().getType() == Material.SIGN_POST ||
                    event.getClickedBlock().getType() == Material.WALL_SIGN) 
                {
                    Sign sign = (Sign) event.getClickedBlock().getState();
                    String signline1 = sign.getLine(0);
                    String signline2 = sign.getLine(1);
                    String sle = signline2;
                    
                    if(signline1.equalsIgnoreCase("[SignEffects]"))
                    {
                        if(sle.equals("CONFUSION") || sle.equals("BLINDNESS") || sle.equals("DAMAGE_RESISTANCE") || sle.equals("FAST_DIGGING") || sle.equals("FIRE_RESISTANCE") || sle.equals("HARM")
                            || sle.equals("HEAL") || sle.equals("HUNGER") || sle.equals("INCREASE_DAMAGE") || sle.equals("INVISIBILITY") || sle.equals("JUMP") || sle.equals("NIGHT_VISION") ||
                            sle.equals("POISON") || sle.equals("REGENERATION") || sle.equals("SLOW") || sle.equals("SLOW_DIGGING") || sle.equals("SPEED") || sle.equals("WATER_BREATHING") ||
                            sle.equals("WEAKNESS") || sle.equals("WITHER"))
                        {
                            if(player.hasPermission(plugin.getConfig().getString("permissions.add")) || player.isOp())
                            {
                                player.addPotionEffect(new PotionEffect(PotionEffectType.getByName(signline2), 500, 1));
                                player.sendMessage(ChatColor.AQUA + "[SignEffects] " + ChatColor.LIGHT_PURPLE + "I just gave you the " + ChatColor.RED + signline2 + ChatColor.LIGHT_PURPLE + " effect!");
                            }
                            else
                            {
                                player.sendMessage(ChatColor.RED + "You don't have permission!");
                            }
                        }
                        else
                        {
                            player.sendMessage(ChatColor.RED + "This potion effect doesn't exist!");
                        }
                    }
                    else
                    {
                        return;
                    }
                }
            }
            else
            {
                return;
            }
        }
    }
     
  3. Offline

    nateracecar5

    Okay I will try. Thanks!

    Okay, so I did take a look at your code, and did have to change it to hook into the Main class, and everything turned out alright! Thanks for the help! Now to add the ability to change the potion level and the ticks, which shouldn't be too hard

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

    Rockon999

    also...
    HTML:
     if(sle.equals("CONFUSION") || sle.equals("BLINDNESS") || sle.equals("DAMAGE_RESISTANCE") || sle.equals("FAST_DIGGING") || sle.equals("FIRE_RESISTANCE") || sle.equals("HARM")
                            || sle.equals("HEAL") || sle.equals("HUNGER") || sle.equals("INCREASE_DAMAGE") || sle.equals("INVISIBILITY") || sle.equals("JUMP") || sle.equals("NIGHT_VISION") ||
                            sle.equals("POISON") || sle.equals("REGENERATION") || sle.equals("SLOW") || sle.equals("SLOW_DIGGING") || sle.equals("SPEED") || sle.equals("WATER_BREATHING") ||
                            sle.equals("WEAKNESS") || sle.equals("WITHER"))
                        {
    that can be replaced with:

    Code:
    if(PotionEffectType.getByName(signline2) != null){
    because PotionEffectType.getByName returns null when it isn't a valid potion :)
     
Thread Status:
Not open for further replies.

Share This Page