Skip to content

Commit

Permalink
Added island builder to easily create islands with predefined values
Browse files Browse the repository at this point in the history
  • Loading branch information
OmerBenGera committed Oct 1, 2022
1 parent dacbeb1 commit 4b2c434
Show file tree
Hide file tree
Showing 23 changed files with 1,243 additions and 680 deletions.
Expand Up @@ -66,13 +66,18 @@ public interface FactoriesManager {
* Create a new Island object.
* Warning: This island is not saved into the database unless inserting it manually!
*
* @param superiorPlayer The owner of the island.
* @param uuid The uuid of the island.
* @param location The location of the island.
* @param islandName The name of the island.
* @param schemName The schematic used to create the island.
* @param owner The owner of the island.
* @param uuid The uuid of the island.
* @param center The location of the island.
* @param islandName The name of the island.
* @param schemName The schematic used to create the island.
*/
Island createIsland(@Nullable SuperiorPlayer superiorPlayer, UUID uuid, Location location, String islandName, String schemName);
Island createIsland(@Nullable SuperiorPlayer owner, UUID uuid, Location center, String islandName, String schemName);

/**
* Create a new builder for a {@link Island} object.
*/
Island.Builder createIslandBuilder();

/**
* Create a new SuperiorPlayer object.
Expand Down
Expand Up @@ -57,6 +57,15 @@ public interface GridManager extends IDatabaseBridgeHolder {
void createIsland(SuperiorPlayer superiorPlayer, String schemName, BigDecimal bonusWorth, BigDecimal bonusLevel,
Biome biome, String islandName, boolean offset);

/**
* Create a new island.
*
* @param builder The builder for the island.
* @param biome A starting biome for the island.
* @param offset Should the island have an offset for its values? If disabled, the bonus will be given.
*/
void createIsland(Island.Builder builder, Biome biome, boolean offset);

/**
* Set the creation algorithm of islands.
*
Expand Down
@@ -1,15 +1,18 @@
package com.bgsoftware.superiorskyblock.api.island;

import com.bgsoftware.superiorskyblock.api.SuperiorSkyblockAPI;
import com.bgsoftware.superiorskyblock.api.data.IDatabaseBridgeHolder;
import com.bgsoftware.superiorskyblock.api.enums.Rating;
import com.bgsoftware.superiorskyblock.api.island.algorithms.IslandBlocksTrackerAlgorithm;
import com.bgsoftware.superiorskyblock.api.island.algorithms.IslandCalculationAlgorithm;
import com.bgsoftware.superiorskyblock.api.island.algorithms.IslandEntitiesTrackerAlgorithm;
import com.bgsoftware.superiorskyblock.api.island.bank.BankTransaction;
import com.bgsoftware.superiorskyblock.api.island.bank.IslandBank;
import com.bgsoftware.superiorskyblock.api.island.warps.IslandWarp;
import com.bgsoftware.superiorskyblock.api.island.warps.WarpCategory;
import com.bgsoftware.superiorskyblock.api.key.Key;
import com.bgsoftware.superiorskyblock.api.missions.IMissionsHolder;
import com.bgsoftware.superiorskyblock.api.missions.Mission;
import com.bgsoftware.superiorskyblock.api.objects.Pair;
import com.bgsoftware.superiorskyblock.api.persistence.IPersistentDataHolder;
import com.bgsoftware.superiorskyblock.api.service.message.IMessageComponent;
Expand All @@ -23,6 +26,7 @@
import org.bukkit.block.Block;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.EntityType;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffectType;

import javax.annotation.Nullable;
Expand Down Expand Up @@ -1868,4 +1872,123 @@ boolean setGeneratorPercentage(Key key, int percentage, World.Environment enviro
*/
void setChestRows(int index, int rows);

/**
* Create a new builder for a {@link Island} object.
*/
static Builder newBuilder() {
return SuperiorSkyblockAPI.getFactory().createIslandBuilder();
}

/**
* The {@link Builder} interface is used to create {@link Island} objects with predefined values.
* All of its methods are setters for all the values possible to create an island with.
* Use {@link Builder#build()} to create the new {@link Island} object. You must set
* {@link Builder#setOwner(SuperiorPlayer)}, {@link Builder#setUniqueId(UUID)} and
* {@link Builder#setCenter(Location)} before creating a new {@link Island}
*/
interface Builder {

Builder setOwner(@Nullable SuperiorPlayer owner);

Builder setUniqueId(UUID uuid);

Builder setCenter(Location center);

Builder setName(String islandName);

Builder setSchematicName(String schematicName);

Builder setCreationTime(long creationTime);

Builder setDiscord(String discord);

Builder setPaypal(String paypal);

Builder setBonusWorth(BigDecimal bonusWorth);

Builder setBonusLevel(BigDecimal bonusLevel);

Builder setLocked(boolean isLocked);

Builder setIgnored(boolean isIgnored);

Builder setDescription(String description);

Builder setGeneratedSchematics(int generatedSchematicsMask);

Builder setUnlockedWorlds(int unlockedWorldsMask);

Builder setLastTimeUpdated(long lastTimeUpdated);

Builder setDirtyChunk(String worldName, int chunkX, int chunkZ);

Builder setBlockCount(Key block, BigInteger count);

Builder setIslandHome(Location location, World.Environment environment);

Builder addIslandMember(SuperiorPlayer superiorPlayer);

Builder addBannedPlayer(SuperiorPlayer superiorPlayer);

Builder setPlayerPermission(SuperiorPlayer superiorPlayer, IslandPrivilege islandPrivilege, boolean value);

Builder setRolePermission(IslandPrivilege islandPrivilege, PlayerRole requiredRole);

Builder setUpgrade(Upgrade upgrade, int level);

Builder setBlockLimit(Key block, int limit);

Builder setRating(SuperiorPlayer superiorPlayer, Rating rating);

Builder setCompletedMission(Mission<?> mission, int finishCount);

Builder setIslandFlag(IslandFlag islandFlag, boolean value);

Builder setGeneratorRate(Key block, int rate, World.Environment environment);

Builder addUniqueVisitor(SuperiorPlayer superiorPlayer, long visitTime);

Builder setEntityLimit(Key entity, int limit);

Builder setIslandEffect(PotionEffectType potionEffectType, int level);

Builder setIslandChest(int index, ItemStack[] contents);

Builder setRoleLimit(PlayerRole playerRole, int limit);

Builder setVisitorHome(Location location, World.Environment environment);

Builder setIslandSize(int islandSize);

Builder setTeamLimit(int teamLimit);

Builder setWarpsLimit(int warpsLimit);

Builder setCropGrowth(double cropGrowth);

Builder setSpawnerRates(double spawnerRates);

Builder setMobDrops(double mobDrops);

Builder setCoopLimit(int coopLimit);

Builder setBankLimit(BigDecimal bankLimit);

Builder setBalance(BigDecimal balance);

Builder setLastInterestTime(long lastInterestTime);

Builder addWarp(String name, String category, Location location, boolean isPrivate, @Nullable ItemStack icon);

Builder addWarpCategory(String name, int slot, @Nullable ItemStack icon);

Builder addBankTransaction(BankTransaction bankTransaction);

Builder setPersistentData(byte[] persistentData);

Island build();


}

}
@@ -1,5 +1,6 @@
package com.bgsoftware.superiorskyblock.api.world.algorithm;

import com.bgsoftware.superiorskyblock.api.island.Island;
import com.bgsoftware.superiorskyblock.api.schematic.Schematic;
import com.bgsoftware.superiorskyblock.api.wrappers.BlockPosition;
import com.bgsoftware.superiorskyblock.api.wrappers.SuperiorPlayer;
Expand All @@ -16,10 +17,16 @@ protected DelegateIslandCreationAlgorithm(IslandCreationAlgorithm handle) {
}

@Override
@Deprecated
public CompletableFuture<IslandCreationResult> createIsland(UUID islandUUID, SuperiorPlayer owner,
BlockPosition lastIsland, String islandName,
Schematic schematic) {
return this.handle.createIsland(islandUUID, owner, lastIsland, islandName, schematic);
}

@Override
public CompletableFuture<IslandCreationResult> createIsland(Island.Builder builder, BlockPosition lastIsland) {
return this.handle.createIsland(builder, lastIsland);
}

}
Expand Up @@ -25,6 +25,16 @@ public interface IslandCreationAlgorithm {
CompletableFuture<IslandCreationResult> createIsland(UUID islandUUID, SuperiorPlayer owner, BlockPosition lastIsland,
String islandName, Schematic schematic);

/**
* Create a new island on the server.
* This method should not only create the Island object itself, but also paste a schematic.
* Teleportation and island initialization will be handled by the plugin.
*
* @param builder The builder of the island.
* @param lastIsland The location of the last generated island.
*/
CompletableFuture<IslandCreationResult> createIsland(Island.Builder builder, BlockPosition lastIsland);

/**
* Class representing result of a creation process.
*/
Expand Down
Expand Up @@ -105,7 +105,7 @@ public class SuperiorSkyblockPlugin extends JavaPlugin implements SuperiorSkyblo
private final Updater updater = new Updater(this, "superiorskyblock2");

private final DataManager dataHandler = new DataManager(this);
private final FactoriesManagerImpl factoriesHandler = new FactoriesManagerImpl();
private final FactoriesManagerImpl factoriesHandler = new FactoriesManagerImpl(this);
private final GridManagerImpl gridHandler = new GridManagerImpl(this,
new DefaultIslandsPurger(), new DefaultIslandPreviews());
private final StackedBlocksManagerImpl stackedBlocksHandler = new StackedBlocksManagerImpl(this,
Expand Down
Expand Up @@ -7,7 +7,6 @@
import com.bgsoftware.superiorskyblock.api.wrappers.SuperiorPlayer;
import com.bgsoftware.superiorskyblock.core.Manager;
import com.bgsoftware.superiorskyblock.core.database.bridge.GridDatabaseBridge;
import com.bgsoftware.superiorskyblock.core.database.cache.CachedIslandInfo;
import com.bgsoftware.superiorskyblock.core.database.cache.CachedPlayerInfo;
import com.bgsoftware.superiorskyblock.core.database.cache.DatabaseCache;
import com.bgsoftware.superiorskyblock.core.database.loader.DatabaseLoader;
Expand All @@ -18,13 +17,19 @@
import com.bgsoftware.superiorskyblock.core.database.serialization.PlayersDeserializer;
import com.bgsoftware.superiorskyblock.core.debug.PluginDebugger;
import com.bgsoftware.superiorskyblock.core.errors.ManagerLoadException;
import com.bgsoftware.superiorskyblock.core.serialization.Serializers;
import com.bgsoftware.superiorskyblock.core.threads.BukkitExecutor;
import com.bgsoftware.superiorskyblock.island.builder.IslandBuilderImpl;
import com.bgsoftware.superiorskyblock.island.role.SPlayerRole;
import org.bukkit.Bukkit;
import org.bukkit.Location;

import java.io.File;
import java.math.BigDecimal;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;

@SuppressWarnings("WeakerAccess")
Expand Down Expand Up @@ -128,7 +133,7 @@ private void loadIslands() {

DatabaseBridge islandsLoader = plugin.getFactory().createDatabaseBridge((Island) null);

DatabaseCache<CachedIslandInfo> databaseCache = new DatabaseCache<>();
DatabaseCache<Island.Builder> databaseCache = new DatabaseCache<>();
AtomicInteger islandsCount = new AtomicInteger();
long startTime = System.currentTimeMillis();

Expand Down Expand Up @@ -156,8 +161,55 @@ private void loadIslands() {
IslandsDeserializer.deserializeBankTransactions(islandsLoader, databaseCache);
IslandsDeserializer.deserializePersistentDataContainer(islandsLoader, databaseCache);

islandsLoader.loadAllObjects("islands", resultSet -> {
plugin.getGrid().createIsland(databaseCache, new DatabaseResult(resultSet));
islandsLoader.loadAllObjects("islands", resultSetRaw -> {
DatabaseResult databaseResult = new DatabaseResult(resultSetRaw);

Optional<UUID> uuid = databaseResult.getUUID("uuid");
if (!uuid.isPresent()) {
SuperiorSkyblockPlugin.log("&cCannot load island with invalid uuid, skipping...");
return;
}

Optional<SuperiorPlayer> owner = databaseResult.getUUID("owner").map(plugin.getPlayers()::getSuperiorPlayer);
if (!owner.isPresent()) {
SuperiorSkyblockPlugin.log("&cCannot load island with invalid owner uuid, skipping...");
return;
}

Optional<Location> center = databaseResult.getString("center").map(Serializers.LOCATION_SERIALIZER::deserialize);
if (!center.isPresent()) {
SuperiorSkyblockPlugin.log("&cCannot load island with invalid center, skipping...");
return;
}

Island.Builder builder = databaseCache.computeIfAbsentInfo(uuid.get(), IslandBuilderImpl::new)
.setOwner(owner.get())
.setUniqueId(uuid.get())
.setCenter(center.get())
.setName(databaseResult.getString("name").orElse(""))
.setSchematicName(databaseResult.getString("island_type").orElse(null))
.setCreationTime(databaseResult.getLong("creation_time").orElse(System.currentTimeMillis() / 1000L))
.setDiscord(databaseResult.getString("discord").orElse("None"))
.setPaypal(databaseResult.getString("paypal").orElse("None"))
.setBonusWorth(databaseResult.getBigDecimal("worth_bonus").orElse(BigDecimal.ZERO))
.setBonusLevel(databaseResult.getBigDecimal("levels_bonus").orElse(BigDecimal.ZERO))
.setLocked(databaseResult.getBoolean("locked").orElse(false))
.setIgnored(databaseResult.getBoolean("ignored").orElse(false))
.setDescription(databaseResult.getString("description").orElse(""))
.setGeneratedSchematics(databaseResult.getInt("generated_schematics").orElse(0))
.setUnlockedWorlds(databaseResult.getInt("unlocked_worlds").orElse(0))
.setLastTimeUpdated(databaseResult.getLong("last_time_updated").orElse(System.currentTimeMillis() / 1000L));

databaseResult.getString("dirty_chunks").ifPresent(dirtyChunks -> {
IslandsDeserializer.deserializeDirtyChunks(builder, dirtyChunks);
});

databaseResult.getString("block_counts").ifPresent(blockCounts -> {
IslandsDeserializer.deserializeBlockCounts(builder, blockCounts);
});

plugin.getGrid().getIslandsContainer().addIsland(builder.build());

islandsCount.incrementAndGet();
});

Expand Down

0 comments on commit 4b2c434

Please sign in to comment.