Solved Arraylist contains player but isn't cancelling pvp

Discussion in 'Plugin Development' started by Fhbgsdhkfbl, Jun 25, 2014.

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

    Fhbgsdhkfbl

    Hey bukkit coders, I made a /spectate command
    and when they're in the arraylist, I want to cancel damage to other players
    and this doesn't seem to work.

    Code:java
    1. @EventHandler
    2. public void onPlayerHit(EntityDamageByEntityEvent e) {
    3. if(e.getDamager() instanceof Player) {
    4. Player p = (Player) e.getEntity();
    5. if(plugin.spectate.contains(p.getName())) {
    6. e.setCancelled(true);
    7. p.sendMessage(ChatColor.GRAY + "You are currently in" + ChatColor.AQUA + " Spectate Mode," + ChatColor.GRAY + " you are not allowed to hit other players.");
    8. }
    9. }
    10. }
     
  2. Offline

    MCForger

    Fhbgsdhkfbl
    Can we see were you add players to the spectate list?
     
  3. Offline

    Fhbgsdhkfbl

    MCForger
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String args[]) {
            Player p = (Player) sender;
            if(cmd.getName().equalsIgnoreCase("spectate")) {
                if(plugin.used.contains(p.getName())) {
                    p.sendMessage(ChatColor.GRAY + "You must not have a kit to go in Spectate mode");
                    return true;
                }
                if(plugin.spectate.contains(p.getName())) {
                    p.sendMessage(ChatColor.GRAY + "You are already in" + ChatColor.AQUA + " Spectate Mode!");
                    return true;
                }
                p.sendMessage(ChatColor.GRAY + "You are now in" + ChatColor.AQUA + " Specate Mode." + ChatColor.GRAY + " Do /unspectate to get out of it.");
                plugin.spectate.add(p.getName());
                p.setGameMode(GameMode.CREATIVE);
                p.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 100000000, 1000)); // Remember, Ticks, then level Jordan!
                }
            if(cmd.getName().equalsIgnoreCase("unspectate")) {
                if(!(plugin.spectate.contains(p.getName()))) {
                    p.sendMessage(ChatColor.GRAY + "You currently aren't in" + ChatColor.AQUA + " Spectate Mode");
                    return true;
                }
                plugin.spectate.remove(p.getName());
                p.sendMessage(ChatColor.GRAY + "You are no longer in" + ChatColor.AQUA + " Spectate Mode. Go PvP now!");
                p.setGameMode(GameMode.SURVIVAL);
                p.teleport(p.getWorld().getSpawnLocation());
                p.removePotionEffect(PotionEffectType.INVISIBILITY);
            }
            return false;
        }
     
    }
     
  4. Offline

    thecrystalflame

    add a debug line to the event that prints true to tha console if the player is in the list. i think you may find that its not adding the player correctly
     
  5. Offline

    MCForger

  6. Offline

    Fhbgsdhkfbl

    MCForger
    I did.
    Full class:
    Code:java
    1. package commands;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.GameMode;
    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. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    12. import org.bukkit.event.player.PlayerDropItemEvent;
    13. import org.bukkit.potion.PotionEffect;
    14. import org.bukkit.potion.PotionEffectType;
    15.  
    16. public class Spectate implements CommandExecutor, Listener {
    17.  
    18. private me.Fhbgsdhkfbl.Main plugin;
    19. public Spectate(me.Fhbgsdhkfbl.Main plugin){
    20. this.plugin = plugin;
    21. }
    22. @EventHandler
    23. public void onSpectateDrop(PlayerDropItemEvent e) {
    24. Player p = e.getPlayer();
    25. if(plugin.spectate.contains(p.getName())) {
    26. e.setCancelled(true);
    27. p.sendMessage(ChatColor.GRAY + "You cannot drop items while in" + ChatColor.AQUA + " Spectate Mode.");
    28. }
    29. }
    30. @EventHandler
    31. public void onPlayerHit(EntityDamageByEntityEvent e) {
    32. if(e.getDamager() instanceof Player) {
    33. Player p = (Player) e.getEntity();
    34. if(plugin.spectate.contains(p.getName())) {
    35. e.setCancelled(true);
    36. p.sendMessage(ChatColor.GRAY + "You are currently in" + ChatColor.AQUA + " Spectate Mode," + ChatColor.GRAY + " you are not allowed to hit other players.");
    37. }
    38. }
    39. }
    40. public boolean onCommand(CommandSender sender, Command cmd, String label, String args[]) {
    41. Player p = (Player) sender;
    42. if(cmd.getName().equalsIgnoreCase("spectate")) {
    43. if(plugin.used.contains(p.getName())) {
    44. p.sendMessage(ChatColor.GRAY + "You must not have a kit to go in Spectate mode");
    45. return true;
    46. }
    47. if(plugin.spectate.contains(p.getName())) {
    48. p.sendMessage(ChatColor.GRAY + "You are already in" + ChatColor.AQUA + " Spectate Mode!");
    49. return true;
    50. }
    51. p.sendMessage(ChatColor.GRAY + "You are now in" + ChatColor.AQUA + " Specate Mode." + ChatColor.GRAY + " Do /unspectate to get out of it.");
    52. plugin.spectate.add(p.getName());
    53. p.setGameMode(GameMode.CREATIVE);
    54. p.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 100000000, 1000)); // Remember, Ticks, then level Jordan!
    55. }
    56. if(cmd.getName().equalsIgnoreCase("unspectate")) {
    57. if(!(plugin.spectate.contains(p.getName()))) {
    58. p.sendMessage(ChatColor.GRAY + "You currently aren't in" + ChatColor.AQUA + " Spectate Mode");
    59. return true;
    60. }
    61. plugin.spectate.remove(p.getName());
    62. p.sendMessage(ChatColor.GRAY + "You are no longer in" + ChatColor.AQUA + " Spectate Mode. Go PvP now!");
    63. p.setGameMode(GameMode.SURVIVAL);
    64. p.teleport(p.getWorld().getSpawnLocation());
    65. p.removePotionEffect(PotionEffectType.INVISIBILITY);
    66. }
    67. return false;
    68. }
    69.  
    70. }
    71.  

    The no-drop when they're in the list works.
     
  7. Offline

    MCForger

    Fhbgsdhkfbl
    Where do you call getServer().getPluginManager().registerEvents(this, this); ?
     
  8. Offline

    Fhbgsdhkfbl

    MCForger
    My main class
    pm.registerEvents(new Spectate(this), this);

    MCForger
    Do you know what the problem is?

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

    MCForger

    Fhbgsdhkfbl
    Not really. I would have to download the code and debug it...can I see the Main class before I do that please?
     
  10. Offline

    Fhbgsdhkfbl

    MCForger
    Code:java
    1. package me.Fhbgsdhkfbl;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.HashMap;
    5. import java.util.List;
    6.  
    7. import net.milkbowl.vault.economy.Economy;
    8.  
    9. import org.bukkit.Bukkit;
    10. import org.bukkit.ChatColor;
    11. import org.bukkit.GameMode;
    12. import org.bukkit.entity.Player;
    13. import org.bukkit.event.EventHandler;
    14. import org.bukkit.event.Listener;
    15. import org.bukkit.event.entity.PlayerDeathEvent;
    16. import org.bukkit.event.player.PlayerJoinEvent;
    17. import org.bukkit.event.player.PlayerQuitEvent;
    18. import org.bukkit.plugin.PluginManager;
    19. import org.bukkit.plugin.RegisteredServiceProvider;
    20. import org.bukkit.plugin.java.JavaPlugin;
    21.  
    22. import commands.Spectate;
    23.  
    24. public class Main extends JavaPlugin implements Listener {
    25. public List<String> used = new ArrayList<String>();
    26. public List<String> refill = new ArrayList<String>();
    27. public List<String> refillcooldown = new ArrayList<String>();
    28. public List<String> soldier = new ArrayList<String>();
    29. public List<String> bartender = new ArrayList<String>();
    30. public List<String> may = new ArrayList<String>();
    31. public List<String> maycooldown = new ArrayList<String>();
    32. public List<String> ninja = new ArrayList<String>();
    33. public List<String> cheetah = new ArrayList<String>();
    34. public List<String> cheetahcooldown = new ArrayList<String>();
    35. public List<String> snowman = new ArrayList<String>();
    36. public List<String> snowz = new ArrayList<String>();
    37. public List<String> shark = new ArrayList<String>();
    38. public List<String> poseidon = new ArrayList<String>();
    39. public List<String> stomper = new ArrayList<String>();
    40. public List<String> fisherman = new ArrayList<String>();
    41. public List<String> eagle = new ArrayList<String>();
    42. public List<String> eaglecooldown = new ArrayList<String>();
    43. public List<String> frog = new ArrayList<String>();
    44. public List<String> frogcooldown = new ArrayList<String>();
    45. public List<String> percy = new ArrayList<String>();
    46. public List<String> tank = new ArrayList<String>();
    47. public List<String> thor = new ArrayList<String>();
    48. public List<String> thorcooldown = new ArrayList<String>();
    49. public List<String> artemis = new ArrayList<String>();
    50. public List<String> ghost = new ArrayList<String>();
    51. public List<String> ghostcooldown = new ArrayList<String>();
    52. public List<String> hades = new ArrayList<String>();
    53. public List<String> hadescooldown = new ArrayList<String>();
    54. public List<String> apollo = new ArrayList<String>();
    55. public List<String> apollocooldown = new ArrayList<String>();
    56. public List<String> switcher = new ArrayList<String>();
    57. public List<String> viper = new ArrayList<String>();
    58. public List<String> snail = new ArrayList<String>();
    59. public List<String> kangaroo = new ArrayList<String>();
    60. public List<String> bumblebee = new ArrayList<String>();
    61. public List<String> bumblebeecooldown = new ArrayList<String>();
    62. public List<String> spectate = new ArrayList<String>();
    63. public HashMap<String, Integer> kills = new HashMap<String, Integer>();
    64. public static Economy econ = null;
    65. private boolean setupEconomy() {
    66. if (getServer().getPluginManager().getPlugin("Vault") == null) {
    67. return false;
    68. }
    69. RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
    70. if (rsp == null) {
    71. return false;
    72. }
    73. econ = rsp.getProvider();
    74. return econ != null;
    75. }
    76. public void onEnable() {
    77. if (!setupEconomy() ) {
    78. getLogger().severe(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName()));
    79. getServer().getPluginManager().disablePlugin(this);
    80.  
    81. return;
    82. }
    83. registercommands();
    84. getServer().getPluginManager().registerEvents(this, this);
    85. PluginManager pm = Bukkit.getPluginManager();
    86. pm.registerEvents(new KitEvents(this), this);
    87. pm.registerEvents(new NoDrop(), this);
    88. pm.registerEvents(new Killstreak(this), this);
    89. pm.registerEvents(new Spectate(this), this);
    90. }
    91.  
    92. public void onDisable() {
    93.  
    94. }
    95. public void registercommands() {
    96. getCommand("knight").setExecutor(new default_kits.PvP(this));
    97. getCommand("archer").setExecutor(new default_kits.Archer(this));
    98. getCommand("kits").setExecutor(new commands.Kits(this));
    99. getCommand("soup").setExecutor(new commands.Soup(this));
    100. getCommand("Soldier").setExecutor(new default_kits.Soldier(this));
    101. getCommand("Bartender").setExecutor(new default_kits.Bartender(this));
    102. getCommand("May").setExecutor(new default_kits.May(this));
    103. getCommand("Ninja").setExecutor(new default_kits.Ninja(this));
    104. getCommand("Snowman").setExecutor(new default_kits.Snowman(this));
    105. getCommand("Coder").setExecutor(new commands.Coder(this));
    106. getCommand("Cheetah").setExecutor(new default_kits.Cheetah(this));
    107. getCommand("Rhino").setExecutor(new default_kits.Rhino(this));
    108. getCommand("Shark").setExecutor(new default_kits.Shark(this));
    109. getCommand("Poseidon").setExecutor(new default_kits.Poseidon(this));
    110. getCommand("Stomper").setExecutor(new default_kits.Stomper(this));
    111. getCommand("Ares").setExecutor(new default_kits.Ares(this));
    112. getCommand("Fisherman").setExecutor(new default_kits.Fisherman(this));
    113. getCommand("Eagle").setExecutor(new default_kits.Eagle(this));
    114. getCommand("Percy").setExecutor(new default_kits.Percy(this));
    115. getCommand("Tank").setExecutor(new default_kits.Tank(this));
    116. getCommand("Thor").setExecutor(new default_kits.Thor(this));
    117. getCommand("Ghost").setExecutor(new default_kits.Ghost(this));
    118. getCommand("Artemis").setExecutor(new default_kits.Artemis(this));
    119. getCommand("Pyro").setExecutor(new default_kits.Pyro(this));
    120. getCommand("Hades").setExecutor(new default_kits.Hades(this));
    121. getCommand("Apollo").setExecutor(new default_kits.Apollo(this));
    122. getCommand("Switcher").setExecutor(new default_kits.Switcher(this));
    123. getCommand("Viper").setExecutor(new default_kits.Viper(this));
    124. getCommand("Kangaroo").setExecutor(new default_kits.Kangaroo(this));
    125. getCommand("Snail").setExecutor(new default_kits.Snail(this));
    126. getCommand("Bumblebee").setExecutor(new default_kits.Bumblebee(this));
    127. getCommand("kit").setExecutor(new commands.Kit(this));
    128. getCommand("kitinfo").setExecutor(new commands.Kit(this));
    129. getCommand("Frog").setExecutor(new default_kits.Frog(this));
    130. getCommand("Cyclops").setExecutor(new default_kits.Cyclops(this));
    131. getCommand("Spectate").setExecutor(new commands.Spectate(this));
    132. getCommand("Unspectate").setExecutor(new commands.Spectate(this));
    133. }
    134. public static int randomBetween1(int min, int max) {
    135. int range = max - min;
    136. return min + (int) (Math.random() * (range));
    137. }
    138. @EventHandler
    139. public void onPLayerD(PlayerDeathEvent e) {
    140. Player p = e.getEntity();
    141. if (p.getKiller() instanceof Player) {
    142. Player killer = (Player) p.getKiller();
    143. int r = randomBetween1(1, 20);
    144. killer.sendMessage("§3You got §e" + r + " dollors §3for killing §4"
    145. + p.getName() + "§3.");
    146. econ.depositPlayer(killer.getName(), r);
    147. p.sendMessage(ChatColor.GRAY + "You were killed by " + ChatColor.AQUA + killer.getName());
    148.  
    149. }
    150.  
    151. }
    152. @EventHandler
    153. public void onPlayerQuit(PlayerQuitEvent e) {
    154. Player p = e.getPlayer();
    155. if(spectate.contains(p.getName())) {
    156. spectate.remove(p.getName());
    157. p.setGameMode(GameMode.SURVIVAL);
    158. }
    159. }
    160. @EventHandler
    161. public void onPlayerJoin(PlayerJoinEvent e) {
    162. Player p = e.getPlayer();
    163. if(spectate.contains(p.getName())) {
    164. spectate.remove(p.getName());
    165. p.setGameMode(GameMode.SURVIVAL);
    166. }
    167. }
    168.  
    169.  
    170. @EventHandler
    171. public void onDeathRemoveKit(PlayerDeathEvent e){
    172. Player p = (Player)e.getEntity();
    173. e.getDrops().clear();
    174. if(used.contains(p.getName())){
    175. used.remove(p.getName());
    176. stomper.remove(p.getName());
    177. refill.remove(p.getName());
    178. refillcooldown.remove(p.getName());
    179. switcher.remove(p.getName());
    180. soldier.remove(p.getName());
    181. ghost.remove(p.getName());
    182. ghostcooldown.remove(p.getName());
    183. bartender.remove(p.getName());
    184. may.remove(p.getName());
    185. maycooldown.remove(p.getName());
    186. ninja.remove(p.getName());
    187. cheetah.remove(p.getName());
    188. cheetahcooldown.remove(p.getName());
    189. snowman.remove(p.getName());
    190. snowz.remove(p.getName());
    191. shark.remove(p.getName());
    192. poseidon.remove(p.getName());
    193. fisherman.remove(p.getName());
    194. eagle.remove(p.getName());
    195. eaglecooldown.remove(p.getName());
    196. frog.remove(p.getName());
    197. frogcooldown.remove(p.getName());
    198. percy.remove(p.getName());
    199. tank.remove(p.getName());
    200. thor.remove(p.getName());
    201. thorcooldown.remove(p.getName());
    202. artemis.remove(p.getName());
    203. hades.remove(p.getName());
    204. hadescooldown.remove(p.getName());
    205. apollo.remove(p.getName());
    206. apollocooldown.remove(p.getName());
    207. switcher.remove(p.getName());
    208. snail.remove(p.getName());
    209. viper.remove(p.getName());
    210. bumblebee.remove(p.getName());
    211. bumblebeecooldown.remove(p.getName());
    212. kangaroo.remove(p.getName());
    213. spectate.remove(p.getName());
    214. }
    215. }
    216. }
    217.  
     
Thread Status:
Not open for further replies.

Share This Page