Skip to content

Commit

Permalink
fix: inconsistent location converter used
Browse files Browse the repository at this point in the history
  • Loading branch information
StarWishsama committed Dec 26, 2024
1 parent ea6ba00 commit 4da3707
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.xzavier0722.mc.plugin.slimefun4.storage.util.LocationUtils;
import io.github.bakedlibs.dough.blocks.BlockPosition;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.Location;

public class SlimefunUniversalBlockData extends SlimefunUniversalData {
Expand All @@ -24,11 +25,11 @@ public SlimefunUniversalBlockData(UUID uuid, String sfId, Location present) {
}

public void initLastPresent() {
setTraitData(UniversalDataTrait.BLOCK, LocationUtils.locationToString(lastPresent.toLocation()));
setTraitData(UniversalDataTrait.BLOCK, LocationUtils.getLocKey(lastPresent.toLocation()));
}

public void setLastPresent(BlockPosition lastPresent) {
setTraitData(UniversalDataTrait.BLOCK, LocationUtils.locationToString(lastPresent.toLocation()));
setTraitData(UniversalDataTrait.BLOCK, LocationUtils.getLocKey(lastPresent.toLocation()));
this.lastPresent = lastPresent;
}

Expand All @@ -41,7 +42,7 @@ public BlockPosition getLastPresent() {

if (lastPresent != null) {
if (data == null) {
setTraitData(UniversalDataTrait.BLOCK, LocationUtils.locationToString(lastPresent.toLocation()));
setTraitData(UniversalDataTrait.BLOCK, LocationUtils.getLocKey(lastPresent.toLocation()));
}

return lastPresent;
Expand All @@ -51,7 +52,29 @@ public BlockPosition getLastPresent() {
return null;
}

lastPresent = new BlockPosition(LocationUtils.toLocation(data));
try {
lastPresent = new BlockPosition(LocationUtils.toLocation(data));
} catch (RuntimeException e) {
if (data.isEmpty()) {
return null;
}

// 修复因使用不一致的文本转换导致的位置无法解析
try {
var lArr = data.split(",");
var bp = new BlockPosition(
Bukkit.getWorld(lArr[0].replace("[world=", "")),
(int) Double.parseDouble(lArr[1].replace("x=", "")),
(int) Double.parseDouble(lArr[2].replace("y=", "")),
(int) Double.parseDouble(lArr[3].replace("z=", "").replace("]", "")));

setTraitData(UniversalDataTrait.BLOCK, LocationUtils.getLocKey(bp.toLocation()));

return bp;
} catch (Exception x) {
throw new RuntimeException("Unable to fix location " + data + ", it might be broken", x);
}
}

return lastPresent;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,24 @@
import org.bukkit.World;

public class LocationUtils {
/**
* 将 {@link Location} 转换为数据库中使用的
* 标准格式位置信息
*
* @param l {@link Location}
* @return 标准化后的位置信息字符串
*/
public static String getLocKey(Location l) {
return l.getWorld().getName() + ";" + l.getBlockX() + ":" + l.getBlockY() + ":" + l.getBlockZ();
}

/**
* 将 {@link Chunk} 转换为数据库中使用的
* 标准格式区块信息
*
* @param chunk {@link Chunk}
* @return 标准化后的区块信息字符串
*/
public static String getChunkKey(Chunk chunk) {
return chunk.getWorld().getName() + ";" + chunk.getX() + ":" + chunk.getZ();
}
Expand Down Expand Up @@ -59,9 +73,17 @@ public static boolean isSameWorld(World w1, World w2) {
return w1.getName().equals(w2.getName());
}

/**
* 将位置转换为易读的文本
*
* 注意: 请不要将其用于数据库转换过程中!
*
* @param location 位置
* @return 易读的位置文本
*/
public static String locationToString(Location location) {
if (location == null) {
return "";
return "null";
}

return "[world="
Expand Down

0 comments on commit 4da3707

Please sign in to comment.