Skip to content

Commit

Permalink
Merge from upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
mcchampions committed Dec 8, 2024
2 parents f09e64e + f24b660 commit 8747177
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 112 deletions.
32 changes: 32 additions & 0 deletions src/main/java/city/norain/slimefun4/utils/TaskUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package city.norain.slimefun4.utils;

import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;

import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;

import lombok.experimental.UtilityClass;
import org.bukkit.Bukkit;

@UtilityClass
public class TaskUtil {
public <T> T runSyncMethod(Callable<T> callable) {
try {
if (Bukkit.isPrimaryThread()) {
return callable.call();
} else {
return Bukkit.getScheduler()
.callSyncMethod(Slimefun.instance(), callable)
.get(1, TimeUnit.SECONDS);
}
} catch (TimeoutException e) {
Slimefun.logger().log(Level.WARNING, "Timeout when executing sync method", e);
return null;
} catch (Exception e) {
Slimefun.logger().log(Level.SEVERE, "Error when executing sync method", e);
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,7 @@ protected void onError(Throwable e) {
}
}

protected void checkDestroy() {
if (destroyed) {
throw new IllegalStateException("Controller cannot be accessed after destroyed.");
}
}
protected void checkDestroy() {}

protected <T> void invokeCallback(IAsyncReadCallback<T> callback, T result) {
if (callback == null) {
Expand All @@ -145,12 +141,10 @@ protected <T> void invokeCallback(IAsyncReadCallback<T> callback, T result) {
}

protected void scheduleReadTask(Runnable run) {
checkDestroy();
readExecutor.submit(run);
}

protected void scheduleWriteTask(Runnable run) {
checkDestroy();
writeExecutor.submit(run);
}

Expand All @@ -171,7 +165,7 @@ protected void deleteData(RecordKey key) {
}

protected void abortScopeTask(ScopeKey key) {
var task = scheduledWriteTasks.remove(key);
QueuedWriteTask task = scheduledWriteTasks.remove(key);
if (task != null) {
task.abort();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ private void loadLoadedChunks() {
* @param forceSavePeriod 强制保存周期
*/
public void initDelayedSaving(Plugin p, int delayedSecond, int forceSavePeriod) {
checkDestroy();
if (delayedSecond < 1 || forceSavePeriod < 1) {
throw new IllegalArgumentException("save period second must be greater than 0!");
}
Expand Down Expand Up @@ -217,8 +216,7 @@ public void setDelayedSavingEnable(boolean isEnable) {
* {@link SlimefunUniversalData}
*/
public ASlimefunDataContainer createBlock(Location l, String sfId) {
checkDestroy();
var sfItem = SlimefunItem.getById(sfId);
SlimefunItem sfItem = SlimefunItem.getById(sfId);

if (sfItem instanceof UniversalBlock) {
SlimefunUniversalData re = createUniversalBlockData(l, sfId);
Expand All @@ -235,17 +233,14 @@ public ASlimefunDataContainer createBlock(Location l, String sfId) {
}
}

public SlimefunUniversalBlockData createUniversalBlockData(Location l, String sfId) {
checkDestroy();

var uuid = UUID.randomUUID();
var uniData = new SlimefunUniversalBlockData(uuid, sfId, l);
public SlimefunUniversalBlockData createUniversalBlockData(Location l, String sfId) { UUID uuid = UUID.randomUUID();
SlimefunUniversalBlockData uniData = new SlimefunUniversalBlockData(uuid, sfId, l);

uniData.setIsDataLoaded(true);

loadedUniversalData.put(uuid, uniData);

var preset = UniversalMenuPreset.getPreset(sfId);
UniversalMenuPreset preset = UniversalMenuPreset.getPreset(sfId);
if (preset != null) {
uniData.setMenu(new UniversalMenu(preset, uuid, l));
}
Expand All @@ -258,17 +253,17 @@ public SlimefunUniversalBlockData createUniversalBlockData(Location l, String sf
}

void saveNewBlock(Location l, String sfId) {
var lKey = LocationUtils.getLocKey(l);
String lKey = LocationUtils.getLocKey(l);

var key = new RecordKey(DataScope.BLOCK_RECORD);
RecordKey key = new RecordKey(DataScope.BLOCK_RECORD);
// key.addCondition(FieldKey.LOCATION, lKey);

var data = new RecordSet();
RecordSet data = new RecordSet();
data.put(FieldKey.LOCATION, lKey);
data.put(FieldKey.CHUNK, LocationUtils.getChunkKey(l.getChunk()));
data.put(FieldKey.SLIMEFUN_ID, sfId);

var scopeKey = new LocationKey(DataScope.NONE, l);
LocationKey scopeKey = new LocationKey(DataScope.NONE, l);
removeDelayedBlockDataUpdates(scopeKey); // Shouldn't have.. But for safe..
scheduleWriteTask(scopeKey, key, data, true);
}
Expand All @@ -280,16 +275,16 @@ void saveNewBlock(Location l, String sfId) {
* @param sfId the item universal data represents
*/
void saveUniversalData(UUID uuid, String sfId, Set<UniversalDataTrait> traits) {
var key = new RecordKey(DataScope.UNIVERSAL_RECORD);
RecordKey key = new RecordKey(DataScope.UNIVERSAL_RECORD);

var data = new RecordSet();
RecordSet data = new RecordSet();
data.put(FieldKey.UNIVERSAL_UUID, uuid.toString());
data.put(FieldKey.SLIMEFUN_ID, sfId);
data.put(
FieldKey.UNIVERSAL_TRAITS,
String.join(",", traits.stream().map(Enum::name).toList()));

var scopeKey = new UUIDKey(DataScope.NONE, uuid);
UUIDKey scopeKey = new UUIDKey(DataScope.NONE, uuid);
removeDelayedBlockDataUpdates(scopeKey); // Shouldn't have.. But for safe..
scheduleWriteTask(scopeKey, key, data, true);
}
Expand All @@ -299,28 +294,22 @@ void saveUniversalData(UUID uuid, String sfId, Set<UniversalDataTrait> traits) {
*
* @param l slimefun block location {@link Location}
*/
public void removeBlock(Location l) {
checkDestroy();

SlimefunBlockData removed = getChunkDataCache(l.getChunk(), true).removeBlockData(l);
public void removeBlock(Location l) { SlimefunBlockData removed = getChunkDataCache(l.getChunk(), true).removeBlockData(l);
if (removed == null) {
getUniversalBlockDataFromCache(l)
.ifPresentOrElse(data -> removeUniversalBlockData(data.getUUID(), l), () -> {
if (Bukkit.isPrimaryThread()) {
Slimefun.getBlockDataService()
.getUniversalDataUUID(l.getBlock())
.ifPresent(uuid -> removeUniversalBlockData(uuid, l));
}
Slimefun.getBlockDataService()
.getUniversalDataUUID(l.getBlock())
.ifPresent(uuid -> removeUniversalBlockData(uuid, l));
});

return;
}

if (!removed.isDataLoaded()) {
return;
}

var menu = removed.getBlockMenu();
BlockMenu menu = removed.getBlockMenu();
if (menu != null) {
menu.lock();
}
Expand All @@ -334,16 +323,13 @@ public void removeBlock(Location l) {
}
}

public void removeBlockData(Location l) {
checkDestroy();

var removed = getChunkDataCache(l.getChunk(), true).removeBlockData(l);
public void removeBlockData(Location l) { SlimefunBlockData removed = getChunkDataCache(l.getChunk(), true).removeBlockData(l);

if (removed == null || !removed.isDataLoaded()) {
return;
}

var menu = removed.getBlockMenu();
BlockMenu menu = removed.getBlockMenu();
if (menu != null) {
InventoryUtil.closeInventory(menu.toInventory());
}
Expand All @@ -353,10 +339,7 @@ public void removeBlockData(Location l) {
}
}

public void removeUniversalBlockData(UUID uuid, Location lastPresent) {
checkDestroy();

var toRemove = loadedUniversalData.get(uuid);
public void removeUniversalBlockData(UUID uuid, Location lastPresent) { var toRemove = loadedUniversalData.get(uuid);

if (toRemove == null) {
return;
Expand Down Expand Up @@ -384,7 +367,6 @@ public void removeUniversalBlockData(UUID uuid, Location lastPresent) {
}

void removeBlockDirectly(Location l) {
checkDestroy();
var scopeKey = new LocationKey(DataScope.NONE, l);
removeDelayedBlockDataUpdates(scopeKey);

Expand All @@ -394,7 +376,6 @@ void removeBlockDirectly(Location l) {
}

void removeUniversalBlockDirectly(UUID uuid) {
checkDestroy();
var scopeKey = new UUIDKey(DataScope.NONE, uuid);
removeDelayedBlockDataUpdates(scopeKey);

Expand All @@ -411,7 +392,6 @@ void removeUniversalBlockDirectly(UUID uuid) {
*/
@Nullable
public SlimefunBlockData getBlockData(Location l) {
checkDestroy();
if (chunkDataLoadMode.readCacheOnly()) {
return getBlockDataFromCache(l);
}
Expand Down Expand Up @@ -468,19 +448,17 @@ public SlimefunBlockData getBlockDataFromCache(Location l) {
*/
@Nullable
public SlimefunUniversalBlockData getUniversalBlockData(UUID uuid) {
checkDestroy();

var key = new RecordKey(DataScope.UNIVERSAL_RECORD);
RecordKey key = new RecordKey(DataScope.UNIVERSAL_RECORD);
key.addCondition(FieldKey.UNIVERSAL_UUID, uuid.toString());
key.addField(FieldKey.SLIMEFUN_ID);

var result = getData(key);
List<RecordSet> result = getData(key);

if (result.isEmpty()) {
return null;
}

var newData = new SlimefunUniversalBlockData(uuid, result.get(0).get(FieldKey.SLIMEFUN_ID));
SlimefunUniversalBlockData newData = new SlimefunUniversalBlockData(uuid, result.get(0).get(FieldKey.SLIMEFUN_ID));

Arrays.stream(result.get(0).get(FieldKey.UNIVERSAL_TRAITS).split(",")).forEach(tname -> {
for (UniversalDataTrait trait : UniversalDataTrait.values()) {
Expand Down Expand Up @@ -510,8 +488,6 @@ public void getUniversalBlockData(UUID uuid, IAsyncReadCallback<SlimefunUniversa
*/
@Nullable
public SlimefunUniversalBlockData getUniversalBlockDataFromCache(UUID uuid) {
checkDestroy();

var cache = loadedUniversalData.get(uuid);

return cache == null
Expand All @@ -524,10 +500,7 @@ public SlimefunUniversalBlockData getUniversalBlockDataFromCache(UUID uuid) {
*
* @param l Slimefun block location {@link Location}
*/
public Optional<SlimefunUniversalBlockData> getUniversalBlockDataFromCache(Location l) {
checkDestroy();

return loadedUniversalData.values().stream()
public Optional<SlimefunUniversalBlockData> getUniversalBlockDataFromCache(Location l) { return loadedUniversalData.values().stream()
.filter(uniData -> uniData instanceof SlimefunUniversalBlockData ubd
&& ubd.getLastPresent().toLocation().equals(l))
.map(data -> (SlimefunUniversalBlockData) data)
Expand Down Expand Up @@ -620,13 +593,11 @@ public void setBlockDataLocation(SlimefunBlockData blockData, Location target) {
}

private SlimefunBlockData getBlockDataFromCache(String cKey, String lKey) {
checkDestroy();
var chunkData = loadedChunk.get(cKey);
return chunkData == null ? null : chunkData.getBlockCacheInternal(lKey);
}

public void loadChunk(Chunk chunk, boolean isNewChunk) {
checkDestroy();
var chunkData = getChunkDataCache(chunk, true);

if (isNewChunk) {
Expand Down Expand Up @@ -907,7 +878,6 @@ public void loadUniversalDataAsync(
}

public SlimefunChunkData getChunkData(Chunk chunk) {
checkDestroy();
loadChunk(chunk, false);
return getChunkDataCache(chunk, false);
}
Expand Down
Loading

0 comments on commit 8747177

Please sign in to comment.