Add a 1 kit per life system.

Discussion in 'Plugin Development' started by Verify_x, Mar 25, 2014.

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

    Verify_x

    I'm making a kit plugin and i need some help. I need to have it so I players can only have 1 kit per life, how would I do this using an ArrayList?
    Here's my code so far:
    Code:java
    1. package ckits.kits;
    2.  
    3. import org.bukkit.Material;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandExecutor;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.enchantments.Enchantment;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.inventory.ItemStack;
    10. import org.bukkit.inventory.PlayerInventory;
    11. import org.bukkit.potion.PotionEffect;
    12. import org.bukkit.potion.PotionEffectType;
    13.  
    14. public class PVP implements CommandExecutor{
    15.  
    16. @Override
    17. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    18. if(!(sender instanceof Player)) {
    19. return false;
    20. }
    21. Player player = (Player)sender;
    22. if(commandLabel.equalsIgnoreCase("pvp")) {
    23. player.sendMessage("§bMcChillz§7§l > §6Taken Kit PVP!");
    24. clearInventory(player.getInventory());
    25. player.getInventory().setHelmet(new ItemStack(Material.IRON_HELMET,1));
    26. player.getInventory().setChestplate(new ItemStack(Material.IRON_CHESTPLATE,1));
    27. player.getInventory().setLeggings(new ItemStack(Material.IRON_LEGGINGS,1));
    28. player.getInventory().setBoots(new ItemStack(Material.IRON_BOOTS,1));
    29.  
    30.  
    31. ItemStack dsword = new ItemStack(Material.DIAMOND_SWORD);
    32. dsword.addEnchantment(Enchantment.DAMAGE_ALL, 1);
    33. player.getInventory().addItem(dsword);
    34. player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 1000000, 1));
    35. for (int i = 0; i < 35; i++)
    36. player.getInventory().addItem(new ItemStack[] { new ItemStack(Material.MUSHROOM_SOUP) });
    37.  
    38. }
    39. return true;
    40. }
    41.  
    42. public void clearInventory(PlayerInventory inv) {
    43. inv.clear();
    44. inv.setHelmet(null);
    45. inv.setChestplate(null);
    46. inv.setLeggings(null);
    47. inv.setBoots(null);
    48.  
    49. }
    50. }
     
  2. Offline

    Derpiee

    Make an ArrayList with the players name
    ArrayList<String> peopleWithKit = new ArrayList<String>();

    And when they type the command
    Add them to array
    peopleWithKit.add(player.getName());

    When they die, remove them
    peopleWithKit.remove(player.getName());

    Add a check to see if they are in the array before giving a kit.
     
  3. Offline

    JD_Guy17

    Verify_x When they have a kit add them to a arraylist and when they die remove them. And at the top of the command check if the arraylist contains the player
     
  4. Offline

    Verify_x

    JD_Guy17
    How would I remove it when they died? would an Event Handler work?
     
  5. Offline

    MooshViolet

    Verify_x Exactly how you would do it.
    Code:
    public ArrayList<String> usedkit = new ArrayList<String>();
    @EventHandler
    public void onPlayerDeath(PlayerDeathEvent event){
    Player p = event.getEntity();
    this.usedkit.remove(player.getName());
       
    }
     
  6. Offline

    GeorgeeeHD

    Code:
    public ArrayList<String> usedkit = new ArrayList<String>();
    @EventHandler
    public void onPlayerDeath(PlayerDeathEvent event){
    Player p = event.getEntity();
    if(this.usedKit.contains(player.getName()) {
        this.usedkit.remove(player.getName());
    }
         
    }
    that would get rid of an error if they die before they pick a kit
     
  7. Offline

    GaaTavares

    Actually if you attempt to remove something of the arraylist, and this something doesn't contains, nothing happens. It wouldn't throw an error (at least it never happened for me).
     
  8. Offline

    Verify_x

    MooshViolet
    I've tried this and still not working.
    Code:java
    1. package ckits.kits;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.Material;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandExecutor;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.enchantments.Enchantment;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.event.EventHandler;
    13. import org.bukkit.event.Listener;
    14. import org.bukkit.event.entity.PlayerDeathEvent;
    15. import org.bukkit.inventory.ItemStack;
    16. import org.bukkit.inventory.PlayerInventory;
    17. import org.bukkit.potion.PotionEffect;
    18. import org.bukkit.potion.PotionEffectType;
    19.  
    20. public class PVP implements CommandExecutor, Listener{
    21. public ArrayList<String> usedkit = new ArrayList<String>();
    22.  
    23. @EventHandler
    24. public void onPlayerDeath(PlayerDeathEvent event){
    25. Player p = event.getEntity();
    26. this.usedkit.remove(p.getName());
    27. }
    28.  
    29.  
    30. @Override
    31. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    32. if(!(sender instanceof Player)) {
    33. return false;
    34. }
    35. Player p = (Player)sender;
    36. if(commandLabel.equalsIgnoreCase("pvp")) {
    37. if(!usedkit.contains(p.getName())){
    38.  
    39. }
    40.  
    41.  
    42. p.sendMessage("§bMcChillz§7§l > §6Taken Kit PVP!");
    43. usedkit.add(p.getName());
    44. clearInventory(p.getInventory());
    45. p.getInventory().setHelmet(new ItemStack(Material.IRON_HELMET,1));
    46. p.getInventory().setChestplate(new ItemStack(Material.IRON_CHESTPLATE,1));
    47. p.getInventory().setLeggings(new ItemStack(Material.IRON_LEGGINGS,1));
    48. p.getInventory().setBoots(new ItemStack(Material.IRON_BOOTS,1));
    49.  
    50.  
    51. ItemStack dsword = new ItemStack(Material.DIAMOND_SWORD);
    52. dsword.addEnchantment(Enchantment.DAMAGE_ALL, 1);
    53. p.getInventory().addItem(dsword);
    54. p.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 1000000, 1));
    55. for (int i = 0; i < 35; i++)
    56. p.getInventory().addItem(new ItemStack[] { new ItemStack(Material.MUSHROOM_SOUP) });
    57.  
    58. }
    59. return true;
    60.  
    61. }
    62.  
    63.  
    64. public void clearInventory(PlayerInventory inv) {
    65. inv.clear();
    66. inv.setHelmet(null);
    67. inv.setChestplate(null);
    68. inv.setLeggings(null);
    69. inv.setBoots(null);
    70.  
    71. }
    72. }
     
  9. Offline

    jusjus112

    Verify_x
    Clear the inventory when they died!
    Code:java
    1. @EventHandler
    2. public void onPlayerDeath(PlayerDeathEvent event){
    3. Player p = (Player) event.getEntity();
    4. if (p instanceof Player) {
    5. this.usedkit.remove(p.getName());
    6. clearInventory(p.getInventory());
    7. }
    8. }
     
  10. Offline

    Rocoty

    GaaTavares you are right. It wouldn't throw an error. It would return false rather than true
     
  11. Offline

    MooshViolet

    Verify_x You have to put
    Code:
    return true;
    Under the statement where you check if they are not in usedkit. (Line 38).

    GeorgeeeHD
    I posted the exact same code above you lol

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

    Verify_x

    MooshViolet
    Ok so I did it. Everything works except removing me from the arraylist on my death.
    Code:java
    1. package ckits.kits;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.Material;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandExecutor;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.enchantments.Enchantment;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.event.EventHandler;
    13. import org.bukkit.event.entity.PlayerDeathEvent;
    14. import org.bukkit.inventory.ItemStack;
    15. import org.bukkit.inventory.PlayerInventory;
    16. import org.bukkit.potion.PotionEffect;
    17. import org.bukkit.potion.PotionEffectType;
    18.  
    19. public class PVP implements CommandExecutor{
    20. ArrayList<String> kits = new ArrayList<String>();
    21.  
    22. @EventHandler
    23. public void onDie(PlayerDeathEvent e) {
    24. Player p = e.getEntity();
    25. e.setDeathMessage(null);
    26. kits.remove(p.getName());
    27. }
    28.  
    29. @Override
    30. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    31. if(!(sender instanceof Player)) {
    32. return false;
    33. }
    34. Player p = (Player)sender;
    35. if(commandLabel.equalsIgnoreCase("pvp")) {
    36. if (!this.kits.contains(p.getName())) {
    37.  
    38. p.sendMessage("§bMcChillz§7§l > §6Taken Kit PVP!");
    39. this.kits.add(p.getName());
    40. clearInventory(p.getInventory());
    41. p.getInventory().setHelmet(new ItemStack(Material.IRON_HELMET,1));
    42. p.getInventory().setChestplate(new ItemStack(Material.IRON_CHESTPLATE,1));
    43. p.getInventory().setLeggings(new ItemStack(Material.IRON_LEGGINGS,1));
    44. p.getInventory().setBoots(new ItemStack(Material.IRON_BOOTS,1));
    45.  
    46.  
    47. ItemStack dsword = new ItemStack(Material.DIAMOND_SWORD);
    48. dsword.addEnchantment(Enchantment.DAMAGE_ALL, 1);
    49. p.getInventory().addItem(dsword);
    50. p.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 1000000, 1));
    51. for (int i = 0; i < 35; i++)
    52. p.getInventory().addItem(new ItemStack[] { new ItemStack(Material.MUSHROOM_SOUP) });
    53. }
    54. else {
    55. p.sendMessage(ChatColor.RED + "Only one kit allowed a life.");
    56. }
    57.  
    58. }
    59. return true;
    60.  
    61. }
    62.  
    63.  
    64. public void clearInventory(PlayerInventory inv) {
    65. inv.clear();
    66. inv.setHelmet(null);
    67. inv.setChestplate(null);
    68. inv.setLeggings(null);
    69. inv.setBoots(null);
    70.  
    71. }
    72. }
     
  13. Offline

    DrTURTLE2

    Verify_x
    Most likely not registering your events.
     
  14. Offline

    xTigerRebornx

    Verify_x The class that the method is in doesn't implement Listener (that, and you probably aren't registering your Events)
     
    DrTURTLE2 likes this.
  15. Offline

    DrTURTLE2


    Nice catch.
     
  16. Offline

    Verify_x

    DrTURTLE2
    xTigerRebornx
    Still not working, I've implemented it as listener and CommandExecutor and registered it, still no luck.
     
  17. Offline

    DrTURTLE2

    Verify_x
    Updated code with pvp class and main class?
     
  18. Offline

    Verify_x

    DrTURTLE2
    Code:java
    1. package ckits.kits;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.Material;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandExecutor;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.enchantments.Enchantment;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.event.EventHandler;
    13. import org.bukkit.event.Listener;
    14. import org.bukkit.event.entity.PlayerDeathEvent;
    15. import org.bukkit.inventory.ItemStack;
    16. import org.bukkit.inventory.PlayerInventory;
    17. import org.bukkit.potion.PotionEffect;
    18. import org.bukkit.potion.PotionEffectType;
    19.  
    20. public class PVP implements CommandExecutor, Listener{
    21. ArrayList<String> kits = new ArrayList<String>();
    22.  
    23. @EventHandler
    24. public void onDie(PlayerDeathEvent e) {
    25. Player p = e.getEntity();
    26. e.setDeathMessage(null);
    27. kits.remove(p.getName());
    28. }
    29.  
    30. @Override
    31. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    32. if(!(sender instanceof Player)) {
    33. return false;
    34. }
    35. Player p = (Player)sender;
    36. if(commandLabel.equalsIgnoreCase("pvp")) {
    37. if (!this.kits.contains(p.getName())) {
    38.  
    39. p.sendMessage("§bMcChillz§7§l > §6Taken Kit PVP!");
    40. this.kits.add(p.getName());
    41. clearInventory(p.getInventory());
    42. p.getInventory().setHelmet(new ItemStack(Material.IRON_HELMET,1));
    43. p.getInventory().setChestplate(new ItemStack(Material.IRON_CHESTPLATE,1));
    44. p.getInventory().setLeggings(new ItemStack(Material.IRON_LEGGINGS,1));
    45. p.getInventory().setBoots(new ItemStack(Material.IRON_BOOTS,1));
    46.  
    47.  
    48. ItemStack dsword = new ItemStack(Material.DIAMOND_SWORD);
    49. dsword.addEnchantment(Enchantment.DAMAGE_ALL, 1);
    50. p.getInventory().addItem(dsword);
    51. p.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 1000000, 1));
    52. for (int i = 0; i < 35; i++)
    53. p.getInventory().addItem(new ItemStack[] { new ItemStack(Material.MUSHROOM_SOUP) });
    54. }
    55. else {
    56. p.sendMessage(ChatColor.RED + "Only one kit allowed a life.");
    57. }
    58.  
    59. }
    60. return true;
    61.  
    62. }
    63.  
    64.  
    65. public void clearInventory(PlayerInventory inv) {
    66. inv.clear();
    67. inv.setHelmet(null);
    68. inv.setChestplate(null);
    69. inv.setLeggings(null);
    70. inv.setBoots(null);
    71.  
    72. }
    73. }




    Code:java
    1. package ckits.main;
    2.  
    3. import org.bukkit.craftbukkit.Main;
    4. import org.bukkit.plugin.PluginManager;
    5. import org.bukkit.plugin.java.JavaPlugin;
    6.  
    7. import ckits.cmd.AdminChatCommand;
    8. import ckits.cmd.AdminCommands;
    9. import ckits.cmd.BroadcastCommand;
    10. import ckits.cmd.ClearChatCommand;
    11. import ckits.cmd.ClearInvCommand;
    12. import ckits.cmd.FlyCommand;
    13. import ckits.cmd.HealCommand;
    14. import ckits.cmd.InfoCommand;
    15. import ckits.cmd.KickBanCommands;
    16. import ckits.cmd.RulesCommand;
    17. import ckits.cmd.SuicideCommand;
    18. import ckits.cmd.TpCommand;
    19. import ckits.kits.PVP;
    20. import ckits.list.AdminChatListener;
    21. import ckits.list.DropListener;
    22. import ckits.list.InventoryListener;
    23. import ckits.list.KickOnJoinListener;
    24. import ckits.list.PlayerListener;
    25. import ckits.list.RefillListener;
    26.  
    27. public class cKitsMain extends JavaPlugin{
    28. public static cKitsMain inst;
    29. public static Main plugin;
    30.  
    31. public void onEnable(){
    32. PluginManager pm = getServer().getPluginManager();
    33. inst = this;
    34.  
    35. // LISTENERS
    36. pm.registerEvents(new InventoryListener(), this);
    37. pm.registerEvents(new RefillListener(), this);
    38. pm.registerEvents(new PlayerListener(), this);
    39. pm.registerEvents(new DropListener(), this);
    40. pm.registerEvents(new AdminChatListener(), this);
    41. pm.registerEvents(new KickOnJoinListener(), this);
    42.  
    43.  
    44. // ADMIN COMMANDS
    45. getCommand("fly").setExecutor(new FlyCommand());
    46. getCommand("admin").setExecutor(new AdminCommands());
    47. getCommand("vis").setExecutor(new AdminCommands());
    48. getCommand("invis").setExecutor(new AdminCommands());
    49. getCommand("broadcast").setExecutor(new BroadcastCommand());
    50. getCommand("adminchat").setExecutor(new AdminChatCommand());
    51. getCommand("clearinv").setExecutor(new ClearInvCommand());
    52. getCommand("cc").setExecutor(new ClearChatCommand());
    53. getCommand("ban").setExecutor(new KickBanCommands());
    54. getCommand("unban").setExecutor(new KickBanCommands());
    55. getCommand("kick").setExecutor(new KickBanCommands());
    56. getCommand("tp").setExecutor(new TpCommand());
    57. getCommand("tphere").setExecutor(new TpCommand());
    58. getCommand("s").setExecutor(new TpCommand());
    59. getCommand("info").setExecutor(new InfoCommand());
    60. getCommand("checkban").setExecutor(new KickBanCommands());
    61.  
    62.  
    63.  
    64.  
    65. //KIT LISTENERS
    66. pm.registerEvents(new PVP(), this);
    67.  
    68.  
    69. // KITS
    70. getCommand("pvp").setExecutor(new PVP());
    71.  
    72. System.out.println("cKits Enabled!");
    73. }
    74.  
    75. public void onDisable(){
    76. System.out.println("cKits Disabled!");
    77. }
    78.  
    79.  
    80.  
    81. }
    82.  
     
  19. Offline

    DrTURTLE2

    Verify_x
    Not sure if this is the problem, but try:
    Code:java
    1. this.kits.remove(p.getName());

    instead of this:
    Code:java
    1. kits.remove(p.getName());
     
  20. Offline

    Verify_x

Thread Status:
Not open for further replies.

Share This Page