ArrayList doesn't seem to be working

Discussion in 'Plugin Development' started by JellyYods, Jun 9, 2014.

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

    JellyYods

    Okay so I'm making a kit called vampire. What this kit does is when you kill someone it will give them a potion of instant damage or health I don't remember, but anyways When I do /vampire and kill someone I don't get regeneration or a potion at all.

    Main Class

    Code:java
    1. package Bods.Main;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.plugin.PluginDescriptionFile;
    8. import org.bukkit.plugin.PluginManager;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10.  
    11. import Bods.Commands.CommandVampire;
    12.  
    13. public class Main extends JavaPlugin implements Listener {
    14.  
    15. public final Logger logger = Logger.getLogger("Minecraft");
    16.  
    17.  
    18. @Override
    19. public void onDisable() {
    20. PluginDescriptionFile pdfFile = this.getDescription();
    21. this.logger.info(pdfFile.getName() + " Has been disabled! ");
    22. }
    23.  
    24. @Override
    25. public void onEnable() {
    26. PluginDescriptionFile pdfFile = this.getDescription();
    27. this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " has been enabled! " + pdfFile.getDescription() + pdfFile.getAuthors());
    28.  
    29. PluginManager pm = Bukkit.getServer().getPluginManager();
    30. pm.registerEvents(new CommandVampire(), this);
    31. getCommand("vampire").setExecutor(new CommandVampire()); }
    32.  
    33. }


    Here's the Vampire Class

    Code:java
    1. package Bods.Commands;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.Color;
    7. import org.bukkit.Material;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandExecutor;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.enchantments.Enchantment;
    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.inventory.ItemStack;
    17. import org.bukkit.inventory.meta.LeatherArmorMeta;
    18. import org.bukkit.potion.PotionEffect;
    19. import org.bukkit.potion.PotionEffectType;
    20.  
    21.  
    22. public class CommandVampire implements Listener, CommandExecutor {
    23.  
    24. public double Boost = (18.0D);
    25.  
    26. ArrayList<String> Vampire = new ArrayList<String>();
    27.  
    28. @EventHandler
    29. public void Remove(PlayerDeathEvent e) {
    30. Vampire.remove(e.getEntity().getPlayer().getName());
    31. }
    32.  
    33. @EventHandler
    34. public void onDeath(PlayerDeathEvent e) {
    35. if (Vampire.contains(e.getEntity().getPlayer().getName())) {
    36. Player k = e.getEntity().getPlayer().getKiller();
    37. k.setHealth(Boost);
    38. k.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 10 , 10));
    39. k.getInventory().addItem(new ItemStack(Material.POTION, 1, (short)16428));
    40. }
    41. }
    42.  
    43. public boolean onCommand(CommandSender sender, Command cmd, String label,
    44. String[] args) {
    45.  
    46. Player player = (Player)sender;
    47.  
    48. if(cmd.getName().equalsIgnoreCase("vampire")) {
    49.  
    50. player.getInventory().clear();
    51.  
    52. ItemStack Helm = new ItemStack(Material.LEATHER_HELMET);
    53. LeatherArmorMeta Helmet = (LeatherArmorMeta)Helm.getItemMeta();
    54. Helmet.setColor(Color.RED);
    55. player.getInventory().setHelmet(Helm);
    56.  
    57. ItemStack Chest = new ItemStack(Material.LEATHER_CHESTPLATE);
    58. LeatherArmorMeta Chestplate = (LeatherArmorMeta)Chest.getItemMeta();
    59. Chestplate.setColor(Color.BLACK);
    60. player.getInventory().setChestplate(Chest);
    61.  
    62. player.getInventory().setLeggings(new ItemStack(Material.GOLD_LEGGINGS));
    63.  
    64. player.getInventory().setBoots(new ItemStack(Material.IRON_BOOTS));
    65.  
    66. ItemStack Sword = new ItemStack(Material.IRON_SWORD);
    67. Sword.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, 2);
    68. Sword.addUnsafeEnchantment(Enchantment.DURABILITY, 15);
    69.  
    70. player.getInventory().addItem(new ItemStack[] { Sword });
    71.  
    72. for (int Soup = 0; Soup < 35; Soup++) {
    73. player.getInventory().addItem(new ItemStack(Material.MUSHROOM_SOUP));
    74. }
    75.  
    76. player.sendMessage(ChatColor.YELLOW + "You have received the Vampire Kit!");
    77.  
    78. Vampire.add(player.getName());
    79.  
    80. }
    81.  
    82. return false;
    83. }
    84.  
    85. }
    86.  
    87.  


    And the plugin.yml

    name: BodsPvP

    version: 1.0

    main: Bods.Main.Main

    commands:

    vampire:

    description: When you kill another player you get healed and get a splash potion of healing!
     
  2. Offline

    teej107

    You are creating 2 CommandVampire instances between your event registering and setting your command executor. Try assigning CommandVampire to one variable so you won't create 2 instances of it and see if that works.

    Edit: Oh I see something else. This might be called before your onDeath event.
    Code:java
    1. @EventHandler
    2. public void Remove(PlayerDeathEvent e) {
    3. Vampire.remove(e.getEntity().getPlayer().getName());
    4. }
     
    Garris0n likes this.
  3. Offline

    JellyYods

    teej107 what do you mean by I am assigning 2 CommandVampire instances does that mean Im register my event ad command in the same class?
     
  4. Offline

    teej107

    JellyYods No, what I mean is that you are creating 2 CommandVampire instances. But I don't think that is the problem. What I think is the problem is this method/event you have here:
    Code:java
    1. @EventHandler
    2. public void Remove(PlayerDeathEvent e) {
    3. Vampire.remove(e.getEntity().getPlayer().getName());
    4. }
    It might be being called before your other event.
     
  5. Offline

    spy_1134

    You should do this:
    Code:java
    1. CommandVampire cv = new CommandVampire();
    2. pm.registerEvents(cv, this);
    3. getCommand("vampire").setExecutor(cv);
    4.  


    That way you don't get two instances of the same class. They would each have a different copy of the list of players.

    EDIT: What the other guy said is a problem too. Just make it so the remove method is not an event handler and call it from your other on death event method after you do everything else that you need to.
     
  6. Offline

    teej107

    spy_1134 JellyYods Yes that is a good idea but I don't think that is causing the problem. It is probably his Remove method conflicting with the onDeath method.
     
  7. Offline

    spy_1134

    It's just as big of a problem actually. If the player only gets added to the list in the command handler copy of that class then the one handling events will never see them and won't do anything after they kill a player because it will look as if they had never entered the command in the first place. They need to be the same instance and that needs to be fixed too to prevent the player being removed from the list before the event is triggered.
     
  8. Offline

    JellyYods

    Alright so the first one on the Remove I set it to the very bottom of the class so that shouldn't be an issue and in Main class I did what you wrote accept with cv I did Cvampire
     
  9. Offline

    teej107

    JellyYods Instead of having 2 of the same event, why don't you put the code bit in Remove at the bottom of onDeath? Just my suggestion.

    spy_1134 Good point. I didn't think about that. That is what is causing the problem but also your Remove method could be called first which would also thwart your onDeath method
     
  10. Offline

    JellyYods

    Alright so spy_1134 I need to put it the way I had it and remove the DeathEvent where it remove them from the array list
     
  11. Offline

    spy_1134

    The name can be whatever you want so long as you keep it consistent. Naming conventions dictate that you should typically only use capital letters in the beginning of class names and not variables however, so cVampire would be a good name, but Cvampire wouldn't.

    Edit: Do what the other guy said too and make it so your remove method is not an event handler. Just run it from the end of your other on death event method.
     
  12. Offline

    JellyYods

    Thank you spy_1134 and teej107 it works Thanks again I have been working for a day now
     
  13. Offline

    bigteddy98

    Set status to solved please when solved, I was reading this until I found out it had already been solved! :(
     
  14. Offline

    JellyYods

    I have another problem I have the pvp kit When I kill someone it gives them the regen and the potion, but not the vampire when I get a kill it only worls once then the second time as a vampire it does nothing happenes
    I think it might have to do with them removing the player from the list during the

    I fixed the pvp kit and any kit getting a potion and regen but vampire works once when you kill someone

    I think I need to do something like this spy_1134 teej107

    Code:java
    1. rrayList<String> Vampire = new ArrayList<String>();
    2.  
    3. @EventHandler
    4. public void onDeath(PlayerDeathEvent e) {
    5. if (Vampire.contains(e.getEntity().getPlayer().getName())) {
    6. Player killed = e.getEntity().getPlayer();
    7. Player killer = e.getEntity().getPlayer().getKiller();
    8. if(killer instanceof Player && killed killed)
    9. killer.setHealth(Boost);
    10. killer.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 20 , 20));
    11. killer.getInventory().addItem(new ItemStack(Material.POTION, 1, (short)16428));
    12. }
    13. Vampire.remove(e.getEntity().getKiller());
    14. }


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

    teej107

    JellyYods If you want to give the regen and a potion everytime, don't remove them from the arraylist. Also please check Java Naming Conventions. Variables don't start with capital letters. It won't hurt your code but it's a rule that Java developers should follow.
     
  16. Offline

    JellyYods

    Well I don't want them to get potions anymore after they die unless they do the command again I'm making a kit server

    So I don't pvp kit getting pots

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

    teej107

    JellyYods You're removing the player's killer and not the player
    Code:java
    1. Vampire.remove(e.getEntity().getKiller());
     
  18. Offline

    JellyYods

    Alright so Im pvp kit I get regen and pots all the time when Im vampire it gives it to me once because at the bottom of the code it removes me how do I keep it in till I die

    Where would I put the code
     
  19. Offline

    teej107

    JellyYods
    It looks fine where it is at.
     
Thread Status:
Not open for further replies.

Share This Page