Help!

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

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

    Abdalion

    Hello.
    I'm staring with plugin making and i need help with a simple plugin i'm making.
    I wish to know how to cancel the PlayerPickUpItemEvent with a command. I mean when the player issues a command, he stops picking up items and then with another command, he picks up items again. Thank you =)
     
  2. Offline

    Gamecube762

    1. Create a List<UUID>
    2. onCommand - check if the player is in the list then remove them if so, else add them.
    3. onPickUpItem - Check if player is in the list, cancel the event
     
    DinosParkour likes this.
  3. Offline

    Abdalion

    Understood. As i said i am a noob, so, would you give me the code please? Its not like im gonna upload the plugin, im just learning :p
     
  4. Offline

    Gamecube762

    While giving the code would be easiest, it would cause you to learn less. I can give you some pseudo code though.

    List<UUID> list = List<UUID> //this goes outside of the onCommand and onItemPickup

    if list contains ( player getUUID ) // remove them or cancel event
    else //add them
     
  5. Offline

    Abdalion

    This is the way to add someone to a list? I researched, but not sure.

    Code:java
    1.  
    2. ArrayList <Player> list = new ArrayList<Player>();
    3.  
    4. }
    5. @EventHandler
    6. public void onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    7. Player p = (Player) sender;
    8. if (commandLabel.equalsIgnoreCase("Nopick")){
    9. list.add(p);
    10.  
    11. }
    12. return false;
    13. }
    14.  
    15. }
    16.  


    Also, can you lend me your e-mail or your skype? I can't PM you
     
  6. Offline

    Gamecube762

    Ya, but I suggest storing the Player's UUID or their name. Storing the player itself can cause problems later if the player leaves.
     
  7. Offline

    Abdalion

    So, i have to use this code to create the list?
    Code:java
    1. List<UUID> list = List<UUID>


    When i try to use it by its self, it shows me an error.
     
  8. Offline

    Gamecube762

    Try
    Code:java
    1. List<UUID> list = new ArrayList<UUID>();
     
  9. Offline

    Abdalion

    Well, this is what i did so far. It's got a bunch of mistakes, please help me fix them and also please help me finish the little part missing on the code.
    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.command.Command;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.event.EventHandler;
    13. import org.bukkit.plugin.PluginDescriptionFile;
    14. import org.bukkit.plugin.java.JavaPlugin;
    15.  
    16.  
    17. public class Nopick extends JavaPlugin{
    18. public final Logger logger = Logger.getLogger("Minecraft");
    19. public static Nopick plugin;
    20. List<UUID> list = new ArrayList<UUID>();
    21. @Override
    22. public void onEnable() {
    23. PluginDescriptionFile pdfFile = this.getDescription();
    24. this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " fue habilitado!");
    25. }
    26.  
    27. @Override
    28. public void onDisable() {
    29. getLogger().info("fue deshabilitado!");
    30.  
    31.  
    32. }
    33. @EventHandler
    34. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    35. Player p = (Player) sender;
    36. if(commandLabel.equalsIgnoreCase("Nopick")){
    37. list.add(p.getUUID());
    38.  
    39. }
    40. return false;
    41. }
    42.  
    43. @EventHandler
    44. public void PlayerPickupItemEvent(final Player player, final Item item, final int remaining)
    45. if(/*What do i have to put here? "If the player is on the list"*/)
    46. event.setCancelled(true);
    47.  
    48. }
    49.  
     
  10. Offline

    Gamecube762


    This has it most of it done for you, you just need to format it correctly
     
  11. Offline

    Abdalion

    Solved it, but still, plugin not working.
    Code:
    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.command.Command;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.event.EventHandler;
    13. import org.bukkit.event.player.PlayerPickupItemEvent;
    14. import org.bukkit.plugin.PluginDescriptionFile;
    15. import org.bukkit.plugin.java.JavaPlugin;
    16.  
    17.  
    18. public class Nopick extends JavaPlugin{
    19. public final Logger logger = Logger.getLogger("Minecraft");
    20. public static Nopick plugin;
    21. @Override
    22. public void onEnable() {
    23. PluginDescriptionFile pdfFile = this.getDescription();
    24. this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " fue habilitado!");
    25. }
    26. List<UUID> list = new ArrayList<UUID>();
    27. @Override
    28. public void onDisable() {
    29. getLogger().info("fue deshabilitado!");
    30.  
    31.  
    32. }
    33.  
    34. @EventHandler
    35. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    36. Player p = (Player) sender;
    37. if(commandLabel.equalsIgnoreCase("Nopick")){
    38. list.add(p.getUniqueId());
    39.  
    40. }
    41. return false;
    42. }
    43.  
    44. @EventHandler
    45. public void onPickup(PlayerPickupItemEvent event) {
    46. Player player = event.getPlayer();
    47. if(list.contains(player.getUniqueId()))
    48. event.setCancelled(true);
    49.  
    50. }
    51. }
    52.  


    To be clear: The problem is that when i enter the game and do /Nopick, the user still picks up items on the floor.

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

    Gamecube762

    Abdalion You seem to have forgotten to register your events.
     
  13. Offline

    JaguarJo

    Moved thread to a more appropriate forum.
     
  14. Offline

    Abdalion

    Not working. I created a new class called PlayerListener.
    So the two classes are:
    Nopick:

    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. @Override
    17. public void onEnable() {
    18. PluginDescriptionFile pdfFile = this.getDescription();
    19. new PlayerListener(this);
    20. this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " fue habilitado!");
    21. }
    22. List<UUID> list = new ArrayList<UUID>();
    23. @Override
    24. public void onDisable() {
    25. getLogger().info("fue deshabilitado!");
    26.  
    27.  
    28. }
    29.  
    30.  
    31.  
    32.  
    33. }
    34.  


    And PlayerListener:
    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.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.player.PlayerPickupItemEvent;
    13.  
    14. public class PlayerListener implements Listener {
    15. List<UUID> list = new ArrayList<UUID>();
    16. public PlayerListener(Nopick plugin) {
    17.  
    18. plugin.getServer().getPluginManager().registerEvents(this, plugin);
    19.  
    20. }
    21.  
    22. @EventHandler
    23. public void onPickup(PlayerPickupItemEvent event) {
    24. Player player = event.getPlayer();
    25. if(list.contains(player.getUniqueId()))
    26. event.setCancelled(true);
    27.  
    28. }
    29.  
    30. @EventHandler
    31. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    32. Player p = (Player) sender;
    33. if(commandLabel.equalsIgnoreCase("Nopick")){
    34. list.add(p.getUniqueId());
    35.  
    36. }
    37. return false;
    38. }
    39. }
    40.  


    What am i doing wrong? I put the command and still picks up items.
     
  15. Offline

    ZodiacTheories

    Abdalion

    In your onEnable() do this:

    Code:java
    1. getServer().getPluginManager().registerEvents(new PlayerListener(), this);
     
  16. Offline

    Skionz

    Abdalion looks like you forgot some brackets here
    Code:
    if(list.contains(player.getUniqueId()))
     
  17. Offline

    YoloEnderman

    Why does your onCommand have @EventHandler ?
    You should change it to:
    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("Nopick")){
    5. list.add(p.getUniqueId());
    6.  
    7. }
    8. return false;
    9. }
     
  18. Offline

    Abdalion


    new PlayerListener shows me an error "The constructor PlayerListener() is undefined"

    What should i do?
     
  19. Offline

    ZodiacTheories

    Abdalion

    Oh yeah, add the Nopick parameter inside the parentheses
     
  20. Offline

    Abdalion


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


    Nopick cannot be resolved to a variable D:
     
  21. Offline

    xTigerRebornx

    ZodiacTheories He is already registering his events in his Listeners constructor, therefor there is no need for that.
    Abdalion You don't implement CommandExecutor, and you never set the command's executor (as the onCommand is outside the main class it needs to be set).
    Keep in mind, since you are using a List, both the Listener you register and the executor you set need to share the same instance of PlayerListener, otherwise they will use separate lists and not work.
     
  22. Offline

    Abdalion


    So what should i do instead of: getServer().getPluginManager().registerEvents(new PlayerListener(Nopick), this); ?
    Please remember i am starting with coding, and i don't get you D:
     
  23. Offline

    xTigerRebornx

  24. Offline

    Abdalion

    Ok, i created a new project called "Nopick1" to test.
    2 classes:
    Nopic.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.  


    And 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.player.PlayerPickupItemEvent;
    12.  
    13.  
    14.  
    15. public class Nopick1CommandExecutor implements CommandExecutor {
    16. private final Nopick plugin;
    17.  
    18. public Nopick1CommandExecutor(Nopick plugin) {
    19. this.plugin = plugin;
    20. }
    21.  
    22. List<UUID> list = new ArrayList<UUID>();
    23.  
    24. public void onPickup(PlayerPickupItemEvent event) {
    25. Player player = event.getPlayer();
    26. if(list.contains(player.getUniqueId()))
    27. event.setCancelled(true);
    28.  
    29. }
    30.  
    31. @Override
    32. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    33. Player p = (Player) sender;
    34. if(commandLabel.equalsIgnoreCase("Nopick")){
    35. list.add(p.getUniqueId());
    36.  
    37.  
    38. }
    39. return false;
    40. }
    41. }
    42.  
    43.  


    Is it Ok? Just getting a warning at " private final Nopick plugin; " The value of the field Nopick1CommandExecutor.plugin is not used.
    Edit: Tryied the plugin. Not working.
     
  25. Offline

    xTigerRebornx

    Abdalion You've set the executor, but now you aren't registering your events and you've forgotten the @EventHandler annotation.
    (that being why the event part isn't working, there are other problems like blindly casting the sender)
     
  26. Offline

    Abdalion


    I added the @EventHandler
    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);


    How do i register the event?
     
  27. Offline

    xTigerRebornx

    Abdalion The same way you did it when you had your code before.
     
  28. Offline

    Abdalion

    Have you got skype? I have some doubts
     
  29. Offline

    xTigerRebornx

    Abdalion I'd prefer to keep it on this thread.
     
  30. Offline

    ZodiacTheories

    Oh yeah, didn't catch that
     
Thread Status:
Not open for further replies.

Share This Page