[Solved] Nothing is executed on right click, but null is caught when conditions not met

Discussion in 'Plugin Development' started by ZeusAllMighty11, Sep 4, 2012.

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

    ZeusAllMighty11

    Code:java
    1.  
    2. package me.zeus.MCSnD;
    3.  
    4. import java.util.ArrayList;
    5. import java.util.HashMap;
    6.  
    7. import org.bukkit.Bukkit;
    8. import org.bukkit.ChatColor;
    9. import org.bukkit.Material;
    10. import org.bukkit.block.Block;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.event.EventHandler;
    13. import org.bukkit.event.Listener;
    14. import org.bukkit.event.block.Action;
    15. import org.bukkit.event.player.PlayerInteractEvent;
    16. import org.bukkit.inventory.ItemStack;
    17.  
    18. public class BombListener implements Listener {
    19.  
    20. public static HashMap<String, Boolean> isAlive = new HashMap<String, Boolean>();
    21. public static ArrayList<String> inGame = new ArrayList<String>();
    22. public static ArrayList<String> bombCarrier = new ArrayList<String>();
    23.  
    24. @EventHandler
    25. public void onBombInteract(final PlayerInteractEvent e) {
    26. final Player bombcarrier = e.getPlayer();
    27. final Block clicked = e.getClickedBlock();
    28.  
    29. if (bombcarrier instanceof Player) {
    30. if (e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
    31. if (clicked.getType().equals(Material.TNT)) {
    32. if (inGame.contains(bombcarrier)) {
    33. clicked.setType(Material.AIR);
    34.  
    35. // add bomb carrier to the list of bomb carrier
    36.  
    37. bombCarrier.add(bombcarrier.getName());
    38.  
    39. // Give the bomb carrier the wool they broke
    40.  
    41. bombcarrier.getInventory().addItem(
    42. new ItemStack((Material.WOOL), 1));
    43.  
    44. // broadcast which user has the bomb
    45.  
    46. Bukkit.broadcastMessage(ChatColor.GOLD + "[MCSnD]"
    47. + ChatColor.YELLOW + "Player " + ChatColor.RED
    48. + bombcarrier.getName() + ChatColor.YELLOW
    49. + " has picked up the bomb! ");
    50.  
    51. // Send the bomb carrier the message they picked bomb up
    52.  
    53. bombcarrier.sendMessage(ChatColor.GOLD + "[MCSnD]"
    54. + ChatColor.GREEN
    55. + " You've got the bomb! Go plant it! ");
    56. }
    57. } else {
    58. System.out.println("null");
    59. }
    60. }
    61. }
    62. }
    63. }
    64.  


    So basically, when a player right clicks TNT (the bomb), they 'pick up' the bomb. It is added to their inventory, and they are added to a special list. (and a message is broadcasted)
    if material is not tnt, null is printed. (works)
    if material is tnt, do said above (does not work)

    Please help, I don't know why it doesn't work.
     
  2. Did you ever add people to inGame?
    Also you are checking whether inGame.contains a Player when inGame is an ArrayList of Strings.
     
    ZeusAllMighty11 likes this.
  3. Offline

    theguynextdoor

    If it is a PLAYER interact event, why do you need to check if the player is a player?
    So you can remove
    Code:
    if (bombcarrier instanceof Player) {
    For comparing enums you should use == instead of .equals.
    So it would be
    Code:
    if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
    This would also apply to checking the material type

    If changing those does not work, post again with some more code maybe and i shall have a better look

    EDIT: Yes as Pandemonues said, are they actually added to the list. (Can't believe i didn't think of that one :p)
     
  4. Offline

    ZeusAllMighty11

    Pandemoneus I have a seperate class so when they do /join it adds them to inGame (works fine)
    theguynextdoor I will try those suggestions

    Did not work... Did your suggestions though

    Code:

    Main:
    Code:java
    1.  
    2. package me.zeus.MCSnD;
    3.  
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.plugin.PluginManager;
    8. import org.bukkit.plugin.java.JavaPlugin;
    9.  
    10. public class main extends JavaPlugin implements Listener {
    11.  
    12. public void onEnable(){
    13.  
    14. System.out.println("[MCSnD] Minecraft SnD Enabled and ready to play!");
    15. PluginManager pm = Bukkit.getServer().getPluginManager();
    16. pm.registerEvents(new BombListener(), this);
    17.  
    18. getCommand("join").setExecutor(new GameCommands());
    19. getCommand("leave").setExecutor(new GameCommands());
    20.  
    21.  
    22. }
    23.  
    24. public void onDisable(){
    25. System.out.println("[MCSnD] Minecraft SnD disabled due to reload or server stop!");
    26.  
    27. }
    28.  
    29. }
    30.  


    GameCommands
    Code:text
    1.  
    2. package me.zeus.MCSnD;
    3.  
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandExecutor;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9.  
    10. public class GameCommands implements CommandExecutor{
    11.  
    12. @Override
    13. public boolean onCommand(final CommandSender sender, final Command cmd,
    14. final String label, final String[] args) {
    15.  
    16. if (cmd.getName().equalsIgnoreCase("join")) {
    17. System.out.print(BombListener.inGame);
    18. if (sender instanceof Player) {
    19. BombListener.inGame.add(sender.getName());
    20. ((Player) sender).setDisplayName(ChatColor.GOLD + "[Game] " + ChatColor.RESET + sender.getName());
    21. sender.sendMessage(ChatColor.GOLD + "[MCSnD]"
    22. + ChatColor.GREEN + " You've joined the game! ");
    23. } else {
    24. sender.sendMessage(ChatColor.GOLD + "[MCSnD]"
    25. + ChatColor.RED + " Sorry, you must be a player to join! ");
    26. }
    27. }
    28.  
    29. if (cmd.getName().equalsIgnoreCase("leave")) {
    30. System.out.print(BombListener.inGame);
    31. if (sender instanceof Player) {
    32. BombListener.inGame.remove(sender.getName());
    33.  
    34. ((Player) sender).setDisplayName(ChatColor.RESET + sender.getName());
    35.  
    36. sender.sendMessage(ChatColor.GOLD + "[MCSnD]"
    37. + ChatColor.RED + " You've left the game! ");
    38. } else {
    39. sender.sendMessage(ChatColor.GOLD + "[MCSnD]"
    40. + ChatColor.RED + " Sorry, you must be a player to leave! ");
    41. }
    42. }
    43.  
    44.  
    45.  
    46.  
    47.  
    48. return true;
    49. }
    50. }
    51.  
    52.  


    BombListener
    Code:text
    1.  
    2.  
    3. plugin YML
    4. [code]
    5. name: MC_SnD
    6. author: ZeusAllMighty11
    7. description: Minecraft version of Call of Duty famous SnD
    8. version: 0.1
    9. main: me.zeus.MCSnD.main
    10. commands:
    11. join:
    12. default: true
    13. usage:
    14. description: Join the red team!
    15. setspawn:
    16. default: true
    17. usage:
    18. description: Set the spawn where all players will spawn!
    19. leave:
    20. default: true
    21. usage:
    22. description: Leave your current team and the game!
    23. [/code]
    24.  
    25. EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 28, 2016
  5. Offline

    theguynextdoor

    Your inGame is a List of strings. But in your listener when you check if they are in, you pass in the player object.

    You do
    Code:
    if (inGame.contains(bombcarrier)) {
    When you want to do
    Code:
    if (inGame.contains(bombcarrier.getName())) {
     
  6. Offline

    ZeusAllMighty11

    wow i'm dumb, thanks
     
  7. Already said so in my post. :)
     
Thread Status:
Not open for further replies.

Share This Page