Help!

Discussion in 'Plugin Development' started by Abdalion, Aug 11, 2014.

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

    Abdalion

    The plugin is not working. I think i registered wrong the events. Please help !
    Code:
    Class: Nopick.Java

    Code:java
    1. package me.Abdalion.Nopick;
    2.  
    3.  
    4. import java.util.ArrayList;
    5. import java.util.List;
    6. import java.util.UUID;
    7. import java.util.logging.Logger;
    8.  
    9. import org.bukkit.plugin.PluginDescriptionFile;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12.  
    13. public class Nopick extends JavaPlugin{
    14. public final Logger logger = Logger.getLogger("Minecraft");
    15. public static Nopick plugin;
    16.  
    17. @Override
    18. public void onEnable() {
    19. PluginDescriptionFile pdfFile = this.getDescription();
    20. this.getCommand("Nopick").setExecutor(new Nopick1CommandExecutor(this));
    21. this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " fue habilitado!");
    22.  
    23. }
    24. List<UUID> list = new ArrayList<UUID>();
    25.  
    26.  
    27. @Override
    28. public void onDisable() {
    29. getLogger().info("fue deshabilitado!");
    30.  
    31.  
    32. }
    33.  
    34.  
    35.  
    36.  
    37. }
    38.  
    39.  
    40.  


    Class: Nopick1CommandExecutor
    Code:java
    1. package me.Abdalion.Nopick;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5. import java.util.UUID;
    6.  
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandExecutor;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.player.PlayerPickupItemEvent;
    14.  
    15.  
    16.  
    17. public class Nopick1CommandExecutor implements CommandExecutor {
    18. private final Nopick plugin;
    19.  
    20. public Nopick1CommandExecutor(Nopick plugin) {
    21. this.plugin = plugin;
    22. }
    23.  
    24. List<UUID> list = new ArrayList<UUID>();
    25.  
    26. public void PlayerListener(Nopick plugin) {
    27.  
    28. plugin.getServer().getPluginManager().registerEvents((Listener) this, plugin);
    29.  
    30. }
    31.  
    32. @EventHandler
    33. public void onPickup(PlayerPickupItemEvent event) {
    34. Player player = event.getPlayer();
    35. if(list.contains(player.getUniqueId()))
    36. event.setCancelled(true);
    37.  
    38. }
    39.  
    40. @Override
    41. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    42. Player p = (Player) sender;
    43. if(commandLabel.equalsIgnoreCase("Nopick")){
    44. list.add(p.getUniqueId());
    45.  
    46.  
    47. }
    48. return false;
    49. }
    50.  
    51.  
    52. }
    53.  


    I am having trouble with the list! I really don't know what to do.
    Created a new project called "Test". Please tell me where i'm wrong!!
    I enter the server, do /test an i still pick up items!
    Main (Test.java)
    Code:java
    1. package me.Abdalion.Test;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5. import java.util.UUID;
    6. import java.util.logging.Logger;
    7.  
    8. import org.bukkit.plugin.PluginDescriptionFile;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10.  
    11.  
    12. public class Test extends JavaPlugin{
    13.  
    14. public final Logger logger = Logger.getLogger("Minecraft");
    15.  
    16. public static Test plugin;
    17.  
    18. @Override
    19. public void onDisable() {
    20. getLogger().info("fue deshabilitado!");
    21. }
    22.  
    23. @Override
    24. public void onEnable() {
    25. PluginDescriptionFile pdfFile = this.getDescription();
    26. this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " fue habilitado!");
    27. new PlayerListener(this);
    28. }
    29.  
    30.  
    31.  
    32.  
    33.  
    34.  
    35.  
    36. }
    37.  


    Listener (PlayerListener.java)
    Code:java
    1. package me.Abdalion.Test;
    2.  
    3.  
    4. import java.util.ArrayList;
    5. import java.util.List;
    6. import java.util.UUID;
    7.  
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.player.PlayerPickupItemEvent;
    14.  
    15.  
    16. public class PlayerListener implements Listener {
    17.  
    18. List<UUID> list = new ArrayList<UUID>();
    19.  
    20. public PlayerListener(Test plugin) {
    21. plugin.getServer().getPluginManager().registerEvents(this, plugin);
    22. }
    23.  
    24. @EventHandler
    25. public void onPickup(PlayerPickupItemEvent event) {
    26. Player player = event.getPlayer();
    27. if(list.contains(player.getUniqueId()))
    28. event.setCancelled(true);
    29. else
    30. event.setCancelled(false);
    31.  
    32. }
    33.  
    34. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    35. Player p = (Player) sender;
    36. if(commandLabel.equalsIgnoreCase("Test")){
    37. list.add(p.getUniqueId());
    38. }
    39. return false;
    40. }
    41. }


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

    xTigerRebornx

    Abdalion You need to implement both Listener and CommandExecutor, using the code you posted a few ago where it wouldn't work, you casted it to Listener, rather you need to implement Listener.
     
  3. Offline

    MineStein

    Abdalion
    You didn't toggle picking up items in the command. Use this:
    Code:java
    1. if (list.contains(playerUUID)) {
    2. list.remove(playerUUID);
    3. } else {
    4. list.add(playerUUID);
    5. }
     
  4. Offline

    xTigerRebornx

    MineStein He is already registering his events in the constructor of his listener.
     
  5. Offline

    Gamecube762

    I think you got that backwards.
    "To be added to the VIP list, you need to be in the VIP list, otherwise you can't be in the VIP list."
     
  6. Offline

    MineStein

    Gamecube762 Whoops... I am full of mistakes today! I'll edit that post now.
     
  7. Offline

    Abdalion

    Still not working and doesn't send the message either. What should i do? D:
    I created a commandExecutor but not sure if it's ok.
    Test.java:
    Code:java
    1. package me.Abdalion.Test;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5. import java.util.UUID;
    6. import java.util.logging.Logger;
    7.  
    8. import me.Abdalion.Test.TestCommandExecutor;
    9.  
    10. import org.bukkit.plugin.PluginDescriptionFile;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13.  
    14. public class Test extends JavaPlugin{
    15.  
    16. public final Logger logger = Logger.getLogger("Minecraft");
    17.  
    18. public static Test plugin;
    19.  
    20. @Override
    21. public void onDisable() {
    22. getLogger().info("fue deshabilitado!");
    23. }
    24.  
    25. @Override
    26. public void onEnable() {
    27. PluginDescriptionFile pdfFile = this.getDescription();
    28. this.getCommand("Test").setExecutor(new TestCommandExecutor(this));
    29. this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " fue habilitado!");
    30. new PlayerListener(this);
    31. }
    32.  
    33. List<UUID> list = new ArrayList<UUID>();
    34.  
    35.  
    36.  
    37.  
    38.  
    39. }
    40.  


    PlayerListener.java
    Code:java
    1. package me.Abdalion.Test;
    2.  
    3.  
    4. import java.util.ArrayList;
    5. import java.util.List;
    6. import java.util.UUID;
    7.  
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.player.PlayerPickupItemEvent;
    14.  
    15.  
    16. public class PlayerListener implements Listener {
    17.  
    18. List<UUID> list = new ArrayList<UUID>();
    19.  
    20. public PlayerListener(Test plugin) {
    21. plugin.getServer().getPluginManager().registerEvents(this, plugin);
    22. }
    23.  
    24. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    25. Player p = (Player) sender;
    26. if(commandLabel.equalsIgnoreCase("Test")){
    27. if (list.contains(p.getUniqueId())) {
    28. list.remove(p.getUniqueId());
    29. p.sendMessage("Test Deshabilitado!");
    30. } else {
    31. list.add(p.getUniqueId());
    32. p.sendMessage("Test habilitado!");
    33. }
    34. }
    35. return false;
    36.  
    37.  
    38. }
    39.  
    40. @EventHandler
    41. public void onPickup(PlayerPickupItemEvent event) {
    42. Player player = event.getPlayer();
    43. if(list.contains(player.getUniqueId())) {
    44. event.setCancelled(true);
    45.  
    46. } else {
    47. event.setCancelled(false);
    48.  
    49.  
    50.  
    51. }
    52. }
    53.  
    54. }


    TestCommandExecutor.java
    Code:java
    1. package me.Abdalion.Test;
    2.  
    3.  
    4. import java.util.ArrayList;
    5. import java.util.List;
    6. import java.util.UUID;
    7.  
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.player.PlayerPickupItemEvent;
    14.  
    15.  
    16. public class PlayerListener implements Listener {
    17.  
    18. List<UUID> list = new ArrayList<UUID>();
    19.  
    20. public PlayerListener(Test plugin) {
    21. plugin.getServer().getPluginManager().registerEvents(this, plugin);
    22. }
    23.  
    24. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    25. Player p = (Player) sender;
    26. if(commandLabel.equalsIgnoreCase("Test")){
    27. if (list.contains(p.getUniqueId())) {
    28. list.remove(p.getUniqueId());
    29. p.sendMessage("Test Deshabilitado!");
    30. } else {
    31. list.add(p.getUniqueId());
    32. p.sendMessage("Test habilitado!");
    33. }
    34. }
    35. return false;
    36.  
    37.  
    38. }
    39.  
    40. @EventHandler
    41. public void onPickup(PlayerPickupItemEvent event) {
    42. Player player = event.getPlayer();
    43. if(list.contains(player.getUniqueId())) {
    44. event.setCancelled(true);
    45.  
    46. } else {
    47. event.setCancelled(false);
    48.  
    49.  
    50.  
    51. }
    52. }
    53.  
    54. }
    55.  
     
  8. Offline

    DinosParkour

    This is how I usually do it:

    Commands.class
    Code:Java
    1. public Main plugin;public Commands(Main instance){plugin = instance;}
    Main.class
    Code:Java
    1. this.getCommand("INSERT_YOUR_COMMAND").setExecutor(newCommands(this));
     
  9. Offline

    Abdalion


    But doing that won't solve my problem
     
  10. Offline

    Gamecube762

    Abdalion Your current problem is that you haven't been registering your events, you've been just creating a new eventListener every time the plugin starts.

    Bukkit#getPluginsManager()#registerEvents( new PlayerListener(this), this )
     
  11. Offline

    Zupsub

    You should NEVER EVER set an event uncancelled, if you don't know EXACTLY what you do.

    Now it seems, that you have duplicated code in PlayerListener/CommandExecuter
    It's good practise to add @EventHandler(ignoreCancelled = true), to be sure the event is only called if necessary.
    Please use the @Override annontation, that makes the code more readable and "fixes" bug, before the exist.
     
  12. Offline

    Abdalion

    Gamecube762

    I added in onEnable
    Code:java
    1. Bukkit.getPluginManager().registerEvents( new PlayerListener(this), this );
    .
    Is that what you ment? Because its not working
     
  13. Offline

    Gamecube762

    In your onPickup try adding debug messages
    System.out.println("String")
    "Pickup Start"
    "Event Canceled"
    ext.
     
  14. Offline

    DinosParkour

    Abdalion
    not sure if what you typed is the same thing, but this one works 100%
    Code:java
    1. Bukkit.getPluginManager().registerEvents(this, this);
     
  15. Offline

    Gamecube762

    Not sure if you know, but this only works if his main class is also an EventListener.
     
  16. Offline

    DinosParkour

    Gamecube762 yes i do know
    Abdalion if you wanted to do that with another class (not the main)

    Events.class
    Code:java
    1. public Main plugin;
    2. public Events(Main instance){plugin= instance;}
    Main.class
    Code:java
    1. private Events Events = new Events(this);
    2.  
    3. @Override
    4. public void onEnable(){
    5. PluginManager pm = getServer().getPluginManager();
    6. pm.registerEvents(Events, this);
    7. //Anything else...
     
  17. Offline

    Abdalion

    Gamecube762
    The problem is in the OnPickup because it sends a message of enable and disable when i do /Test.
    Code:java
    1. @Override
    2. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    3. Player p = (Player) sender;
    4. if(commandLabel.equalsIgnoreCase("Test")){
    5. if (list.contains(p.getUniqueId())) {
    6. list.remove(p.getUniqueId());
    7. p.sendMessage("Test Deshabilitado!");
    8. } else {
    9. list.add(p.getUniqueId());
    10. p.sendMessage("Test habilitado!");
    11. }
    12. }
    13. return false;
    14. }


    The onPickup. I think its allright. Note: When you pickup an item it does NOT send you a message, so its completly not working.
    Code:java
    1. @EventHandler
    2. public void onPickup(PlayerPickupItemEvent event) {
    3. Player player = event.getPlayer();
    4. if(list.contains(player.getUniqueId())) {
    5. event.setCancelled(true);
    6. player.sendMessage("Item agarrado");
    7.  
    8.  
    9.  
    10.  
    11. }
    12. }
     
  18. Offline

    DinosParkour

    Abdalion Huh? I tried your code and it perfectly works for me (if you ignore the part that it spams you with the no-pickup message xD)

    Here's a pastebin, I quickly put your code in 1 class: http://pastebin.com/7L3EEA5T

    EDIT: Now incase you want to fix that spam issue, make a new ArrayList:
    Code:java
    1. ArrayList<UUID> message = new ArrayList<UUID>();//checks if player has gotten the message before

    And then add an if statement before sending the message:
    Code:java
    1. if(!message.contains(player.getUniqueId())){
    2. message.add(player.getUniqueId());
    3. player.sendMessage("You have disabled item-pickup");
    4. Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
    5. public void run(){
    6. message.remove(player.getUniqueId());
    7. }
    8. }, 3 * 20);
    9. }
     
  19. Offline

    Abdalion

  20. Offline

    DinosParkour

    Abdalion I quickly pasted your code in one class. If you wanted your plugin to be more clean, you'd normally put your Events in 1 class, your Commands in 1 class and you'd have your Main class.
    At the top of the Events and the Commands class, put
    Code:java
    1. public Main plugin;
    2. public theNameOfYourClass(Main instance)
    3. {plugin = instance;}

    And then at the top of your main class
    Code:java
    1. private theNameOfYourEventsClass theNameOfYourEventsClass = theNameOfYourEventsClass(this);
    2. private theNameOfYourCommandsClass theNameOfYourCommandsClass = theNameOfYourCommandsClass(this);

    Finally, register the events and don't forget to set the command executor by adding something like this to your onEnable
    Code:java
    1. this.getCommand("test").setExecutor(new theNameOfYourCommandsClass(this));
     
  21. Offline

    Abdalion

    DinosParkour Thank you very much! And also you guys Gamecube and Tiger =)
     
Thread Status:
Not open for further replies.

Share This Page