config fix
This commit is contained in:
@@ -7,7 +7,7 @@ plugins {
|
|||||||
}
|
}
|
||||||
|
|
||||||
version = mod_version
|
version = mod_version
|
||||||
group = 'dev.kxmc'
|
group = 'dev.xkmc'
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
mavenLocal()
|
mavenLocal()
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ neogradle.subsystems.parchment.mappingsVersion=2024.06.02
|
|||||||
|
|
||||||
minecraft_version=1.21.1
|
minecraft_version=1.21.1
|
||||||
minecraft_version_range=[1.21.1,1.22)
|
minecraft_version_range=[1.21.1,1.22)
|
||||||
neo_version=21.1.61
|
neo_version=21.1.93
|
||||||
neo_version_range=[21.1.4,)
|
neo_version_range=[21.1.4,)
|
||||||
loader_version_range=[2,)
|
loader_version_range=[2,)
|
||||||
|
|
||||||
@@ -18,7 +18,7 @@ loader_version_range=[2,)
|
|||||||
mod_id=l2core
|
mod_id=l2core
|
||||||
mod_name=L2Core
|
mod_name=L2Core
|
||||||
mod_license=LGPL-2.1
|
mod_license=LGPL-2.1
|
||||||
mod_version=3.0.8+3
|
mod_version=3.0.8+8
|
||||||
mod_group_id=dev.xkmc
|
mod_group_id=dev.xkmc
|
||||||
mod_authors=lcy0x1
|
mod_authors=lcy0x1
|
||||||
mod_description=Core Library mod for all L2 mods
|
mod_description=Core Library mod for all L2 mods
|
||||||
|
|||||||
@@ -33,7 +33,12 @@ public interface EnchVal {
|
|||||||
ResourceKey<Enchantment> id();
|
ResourceKey<Enchantment> id();
|
||||||
|
|
||||||
@DataGenOnly
|
@DataGenOnly
|
||||||
Holder<Enchantment> datagenDirect(RegistrateProvider pvd);
|
Holder<Enchantment> datagenDirect();
|
||||||
|
|
||||||
|
@DataGenOnly
|
||||||
|
default Holder<Enchantment> datagenDirect(RegistrateProvider pvd) {
|
||||||
|
return datagenDirect();
|
||||||
|
}
|
||||||
|
|
||||||
default Optional<Holder<Enchantment>> safeHolder() {
|
default Optional<Holder<Enchantment>> safeHolder() {
|
||||||
return Optional.ofNullable(CommonHooks.resolveLookup(Registries.ENCHANTMENT)).flatMap(e -> e.get(id()));
|
return Optional.ofNullable(CommonHooks.resolveLookup(Registries.ENCHANTMENT)).flatMap(e -> e.get(id()));
|
||||||
@@ -55,7 +60,7 @@ public interface EnchVal {
|
|||||||
Lazy<Builder> builder();
|
Lazy<Builder> builder();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
default Holder<Enchantment> datagenDirect(RegistrateProvider pvd) {
|
default Holder<Enchantment> datagenDirect() {
|
||||||
var val = builder().get().cache;
|
var val = builder().get().cache;
|
||||||
if (val == null) throw new IllegalStateException("Enchantment is not built yet");
|
if (val == null) throw new IllegalStateException("Enchantment is not built yet");
|
||||||
return new DataGenHolder<>(id(), val);
|
return new DataGenHolder<>(id(), val);
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ public class BaseConfigType<T extends BaseConfig> {
|
|||||||
public final PacketHandlerWithConfig parent;
|
public final PacketHandlerWithConfig parent;
|
||||||
|
|
||||||
final Map<ResourceLocation, T> configs = new HashMap<>();
|
final Map<ResourceLocation, T> configs = new HashMap<>();
|
||||||
|
final Map<ResourceLocation, T> clientConfigs = new HashMap<>();
|
||||||
|
|
||||||
protected BaseConfigType(PacketHandlerWithConfig parent, String id, Class<T> cls) {
|
protected BaseConfigType(PacketHandlerWithConfig parent, String id, Class<T> cls) {
|
||||||
this.parent = parent;
|
this.parent = parent;
|
||||||
@@ -26,4 +27,11 @@ public class BaseConfigType<T extends BaseConfig> {
|
|||||||
public void afterReload() {
|
public void afterReload() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void clientBeforeReload() {
|
||||||
|
clientConfigs.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void clientAfterReload() {
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package dev.xkmc.l2core.serial.config;
|
package dev.xkmc.l2core.serial.config;
|
||||||
|
|
||||||
import com.google.gson.JsonElement;
|
import com.google.gson.JsonElement;
|
||||||
|
import com.tterrag.registrate.providers.ProviderType;
|
||||||
import dev.xkmc.l2serial.serialization.codec.JsonCodec;
|
import dev.xkmc.l2serial.serialization.codec.JsonCodec;
|
||||||
import net.minecraft.core.HolderLookup;
|
import net.minecraft.core.HolderLookup;
|
||||||
import net.minecraft.data.CachedOutput;
|
import net.minecraft.data.CachedOutput;
|
||||||
|
|||||||
@@ -1,16 +1,23 @@
|
|||||||
package dev.xkmc.l2core.serial.config;
|
package dev.xkmc.l2core.serial.config;
|
||||||
|
|
||||||
|
import dev.xkmc.l2core.util.ServerProxy;
|
||||||
import net.minecraft.resources.ResourceLocation;
|
import net.minecraft.resources.ResourceLocation;
|
||||||
|
|
||||||
public class MergedConfigType<T extends BaseConfig> extends BaseConfigType<T> {
|
public class MergedConfigType<T extends BaseConfig> extends BaseConfigType<T> {
|
||||||
|
|
||||||
private T result;
|
private T result, clientResult;
|
||||||
|
|
||||||
MergedConfigType(PacketHandlerWithConfig parent, String id, Class<T> cls) {
|
MergedConfigType(PacketHandlerWithConfig parent, String id, Class<T> cls) {
|
||||||
super(parent, id, cls);
|
super(parent, id, cls);
|
||||||
}
|
}
|
||||||
|
|
||||||
T load() {
|
T load() {
|
||||||
|
if (ServerProxy.isOnClient())
|
||||||
|
return loadClient();
|
||||||
|
return loadServer();
|
||||||
|
}
|
||||||
|
|
||||||
|
T loadServer() {
|
||||||
if (result != null) {
|
if (result != null) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@@ -19,11 +26,28 @@ public class MergedConfigType<T extends BaseConfig> extends BaseConfigType<T> {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
T loadClient() {
|
||||||
|
if (clientResult != null) {
|
||||||
|
return clientResult;
|
||||||
|
}
|
||||||
|
clientResult = new ConfigMerger<>(cls).apply(clientConfigs.values());
|
||||||
|
clientResult.id = ResourceLocation.fromNamespaceAndPath(parent.modid, id);
|
||||||
|
return clientResult;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void afterReload() {
|
public void afterReload() {
|
||||||
result = null;
|
result = null;
|
||||||
if (cls.isAnnotationPresent(ConfigLoadOnStart.class)) {
|
if (cls.isAnnotationPresent(ConfigLoadOnStart.class)) {
|
||||||
load();
|
loadServer();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void clientAfterReload() {
|
||||||
|
clientResult = null;
|
||||||
|
if (cls.isAnnotationPresent(ConfigLoadOnStart.class)) {
|
||||||
|
loadClient();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -114,25 +114,30 @@ public class PacketHandlerWithConfig extends PacketHandler {
|
|||||||
private <T extends BaseConfig> void addJson(BaseConfigType<T> type, ResourceLocation k, JsonElement v) {
|
private <T extends BaseConfig> void addJson(BaseConfigType<T> type, ResourceLocation k, JsonElement v) {
|
||||||
T config = new JsonCodec(getRegistryLookup()).from(v, type.cls, null);
|
T config = new JsonCodec(getRegistryLookup()).from(v, type.cls, null);
|
||||||
if (config != null) {
|
if (config != null) {
|
||||||
addConfig(type, k, config);
|
addServerConfig(type, k, config);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private <T extends BaseConfig> void addConfig(BaseConfigType<T> type, ResourceLocation k, T config) {
|
private <T extends BaseConfig> void addServerConfig(BaseConfigType<T> type, ResourceLocation k, T config) {
|
||||||
config.id = k;
|
config.id = k;
|
||||||
type.configs.put(k, config);
|
type.configs.put(k, config);
|
||||||
configs.add(new ConfigInstance(type.id, k, config));
|
configs.add(new ConfigInstance(type.id, k, config));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private <T extends BaseConfig> void addClientConfig(BaseConfigType<T> type, ResourceLocation k, T config) {
|
||||||
|
config.id = k;
|
||||||
|
type.clientConfigs.put(k, config);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called on client side only
|
* Called on client side only
|
||||||
*/
|
*/
|
||||||
public void apply(ArrayList<ConfigInstance> list) {
|
public void apply(ArrayList<ConfigInstance> list) {
|
||||||
listener_before.forEach(Runnable::run);
|
types.values().forEach(BaseConfigType::clientBeforeReload);
|
||||||
for (var e : list) {
|
for (var e : list) {
|
||||||
addConfig(types.get(e.name), e.id(), Wrappers.cast(e.config));
|
addClientConfig(types.get(e.name), e.id(), Wrappers.cast(e.config));
|
||||||
}
|
}
|
||||||
listener_after.forEach(Runnable::run);
|
types.values().forEach(BaseConfigType::clientAfterReload);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,53 @@
|
|||||||
|
package dev.xkmc.l2core.serial.config;
|
||||||
|
|
||||||
|
import com.tterrag.registrate.AbstractRegistrate;
|
||||||
|
import com.tterrag.registrate.providers.ProviderType;
|
||||||
|
import com.tterrag.registrate.providers.RegistrateProvider;
|
||||||
|
import net.minecraft.core.HolderLookup;
|
||||||
|
import net.minecraft.data.CachedOutput;
|
||||||
|
import net.minecraft.data.DataGenerator;
|
||||||
|
import net.minecraft.data.DataProvider;
|
||||||
|
import net.neoforged.fml.LogicalSide;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.function.BiFunction;
|
||||||
|
|
||||||
|
public class RegistrateNestedProvider implements RegistrateProvider {
|
||||||
|
|
||||||
|
public static final ProviderType<RegistrateNestedProvider> TYPE = ProviderType.registerProvider("l2_custom", RegistrateNestedProvider::new);
|
||||||
|
|
||||||
|
private final List<DataProvider> list = new ArrayList<>();
|
||||||
|
private final AbstractRegistrate<?> reg;
|
||||||
|
private final DataGenerator gen;
|
||||||
|
private final CompletableFuture<HolderLookup.Provider> pvd;
|
||||||
|
|
||||||
|
public RegistrateNestedProvider(ProviderType.Context<RegistrateNestedProvider> ctx) {
|
||||||
|
this.reg = ctx.parent();
|
||||||
|
this.gen = ctx.event().getGenerator();
|
||||||
|
this.pvd = ctx.provider();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public LogicalSide getSide() {
|
||||||
|
return LogicalSide.SERVER;
|
||||||
|
}
|
||||||
|
|
||||||
|
public RegistrateNestedProvider add(BiFunction<DataGenerator, CompletableFuture<HolderLookup.Provider>, DataProvider> factory) {
|
||||||
|
list.add(factory.apply(gen, pvd));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CompletableFuture<?> run(CachedOutput cachedOutput) {
|
||||||
|
reg.genData(TYPE, this);
|
||||||
|
return CompletableFuture.allOf(list.stream().map(e -> e.run(cachedOutput)).toArray(CompletableFuture[]::new));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return "Custom Registrate Provider";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,13 +1,12 @@
|
|||||||
package dev.xkmc.l2core.util;
|
package dev.xkmc.l2core.util;
|
||||||
|
|
||||||
|
import com.mojang.blaze3d.systems.RenderSystem;
|
||||||
import net.minecraft.client.Minecraft;
|
import net.minecraft.client.Minecraft;
|
||||||
import net.minecraft.client.player.LocalPlayer;
|
import net.minecraft.client.player.LocalPlayer;
|
||||||
import net.minecraft.core.RegistryAccess;
|
|
||||||
import net.minecraft.server.MinecraftServer;
|
import net.minecraft.server.MinecraftServer;
|
||||||
import net.minecraft.world.entity.player.Player;
|
import net.minecraft.world.entity.player.Player;
|
||||||
import net.minecraft.world.level.Level;
|
import net.minecraft.world.level.Level;
|
||||||
import net.neoforged.api.distmarker.Dist;
|
import net.neoforged.api.distmarker.Dist;
|
||||||
import net.neoforged.api.distmarker.OnlyIn;
|
|
||||||
import net.neoforged.fml.loading.FMLEnvironment;
|
import net.neoforged.fml.loading.FMLEnvironment;
|
||||||
import net.neoforged.neoforge.server.ServerLifecycleHooks;
|
import net.neoforged.neoforge.server.ServerLifecycleHooks;
|
||||||
|
|
||||||
@@ -36,6 +35,7 @@ public class Proxy {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Deprecated
|
||||||
public static Optional<MinecraftServer> getServer() {
|
public static Optional<MinecraftServer> getServer() {
|
||||||
return Optional.ofNullable(ServerLifecycleHooks.getCurrentServer());
|
return Optional.ofNullable(ServerLifecycleHooks.getCurrentServer());
|
||||||
}
|
}
|
||||||
@@ -45,4 +45,7 @@ public class Proxy {
|
|||||||
return Minecraft.getInstance().player;
|
return Minecraft.getInstance().player;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static boolean isOnClient() {
|
||||||
|
return getServer().isEmpty() || RenderSystem.isOnRenderThread();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,14 @@
|
|||||||
package dev.xkmc.l2core.util;
|
package dev.xkmc.l2core.util;
|
||||||
|
|
||||||
import net.minecraft.core.RegistryAccess;
|
import net.minecraft.core.RegistryAccess;
|
||||||
|
import net.minecraft.server.MinecraftServer;
|
||||||
import net.minecraft.world.level.Level;
|
import net.minecraft.world.level.Level;
|
||||||
import net.neoforged.api.distmarker.Dist;
|
import net.neoforged.api.distmarker.Dist;
|
||||||
import net.neoforged.fml.loading.FMLEnvironment;
|
import net.neoforged.fml.loading.FMLEnvironment;
|
||||||
import net.neoforged.neoforge.server.ServerLifecycleHooks;
|
import net.neoforged.neoforge.server.ServerLifecycleHooks;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
public class ServerProxy {
|
public class ServerProxy {
|
||||||
|
|
||||||
@@ -25,4 +27,13 @@ public class ServerProxy {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Optional<MinecraftServer> getServer() {
|
||||||
|
return Optional.ofNullable(ServerLifecycleHooks.getCurrentServer());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isOnClient() {
|
||||||
|
if (FMLEnvironment.dist == Dist.DEDICATED_SERVER) return false;
|
||||||
|
return Proxy.isOnClient();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user