Advertisement
iSynx

ActionItem.java

Mar 21st, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. package com.desle.staffmode;
  2.  
  3. import com.desle.staffmode.actionitems.ItemRandomTeleport;
  4. import com.desle.staffmode.actionitems.ItemSpyInventory;
  5. import com.desle.staffmode.actionitems.ItemUnvanish;
  6. import com.desle.staffmode.actionitems.ItemVanish;
  7. import com.desle.staffmode.gui.ItemDataUtil;
  8. import java.lang.reflect.Constructor;
  9. import java.util.ArrayList;
  10. import java.util.Arrays;
  11. import java.util.List;
  12. import java.util.logging.Logger;
  13. import net.minecraft.server.v1_8_R1.NBTTagCompound;
  14. import org.bukkit.Bukkit;
  15. import org.bukkit.Material;
  16. import org.bukkit.Sound;
  17. import org.bukkit.entity.Player;
  18. import org.bukkit.inventory.ItemFactory;
  19. import org.bukkit.inventory.ItemStack;
  20. import org.bukkit.inventory.meta.ItemMeta;
  21.  
  22. public abstract class ActionItem
  23. {
  24. protected Material material;
  25. protected String name;
  26. protected String[] lore;
  27. protected ItemStack itemStack;
  28. protected byte data;
  29. protected ItemType type;
  30.  
  31. public ActionItem(Material material, byte data, String name, ItemType type, String... lore)
  32. {
  33. this.material = material;
  34. this.data = data;
  35. this.name = name;
  36. this.lore = lore;
  37. this.type = type;
  38. }
  39.  
  40. public ItemStack getItemStack() {
  41. if (itemStack != null) {
  42. return itemStack;
  43. }
  44. return constructItemStack();
  45. }
  46.  
  47. public abstract void onUse(Player paramPlayer);
  48.  
  49. public void playSuccessSound(Player player) {
  50. player.playSound(player.getLocation(), Sound.WOOD_CLICK, 1.0F, 1.0F);
  51. }
  52.  
  53. public void playErrorSound(Player player) {
  54. player.playSound(player.getLocation(), Sound.LAVA_POP, 1.0F, 1.0F);
  55. }
  56.  
  57. public ItemStack constructItemStack()
  58. {
  59. itemStack = new ItemStack(material, 1, (short)0, Byte.valueOf(data));
  60. ItemMeta itemMeta = Bukkit.getItemFactory().getItemMeta(material);
  61. itemMeta.setDisplayName(name);
  62. itemMeta.setLore(Arrays.asList(lore));
  63. itemStack.setItemMeta(itemMeta);
  64.  
  65. NBTTagCompound compound = ItemDataUtil.getData(itemStack);
  66. compound.setString("staffmode_item", type.name());
  67. itemStack = ItemDataUtil.setData(itemStack, compound);
  68.  
  69. return itemStack;
  70. }
  71.  
  72. public static enum ItemType {
  73. VANISH(ItemVanish.class),
  74. UNVANISH(ItemUnvanish.class),
  75. RANDOMTELEPORT(ItemRandomTeleport.class),
  76. SPYINVENTORY(ItemSpyInventory.class),
  77. FREEZE(com.desle.staffmode.actionitems.ItemFreeze.class);
  78.  
  79. private final Class<? extends ActionItem> itemClass;
  80.  
  81. public ActionItem getActionItem()
  82. {
  83. try {
  84. return (ActionItem)itemClass.getDeclaredConstructor(new Class[0]).newInstance(new Object[0]);
  85. } catch (Exception e) {
  86. e.printStackTrace();
  87. Bukkit.getLogger().warning("Failed to create " + name() + " as ActionItem."); }
  88. return null;
  89. }
  90.  
  91. public static List<ItemType> getAllFor(StaffModePlayer staffModePlayer)
  92. {
  93. List<ItemType> itemTypes = new ArrayList();
  94.  
  95. for (ItemType itemType : values()) {
  96. boolean status = true;
  97.  
  98. switch (itemType) {
  99. case FREEZE:
  100. status = false;
  101. break;
  102. }
  103.  
  104.  
  105.  
  106. if (status) {
  107. itemTypes.add(itemType);
  108. }
  109. }
  110. return itemTypes;
  111. }
  112.  
  113. public static ItemType get(ItemStack itemStack) {
  114. NBTTagCompound compound = ItemDataUtil.getData(itemStack);
  115.  
  116. return valueOf(compound.getString("staffmode_item"));
  117. }
  118.  
  119. private ItemType(Class<? extends ActionItem> itemClass) {
  120. this.itemClass = itemClass;
  121. }
  122. }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement