From 9f9813ec8c7fc86fcf647ee0f29ae5db2deb61a8 Mon Sep 17 00:00:00 2001 From: SD Date: Mon, 1 Mar 2021 17:06:33 +0530 Subject: [PATCH] commit --- .../dimdoors/DimensionalDoorsInitializer.java | 9 ++- .../org/dimdev/dimdoors/block/ModBlocks.java | 9 +-- .../dimdev/dimdoors/block/door/DoorData.java | 36 ++++++--- .../dimdoors/block/door/DoorDataReader.java | 74 +++++++++++++------ .../door/condition/AlwaysTrueCondition.java | 2 +- .../block/door/condition/Condition.java | 2 +- .../dimdoors/rift/targets/VirtualTarget.java | 4 +- src/main/resources/dimdoors.mixins.json | 1 - 8 files changed, 93 insertions(+), 44 deletions(-) diff --git a/src/main/java/org/dimdev/dimdoors/DimensionalDoorsInitializer.java b/src/main/java/org/dimdev/dimdoors/DimensionalDoorsInitializer.java index 000581f5..47137521 100644 --- a/src/main/java/org/dimdev/dimdoors/DimensionalDoorsInitializer.java +++ b/src/main/java/org/dimdev/dimdoors/DimensionalDoorsInitializer.java @@ -1,10 +1,12 @@ package org.dimdev.dimdoors; import java.nio.file.Path; +import java.util.function.Supplier; import me.sargunvohra.mcmods.autoconfig1u.AutoConfig; import me.sargunvohra.mcmods.autoconfig1u.ConfigHolder; import org.dimdev.dimdoors.block.ModBlocks; +import org.dimdev.dimdoors.block.door.DoorDataReader; import org.dimdev.dimdoors.block.door.condition.Condition; import org.dimdev.dimdoors.block.entity.ModBlockEntityTypes; import org.dimdev.dimdoors.command.ModCommands; @@ -58,10 +60,10 @@ import net.fabricmc.loader.api.FabricLoader; import net.fabricmc.loader.api.ModContainer; public class DimensionalDoorsInitializer implements ModInitializer { + private static final Supplier CONFIG_ROOT = () -> FabricLoader.getInstance().getConfigDir().resolve("dimdoors").toAbsolutePath(); public static final ConfigHolder CONFIG_MANAGER = AutoConfig.register(ModConfig.class, SubRootJanksonConfigSerializer::new); private static MinecraftServer server; private static ModContainer dimDoorsMod; - private static Path configRoot; @NotNull public static MinecraftServer getServer() { @@ -84,13 +86,12 @@ public class DimensionalDoorsInitializer implements ModInitializer { } public static Path getConfigRoot() { - return configRoot; + return CONFIG_ROOT.get(); } @Override public void onInitialize() { dimDoorsMod = FabricLoader.getInstance().getModContainer("dimdoors").orElseThrow(RuntimeException::new); - configRoot = FabricLoader.getInstance().getConfigDir().resolve("dimdoors"); ServerLifecycleEvents.SERVER_STARTING.register((minecraftServer) -> { server = minecraftServer; }); @@ -117,6 +118,8 @@ public class DimensionalDoorsInitializer implements ModInitializer { PocketAddon.PocketAddonType.register(); Condition.ConditionType.register(); + DoorDataReader.read(); + ResourceManagerHelper.get(ResourceType.SERVER_DATA).registerReloadListener(PocketLoader.getInstance()); ResourceManagerHelper.registerBuiltinResourcePack(new Identifier("dimdoors", "default_pockets"), dimDoorsMod, ResourcePackActivationType.DEFAULT_ENABLED); diff --git a/src/main/java/org/dimdev/dimdoors/block/ModBlocks.java b/src/main/java/org/dimdev/dimdoors/block/ModBlocks.java index 10bd7238..e48b101a 100644 --- a/src/main/java/org/dimdev/dimdoors/block/ModBlocks.java +++ b/src/main/java/org/dimdev/dimdoors/block/ModBlocks.java @@ -30,10 +30,10 @@ public final class ModBlocks { public static final Block GOLD_DOOR = register("dimdoors:gold_door", new DoorBlock(FabricBlockSettings.of(Material.METAL, MapColor.GOLD).strength(5.0F).breakByHand(false).breakByTool(FabricToolTags.PICKAXES).nonOpaque())); public static final Block QUARTZ_DOOR = register("dimdoors:quartz_door", new DoorBlock(FabricBlockSettings.of(Material.STONE, MapColor.OFF_WHITE).strength(5.0F).breakByHand(false).breakByTool(FabricToolTags.PICKAXES).nonOpaque())); - public static final Block OAK_DIMENSIONAL_DOOR = register("dimdoors:oak_dimensional_door", new DimensionalDoorBlock(FabricBlockSettings.copyOf(Blocks.OAK_DOOR).luminance(state -> 10))); - public static final Block IRON_DIMENSIONAL_DOOR = register("dimdoors:iron_dimensional_door", new DimensionalDoorBlock(FabricBlockSettings.copyOf(Blocks.IRON_DOOR).luminance(state -> 10))); - public static final Block GOLD_DIMENSIONAL_DOOR = register("dimdoors:gold_dimensional_door", new DimensionalDoorBlock(FabricBlockSettings.copyOf(GOLD_DOOR).luminance(state -> 10))); - public static final Block QUARTZ_DIMENSIONAL_DOOR = register("dimdoors:quartz_dimensional_door", new DimensionalDoorBlock(FabricBlockSettings.copy(QUARTZ_DOOR).luminance(state -> 10))); +// public static final Block OAK_DIMENSIONAL_DOOR = register("dimdoors:oak_dimensional_door", new DimensionalDoorBlock(FabricBlockSettings.copyOf(Blocks.OAK_DOOR).luminance(state -> 10))); +// public static final Block IRON_DIMENSIONAL_DOOR = register("dimdoors:iron_dimensional_door", new DimensionalDoorBlock(FabricBlockSettings.copyOf(Blocks.IRON_DOOR).luminance(state -> 10))); +// public static final Block GOLD_DIMENSIONAL_DOOR = register("dimdoors:gold_dimensional_door", new DimensionalDoorBlock(FabricBlockSettings.copyOf(GOLD_DOOR).luminance(state -> 10))); +// public static final Block QUARTZ_DIMENSIONAL_DOOR = register("dimdoors:quartz_dimensional_door", new DimensionalDoorBlock(FabricBlockSettings.copy(QUARTZ_DOOR).luminance(state -> 10))); public static final Block OAK_DIMENSIONAL_TRAPDOOR = register("dimdoors:wood_dimensional_trapdoor", new DimensionalTrapdoorBlock(FabricBlockSettings.copyOf(Blocks.OAK_TRAPDOOR).luminance(state -> 10))); public static final Block DIMENSIONAL_PORTAL = register("dimdoors:dimensional_portal", new DimensionalPortalBlock(FabricBlockSettings.of(Material.AIR).collidable(false).strength(-1.0F, 3600000.0F).nonOpaque().dropsNothing().luminance(10))); @@ -99,7 +99,6 @@ public final class ModBlocks { BLOCKS.forEach((str, block) -> { Registry.register(Registry.BLOCK, str, block); }); - DoorDataReader.read(); BLOCKS.clear(); } diff --git a/src/main/java/org/dimdev/dimdoors/block/door/DoorData.java b/src/main/java/org/dimdev/dimdoors/block/door/DoorData.java index 3211a121..b7e82b8a 100644 --- a/src/main/java/org/dimdev/dimdoors/block/door/DoorData.java +++ b/src/main/java/org/dimdev/dimdoors/block/door/DoorData.java @@ -28,10 +28,10 @@ import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; public final class DoorData implements AutoCloseable { public static final List DOORS = new ArrayList<>(); - private String id; - private UnbakedItemSettings itemSettings; - private UnbakedBlockSettings blockSettings; - private RiftDataList riftDataList; + private final String id; + private final UnbakedItemSettings itemSettings; + private final UnbakedBlockSettings blockSettings; + private final RiftDataList riftDataList; private boolean closed = false; public static DoorData fromJson(JsonObject json) { @@ -51,17 +51,38 @@ public final class DoorData implements AutoCloseable { public JsonObject toJson(JsonObject json) { json.addProperty("id", this.id); + json.add("itemSettings", this.itemSettings.toJson(new JsonObject())); + json.add("blockSettings", this.blockSettings.toJson(new JsonObject())); + json.add("riftData", this.riftDataList.toJson()); return json; } private Consumer createSetupFunction() { + RiftDataList riftDataList = this.riftDataList; + return rift -> { - RiftDataList.OptRiftData riftData = this.riftDataList.getRiftData(rift); + RiftDataList.OptRiftData riftData = riftDataList.getRiftData(rift); riftData.getDestination().ifPresent(rift::setDestination); riftData.getProperties().ifPresent(rift::setProperties); }; } + public String getId() { + return id; + } + + public UnbakedItemSettings getItemSettings() { + return itemSettings; + } + + public UnbakedBlockSettings getBlockSettings() { + return blockSettings; + } + + public RiftDataList getRiftDataList() { + return riftDataList; + } + @Override public void close() { if (closed) { @@ -87,11 +108,6 @@ public final class DoorData implements AutoCloseable { Registry.register(Registry.BLOCK, id, doorBlock); Registry.register(Registry.ITEM, id, doorItem); DOORS.add(doorBlock); - - this.id = null; - this.blockSettings = null; - this.itemSettings = null; - this.riftDataList = null; this.closed = true; } diff --git a/src/main/java/org/dimdev/dimdoors/block/door/DoorDataReader.java b/src/main/java/org/dimdev/dimdoors/block/door/DoorDataReader.java index 1aafe763..23208cce 100644 --- a/src/main/java/org/dimdev/dimdoors/block/door/DoorDataReader.java +++ b/src/main/java/org/dimdev/dimdoors/block/door/DoorDataReader.java @@ -8,9 +8,11 @@ import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.LinkedList; +import java.util.List; import java.util.Optional; import java.util.OptionalInt; import java.util.stream.Collectors; +import java.util.stream.Stream; import com.google.gson.Gson; import com.google.gson.GsonBuilder; @@ -79,7 +81,7 @@ public class DoorDataReader { ), new RiftDataList(Util.make(new LinkedList<>(), list -> list.add(new Pair<>(new RiftDataList.OptRiftData(Optional.of(RandomTarget.builder().acceptedGroups(Collections.singleton(0)).coordFactor(1).negativeDepthFactor(80).positiveDepthFactor(Double.MAX_VALUE).weightMaximum(100).noLink(false).newRiftWeight(0).build()), Optional.empty()), AlwaysTrueCondition.INSTANCE))) )); private static final DoorData DEFAULT_QUARTZ_DIMENSIONAL_DOOR = new DoorData( - "dimdoors:gold_dimensional_door", + "dimdoors:quartz_dimensional_door", new DoorData.UnbakedItemSettings( Optional.empty(), OptionalInt.of(1), @@ -98,31 +100,50 @@ public class DoorDataReader { )); public static void read() { - try { - Path doorDir = DimensionalDoorsInitializer.getConfigRoot().resolve("doors"); + Path doorDir = DimensionalDoorsInitializer.getConfigRoot().resolve("doors"); - if (Files.notExists(doorDir)) { - writeDefault(doorDir); + if (Files.exists(doorDir) && !Files.isDirectory(doorDir)) { + try { + Files.delete(doorDir); + } catch (IOException e) { + LOGGER.error("Error deleting " + doorDir, e); return; } + } - if (Files.isDirectory(doorDir)) { - for (Path p : Files.list(doorDir).collect(Collectors.toList())) { - if (!Files.isDirectory(p) && Files.isRegularFile(p)) { - String jsonStr = new String(Files.readAllBytes(p), StandardCharsets.UTF_8); - JsonObject json = GSON.fromJson(jsonStr, JsonObject.class); - try (DoorData ignored = DoorData.fromJson(json)) { - LOGGER.info("Loading door json from {}", p.toAbsolutePath().toString()); - } + if (Files.notExists(doorDir)) { + try { + Files.createDirectory(doorDir); + writeDefault(doorDir); + } catch (IOException e) { + LOGGER.error("Error creating directory " + doorDir, e); + return; + } + } + + if (Files.isDirectory(doorDir)) { + List paths; + try { + paths = Files.list(doorDir).collect(Collectors.toList()); + } catch (IOException e) { + LOGGER.error("Error retrieving paths in directory " + doorDir, e); + return; + } + for (Path p : paths) { + if (!Files.isDirectory(p) && Files.isRegularFile(p)) { + String jsonStr; + try { + jsonStr = new String(Files.readAllBytes(p), StandardCharsets.UTF_8); + } catch (IOException e) { + LOGGER.error("Error reading " + p, e); + return; + } + JsonObject json = GSON.fromJson(jsonStr, JsonObject.class); + try (DoorData ignored = DoorData.fromJson(json)) { + LOGGER.info("Loaded door json from {} with id {}", p.toAbsolutePath().toString(), ignored.getId()); } } - } else { - Files.delete(doorDir); - writeDefault(doorDir); } - } catch (IOException | RuntimeException e) { - LOGGER.error("Error leading door json", e); - e.printStackTrace(); } } @@ -133,9 +154,18 @@ public class DoorDataReader { writeDefault(root.resolve("quartz_dimensional_door.json"), DEFAULT_QUARTZ_DIMENSIONAL_DOOR); } - private static void writeDefault(Path path, DoorData doorData) throws IOException { - Files.createFile(path); + private static void writeDefault(Path path, DoorData doorData) { + try { + Files.createFile(path); + } catch (IOException e) { + LOGGER.error("Error creating " + path, e); + return; + } String json = GSON.toJson(doorData.toJson(new JsonObject())); - Files.write(path, json.getBytes(StandardCharsets.UTF_8)); + try { + Files.write(path, json.getBytes(StandardCharsets.UTF_8)); + } catch (IOException e) { + LOGGER.error("Error writing to " + path, e); + } } } diff --git a/src/main/java/org/dimdev/dimdoors/block/door/condition/AlwaysTrueCondition.java b/src/main/java/org/dimdev/dimdoors/block/door/condition/AlwaysTrueCondition.java index f356c896..b087a150 100644 --- a/src/main/java/org/dimdev/dimdoors/block/door/condition/AlwaysTrueCondition.java +++ b/src/main/java/org/dimdev/dimdoors/block/door/condition/AlwaysTrueCondition.java @@ -17,6 +17,6 @@ public enum AlwaysTrueCondition implements Condition { @Override public Condition.ConditionType getType() { - return null; + return ConditionType.ALWAYS_TRUE; } } diff --git a/src/main/java/org/dimdev/dimdoors/block/door/condition/Condition.java b/src/main/java/org/dimdev/dimdoors/block/door/condition/Condition.java index 6bc9938a..660434ca 100644 --- a/src/main/java/org/dimdev/dimdoors/block/door/condition/Condition.java +++ b/src/main/java/org/dimdev/dimdoors/block/door/condition/Condition.java @@ -20,7 +20,7 @@ public interface Condition { boolean matches(EntranceRiftBlockEntity rift); default JsonObject toJson(JsonObject json) { - json.addProperty("type", "dimdoors:" + getType().getId()); + json.addProperty("type", getType().getId()); this.toJsonInner(json); return json; } diff --git a/src/main/java/org/dimdev/dimdoors/rift/targets/VirtualTarget.java b/src/main/java/org/dimdev/dimdoors/rift/targets/VirtualTarget.java index 1f53c2b6..5ddc39ce 100644 --- a/src/main/java/org/dimdev/dimdoors/rift/targets/VirtualTarget.java +++ b/src/main/java/org/dimdev/dimdoors/rift/targets/VirtualTarget.java @@ -26,7 +26,8 @@ public abstract class VirtualTarget implements Target { protected Location location; public static VirtualTarget fromTag(CompoundTag nbt) { - return Objects.requireNonNull(REGISTRY.get(new Identifier(nbt.getString("type")))).fromTag(nbt); + Identifier id = new Identifier(nbt.getString("type")); + return Objects.requireNonNull(REGISTRY.get(id), "Unknown virtual target type " + id.toString()).fromTag(nbt); } public static CompoundTag toTag(VirtualTarget virtualTarget) { @@ -126,6 +127,7 @@ public abstract class VirtualTarget implements Target { } } + @SuppressWarnings("EqualsWhichDoesntCheckParameterClass") public static class NoneTarget extends VirtualTarget { public static final NoneTarget INSTANCE = new NoneTarget(); diff --git a/src/main/resources/dimdoors.mixins.json b/src/main/resources/dimdoors.mixins.json index b46fc422..c9f788dc 100644 --- a/src/main/resources/dimdoors.mixins.json +++ b/src/main/resources/dimdoors.mixins.json @@ -14,7 +14,6 @@ "ServerPlayerInteractionManagerMixin", "accessor.BuiltinBiomesAccessor", "accessor.ChunkGeneratorAccessor", - "accessor.ChunkGeneratorSettingsAccessor", "accessor.DefaultParticleTypeAccessor", "accessor.DirectionAccessor", "accessor.EntityAccessor",