My plugin won't work!

Discussion in 'Plugin Development' started by tacos1223, Apr 22, 2014.

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

    tacos1223

    I am trying to make it so when you place stone, it gives you night vision, and if you place stone when you already have nightvision, it will get turned off.

    Everything is working correctly and the plugin enables in console.

    My main class:
    Code:
    package me.Tacos1223.NightVision;
     
    import java.util.logging.Logger;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockPlaceEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
     
    public class NightVision
    extends JavaPlugin
    implements Listener
    {
          public static NightVision plugin;
          public final Logger logger = Logger.getLogger("Minecraft");
          public final MyBlockListener bl = new MyBlockListener(this);
       
        public void onEnable() {
            this.getServer().getPluginManager().registerEvents(this, this);
            PluginDescriptionFile pdffile = getDescription();
            this.logger.info("[" + pdffile.getName() + "]" + " v" + pdffile.getVersion() + " is now Enabled!");
          }
       
        public void onDisable() {
            PluginDescriptionFile pdffile = getDescription();
            this.logger.info("[" + pdffile.getName() + "]" + " v" + pdffile.getVersion() + " is now Disabled!");
          }
    }
    
    And this is my blocklistener class:
    Code:
    package me.Tacos1223.NightVision;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockPlaceEvent;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
     
    public class MyBlockListener implements Listener {
       
        public static NightVision plugin;
        public static Material[] blacklist = {Material.STONE};
       
        public MyBlockListener(NightVision instance){
            plugin = instance;
        }
       
        public void onBlockPlace(BlockPlaceEvent event){
            Material block = event.getBlock().getType();
            Player player = event.getPlayer();
            for(Material blocked : blacklist){
                if(blocked == block);
                event.getBlock().setType(Material.AIR);
                {
                    if (!player.hasPotionEffect(PotionEffectType.SPEED))
                    {
                        PotionEffect potion = new PotionEffect(PotionEffectType.NIGHT_VISION, 3, 10000000);
                          player.addPotionEffect(potion);
                          player.sendMessage(ChatColor.GREEN + "You now have night vision!");
                      player.removePotionEffect(PotionEffectType.SPEED);
                      player.sendMessage(ChatColor.RED + "You no longer have night vision!");
                    }
                    else
                    {
                          player.removePotionEffect(PotionEffectType.SPEED);
                          player.sendMessage(ChatColor.RED + "You no longer have night vision!");
                    }
                  }
    }
    }
    }
    Thanks,
    Tyler.
     
  2. Offline

    BlazingBroGamer

    tacos1223
    Mabe for the if(blocked == block), you could put
    Code:
    if(blocked.contains(block))
     
  3. Offline

    tacos1223

    Sorry if this is nooby, but I have never encountered this, when I added what you said it gives me this error:
    "The method contains(Material) is undefined for the type Material."
     
  4. Offline

    ever_x

    Code:java
    1. this.getServer.getPluginManager.registerEvents(this, this);

    Should be
    Code:java
    1. this.getServer().getPluginManager().registerEvents(this.bl, this);

    There you go!
    Also,
    Code:java
    1. if(blocked == block);

    You must remove the semicolon at the end, or that line does nothing!
    Code:java
    1. [FONT=Consolas] if (!player.hasPotionEffect(PotionEffectType.SPEED))[/FONT]
    2. [FONT=Consolas] {[/FONT]
    3. [FONT=Consolas] PotionEffect potion = new PotionEffect(PotionEffectType.NIGHT_VISION, 3, 10000000);[/FONT]
    4. [FONT=Consolas] player.addPotionEffect(potion);[/FONT]
    5. [FONT=Consolas] player.sendMessage(ChatColor.GREEN + "You now have night vision!");[/FONT]
    6. [FONT=Consolas] //Not needed player.removePotionEffect(PotionEffectType.SPEED);[/FONT]
    7. [FONT=Consolas] //Also this player.sendMessage(ChatColor.RED + "You no longer have night vision!");[/FONT]
    8. [FONT=Consolas] }[/FONT]

    Not sure about the two commented lines; your current code will add Night Vision, remove speed and send the player two messages at the same time (one saying night vision added, and the other removed). Just remove the commented sendMessage() line and it should work much better.
     
    tacos1223 and BlazingBroGamer like this.
  5. Offline

    tacos1223

    That must of been something I forgot, but when I save the changes, put it on my server, and reload, nothing happens when I place the block still. What am I doing wrong?

    ever_x than you for that by the way!
     
  6. Offline

    BlazingBroGamer

    tacos1223
    I don't recommend you to reload... It will be bad for your server...
     
  7. Offline

    tacos1223

    ever_x
    I kind of want to keep those message lines so it informs the player.
    Here are my updated classes so far:

    Main:
    Code:
    package me.Tacos1223.NightVision;
     
    import java.util.logging.Logger;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockPlaceEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
     
    public class NightVision
    extends JavaPlugin
    implements Listener
    {
          public static NightVision plugin;
          public final Logger logger = Logger.getLogger("Minecraft");
          public final MyBlockListener bl = new MyBlockListener(this);
     
        public void onEnable() {
            this.getServer().getPluginManager().registerEvents(this.bl, this);
            PluginDescriptionFile pdffile = getDescription();
            this.logger.info("[" + pdffile.getName() + "]" + " v" + pdffile.getVersion() + " is now Enabled!");
          }
     
        public void onDisable() {
            PluginDescriptionFile pdffile = getDescription();
            this.logger.info("[" + pdffile.getName() + "]" + " v" + pdffile.getVersion() + " is now Disabled!");
          }
    }
    MyBlockListener class:
    Code:
    package me.Tacos1223.NightVision;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockPlaceEvent;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
     
    public class MyBlockListener implements Listener {
     
        public static NightVision plugin;
        public static Material[] blacklist = {Material.REDSTONE_LAMP_OFF};
     
        public MyBlockListener(NightVision instance){
            plugin = instance;
        }
     
        public void onBlockPlace(BlockPlaceEvent event){
            Material block = event.getBlock().getType();
            Player player = event.getPlayer();
            for(Material blocked : blacklist){
                if(blocked == block)
                event.getBlock().setType(Material.AIR);
                {
                    if (!player.hasPotionEffect(PotionEffectType.NIGHT_VISION))
                    {
                        PotionEffect potion = new PotionEffect(PotionEffectType.NIGHT_VISION, 3, 10000000);
                          player.addPotionEffect(potion);
                          player.sendMessage(ChatColor.GREEN + "You now have night vision!");
                      player.removePotionEffect(PotionEffectType.NIGHT_VISION);
                      player.sendMessage(ChatColor.RED + "You no longer have night vision!");
                    }
                    else
                    {
                          player.removePotionEffect(PotionEffectType.NIGHT_VISION);
                          player.sendMessage(ChatColor.RED + "You no longer have night vision!");
                    }
                  }
    }
    }
    }
     
  8. Offline

    ever_x

    tacos1223 OK, ignore my horribly formatted edit above.
    Edit: So I take way to long to type and you already addressed that issue! Here is the if/else statement that I think will work.

    Code:java
    1. if (!player.hasPotionEffect(PotionEffectType.NIGHT_VISION))
    2. {
    3. PotionEffect potion = new PotionEffect(PotionEffectType.NIGHT_VISION, 3, 10000000);
    4. player.addPotionEffect(potion);
    5. player.sendMessage(ChatColor.GREEN + "You now have night vision!");
    6. }
    7. else
    8. {
    9. player.removePotionEffect(PotionEffectType.NIGHT_VISION);
    10. player.sendMessage(ChatColor.RED + "You no longer have night vision!");
    11. }
    12.  
     
    tacos1223 likes this.
  9. Offline

    tacos1223

    Yea, that was my mistake, I changed it to night vision.

    Yea, it didn't work. I don't know why, I mean, everything looks correct and I am getting no errors. When I place the block, nothing happens, no messages, effects, or anything.
    ever_x Thank you for your help so far, I really appreciate it!

    EDIT: I changed stone to redstone lamp!

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

    ever_x

    Does it work? I'm just booting up a test server to test the code!

    OK, I am retarded. I didn't notice that your event handler didn't have the @ EventHandler above it!
    I have changed a few things to provide a toggle functionality:
    Code:java
    1.  
    2. public class MyBlockListener implements Listener {
    3.  
    4. public static Test plugin;
    5. public static Material[] blacklist = {Material.STONE};
    6. private ArrayList<String> toggle = new ArrayList<String>();
    7.  
    8. public MyBlockListener(Test instance){
    9. plugin = instance;
    10. }
    11.  
    12. @EventHandler
    13. public void onBlockPlace(BlockPlaceEvent event){
    14. Material block = event.getBlock().getType();
    15. Player player = event.getPlayer();
    16. for(Material blocked : blacklist){
    17. if(blocked == block)
    18. event.getBlock().setType(Material.AIR);
    19. {
    20. if (toggle.contains(player.getName()))//Player is in toggle, remove all effects!
    21. {
    22. player.removePotionEffect(PotionEffectType.NIGHT_VISION);
    23. player.sendMessage(ChatColor.RED + "You no longer have night vision!");
    24. toggle.remove(player.getName());
    25. return;
    26. }
    27.  
    28. if (!toggle.contains(player.getName()))//They are not in the toggle. Add them and add effects!
    29. {
    30. PotionEffect potion = new PotionEffect(PotionEffectType.NIGHT_VISION, 1, 100);
    31. player.addPotionEffect(potion);
    32. player.sendMessage(ChatColor.GREEN + "You now have night vision!");
    33. toggle.add(player.getName());
    34. }
    35. }
    36. }
    37.  

    That should work dandy (I have tested it). The only problem is that the toggle works, but I can't seem to apply the potion effect for some reason. I will continue to look into it though :)

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

    tacos1223

    *cheers*
    It works now. Thank you so much for your help! :D

    EDIT: I just found out that the effect applies to all blocks but it only removes the stone block.
     
  12. Offline

    xMrPoi

    And why is that
     
  13. Offline

    ever_x

    tacos1223 Ah ok, ill take a look at that tomorrow. Probably just a small bug somewhere along the line :)
    xMrPoi In a nutshell, memory leaks.
     
  14. Offline

    BlazingBroGamer

    xMrPoi
    This is really dangerous for the server, and there is this thing called Perm Gen Error, if you spam reload too much. This will most probably crash your server, and you will have to wait for the server to start responding again
     
  15. Offline

    xMrPoi

    Oh wow. I didn't know reloading the server was that bad
     
Thread Status:
Not open for further replies.

Share This Page