mmoHelper.java 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * This file is from mmoMinecraft (http://code.google.com/p/mmo-minecraft/).
  3. *
  4. * mmoMinecraft is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. package com.gmail.nossr50.spout;
  17. import java.util.ArrayList;
  18. import java.util.HashMap;
  19. import org.bukkit.Bukkit;
  20. import org.bukkit.ChatColor;
  21. import org.bukkit.World;
  22. import org.bukkit.entity.Entity;
  23. import org.bukkit.entity.LivingEntity;
  24. import org.bukkit.entity.Player;
  25. import org.bukkit.entity.Tameable;
  26. import org.bukkit.inventory.ItemStack;
  27. import org.bukkit.plugin.Plugin;
  28. import org.bukkit.entity.*;
  29. import org.getspout.spoutapi.gui.Container;
  30. import org.getspout.spoutapi.gui.GenericContainer;
  31. import org.getspout.spoutapi.gui.Widget;
  32. import org.getspout.spoutapi.gui.WidgetAnchor;
  33. import org.getspout.spoutapi.player.SpoutPlayer;
  34. import com.gmail.nossr50.Users;
  35. import com.gmail.nossr50.config.LoadProperties;
  36. import com.gmail.nossr50.party.Party;
  37. import com.gmail.nossr50.spout.util.GenericLivingEntity;
  38. public class mmoHelper
  39. {
  40. /**
  41. * A map of player containers, each container is their party bar
  42. */
  43. public static HashMap<Player, GenericContainer> containers = new HashMap<Player, GenericContainer>();
  44. /**
  45. * Get the percentage health of a Player.
  46. * @param player The Player we're interested in
  47. * @return The percentage of max health
  48. */
  49. public static int getHealth(Entity player) {
  50. if (player != null && player instanceof LivingEntity) {
  51. try {
  52. return Math.min(((LivingEntity) player).getHealth() * 5, 100);
  53. } catch (Exception e) {
  54. }
  55. }
  56. return 0;
  57. }
  58. /**
  59. * Get the colour of a LivingEntity target from a player's point of view.
  60. * @param player The player viewing the target
  61. * @param target The target to name
  62. * @return The name to use
  63. */
  64. public static String getColor(Player player, LivingEntity target) {
  65. if (target instanceof Player) {
  66. if (((Player) target).isOp()) {
  67. return ChatColor.GOLD.toString();
  68. }
  69. return ChatColor.YELLOW.toString();
  70. } else {
  71. if (target instanceof Monster) {
  72. if (player != null && player.equals(((Monster) target).getTarget())) {
  73. return ChatColor.RED.toString();
  74. } else {
  75. return ChatColor.YELLOW.toString();
  76. }
  77. } else if (target instanceof WaterMob) {
  78. return ChatColor.GREEN.toString();
  79. } else if (target instanceof Flying) {
  80. return ChatColor.YELLOW.toString();
  81. } else if (target instanceof Animals) {
  82. if (player != null && player.equals(((Animals) target).getTarget())) {
  83. return ChatColor.RED.toString();
  84. } else if (target instanceof Tameable) {
  85. Tameable pet = (Tameable) target;
  86. if (pet.isTamed()) {
  87. return ChatColor.GREEN.toString();
  88. } else {
  89. return ChatColor.YELLOW.toString();
  90. }
  91. } else {
  92. return ChatColor.GRAY.toString();
  93. }
  94. } else {
  95. return ChatColor.GRAY.toString();
  96. }
  97. }
  98. }
  99. /**
  100. * Get the percentage armour of a Player.
  101. * @param player The Player we're interested in
  102. * @return The percentage of max armour
  103. */
  104. public static int getArmor(Entity player) {
  105. if (player != null && player instanceof Player) {
  106. int armor = 0, max, multi[] = {15, 30, 40, 15};
  107. ItemStack inv[] = ((Player) player).getInventory().getArmorContents();
  108. for (int i = 0; i < inv.length; i++) {
  109. max = inv[i].getType().getMaxDurability();
  110. if (max >= 0) {
  111. armor += multi[i] * (max - inv[i].getDurability()) / max;
  112. }
  113. }
  114. return armor;
  115. }
  116. return 0;
  117. }
  118. public static String getSimpleName(LivingEntity target, boolean showOwner) {
  119. String name = "";
  120. if (target instanceof Player) {
  121. if (LoadProperties.showDisplayName) {
  122. name += ((Player) target).getName();
  123. } else {
  124. name += ((Player) target).getDisplayName();
  125. }
  126. } else if (target instanceof HumanEntity) {
  127. name += ((HumanEntity) target).getName();
  128. } else {
  129. if (target instanceof Tameable) {
  130. if (((Tameable) target).isTamed()) {
  131. if (showOwner && ((Tameable) target).getOwner() instanceof Player) {
  132. if (LoadProperties.showDisplayName) {
  133. name += ((Player) ((Tameable) target).getOwner()).getName() + "'s ";
  134. } else {
  135. name += ((Player) ((Tameable) target).getOwner()).getDisplayName() + "'s ";
  136. }
  137. } else {
  138. name += "Pet ";
  139. }
  140. }
  141. }
  142. if (target instanceof Chicken) {
  143. name += "Chicken";
  144. } else if (target instanceof Cow) {
  145. name += "Cow";
  146. } else if (target instanceof Creeper) {
  147. name += "Creeper";
  148. } else if (target instanceof Ghast) {
  149. name += "Ghast";
  150. } else if (target instanceof Giant) {
  151. name += "Giant";
  152. } else if (target instanceof Pig) {
  153. name += "Pig";
  154. } else if (target instanceof PigZombie) {
  155. name += "PigZombie";
  156. } else if (target instanceof Sheep) {
  157. name += "Sheep";
  158. } else if (target instanceof Slime) {
  159. name += "Slime";
  160. } else if (target instanceof Skeleton) {
  161. name += "Skeleton";
  162. } else if (target instanceof Spider) {
  163. name += "Spider";
  164. } else if (target instanceof Squid) {
  165. name += "Squid";
  166. } else if (target instanceof Wolf) {
  167. name += "Wolf";
  168. } else if (target instanceof Zombie) {
  169. name += "Zombie";
  170. } else if (target instanceof Monster) {
  171. name += "Monster";
  172. } else if (target instanceof Creature) {
  173. name += "Creature";
  174. } else {
  175. name += "Unknown";
  176. }
  177. }
  178. return name;
  179. }
  180. public static LivingEntity[] getPets(HumanEntity player) {
  181. ArrayList<LivingEntity> pets = new ArrayList<LivingEntity>();
  182. if (player != null && (!(player instanceof Player) || ((Player) player).isOnline())) {
  183. String name = player.getName();
  184. for (World world : Bukkit.getServer().getWorlds()) {
  185. for (LivingEntity entity : world.getLivingEntities()) {
  186. if (entity instanceof Tameable && ((Tameable) entity).isTamed() && ((Tameable) entity).getOwner() instanceof Player) {
  187. if (name.equals(((Player) ((Tameable) entity).getOwner()).getName())) {
  188. pets.add(entity);
  189. }
  190. }
  191. }
  192. }
  193. }
  194. LivingEntity[] list = new LivingEntity[pets.size()];
  195. pets.toArray(list);
  196. return list;
  197. }
  198. public static void update(Player player)
  199. {
  200. //boolean show_pets = true;
  201. Container container = containers.get(player);
  202. if (container != null)
  203. {
  204. int index = 0;
  205. Widget[] bars = container.getChildren();
  206. for (String name : Party.getInstance().getPartyMembersByName(player).meFirst(player.getName()))
  207. {
  208. GenericLivingEntity bar;
  209. if (index >= bars.length)
  210. {
  211. container.addChild(bar = new GenericLivingEntity());
  212. } else {
  213. bar = (GenericLivingEntity)bars[index];
  214. }
  215. bar.setEntity(name, Party.getInstance().isPartyLeader(name, Users.getProfile(Bukkit.getServer().getPlayer(name)).getParty()) ? ChatColor.GREEN + "@" : "");
  216. //bar.setTargets(show_pets ? getPets(Bukkit.getServer().getPlayer(name)) : null);
  217. index++;
  218. }
  219. while (index < bars.length) {
  220. container.removeChild(bars[index++]);
  221. }
  222. container.updateLayout();
  223. }
  224. }
  225. public static void initialize(SpoutPlayer sPlayer, Plugin plugin)
  226. {
  227. GenericContainer container = new GenericContainer();
  228. container.setAlign(WidgetAnchor.TOP_LEFT)
  229. .setAnchor(WidgetAnchor.TOP_LEFT)
  230. .setX(3)
  231. .setY(3)
  232. .setWidth(427)
  233. .setHeight(240)
  234. .setFixed(true);
  235. mmoHelper.containers.put(sPlayer, container);
  236. sPlayer.getMainScreen().attachWidget(plugin, container);
  237. }
  238. /**
  239. * Update all parties.
  240. */
  241. public static void updateAll() {
  242. for(Player x : Bukkit.getServer().getOnlinePlayers())
  243. {
  244. if(Users.getProfile(x).inParty())
  245. {
  246. update(x);
  247. }
  248. }
  249. }
  250. }