Help with Hashmaps

Discussion in 'Plugin Development' started by Violence010, Mar 12, 2014.

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

    Violence010

    Hello, i am making a spells plugin, and i'm saving the players current spell to a hashmap, and i was wondering if anyone could help me save the players current spell to the hashmap, i tried making one, but it didn't work

    Here's my main class
    Code:java
    1. package me.XGravity_GuyX.main;
    2.  
    3. import java.util.HashMap;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Material;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.block.Action;
    12. import org.bukkit.event.player.PlayerInteractEvent;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14.  
    15. public class Main extends JavaPlugin implements Listener{
    16.  
    17.  
    18. public void onEnable(){
    19.  
    20. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    21.  
    22. }
    23. static int spellCount = Spell.getCount();
    24.  
    25. static int currentSpell = 1;
    26.  
    27. public static void main(String[] args) {
    28.  
    29. for (int i = 0; i < 10; i++) {
    30. switchSpell();
    31. }
    32. }
    33.  
    34. public static void switchSpell(Player p) {
    35. currentSpell = currentSpell == spellCount ? 1 : currentSpell + 1;
    36. Spell s = Spell.getSpell(currentSpell);
    37. if (s == null)
    38. return;
    39. System.out.println("[DH-Spells] Switched to: " + s.getName() + "!");
    40. HashMap<Player, ?> map = new HashMap<Player, ?>();
    41. map.get
    42. }
    43.  
    44. @EventHandler
    45. public void onPlayerInteract(PlayerInteractEvent e) {
    46. Player p = (Player) e.getPlayer();
    47. if (e.getAction() == Action.RIGHT_CLICK_AIR) {
    48. if (p.getItemInHand() != null) {
    49. if (p.getItemInHand().getType() == Material.STICK) {
    50.  
    51. switchspell(p);
    52.  
    53. }
    54.  
    55.  
    56.  
    57. }
    58. }
    59. }
    60. }
    61.  



    and here is my spell class

    Code:java
    1. package me.XGravity_GuyX.main;
    2.  
    3. import org.bukkit.ChatColor;
    4.  
    5. public enum Spell {
    6.  
    7. AVADAKEDAVRA(1, ChatColor.ITALIC + "Avada Kedavra"), BOMBARDA(2, ChatColor.ITALIC + "Bombarda"), BOMBARDAMAXIMA(3, ChatColor.ITALIC + "Bombarda Maxima"), CONFRINGO(4, ChatColor.ITALIC + "Confringo"), CONJUNCTIVITIS(5, ChatColor.ITALIC + "Conjunctivitis"), DIPULSO(6, ChatColor.ITALIC + "Dipulso"), FLIPENDO(7, ChatColor.ITALIC + "Flipendo");
    8. int id;
    9. String name;
    10. Spell(int id, String name) {this.id = id; this.name = name;}
    11. public int getId() {return id;}
    12. public static int getCount() {return Spell.values().length;}
    13. public String getName() {return name;}
    14. public static Spell getSpell(int id) {
    15. for (Spell s : Spell.values()) {
    16. if (s.getId()==id) {
    17. return s;
    18. }
    19. }
    20. return null;
    21.  
    22. }
    23.  
    24. }
    25.  



    if anyone could help me out here, it would be much appreciated
     
  2. Offline

    Amgis

    Violence010

    In the case that you have some piece of information that you need to attach to a player, it's best to make your own custom player class that contains a org.bukkit.entity.Player and his/her custom properties. This is especially important when you plan to expand your plugin and add more data to your custom player class. It will become very burdensome to have to create a Map for every property of each player you add to your program.

    In which case:
    Code:java
    1. public class CustomPlayer{
    2.  
    3. private final Player player;
    4.  
    5. private Spell currentSpell;
    6.  
    7. public CustomPlayer(Player player) {
    8.  
    9. this.player = player;
    10.  
    11. this.currentSpell = null;
    12. }
    13.  
    14. public boolean hasSpell() {
    15.  
    16. return this.currentSpell != null;
    17. }
    18.  
    19. public Spell getCurrentSpell() {
    20.  
    21. return this.currentSpell;
    22. }
    23.  
    24. public void setSpell(Spell spell) {
    25.  
    26. this.currentSpell = spell;
    27. }
    28.  
    29. public Player getPlayer() {
    30.  
    31. return this.player;
    32. }
    33.  
    34. @Override
    35. public boolean equals(Object object) {
    36.  
    37. if(object instanceof CustomPlayer) {
    38.  
    39. return ((CustomPlayer) object).getPlayer().equals(this.getPlayer());
    40. }
    41.  
    42. return false;
    43. }
    44. }
     
  3. Offline

    GameplayJDK

    Violence010

    Make a public HashMap and use put(key, value) to add an item, get(key) to get the value with key as key.
    Also you should store the players name instead of the player itself to save memory.

    Code:java
    1. package me.XGravity_GuyX.main;
    2.  
    3. import java.util.HashMap;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Material;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.block.Action;
    12. import org.bukkit.event.player.PlayerInteractEvent;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14.  
    15. public class Main extends JavaPlugin implements Listener{
    16.  
    17.  
    18. public void onEnable(){
    19.  
    20. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    21.  
    22. }
    23. static int spellCount = Spell.getCount();
    24.  
    25. static int currentSpell = 1;
    26.  
    27. public static void main(String[] args) {
    28.  
    29. for (int i = 0; i < 10; i++) {
    30. switchSpell();
    31. }
    32. }
    33. public HashMap<String, Objekt> map = new HashMap<String, Object>();
    34. public static void switchSpell(Player p) {
    35. currentSpell = currentSpell == spellCount ? 1 : currentSpell + 1;
    36. Spell s = Spell.getSpell(currentSpell);
    37. if (s == null)
    38. return;
    39. System.out.println("[DH-Spells] Switched to: " + s.getName() + "!");
    40.  
    41. map.getValue(keygen);
    42. map.put(p.getName(), Object);
    43. }
    44.  
    45. @EventHandler
    46. public void onPlayerInteract(PlayerInteractEvent e) {
    47. Player p = (Player) e.getPlayer();
    48. if (e.getAction() == Action.RIGHT_CLICK_AIR) {
    49. if (p.getItemInHand() != null) {
    50. if (p.getItemInHand().getType() == Material.STICK) {
    51.  
    52. switchspell(p);
    53.  
    54. }
    55.  
    56.  
    57.  
    58. }
    59. }
    60. }
    61. }
    62.  
     
Thread Status:
Not open for further replies.

Share This Page