9
0
mirror of https://github.com/Xiao-MoMi/Custom-Crops.git synced 2025-12-28 03:19:15 +00:00

Code clean up

This commit is contained in:
XiaoMoMi
2024-08-13 15:40:26 +08:00
parent 8aac425480
commit 617c2b1609
8 changed files with 32 additions and 48 deletions

View File

@@ -26,7 +26,6 @@ import net.momirealms.customcrops.api.event.CropBreakEvent;
import net.momirealms.customcrops.api.event.CropPlantEvent;
import net.momirealms.customcrops.api.mechanic.item.Crop;
import net.momirealms.customcrops.api.mechanic.world.level.WorldCrop;
import net.momirealms.customcrops.api.util.LogUtils;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;

View File

@@ -33,7 +33,6 @@ import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.TimeUnit;
/**
* Represents a repository which contains {@link Dependency}s.

View File

@@ -17,8 +17,6 @@
package net.momirealms.customcrops.mechanic.action;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.events.PacketContainer;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.sound.Sound;
import net.kyori.adventure.text.Component;

View File

@@ -17,7 +17,6 @@
package net.momirealms.customcrops.mechanic.item.impl;
import com.saicone.rtag.item.ItemObject;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.ScoreComponent;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;

View File

@@ -62,12 +62,16 @@ public class WorldManagerImpl implements WorldManager, Listener {
this.plugin = plugin;
this.loadedWorlds = new ConcurrentHashMap<>();
this.worldSettingMap = new HashMap<>();
if (Bukkit.getPluginManager().isPluginEnabled("SlimeWorldManager")) {
try {
// to ignore the warning from asp
Class.forName("com.infernalsuite.aswm.api.SlimePlugin");
this.worldAdaptor = new SlimeWorldAdaptor(this, 1);
} else if (Bukkit.getPluginManager().isPluginEnabled("SlimeWorldPlugin")) {
this.worldAdaptor = new SlimeWorldAdaptor(this, 2);
} else {
this.worldAdaptor = new BukkitWorldAdaptor(this);
} catch (ClassNotFoundException e) {
if (Bukkit.getPluginManager().isPluginEnabled("SlimeWorldPlugin")) {
this.worldAdaptor = new SlimeWorldAdaptor(this, 2);
} else {
this.worldAdaptor = new BukkitWorldAdaptor(this);
}
}
}

View File

@@ -18,7 +18,6 @@
package net.momirealms.customcrops.mechanic.world.adaptor;
import com.flowpowered.nbt.*;
import com.infernalsuite.aswm.api.SlimePlugin;
import com.infernalsuite.aswm.api.events.LoadSlimeWorldEvent;
import com.infernalsuite.aswm.api.world.SlimeWorld;
import net.momirealms.customcrops.api.CustomCropsPlugin;
@@ -84,6 +83,18 @@ public class SlimeWorldAdaptor extends BukkitWorldAdaptor {
}
}
private CompoundMap createOrGetDataMap(SlimeWorld world) {
Optional<CompoundTag> optionalCompoundTag = world.getExtraData().getAsCompoundTag("customcrops");
CompoundMap ccDataMap;
if (optionalCompoundTag.isEmpty()) {
ccDataMap = new CompoundMap();
world.getExtraData().getValue().put(new CompoundTag("customcrops", ccDataMap));
} else {
ccDataMap = optionalCompoundTag.get().getValue();
}
return ccDataMap;
}
@EventHandler (ignoreCancelled = true)
public void onSlimeWorldLoad(LoadSlimeWorldEvent event) {
World world = Bukkit.getWorld(event.getSlimeWorld().getName());
@@ -103,14 +114,7 @@ public class SlimeWorldAdaptor extends BukkitWorldAdaptor {
super.saveInfoData(customCropsWorld);
return;
}
Optional<CompoundTag> optionalCompoundTag = slimeWorld.getExtraData().getAsCompoundTag("customcrops");
if (optionalCompoundTag.isEmpty()) {
LogUtils.warn("Failed to unload data for world " + customCropsWorld.getWorldName() + " because slime world format is incorrect.");
return;
}
CompoundMap ccDataMap = optionalCompoundTag.get().getValue();
CompoundMap ccDataMap = createOrGetDataMap(slimeWorld);
ccDataMap.put(new StringTag("world-info", gson.toJson(customCropsWorld.getInfoData())));
}
@@ -132,15 +136,7 @@ public class SlimeWorldAdaptor extends BukkitWorldAdaptor {
return;
}
Optional<CompoundTag> optionalCompoundTag = slimeWorld.getExtraData().getAsCompoundTag("customcrops");
CompoundMap ccDataMap;
if (optionalCompoundTag.isEmpty()) {
ccDataMap = new CompoundMap();
slimeWorld.getExtraData().getValue().put(new CompoundTag("customcrops", ccDataMap));
} else {
ccDataMap = optionalCompoundTag.get().getValue();
}
CompoundMap ccDataMap = createOrGetDataMap(slimeWorld);
String json = Optional.ofNullable(ccDataMap.get("world-info")).map(tag -> tag.getAsStringTag().get().getValue()).orElse(null);
WorldInfoData data = (json == null || json.equals("null")) ? WorldInfoData.empty() : gson.fromJson(json, WorldInfoData.class);
customCropsWorld.setInfoData(data);
@@ -167,13 +163,8 @@ public class SlimeWorldAdaptor extends BukkitWorldAdaptor {
return;
}
Optional<CompoundTag> optionalCompoundTag = slimeWorld.getExtraData().getAsCompoundTag("customcrops");
if (optionalCompoundTag.isEmpty()) {
LogUtils.warn("Failed to load data for " + chunkPos + " in world " + customCropsWorld.getWorldName() + " because slime world format is incorrect.");
return;
}
Tag<?> chunkTag = optionalCompoundTag.get().getValue().get(chunkPos.getAsString());
CompoundMap ccDataMap = createOrGetDataMap(slimeWorld);
Tag<?> chunkTag = ccDataMap.get(chunkPos.getAsString());
if (chunkTag == null) {
return;
}
@@ -191,31 +182,27 @@ public class SlimeWorldAdaptor extends BukkitWorldAdaptor {
@Override
public void saveChunkToCachedRegion(CustomCropsChunk customCropsChunk) {
CustomCropsWorld customCropsWorld = customCropsChunk.getCustomCropsWorld();
SlimeWorld slimeWorld = getSlimeWorld(customCropsChunk.getCustomCropsWorld().getWorldName());
SlimeWorld slimeWorld = getSlimeWorld(customCropsWorld.getWorldName());
if (slimeWorld == null) {
super.saveChunkToCachedRegion(customCropsChunk);
return;
}
Optional<CompoundTag> optionalCompoundTag = slimeWorld.getExtraData().getAsCompoundTag("customcrops");
if (optionalCompoundTag.isEmpty()) {
LogUtils.warn("Failed to save data for " + customCropsChunk.getChunkPos() + " in world " + customCropsWorld.getWorldName() + " because slime world format is incorrect.");
return;
}
CompoundMap ccDataMap = createOrGetDataMap(slimeWorld);
SerializableChunk serializableChunk = toSerializableChunk((CChunk) customCropsChunk);
if (Bukkit.isPrimaryThread()) {
if (serializableChunk.canPrune()) {
optionalCompoundTag.get().getValue().remove(customCropsChunk.getChunkPos().getAsString());
ccDataMap.remove(customCropsChunk.getChunkPos().getAsString());
} else {
optionalCompoundTag.get().getValue().put(chunkToTag(serializableChunk));
ccDataMap.put(chunkToTag(serializableChunk));
}
} else {
CustomCropsPlugin.get().getScheduler().runTaskSync(() -> {
if (serializableChunk.canPrune()) {
optionalCompoundTag.get().getValue().remove(customCropsChunk.getChunkPos().getAsString());
ccDataMap.remove(customCropsChunk.getChunkPos().getAsString());
} else {
optionalCompoundTag.get().getValue().put(chunkToTag(serializableChunk));
ccDataMap.put(chunkToTag(serializableChunk));
}
}, null);
}

View File

@@ -18,6 +18,7 @@ softdepend:
- RealisticSeasons
- AdvancedSeasons
- SlimeWorldManager
- SlimeWorldPlugin
- HuskClaims
- HuskTowns
- Residence