Guest User

Untitled

a guest
Aug 14th, 2014
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.48 KB | None | 0 0
  1. package loot;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Random;
  6.  
  7. import org.bukkit.Bukkit;
  8. import org.bukkit.ChatColor;
  9. import org.bukkit.Location;
  10. import org.bukkit.Material;
  11. import org.bukkit.World;
  12. import org.bukkit.block.Block;
  13. import org.bukkit.block.BlockFace;
  14. import org.bukkit.block.Chest;
  15. import org.bukkit.command.Command;
  16. import org.bukkit.command.CommandSender;
  17. import org.bukkit.entity.Player;
  18. import org.bukkit.event.EventHandler;
  19. import org.bukkit.event.Listener;
  20. import org.bukkit.event.inventory.InventoryOpenEvent;
  21. import org.bukkit.inventory.ItemStack;
  22. import org.bukkit.plugin.java.JavaPlugin;
  23. import org.bukkit.scheduler.BukkitRunnable;
  24.  
  25. public final class LootChests
  26. extends JavaPlugin
  27. implements Listener
  28. {
  29. private static final Random random = new Random();
  30. private static final String prefix = "[" + ChatColor.LIGHT_PURPLE + "LootChest" + ChatColor.RESET + "] ";
  31. private static LootChests instance;
  32. private World world;
  33. private int minX;
  34. private int minZ;
  35. private int maxX;
  36. private int maxZ;
  37. private int maxUnique;
  38. private List<ItemStack> contents;
  39. private boolean donePrevious = true;
  40.  
  41. public static LootChests get()
  42. {
  43. return instance;
  44. }
  45.  
  46. @SuppressWarnings({ "unchecked", "rawtypes" })
  47. public void onEnable()
  48. {
  49. instance = this;
  50. saveDefaultConfig();
  51. getServer().getPluginManager().registerEvents(this, this);
  52. getServer().getScheduler().cancelTasks(this);
  53.  
  54. this.world = getServer().getWorld(getConfig().getString("world"));
  55. if (this.world != null )
  56. {
  57. this.minX = getConfig().getInt("min.x");
  58. this.minZ = getConfig().getInt("min.z");
  59. this.maxX = getConfig().getInt("max.x");
  60. this.maxZ = getConfig().getInt("max.z");
  61. this.maxUnique = getConfig().getInt("max-unique-items");
  62. this.contents = new ArrayList();
  63. for (String s : getConfig().getConfigurationSection("contents").getKeys(false))
  64. {
  65. Material mat = Material.matchMaterial(s);
  66. if (mat != null)
  67. {
  68. int amount = getConfig().getInt("contents." + s + ".amount", 1);
  69. int data = getConfig().getInt("contents." + s + ".data", 0);
  70. ItemStack stack = new ItemStack(mat, amount, (short)data);
  71. this.contents.add(stack);
  72. }
  73. else
  74. {
  75. getLogger().severe("Could not get Material from " + s);
  76. }
  77. }
  78. new BukkitRunnable()
  79. {
  80. public void run()
  81. {
  82. LootChests.this.spawnChest();
  83. }
  84. }.runTaskTimer(this, 0L, getConfig().getInt("delay") * 20);
  85. }
  86. else
  87. {
  88. getLogger().severe(getConfig().getString("world") + " is not a world!");
  89. }
  90. }
  91.  
  92. @EventHandler
  93. public void onInventoryOpen(InventoryOpenEvent e)
  94. {
  95. if (e.getInventory().getName().equalsIgnoreCase("LootChest"))
  96. {
  97. Player p = (Player)e.getPlayer();
  98. String message = getConfig().getString("chest-found");
  99. ChatColor.translateAlternateColorCodes('&', message);
  100. message = message.replaceAll("%p", p.getName());
  101. Bukkit.broadcastMessage(message);
  102. }
  103. }
  104.  
  105. public boolean onCommand(CommandSender cs, Command c, String l, String[] args)
  106. {
  107. if (args.length == 0) {
  108. cs.sendMessage(prefix);
  109. } else if (args.length == 1) {
  110. if (args[0].equalsIgnoreCase("reload"))
  111. {
  112. reloadConfig();
  113. getServer().getPluginManager().disablePlugin(this);
  114. getServer().getPluginManager().enablePlugin(this);
  115. cs.sendMessage(prefix + "Plugin reloaded!");
  116. }
  117. else if (args[0].equalsIgnoreCase("spawn"))
  118. {
  119. spawnChest();
  120. cs.sendMessage(prefix + "Chest spawned!");
  121. }
  122. }
  123. return true;
  124. }
  125.  
  126. @SuppressWarnings("unchecked")
  127. public void spawnChest()
  128. {
  129. if (this.donePrevious)
  130. {
  131. this.donePrevious = false;
  132. Block block;
  133. do
  134. {
  135. int x = random.nextInt(this.maxX - this.minX) + this.minX;
  136. int z = random.nextInt(this.maxZ - this.minZ) + this.minZ;
  137. block = this.world.getHighestBlockAt(x, z);
  138. } while (!block.getRelative(BlockFace.DOWN).getType().isSolid());
  139. block.setType(Material.CHEST);
  140. Location loc = block.getLocation();
  141. Chest chest = (Chest)block.getState();
  142. if (this.contents.size() == this.maxUnique)
  143. {
  144. for (int i = 0; i < this.contents.size(); i++) {
  145. chest.getInventory().setItem(i, ((ItemStack)this.contents.get(i)).clone());
  146. }
  147. }
  148. else
  149. {
  150. @SuppressWarnings("rawtypes")
  151. List<ItemStack> localContents = new ArrayList(this.contents);
  152. for (int i = 0; i < this.maxUnique; i++) {
  153. do
  154. {
  155. ItemStack stack = ((ItemStack)localContents.get(random.nextInt(localContents.size()))).clone();
  156. if (!chest.getInventory().contains(stack.getType()))
  157. {
  158. chest.getInventory().setItem(i, stack);
  159. localContents.remove(stack);
  160. }
  161. } while (chest.getInventory().getItem(i) == null);
  162. }
  163. }
  164. String message = getConfig().getString("chest-spawned");
  165. message = message.replace("%x", String.valueOf(loc.getBlockX())).replace("%y", String.valueOf(loc.getBlockY())).replace("%z", String.valueOf(loc.getBlockZ()));
  166. ChatColor.translateAlternateColorCodes('&', message);
  167. Bukkit.broadcastMessage(message);
  168. this.donePrevious = true;
  169. }
  170. }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment