Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.69 KB | None | 0 0
  1. package com.hiroku.skyblock.world;
  2.  
  3. import java.math.BigDecimal;
  4. import java.time.Instant;
  5. import java.util.ArrayList;
  6. import java.util.Date;
  7. import java.util.List;
  8. import java.util.Optional;
  9. import java.util.UUID;
  10.  
  11. import org.spongepowered.api.Sponge;
  12. import org.spongepowered.api.entity.living.player.Player;
  13. import org.spongepowered.api.entity.living.player.User;
  14. import org.spongepowered.api.service.user.UserStorageService;
  15. import org.spongepowered.api.text.Text;
  16. import org.spongepowered.api.world.World;
  17.  
  18. import com.flowpowered.math.vector.Vector3d;
  19. import com.google.common.collect.ImmutableList;
  20. import com.google.common.collect.Lists;
  21. import com.hiroku.skyblock.SkyblockPlugin;
  22. import com.hiroku.skyblock.challenges.data.ChallengeData;
  23. import com.hiroku.skyblock.exceptions.CreateIslandException;
  24. import com.hiroku.skyblock.exceptions.InvalidRegionException;
  25. import com.hiroku.skyblock.spawning.SkyblockSpawnSet;
  26. import com.hiroku.skyblock.spawning.SkyblockSpawner;
  27. import com.hiroku.skyblock.world.regions.IRegionPattern;
  28. import com.hiroku.skyblock.world.regions.Region;
  29. import com.hiroku.skyblock.world.regions.SpiralRegionPattern;
  30. import com.hiroku.skyblock.world.tasks.GenSkyblockTask;
  31.  
  32. /**
  33. * This class represents a Skyblock instance, containing info such as the Skyblock's
  34. * spawn point, owner, members, date created, and much much more.
  35. *
  36. * Two constructors will be available here, one for generation, and the other
  37. * for caching existing data.
  38. *
  39. * The conventional naming for this Object will be 'sb'
  40. *
  41. * @author NickImpact
  42. */
  43. public class Skyblock
  44. {
  45. public static final IRegionPattern PATTERN = new SpiralRegionPattern();
  46.  
  47. /** The UUID of the Skyblock island */
  48. private UUID skyblockID;
  49.  
  50. /** The region the Skyblock island is assigned to */
  51. private Region region;
  52.  
  53. /** The UUID of the Grief Prevention claim encompassing the Region */
  54. private UUID claim;
  55.  
  56. /** The UUID of the owner of this Skyblock */
  57. private UUID owner;
  58.  
  59. /** The Schematic of which was used to gen the Skyblock island */
  60. private Schematic schematic;
  61.  
  62. private ArrayList<String> spawnSets = new ArrayList<>();
  63.  
  64. public transient SkyblockSpawner spawner = new SkyblockSpawner(this);
  65.  
  66. /** Each registered member of the island */
  67. private List<UUID> members;
  68.  
  69. /** The spawn point of the Skyblock island */
  70. private Vector3d spawn;
  71.  
  72. /** Specifies the Date of which this Skyblock was created */
  73. private Date creation;
  74.  
  75. /** Specific the date of the last owner/member login */
  76. private Date lastLogin;
  77.  
  78. /** The amount of money in the Skyblock account. */
  79. private volatile BigDecimal balance = BigDecimal.ZERO;
  80.  
  81. /** Whether or not online players not assigned to an island can tp to an island */
  82. public boolean open;
  83.  
  84. private ChallengeData challengeData;
  85.  
  86. public Skyblock()
  87. {
  88. ;
  89. }
  90.  
  91. /**
  92. * This constructor serves as an alias to Skyblock generation, where
  93. * no schematic has been specified by the user
  94. *
  95. * @param owner The owner of the to-be-generated Skyblock
  96. * @throws CreateIslandException Occurs on any failed attempt of finding a suitable region
  97. */
  98. public Skyblock(User owner) throws CreateIslandException
  99. {
  100. this(owner, new Schematic("default", "Default"), 64);
  101. }
  102.  
  103. /**
  104. * This constructor's main task is to handle Skyblock generation, assigning
  105. * key and essential data to the Skyblock instance prior to generation.
  106. *
  107. * Once all data has been assigned, and we have ensured a region for creation,
  108. * we will then begin the island generation task, saving everything to memory
  109. * immediately following.
  110. *
  111. * @param owner The User being granted the Skyblock island
  112. * @param schematic The schematic to use for this Skyblock generation
  113. *
  114. * @throws CreateIslandException Occurs on any failed attempt of finding a suitable region
  115. */
  116. public Skyblock(User owner, Schematic schematic, int yLevel) throws CreateIslandException
  117. {
  118. this.skyblockID = UUID.randomUUID();
  119.  
  120. this.owner = owner.getUniqueId();
  121. this.schematic = schematic;
  122.  
  123. this.members = Lists.newArrayList();
  124.  
  125. this.creation = Date.from(Instant.now());
  126. this.lastLogin = Date.from(Instant.now());
  127.  
  128. this.spawnSets = Lists.newArrayList();
  129.  
  130. try
  131. {
  132. if(owner.getUniqueId().equals(UUID.fromString("a8d614a7-7e28-4f69-ae54-3ad8deb82efc")))
  133. this.region = new Region(4, 20);
  134. else if(owner.getUniqueId().equals(UUID.fromString("2120447d-bb7a-49f3-83a0-aad8d4f2b7c5")))
  135. this.region = new Region(6, 9);
  136. else
  137. this.region = PATTERN.nextRegion();
  138. }
  139. catch (InvalidRegionException e)
  140. {
  141. throw new CreateIslandException(e.getText());
  142. }
  143.  
  144. this.claim = ClaimHelper.forgeIslandClaim(owner.getUniqueId(), region).getUniqueId();
  145.  
  146. this.open = false;
  147.  
  148. owner.getPlayer().ifPresent(player ->
  149. {
  150. player.sendMessage(Text.of(
  151. SkyblockPlugin.PREFIX, "Please wait will your island is generated..."
  152. ));
  153. });
  154.  
  155. GenSkyblockTask genTask = new GenSkyblockTask((Player)owner, this, schematic, yLevel < 30 ? 30 : yLevel > 120 ? 120 : yLevel);
  156. Sponge.getScheduler().createTaskBuilder().execute(genTask).submit(SkyblockPlugin.getInstance());
  157.  
  158. System.out.println(String.format("Adding Skyblock in region (%s, %s)", region.getX(), region.getZ()));
  159.  
  160. save();
  161. }
  162.  
  163. /**
  164. * Retrieves the UUID of this Skyblock island
  165. *
  166. * @return The UUID of this Skyblock island
  167. */
  168. public UUID getSkyblockID()
  169. {
  170. return skyblockID;
  171. }
  172.  
  173. /**
  174. * Retrieves the UUID of this Skyblock's owner
  175. *
  176. * @return The UUID of the Skyblock's owner
  177. */
  178. public UUID getOwner()
  179. {
  180. return owner;
  181. }
  182.  
  183. /**
  184. * Retrieves the UUID of this Skyblock Island's GP claim
  185. *
  186. * @return The UUID of the GP claim
  187. */
  188. public UUID getClaim()
  189. {
  190. return claim;
  191. }
  192.  
  193. /**
  194. * Fetches the name of the schematic used to generate the Skyblock
  195. *
  196. * @return The name of a schematic file
  197. */
  198. public Schematic getSchematic()
  199. {
  200. return schematic;
  201. }
  202.  
  203. public World getWorld()
  204. {
  205. return Sponge.getServer().getWorld(Sponge.getServer().getDefaultWorldName()).get();
  206. }
  207.  
  208. /**
  209. * Fetches the region associated to this particular skyblock
  210. *
  211. * @return The region associated to this skyblock island
  212. */
  213. public Region getRegion()
  214. {
  215. return this.region;
  216. }
  217.  
  218. /**
  219. * Checks to see if the specific User is a valid member of this Skyblock
  220. *
  221. * @param user The user in question
  222. * @return True if valid, false otherwise
  223. */
  224. public boolean isMember(User user)
  225. {
  226. return this.isMember(user.getUniqueId());
  227. }
  228.  
  229. /**
  230. * Attempts to add a User to this Skyblock's member list
  231. *
  232. * @param user The User being added to the member list
  233. * @return True if addition is successful, false otherwise
  234. */
  235. public boolean addMember(User user)
  236. {
  237. if(this.isMember(user)) return false;
  238.  
  239. return this.members.add(user.getUniqueId());
  240. }
  241.  
  242. /**
  243. * Attempts to remove a User from this Skyblock's member list
  244. *
  245. * @param user The User being removed from the member list
  246. * @return True if removal is successful, false otherwise
  247. */
  248. public boolean removeMember(User user)
  249. {
  250. if(!this.isMember(user)) return false;
  251.  
  252. return this.members.remove(user.getUniqueId());
  253. }
  254.  
  255. /**
  256. * Checks to see if a specific UUID of a User is a valid member of this Skyblock
  257. *
  258. * @param uuid The UUID of the User in question
  259. * @return True if valid, false otherwise
  260. */
  261. public boolean isMember(UUID uuid)
  262. {
  263. return this.members.contains(uuid);
  264. }
  265.  
  266. /**
  267. * Finds all online members belonging to this Skyblock
  268. *
  269. * @return A List of all players online for this Skyblock, with the owner being index 0 if online
  270. */
  271. public List<Player> getOnlineMembers()
  272. {
  273. List<Player> online = Lists.newArrayList();
  274.  
  275. Optional<Player> owner = Sponge.getServer().getPlayer(this.getOwner());
  276. owner.ifPresent(online::add);
  277.  
  278. for(UUID uuid : members)
  279. {
  280. Optional<Player> player = Sponge.getServer().getPlayer(uuid);
  281. player.ifPresent(online::add);
  282. }
  283.  
  284. return online;
  285. }
  286.  
  287. /**
  288. * Returns a list of all the names of each member within a Skyblock instance
  289. *
  290. * First, we check the server for the player. If we can't find a player who is
  291. * online with a matching UUID, we will then check the {@link UserStorageService}
  292. *
  293. * @return A list of all members on the Island, with the first index being the owner.
  294. */
  295. public List<Text> getAllMembers()
  296. {
  297. List<Text> names = Lists.newArrayList();
  298.  
  299. Optional<Player> owner = Sponge.getServer().getPlayer(this.getOwner());
  300. if(owner.isPresent())
  301. {
  302. names.add(Text.of(owner.get().getName()));
  303. }
  304. else
  305. {
  306. User ussOwner = SkyblockPlugin.getInstance().getUss().get(this.getOwner()).orElse(null);
  307. if(ussOwner != null)
  308. {
  309. names.add(Text.of(ussOwner.getName()));
  310. }
  311. }
  312.  
  313. for(UUID uuid : members)
  314. {
  315. Optional<Player> player = Sponge.getServer().getPlayer(uuid);
  316. if(player.isPresent())
  317. {
  318. names.add(Text.of(player.get().getName()));
  319. }
  320. else
  321. {
  322. User member = SkyblockPlugin.getInstance().getUss().get(uuid).orElse(null);
  323. if(member != null)
  324. {
  325. names.add(Text.of(member.getName()));
  326. }
  327. else
  328. {
  329. // TODO - Mark an invalid member within the cache
  330. }
  331. }
  332. }
  333. return names;
  334. }
  335.  
  336. /**
  337. * Gets an immutable list of all the members of the Skyblock in UUID form.
  338. *
  339. * @return - A list of all member UUIDs.
  340. */
  341. public ImmutableList<UUID> getAllMemberUUIDs()
  342. {
  343. ArrayList<UUID> allMembers = Lists.newArrayList();
  344. if(members != null)
  345. allMembers.addAll(members);
  346. allMembers.add(owner);
  347.  
  348. return ImmutableList.copyOf(allMembers);
  349. }
  350.  
  351. /**
  352. * @return The number of members in the Skyblock.
  353. */
  354. public int getMemberCount()
  355. {
  356. return members.size() + 1;
  357. }
  358.  
  359. /**
  360. * This method is primarily used for the idea of a separate Skyblock chat, allowing anyone
  361. * to message only the members of an island they belong to.
  362. *
  363. * @param text The text of the message
  364. */
  365. public void sendMessage(Text text){
  366. for(Player player : this.getOnlineMembers()){
  367. player.sendMessage(text);
  368. }
  369. }
  370.  
  371. /**
  372. * Retrieves a position in the world referring to the Skyblock island's
  373. * spawn point
  374. *
  375. * @return The spawn point of the Skyblock island
  376. */
  377. public Vector3d getSpawn()
  378. {
  379. return spawn;
  380. }
  381.  
  382. /**
  383. * Sets the spawn of a Skyblock island
  384. *
  385. * @param position The spawn point of the Skyblock island
  386. */
  387. public void setSpawn(Vector3d position)
  388. {
  389. this.spawn = position;
  390. }
  391.  
  392. /**
  393. * Retrieves the Date of creation for this Skyblock island.
  394. * Mainly to be used for player's to see Skyblock island history,
  395. * but can be molded into a tool to check for inactive islands.
  396. *
  397. * @return The Date of creation for this Skyblock island
  398. */
  399. public Date getCreation()
  400. {
  401. return creation;
  402. }
  403.  
  404. /**
  405. * Retrieves the last login that took place for any of the Skyblock's
  406. * inhabitants. This method is primarily useful for determining inactive
  407. * Skyblock islands that need to be removed from the world.
  408. *
  409. * @return The last login made by any inhabitant on the Skyblock island
  410. */
  411. public Date getLastLogin()
  412. {
  413. return lastLogin;
  414. }
  415.  
  416. /**
  417. * Adds the given {@link SkyblockSpawnSet} to this Skyblock.
  418. */
  419. public void addSpawnSet(SkyblockSpawnSet spawnSet)
  420. {
  421. spawnSets.add(spawnSet.id);
  422. spawner.spawnSets.add(spawnSet);
  423. }
  424.  
  425. /**
  426. * Gets all the String form of all the Skyblock's spawner's {@link SkyblockSpawnSet}s.
  427. */
  428. public ImmutableList<String> getSpawnSets()
  429. {
  430. return ImmutableList.copyOf(spawnSets);
  431. }
  432.  
  433. /**
  434. * Sets the balance of the Skyblock to the given amount.
  435. */
  436. public void setBalance(BigDecimal amount)
  437. {
  438. this.balance = amount;
  439. }
  440.  
  441. /**
  442. * Gets the current balance of the Skyblock
  443. */
  444. public BigDecimal getBalance()
  445. {
  446. return balance;
  447. }
  448.  
  449. /**
  450. * Adds the given amount to the Skyblock's balance
  451. */
  452. public void deposit(BigDecimal amount)
  453. {
  454. balance.add(amount);
  455. }
  456.  
  457. /**
  458. * Attempts to withdraw the specified amount from the
  459. * Skyblock balance, and returns the amount that could
  460. * be withdrawn.
  461. */
  462. public BigDecimal withdraw(BigDecimal amount)
  463. {
  464. BigDecimal withdrawn = BigDecimal.ZERO;
  465.  
  466. if (balance.compareTo(amount) != 1)
  467. {
  468. withdrawn = BigDecimal.valueOf(balance.doubleValue());
  469. balance = BigDecimal.ZERO;
  470. }
  471. else
  472. {
  473. balance.subtract(amount);
  474. withdrawn = amount;
  475. }
  476. return withdrawn;
  477. }
  478.  
  479. public ChallengeData getChallengeData()
  480. {
  481. return this.challengeData;
  482. }
  483.  
  484. /**
  485. * This method will save the current state of the island to
  486. * the registry, as well as queue it for the next storage write
  487. */
  488. private void save()
  489. {
  490. SkyblockPlugin.getInstance().getIslandRegistry().addSkyblock(this);
  491. }
  492. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement