Skip to content

Commit 2c8b76c

Browse files
committed
Detect default world and, if found, only send its ID
1 parent 29c5b65 commit 2c8b76c

3 files changed

Lines changed: 119 additions & 6 deletions

File tree

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,39 @@
11
package com.turikhay.mc.mapmodcompanion.spigot;
22

3+
import org.bukkit.World;
34
import org.bukkit.event.Listener;
45
import org.bukkit.plugin.java.JavaPlugin;
56

6-
import java.util.Arrays;
7-
import java.util.List;
7+
import java.util.*;
88

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

14+
public static final boolean DISABLE_DEFAULT_WORLD_ID = Boolean.parseBoolean(
15+
System.getProperty(CompanionSpigot.class.getPackage().getName() + ".defaultId", "false")
16+
);
17+
1418
List<Handler<?>> handlers = Arrays.asList(
1519
new XaerosHandler(this),
1620
new WorldIdHandler(this)
1721
);
1822

23+
DefaultWorld defaultWorld;
24+
1925
@Override
2026
public void onEnable() {
27+
if (DISABLE_DEFAULT_WORLD_ID) {
28+
getLogger().info("Plugin will not use default world ID for every world");
29+
defaultWorld = DefaultWorld.empty();
30+
} else {
31+
defaultWorld = DefaultWorld.detectDefaultWorld(this);
32+
}
2133
handlers.forEach(Handler::init);
2234
}
35+
36+
Optional<World> getDefaultWorld() {
37+
return defaultWorld.optional();
38+
}
2339
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.turikhay.mc.mapmodcompanion.spigot;
2+
3+
import org.bukkit.World;
4+
5+
import java.util.*;
6+
7+
interface DefaultWorld {
8+
Optional<World> optional();
9+
10+
static Empty empty() {
11+
return new Empty();
12+
}
13+
14+
static DefaultWorld detectDefaultWorld(CompanionSpigot plugin) {
15+
List<World> worlds = plugin.getServer().getWorlds();
16+
if (worlds.isEmpty()) {
17+
throw new RuntimeException("world list is empty");
18+
}
19+
Set<World.Environment> expectedEnv = new HashSet<>(Arrays.asList(
20+
World.Environment.NORMAL,
21+
World.Environment.NETHER,
22+
World.Environment.THE_END
23+
));
24+
for (World world : worlds) {
25+
World.Environment env = world.getEnvironment();
26+
boolean isExpected = expectedEnv.remove(env);
27+
if (!isExpected) {
28+
// Non-default server configuration
29+
plugin.getLogger().severe("Unexpected world: " + world);
30+
plugin.getLogger().severe("For every world plugin will now send their unique IDs");
31+
return new Empty();
32+
}
33+
}
34+
World defaultWorld = worlds.get(0);
35+
if (CompanionSpigot.ENABLE_LOGGING) {
36+
plugin.getLogger().info("Selected default world: " + defaultWorld + " (" + defaultWorld.getUID() + ")");
37+
}
38+
return new ByUUID(plugin, defaultWorld.getUID());
39+
}
40+
41+
class ByUUID implements DefaultWorld {
42+
private final CompanionSpigot plugin;
43+
private final UUID uuid;
44+
45+
public ByUUID(CompanionSpigot plugin, UUID uuid) {
46+
this.plugin = plugin;
47+
this.uuid = uuid;
48+
}
49+
50+
@Override
51+
public Optional<World> optional() {
52+
World world = plugin.getServer().getWorld(uuid);
53+
if (world == null) {
54+
plugin.getLogger().warning("Couldn't find world " + uuid);
55+
return Optional.empty();
56+
}
57+
return Optional.of(world);
58+
}
59+
}
60+
61+
class Empty implements DefaultWorld {
62+
@Override
63+
public Optional<World> optional() {
64+
return Optional.empty();
65+
}
66+
}
67+
}

spigot/src/main/java/com/turikhay/mc/mapmodcompanion/spigot/Handler.java

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public abstract class Handler<Id extends IdMessagePacket<?>> implements Listener
1616
private final String channelName;
1717
protected final CompanionSpigot plugin;
1818

19+
private IdRef<Id> defaultId;
20+
1921
public Handler(String channelName, CompanionSpigot plugin) {
2022
this.channelName = channelName;
2123
this.plugin = plugin;
@@ -24,6 +26,7 @@ public Handler(String channelName, CompanionSpigot plugin) {
2426
public void init() {
2527
plugin.getServer().getMessenger().registerOutgoingPluginChannel(plugin, channelName);
2628
plugin.getServer().getPluginManager().registerEvents(this, plugin);
29+
defaultId = plugin.getDefaultWorld().map(world -> IdRef.of(getId(world))).orElse(null);
2730
}
2831

2932
@EventHandler(priority = EventPriority.MONITOR)
@@ -39,15 +42,20 @@ public void onWorldChanged(PlayerChangedWorldEvent event) {
3942
public void sendLevelId(Player player, EventSource source) {
4043
scheduleLevelIdPacket(
4144
() -> {
42-
Id id = getId(player.getWorld());
43-
byte[] data = IdMessagePacket.bytesPacket(id);
45+
IdRef<Id> idRef;
46+
if (defaultId == null) {
47+
idRef = IdRef.of(getId(player.getWorld()));
48+
} else {
49+
idRef = defaultId;
50+
}
4451
if (CompanionSpigot.ENABLE_LOGGING) {
4552
plugin.getLogger().info(String.format(Locale.ROOT,
4653
"Sending world id to %s (channel: %s): %s. Data: %s",
47-
player.getName(), channelName, id, Arrays.toString(data)
54+
player.getName(), channelName, idRef.id,
55+
Arrays.toString(idRef.data)
4856
));
4957
}
50-
player.sendPluginMessage(plugin, channelName, data);
58+
player.sendPluginMessage(plugin, channelName, idRef.data);
5159
},
5260
source
5361
);
@@ -61,4 +69,26 @@ public enum EventSource {
6169
WORLD_CHANGE,
6270
PLUGIN_MESSAGE
6371
}
72+
73+
private static class IdRef<Id extends IdMessagePacket<?>> {
74+
private final Id id;
75+
private final byte[] data;
76+
77+
private IdRef(Id id, byte[] data) {
78+
this.id = id;
79+
this.data = data;
80+
}
81+
82+
@Override
83+
public String toString() {
84+
return "DefaultId{" +
85+
"id=" + id +
86+
", data=" + Arrays.toString(data) +
87+
'}';
88+
}
89+
90+
private static <Id extends IdMessagePacket<?>> IdRef<Id> of(Id id) {
91+
return new IdRef<>(id, IdMessagePacket.bytesPacket(id));
92+
}
93+
}
6494
}

0 commit comments

Comments
 (0)