Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abstract out issuer awareness logic to a builder #3135

Draft
wants to merge 1 commit into
base: MV5
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package org.mvplugins.multiverse.core.commandtools;

import co.aikar.commands.BukkitCommandExecutionContext;
import co.aikar.commands.BukkitCommandIssuer;
import co.aikar.commands.InvalidCommandArgument;
import co.aikar.commands.contexts.IssuerAwareContextResolver;
import com.google.common.base.Function;
import com.google.common.base.Predicate;

import java.util.Objects;

Check warning on line 10 in src/main/java/org/mvplugins/multiverse/core/commandtools/IssuerAwarenessContextBuilder.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 Wrong order for 'java.util.Objects' import. Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/commandtools/IssuerAwarenessContextBuilder.java:10:1: warning: Wrong order for 'java.util.Objects' import. (com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck)
import java.util.function.Supplier;

public class IssuerAwarenessContextBuilder<T> {

Check warning on line 13 in src/main/java/org/mvplugins/multiverse/core/commandtools/IssuerAwarenessContextBuilder.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 Class IssuerAwarenessContextBuilder should be declared as final. Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/commandtools/IssuerAwarenessContextBuilder.java:13:1: warning: Class IssuerAwarenessContextBuilder should be declared as final. (com.puppycrawl.tools.checkstyle.checks.design.FinalClassCheck)

Check warning on line 13 in src/main/java/org/mvplugins/multiverse/core/commandtools/IssuerAwarenessContextBuilder.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 Missing a Javadoc comment. Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/commandtools/IssuerAwarenessContextBuilder.java:13:1: warning: Missing a Javadoc comment. (com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocTypeCheck)

public static <T> IssuerAwarenessContextBuilder<T> create() {

Check warning on line 15 in src/main/java/org/mvplugins/multiverse/core/commandtools/IssuerAwarenessContextBuilder.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 Missing a Javadoc comment. Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/commandtools/IssuerAwarenessContextBuilder.java:15:5: warning: Missing a Javadoc comment. (com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocMethodCheck)
return new IssuerAwarenessContextBuilder<>();
}

private Predicate<BukkitCommandIssuer> canGetFromIssuer;
private Function<BukkitCommandIssuer, T> getFromIssuer;
private Function<String, T> getFromContext;
private Supplier<InvalidCommandArgument> issuerOnlyError;
private Function<String, InvalidCommandArgument> issuerAwareError;
private Function<String, InvalidCommandArgument> inputOnlyError;

private IssuerAwarenessContextBuilder() {
// Dont expose constructor
}

public IssuerAwarenessContextBuilder<T> canGetFromIssuer(Predicate<BukkitCommandIssuer> canGetFromIssuer) {

Check warning on line 30 in src/main/java/org/mvplugins/multiverse/core/commandtools/IssuerAwarenessContextBuilder.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 Missing a Javadoc comment. Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/commandtools/IssuerAwarenessContextBuilder.java:30:5: warning: Missing a Javadoc comment. (com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocMethodCheck)
this.canGetFromIssuer = canGetFromIssuer;
return this;
}

public IssuerAwarenessContextBuilder<T> getFromIssuer(Function<BukkitCommandIssuer, T> getFromIssuer) {

Check warning on line 35 in src/main/java/org/mvplugins/multiverse/core/commandtools/IssuerAwarenessContextBuilder.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 Missing a Javadoc comment. Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/commandtools/IssuerAwarenessContextBuilder.java:35:5: warning: Missing a Javadoc comment. (com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocMethodCheck)
this.getFromIssuer = getFromIssuer;
return this;
}

public IssuerAwarenessContextBuilder<T> getFromContext(Function<String, T> getFromContext) {

Check warning on line 40 in src/main/java/org/mvplugins/multiverse/core/commandtools/IssuerAwarenessContextBuilder.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 Missing a Javadoc comment. Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/commandtools/IssuerAwarenessContextBuilder.java:40:5: warning: Missing a Javadoc comment. (com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocMethodCheck)
this.getFromContext = getFromContext;
return this;
}

public IssuerAwarenessContextBuilder<T> issuerOnlyError(Supplier<InvalidCommandArgument> issuerOnlyError) {
this.issuerOnlyError = issuerOnlyError;
return this;
}

public IssuerAwarenessContextBuilder<T> issuerAwareError(Function<String, InvalidCommandArgument> issuerAwareError) {
this.issuerAwareError = issuerAwareError;
return this;
}

public IssuerAwarenessContextBuilder<T> inputOnlyError(Function<String, InvalidCommandArgument> inputOnlyError) {
this.inputOnlyError = inputOnlyError;
return this;
}

public IssuerAwareContextResolver<T, BukkitCommandExecutionContext> build() {
Objects.requireNonNull(canGetFromIssuer);
Objects.requireNonNull(getFromIssuer);
Objects.requireNonNull(getFromContext);
Objects.requireNonNull(issuerOnlyError);
Objects.requireNonNull(issuerAwareError);
Objects.requireNonNull(inputOnlyError);

return context -> {
String resolve = context.getFlagValue("resolve", "");

if (resolve.equals("issuerOnly")) {
if (canGetFromIssuer.test(context.getIssuer())) {
return getFromIssuer.apply(context.getIssuer());
}
if (context.isOptional()) {
return null;
}
throw issuerOnlyError.get();
}

String input = context.getFirstArg();
T result = getFromContext.apply(input);

if (resolve.equals("issuerAware")) {
if (result != null) {
context.popFirstArg();
return result;
}
if (canGetFromIssuer.test(context.getIssuer())) {
return getFromIssuer.apply(context.getIssuer());
}
if (context.isOptional()) {
return null;
}
throw issuerAwareError.apply(input);
}

if (result != null) {
context.popFirstArg();
return result;
}
if (context.isOptional()) {
return null;
}
throw inputOnlyError.apply(input);
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import co.aikar.commands.InvalidCommandArgument;
import co.aikar.commands.PaperCommandContexts;
import co.aikar.commands.contexts.ContextResolver;
import co.aikar.commands.contexts.IssuerAwareContextResolver;
import com.google.common.base.Strings;
import jakarta.inject.Inject;
import org.bukkit.GameRule;
Expand Down Expand Up @@ -54,7 +55,7 @@
registerContext(GameRuleValue.class, this::parseGameRuleValue);
registerIssuerAwareContext(LoadedMultiverseWorld.class, this::parseLoadedMultiverseWorld);
registerIssuerAwareContext(LoadedMultiverseWorld[].class, this::parseLoadedMultiverseWorldArray);
registerIssuerAwareContext(MultiverseWorld.class, this::parseMultiverseWorld);
registerIssuerAwareContext(MultiverseWorld.class, this.parseMultiverseWorld());
registerIssuerAwareContext(Player.class, this::parsePlayer);
registerIssuerAwareContext(Player[].class, this::parsePlayerArray);
}
Expand Down Expand Up @@ -224,48 +225,16 @@
throw new InvalidCommandArgument("World " + worldStrings + " is not a loaded multiverse world.");
}

private MultiverseWorld parseMultiverseWorld(BukkitCommandExecutionContext context) {
String resolve = context.getFlagValue("resolve", "");

// Get world based on sender only
if (resolve.equals("issuerOnly")) {
if (context.getIssuer().isPlayer()) {
return worldManager.getWorld(context.getIssuer().getPlayer().getWorld()).getOrNull();
}
if (context.isOptional()) {
return null;
}
throw new InvalidCommandArgument("This command can only be used by a player in a Multiverse World.");
}

String worldName = context.getFirstArg();
MultiverseWorld world = worldManager.getWorld(worldName).getOrNull();

// Get world based on input, fallback to sender if input is not a world
if (resolve.equals("issuerAware")) {
if (world != null) {
context.popFirstArg();
return world;
}
if (context.getIssuer().isPlayer()) {
return worldManager.getWorld(context.getPlayer().getWorld())
.getOrElseThrow(() -> new InvalidCommandArgument("You are not in a multiverse world. Either specify a multiverse world name or use this command in a multiverse world."));
}
if (context.isOptional()) {
return null;
}
throw new InvalidCommandArgument("World '" + worldName + "' is not a loaded multiverse world. Remember to specify the world name when using this command in console.");
}

// Get world based on input only
if (world != null) {
context.popFirstArg();
return world;
}
if (context.isOptional()) {
return null;
}
throw new InvalidCommandArgument("World " + worldName + " is not a loaded multiverse world.");
private IssuerAwareContextResolver<MultiverseWorld, BukkitCommandExecutionContext> parseMultiverseWorld() {
return IssuerAwarenessContextBuilder.<MultiverseWorld>create()
.canGetFromIssuer(BukkitCommandIssuer::isPlayer)
.getFromIssuer(issuer -> worldManager.getWorld(issuer.getPlayer().getWorld())
.getOrElseThrow(() -> new InvalidCommandArgument("You must be in a multiverse world to use this command.")))

Check warning on line 232 in src/main/java/org/mvplugins/multiverse/core/commandtools/MVCommandContexts.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 Line is longer than 120 characters (found 132). Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/commandtools/MVCommandContexts.java:232:0: warning: Line is longer than 120 characters (found 132). (com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck)
.getFromContext(input -> worldManager.getWorld(input).getOrNull())
.issuerOnlyError(() -> new InvalidCommandArgument("This command can only be used by a player in a Multiverse World."))

Check warning on line 234 in src/main/java/org/mvplugins/multiverse/core/commandtools/MVCommandContexts.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 Line is longer than 120 characters (found 134). Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/commandtools/MVCommandContexts.java:234:0: warning: Line is longer than 120 characters (found 134). (com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck)
.issuerAwareError(input -> new InvalidCommandArgument("World '" + input + "' is not a multiverse world. Remember to specify the world name when using this command in console."))

Check warning on line 235 in src/main/java/org/mvplugins/multiverse/core/commandtools/MVCommandContexts.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 Line is longer than 120 characters (found 193). Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/commandtools/MVCommandContexts.java:235:0: warning: Line is longer than 120 characters (found 193). (com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck)
.inputOnlyError(input -> new InvalidCommandArgument("World '" + input + "' is not a multiverse world."))
.build();
}

private Player parsePlayer(BukkitCommandExecutionContext context) {
Expand Down
Loading