Skip to content

Commit

Permalink
Some fixes after testing
Browse files Browse the repository at this point in the history
  • Loading branch information
mschae23 committed Nov 30, 2024
1 parent 4593ba5 commit 43b481e
Show file tree
Hide file tree
Showing 15 changed files with 200 additions and 61 deletions.
8 changes: 4 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ org.gradle.jvmargs=-Xmx1G

# Fabric Properties
# check these on https://fabricmc.net/versions.html
minecraft_version=1.21.4-pre3
yarn_mappings=1.21.4-pre3+build.2
minecraft_version=1.21.4-rc3
yarn_mappings=1.21.4-rc3+build.3
loader_version=0.16.9

# Mod Properties
mod_version = 4.0.0
mod_version = 4.0.0-beta.1
maven_group = de.mschae23.minecraft.mod
archives_base_name = grind-enchantments

# Dependencies
fabric_api_version=0.110.0+1.21.4
fabric_api_version=0.110.2+1.21.4
codec_config_api_version=3.0.0+1.21.3
tax_free_levels_version=1.4.1-fabric-1.21.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright (C) 2024 mschae23
*
* This file is part of Grind enchantments.
*
* Grind enchantments is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package de.mschae23.grindenchantments;

import java.util.stream.Stream;
import net.minecraft.registry.RegistryWrapper;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientConfigurationConnectionEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientConfigurationNetworking;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
import de.mschae23.grindenchantments.config.ClientConfig;
import de.mschae23.grindenchantments.config.sync.ServerConfigS2CPayload;
import de.mschae23.grindenchantments.registry.GrindEnchantmentsRegistries;
import org.apache.logging.log4j.Level;

public class GrindEnchantmentsClient implements ClientModInitializer {
private static ClientConfig CLIENT_CONFIG = ClientConfig.DEFAULT;

@Override
public void onInitializeClient() {
ClientLifecycleEvents.CLIENT_STARTED.register(client -> {
CLIENT_CONFIG = GrindEnchantmentsMod.initializeClientConfig();
GrindEnchantmentsMod.LOCAL_SERVER_CONFIG = GrindEnchantmentsMod.initializeServerConfig(RegistryWrapper.WrapperLookup.of(
Stream.of(GrindEnchantmentsRegistries.COST_FUNCTION)));

ClientConfigurationNetworking.registerGlobalReceiver(ServerConfigS2CPayload.ID, (payload, context) -> {
//noinspection resource
context.client().execute(() -> {
GrindEnchantmentsMod.SERVER_CONFIG = payload.config();

if (CLIENT_CONFIG.sync().logReceivedConfig()) {
GrindEnchantmentsMod.log(Level.INFO, "Received server config: " + GrindEnchantmentsMod.SERVER_CONFIG);
}
});
});
});

ClientPlayConnectionEvents.INIT.register((handler, client2) -> {
if (GrindEnchantmentsMod.SERVER_CONFIG != null) {
GrindEnchantmentsMod.SERVER_CONFIG.validateRegistryEntries(handler.getRegistryManager());
}
});

// Set server config to null when joining a world, so that it is known whether the server sent its config
ClientConfigurationConnectionEvents.INIT.register((handler, client2) -> GrindEnchantmentsMod.SERVER_CONFIG = null);
// Set server config to null when leaving the world too, for the same reason
ClientConfigurationConnectionEvents.DISCONNECT.register((handler, client2) -> GrindEnchantmentsMod.SERVER_CONFIG = null);
ClientPlayConnectionEvents.DISCONNECT.register((handler, client2) -> GrindEnchantmentsMod.SERVER_CONFIG = null);
}

public static ClientConfig getClientConfig() {
return CLIENT_CONFIG;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@
import net.minecraft.registry.RegistryWrapper;
import net.minecraft.util.Identifier;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientConfigurationConnectionEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientConfigurationNetworking;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
import net.fabricmc.fabric.api.networking.v1.ServerConfigurationConnectionEvents;
Expand Down Expand Up @@ -74,10 +70,9 @@ public class GrindEnchantmentsMod implements ModInitializer {
public static final String MODID = "grindenchantments";
public static final Logger LOGGER = LogManager.getLogger("Grind Enchantments");

private static ClientConfig CLIENT_CONFIG = ClientConfig.DEFAULT;
@Nullable
private static ServerConfig SERVER_CONFIG = null;
private static ServerConfig LOCAL_SERVER_CONFIG = ServerConfig.DEFAULT;
static ServerConfig SERVER_CONFIG = null;
static ServerConfig LOCAL_SERVER_CONFIG = ServerConfig.DEFAULT;

@Override
public void onInitialize() {
Expand All @@ -90,43 +85,20 @@ public void onInitialize() {
ServerLifecycleEvents.SERVER_STARTING.register(server -> {
SERVER_CONFIG = initializeServerConfig(server.getRegistryManager());
SERVER_CONFIG.validateRegistryEntries(server.getRegistryManager());
LOCAL_SERVER_CONFIG = SERVER_CONFIG;
});
ServerLifecycleEvents.SERVER_STOPPING.register(server -> SERVER_CONFIG = null);

// Multiplayer
PayloadTypeRegistry.configurationS2C().register(ServerConfigS2CPayload.ID,
ServerConfigS2CPayload.createPacketCodec(CostFunction.createPacketCodec()));

ClientLifecycleEvents.CLIENT_STARTED.register(client -> {
CLIENT_CONFIG = GrindEnchantmentsMod.initializeClientConfig();
LOCAL_SERVER_CONFIG = GrindEnchantmentsMod.initializeServerConfig(RegistryWrapper.WrapperLookup.of(
Stream.of(GrindEnchantmentsRegistries.COST_FUNCTION)));

ClientConfigurationNetworking.registerGlobalReceiver(ServerConfigS2CPayload.ID, (payload, context) -> {
//noinspection resource
context.client().execute(() -> {
// log(Level.DEBUG, payload.config());
SERVER_CONFIG = payload.config();
});
});
ClientPlayConnectionEvents.INIT.register((handler, client2) -> {
if (SERVER_CONFIG != null) {
SERVER_CONFIG.validateRegistryEntries(handler.getRegistryManager());
}
});
});
ServerConfigurationConnectionEvents.CONFIGURE.register((handler, server) -> {
if (ServerConfigurationNetworking.canSend(handler, ServerConfigS2CPayload.ID)) {
ServerConfigurationNetworking.send(handler, new ServerConfigS2CPayload(SERVER_CONFIG != null ? SERVER_CONFIG : LOCAL_SERVER_CONFIG));
}
});

// Set server config to null when joining a world, so that it is known whether the server sent its config
ClientConfigurationConnectionEvents.INIT.register((handler, client) -> SERVER_CONFIG = null);
// Set server config to null when leaving the world too, for the same reason
ClientConfigurationConnectionEvents.DISCONNECT.register((handler, client) -> SERVER_CONFIG = null);
ClientPlayConnectionEvents.DISCONNECT.register((handler, client) -> SERVER_CONFIG = null);

DisenchantOperation disenchant = new DisenchantOperation();
MoveOperation move = new MoveOperation();
ResetRepairCostOperation resetRepairCost = new ResetRepairCostOperation();
Expand All @@ -151,12 +123,12 @@ public void onInitialize() {

public static ServerConfig getServerConfig() {
return SERVER_CONFIG == null ?
CLIENT_CONFIG.useLocalIfUnsynced() ? LOCAL_SERVER_CONFIG : ServerConfig.DISABLED
GrindEnchantmentsClient.getClientConfig().sync().useLocalIfUnsynced() ? LOCAL_SERVER_CONFIG : ServerConfig.DISABLED
: SERVER_CONFIG;
}

public static ClientConfig getClientConfig() {
return CLIENT_CONFIG;
return GrindEnchantmentsClient.getClientConfig();
}

public static <C extends ModConfig<C>> ModConfig.Type<C, ? extends ModConfig<C>> getConfigType(ModConfig.Type<C, ? extends ModConfig<C>>[] versions, int version) {
Expand Down Expand Up @@ -192,7 +164,7 @@ private static <C extends ModConfig<C>> C initializeGenericConfig(Path configNam

ConfigIo.encodeConfig(writer, codec, config.latest(), ops);
} catch (IOException e) {
log(Level.ERROR, "IO exception while trying to write updated config: " + e.getLocalizedMessage());
log(Level.ERROR, "IO exception while trying to write updated " + kind + " config: " + e.getLocalizedMessage());
} catch (ConfigException e) {
log(Level.ERROR, e.getLocalizedMessage());
}
Expand All @@ -203,14 +175,18 @@ private static <C extends ModConfig<C>> C initializeGenericConfig(Path configNam
log(Level.ERROR, e.getLocalizedMessage());
}
} else {
// Write default config if the file doesn't exist
try (OutputStream output = Files.newOutputStream(filePath, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE);
OutputStreamWriter writer = new OutputStreamWriter(new BufferedOutputStream(output))) {
log(Level.INFO, "Writing default " + kind + " config.");
try {
Files.createDirectories(filePath.getParent());

// Write default config if the file doesn't exist
try (OutputStream output = Files.newOutputStream(filePath, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE);
OutputStreamWriter writer = new OutputStreamWriter(new BufferedOutputStream(output))) {
log(Level.INFO, "Writing default " + kind + " config.");

ConfigIo.encodeConfig(writer, codec, latestDefault, ops);
ConfigIo.encodeConfig(writer, codec, latestDefault, ops);
}
} catch (IOException e) {
log(Level.ERROR, "IO exception while trying to write config: " + e.getLocalizedMessage());
log(Level.ERROR, "IO exception while trying to write " + kind + " config: " + e.getLocalizedMessage());
} catch (ConfigException e) {
log(Level.ERROR, e.getLocalizedMessage());
}
Expand All @@ -224,7 +200,7 @@ public static ClientConfig initializeClientConfig() {
JsonOps.INSTANCE, "client");
}

private static ServerConfig initializeServerConfig(RegistryWrapper.WrapperLookup wrapperLookup) {
public static ServerConfig initializeServerConfig(RegistryWrapper.WrapperLookup wrapperLookup) {
return initializeGenericConfig(Path.of("server.json"), ServerConfig.DEFAULT, ServerConfig.CODEC,
RegistryOps.of(JsonOps.INSTANCE, wrapperLookup), "server");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@
import de.mschae23.config.api.ModConfig;
import de.mschae23.grindenchantments.GrindEnchantmentsMod;

public record ClientConfig(boolean showLevelCost, boolean useLocalIfUnsynced) implements ModConfig<ClientConfig> {
public record ClientConfig(boolean showLevelCost, ClientSyncConfig sync) implements ModConfig<ClientConfig> {
public static final MapCodec<ClientConfig> TYPE_CODEC = RecordCodecBuilder.mapCodec(instance -> instance.group(
Codec.BOOL.fieldOf("show_enchantment_cost").forGetter(ClientConfig::showLevelCost),
Codec.BOOL.fieldOf("use_local_server_config_if_unsynced").forGetter(ClientConfig::useLocalIfUnsynced)
ClientSyncConfig.CODEC.fieldOf("sync_options").forGetter(ClientConfig::sync)
).apply(instance, instance.stable(ClientConfig::new)));

public static final ModConfig.Type<ClientConfig, ClientConfig> TYPE = new ModConfig.Type<>(4, TYPE_CODEC);
public static final ClientConfig DEFAULT = new ClientConfig(true, true);
public static final ClientConfig DEFAULT = new ClientConfig(true, ClientSyncConfig.DEFAULT);
@SuppressWarnings("unchecked")
public static final ModConfig.Type<ClientConfig, ? extends ModConfig<ClientConfig>>[] VERSIONS = new ModConfig.Type[] { TYPE, };

Expand All @@ -58,7 +58,7 @@ public boolean shouldUpdate() {
public String toString() {
return "ClientConfig{" +
"showLevelCost=" + this.showLevelCost +
", useLocalIfUnsynced=" + this.useLocalIfUnsynced +
", sync=" + this.sync +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (C) 2024 mschae23
*
* This file is part of Grind enchantments.
*
* Grind enchantments is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package de.mschae23.grindenchantments.config;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;

public record ClientSyncConfig(boolean useLocalIfUnsynced, boolean logReceivedConfig) {
public static final Codec<ClientSyncConfig> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.BOOL.fieldOf("use_local_server_config_if_unsynced").forGetter(ClientSyncConfig::useLocalIfUnsynced),
Codec.BOOL.fieldOf("log_received_config").forGetter(ClientSyncConfig::logReceivedConfig)
).apply(instance, instance.stable(ClientSyncConfig::new)));

public static final ClientSyncConfig DEFAULT = new ClientSyncConfig(true, false);

@Override
public String toString() {
return "ClientSyncConfig{" +
"useLocalIfUnsynced=" + this.useLocalIfUnsynced +
", logReceivedConfig=" + this.logReceivedConfig +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public String toString() {
return "DisenchantConfig{" +
"enabled=" + this.enabled +
", consumeItem=" + this.consumeItem +
", costFunction=" + this.costFunction +
", costConfig=" + this.costFunction +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static PacketCodec<PacketByteBuf, MoveConfig> createPacketCodec(PacketCod
public String toString() {
return "MoveConfig{" +
"enabled=" + this.enabled +
", costFunction=" + this.costFunction +
", costConfig=" + this.costFunction +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public String toString() {
"enabled=" + this.enabled +
", catalystItems=" + this.catalystItems +
", requiresEnchantment=" + this.requiresEnchantment +
", costFunction=" + this.costFunction +
", costConfig=" + this.costFunction +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (C) 2024 mschae23
*
* This file is part of Grind enchantments.
*
* Grind enchantments is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package de.mschae23.grindenchantments.config.legacy.v1;

import com.mojang.serialization.Codec;
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import de.mschae23.grindenchantments.cost.CostFunction;
import de.mschae23.grindenchantments.cost.TransformCostFunction;

@Deprecated
public record CostConfigV1(CostFunction function, double factor, double offset) {
public static final MapCodec<CostConfigV1> CODEC = RecordCodecBuilder.mapCodec(instance -> instance.group(
CostFunction.CODEC.fieldOf("count_mode").forGetter(CostConfigV1::function),
Codec.DOUBLE.fieldOf("cost_factor").orElse(1.0).forGetter(CostConfigV1::factor),
Codec.DOUBLE.fieldOf("cost_offset").orElse(0.0).forGetter(CostConfigV1::offset)
).apply(instance, instance.stable(CostConfigV1::new)));

public CostFunction latest() {
return new TransformCostFunction(this.function, this.factor, this.offset);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,16 @@
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import de.mschae23.grindenchantments.config.DisenchantConfig;
import de.mschae23.grindenchantments.cost.CostFunction;

@Deprecated
public record DisenchantConfigV1(boolean enabled, boolean consumeItem, CostFunction costFunction) {
public record DisenchantConfigV1(boolean enabled, boolean consumeItem, CostConfigV1 costConfig) {
public static final Codec<DisenchantConfigV1> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.BOOL.fieldOf("enabled").forGetter(DisenchantConfigV1::enabled),
Codec.BOOL.fieldOf("consume_enchanted_item").forGetter(DisenchantConfigV1::consumeItem),
CostFunction.CODEC.fieldOf("cost_config").forGetter(DisenchantConfigV1::costFunction)
CostConfigV1.CODEC.fieldOf("cost_config").forGetter(DisenchantConfigV1::costConfig)
).apply(instance, instance.stable(DisenchantConfigV1::new)));

public DisenchantConfig latest() {
return new DisenchantConfig(this.enabled, this.consumeItem, this.costFunction);
return new DisenchantConfig(this.enabled, this.consumeItem, this.costConfig.latest());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,15 @@
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import de.mschae23.grindenchantments.config.MoveConfig;
import de.mschae23.grindenchantments.cost.CostFunction;

@Deprecated
public record MoveConfigV1(boolean enabled, CostFunction costFunction) {
public record MoveConfigV1(boolean enabled, CostConfigV1 costConfig) {
public static final Codec<MoveConfigV1> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.BOOL.fieldOf("enabled").forGetter(MoveConfigV1::enabled),
CostFunction.CODEC.fieldOf("cost_config").forGetter(MoveConfigV1::costFunction)
CostConfigV1.CODEC.fieldOf("cost_config").forGetter(MoveConfigV1::costConfig)
).apply(instance, instance.stable(MoveConfigV1::new)));

public MoveConfig latest() {
return new MoveConfig(this.enabled, this.costFunction);
return new MoveConfig(this.enabled, this.costConfig.latest());
}
}
Loading

0 comments on commit 43b481e

Please sign in to comment.