Skip to content

Instantly share code, notes, and snippets.

@MarkL4YG
Last active April 10, 2017 20:25
Show Gist options
  • Save MarkL4YG/aebf0e604a997d82e5e0c337af4b5483 to your computer and use it in GitHub Desktop.
Save MarkL4YG/aebf0e604a997d82e5e0c337af4b5483 to your computer and use it in GitHub Desktop.
An example for why I think something is wrong.
//This is the working solution. Comments show where things get weird.
@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
// A lot of sh*t that doesn't actually matter
List<World> l = new ArrayList<World>();
l.addAll(Sponge.getServer().getWorlds());
List<Text> t = new ArrayList<Text>();
final String CHATDIV = new String(new char[40]).replace("\0", "+");
t.add(Text.of(TextColors.WHITE, CHATDIV));
t.add(Text.of(TextColors.WHITE, "Worlds: "));
// Go through all worlds obtained by #getWorlds()
// One would guess that those includes unloaded worlds due to ``World`` having a #isLoaded funtion.
// In fact. Those are only loaded worlds so no check of #isLoaded needed.
l.forEach(w -> t.add(Text.of(
TextActions.showText(Text.of("Click to teleport")),
TextActions.runCommand("/world tp " + w.getName()),
(w.isLoaded()) ? TextColors.GREEN : TextColors.RED, " - ", w.getName()
)));
List<WorldProperties> props = new ArrayList<WorldProperties>();
props.addAll(Sponge.getServer().getUnloadedWorlds());
// Go through all WoldProperties obtained by #getUnloadedWorlds()
// One would guess that those are only the unloaded worlds
// This is what actually happens:
// 1. They're only WorldProperties not ``World``s
// 2. #getWorld(<someUnloadedWorldsUUID>) returns Optional.empty()
// -> Impossible to (~get~)retrieve an actual ``World`` reference for an unloaded world?
// 3. The colletion also includes loaded worlds?!
props.stream()
// Skip all worlds that have a ``World`` reference thus appear to be loaded??
.filter(w -> !Sponge.getServer().getWorld(w.getUniqueId()).isPresent())
// Now we actually (seem to) have only the unloaded worlds.
.forEach(w -> t.add(Text.of(
TextActions.showText(Text.of("Click to teleport")),
TextActions.runCommand("/world tp " + w.getWorldName()), TextColors.RED, " - ", w.getWorldName()
)));
//Other stuff
t.add(Text.of(TextColors.WHITE, CHATDIV));
src.sendMessage(Text.joinWith(Text.of("\n"), t));
return CommandResult.success();
}
//This is what I think should actually be used.
@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
// A lot of sh*t that doesn't actually matter
List<World> l = new ArrayList<World>();
l.addAll(Sponge.getServer().getWorlds());
List<Text> t = new ArrayList<Text>();
final String CHATDIV = new String(new char[40]).replace("\0", "+");
t.add(Text.of(TextColors.WHITE, CHATDIV));
t.add(Text.of(TextColors.WHITE, "Worlds: "));
// Go through all worlds obtained by #getWorlds()
l.stream()
// Skip unloaded worlds
.filter(World::isLoaded)
.forEach(w -> t.add(Text.of(
TextActions.showText(Text.of("Click to teleport")),
TextActions.runCommand("/world tp " + w.getName()),
(w.isLoaded()) ? TextColors.GREEN : TextColors.RED, " - ", w.getName()
)));
List<WorldProperties> props = new ArrayList<WorldProperties>();
props.addAll(Sponge.getServer().getUnloadedWorlds());
// Go through all Worlds obtained by #getUnloadedWorlds()
props.stream()
// No need to filter worlds since they should be unloaded?
.forEach(w -> t.add(Text.of(
TextActions.showText(Text.of("Click to teleport")),
TextActions.runCommand("/world tp " + w.getWorldName()), TextColors.RED, " - ", w.getWorldName()
)));
//Other stuff
t.add(Text.of(TextColors.WHITE, CHATDIV));
src.sendMessage(Text.joinWith(Text.of("\n"), t));
return CommandResult.success();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment