-
-
Notifications
You must be signed in to change notification settings - Fork 800
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ead7acd
commit 259fe31
Showing
19 changed files
with
794 additions
and
1 deletion.
There are no files selected for viewing
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
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
86 changes: 86 additions & 0 deletions
86
Bukkit/src/main/java/com/plotsquared/bukkit/inject/CloudModule.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,86 @@ | ||
package com.plotsquared.bukkit.inject; | ||
|
||
import cloud.commandframework.CloudCapability; | ||
import cloud.commandframework.CommandManager; | ||
import cloud.commandframework.bukkit.CloudBukkitCapabilities; | ||
import cloud.commandframework.execution.AsynchronousCommandExecutionCoordinator; | ||
import cloud.commandframework.minecraft.extras.AudienceProvider; | ||
import cloud.commandframework.minecraft.extras.MinecraftExceptionHandler; | ||
import cloud.commandframework.paper.PaperCommandManager; | ||
import com.google.inject.AbstractModule; | ||
import com.google.inject.Key; | ||
import com.google.inject.TypeLiteral; | ||
import com.plotsquared.bukkit.BukkitPlatform; | ||
import com.plotsquared.bukkit.util.BukkitUtil; | ||
import com.plotsquared.core.commands.CommonCommandRequirement; | ||
import com.plotsquared.core.commands.PlotSquaredCaptionProvider; | ||
import com.plotsquared.core.commands.processing.CommandRequirementPostprocessor; | ||
import com.plotsquared.core.configuration.caption.TranslatableCaption; | ||
import com.plotsquared.core.player.ConsolePlayer; | ||
import com.plotsquared.core.player.PlotPlayer; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.command.CommandSender; | ||
import org.bukkit.entity.Player; | ||
import org.checkerframework.checker.nullness.qual.NonNull; | ||
|
||
public class CloudModule extends AbstractModule { | ||
|
||
private static final Logger LOGGER = LogManager.getLogger("PlotSquared/" + CloudModule.class.getSimpleName()); | ||
|
||
private static @NonNull CommandSender convert(final @NonNull PlotPlayer<?> player) { | ||
if (player instanceof ConsolePlayer) { | ||
return Bukkit.getConsoleSender(); | ||
} | ||
return (Player) player.getPlatformPlayer(); | ||
} | ||
|
||
private static @NonNull PlotPlayer<?> convert (final @NonNull CommandSender sender) { | ||
if (sender instanceof Player player) { | ||
return BukkitUtil.adapt(player); | ||
} | ||
return ConsolePlayer.getConsole(); | ||
} | ||
|
||
private final BukkitPlatform bukkitPlatform; | ||
|
||
public CloudModule(final @NonNull BukkitPlatform bukkitPlatform) { | ||
this.bukkitPlatform = bukkitPlatform; | ||
} | ||
|
||
@Override | ||
protected void configure() { | ||
try { | ||
final PaperCommandManager<PlotPlayer<?>> commandManager = new PaperCommandManager<PlotPlayer<?>>( | ||
this.bukkitPlatform, | ||
AsynchronousCommandExecutionCoordinator.<PlotPlayer<?>>builder().withAsynchronousParsing().build(), | ||
CloudModule::convert, | ||
CloudModule::convert | ||
); | ||
commandManager.captionRegistry().registerProvider(new PlotSquaredCaptionProvider()); | ||
if (commandManager.hasCapability(CloudBukkitCapabilities.ASYNCHRONOUS_COMPLETION)) { | ||
commandManager.registerAsynchronousCompletions(); | ||
} | ||
if (commandManager.hasCapability(CloudBukkitCapabilities.NATIVE_BRIGADIER)) { | ||
commandManager.registerBrigadier(); | ||
} | ||
|
||
final CommandRequirementPostprocessor requirementPostprocessor = new CommandRequirementPostprocessor(); | ||
requirementPostprocessor.registerRequirements(CommonCommandRequirement.values()); | ||
commandManager.registerCommandPostProcessor(requirementPostprocessor); | ||
|
||
// TODO(City): Override parsing errors using MM parsing. | ||
MinecraftExceptionHandler.<PlotPlayer<?>>create(PlotPlayer::getAudience) | ||
.defaultHandlers() | ||
.decorator((ctx, component) -> TranslatableCaption.of("core.prefix"). | ||
toComponent(ctx.context().sender()) | ||
.append(component)) | ||
.registerTo(commandManager); | ||
|
||
bind(Key.get(new TypeLiteral<CommandManager<PlotPlayer<?>>>() {})).toInstance(commandManager); | ||
} catch (final Exception e) { | ||
LOGGER.error("Failed to configure command manager", e); | ||
} | ||
} | ||
} |
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
28 changes: 28 additions & 0 deletions
28
Core/src/main/java/com/plotsquared/core/commands/CommandRequirement.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,28 @@ | ||
package com.plotsquared.core.commands; | ||
|
||
import cloud.commandframework.context.CommandContext; | ||
import cloud.commandframework.keys.CloudKeyHolder; | ||
import com.plotsquared.core.configuration.caption.TranslatableCaption; | ||
import com.plotsquared.core.player.PlotPlayer; | ||
import org.checkerframework.checker.nullness.qual.NonNull; | ||
|
||
/** | ||
* Something that is required for a command to be executed. | ||
*/ | ||
public interface CommandRequirement extends CloudKeyHolder<Boolean> { | ||
|
||
/** | ||
* Returns the caption sent when the requirement is not met. | ||
* | ||
* @return the caption | ||
*/ | ||
@NonNull TranslatableCaption failureCaption(); | ||
|
||
/** | ||
* Evaluates whether the requirement is met. | ||
* | ||
* @param context command context to evaluate | ||
* @return {@code true} if the requirement is met, else {@code false} | ||
*/ | ||
boolean evaluate(final @NonNull CommandContext<PlotPlayer<?>> context); | ||
} |
50 changes: 50 additions & 0 deletions
50
Core/src/main/java/com/plotsquared/core/commands/CommonCommandRequirement.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,50 @@ | ||
package com.plotsquared.core.commands; | ||
|
||
import cloud.commandframework.context.CommandContext; | ||
import cloud.commandframework.keys.CloudKey; | ||
import com.plotsquared.core.configuration.caption.TranslatableCaption; | ||
import com.plotsquared.core.player.PlotPlayer; | ||
import org.checkerframework.checker.nullness.qual.NonNull; | ||
|
||
import java.util.function.Predicate; | ||
|
||
/** | ||
* Common {@link CommandRequirement command requirements}. | ||
*/ | ||
public enum CommonCommandRequirement implements CommandRequirement { | ||
/** | ||
* Requires that the command sender is currently in a plot. | ||
*/ | ||
REQUIRES_PLOT(TranslatableCaption.of("errors.not_in_plot"), ctx -> ctx.sender().getCurrentPlot() != null), | ||
/** | ||
* Requires that the command sender is in a claimed plot. | ||
*/ | ||
REQUIRES_OWNER(TranslatableCaption.of("working.plot_not_claimed"), | ||
ctx -> ctx.sender().getCurrentPlot() != null && ctx.sender().getCurrentPlot().hasOwner() | ||
); | ||
|
||
private final TranslatableCaption failureCaption; | ||
private final Predicate<CommandContext<PlotPlayer<?>>> predicate; | ||
|
||
CommonCommandRequirement( | ||
final @NonNull TranslatableCaption failureCaption, | ||
final @NonNull Predicate<CommandContext<PlotPlayer<?>>> predicate | ||
) { | ||
this.failureCaption = failureCaption; | ||
this.predicate = predicate; | ||
} | ||
|
||
public @NonNull TranslatableCaption failureCaption() { | ||
return this.failureCaption; | ||
} | ||
|
||
@Override | ||
public boolean evaluate(final @NonNull CommandContext<PlotPlayer<?>> context) { | ||
return this.predicate.test(context); | ||
} | ||
|
||
@Override | ||
public @NonNull CloudKey<Boolean> key() { | ||
return CloudKey.of(String.format("requirement_%s", this.name()), Boolean.class); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
Core/src/main/java/com/plotsquared/core/commands/PlotSquaredCaptionProvider.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,32 @@ | ||
package com.plotsquared.core.commands; | ||
|
||
import cloud.commandframework.captions.Caption; | ||
import cloud.commandframework.captions.CaptionProvider; | ||
import com.plotsquared.core.PlotSquared; | ||
import com.plotsquared.core.configuration.caption.CaptionMap; | ||
import com.plotsquared.core.configuration.caption.TranslatableCaption; | ||
import com.plotsquared.core.player.PlotPlayer; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.checkerframework.checker.nullness.qual.NonNull; | ||
import org.checkerframework.checker.nullness.qual.Nullable; | ||
|
||
/** | ||
* {@link CaptionProvider} that retrieves caption values from the {@link CaptionMap caption map}. | ||
*/ | ||
public final class PlotSquaredCaptionProvider implements CaptionProvider<PlotPlayer<?>> { | ||
|
||
private static final Logger LOGGER = LogManager.getLogger("PlotSquared/" + PlotSquaredCaptionProvider.class.getSimpleName()); | ||
|
||
@Override | ||
public @Nullable String provide(final @NonNull Caption caption, final @NonNull PlotPlayer<?> recipient) { | ||
try { | ||
return PlotSquared.get() | ||
.getCaptionMap(TranslatableCaption.DEFAULT_NAMESPACE) | ||
.getMessage(TranslatableCaption.of(caption.key()), recipient); | ||
} catch (final CaptionMap.NoSuchCaptionException ignored) { | ||
LOGGER.warn("Missing caption '{}', will attempt to fall back on Cloud defaults", caption.key()); | ||
return null; | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
Core/src/main/java/com/plotsquared/core/commands/PlotSquaredCommandBean.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,47 @@ | ||
package com.plotsquared.core.commands; | ||
|
||
import cloud.commandframework.Command; | ||
import cloud.commandframework.CommandBean; | ||
import cloud.commandframework.CommandProperties; | ||
import com.plotsquared.core.command.CommandCategory; | ||
import com.plotsquared.core.player.PlotPlayer; | ||
import org.checkerframework.checker.nullness.qual.NonNull; | ||
|
||
import java.util.Set; | ||
|
||
public abstract class PlotSquaredCommandBean extends CommandBean<PlotPlayer<?>> { | ||
|
||
/** | ||
* Returns the category of the command. | ||
* | ||
* @return the category | ||
*/ | ||
public abstract @NonNull CommandCategory category(); | ||
|
||
/** | ||
* Returns the requirements for the command to be executable. | ||
* | ||
* @return the requirements | ||
*/ | ||
public abstract @NonNull Set<@NonNull CommandRequirement> requirements(); | ||
|
||
@Override | ||
protected final @NonNull CommandProperties properties() { | ||
return CommandProperties.of("platsquared", "plat"); | ||
} | ||
|
||
@Override | ||
protected final Command.@NonNull Builder<PlotPlayer<?>> configure(final Command.@NonNull Builder<PlotPlayer<?>> builder) { | ||
Command.@NonNull Builder<PlotPlayer<?>> intermediaryBuilder = | ||
this.configurePlotCommand(builder.meta(PlotSquaredCommandMeta.META_CATEGORY, | ||
this.category())); | ||
for (final CommandRequirement requirement : this.requirements()) { | ||
intermediaryBuilder = intermediaryBuilder.meta(requirement.key(), true); | ||
} | ||
return intermediaryBuilder; | ||
} | ||
|
||
protected abstract Command.@NonNull Builder<PlotPlayer<?>> configurePlotCommand( | ||
Command.@NonNull Builder<PlotPlayer<?>> builder | ||
); | ||
} |
56 changes: 56 additions & 0 deletions
56
Core/src/main/java/com/plotsquared/core/commands/PlotSquaredCommandManager.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,56 @@ | ||
package com.plotsquared.core.commands; | ||
|
||
import cloud.commandframework.CommandManager; | ||
import cloud.commandframework.annotations.injection.GuiceInjectionService; | ||
import com.google.inject.Inject; | ||
import com.google.inject.Injector; | ||
import com.google.inject.Key; | ||
import com.google.inject.Singleton; | ||
import com.google.inject.TypeLiteral; | ||
import com.plotsquared.core.commands.injection.PlotInjector; | ||
import com.plotsquared.core.player.PlotPlayer; | ||
import com.plotsquared.core.plot.Plot; | ||
import org.checkerframework.checker.nullness.qual.NonNull; | ||
|
||
import java.util.Set; | ||
|
||
@Singleton | ||
public final class PlotSquaredCommandManager { | ||
|
||
private final CommandManager<PlotPlayer<?>> commandManager; | ||
private final Injector injector; | ||
|
||
@Inject | ||
public PlotSquaredCommandManager( | ||
final @NonNull CommandManager<PlotPlayer<?>> commandManager, | ||
final @NonNull Injector injector | ||
) { | ||
this.commandManager = commandManager; | ||
this.injector = injector; | ||
this.registerInjectors(); | ||
} | ||
|
||
/** | ||
* Registers the commands that are shipped with PlotSquared. | ||
*/ | ||
public void registerDefaultCommands() { | ||
final Set<PlotSquaredCommandBean> commands = | ||
this.injector.getInstance(Key.get(new TypeLiteral<Set<PlotSquaredCommandBean>>() {})); | ||
commands.forEach(command -> this.commandManager().command(command)); | ||
} | ||
|
||
/** | ||
* Returns the command manager. | ||
* | ||
* @return the command manager | ||
*/ | ||
public @NonNull CommandManager<PlotPlayer<?>> commandManager() { | ||
return this.commandManager; | ||
} | ||
|
||
private void registerInjectors() { | ||
this.commandManager.parameterInjectorRegistry().registerInjector(Plot.class, | ||
this.injector.getInstance(PlotInjector.class)); | ||
this.commandManager.parameterInjectorRegistry().registerInjectionService(GuiceInjectionService.create(this.injector)); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
Core/src/main/java/com/plotsquared/core/commands/PlotSquaredCommandMeta.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,18 @@ | ||
package com.plotsquared.core.commands; | ||
|
||
import cloud.commandframework.keys.CloudKey; | ||
import com.plotsquared.core.command.CommandCategory; | ||
|
||
/** | ||
* Shared {@link cloud.commandframework.meta.CommandMeta command meta} keys. | ||
*/ | ||
public final class PlotSquaredCommandMeta { | ||
|
||
/** | ||
* Key that determines what {@link CommandCategory category} a command belongs to. | ||
*/ | ||
public static final CloudKey<CommandCategory> META_CATEGORY = CloudKey.of("category", CommandCategory.class); | ||
|
||
private PlotSquaredCommandMeta() { | ||
} | ||
} |
Oops, something went wrong.