From f8c4f23658a18b356087ae8a973fa04de83dbd64 Mon Sep 17 00:00:00 2001 From: Kenzie Togami Date: Sun, 14 Apr 2019 01:30:40 -0700 Subject: [PATCH] Partial work on biome commands, need logging replacement --- .../worldedit/command/BiomeCommands.java | 74 ++++++++----------- .../CommandPermissionsConditionGenerator.java | 7 +- .../extension/platform/CommandManager.java | 13 ++-- .../platform/CommandManagerCallable.java | 32 ++++---- 4 files changed, 60 insertions(+), 66 deletions(-) diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/BiomeCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/BiomeCommands.java index f3d9e9694..a1df6dcb5 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/BiomeCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/BiomeCommands.java @@ -19,17 +19,11 @@ package com.sk89q.worldedit.command; -import static com.google.common.base.Preconditions.checkNotNull; -import static com.sk89q.minecraft.util.commands.Logging.LogMode.REGION; - -import com.sk89q.minecraft.util.commands.Command; -import com.sk89q.minecraft.util.commands.CommandContext; -import com.sk89q.minecraft.util.commands.CommandPermissions; -import com.sk89q.minecraft.util.commands.Logging; import com.sk89q.worldedit.EditSession; import com.sk89q.worldedit.LocalSession; import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.WorldEditException; +import com.sk89q.worldedit.command.util.CommandPermissions; import com.sk89q.worldedit.entity.Player; import com.sk89q.worldedit.extension.platform.Capability; import com.sk89q.worldedit.function.FlatRegionFunction; @@ -46,11 +40,14 @@ import com.sk89q.worldedit.regions.FlatRegion; import com.sk89q.worldedit.regions.Region; import com.sk89q.worldedit.regions.Regions; import com.sk89q.worldedit.util.Location; -import com.sk89q.worldedit.util.command.binding.Switch; import com.sk89q.worldedit.world.World; import com.sk89q.worldedit.world.biome.BiomeData; import com.sk89q.worldedit.world.biome.BiomeType; import com.sk89q.worldedit.world.registry.BiomeRegistry; +import org.enginehub.piston.annotation.Command; +import org.enginehub.piston.annotation.CommandContainer; +import org.enginehub.piston.annotation.param.Arg; +import org.enginehub.piston.annotation.param.Switch; import java.util.Collection; import java.util.HashSet; @@ -59,33 +56,27 @@ import java.util.Set; /** * Implements biome-related commands such as "/biomelist". */ +@CommandContainer public class BiomeCommands { - private final WorldEdit worldEdit; - /** * Create a new instance. - * - * @param worldEdit reference to WorldEdit */ - public BiomeCommands(WorldEdit worldEdit) { - checkNotNull(worldEdit); - this.worldEdit = worldEdit; + public BiomeCommands() { } @Command( - aliases = { "biomelist", "biomels" }, - usage = "[page]", - desc = "Gets all biomes available.", - max = 1 + name = "biomelist", + aliases = { "biomels" }, + desc = "Gets all biomes available." ) @CommandPermissions("worldedit.biome.list") - public void biomeList(Player player, CommandContext args) throws WorldEditException { - int page; + public void biomeList(Player player, + @Arg(desc = "Page number.", def = "0") int page) throws WorldEditException { int offset; int count = 0; - if (args.argsLength() == 0 || (page = args.getInteger(0)) < 2) { + if (page < 2) { page = 1; offset = 0; } else { @@ -115,24 +106,24 @@ public class BiomeCommands { } @Command( - aliases = { "biomeinfo" }, - flags = "pt", + name = "biomeinfo", desc = "Get the biome of the targeted block.", - help = - "Get the biome of the block.\n" + - "By default use all the blocks contained in your selection.\n" + - "-t use the block you are looking at.\n" + - "-p use the block you are currently in", - max = 0 + descFooter = "By default, uses all blocks in your selection." ) @CommandPermissions("worldedit.biome.info") - public void biomeInfo(Player player, LocalSession session, CommandContext args) throws WorldEditException { + public void biomeInfo(Player player, LocalSession session, + @Switch( + name = 't', desc="Use the block you are looking at." + ) boolean useLineOfSight, + @Switch( + name = 'p', desc="Use the block you are currently in." + ) boolean usePosition) throws WorldEditException { BiomeRegistry biomeRegistry = WorldEdit.getInstance().getPlatformManager() .queryCapability(Capability.GAME_HOOKS).getRegistries().getBiomeRegistry(); Set biomes = new HashSet<>(); String qualifier; - if (args.hasFlag('t')) { + if (useLineOfSight) { Location blockPosition = player.getBlockTrace(300); if (blockPosition == null) { player.printError("No block in sight!"); @@ -143,7 +134,7 @@ public class BiomeCommands { biomes.add(biome); qualifier = "at line of sight point"; - } else if (args.hasFlag('p')) { + } else if (usePosition) { BiomeType biome = player.getWorld().getBiome(player.getLocation().toVector().toBlockPoint().toBlockVector2()); biomes.add(biome); @@ -177,18 +168,15 @@ public class BiomeCommands { } @Command( - aliases = { "/setbiome" }, - usage = "", - flags = "p", - desc = "Sets the biome of the player's current block or region.", - help = - "Set the biome of the region.\n" + - "By default use all the blocks contained in your selection.\n" + - "-p use the block you are currently in" + name = "/setbiome", + desc = "Sets the biome of the player's current block or region.", + descFooter = "By default, uses all the blocks in your selection" ) - @Logging(REGION) +// @Logging(REGION) @CommandPermissions("worldedit.biome.set") - public void setBiome(Player player, LocalSession session, EditSession editSession, BiomeType target, @Switch('p') boolean atPosition) throws WorldEditException { + public void setBiome(Player player, LocalSession session, EditSession editSession, + BiomeType target, + @Switch(name = 'p', desc = "Use your current position") boolean atPosition) throws WorldEditException { World world = player.getWorld(); Region region; Mask mask = editSession.getMask(); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/CommandPermissionsConditionGenerator.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/CommandPermissionsConditionGenerator.java index d00d286d6..84011e0ff 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/CommandPermissionsConditionGenerator.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/util/CommandPermissionsConditionGenerator.java @@ -41,9 +41,8 @@ public class CommandPermissionsConditionGenerator implements CommandConditionGen CommandPermissions annotation = commandMethod.getAnnotation(CommandPermissions.class); checkNotNull(annotation, "Annotation is missing from commandMethod"); Set permissions = ImmutableSet.copyOf(annotation.value()); - return p -> { - Actor actor = p.injectedValue(ACTOR_KEY); - return permissions.stream().anyMatch(actor::hasPermission); - }; + return p -> p.injectedValue(ACTOR_KEY) + .map(actor -> permissions.stream().anyMatch(actor::hasPermission)) + .orElse(false); } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/CommandManager.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/CommandManager.java index 9a2a9da78..da697d182 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/CommandManager.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/CommandManager.java @@ -145,7 +145,7 @@ public final class CommandManager { dispatcher = new CommandGraph() .builder(builder) .commands() - .registerMethods(new BiomeCommands(worldEdit)) + .registerMethods(new BiomeCommands()) .registerMethods(new ChunkCommands(worldEdit)) .registerMethods(new ClipboardCommands(worldEdit)) .registerMethods(new GeneralCommands(worldEdit)) @@ -198,11 +198,12 @@ public final class CommandManager { desc.setPermissions(ImmutableList.of()); org.enginehub.piston.CommandManager manager = DefaultCommandManagerService.getInstance() .newCommandManager(); - new SchematicCommandsRegistration( - manager, - new SchematicCommands(worldEdit), - new CommandPermissionsConditionGenerator() - ); + SchematicCommandsRegistration.builder() + .commandManager(manager) + .containerInstance(new SchematicCommands(worldEdit)) + .commandPermissionsConditionGenerator( + new CommandPermissionsConditionGenerator() + ).build(); return new CommandManagerCallable(worldEdit, manager, desc); } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/CommandManagerCallable.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/CommandManagerCallable.java index bb6079713..fe22518ef 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/CommandManagerCallable.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/CommandManagerCallable.java @@ -33,6 +33,7 @@ import org.enginehub.piston.CommandManager; import java.util.Collections; import java.util.List; +import java.util.Optional; /** * Hack to get {@link CommandManager} working under {@link CommandCallable}. @@ -51,20 +52,25 @@ public class CommandManagerCallable implements CommandCallable { @Override public Object call(String arguments, CommandLocals locals, String[] parentCommands) throws CommandException { - manager.injectValue(Key.get(Actor.class), () -> locals.get(Actor.class)); - manager.injectValue(Key.get(Player.class), () -> getPlayer(locals)); - manager.injectValue(Key.get(LocalSession.class), () -> { - Player sender = getPlayer(locals); - return worldEdit.getSessionManager().get(sender); - }); - manager.injectValue(Key.get(EditSession.class), () -> { - Player sender = getPlayer(locals); - LocalSession session = worldEdit.getSessionManager().get(sender); - EditSession editSession = session.createEditSession(sender); - editSession.enableStandardMode(); - session.tellVersion(sender); - return editSession; + manager.injectValue(Key.get(Actor.class), access -> Optional.of(locals.get(Actor.class))); + manager.injectValue(Key.get(Player.class), access -> { + Actor actor = locals.get(Actor.class); + return actor instanceof Player ? Optional.of(((Player) actor)) : Optional.empty(); }); + manager.injectValue(Key.get(LocalSession.class), access -> + access.injectedValue(Key.get(Player.class)) + .map(worldEdit.getSessionManager()::get) + ); + manager.injectValue(Key.get(EditSession.class), access -> + access.injectedValue(Key.get(Player.class)) + .map(sender -> { + LocalSession session = worldEdit.getSessionManager().get(sender); + EditSession editSession = session.createEditSession(sender); + editSession.enableStandardMode(); + session.tellVersion(sender); + return editSession; + }) + ); return manager.execute(Splitter.on(' ').splitToList(arguments)); }