Skip to content

Commit

Permalink
Switch to numeric world IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
turikhay committed Sep 7, 2022
1 parent b968ddf commit c4d750e
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<WorldId> {
public /* sealed */ abstract class WorldId implements IdMessagePacket<WorldId> {
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
}
Expand All @@ -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 + '\'' +
'}';
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Handler<?>> handlers = Arrays.asList(
new XaerosHandler(this),
new WorldIdHandler(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c4d750e

Please sign in to comment.