Skip to content

Commit

Permalink
Added configurable block counts save threshold (#1675)
Browse files Browse the repository at this point in the history
  • Loading branch information
OmerBenGera committed Apr 13, 2023
1 parent 1734de4 commit 5c58c08
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 11 deletions.
Expand Up @@ -11,6 +11,7 @@
import org.bukkit.configuration.ConfigurationSection;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -606,6 +607,12 @@ public interface SettingsManager {
*/
List<RespawnAction> getPlayerRespawn();

/**
* Get the threshold between saves for block counts.
* Config-path: block-counts-save-threshold
*/
BigInteger getBlockCountsSaveThreshold();

interface Database {

/**
Expand Down
Expand Up @@ -37,6 +37,7 @@
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -211,6 +212,7 @@ public class SettingsContainer {
public final int bossBarLimit;
public final boolean deleteUnsafeWarps;
public final List<RespawnAction> playerRespawnActions;
public final BigInteger blockCountsSaveThreshold;

public SettingsContainer(SuperiorSkyblockPlugin plugin, YamlConfiguration config) throws ManagerLoadException {
databaseType = config.getString("database.type").toUpperCase(Locale.ENGLISH);
Expand Down Expand Up @@ -544,6 +546,7 @@ else if (sections.length == 3)
Log.warn("Invalid respawn action ", respawnAction + ", skipping...");
}
});
blockCountsSaveThreshold = BigInteger.valueOf(config.getInt("block-counts-save-threshold", 100));
}

private List<String> loadInteractables(SuperiorSkyblockPlugin plugin) {
Expand Down
Expand Up @@ -28,6 +28,7 @@

import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -565,6 +566,11 @@ public List<RespawnAction> getPlayerRespawn() {
return this.container.playerRespawnActions;
}

@Override
public BigInteger getBlockCountsSaveThreshold() {
return this.container.blockCountsSaveThreshold;
}

public void updateValue(String path, Object value) throws IOException {
File file = new File(plugin.getDataFolder(), "config.yml");

Expand Down
32 changes: 22 additions & 10 deletions src/main/java/com/bgsoftware/superiorskyblock/island/SIsland.java
Expand Up @@ -198,7 +198,8 @@ public class SIsland implements Island {
* Island Flags
*/
private volatile boolean beingRecalculated = false;
private final AtomicInteger blocksUpdateCounter = new AtomicInteger(0);
private final AtomicReference<BigInteger> currentTotalBlockCounts = new AtomicReference<>(BigInteger.ZERO);
private volatile BigInteger lastSavedBlockCounts = BigInteger.ZERO;
private SuperiorPlayer owner;
private String creationTimeDate;
/*
Expand Down Expand Up @@ -298,8 +299,10 @@ public SIsland(IslandBuilderImpl builder) {
}
});
if (!builder.blockCounts.isEmpty()) {
plugin.getProviders().addPricesLoadCallback(() -> builder.blockCounts.forEach((block, count) ->
handleBlockPlace(block, count, false, false)));
plugin.getProviders().addPricesLoadCallback(() -> {
builder.blockCounts.forEach((block, count) -> handleBlockPlace(block, count, false, false));
this.lastSavedBlockCounts = this.currentTotalBlockCounts.get();
});
}

builder.warpCategories.forEach(warpCategoryRecord -> {
Expand Down Expand Up @@ -2002,6 +2005,8 @@ public void handleBlockPlace(Key key, BigInteger amount, boolean save, boolean u
if (!trackedBlock)
return;

BigInteger newTotalBlocksCount = this.currentTotalBlockCounts.updateAndGet(count -> count.add(amount));

BigDecimal oldWorth = getWorth();
BigDecimal oldLevel = getIslandLevel();

Expand All @@ -2024,7 +2029,7 @@ public void handleBlockPlace(Key key, BigInteger amount, boolean save, boolean u
updateLastTime();

if (save)
saveBlockCounts(oldWorth, oldLevel);
saveBlockCounts(newTotalBlocksCount, oldWorth, oldLevel);
}

@Override
Expand All @@ -2037,6 +2042,8 @@ public void handleBlocksPlace(Map<Key, Integer> blocks) {
blocks.forEach((blockKey, amount) ->
handleBlockPlace(blockKey, BigInteger.valueOf(amount), false, false));

this.lastSavedBlockCounts = this.currentTotalBlockCounts.get();

IslandsDatabaseBridge.saveBlockCounts(this);
IslandsDatabaseBridge.saveDirtyChunks(this.dirtyChunksContainer);

Expand Down Expand Up @@ -2083,6 +2090,8 @@ public void handleBlockBreak(Key key, BigInteger amount, boolean save) {
if (!untrackedBlocks)
return;

BigInteger newTotalBlocksCount = this.currentTotalBlockCounts.updateAndGet(count -> count.subtract(amount));

BigDecimal oldWorth = getWorth(), oldLevel = getIslandLevel();

BigDecimal blockValue = plugin.getBlockValues().getBlockWorth(key);
Expand All @@ -2103,7 +2112,7 @@ public void handleBlockBreak(Key key, BigInteger amount, boolean save) {
updateLastTime();

if (save)
saveBlockCounts(oldWorth, oldLevel);
saveBlockCounts(newTotalBlocksCount, oldWorth, oldLevel);
}

@Override
Expand Down Expand Up @@ -3698,7 +3707,7 @@ private void calcIslandWorthInternal(@Nullable SuperiorPlayer asker, @Nullable R
plugin.getMenus().refreshValues(this);
plugin.getMenus().refreshCounts(this);

saveBlockCounts(oldWorth, oldLevel);
saveBlockCounts(this.currentTotalBlockCounts.get(), oldWorth, oldLevel);
updateLastTime();

beingRecalculated = false;
Expand Down Expand Up @@ -3802,25 +3811,28 @@ private void replacePermissions(SuperiorPlayer originalPlayer, @Nullable Superio
}
}

private void saveBlockCounts(BigDecimal oldWorth, BigDecimal oldLevel) {
private void saveBlockCounts(BigInteger currentTotalBlocksCount, BigDecimal oldWorth, BigDecimal oldLevel) {
BigDecimal newWorth = getWorth();
BigDecimal newLevel = getIslandLevel();

if (oldLevel.compareTo(newLevel) != 0 || oldWorth.compareTo(newWorth) != 0) {
BukkitExecutor.async(() -> plugin.getEventsBus().callIslandWorthUpdateEvent(this, oldWorth, oldLevel, newWorth, newLevel), 0L);
}

if (blocksUpdateCounter.incrementAndGet() >= Bukkit.getOnlinePlayers().size() * 10) {
BigInteger deltaBlockCounts = this.lastSavedBlockCounts.subtract(currentTotalBlocksCount);
if (deltaBlockCounts.compareTo(BigInteger.ZERO) < 0)
deltaBlockCounts = deltaBlockCounts.negate();

if (deltaBlockCounts.compareTo(plugin.getSettings().getBlockCountsSaveThreshold()) >= 0) {
this.lastSavedBlockCounts = currentTotalBlocksCount;
IslandsDatabaseBridge.saveBlockCounts(this);
blocksUpdateCounter.set(0);
plugin.getGrid().sortIslands(SortingTypes.BY_WORTH);
plugin.getGrid().sortIslands(SortingTypes.BY_LEVEL);
plugin.getMenus().refreshValues(this);
plugin.getMenus().refreshCounts(this);
} else {
IslandsDatabaseBridge.markBlockCountsToBeSaved(this);
}

}

public void syncUpgrades(boolean overrideCustom) {
Expand Down
8 changes: 7 additions & 1 deletion src/main/resources/config.yml
Expand Up @@ -796,4 +796,10 @@ delete-unsafe-warps: true
# VANILLA - Respawn the player in his vanilla spawn point (bed location or world spawn point).
player-respawn:
- ISLAND_TELEPORT
- SPAWN_TELEPORT
- SPAWN_TELEPORT

# The amount of blocks that are required to be updated on the island before updating the block counts in the database.
# This means that if the threshold is at 100, it will require 100 blocks to be added/removed before updating the
# database with the new block counts. If the server stops and there was a change to the block counts, the block counts
# will be saved to the database despite the threshold.
block-counts-save-threshold: 100

0 comments on commit 5c58c08

Please sign in to comment.