Advertisement
Greenadine

Portal.java

Sep 1st, 2017
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.76 KB | None | 0 0
  1. package me.greenadine.worldspawns.portals;
  2.  
  3. import com.sk89q.worldedit.bukkit.WorldEditPlugin;
  4. import com.sk89q.worldedit.bukkit.selections.Selection;
  5. import me.greenadine.worldspawns.Lang;
  6. import me.greenadine.worldspawns.Main;
  7. import me.greenadine.worldspawns.SettingsManager;
  8. import org.bukkit.Bukkit;
  9. import org.bukkit.ChatColor;
  10. import org.bukkit.Location;
  11. import org.bukkit.World;
  12. import org.bukkit.block.Block;
  13. import org.bukkit.configuration.file.FileConfiguration;
  14. import org.bukkit.configuration.file.YamlConfiguration;
  15. import org.bukkit.entity.Player;
  16.  
  17. import java.io.File;
  18. import java.io.IOException;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21.  
  22. public class Portal {
  23.  
  24. /*
  25. * WARNING: W.I.P.
  26. * TODO: Send informative messages.
  27. * TODO: Test methods.
  28. */
  29.  
  30. private Main main;
  31.  
  32. private File file;
  33.  
  34. private List<String> portalList = new ArrayList<String>();
  35. private List<Block> blocks = new ArrayList<Block>();
  36.  
  37. public boolean enabled;
  38. private String name;
  39. private World world;
  40. private String type;
  41. private String owner;
  42. private int blocksCount;
  43.  
  44. private Block mainBlock;
  45.  
  46. WorldEditPlugin worldEdit = (WorldEditPlugin) Bukkit.getServer().getPluginManager().getPlugin("WorldEdit");
  47.  
  48. private String prefix = ChatColor.translateAlternateColorCodes('&', Lang.PREFIX.toString());
  49.  
  50. SettingsManager settings;
  51.  
  52. public Portal(Main main, File file) {
  53. this.main = main;
  54. this.file = file;
  55. this.settings = SettingsManager.getInstance();
  56. }
  57.  
  58. // TODO: fix warning
  59. public void createNew(Player player, String name, String type) {
  60. enabled = main.getConfig().getBoolean("components.portals");
  61. if (type.equals("hub")) {
  62. if (enabled == true) {
  63. Selection selection = worldEdit.getSelection(player);
  64.  
  65. if (selection != null) {
  66. Location min = selection.getMinimumPoint();
  67. Location max = selection.getMaximumPoint();
  68.  
  69. List<Block> blocks = getPortalBlocks(min, max);
  70.  
  71. if (blocks.size() > 200) {
  72. player.sendMessage(
  73. prefix + ChatColor.translateAlternateColorCodes('&', Lang.PORTAL_SELECTION_TOO_BIG
  74. .toString().replaceAll("%blocks%", String.valueOf(blocks.size()))));
  75. return;
  76. } else {
  77. if (file != null) {
  78. if (portalList.contains(name)) {
  79. player.sendMessage(prefix + ChatColor.translateAlternateColorCodes('&',
  80. Lang.COMMAND_SETPORTAL_EXISTS.toString().replaceAll("%pluginname%", name)));
  81. return;
  82. }
  83. player.sendMessage(prefix + ChatColor.translateAlternateColorCodes('&', Lang.PORTAL_CREATING
  84. .toString().replaceAll("%name%", name).replaceAll("%type%", "hub")));
  85. portalList.add(name);
  86. if (!file.exists()) {
  87. try {
  88. file.mkdirs();
  89. } catch (Exception e) {
  90. player.sendMessage(prefix + ChatColor.translateAlternateColorCodes('&',
  91. Lang.PORTAL_FILE_CREATE_FAIL.toString().replaceAll("%name%", name)
  92. .replaceAll("%type%", "hub")));
  93. e.printStackTrace();
  94. return;
  95. }
  96. }
  97.  
  98. FileConfiguration portalConf = YamlConfiguration.loadConfiguration(file);
  99.  
  100. portalConf.set("name", name);
  101. portalConf.set("type", "hub");
  102. portalConf.set("owner", player.getName());
  103.  
  104. portalConf.createSection("blocks");
  105.  
  106. int i = 0;
  107. for (Block block : blocks) {
  108. double x = block.getX();
  109. double y = block.getY();
  110. double z = block.getZ();
  111.  
  112. portalConf.set("blocks." + i + ".x", x);
  113. portalConf.set("blocks." + i + ".y", y);
  114. portalConf.set("blocks." + i + ".z", z);
  115. i++;
  116. }
  117. try {
  118. portalConf.save(file);
  119. } catch (IOException e) {
  120. player.sendMessage(prefix + ChatColor.translateAlternateColorCodes('&',
  121. Lang.PORTAL_FILE_SAVE_FAIL.toString().replaceAll("%name%", name)));
  122. e.printStackTrace();
  123. return;
  124. }
  125. this.type = type;
  126. this.name = name;
  127. this.owner = player.getName();
  128. this.blocksCount = i;
  129. player.sendMessage(prefix + ChatColor.translateAlternateColorCodes('&',
  130. Lang.PORTAL_CREATED_HUB.toString().replaceAll("%name%", name)));
  131. } else {
  132. player.sendMessage(prefix + ChatColor.translateAlternateColorCodes('&',
  133. Lang.PORTAL_FILE_NULL.toString().replaceAll("%name%", name)));
  134.  
  135. try {
  136. file.mkdirs();
  137. } catch (Exception e) {
  138. player.sendMessage(prefix + ChatColor.translateAlternateColorCodes('&',
  139. Lang.PORTAL_FILE_CREATE_FAIL.toString().replaceAll("%name%", name)));
  140. e.printStackTrace();
  141. }
  142. }
  143. }
  144. } else {
  145. player.sendMessage(prefix
  146. + ChatColor.translateAlternateColorCodes('&', Lang.PORTAL_SELECTION_NULL.toString()));
  147. return;
  148. }
  149. } else {
  150. player.sendMessage(ChatColor.RED + "Portals feature disabled! Check if WorldEdit is installed.");
  151. main.consoleMessage(ChatColor.RED + "Portals feature disabled! Check if WorldEdit is installed.");
  152. return;
  153. }
  154. } else {
  155. return;
  156. }
  157. }
  158.  
  159. public void createNew(Player player, String name, String type, String spawn) {
  160. enabled = main.getConfig().getBoolean("components.portals");
  161. if (type.equals("spawn")) {
  162. if (enabled == true) {
  163. Selection selection = worldEdit.getSelection(player);
  164.  
  165. if (selection != null) {
  166. Location min = selection.getMinimumPoint();
  167. Location max = selection.getMaximumPoint();
  168.  
  169. List<Block> blocks = getPortalBlocks(min, max);
  170.  
  171. if (blocks.size() > 200) {
  172. player.sendMessage(
  173. prefix + ChatColor.translateAlternateColorCodes('&', Lang.PORTAL_SELECTION_TOO_BIG
  174. .toString().replaceAll("%blocks%", String.valueOf(blocks.size()))));
  175. return;
  176. } else {
  177. if (file != null) {
  178. if (portalList.contains(name)) {
  179. player.sendMessage(prefix + ChatColor.translateAlternateColorCodes('&',
  180. Lang.COMMAND_SETPORTAL_EXISTS.toString().replaceAll("%pluginname%", name)));
  181. return;
  182. }
  183. player.sendMessage(prefix + ChatColor.translateAlternateColorCodes('&', Lang.PORTAL_CREATING
  184. .toString().replaceAll("%name%", name).replaceAll("%type%", "hub")));
  185. portalList.add(name);
  186. if (!file.exists()) {
  187. try {
  188. file.mkdirs();
  189. } catch (Exception e) {
  190. player.sendMessage(prefix + ChatColor.translateAlternateColorCodes('&',
  191. Lang.PORTAL_FILE_CREATE_FAIL.toString().replaceAll("%name%", name)
  192. .replaceAll("%type%", "hub")));
  193. e.printStackTrace();
  194. return;
  195. }
  196. }
  197.  
  198. FileConfiguration portalConf = YamlConfiguration.loadConfiguration(file);
  199.  
  200. portalConf.set("name", name);
  201. portalConf.set("type", "hub");
  202. portalConf.set("owner", player.getName());
  203. portalConf.set("world", world);
  204.  
  205. portalConf.createSection("blocks");
  206.  
  207. int i = 0;
  208. for (Block block : blocks) {
  209. double x = block.getX();
  210. double y = block.getY();
  211. double z = block.getZ();
  212.  
  213. portalConf.set("blocks." + i + ".x", x);
  214. portalConf.set("blocks." + i + ".y", y);
  215. portalConf.set("blocks." + i + ".z", z);
  216. try {
  217. portalConf.save(file);
  218. } catch (IOException e) {
  219. player.sendMessage(prefix + ChatColor.translateAlternateColorCodes('&',
  220. Lang.PORTAL_FILE_SAVE_FAIL.toString()));
  221. e.printStackTrace();
  222. }
  223. i++;
  224. return;
  225. }
  226. this.type = type;
  227. this.name = name;
  228. this.owner = player.getName();
  229. player.sendMessage(prefix + ChatColor.translateAlternateColorCodes('&',
  230. Lang.PORTAL_CREATED_HUB.toString().replaceAll("%name%", name)));
  231. } else {
  232. player.sendMessage(prefix + ChatColor.translateAlternateColorCodes('&',
  233. Lang.PORTAL_FILE_NULL.toString().replaceAll("%name%", name)));
  234.  
  235. try {
  236. file.mkdirs();
  237. } catch (Exception e) {
  238. player.sendMessage(prefix + ChatColor.translateAlternateColorCodes('&',
  239. Lang.PORTAL_FILE_CREATE_FAIL.toString().replaceAll("%name%", name)));
  240. e.printStackTrace();
  241. }
  242. }
  243. }
  244. } else {
  245. player.sendMessage(prefix
  246. + ChatColor.translateAlternateColorCodes('&', Lang.PORTAL_SELECTION_NULL.toString()));
  247. return;
  248. }
  249. } else {
  250. player.sendMessage(ChatColor.RED + "Portals feature disabled! Check if WorldEdit is installed.");
  251. main.consoleMessage(ChatColor.RED + "Portals feature disabled! Check if WorldEdit is installed.");
  252. return;
  253. }
  254. } else {
  255. return;
  256. }
  257. }
  258.  
  259. public String getName() {
  260. return name;
  261. }
  262.  
  263. public List<Block> getBlocks() {
  264. return blocks;
  265. }
  266.  
  267. public List<Block> getPortalBlocks(String portalName) {
  268. FileConfiguration conf = YamlConfiguration.loadConfiguration(this.getFile(portalName));
  269. @SuppressWarnings("unchecked")
  270. List<Block> list = (List<Block>) conf.getList("blocks");
  271. return list;
  272. }
  273.  
  274. public World getWorld() {
  275. return world;
  276. }
  277.  
  278. public String getType(String portalName) {
  279. FileConfiguration conf = YamlConfiguration.loadConfiguration(this.getFile(portalName));
  280. String type = conf.getString("type");
  281. return type;
  282. }
  283.  
  284. public Location getPortalLocation() {
  285. Location location = new Location(world, mainBlock.getX(), mainBlock.getY(), mainBlock.getZ());
  286. return location;
  287. }
  288.  
  289. public List<String> getPortals() {
  290. return portalList;
  291. }
  292.  
  293. public Block getMainBlock() {
  294. return mainBlock;
  295. }
  296.  
  297. public String getType() {
  298. return type;
  299. }
  300.  
  301. public String getOwner() {
  302. return owner;
  303. }
  304.  
  305. public File getFile() {
  306. return file;
  307. }
  308.  
  309. public File getFile(String portalName) {
  310. File f = new File(main.getDataFolder(), File.separator + "portals" + File.separator + portalName);
  311. return f;
  312. }
  313.  
  314. private List<Block> getPortalBlocks(Location loc1, Location loc2) {
  315. int topBlockX = (loc1.getBlockX() < loc2.getBlockX() ? loc2.getBlockX() : loc1.getBlockX());
  316. int bottomBlockX = (loc1.getBlockX() > loc2.getBlockX() ? loc2.getBlockX() : loc1.getBlockX());
  317.  
  318. int topBlockY = (loc1.getBlockY() < loc2.getBlockY() ? loc2.getBlockY() : loc1.getBlockY());
  319. int bottomBlockY = (loc1.getBlockY() > loc2.getBlockY() ? loc2.getBlockY() : loc1.getBlockY());
  320.  
  321. int topBlockZ = (loc1.getBlockZ() < loc2.getBlockZ() ? loc2.getBlockZ() : loc1.getBlockZ());
  322. int bottomBlockZ = (loc1.getBlockZ() > loc2.getBlockZ() ? loc2.getBlockZ() : loc1.getBlockZ());
  323.  
  324. for (int x = bottomBlockX; x <= topBlockX; x++) {
  325. for (int z = bottomBlockZ; z <= topBlockZ; z++) {
  326. for (int y = bottomBlockY; y <= topBlockY; y++) {
  327. Block block = loc1.getWorld().getBlockAt(x, y, z);
  328.  
  329. blocks.add(block);
  330. }
  331. }
  332. }
  333.  
  334. Location loc = new Location(loc1.getWorld(), loc1.getX(), loc1.getY(), loc1.getZ());
  335. Block b = loc.getBlock();
  336. this.mainBlock = b;
  337. return blocks;
  338. }
  339. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement