From 23279c007eab9e0897976861619751f6466786fc Mon Sep 17 00:00:00 2001 From: Kenzie Togami Date: Fri, 26 Apr 2019 01:38:06 -0700 Subject: [PATCH] Fix intialization, rework registration --- .../worldedit/command/ApplyBrushCommands.java | 27 ++++++++-- .../worldedit/command/PaintBrushCommands.java | 27 ++++++++-- .../command/factory/ItemUseFactory.java | 19 +++++++ .../command/factory/ReplaceFactory.java | 19 +++++++ .../command/factory/TreeGeneratorFactory.java | 19 +++++++ .../platform/PlatformCommandManager.java | 45 +++++++++------- .../worldedit/function/ItemUseFunction.java | 19 +++++++ .../command/CommandRegistrationHandler.java | 54 +++++++++++++++++++ .../worldedit/util/command/CommandUtil.java | 29 ---------- 9 files changed, 202 insertions(+), 56 deletions(-) create mode 100644 worldedit-core/src/main/java/com/sk89q/worldedit/util/command/CommandRegistrationHandler.java diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/ApplyBrushCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/ApplyBrushCommands.java index 2e9297b24..d324c3484 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/ApplyBrushCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/ApplyBrushCommands.java @@ -1,3 +1,22 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * Copyright (C) WorldEdit team and contributors + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + package com.sk89q.worldedit.command; import com.google.common.collect.ImmutableList; @@ -5,9 +24,9 @@ import com.google.common.collect.ImmutableSet; import com.sk89q.worldedit.LocalSession; import com.sk89q.worldedit.WorldEditException; import com.sk89q.worldedit.blocks.BaseItem; -import com.sk89q.worldedit.command.factory.TreeGeneratorFactory; import com.sk89q.worldedit.command.factory.ItemUseFactory; import com.sk89q.worldedit.command.factory.ReplaceFactory; +import com.sk89q.worldedit.command.factory.TreeGeneratorFactory; import com.sk89q.worldedit.command.util.PermissionCondition; import com.sk89q.worldedit.entity.Player; import com.sk89q.worldedit.function.Contextual; @@ -16,7 +35,7 @@ import com.sk89q.worldedit.function.factory.Apply; import com.sk89q.worldedit.function.pattern.Pattern; import com.sk89q.worldedit.regions.factory.RegionFactory; import com.sk89q.worldedit.util.TreeGenerator; -import com.sk89q.worldedit.util.command.CommandUtil; +import com.sk89q.worldedit.util.command.CommandRegistrationHandler; import com.sk89q.worldedit.util.formatting.text.TextComponent; import com.sk89q.worldedit.util.formatting.text.TranslatableComponent; import org.enginehub.piston.CommandManager; @@ -47,14 +66,14 @@ public class ApplyBrushCommands { .ofTypes(ImmutableList.of(Key.of(double.class))) .build(); - public static void register(CommandManager commandManager) { + public static void register(CommandManager commandManager, CommandRegistrationHandler registration) { commandManager.register("apply", builder -> { builder.description(TextComponent.of("Apply brush, apply a function to every block")); builder.action(org.enginehub.piston.Command.Action.NULL_ACTION); CommandManager manager = DefaultCommandManagerService.getInstance() .newCommandManager(); - CommandUtil.register( + registration.register( manager, ApplyBrushCommandsRegistration.builder(), new ApplyBrushCommands() diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/PaintBrushCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/PaintBrushCommands.java index 649c2988b..c365e129f 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/PaintBrushCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/PaintBrushCommands.java @@ -1,3 +1,22 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * Copyright (C) WorldEdit team and contributors + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + package com.sk89q.worldedit.command; import com.google.common.collect.ImmutableList; @@ -5,9 +24,9 @@ import com.google.common.collect.ImmutableSet; import com.sk89q.worldedit.LocalSession; import com.sk89q.worldedit.WorldEditException; import com.sk89q.worldedit.blocks.BaseItem; -import com.sk89q.worldedit.command.factory.TreeGeneratorFactory; import com.sk89q.worldedit.command.factory.ItemUseFactory; import com.sk89q.worldedit.command.factory.ReplaceFactory; +import com.sk89q.worldedit.command.factory.TreeGeneratorFactory; import com.sk89q.worldedit.command.util.PermissionCondition; import com.sk89q.worldedit.entity.Player; import com.sk89q.worldedit.function.Contextual; @@ -16,7 +35,7 @@ import com.sk89q.worldedit.function.factory.Paint; import com.sk89q.worldedit.function.pattern.Pattern; import com.sk89q.worldedit.regions.factory.RegionFactory; import com.sk89q.worldedit.util.TreeGenerator; -import com.sk89q.worldedit.util.command.CommandUtil; +import com.sk89q.worldedit.util.command.CommandRegistrationHandler; import com.sk89q.worldedit.util.formatting.text.TextComponent; import com.sk89q.worldedit.util.formatting.text.TranslatableComponent; import org.enginehub.piston.CommandManager; @@ -52,14 +71,14 @@ public class PaintBrushCommands { .ofTypes(ImmutableList.of(Key.of(double.class))) .build(); - public static void register(CommandManager commandManager) { + public static void register(CommandManager commandManager, CommandRegistrationHandler registration) { commandManager.register("paint", builder -> { builder.description(TextComponent.of("Paint brush, apply a function to a surface")); builder.action(org.enginehub.piston.Command.Action.NULL_ACTION); CommandManager manager = DefaultCommandManagerService.getInstance() .newCommandManager(); - CommandUtil.register( + registration.register( manager, PaintBrushCommandsRegistration.builder(), new PaintBrushCommands() diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/factory/ItemUseFactory.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/factory/ItemUseFactory.java index 33cd470c8..8b25780d4 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/factory/ItemUseFactory.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/factory/ItemUseFactory.java @@ -1,3 +1,22 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * Copyright (C) WorldEdit team and contributors + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + package com.sk89q.worldedit.command.factory; import com.sk89q.worldedit.EditSession; diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/factory/ReplaceFactory.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/factory/ReplaceFactory.java index 03453fcd7..4c1797b52 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/factory/ReplaceFactory.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/factory/ReplaceFactory.java @@ -1,3 +1,22 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * Copyright (C) WorldEdit team and contributors + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + package com.sk89q.worldedit.command.factory; import com.sk89q.worldedit.extent.NullExtent; diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/factory/TreeGeneratorFactory.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/factory/TreeGeneratorFactory.java index 47548b4d5..76d875851 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/factory/TreeGeneratorFactory.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/factory/TreeGeneratorFactory.java @@ -1,3 +1,22 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * Copyright (C) WorldEdit team and contributors + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + package com.sk89q.worldedit.command.factory; import com.sk89q.worldedit.EditSession; diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandManager.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandManager.java index ed93a11b8..b8e5f2101 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandManager.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandManager.java @@ -84,9 +84,11 @@ import com.sk89q.worldedit.event.platform.CommandEvent; import com.sk89q.worldedit.event.platform.CommandSuggestionEvent; import com.sk89q.worldedit.extent.Extent; import com.sk89q.worldedit.internal.annotation.Selection; +import com.sk89q.worldedit.internal.command.CommandLoggingHandler; import com.sk89q.worldedit.internal.command.WorldEditExceptionConverter; import com.sk89q.worldedit.regions.Region; import com.sk89q.worldedit.session.request.Request; +import com.sk89q.worldedit.util.command.CommandRegistrationHandler; import com.sk89q.worldedit.util.command.parametric.ExceptionConverter; import com.sk89q.worldedit.util.eventbus.Subscribe; import com.sk89q.worldedit.util.formatting.text.TextComponent; @@ -124,8 +126,6 @@ import java.util.regex.Pattern; import java.util.stream.Collectors; import static com.google.common.base.Preconditions.checkNotNull; -import static com.sk89q.worldedit.util.command.CommandUtil.COMMAND_LOG; -import static com.sk89q.worldedit.util.command.CommandUtil.register; /** * Handles the registration and invocation of commands. @@ -136,6 +136,8 @@ public final class PlatformCommandManager { public static final Pattern COMMAND_CLEAN_PATTERN = Pattern.compile("^[/]+"); private static final Logger log = LoggerFactory.getLogger(PlatformCommandManager.class); + private static final java.util.logging.Logger COMMAND_LOG = + java.util.logging.Logger.getLogger("com.sk89q.worldedit.CommandLog"); private static final Pattern numberFormatExceptionPattern = Pattern.compile("^For input string: \"(.*)\"$"); private final WorldEdit worldEdit; @@ -144,6 +146,7 @@ public final class PlatformCommandManager { private final InjectedValueStore globalInjectedValues; private final DynamicStreamHandler dynamicHandler = new DynamicStreamHandler(); private final WorldEditExceptionConverter exceptionConverter; + private final CommandRegistrationHandler registration; /** * Create a new instance. @@ -159,6 +162,10 @@ public final class PlatformCommandManager { this.commandManager = DefaultCommandManagerService.getInstance() .newCommandManager(); this.globalInjectedValues = MapBackedValueStore.create(); + this.registration = new CommandRegistrationHandler( + ImmutableList.of( + new CommandLoggingHandler(worldEdit, COMMAND_LOG) + )); // setup separate from main constructor // ensures that everything is definitely assigned initialize(); @@ -239,7 +246,7 @@ public final class PlatformCommandManager { CommandManager manager = DefaultCommandManagerService.getInstance() .newCommandManager(); - register( + this.registration.register( manager, registration, instance @@ -282,8 +289,8 @@ public final class PlatformCommandManager { BrushCommandsRegistration.builder(), new BrushCommands(worldEdit), manager -> { - PaintBrushCommands.register(manager); - ApplyBrushCommands.register(manager); + PaintBrushCommands.register(manager, registration); + ApplyBrushCommands.register(manager, registration); } ); registerSubCommands( @@ -293,72 +300,72 @@ public final class PlatformCommandManager { WorldEditCommandsRegistration.builder(), new WorldEditCommands(worldEdit) ); - register( + this.registration.register( commandManager, BiomeCommandsRegistration.builder(), new BiomeCommands() ); - register( + this.registration.register( commandManager, ChunkCommandsRegistration.builder(), new ChunkCommands(worldEdit) ); - register( + this.registration.register( commandManager, ClipboardCommandsRegistration.builder(), new ClipboardCommands() ); - register( + this.registration.register( commandManager, GeneralCommandsRegistration.builder(), new GeneralCommands(worldEdit) ); - register( + this.registration.register( commandManager, GenerationCommandsRegistration.builder(), new GenerationCommands(worldEdit) ); - register( + this.registration.register( commandManager, HistoryCommandsRegistration.builder(), new HistoryCommands(worldEdit) ); - register( + this.registration.register( commandManager, NavigationCommandsRegistration.builder(), new NavigationCommands(worldEdit) ); - register( + this.registration.register( commandManager, RegionCommandsRegistration.builder(), new RegionCommands() ); - register( + this.registration.register( commandManager, ScriptingCommandsRegistration.builder(), new ScriptingCommands(worldEdit) ); - register( + this.registration.register( commandManager, SelectionCommandsRegistration.builder(), new SelectionCommands(worldEdit) ); - register( + this.registration.register( commandManager, SnapshotUtilCommandsRegistration.builder(), new SnapshotUtilCommands(worldEdit) ); - register( + this.registration.register( commandManager, ToolCommandsRegistration.builder(), new ToolCommands(worldEdit) ); - register( + this.registration.register( commandManager, ToolUtilCommandsRegistration.builder(), new ToolUtilCommands(worldEdit) ); - register( + this.registration.register( commandManager, UtilityCommandsRegistration.builder(), new UtilityCommands(worldEdit) diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/function/ItemUseFunction.java b/worldedit-core/src/main/java/com/sk89q/worldedit/function/ItemUseFunction.java index b3b8e4748..4e439f0c3 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/function/ItemUseFunction.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/function/ItemUseFunction.java @@ -1,3 +1,22 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * Copyright (C) WorldEdit team and contributors + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + package com.sk89q.worldedit.function; import com.sk89q.worldedit.WorldEditException; diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/util/command/CommandRegistrationHandler.java b/worldedit-core/src/main/java/com/sk89q/worldedit/util/command/CommandRegistrationHandler.java new file mode 100644 index 000000000..8e243be98 --- /dev/null +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/util/command/CommandRegistrationHandler.java @@ -0,0 +1,54 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * Copyright (C) WorldEdit team and contributors + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package com.sk89q.worldedit.util.command; + +import com.google.common.collect.ImmutableList; +import com.sk89q.worldedit.WorldEdit; +import com.sk89q.worldedit.command.util.CommandPermissionsConditionGenerator; +import com.sk89q.worldedit.internal.command.CommandLoggingHandler; +import org.enginehub.piston.CommandManager; +import org.enginehub.piston.gen.CommandCallListener; +import org.enginehub.piston.gen.CommandRegistration; + +import java.util.List; +import java.util.logging.Logger; + +public class CommandRegistrationHandler { + + private static final CommandPermissionsConditionGenerator PERM_GEN = new CommandPermissionsConditionGenerator(); + + private final List callListeners; + + public CommandRegistrationHandler(List callListeners) { + this.callListeners = ImmutableList.copyOf(callListeners); + } + + public void register(CommandManager manager, CommandRegistration registration, CI instance) { + registration.containerInstance(instance) + .commandManager(manager) + .listeners(callListeners); + if (registration instanceof CommandPermissionsConditionGenerator.Registration) { + ((CommandPermissionsConditionGenerator.Registration) registration).commandPermissionsConditionGenerator( + PERM_GEN + ); + } + registration.build(); + } +} diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/util/command/CommandUtil.java b/worldedit-core/src/main/java/com/sk89q/worldedit/util/command/CommandUtil.java index ccf702e16..3d86fbfb9 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/util/command/CommandUtil.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/util/command/CommandUtil.java @@ -19,22 +19,13 @@ package com.sk89q.worldedit.util.command; -import com.google.common.collect.ImmutableList; -import com.sk89q.worldedit.WorldEdit; -import com.sk89q.worldedit.command.util.CommandPermissionsConditionGenerator; import com.sk89q.worldedit.extension.platform.PlatformCommandManager; -import com.sk89q.worldedit.internal.command.CommandLoggingHandler; import org.enginehub.piston.Command; -import org.enginehub.piston.CommandManager; -import org.enginehub.piston.gen.CommandCallListener; -import org.enginehub.piston.gen.CommandRegistration; import org.enginehub.piston.part.SubCommandPart; import java.util.Comparator; -import java.util.List; import java.util.Map; import java.util.function.Function; -import java.util.logging.Logger; import java.util.stream.Collectors; public class CommandUtil { @@ -57,26 +48,6 @@ public class CommandUtil { return BY_CLEAN_NAME; } - private static final CommandPermissionsConditionGenerator PERM_GEN = new CommandPermissionsConditionGenerator(); - - public static final Logger COMMAND_LOG = - Logger.getLogger("com.sk89q.worldedit.CommandLog"); - private static final List CALL_LISTENERS = ImmutableList.of( - new CommandLoggingHandler(WorldEdit.getInstance(), COMMAND_LOG) - ); - - public static void register(CommandManager manager, CommandRegistration registration, CI instance) { - registration.containerInstance(instance) - .commandManager(manager) - .listeners(CALL_LISTENERS); - if (registration instanceof CommandPermissionsConditionGenerator.Registration) { - ((CommandPermissionsConditionGenerator.Registration) registration).commandPermissionsConditionGenerator( - PERM_GEN - ); - } - registration.build(); - } - private CommandUtil() { } }