Skip to content

Commit

Permalink
Separated the blocks-tracker from the Island object
Browse files Browse the repository at this point in the history
  • Loading branch information
OmerBenGera committed Jan 14, 2022
1 parent 630a054 commit 5648b25
Show file tree
Hide file tree
Showing 8 changed files with 320 additions and 150 deletions.
@@ -1,6 +1,7 @@
package com.bgsoftware.superiorskyblock.api.factory;

import com.bgsoftware.superiorskyblock.api.island.Island;
import com.bgsoftware.superiorskyblock.api.island.algorithms.IslandBlocksTrackerAlgorithm;
import com.bgsoftware.superiorskyblock.api.island.algorithms.IslandCalculationAlgorithm;

public interface IslandsFactory {
Expand All @@ -19,4 +20,11 @@ public interface IslandsFactory {
*/
IslandCalculationAlgorithm createIslandCalculationAlgorithm(Island island);

/**
* Create a blocks-tracking algorithm for an island.
*
* @param island The island to set the algorithm to.
*/
IslandBlocksTrackerAlgorithm createIslandBlocksTrackerAlgorithm(Island island);

}
Expand Up @@ -1181,6 +1181,13 @@ public interface Island extends Comparable<Island>, IMissionsHolder {
*/
int getExactBlockLimit(Key key);

/**
* Get the block key used as a limit for another block key.
*
* @param key The block's key to check.
*/
Key getBlockLimitKey(Key key);

/**
* Get all the blocks limits for the island.
*/
Expand Down
@@ -0,0 +1,63 @@
package com.bgsoftware.superiorskyblock.api.island.algorithms;

import com.bgsoftware.superiorskyblock.api.key.Key;

import java.math.BigInteger;
import java.util.Map;

public interface IslandBlocksTrackerAlgorithm {

/**
* Track a new block with a specific amount.
*
* @param key The block's key that should be tracked.
* @param amount The amount of the block.
* @return Whether the block was successfully tracked.
*/
boolean trackBlock(Key key, BigInteger amount);

/**
* Untrack a block with a specific amount.
*
* @param key The block's key that should be untracked.
* @param amount The amount of the block.
* @return Whether the block was successfully untracked.
*/
boolean untrackBlock(Key key, BigInteger amount);

/**
* Get the amount of blocks that are on the island.
*
* @param key The block's key to check.
*/
BigInteger getBlockCount(Key key);

/**
* Get the amount of blocks that are on the island.
* Unlike getBlockCount(Key), this method returns the count for
* the exactly block that is given as a parameter.
*
* @param key The block's key to check.
*/
BigInteger getExactBlockCount(Key key);

/**
* Get all the blocks that are on the island.
*/
Map<Key, BigInteger> getBlockCounts();

/**
* Clear all the block counts of the island.
*/
void clearBlockCounts();

/**
* Set whether the tracker should be in "loading data mode" or not.
* When loading mode is enabled, the tracker should only track blocks for the given blocks
* and not their variants (limits, global blocks, etc)
*
* @param loadingDataMode loading mode
*/
void setLoadingDataMode(boolean loadingDataMode);

}
Expand Up @@ -8,6 +8,7 @@
import com.bgsoftware.superiorskyblock.api.island.IslandFlag;
import com.bgsoftware.superiorskyblock.api.island.IslandPrivilege;
import com.bgsoftware.superiorskyblock.api.island.PlayerRole;
import com.bgsoftware.superiorskyblock.api.island.algorithms.IslandBlocksTrackerAlgorithm;
import com.bgsoftware.superiorskyblock.api.island.warps.IslandWarp;
import com.bgsoftware.superiorskyblock.api.island.warps.WarpCategory;
import com.bgsoftware.superiorskyblock.api.missions.Mission;
Expand Down Expand Up @@ -154,7 +155,7 @@ public static void deserializeWarps(Island island) {
});
}

public static void deserializeBlockCounts(String blocks, Island island) {
public static void deserializeBlockCounts(String blocks, IslandBlocksTrackerAlgorithm blocksTrackerAlgorithm) {
if (blocks == null || blocks.isEmpty())
return;

Expand All @@ -170,7 +171,7 @@ public static void deserializeBlockCounts(String blocks, Island island) {
JsonObject blockCountObject = blockCountElement.getAsJsonObject();
Key blockKey = Key.of(blockCountObject.get("id").getAsString());
BigInteger amount = new BigInteger(blockCountObject.get("amount").getAsString());
island.handleBlockPlace(blockKey, amount, false, false);
blocksTrackerAlgorithm.trackBlock(blockKey, amount);
});
}

Expand Down
Expand Up @@ -9,13 +9,15 @@
import com.bgsoftware.superiorskyblock.api.handlers.GridManager;
import com.bgsoftware.superiorskyblock.api.handlers.StackedBlocksManager;
import com.bgsoftware.superiorskyblock.api.island.Island;
import com.bgsoftware.superiorskyblock.api.island.algorithms.IslandBlocksTrackerAlgorithm;
import com.bgsoftware.superiorskyblock.api.island.algorithms.IslandCalculationAlgorithm;
import com.bgsoftware.superiorskyblock.api.island.bank.IslandBank;
import com.bgsoftware.superiorskyblock.api.player.algorithm.PlayerTeleportAlgorithm;
import com.bgsoftware.superiorskyblock.api.wrappers.SuperiorPlayer;
import com.bgsoftware.superiorskyblock.database.DatabaseResult;
import com.bgsoftware.superiorskyblock.database.sql.SQLDatabaseBridge;
import com.bgsoftware.superiorskyblock.island.SIsland;
import com.bgsoftware.superiorskyblock.island.algorithms.DefaultIslandBlocksTrackerAlgorithm;
import com.bgsoftware.superiorskyblock.island.algorithms.DefaultIslandCalculationAlgorithm;
import com.bgsoftware.superiorskyblock.island.bank.SIslandBank;
import com.bgsoftware.superiorskyblock.player.SSuperiorPlayer;
Expand Down Expand Up @@ -87,6 +89,11 @@ public IslandCalculationAlgorithm createIslandCalculationAlgorithm(Island island
islandsFactory.createIslandCalculationAlgorithm(island);
}

public IslandBlocksTrackerAlgorithm createIslandBlocksTrackerAlgorithm(Island island) {
return islandsFactory == null ? new DefaultIslandBlocksTrackerAlgorithm(island) :
islandsFactory.createIslandBlocksTrackerAlgorithm(island);
}

public PlayerTeleportAlgorithm createPlayerTeleportAlgorithm(SuperiorPlayer superiorPlayer) {
return playersFactory == null ? DefaultPlayerTeleportAlgorithm.getInstance() :
playersFactory.createPlayerTeleportAlgorithm(superiorPlayer);
Expand Down

0 comments on commit 5648b25

Please sign in to comment.