From 9408b3cb56884acfbaf9c7f10e524ae446ca9f93 Mon Sep 17 00:00:00 2001 From: YouHaveTrouble Date: Wed, 21 May 2025 20:16:34 +0200 Subject: [PATCH] schematic load from file prototype command and adjustments that it required --- .../me/youhavetrouble/blockedit/BELocale.java | 8 ++- .../blockedit/BlockEditCommands.java | 55 +++++++++++++++++++ .../blockedit/SchematicHandler.java | 9 +-- ...derForSchematicFileExtensionException.java | 22 ++++++++ .../blockedit/schematic/Schematic.java | 4 +- .../schematic/SchematicProvider.java | 8 +-- src/main/resources/locale/en_US.json | 7 ++- 7 files changed, 98 insertions(+), 15 deletions(-) create mode 100644 src/main/java/me/youhavetrouble/blockedit/exception/NoProviderForSchematicFileExtensionException.java diff --git a/src/main/java/me/youhavetrouble/blockedit/BELocale.java b/src/main/java/me/youhavetrouble/blockedit/BELocale.java index 2c35f71..37bc4fa 100644 --- a/src/main/java/me/youhavetrouble/blockedit/BELocale.java +++ b/src/main/java/me/youhavetrouble/blockedit/BELocale.java @@ -13,7 +13,8 @@ public class BELocale { private static final Locale defaultLocale = Locale.of("en_US"); public final String couldNotFindWandById, selectArea, copiedSelectionToClipboard, selectionReset, firstPositionSet, - secondPositionSet, pastingClipboard, clipboardRotated, settingBlocks, replacingBlocks; + secondPositionSet, pastingClipboard, clipboardRotated, settingBlocks, replacingBlocks, schematicLoaded, + startedLoadingSchematic, noProviderForSchematicFileExtension, schematicLoadError, schematicNotFound; protected BELocale(JsonObject json) { @@ -27,6 +28,11 @@ public class BELocale { clipboardRotated = json.get("clipboard_rotated").getAsString(); settingBlocks = json.get("setting_blocks").getAsString(); replacingBlocks = json.get("replacing_blocks").getAsString(); + startedLoadingSchematic = json.get("started_loading_schematic").getAsString(); + schematicLoaded = json.get("schematic_loaded").getAsString(); + noProviderForSchematicFileExtension = json.get("no_provider_for_schematic_file_extension").getAsString(); + schematicLoadError = json.get("schematic_load_error").getAsString(); + schematicNotFound = json.get("schematic_not_found").getAsString(); } protected static void registerLocale(Locale locale, BELocale blockEditLocale) { diff --git a/src/main/java/me/youhavetrouble/blockedit/BlockEditCommands.java b/src/main/java/me/youhavetrouble/blockedit/BlockEditCommands.java index b3f2db6..283c09c 100644 --- a/src/main/java/me/youhavetrouble/blockedit/BlockEditCommands.java +++ b/src/main/java/me/youhavetrouble/blockedit/BlockEditCommands.java @@ -12,9 +12,12 @@ import io.papermc.paper.command.brigadier.argument.resolvers.BlockPositionResolv import io.papermc.paper.plugin.lifecycle.event.LifecycleEventManager; import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents; import me.youhavetrouble.blockedit.api.BlockEditAPI; +import me.youhavetrouble.blockedit.exception.NoProviderForSchematicFileExtensionException; +import me.youhavetrouble.blockedit.exception.SchematicLoadException; import me.youhavetrouble.blockedit.operations.PasteOperation; import me.youhavetrouble.blockedit.operations.ReplaceOperation; import me.youhavetrouble.blockedit.operations.SetOperation; +import me.youhavetrouble.blockedit.schematic.Schematic; import me.youhavetrouble.blockedit.util.Selection; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; @@ -27,6 +30,7 @@ import org.bukkit.util.Vector; import org.jetbrains.annotations.NotNull; import java.util.HashMap; +import java.util.concurrent.CompletableFuture; @SuppressWarnings("UnstableApiUsage") public class BlockEditCommands { @@ -373,4 +377,55 @@ public class BlockEditCommands { .build(); } + private static LiteralCommandNode schematicCommand() { + return Commands.literal("schematic") + .requires(css -> { + if (!(css.getSender() instanceof Player player)) return false; + return player.hasPermission("blockedit.command.schematic"); + }) + .then(Commands.literal("load") + .then(Commands.literal("file") + .then(Commands.argument("schematic_name", StringArgumentType.word()) + .requires(commandSourceStack -> { + if (!(commandSourceStack.getSender() instanceof Player player)) + return false; + return player.hasPermission("blockedit.command.schematic.load.file"); + }) + .executes(context -> { + CompletableFuture.runAsync(() -> { + Player player = (Player) context.getSource().getSender(); + String schematicName = context.getArgument("schematic_name", String.class); + + player.sendMessage(Component.text(BELocale.getLocale(player.locale()).startedLoadingSchematic.formatted(schematicName), NamedTextColor.GRAY)); + + Schematic schematic; + try { + schematic = BlockEditAPI.getSchematicHandler().loadSchematic(schematicName); + } catch (NoProviderForSchematicFileExtensionException e) { + player.sendMessage(Component.text(BELocale.getLocale(player.locale()).noProviderForSchematicFileExtension.formatted(e.getExtension()), NamedTextColor.RED)); + return; + } catch (SchematicLoadException e) { + player.sendMessage(Component.text(BELocale.getLocale(player.locale()).schematicLoadError.formatted(schematicName), NamedTextColor.RED)); + BlockEdit.getPlugin().getSLF4JLogger().error("Could not load schematic {} due to provider error ", schematicName, e); + return; + } + + if (schematic == null) { + player.sendMessage(Component.text(BELocale.getLocale(player.locale()).schematicNotFound.formatted(schematicName), NamedTextColor.RED)); + return; + } + + BEPlayer bePlayer = BEPlayer.getByPlayer(player); + bePlayer.setClipboard(schematic.asClipboard()); + player.sendMessage(Component.text(BELocale.getLocale(player.locale()).schematicLoaded.formatted(schematicName), NamedTextColor.GRAY)); + }); + + return Command.SINGLE_SUCCESS; + }) + ) + ) + ) + .build(); + } + } diff --git a/src/main/java/me/youhavetrouble/blockedit/SchematicHandler.java b/src/main/java/me/youhavetrouble/blockedit/SchematicHandler.java index 9607c28..c464994 100644 --- a/src/main/java/me/youhavetrouble/blockedit/SchematicHandler.java +++ b/src/main/java/me/youhavetrouble/blockedit/SchematicHandler.java @@ -1,5 +1,6 @@ package me.youhavetrouble.blockedit; +import me.youhavetrouble.blockedit.exception.NoProviderForSchematicFileExtensionException; import me.youhavetrouble.blockedit.exception.SchematicHandlerRegistrationException; import me.youhavetrouble.blockedit.exception.SchematicLoadException; import me.youhavetrouble.blockedit.exception.SchematicSaveException; @@ -117,13 +118,9 @@ public class SchematicHandler { String fileExtension = fileName.substring(fileName.lastIndexOf('.') + 1); SchematicProvider schematicProvider = schematicProvidersByExtension.get(fileExtension); if (schematicProvider == null) { - throw new SchematicLoadException("No schematic provider found for file extension " + fileExtension, schematicName); + throw new NoProviderForSchematicFileExtensionException("No schematic provider found for file extension " + fileExtension, schematicName, fileExtension); } - Schematic schematic = schematicProvider.load(schematicName); - if (schematic == null) { - throw new SchematicLoadException("Schematic could not be loaded", schematicName); - } - return schematic; + return schematicProvider.load(schematicName); } /** diff --git a/src/main/java/me/youhavetrouble/blockedit/exception/NoProviderForSchematicFileExtensionException.java b/src/main/java/me/youhavetrouble/blockedit/exception/NoProviderForSchematicFileExtensionException.java new file mode 100644 index 0000000..1d7add3 --- /dev/null +++ b/src/main/java/me/youhavetrouble/blockedit/exception/NoProviderForSchematicFileExtensionException.java @@ -0,0 +1,22 @@ +package me.youhavetrouble.blockedit.exception; + +import org.jetbrains.annotations.NotNull; + +public class NoProviderForSchematicFileExtensionException extends SchematicLoadException { + + private String extension; + + public NoProviderForSchematicFileExtensionException( + @NotNull String message, + @NotNull String schematicName, + @NotNull String extension + ) { + super(message, schematicName); + this.extension = extension; + } + + public String getExtension() { + return extension; + } + +} diff --git a/src/main/java/me/youhavetrouble/blockedit/schematic/Schematic.java b/src/main/java/me/youhavetrouble/blockedit/schematic/Schematic.java index e1bbd11..5e83b5d 100644 --- a/src/main/java/me/youhavetrouble/blockedit/schematic/Schematic.java +++ b/src/main/java/me/youhavetrouble/blockedit/schematic/Schematic.java @@ -1,16 +1,14 @@ package me.youhavetrouble.blockedit.schematic; import me.youhavetrouble.blockedit.util.Clipboard; -import org.bukkit.Location; import org.jetbrains.annotations.NotNull; public abstract class Schematic { /** * Get the schematic as a Clipboard object - * @param originLocation The location to use as the origin for the clipboard * @return The clipboard object containing the schematic */ - public abstract @NotNull Clipboard asClipboard(@NotNull Location originLocation); + public abstract @NotNull Clipboard asClipboard(); } diff --git a/src/main/java/me/youhavetrouble/blockedit/schematic/SchematicProvider.java b/src/main/java/me/youhavetrouble/blockedit/schematic/SchematicProvider.java index ee1b1f6..85e6e14 100644 --- a/src/main/java/me/youhavetrouble/blockedit/schematic/SchematicProvider.java +++ b/src/main/java/me/youhavetrouble/blockedit/schematic/SchematicProvider.java @@ -1,5 +1,6 @@ package me.youhavetrouble.blockedit.schematic; +import me.youhavetrouble.blockedit.exception.SchematicLoadException; import me.youhavetrouble.blockedit.util.Clipboard; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -15,8 +16,6 @@ public interface SchematicProvider { */ @NotNull String name(); - - /** * Save the schematic * @param schematic The schematic to save @@ -26,9 +25,10 @@ public interface SchematicProvider { /** * Load a schematic * @param name Name of the schematic to load - * @return The loaded schematic. Returns null if the schematic could not be loaded. + * @return The loaded schematic. Returns null if the schematic does not exist. + * @throws SchematicLoadException If the schematic could not be loaded */ - @Nullable S load(@NotNull String name); + @Nullable S load(@NotNull String name) throws SchematicLoadException; /** * Create a new schematic from the given clipboard diff --git a/src/main/resources/locale/en_US.json b/src/main/resources/locale/en_US.json index 24abaf2..4b3a64b 100644 --- a/src/main/resources/locale/en_US.json +++ b/src/main/resources/locale/en_US.json @@ -8,5 +8,10 @@ "pasting_clipboard": "Pasting clipboard...", "clipboard_rotated": "Clipboard rotated %s degrees", "setting_blocks": "Setting blocks...", - "replacing_blocks": "Replacing blocks..." + "replacing_blocks": "Replacing blocks...", + "started_loading_schematic": "Started loading schematic %s", + "schematic_loaded": "Loaded schematic %s", + "no_provider_for_schematic_file": "No provider for schematic file type %s", + "schematic_load_error": "Error loading schematic %s: %s", + "schematic_not_found": "Schematic %s not found" }