-
Notifications
You must be signed in to change notification settings - Fork 135
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
Showing
78 changed files
with
3,910 additions
and
1,922 deletions.
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
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
63 changes: 63 additions & 0 deletions
63
common/src/main/java/de/bluecolored/bluemap/common/commands/BrigadierExecutionHandler.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,63 @@ | ||
/* | ||
* This file is part of BlueMap, licensed under the MIT License (MIT). | ||
* | ||
* Copyright (c) Blue (Lukas Rieger) <https://bluecolored.de> | ||
* Copyright (c) contributors | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package de.bluecolored.bluemap.common.commands; | ||
|
||
import com.mojang.brigadier.exceptions.CommandSyntaxException; | ||
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; | ||
import de.bluecolored.bluecommands.ParseFailure; | ||
import de.bluecolored.bluecommands.ParseResult; | ||
import de.bluecolored.bluecommands.brigadier.CommandExecutionHandler; | ||
import de.bluecolored.bluemap.common.plugin.Plugin; | ||
import de.bluecolored.bluemap.common.serverinterface.CommandSource; | ||
|
||
import java.util.Comparator; | ||
|
||
public class BrigadierExecutionHandler extends CommandExecutor implements CommandExecutionHandler<CommandSource, Object> { | ||
|
||
public BrigadierExecutionHandler(Plugin plugin) { | ||
super(plugin); | ||
} | ||
|
||
@Override | ||
public int handle(ParseResult<CommandSource, Object> parseResult) throws CommandSyntaxException { | ||
ExecutionResult executionResult = this.execute(parseResult); | ||
if (executionResult.parseFailure()) | ||
return parseFailure(parseResult); | ||
return executionResult.resultCode(); | ||
} | ||
|
||
private int parseFailure(ParseResult<CommandSource, Object> result) throws CommandSyntaxException { | ||
ParseFailure<CommandSource, Object> failure = result.getFailures().stream() | ||
.max(Comparator.comparing(ParseFailure::getPosition)) | ||
.orElseThrow(IllegalAccessError::new); | ||
throw new CommandSyntaxException( | ||
new SimpleCommandExceptionType(failure::getReason), | ||
failure::getReason, | ||
result.getInput(), | ||
failure.getPosition() | ||
); | ||
} | ||
|
||
} |
91 changes: 91 additions & 0 deletions
91
common/src/main/java/de/bluecolored/bluemap/common/commands/CommandExecutor.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,91 @@ | ||
/* | ||
* This file is part of BlueMap, licensed under the MIT License (MIT). | ||
* | ||
* Copyright (c) Blue (Lukas Rieger) <https://bluecolored.de> | ||
* Copyright (c) contributors | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package de.bluecolored.bluemap.common.commands; | ||
|
||
import de.bluecolored.bluecommands.ParseMatch; | ||
import de.bluecolored.bluecommands.ParseResult; | ||
import de.bluecolored.bluemap.common.plugin.Plugin; | ||
import de.bluecolored.bluemap.common.serverinterface.CommandSource; | ||
import de.bluecolored.bluemap.core.BlueMap; | ||
import de.bluecolored.bluemap.core.logger.Logger; | ||
import lombok.RequiredArgsConstructor; | ||
import net.kyori.adventure.text.ComponentLike; | ||
|
||
import java.util.Comparator; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import static de.bluecolored.bluemap.common.commands.TextFormat.NEGATIVE_COLOR; | ||
import static net.kyori.adventure.text.Component.text; | ||
|
||
@RequiredArgsConstructor | ||
public class CommandExecutor { | ||
|
||
private final Plugin plugin; | ||
|
||
public ExecutionResult execute(ParseResult<CommandSource, Object> parseResult) { | ||
if (parseResult.getMatches().isEmpty()) { | ||
|
||
// check if the plugin is not loaded first | ||
if (!Commands.checkPluginLoaded(plugin, parseResult.getContext())) | ||
return new ExecutionResult(0, false); | ||
|
||
return new ExecutionResult(0, true); | ||
} | ||
|
||
ParseMatch<CommandSource, Object> match = parseResult.getMatches().stream() | ||
.max(Comparator.comparing(ParseMatch::getPriority)) | ||
.orElseThrow(IllegalStateException::new); | ||
|
||
if (!Commands.checkExecutablePreconditions(plugin, match.getContext(), match.getExecutable())) | ||
return new ExecutionResult(0, false); | ||
|
||
return CompletableFuture.supplyAsync(match::execute, BlueMap.THREAD_POOL) | ||
.thenApply(result -> switch (result) { | ||
case Number n -> n.intValue(); | ||
case ComponentLike c -> { | ||
match.getContext().sendMessage(c.asComponent()); | ||
yield 1; | ||
} | ||
case Boolean b -> b ? 1 : 0; | ||
case null, default -> 1; | ||
}) | ||
.exceptionally(e -> { | ||
Logger.global.logError("Command execution for '%s' failed".formatted(parseResult.getInput()), e); | ||
parseResult.getContext().sendMessage(text("There was an error executing this command! See logs or console for details.") | ||
.color(NEGATIVE_COLOR)); | ||
return 0; | ||
}) | ||
.completeOnTimeout(1, 100, TimeUnit.MILLISECONDS) | ||
.thenApply(code -> new ExecutionResult(code, false)) | ||
.join(); | ||
} | ||
|
||
public record ExecutionResult ( | ||
int resultCode, | ||
boolean parseFailure | ||
) {} | ||
|
||
} |
179 changes: 179 additions & 0 deletions
179
common/src/main/java/de/bluecolored/bluemap/common/commands/Commands.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,179 @@ | ||
/* | ||
* This file is part of BlueMap, licensed under the MIT License (MIT). | ||
* | ||
* Copyright (c) Blue (Lukas Rieger) <https://bluecolored.de> | ||
* Copyright (c) contributors | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package de.bluecolored.bluemap.common.commands; | ||
|
||
import com.flowpowered.math.vector.Vector3d; | ||
import com.github.benmanes.caffeine.cache.Cache; | ||
import com.github.benmanes.caffeine.cache.Caffeine; | ||
import com.github.benmanes.caffeine.cache.LoadingCache; | ||
import de.bluecolored.bluecommands.*; | ||
import de.bluecolored.bluemap.common.commands.arguments.MapBackedArgumentParser; | ||
import de.bluecolored.bluemap.common.commands.arguments.StringSetArgumentParser; | ||
import de.bluecolored.bluemap.common.commands.commands.*; | ||
import de.bluecolored.bluemap.common.config.storage.StorageConfig; | ||
import de.bluecolored.bluemap.common.plugin.Plugin; | ||
import de.bluecolored.bluemap.common.rendermanager.RenderTask; | ||
import de.bluecolored.bluemap.common.rendermanager.TileUpdateStrategy; | ||
import de.bluecolored.bluemap.common.serverinterface.CommandSource; | ||
import de.bluecolored.bluemap.common.serverinterface.ServerWorld; | ||
import de.bluecolored.bluemap.core.map.BmMap; | ||
import de.bluecolored.bluemap.core.world.World; | ||
import net.kyori.adventure.text.event.ClickEvent; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.Map; | ||
import java.util.Random; | ||
import java.util.Set; | ||
import java.util.stream.Stream; | ||
|
||
import static de.bluecolored.bluemap.common.commands.TextFormat.*; | ||
import static net.kyori.adventure.text.Component.text; | ||
|
||
public class Commands { | ||
|
||
private static final Cache<String, RenderTask> REF_TO_RENDERTASK = Caffeine.newBuilder() | ||
.weakValues() | ||
.build(); | ||
private static final LoadingCache<RenderTask, String> RENDERTASK_TO_REF = Caffeine.newBuilder() | ||
.weakKeys() | ||
.build(Commands::safeRandomRef); | ||
|
||
public static de.bluecolored.bluecommands.Command<CommandSource, Object> create(Plugin plugin) { | ||
BlueCommands<CommandSource> builder = new BlueCommands<>(); | ||
|
||
builder.setArgumentParserForArgumentType(BmMap.class, new MapBackedArgumentParser<>("map", () -> | ||
plugin.isLoaded() ? plugin.getBlueMap().getMaps() : Map.of())); | ||
builder.setArgumentParserForArgumentType(StorageConfig.class, new MapBackedArgumentParser<>("storage", () -> | ||
plugin.isLoaded() ? plugin.getBlueMap().getConfig().getStorageConfigs() : Map.of())); | ||
builder.setArgumentParserForArgumentType(RenderTask.class, new MapBackedArgumentParser<>("render-task", REF_TO_RENDERTASK.asMap())); | ||
|
||
builder.setArgumentParserForId("storage-id", new StringSetArgumentParser("storage", () -> | ||
plugin.isLoaded() ? plugin.getBlueMap().getConfig().getStorageConfigs().keySet() : Set.of())); | ||
|
||
builder.setContextResolverForType(ServerWorld.class, c -> c.getWorld().orElse(null)); | ||
builder.setContextResolverForType(World.class, c -> plugin.isLoaded() ? c.getWorld().map(plugin::getWorld).orElse(null) : null); | ||
builder.setContextResolverForType(Vector3d.class, c -> c.getPosition().orElse(null)); | ||
|
||
builder.setAnnotationContextPredicate(Permission.class, (permission, commandSource) -> | ||
permission == null || commandSource.hasPermission(permission.value()) | ||
); | ||
builder.setAnnotationContextPredicate(WithWorld.class, (withWorld, commandSource) -> | ||
withWorld == null || plugin.isLoaded() && commandSource.getWorld().map(plugin::getWorld).isPresent() | ||
); | ||
builder.setAnnotationContextPredicate(WithPosition.class, (withPosition, commandSource) -> | ||
withPosition == null || commandSource.getPosition().isPresent() | ||
); | ||
|
||
de.bluecolored.bluecommands.Command<CommandSource, Object> commands = new LiteralCommand<>("bluemap"); | ||
|
||
// register commands | ||
Stream.of( | ||
new DebugCommand(plugin), | ||
new FreezeCommand(plugin), | ||
new HelpCommand(plugin), | ||
new MapListCommand(plugin), | ||
new PurgeCommand(plugin), | ||
new ReloadCommand(plugin), | ||
new StartCommand(plugin), | ||
new StatusCommand(plugin), | ||
new StopCommand(plugin), | ||
new StoragesCommand(plugin), | ||
new TasksCommand(plugin), | ||
new TroubleshootCommand(plugin), | ||
new UnfreezeCommand(plugin), | ||
new VersionCommand(plugin) | ||
) | ||
.map(builder::createCommand) | ||
.forEach(commands::addSubCommand); | ||
|
||
// register an update-command for each update-strategy | ||
Map.of( | ||
"update", TileUpdateStrategy.FORCE_NONE, | ||
"fix-edges", TileUpdateStrategy.FORCE_EDGE, | ||
"force-update", TileUpdateStrategy.FORCE_ALL | ||
).forEach((updateLiteral, strategy) -> { | ||
Command<CommandSource, Object> updateCommand = new LiteralCommand<>(updateLiteral); | ||
updateCommand.addSubCommand(builder.createCommand(new UpdateCommand(plugin, strategy))); | ||
commands.addSubCommand(updateCommand); | ||
}); | ||
|
||
return commands; | ||
} | ||
|
||
public static String getRefForTask(RenderTask task) { | ||
return RENDERTASK_TO_REF.get(task); | ||
} | ||
|
||
public static @Nullable RenderTask getTaskForRef(String ref) { | ||
return REF_TO_RENDERTASK.getIfPresent(ref); | ||
} | ||
|
||
public static boolean checkExecutablePreconditions(Plugin plugin, CommandSource context, CommandExecutable<CommandSource, Object> executable) { | ||
if (executable instanceof MethodCommandExecutable<CommandSource> methodExecutable) { | ||
|
||
// check if plugin needs to be loaded | ||
if (methodExecutable.getMethod().getAnnotation(Unloaded.class) == null) { | ||
return Commands.checkPluginLoaded(plugin, context); | ||
} | ||
|
||
} | ||
|
||
return true; | ||
} | ||
|
||
public static boolean checkPluginLoaded(Plugin plugin, CommandSource context){ | ||
if (!plugin.isLoaded()) { | ||
if (plugin.isLoading()) { | ||
context.sendMessage(lines( | ||
text("⌛ BlueMap is still loading!").color(INFO_COLOR), | ||
text("Please try again in a few seconds.").color(BASE_COLOR) | ||
)); | ||
} else { | ||
context.sendMessage(lines( | ||
text("❌ BlueMap is not loaded!").color(NEGATIVE_COLOR), | ||
format("Check your server-console for errors or warnings and try using %.", | ||
command("/bluemap reload").color(HIGHLIGHT_COLOR) | ||
).color(BASE_COLOR) | ||
)); | ||
} | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private static synchronized String safeRandomRef(RenderTask task) { | ||
String ref = randomRef(); | ||
while (REF_TO_RENDERTASK.asMap().putIfAbsent(ref, task) != null) ref = randomRef(); | ||
return ref; | ||
} | ||
|
||
private static String randomRef() { | ||
StringBuilder ref = new StringBuilder(Integer.toString(Math.abs(new Random().nextInt()), 16)); | ||
while (ref.length() < 4) ref.insert(0, "0"); | ||
return ref.subSequence(0, 4).toString(); | ||
} | ||
|
||
} |
Oops, something went wrong.