Development Assistance Preform a command before beginning an Event

Discussion in 'Plugin Help/Development/Requests' started by Ricecutter0, Mar 21, 2015.

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

    Ricecutter0

    Hello, I'm having a little trouble with a plugin I'm trying to make. In this plugin, I'm trying to make it get a block ID and location when right-clicking the said block, but before you right-click the block, you must preform a command (/blockinfo). I have figured out how to do so.. but when I attempt to add the UUID to the List when they perform the command.. it doesn't. <-- LINE 33 (BlockGet.java)

    Main.java (open)
    Code:java
    1. package me.Ricecutter0.BlockInfo;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.event.Listener;
    6. import org.bukkit.plugin.Plugin;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class Main extends JavaPlugin{
    10. private static Plugin plugin;
    11.  
    12. static ConfigManager cm = ConfigManager.getInstance();
    13.  
    14. static String prefix;
    15.  
    16.  
    17.  
    18. @Override
    19. public void onEnable(){
    20. plugin = this;
    21. ConfigManager.setup(this);
    22. registerEvents(this, new BlockGet());
    23. getCommand("blockinfo").setExecutor(new BlockGet());
    24. prefix = color(cm.getConfig().getString("prefix"));
    25. }
    26.  
    27. @Override
    28. public void onDisable(){
    29.  
    30. }
    31.  
    32. public static void registerEvents(org.bukkit.plugin.Plugin plugin, Listener... listeners) {
    33. for (Listener listener : listeners) {
    34. Bukkit.getServer().getPluginManager().registerEvents(listener, plugin);
    35. }
    36. }
    37.  
    38. public static Plugin getPlugin() {
    39. return plugin;
    40. }
    41.  
    42. public static String color(String str){
    43. return ChatColor.translateAlternateColorCodes('&',str);
    44. }
    45.  
    46. }


    BlockGet.java (COMMAND CLASS) (open)
    Code:java
    1. package me.Ricecutter0.BlockInfo;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5. import java.util.UUID;
    6.  
    7. import org.bukkit.Location;
    8. import org.bukkit.block.Block;
    9. import org.bukkit.command.Command;
    10. import org.bukkit.command.CommandExecutor;
    11. import org.bukkit.command.CommandSender;
    12. import org.bukkit.entity.Player;
    13. import org.bukkit.event.EventHandler;
    14. import org.bukkit.event.Listener;
    15. import org.bukkit.event.block.Action;
    16. import org.bukkit.event.player.PlayerInteractEvent;
    17.  
    18. public class BlockGet implements CommandExecutor, Listener {
    19.  
    20.  
    21.  
    22. public List<UUID> players = new ArrayList<UUID>();
    23.  
    24. @Override
    25. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    26. if(!(sender instanceof Player)){
    27. System.out.println("The Console is unable to handle this command.");
    28. return false;
    29. }
    30. Player p = (Player) sender;
    31.  
    32. p.sendMessage(Main.prefix + Main.color("&7Right-Click the block you'd like to recieve information on."));
    33. players.add(p.getUniqueId());
    34. p.sendMessage("Attempt...1"); //Used to see if the UUID was added
    35. return false;
    36. }
    37.  
    38. @SuppressWarnings({ "deprecation", "unused" })
    39. @EventHandler
    40. public void onPlayerInteract(PlayerInteractEvent e){
    41. Player p = e.getPlayer();
    42.  
    43. Block b = e.getClickedBlock();
    44.  
    45. Action a = e.getAction();
    46.  
    47. if(!players.contains(p.getUniqueId())){
    48. p.sendMessage("Attempt...2"); //Used to see if the UUID was added
    49. return;
    50. }
    51.  
    52. if(a.equals(Action.RIGHT_CLICK_BLOCK)){
    53. Location loc = b.getLocation();
    54. Integer id = b.getTypeId();
    55.  
    56. p.sendMessage(Main.color("&6------------"));
    57. p.sendMessage(Main.color("Block ID:&a " + id));
    58. p.sendMessage(Main.color("&6------------"));
    59. players.remove(p.getUniqueId());
    60. p.sendMessage("Attempt...3"); //Used to see if the UUID was added
    61.  
    62.  
    63. }
    64. }
    65. }


    EDIT: I updated my codes, and addressed more details about the issue.

    Any help would be appreciated, thank you.
     
    Last edited: Mar 21, 2015
  2. Offline

    Dsi_Mario

    What will right clicking the block do? I'm having a hard time understanding what you said.
     
  3. Offline

    Ricecutter0

    @Dsi_Mario

    I'll edit my post, but what I mean is that when you right-click the block (after preforming the /blockinfo command) it shows you the block information, which is the block location and the block ID.
     
  4. Offline

    Dsi_Mario

    You can make a HashMap of a player and a boolean to see if they did a command, and then Listen in on PlayerInteractEvent, see if the action is right clicking a block, and then give the block information (event.getClickedBlock().getX(), y, z, event.getClickedBlock().getTypeId()).

    Then again, I'm bad at explaining things, and this probably isn't the best way of doing things.
     
  5. Offline

    Ricecutter0

    @Dsi_Mario

    Could you give me an example in code?
     
  6. Offline

    Dsi_Mario

    What part do you want an example for?
     
  7. Offline

    Ricecutter0

    That part
    @Dsi_Mario
     
  8. Offline

    Dsi_Mario

    Code:
    public HashMap<UUID, Boolean> s = new HashMap<>();
    
    command checks here{
    UUID u = sender.getUniqueId();
    if(s.containsKey(y){
    if(s.get(u)){
    s.put(u, false);
    //Doing the command while activated
    }else{
    s.put(u, true);
    //Doing the command while non activated
    }
    }else{
    s.put(u, true);
    //First time doing the command
    }
    Need more help? http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html
     
  9. Offline

    Ruptur

    @Dsi_Mario
    @Ricecutter0

    A hashmap is unnessesery
    You could save the players to a list then add them to the link on the command and on block click remove them.
     
  10. Offline

    Ricecutter0

    I updated my codes, and addressed new details to the issue in the first post.
     
  11. Offline

    Dsi_Mario

    @Ruptur
    I guess I had HashMaps on the mind.
     
Thread Status:
Not open for further replies.

Share This Page