Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.hiroku.skyblock.world;
- import java.math.BigDecimal;
- import java.time.Instant;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- import java.util.Optional;
- import java.util.UUID;
- import org.spongepowered.api.Sponge;
- import org.spongepowered.api.entity.living.player.Player;
- import org.spongepowered.api.entity.living.player.User;
- import org.spongepowered.api.service.user.UserStorageService;
- import org.spongepowered.api.text.Text;
- import org.spongepowered.api.world.World;
- import com.flowpowered.math.vector.Vector3d;
- import com.google.common.collect.ImmutableList;
- import com.google.common.collect.Lists;
- import com.hiroku.skyblock.SkyblockPlugin;
- import com.hiroku.skyblock.challenges.data.ChallengeData;
- import com.hiroku.skyblock.exceptions.CreateIslandException;
- import com.hiroku.skyblock.exceptions.InvalidRegionException;
- import com.hiroku.skyblock.spawning.SkyblockSpawnSet;
- import com.hiroku.skyblock.spawning.SkyblockSpawner;
- import com.hiroku.skyblock.world.regions.IRegionPattern;
- import com.hiroku.skyblock.world.regions.Region;
- import com.hiroku.skyblock.world.regions.SpiralRegionPattern;
- import com.hiroku.skyblock.world.tasks.GenSkyblockTask;
- /**
- * This class represents a Skyblock instance, containing info such as the Skyblock's
- * spawn point, owner, members, date created, and much much more.
- *
- * Two constructors will be available here, one for generation, and the other
- * for caching existing data.
- *
- * The conventional naming for this Object will be 'sb'
- *
- * @author NickImpact
- */
- public class Skyblock
- {
- public static final IRegionPattern PATTERN = new SpiralRegionPattern();
- /** The UUID of the Skyblock island */
- private UUID skyblockID;
- /** The region the Skyblock island is assigned to */
- private Region region;
- /** The UUID of the Grief Prevention claim encompassing the Region */
- private UUID claim;
- /** The UUID of the owner of this Skyblock */
- private UUID owner;
- /** The Schematic of which was used to gen the Skyblock island */
- private Schematic schematic;
- private ArrayList<String> spawnSets = new ArrayList<>();
- public transient SkyblockSpawner spawner = new SkyblockSpawner(this);
- /** Each registered member of the island */
- private List<UUID> members;
- /** The spawn point of the Skyblock island */
- private Vector3d spawn;
- /** Specifies the Date of which this Skyblock was created */
- private Date creation;
- /** Specific the date of the last owner/member login */
- private Date lastLogin;
- /** The amount of money in the Skyblock account. */
- private volatile BigDecimal balance = BigDecimal.ZERO;
- /** Whether or not online players not assigned to an island can tp to an island */
- public boolean open;
- private ChallengeData challengeData;
- public Skyblock()
- {
- ;
- }
- /**
- * This constructor serves as an alias to Skyblock generation, where
- * no schematic has been specified by the user
- *
- * @param owner The owner of the to-be-generated Skyblock
- * @throws CreateIslandException Occurs on any failed attempt of finding a suitable region
- */
- public Skyblock(User owner) throws CreateIslandException
- {
- this(owner, new Schematic("default", "Default"), 64);
- }
- /**
- * This constructor's main task is to handle Skyblock generation, assigning
- * key and essential data to the Skyblock instance prior to generation.
- *
- * Once all data has been assigned, and we have ensured a region for creation,
- * we will then begin the island generation task, saving everything to memory
- * immediately following.
- *
- * @param owner The User being granted the Skyblock island
- * @param schematic The schematic to use for this Skyblock generation
- *
- * @throws CreateIslandException Occurs on any failed attempt of finding a suitable region
- */
- public Skyblock(User owner, Schematic schematic, int yLevel) throws CreateIslandException
- {
- this.skyblockID = UUID.randomUUID();
- this.owner = owner.getUniqueId();
- this.schematic = schematic;
- this.members = Lists.newArrayList();
- this.creation = Date.from(Instant.now());
- this.lastLogin = Date.from(Instant.now());
- this.spawnSets = Lists.newArrayList();
- try
- {
- if(owner.getUniqueId().equals(UUID.fromString("a8d614a7-7e28-4f69-ae54-3ad8deb82efc")))
- this.region = new Region(4, 20);
- else if(owner.getUniqueId().equals(UUID.fromString("2120447d-bb7a-49f3-83a0-aad8d4f2b7c5")))
- this.region = new Region(6, 9);
- else
- this.region = PATTERN.nextRegion();
- }
- catch (InvalidRegionException e)
- {
- throw new CreateIslandException(e.getText());
- }
- this.claim = ClaimHelper.forgeIslandClaim(owner.getUniqueId(), region).getUniqueId();
- this.open = false;
- owner.getPlayer().ifPresent(player ->
- {
- player.sendMessage(Text.of(
- SkyblockPlugin.PREFIX, "Please wait will your island is generated..."
- ));
- });
- GenSkyblockTask genTask = new GenSkyblockTask((Player)owner, this, schematic, yLevel < 30 ? 30 : yLevel > 120 ? 120 : yLevel);
- Sponge.getScheduler().createTaskBuilder().execute(genTask).submit(SkyblockPlugin.getInstance());
- System.out.println(String.format("Adding Skyblock in region (%s, %s)", region.getX(), region.getZ()));
- save();
- }
- /**
- * Retrieves the UUID of this Skyblock island
- *
- * @return The UUID of this Skyblock island
- */
- public UUID getSkyblockID()
- {
- return skyblockID;
- }
- /**
- * Retrieves the UUID of this Skyblock's owner
- *
- * @return The UUID of the Skyblock's owner
- */
- public UUID getOwner()
- {
- return owner;
- }
- /**
- * Retrieves the UUID of this Skyblock Island's GP claim
- *
- * @return The UUID of the GP claim
- */
- public UUID getClaim()
- {
- return claim;
- }
- /**
- * Fetches the name of the schematic used to generate the Skyblock
- *
- * @return The name of a schematic file
- */
- public Schematic getSchematic()
- {
- return schematic;
- }
- public World getWorld()
- {
- return Sponge.getServer().getWorld(Sponge.getServer().getDefaultWorldName()).get();
- }
- /**
- * Fetches the region associated to this particular skyblock
- *
- * @return The region associated to this skyblock island
- */
- public Region getRegion()
- {
- return this.region;
- }
- /**
- * Checks to see if the specific User is a valid member of this Skyblock
- *
- * @param user The user in question
- * @return True if valid, false otherwise
- */
- public boolean isMember(User user)
- {
- return this.isMember(user.getUniqueId());
- }
- /**
- * Attempts to add a User to this Skyblock's member list
- *
- * @param user The User being added to the member list
- * @return True if addition is successful, false otherwise
- */
- public boolean addMember(User user)
- {
- if(this.isMember(user)) return false;
- return this.members.add(user.getUniqueId());
- }
- /**
- * Attempts to remove a User from this Skyblock's member list
- *
- * @param user The User being removed from the member list
- * @return True if removal is successful, false otherwise
- */
- public boolean removeMember(User user)
- {
- if(!this.isMember(user)) return false;
- return this.members.remove(user.getUniqueId());
- }
- /**
- * Checks to see if a specific UUID of a User is a valid member of this Skyblock
- *
- * @param uuid The UUID of the User in question
- * @return True if valid, false otherwise
- */
- public boolean isMember(UUID uuid)
- {
- return this.members.contains(uuid);
- }
- /**
- * Finds all online members belonging to this Skyblock
- *
- * @return A List of all players online for this Skyblock, with the owner being index 0 if online
- */
- public List<Player> getOnlineMembers()
- {
- List<Player> online = Lists.newArrayList();
- Optional<Player> owner = Sponge.getServer().getPlayer(this.getOwner());
- owner.ifPresent(online::add);
- for(UUID uuid : members)
- {
- Optional<Player> player = Sponge.getServer().getPlayer(uuid);
- player.ifPresent(online::add);
- }
- return online;
- }
- /**
- * Returns a list of all the names of each member within a Skyblock instance
- *
- * First, we check the server for the player. If we can't find a player who is
- * online with a matching UUID, we will then check the {@link UserStorageService}
- *
- * @return A list of all members on the Island, with the first index being the owner.
- */
- public List<Text> getAllMembers()
- {
- List<Text> names = Lists.newArrayList();
- Optional<Player> owner = Sponge.getServer().getPlayer(this.getOwner());
- if(owner.isPresent())
- {
- names.add(Text.of(owner.get().getName()));
- }
- else
- {
- User ussOwner = SkyblockPlugin.getInstance().getUss().get(this.getOwner()).orElse(null);
- if(ussOwner != null)
- {
- names.add(Text.of(ussOwner.getName()));
- }
- }
- for(UUID uuid : members)
- {
- Optional<Player> player = Sponge.getServer().getPlayer(uuid);
- if(player.isPresent())
- {
- names.add(Text.of(player.get().getName()));
- }
- else
- {
- User member = SkyblockPlugin.getInstance().getUss().get(uuid).orElse(null);
- if(member != null)
- {
- names.add(Text.of(member.getName()));
- }
- else
- {
- // TODO - Mark an invalid member within the cache
- }
- }
- }
- return names;
- }
- /**
- * Gets an immutable list of all the members of the Skyblock in UUID form.
- *
- * @return - A list of all member UUIDs.
- */
- public ImmutableList<UUID> getAllMemberUUIDs()
- {
- ArrayList<UUID> allMembers = Lists.newArrayList();
- if(members != null)
- allMembers.addAll(members);
- allMembers.add(owner);
- return ImmutableList.copyOf(allMembers);
- }
- /**
- * @return The number of members in the Skyblock.
- */
- public int getMemberCount()
- {
- return members.size() + 1;
- }
- /**
- * This method is primarily used for the idea of a separate Skyblock chat, allowing anyone
- * to message only the members of an island they belong to.
- *
- * @param text The text of the message
- */
- public void sendMessage(Text text){
- for(Player player : this.getOnlineMembers()){
- player.sendMessage(text);
- }
- }
- /**
- * Retrieves a position in the world referring to the Skyblock island's
- * spawn point
- *
- * @return The spawn point of the Skyblock island
- */
- public Vector3d getSpawn()
- {
- return spawn;
- }
- /**
- * Sets the spawn of a Skyblock island
- *
- * @param position The spawn point of the Skyblock island
- */
- public void setSpawn(Vector3d position)
- {
- this.spawn = position;
- }
- /**
- * Retrieves the Date of creation for this Skyblock island.
- * Mainly to be used for player's to see Skyblock island history,
- * but can be molded into a tool to check for inactive islands.
- *
- * @return The Date of creation for this Skyblock island
- */
- public Date getCreation()
- {
- return creation;
- }
- /**
- * Retrieves the last login that took place for any of the Skyblock's
- * inhabitants. This method is primarily useful for determining inactive
- * Skyblock islands that need to be removed from the world.
- *
- * @return The last login made by any inhabitant on the Skyblock island
- */
- public Date getLastLogin()
- {
- return lastLogin;
- }
- /**
- * Adds the given {@link SkyblockSpawnSet} to this Skyblock.
- */
- public void addSpawnSet(SkyblockSpawnSet spawnSet)
- {
- spawnSets.add(spawnSet.id);
- spawner.spawnSets.add(spawnSet);
- }
- /**
- * Gets all the String form of all the Skyblock's spawner's {@link SkyblockSpawnSet}s.
- */
- public ImmutableList<String> getSpawnSets()
- {
- return ImmutableList.copyOf(spawnSets);
- }
- /**
- * Sets the balance of the Skyblock to the given amount.
- */
- public void setBalance(BigDecimal amount)
- {
- this.balance = amount;
- }
- /**
- * Gets the current balance of the Skyblock
- */
- public BigDecimal getBalance()
- {
- return balance;
- }
- /**
- * Adds the given amount to the Skyblock's balance
- */
- public void deposit(BigDecimal amount)
- {
- balance.add(amount);
- }
- /**
- * Attempts to withdraw the specified amount from the
- * Skyblock balance, and returns the amount that could
- * be withdrawn.
- */
- public BigDecimal withdraw(BigDecimal amount)
- {
- BigDecimal withdrawn = BigDecimal.ZERO;
- if (balance.compareTo(amount) != 1)
- {
- withdrawn = BigDecimal.valueOf(balance.doubleValue());
- balance = BigDecimal.ZERO;
- }
- else
- {
- balance.subtract(amount);
- withdrawn = amount;
- }
- return withdrawn;
- }
- public ChallengeData getChallengeData()
- {
- return this.challengeData;
- }
- /**
- * This method will save the current state of the island to
- * the registry, as well as queue it for the next storage write
- */
- private void save()
- {
- SkyblockPlugin.getInstance().getIslandRegistry().addSkyblock(this);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement