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 23, 2024
2 parents e49747c + bb4d041 commit ac05b89
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.xzavier0722.mc.plugin.slimefun4.storage.util.LocationUtils;
import io.github.bakedlibs.dough.collections.Pair;
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem;
import io.github.thebusybiscuit.slimefun4.core.services.BlockDataService;
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;

import java.util.*;
Expand Down Expand Up @@ -139,7 +140,7 @@ private void loadLoadedWorlds() {
Slimefun.instance(),
() -> {
initLoading = true;
for (var world : Bukkit.getWorlds()) {
for (World world : Bukkit.getWorlds()) {
loadWorld(world);
}
initLoading = false;
Expand All @@ -156,8 +157,8 @@ private void loadLoadedChunks() {
Slimefun.instance(),
() -> {
initLoading = true;
for (var world : Bukkit.getWorlds()) {
for (var chunk : world.getLoadedChunks()) {
for (World world : Bukkit.getWorlds()) {
for (Chunk chunk : world.getLoadedChunks()) {
loadChunk(chunk, false);
}
}
Expand Down Expand Up @@ -211,29 +212,27 @@ public void setDelayedSavingEnable(boolean isEnable) {
*
* @param l Slimefun 方块位置 {@link Location}
* @param sfId Slimefun 物品 ID {@link SlimefunItem#getId()}
* @return 方块数据, 由于 {@link SlimefunItem} 的不同会返回两种数据中的一种
* {@link SlimefunBlockData}
* {@link SlimefunUniversalData}
* @return 方块数据, {@link SlimefunBlockData}
*/
public ASlimefunDataContainer createBlock(Location l, String sfId) {
SlimefunItem sfItem = SlimefunItem.getById(sfId);

if (sfItem instanceof UniversalBlock) {
SlimefunUniversalData re = createUniversalBlockData(l, sfId);
SlimefunUniversalData re = createUniversalBlock(l, sfId);
if (Slimefun.getRegistry().getTickerBlocks().contains(sfId)) {
Slimefun.getTickerTask().enableTicker(l, re.getUUID());
}
return re;
} else {
var re = getChunkDataCache(l.getChunk(), true).createBlockData(l, sfId);
if (Slimefun.getRegistry().getTickerBlocks().contains(sfId)) {
Slimefun.getTickerTask().enableTicker(l);
}
return re;
}
SlimefunBlockData re = getChunkDataCache(l.getChunk(), true).createBlockData(l, sfId);
if (Slimefun.getRegistry().getTickerBlocks().contains(sfId)) {
Slimefun.getTickerTask().enableTicker(l);
}
return re;
}

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

uniData.setIsDataLoaded(true);
Expand Down Expand Up @@ -294,7 +293,8 @@ void saveUniversalData(UUID uuid, String sfId, Set<UniversalDataTrait> traits) {
*
* @param l slimefun block location {@link Location}
*/
public void removeBlock(Location l) { 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), () -> Slimefun.getBlockDataService()
Expand All @@ -321,7 +321,8 @@ void saveUniversalData(UUID uuid, String sfId, Set<UniversalDataTrait> traits) {
}
}

public void removeBlockData(Location l) { SlimefunBlockData 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;
Expand All @@ -337,7 +338,8 @@ void saveUniversalData(UUID uuid, String sfId, Set<UniversalDataTrait> traits) {
}
}

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

if (toRemove == null) {
return;
Expand All @@ -351,7 +353,7 @@ void saveUniversalData(UUID uuid, String sfId, Set<UniversalDataTrait> traits) {
toRemove.setPendingRemove(true);
removeUniversalBlockDirectly(uuid);

var menu = ubd.getMenu();
UniversalMenu menu = ubd.getMenu();
if (menu != null) {
menu.lock();
}
Expand Down Expand Up @@ -488,19 +490,23 @@ public void getUniversalBlockData(UUID uuid, IAsyncReadCallback<SlimefunUniversa
public SlimefunUniversalBlockData getUniversalBlockDataFromCache(UUID uuid) {
var cache = loadedUniversalData.get(uuid);

return cache == null
? getUniversalBlockData(uuid)
: (cache instanceof SlimefunUniversalBlockData ubd ? ubd : null);
if (cache instanceof SlimefunUniversalBlockData ubd) {
return ubd;
} else {
return null;
}
}

/**
* Get slimefun universal data from cache by location
*
* @param l Slimefun block location {@link Location}
*/
public Optional<SlimefunUniversalBlockData> getUniversalBlockDataFromCache(Location l) { 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))
&& ubd.getLastPresent() != null
&& l.equals(ubd.getLastPresent().toLocation()))
.map(data -> (SlimefunUniversalBlockData) data)
.findFirst();
}
Expand Down Expand Up @@ -1274,11 +1280,16 @@ private void migrateUniversalData(
return;
}

var universalData = createUniversalBlockData(l, sfId);
SlimefunUniversalBlockData universalData = createUniversalBlock(l, sfId);

Slimefun.runSync(
() -> Slimefun.getBlockDataService()
.updateUniversalDataUUID(l.getBlock(), String.valueOf(universalData.getUUID())),
() -> {
if (BlockDataService
.isTileEntity(l.getBlock().getType())) {
Slimefun.getBlockDataService()
.updateUniversalDataUUID(l.getBlock(), String.valueOf(universalData.getUUID()));
}
},
10L);

kvData.forEach(recordSet -> universalData.setData(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ public void run() {
return;
}

if (Bukkit.getOfflinePlayer(UUID.fromString(pUuid)).isOnline()) {
if (Bukkit.getOfflinePlayer(UUID.fromString(pUuid)).isConnected()) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import com.xzavier0722.mc.plugin.slimefun4.storage.controller.attributes.UniversalDataTrait;
import com.xzavier0722.mc.plugin.slimefun4.storage.util.LocationUtils;
import io.github.bakedlibs.dough.blocks.BlockPosition;

import java.util.UUID;
import java.util.logging.Level;

import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
import org.bukkit.Location;

public class SlimefunUniversalBlockData extends SlimefunUniversalData {
Expand All @@ -19,6 +20,7 @@ public SlimefunUniversalBlockData(UUID uuid, String sfId, BlockPosition present)
super(uuid, sfId);

this.lastPresent = present;
setTraitData(UniversalDataTrait.BLOCK, LocationUtils.locationToString(lastPresent.toLocation()));
}

public SlimefunUniversalBlockData(UUID uuid, String sfId, Location present) {
Expand All @@ -30,6 +32,10 @@ public void setLastPresent(BlockPosition lastPresent) {
this.lastPresent = lastPresent;
}

public void setLastPresent(Location l) {
setLastPresent(new BlockPosition(l));
}

public BlockPosition getLastPresent() {
if (lastPresent != null) {
return lastPresent;
Expand All @@ -38,7 +44,8 @@ public BlockPosition getLastPresent() {
var data = getData("location");

if (data == null) {
throw new IllegalArgumentException("UniversalBlockData missing location data");
Slimefun.logger().log(Level.WARNING.WARNING, "UniversalBlockData [" + getUUID() + "] missing location data");
return null;
}

lastPresent = new BlockPosition(LocationUtils.toLocation(data));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.github.thebusybiscuit.slimefun4.core.services;

import com.xzavier0722.mc.plugin.slimefun4.storage.controller.SlimefunBlockData;
import com.xzavier0722.mc.plugin.slimefun4.storage.controller.SlimefunUniversalBlockData;
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
import io.github.thebusybiscuit.slimefun4.utils.tags.SlimefunTag;

Expand Down Expand Up @@ -111,7 +113,17 @@ public Optional<UUID> getUniversalDataUUID(Block b) {

return uuid.map(data -> {
try {
return UUID.fromString(data);
UUID uniId = UUID.fromString(data);

SlimefunUniversalBlockData uniData =
Slimefun.getDatabaseManager().getBlockDataController().getUniversalBlockDataFromCache(uniId);

// Auto fix missing location
if (uniData != null && uniData.getLastPresent() == null) {
uniData.setLastPresent(b.getLocation());
}

return uniId;
} catch (IllegalArgumentException e) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.xzavier0722.mc.plugin.slimefun4.storage.controller.BlockDataController;
import com.xzavier0722.mc.plugin.slimefun4.storage.controller.SlimefunBlockData;
import com.xzavier0722.mc.plugin.slimefun4.storage.controller.SlimefunUniversalBlockData;
import com.xzavier0722.mc.plugin.slimefun4.storage.controller.attributes.UniversalBlock;
import com.xzavier0722.mc.plugin.slimefun4.storage.util.StorageCacheUtils;
import io.github.thebusybiscuit.slimefun4.api.events.ExplosiveToolBreakBlocksEvent;
import io.github.thebusybiscuit.slimefun4.api.events.SlimefunBlockBreakEvent;
Expand Down Expand Up @@ -141,12 +142,18 @@ public void onBlockPlace(BlockPlaceEvent e) {
Slimefun.getBlockDataService().setBlockData(block, sfItem.getId());
}

var data = Slimefun.getDatabaseManager()
.getBlockDataController()
.createBlock(block.getLocation(), sfItem.getId());
if (sfItem instanceof UniversalBlock) {
SlimefunUniversalBlockData data = Slimefun.getDatabaseManager()
.getBlockDataController()
.createUniversalBlock(block.getLocation(), sfItem.getId());

if (data instanceof SlimefunUniversalBlockData) {
Slimefun.getBlockDataService().updateUniversalDataUUID(block, data.getKey());
if (Slimefun.getBlockDataService().isTileEntity(block.getType())) {
Slimefun.getBlockDataService().updateUniversalDataUUID(block, data.getKey());
}
} else {
Slimefun.getDatabaseManager()
.getBlockDataController()
.createBlock(block.getLocation(), sfItem.getId());
}

sfItem.callItemHandler(BlockPlaceHandler.class, handler -> handler.onPlayerPlace(e));
Expand Down

0 comments on commit ac05b89

Please sign in to comment.