From c4d750e0f3b9e7ab3402aa4fdaddc8d1666cb294 Mon Sep 17 00:00:00 2001 From: turikhay Date: Wed, 7 Sep 2022 21:50:16 +0300 Subject: [PATCH] Switch to numeric world IDs --- .../bungee/WorldIdPacketHandler.java | 2 +- .../mc/mapmodcompanion/worldid/WorldId.java | 101 +++++++++++++++--- .../spigot/CompanionSpigot.java | 4 + .../spigot/WorldIdHandler.java | 8 +- 4 files changed, 98 insertions(+), 17 deletions(-) diff --git a/bungee/src/main/java/com/turikhay/mc/mapmodcompanion/bungee/WorldIdPacketHandler.java b/bungee/src/main/java/com/turikhay/mc/mapmodcompanion/bungee/WorldIdPacketHandler.java index 43be38c..67439a6 100644 --- a/bungee/src/main/java/com/turikhay/mc/mapmodcompanion/bungee/WorldIdPacketHandler.java +++ b/bungee/src/main/java/com/turikhay/mc/mapmodcompanion/bungee/WorldIdPacketHandler.java @@ -20,6 +20,6 @@ public WorldId tryRead(byte[] data) { @Override public WorldId getId(Server server) { - return new WorldId(server.getInfo().getName()); + return WorldId.textual(server.getInfo().getName()); } } diff --git a/common/src/main/java/com/turikhay/mc/mapmodcompanion/worldid/WorldId.java b/common/src/main/java/com/turikhay/mc/mapmodcompanion/worldid/WorldId.java index ac458f9..54af78f 100644 --- a/common/src/main/java/com/turikhay/mc/mapmodcompanion/worldid/WorldId.java +++ b/common/src/main/java/com/turikhay/mc/mapmodcompanion/worldid/WorldId.java @@ -5,26 +5,27 @@ import javax.annotation.Nullable; import java.io.*; import java.nio.charset.StandardCharsets; +import java.util.Objects; -public class WorldId implements IdMessagePacket { +public /* sealed */ abstract class WorldId implements IdMessagePacket { private static final int MAGIC_MARKER = 42; - private final String id; - - public WorldId(String id) { - this.id = id; - } - @Override public WorldId combineWith(WorldId packet) { - return new WorldId(packet.id + '_' + id); + if (this instanceof Numeric || packet instanceof Numeric) { + // combining with numeric id always "pollutes" the result + return new WorldId.Numeric( + Objects.hash(getNumericId(), packet.getNumericId()) + ); + } + return new WorldId.Textual(packet.getStringId() + '_' + getStringId()); } @Override public void constructPacket(DataOutputStream out) throws IOException { out.writeByte(0); // packetId out.writeByte(MAGIC_MARKER); // 42 (literally) - byte[] data = id.getBytes(StandardCharsets.UTF_8); + byte[] data = getStringId().getBytes(StandardCharsets.UTF_8); out.write(data.length); // length out.write(data); // UTF } @@ -45,16 +46,86 @@ public static WorldId tryRead(byte[] data) { if (read < length) { return null; } - return new WorldId(new String(buf, StandardCharsets.UTF_8)); + String id = new String(buf, StandardCharsets.UTF_8); + Numeric possiblyNumeric = Numeric.tryNumeric(id); + if (possiblyNumeric != null) { + return possiblyNumeric; + } + return new Textual(id); } catch (IOException ignored) { } return null; } - @Override - public String toString() { - return "WorldId{" + - "id='" + id + '\'' + - '}'; + public static WorldId textual(String id) { + return new Textual(id); + } + + public static WorldId numeric(int id) { + return new Numeric(id); + } + + protected abstract int getNumericId(); + protected abstract String getStringId(); + + static class Numeric extends WorldId { + private final int id; + + public Numeric(int id) { + this.id = id; + } + + @Override + protected int getNumericId() { + return id; + } + + @Override + protected String getStringId() { + return String.valueOf(id); + } + + @Override + public String toString() { + return "WorldId.Numeric{" + + "id=" + id + + '}'; + } + + @Nullable + public static Numeric tryNumeric(String value) { + int numeric; + try { + numeric = Integer.parseInt(value); + } catch (NumberFormatException ignored) { + return null; + } + return new Numeric(numeric); + } + } + + static class Textual extends WorldId { + private final String id; + + public Textual(String id) { + this.id = id; + } + + @Override + protected int getNumericId() { + return id.hashCode(); + } + + @Override + protected String getStringId() { + return id; + } + + @Override + public String toString() { + return "WorldId.Textual{" + + "id='" + id + '\'' + + '}'; + } } } diff --git a/spigot/src/main/java/com/turikhay/mc/mapmodcompanion/spigot/CompanionSpigot.java b/spigot/src/main/java/com/turikhay/mc/mapmodcompanion/spigot/CompanionSpigot.java index d810300..3622a93 100644 --- a/spigot/src/main/java/com/turikhay/mc/mapmodcompanion/spigot/CompanionSpigot.java +++ b/spigot/src/main/java/com/turikhay/mc/mapmodcompanion/spigot/CompanionSpigot.java @@ -15,6 +15,10 @@ public class CompanionSpigot extends JavaPlugin implements Listener { System.getProperty(CompanionSpigot.class.getPackage().getName() + ".defaultId", "false") ); + public static final boolean USE_TEXTUAL_WORLD_ID = Boolean.parseBoolean( + System.getProperty(CompanionSpigot.class.getPackage().getName() + ".useTextualId", "false") + ); + List> handlers = Arrays.asList( new XaerosHandler(this), new WorldIdHandler(this) diff --git a/spigot/src/main/java/com/turikhay/mc/mapmodcompanion/spigot/WorldIdHandler.java b/spigot/src/main/java/com/turikhay/mc/mapmodcompanion/spigot/WorldIdHandler.java index ebfa76f..ce2e944 100644 --- a/spigot/src/main/java/com/turikhay/mc/mapmodcompanion/spigot/WorldIdHandler.java +++ b/spigot/src/main/java/com/turikhay/mc/mapmodcompanion/spigot/WorldIdHandler.java @@ -9,6 +9,7 @@ import java.util.Arrays; import java.util.Locale; +import java.util.UUID; import static com.turikhay.mc.mapmodcompanion.worldid.WorldIdCompanion.WORLD_ID_CHANNEL_NAME; import static com.turikhay.mc.mapmodcompanion.worldid.WorldIdCompanion.WORLD_ID_PACKET_DELAY; @@ -36,7 +37,12 @@ public void scheduleLevelIdPacket(Runnable r, EventSource source) { @Override public WorldId getId(World world) { - return new WorldId(world.getUID().toString()); + UUID uuid = world.getUID(); + if (CompanionSpigot.USE_TEXTUAL_WORLD_ID) { + return WorldId.textual(uuid.toString()); + } else { + return WorldId.numeric(uuid.hashCode()); + } } @Override