schematic load from file prototype command and adjustments that it required

This commit is contained in:
2025-05-21 20:16:34 +02:00
parent bc5acf2c8c
commit 9408b3cb56
7 changed files with 98 additions and 15 deletions
@@ -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) {
@@ -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<CommandSourceStack> 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();
}
}
@@ -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<S extends Schematic> {
String fileExtension = fileName.substring(fileName.lastIndexOf('.') + 1);
SchematicProvider<S> 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);
}
/**
@@ -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;
}
}
@@ -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();
}
@@ -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<S extends Schematic> {
*/
@NotNull String name();
/**
* Save the schematic
* @param schematic The schematic to save
@@ -26,9 +25,10 @@ public interface SchematicProvider<S extends Schematic> {
/**
* 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
+6 -1
View File
@@ -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"
}