Make PocketTemplate s store their id

This commit is contained in:
SD 2021-03-31 16:44:14 +05:30
parent 38ab66af2a
commit 7b267ecadc
3 changed files with 19 additions and 15 deletions

View file

@ -6,18 +6,14 @@ import java.io.IOException;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer; import java.util.function.Consumer;
import com.google.common.collect.HashBiMap;
import com.mojang.brigadier.Command; import com.mojang.brigadier.Command;
import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.sk89q.jnbt.NBTInputStream; import com.sk89q.jnbt.NBTInputStream;
import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.extent.clipboard.Clipboard; import com.sk89q.worldedit.extent.clipboard.Clipboard;
import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat;
import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormats;
import com.sk89q.worldedit.extent.clipboard.io.SpongeSchematicReader; import com.sk89q.worldedit.extent.clipboard.io.SpongeSchematicReader;
import com.sk89q.worldedit.fabric.FabricAdapter; import com.sk89q.worldedit.fabric.FabricAdapter;
import com.sk89q.worldedit.function.operation.Operations;
import com.sk89q.worldedit.session.ClipboardHolder; import com.sk89q.worldedit.session.ClipboardHolder;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
@ -25,7 +21,6 @@ import org.dimdev.dimdoors.DimensionalDoorsInitializer;
import org.dimdev.dimdoors.api.util.BlockPlacementType; import org.dimdev.dimdoors.api.util.BlockPlacementType;
import org.dimdev.dimdoors.command.arguments.EnumArgumentType; import org.dimdev.dimdoors.command.arguments.EnumArgumentType;
import org.dimdev.dimdoors.command.arguments.PocketTemplateArgumentType; import org.dimdev.dimdoors.command.arguments.PocketTemplateArgumentType;
import org.dimdev.dimdoors.pockets.PocketLoader;
import org.dimdev.dimdoors.pockets.PocketTemplate; import org.dimdev.dimdoors.pockets.PocketTemplate;
import org.dimdev.dimdoors.util.schematic.Schematic; import org.dimdev.dimdoors.util.schematic.Schematic;
import org.dimdev.dimdoors.util.schematic.SchematicPlacer; import org.dimdev.dimdoors.util.schematic.SchematicPlacer;
@ -95,7 +90,7 @@ public class PocketCommand {
throw new RuntimeException(e); // Can't happen, the stream is a ByteArrayInputStream throw new RuntimeException(e); // Can't happen, the stream is a ByteArrayInputStream
} }
WorldEdit.getInstance().getSessionManager().get(FabricAdapter.adaptPlayer(player)).setClipboard(new ClipboardHolder(clipboard)); WorldEdit.getInstance().getSessionManager().get(FabricAdapter.adaptPlayer(player)).setClipboard(new ClipboardHolder(clipboard));
messageSender.accept(new TranslatableText("commands.pocket.loadedSchem", HashBiMap.create(PocketLoader.getInstance().getTemplates()).inverse().get(template).reduce(String::concat).get())); messageSender.accept(new TranslatableText("commands.pocket.loadedSchem", template.getId()));
}; };
if (async) { if (async) {
CompletableFuture.runAsync(task); CompletableFuture.runAsync(task);
@ -113,7 +108,7 @@ public class PocketCommand {
blockPlacementType blockPlacementType
); );
String id = HashBiMap.create(PocketLoader.getInstance().getTemplates()).inverse().get(template).reduce(String::concat).get(); String id = template.getId().toString();
source.getCommandSource().sendFeedback(new TranslatableText("commands.pocket.placedSchem", id, "" + source.getBlockPos().getX() + ", " + source.getBlockPos().getY() + ", " + source.getBlockPos().getZ(), source.world.getRegistryKey().getValue().toString()), true); source.getCommandSource().sendFeedback(new TranslatableText("commands.pocket.placedSchem", id, "" + source.getBlockPos().getX() + ", " + source.getBlockPos().getY() + ", " + source.getBlockPos().getZ(), source.world.getRegistryKey().getValue().toString()), true);
return Command.SINGLE_SUCCESS; return Command.SINGLE_SUCCESS;
} }

View file

@ -4,7 +4,9 @@ import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.*; import java.util.*;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.function.BiFunction;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import com.google.gson.*; import com.google.gson.*;
@ -83,17 +85,17 @@ public class PocketLoader implements SimpleSynchronousResourceReloadListener {
}); });
} }
private <T> CompletableFuture<SimpleTree<String, T>> loadResourcePathFromCompressedNbtToTree(ResourceManager manager, String startingPath, String extension, Function<CompoundTag, T> reader) { private <T> CompletableFuture<SimpleTree<String, T>> loadResourcePathFromCompressedNbtToTree(ResourceManager manager, String startingPath, String extension, BiFunction<CompoundTag, String, T> reader) {
int sub = startingPath.endsWith("/") ? 0 : 1; int sub = startingPath.endsWith("/") ? 0 : 1;
Function<Identifier, Path<String>> normalizer = id -> Path.stringPath(id.getNamespace() + ":" + id.getPath().substring(0, id.getPath().lastIndexOf(".")).substring(startingPath.length() + sub));
Collection<Identifier> ids = manager.findResources(startingPath, str -> str.endsWith(extension)); Collection<Identifier> ids = manager.findResources(startingPath, str -> str.endsWith(extension));
return CompletableFuture.supplyAsync(() -> { return CompletableFuture.supplyAsync(() -> {
SimpleTree<String, T> tree = new SimpleTree<>(String.class); SimpleTree<String, T> tree = new SimpleTree<>(String.class);
tree.putAll(ids.parallelStream().unordered().collect(Collectors.toConcurrentMap( tree.putAll(ids.parallelStream().unordered().collect(Collectors.toConcurrentMap(
id -> Path.stringPath(id.getNamespace() + ":" + id.getPath().substring(0, id.getPath().lastIndexOf(".")).substring(startingPath.length() + sub)), normalizer,
id -> { id -> {
try { try {
return reader.apply(NbtIo.readCompressed(manager.getResource(id).getInputStream())); return reader.apply(NbtIo.readCompressed(manager.getResource(id).getInputStream()), normalizer.apply(id).reduce(String::concat).get());
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException("Error loading resource: " + id); throw new RuntimeException("Error loading resource: " + id);
} }
@ -139,9 +141,9 @@ public class PocketLoader implements SimpleSynchronousResourceReloadListener {
return PocketGenerator.deserialize(NbtUtil.asCompoundTag(tag, "Could not load PocketGenerator since its json does not represent a CompoundTag!")); return PocketGenerator.deserialize(NbtUtil.asCompoundTag(tag, "Could not load PocketGenerator since its json does not represent a CompoundTag!"));
} }
private PocketTemplate loadPocketTemplate(CompoundTag tag) { private PocketTemplate loadPocketTemplate(CompoundTag tag, String id) {
try { try {
return new PocketTemplate(Schematic.fromTag(tag)); return new PocketTemplate(Schematic.fromTag(tag), new Identifier(id));
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException("Error loading " + tag.toString(), e); throw new RuntimeException("Error loading " + tag.toString(), e);
} }

View file

@ -1,5 +1,6 @@
package org.dimdev.dimdoors.pockets; package org.dimdev.dimdoors.pockets;
import net.minecraft.util.Identifier;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk; import net.minecraft.world.chunk.Chunk;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
@ -21,10 +22,12 @@ public class PocketTemplate {
private static final Logger LOGGER = LogManager.getLogger(); private static final Logger LOGGER = LogManager.getLogger();
private static final boolean replacingPlaceholders = false; private static final boolean replacingPlaceholders = false;
private final Schematic schematic; private final Schematic schematic;
private final Identifier id;
public PocketTemplate(Schematic schematic) { public PocketTemplate(Schematic schematic, Identifier id) {
this.schematic = schematic; this.schematic = schematic;
} this.id = id;
}
/* /*
public void setup(Pocket pocket, VirtualTarget linkTo, LinkProperties linkProperties) { public void setup(Pocket pocket, VirtualTarget linkTo, LinkProperties linkProperties) {
@ -87,4 +90,8 @@ public class PocketTemplate {
public Schematic getSchematic() { public Schematic getSchematic() {
return this.schematic; return this.schematic;
} }
public Identifier getId() {
return id;
}
} }