SchematicCommand half done

This commit is contained in:
CreepyCre 2021-03-27 11:28:01 +01:00
parent 002510735d
commit 476671a417
2 changed files with 72 additions and 38 deletions

View file

@ -1,60 +1,45 @@
package org.dimdev.dimdoors.command;
import java.io.IOException;
import java.io.InputStream;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.dimdev.dimdoors.DimensionalDoorsInitializer;
import org.dimdev.dimdoors.command.arguments.SchematicNamespaceArgumentType;
import org.dimdev.dimdoors.command.arguments.PocketTemplateArgumentType;
import org.dimdev.dimdoors.pockets.PocketTemplate;
import org.dimdev.dimdoors.util.BlockPlacementType;
import org.dimdev.dimdoors.util.schematic.Schematic;
import org.dimdev.dimdoors.util.schematic.SchematicPlacer;
import net.minecraft.nbt.NbtIo;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.TranslatableText;
import static net.minecraft.server.command.CommandManager.argument;
import static net.minecraft.server.command.CommandManager.literal;
public class SchematicCommand {
private static final Logger LOGGER = LogManager.getLogger();
private static final Logger LOGGER = LogManager.getLogger();
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
dispatcher.register(literal("schematicv2")
.then(argument("namespace", new SchematicNamespaceArgumentType())
.then(argument("schematic_name", StringArgumentType.string())
.executes(SchematicCommand::place)
)
)
);
}
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
dispatcher.register(literal("schematic") // TODO: better command name
.then(argument("pocket_template", new PocketTemplateArgumentType())
.executes(SchematicCommand::place)
)
);
}
private static int place(CommandContext<ServerCommandSource> ctx) throws CommandSyntaxException {
ServerPlayerEntity player = ctx.getSource().getPlayer();
String id = StringArgumentType.getString(ctx, "schematic_name");
String ns = SchematicNamespaceArgumentType.getValue(ctx, "namespace");
private static int place(CommandContext<ServerCommandSource> ctx) throws CommandSyntaxException {
ServerPlayerEntity player = ctx.getSource().getPlayer();
PocketTemplate template = PocketTemplateArgumentType.getValue(ctx, "pocket_template");
try (InputStream in = DimensionalDoorsInitializer.class.getResourceAsStream("/resourcepacks/default_pockets/data/dimdoors/pockets/schematic/v2/" + ns + "/" + id + ".schem")) {
SchematicPlacer.place(
Schematic.fromTag(NbtIo.readCompressed(in)),
ctx.getSource().getWorld(),
ctx.getSource().getPlayer().getBlockPos(),
BlockPlacementType.SECTION_NO_UPDATE
);
} catch (IOException e) {
e.printStackTrace();
throw new SimpleCommandExceptionType(new TranslatableText("command.dimdoors.schematicv2.unknownSchematic", id, ns)).create();
}
SchematicPlacer.place(
template.getSchematic(),
ctx.getSource().getWorld(),
ctx.getSource().getPlayer().getBlockPos(),
BlockPlacementType.SECTION_NO_UPDATE // TODO: placement type argument
);
LOGGER.info(String.format("Placed schematic %s from namespace %s at %s in world %s", id, ns, player.getBlockPos(), player.getServerWorld().getRegistryKey().getValue()));
return 1;
}
// TODO
//LOGGER.info(String.format("Placed schematic %s from namespace %s at %s in world %s", id, ns, player.getBlockPos(), player.getServerWorld().getRegistryKey().getValue()));
return 1;
}
}

View file

@ -0,0 +1,49 @@
package org.dimdev.dimdoors.command.arguments;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import net.minecraft.command.CommandSource;
import net.minecraft.util.Identifier;
import org.dimdev.dimdoors.pockets.PocketLoader;
import org.dimdev.dimdoors.pockets.PocketTemplate;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.TreeSet;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
public class PocketTemplateArgumentType implements ArgumentType<PocketTemplate> {
@Override
public PocketTemplate parse(StringReader reader) throws CommandSyntaxException {
Identifier value = Identifier.tryParse(Objects.requireNonNull(reader.readString()));
if (!getPocketTemplates().containsKey(value)) {
// TODO: throw
}
return getPocketTemplates().get(value);
}
@Override
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
return CommandSource.suggestMatching(getExamples(), builder);
}
@Override
public Collection<String> getExamples() {
return getPocketTemplates().keySet().parallelStream().map(Identifier::toString).map(id -> "\"" + id + "\"").collect(Collectors.toCollection(TreeSet::new));
}
private Map<Identifier, PocketTemplate> getPocketTemplates() {
return PocketLoader.getInstance().getTemplates();
}
public static PocketTemplate getValue(CommandContext<?> context, final String name) {
return context.getArgument(name, PocketTemplate.class);
}
}