Skip to content

Commit

Permalink
Detect default world and, if found, only send its ID
Browse files Browse the repository at this point in the history
  • Loading branch information
turikhay committed Sep 7, 2022
1 parent 29c5b65 commit 2c8b76c
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,39 @@
package com.turikhay.mc.mapmodcompanion.spigot;

import org.bukkit.World;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.Arrays;
import java.util.List;
import java.util.*;

public class CompanionSpigot extends JavaPlugin implements Listener {
public static final boolean ENABLE_LOGGING = Boolean.parseBoolean(
System.getProperty(CompanionSpigot.class.getPackage().getName() + ".debug", "false")
);

public static final boolean DISABLE_DEFAULT_WORLD_ID = Boolean.parseBoolean(
System.getProperty(CompanionSpigot.class.getPackage().getName() + ".defaultId", "false")
);

List<Handler<?>> handlers = Arrays.asList(
new XaerosHandler(this),
new WorldIdHandler(this)
);

DefaultWorld defaultWorld;

@Override
public void onEnable() {
if (DISABLE_DEFAULT_WORLD_ID) {
getLogger().info("Plugin will not use default world ID for every world");
defaultWorld = DefaultWorld.empty();
} else {
defaultWorld = DefaultWorld.detectDefaultWorld(this);
}
handlers.forEach(Handler::init);
}

Optional<World> getDefaultWorld() {
return defaultWorld.optional();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.turikhay.mc.mapmodcompanion.spigot;

import org.bukkit.World;

import java.util.*;

interface DefaultWorld {
Optional<World> optional();

static Empty empty() {
return new Empty();
}

static DefaultWorld detectDefaultWorld(CompanionSpigot plugin) {
List<World> worlds = plugin.getServer().getWorlds();
if (worlds.isEmpty()) {
throw new RuntimeException("world list is empty");
}
Set<World.Environment> expectedEnv = new HashSet<>(Arrays.asList(
World.Environment.NORMAL,
World.Environment.NETHER,
World.Environment.THE_END
));
for (World world : worlds) {
World.Environment env = world.getEnvironment();
boolean isExpected = expectedEnv.remove(env);
if (!isExpected) {
// Non-default server configuration
plugin.getLogger().severe("Unexpected world: " + world);
plugin.getLogger().severe("For every world plugin will now send their unique IDs");
return new Empty();
}
}
World defaultWorld = worlds.get(0);
if (CompanionSpigot.ENABLE_LOGGING) {
plugin.getLogger().info("Selected default world: " + defaultWorld + " (" + defaultWorld.getUID() + ")");
}
return new ByUUID(plugin, defaultWorld.getUID());
}

class ByUUID implements DefaultWorld {
private final CompanionSpigot plugin;
private final UUID uuid;

public ByUUID(CompanionSpigot plugin, UUID uuid) {
this.plugin = plugin;
this.uuid = uuid;
}

@Override
public Optional<World> optional() {
World world = plugin.getServer().getWorld(uuid);
if (world == null) {
plugin.getLogger().warning("Couldn't find world " + uuid);
return Optional.empty();
}
return Optional.of(world);
}
}

class Empty implements DefaultWorld {
@Override
public Optional<World> optional() {
return Optional.empty();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public abstract class Handler<Id extends IdMessagePacket<?>> implements Listener
private final String channelName;
protected final CompanionSpigot plugin;

private IdRef<Id> defaultId;

public Handler(String channelName, CompanionSpigot plugin) {
this.channelName = channelName;
this.plugin = plugin;
Expand All @@ -24,6 +26,7 @@ public Handler(String channelName, CompanionSpigot plugin) {
public void init() {
plugin.getServer().getMessenger().registerOutgoingPluginChannel(plugin, channelName);
plugin.getServer().getPluginManager().registerEvents(this, plugin);
defaultId = plugin.getDefaultWorld().map(world -> IdRef.of(getId(world))).orElse(null);
}

@EventHandler(priority = EventPriority.MONITOR)
Expand All @@ -39,15 +42,20 @@ public void onWorldChanged(PlayerChangedWorldEvent event) {
public void sendLevelId(Player player, EventSource source) {
scheduleLevelIdPacket(
() -> {
Id id = getId(player.getWorld());
byte[] data = IdMessagePacket.bytesPacket(id);
IdRef<Id> idRef;
if (defaultId == null) {
idRef = IdRef.of(getId(player.getWorld()));
} else {
idRef = defaultId;
}
if (CompanionSpigot.ENABLE_LOGGING) {
plugin.getLogger().info(String.format(Locale.ROOT,
"Sending world id to %s (channel: %s): %s. Data: %s",
player.getName(), channelName, id, Arrays.toString(data)
player.getName(), channelName, idRef.id,
Arrays.toString(idRef.data)
));
}
player.sendPluginMessage(plugin, channelName, data);
player.sendPluginMessage(plugin, channelName, idRef.data);
},
source
);
Expand All @@ -61,4 +69,26 @@ public enum EventSource {
WORLD_CHANGE,
PLUGIN_MESSAGE
}

private static class IdRef<Id extends IdMessagePacket<?>> {
private final Id id;
private final byte[] data;

private IdRef(Id id, byte[] data) {
this.id = id;
this.data = data;
}

@Override
public String toString() {
return "DefaultId{" +
"id=" + id +
", data=" + Arrays.toString(data) +
'}';
}

private static <Id extends IdMessagePacket<?>> IdRef<Id> of(Id id) {
return new IdRef<>(id, IdMessagePacket.bytesPacket(id));
}
}
}

0 comments on commit 2c8b76c

Please sign in to comment.