add World context to Sponge

This commit is contained in:
Aikar
2017-07-03 18:39:33 -05:00
parent bafbef5358
commit 60821e6c00
@@ -27,8 +27,10 @@ import co.aikar.commands.annotation.Optional;
import co.aikar.commands.contexts.CommandResultSupplier;
import co.aikar.commands.contexts.OnlinePlayer;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.world.World;
import java.util.HashSet;
import java.util.Set;
@@ -79,6 +81,20 @@ public class SpongeCommandContexts extends CommandContexts<SpongeCommandExecutio
}
return players.toArray(new OnlinePlayer[players.size()]);
});
registerIssuerAwareContext(World.class, (c) -> {
String firstArg = c.getFirstArg();
java.util.Optional<World> world = firstArg != null ? Sponge.getServer().getWorld(firstArg) : java.util.Optional.empty();
if (world.isPresent()) {
c.popFirstArg();
}
if (!world.isPresent() && c.getSource() instanceof Player) {
world = java.util.Optional.of(((Player) c.getSource()).getWorld());
}
if (!world.isPresent()) {
throw new InvalidCommandArgument(MinecraftMessageKeys.INVALID_WORLD);
}
return world.get();
});
}
@Nullable