-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Detect default world and, if found, only send its ID
- Loading branch information
Showing
3 changed files
with
119 additions
and
6 deletions.
There are no files selected for viewing
20 changes: 18 additions & 2 deletions
20
spigot/src/main/java/com/turikhay/mc/mapmodcompanion/spigot/CompanionSpigot.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
spigot/src/main/java/com/turikhay/mc/mapmodcompanion/spigot/DefaultWorld.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters