Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.23 KB | None | 0 0
  1. package fr.creepysoul.luckypackages.animation;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7.  
  8. import org.bukkit.ChatColor;
  9. import org.bukkit.Effect;
  10. import org.bukkit.Location;
  11. import org.bukkit.block.Block;
  12. import org.bukkit.block.Chest;
  13. import org.bukkit.craftbukkit.v1_8_R3.CraftWorld;
  14. import org.bukkit.entity.Item;
  15. import org.bukkit.inventory.ItemStack;
  16. import org.bukkit.scheduler.BukkitRunnable;
  17. import org.bukkit.util.Vector;
  18.  
  19. import com.gmail.filoghost.holographicdisplays.api.Hologram;
  20. import com.gmail.filoghost.holographicdisplays.api.HologramsAPI;
  21. import com.gmail.filoghost.holographicdisplays.api.line.TextLine;
  22.  
  23. import fr.creepysoul.luckypackages.utils.MathUtils;
  24. import me.chubbyduck1.luckypackages.Core;
  25. import me.chubbyduck1.luckypackages.files.AnimationFile;
  26. import me.chubbyduck1.luckypackages.files.CratesFile;
  27. import me.chubbyduck1.luckypackages.utils.CrateUtils;
  28. import net.minecraft.server.v1_8_R3.BlockPosition;
  29. import net.minecraft.server.v1_8_R3.TileEntityChest;
  30. import net.minecraft.server.v1_8_R3.World;
  31.  
  32. public class Animation {
  33.  
  34. public static Item item = null;
  35.  
  36. public Animation(Location location, String crate) {
  37. List<ItemStack> rewards = getRewards(crate);
  38. Location loc = MathUtils.getCenter(location.clone());
  39. loc.add(0.0, 2.3, 0.0);
  40.  
  41. Hologram hologram = summonHologram(crate, loc);
  42.  
  43. new BukkitRunnable() {
  44.  
  45. boolean error = false;
  46. int step = 0;
  47. int rewardCount = 0;
  48. Item item = null;
  49.  
  50. @Override
  51. public void run() {
  52. if (error) {
  53. if (item != null) {
  54. item.remove();
  55. }
  56. cancel();
  57. } else {
  58. if (step == 0) {
  59. Block chest = location.getBlock();
  60.  
  61. if (chest.getState() instanceof Chest) {
  62. playChestAction((Chest) chest.getState(), true);
  63. } else {
  64. error = true;
  65. }
  66.  
  67. step++;
  68. } else if (step == 1) {
  69. Location loc = MathUtils.getCenter(location.clone());
  70. loc.add(0.0, 1.0, 0.0);
  71.  
  72. loc.setDirection(new Vector(0.0, 10.0, 0.0));
  73.  
  74. loc.getWorld().playEffect(loc, Effect.valueOf(AnimationFile.getConfig().getString("particles")), 0);
  75.  
  76. item = summonItem(loc, rewards.get(rewardCount));
  77. item.setVelocity(new Vector(0.0, 0.2, 0.0));
  78. item.setPickupDelay(Integer.MAX_VALUE);
  79.  
  80. String itemName = null;
  81.  
  82. for (String crates : CratesFile.getConfig().getConfigurationSection("Crates").getKeys(false)) {
  83. if (crates.equalsIgnoreCase(crate)) {
  84. itemName = ChatColor.translateAlternateColorCodes('&', CratesFile.getConfig().getString("Crates." + crates + ".Rewards." + String.valueOf(rewardCount + 1) + ".Name"));
  85. }
  86. }
  87. setItemName(hologram, itemName);
  88.  
  89. rewardCount++;
  90.  
  91. if (rewardCount >= rewards.size()) {
  92. rewardCount = 0;
  93. }
  94.  
  95. step++;
  96. } else if (step == 6) {
  97. Block chest = location.getBlock();
  98.  
  99. if (chest.getState() instanceof Chest) {
  100. playChestAction((Chest) chest.getState(), false);
  101. } else {
  102. error = true;
  103. }
  104.  
  105. step++;
  106. } else if (step == 7) {
  107. if (item != null) {
  108. item.remove();
  109. }
  110. step++;
  111. } else if (step == 10) {
  112. step = 0;
  113. } else {
  114. step++;
  115. }
  116.  
  117. if (item != null) {
  118. Animation.item = item;
  119. }
  120. }
  121. }
  122. }.runTaskTimer(Core.core, 5, 5);
  123. }
  124.  
  125. public static void playChestAction(Chest chest, boolean open) {
  126. Location location = chest.getLocation();
  127. World world = ((CraftWorld) location.getWorld()).getHandle();
  128. BlockPosition position = new BlockPosition(location.getX(), location.getY(), location.getZ());
  129. TileEntityChest tileChest = (TileEntityChest) world.getTileEntity(position);
  130. world.playBlockAction(position, tileChest.w(), 1, open ? 1 : 0);
  131. }
  132.  
  133. public static Hologram summonHologram(String crate, Location location) {
  134. Hologram hologram = HologramsAPI.createHologram(Core.core, location);
  135.  
  136. hologram.appendTextLine(ChatColor.translateAlternateColorCodes('&', AnimationFile.getConfig().getString("hologram.line1").replace("%crateName%", crate)));
  137.  
  138. return hologram;
  139. }
  140.  
  141. public static void setItemName(Hologram hologram, String itemName) {
  142. try {
  143. hologram.removeLine(1);
  144. } catch (Exception e) {
  145. }
  146.  
  147. hologram.appendTextLine(itemName);
  148. }
  149.  
  150. public static Item summonItem(Location location, ItemStack itemStack) {
  151. Item item = location.getWorld().dropItem(location, itemStack);
  152.  
  153. return item;
  154. }
  155.  
  156. private List<ItemStack> getRewards(String Crate) {
  157. List<ItemStack> crateRewards = new ArrayList<ItemStack>();
  158.  
  159. for (String crate : CratesFile.getConfig().getConfigurationSection("Crates.").getKeys(false)) {
  160. if (crate.equalsIgnoreCase(Crate)) {
  161. for (String item : CratesFile.getConfig().getConfigurationSection("Crates." + crate + ".Rewards").getKeys(false)) {
  162. String[] material = CratesFile.getConfig().getString("Crates." + crate + ".Rewards." + item + ".Material").split(":");
  163. if (material[0].equalsIgnoreCase("SKULL")) {
  164. crateRewards.add(CrateUtils.getCrateRewardSkull(crate, item));
  165. } else {
  166. crateRewards.add(CrateUtils.getCrateReward(crate, item));
  167. }
  168. }
  169. }
  170. }
  171.  
  172. return crateRewards;
  173. }
  174.  
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement