Posted By

NonameSL on 07/09/14


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

Jay-Jay


MyCharacter.java


 / Published in: Java
 

MyCharacter.java class

  1. package me.NonameSL.CharacterLibrary;
  2.  
  3.  
  4. import java.util.HashMap;
  5. import java.util.HashSet;
  6. import java.util.Map.Entry;
  7. import java.util.Set;
  8. import org.bukkit.Bukkit;
  9. import org.bukkit.entity.Player;
  10.  
  11. public class MyCharacter
  12. {
  13. private String name;
  14. private int maxhealth;
  15. public static HashMap<MyCharacter, Set<Player>> playersWithCharacters = new HashMap<>();
  16.  
  17. private HashMap<ActionTrigger, CharacterAction> skills = new HashMap<>();
  18. public MyCharacter(String name, int maxhealth){
  19. this(name, maxhealth, new HashMap<ActionTrigger, CharacterAction>());
  20. }
  21. public MyCharacter(int maxhealth) {
  22. this("", maxhealth);
  23. }
  24. public MyCharacter(int maxhealth, HashMap<ActionTrigger, CharacterAction> skills) {
  25. this("", maxhealth, skills);
  26. }
  27. public MyCharacter(String name, int maxhealth, HashMap<ActionTrigger, CharacterAction> skills){
  28. this.name=name;
  29. this.maxhealth = maxhealth;
  30. this.skills = skills;
  31. if (!playersWithCharacters.containsKey(this))
  32. playersWithCharacters.put(this, new HashSet<Player>());
  33. }
  34.  
  35. public String getName()
  36. {
  37. return this.name;
  38. }
  39. public void setName(String name) {
  40. this.name = name;
  41. }
  42. /**
  43.   *
  44.   * @return a clone of the skills map
  45.   */
  46. public HashMap<ActionTrigger, CharacterAction> getSkills()
  47. {
  48. return new HashMap<ActionTrigger, CharacterAction>(this.skills);
  49. }
  50. public void setSkill(ActionTrigger at, CharacterAction ca) {
  51. if (this.skills.containsKey(at))
  52. removeSkill(at);
  53. addSkill(ca, at);
  54. }
  55.  
  56. public void addSkill(CharacterAction ca, ActionTrigger at) {
  57. if (this.skills.containsKey(at)) {
  58. Bukkit.getServer().getConsoleSender().sendMessage("§2[§1CharacterLibrary§r§2]§4§uERROR:§r§4 Tried to add skill with " + at.toString().replace("_", " ").toLowerCase() + "tigger but character alraedy has one!");
  59. return;
  60. }
  61. this.skills.put(at, ca);
  62. }
  63. public void removeSkill(ActionTrigger at) {
  64. if (this.skills.containsKey(at)) this.skills.remove(at);
  65. }
  66.  
  67. public boolean removeSkillsFor(ActionTrigger at)
  68. {
  69. boolean breturn = false;
  70. for (Entry<?, ?> entry : this.skills.entrySet()) {
  71. if (at.equals(entry.getValue())) {
  72. this.skills.remove(entry.getKey());
  73. breturn = true;
  74. }
  75. }
  76. return breturn;
  77. }
  78.  
  79. public int getMaxHealth()
  80. {
  81. return this.maxhealth;
  82. }
  83. public void setMaxHealth(int maxhealth) {
  84. this.maxhealth = maxhealth;
  85. }
  86.  
  87. public static enum ActionTrigger
  88. {
  89. LEFT_CLICK_BLOCK, RIGHT_CLICK_BLOCK,
  90. LEFT_CLICK_AIR, RIGHT_CLICK_AIR,
  91. SPRINT, SPLASH_POTION_EFFECT, CROUCH,
  92. HIT_BY_ENTITY, HIT_ENTITY, ITEM_DROP, EGG_THROW, DEATH;
  93. }
  94. }

Report this snippet  

You need to login to post a comment.