This commit is contained in:
SD 2021-03-01 17:06:33 +05:30
parent dac3a2a60b
commit 9f9813ec8c
No known key found for this signature in database
GPG key ID: E36B57EE08544BC5
8 changed files with 93 additions and 44 deletions

View file

@ -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<Path> CONFIG_ROOT = () -> FabricLoader.getInstance().getConfigDir().resolve("dimdoors").toAbsolutePath();
public static final ConfigHolder<ModConfig> 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);

View file

@ -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();
}

View file

@ -28,10 +28,10 @@ import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
public final class DoorData implements AutoCloseable {
public static final List<Block> 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<? super EntranceRiftBlockEntity> 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;
}

View file

@ -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<Path> 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);
}
}
}

View file

@ -17,6 +17,6 @@ public enum AlwaysTrueCondition implements Condition {
@Override
public Condition.ConditionType<?> getType() {
return null;
return ConditionType.ALWAYS_TRUE;
}
}

View file

@ -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;
}

View file

@ -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();

View file

@ -14,7 +14,6 @@
"ServerPlayerInteractionManagerMixin",
"accessor.BuiltinBiomesAccessor",
"accessor.ChunkGeneratorAccessor",
"accessor.ChunkGeneratorSettingsAccessor",
"accessor.DefaultParticleTypeAccessor",
"accessor.DirectionAccessor",
"accessor.EntityAccessor",