Skip to content

Commit 43b481e

Browse files
committed
Some fixes after testing
1 parent 4593ba5 commit 43b481e

15 files changed

+200
-61
lines changed

gradle.properties

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ org.gradle.jvmargs=-Xmx1G
33

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

1010
# Mod Properties
11-
mod_version = 4.0.0
11+
mod_version = 4.0.0-beta.1
1212
maven_group = de.mschae23.minecraft.mod
1313
archives_base_name = grind-enchantments
1414

1515
# Dependencies
16-
fabric_api_version=0.110.0+1.21.4
16+
fabric_api_version=0.110.2+1.21.4
1717
codec_config_api_version=3.0.0+1.21.3
1818
tax_free_levels_version=1.4.1-fabric-1.21.1
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright (C) 2024 mschae23
3+
*
4+
* This file is part of Grind enchantments.
5+
*
6+
* Grind enchantments is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Lesser General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
20+
package de.mschae23.grindenchantments;
21+
22+
import java.util.stream.Stream;
23+
import net.minecraft.registry.RegistryWrapper;
24+
import net.fabricmc.api.ClientModInitializer;
25+
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents;
26+
import net.fabricmc.fabric.api.client.networking.v1.ClientConfigurationConnectionEvents;
27+
import net.fabricmc.fabric.api.client.networking.v1.ClientConfigurationNetworking;
28+
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
29+
import de.mschae23.grindenchantments.config.ClientConfig;
30+
import de.mschae23.grindenchantments.config.sync.ServerConfigS2CPayload;
31+
import de.mschae23.grindenchantments.registry.GrindEnchantmentsRegistries;
32+
import org.apache.logging.log4j.Level;
33+
34+
public class GrindEnchantmentsClient implements ClientModInitializer {
35+
private static ClientConfig CLIENT_CONFIG = ClientConfig.DEFAULT;
36+
37+
@Override
38+
public void onInitializeClient() {
39+
ClientLifecycleEvents.CLIENT_STARTED.register(client -> {
40+
CLIENT_CONFIG = GrindEnchantmentsMod.initializeClientConfig();
41+
GrindEnchantmentsMod.LOCAL_SERVER_CONFIG = GrindEnchantmentsMod.initializeServerConfig(RegistryWrapper.WrapperLookup.of(
42+
Stream.of(GrindEnchantmentsRegistries.COST_FUNCTION)));
43+
44+
ClientConfigurationNetworking.registerGlobalReceiver(ServerConfigS2CPayload.ID, (payload, context) -> {
45+
//noinspection resource
46+
context.client().execute(() -> {
47+
GrindEnchantmentsMod.SERVER_CONFIG = payload.config();
48+
49+
if (CLIENT_CONFIG.sync().logReceivedConfig()) {
50+
GrindEnchantmentsMod.log(Level.INFO, "Received server config: " + GrindEnchantmentsMod.SERVER_CONFIG);
51+
}
52+
});
53+
});
54+
});
55+
56+
ClientPlayConnectionEvents.INIT.register((handler, client2) -> {
57+
if (GrindEnchantmentsMod.SERVER_CONFIG != null) {
58+
GrindEnchantmentsMod.SERVER_CONFIG.validateRegistryEntries(handler.getRegistryManager());
59+
}
60+
});
61+
62+
// Set server config to null when joining a world, so that it is known whether the server sent its config
63+
ClientConfigurationConnectionEvents.INIT.register((handler, client2) -> GrindEnchantmentsMod.SERVER_CONFIG = null);
64+
// Set server config to null when leaving the world too, for the same reason
65+
ClientConfigurationConnectionEvents.DISCONNECT.register((handler, client2) -> GrindEnchantmentsMod.SERVER_CONFIG = null);
66+
ClientPlayConnectionEvents.DISCONNECT.register((handler, client2) -> GrindEnchantmentsMod.SERVER_CONFIG = null);
67+
}
68+
69+
public static ClientConfig getClientConfig() {
70+
return CLIENT_CONFIG;
71+
}
72+
}

src/main/java/de/mschae23/grindenchantments/GrindEnchantmentsMod.java

Lines changed: 17 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@
3434
import net.minecraft.registry.RegistryWrapper;
3535
import net.minecraft.util.Identifier;
3636
import net.fabricmc.api.ModInitializer;
37-
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents;
38-
import net.fabricmc.fabric.api.client.networking.v1.ClientConfigurationConnectionEvents;
39-
import net.fabricmc.fabric.api.client.networking.v1.ClientConfigurationNetworking;
40-
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
4137
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
4238
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
4339
import net.fabricmc.fabric.api.networking.v1.ServerConfigurationConnectionEvents;
@@ -74,10 +70,9 @@ public class GrindEnchantmentsMod implements ModInitializer {
7470
public static final String MODID = "grindenchantments";
7571
public static final Logger LOGGER = LogManager.getLogger("Grind Enchantments");
7672

77-
private static ClientConfig CLIENT_CONFIG = ClientConfig.DEFAULT;
7873
@Nullable
79-
private static ServerConfig SERVER_CONFIG = null;
80-
private static ServerConfig LOCAL_SERVER_CONFIG = ServerConfig.DEFAULT;
74+
static ServerConfig SERVER_CONFIG = null;
75+
static ServerConfig LOCAL_SERVER_CONFIG = ServerConfig.DEFAULT;
8176

8277
@Override
8378
public void onInitialize() {
@@ -90,43 +85,20 @@ public void onInitialize() {
9085
ServerLifecycleEvents.SERVER_STARTING.register(server -> {
9186
SERVER_CONFIG = initializeServerConfig(server.getRegistryManager());
9287
SERVER_CONFIG.validateRegistryEntries(server.getRegistryManager());
88+
LOCAL_SERVER_CONFIG = SERVER_CONFIG;
9389
});
9490
ServerLifecycleEvents.SERVER_STOPPING.register(server -> SERVER_CONFIG = null);
9591

9692
// Multiplayer
9793
PayloadTypeRegistry.configurationS2C().register(ServerConfigS2CPayload.ID,
9894
ServerConfigS2CPayload.createPacketCodec(CostFunction.createPacketCodec()));
9995

100-
ClientLifecycleEvents.CLIENT_STARTED.register(client -> {
101-
CLIENT_CONFIG = GrindEnchantmentsMod.initializeClientConfig();
102-
LOCAL_SERVER_CONFIG = GrindEnchantmentsMod.initializeServerConfig(RegistryWrapper.WrapperLookup.of(
103-
Stream.of(GrindEnchantmentsRegistries.COST_FUNCTION)));
104-
105-
ClientConfigurationNetworking.registerGlobalReceiver(ServerConfigS2CPayload.ID, (payload, context) -> {
106-
//noinspection resource
107-
context.client().execute(() -> {
108-
// log(Level.DEBUG, payload.config());
109-
SERVER_CONFIG = payload.config();
110-
});
111-
});
112-
ClientPlayConnectionEvents.INIT.register((handler, client2) -> {
113-
if (SERVER_CONFIG != null) {
114-
SERVER_CONFIG.validateRegistryEntries(handler.getRegistryManager());
115-
}
116-
});
117-
});
11896
ServerConfigurationConnectionEvents.CONFIGURE.register((handler, server) -> {
11997
if (ServerConfigurationNetworking.canSend(handler, ServerConfigS2CPayload.ID)) {
12098
ServerConfigurationNetworking.send(handler, new ServerConfigS2CPayload(SERVER_CONFIG != null ? SERVER_CONFIG : LOCAL_SERVER_CONFIG));
12199
}
122100
});
123101

124-
// Set server config to null when joining a world, so that it is known whether the server sent its config
125-
ClientConfigurationConnectionEvents.INIT.register((handler, client) -> SERVER_CONFIG = null);
126-
// Set server config to null when leaving the world too, for the same reason
127-
ClientConfigurationConnectionEvents.DISCONNECT.register((handler, client) -> SERVER_CONFIG = null);
128-
ClientPlayConnectionEvents.DISCONNECT.register((handler, client) -> SERVER_CONFIG = null);
129-
130102
DisenchantOperation disenchant = new DisenchantOperation();
131103
MoveOperation move = new MoveOperation();
132104
ResetRepairCostOperation resetRepairCost = new ResetRepairCostOperation();
@@ -151,12 +123,12 @@ public void onInitialize() {
151123

152124
public static ServerConfig getServerConfig() {
153125
return SERVER_CONFIG == null ?
154-
CLIENT_CONFIG.useLocalIfUnsynced() ? LOCAL_SERVER_CONFIG : ServerConfig.DISABLED
126+
GrindEnchantmentsClient.getClientConfig().sync().useLocalIfUnsynced() ? LOCAL_SERVER_CONFIG : ServerConfig.DISABLED
155127
: SERVER_CONFIG;
156128
}
157129

158130
public static ClientConfig getClientConfig() {
159-
return CLIENT_CONFIG;
131+
return GrindEnchantmentsClient.getClientConfig();
160132
}
161133

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

193165
ConfigIo.encodeConfig(writer, codec, config.latest(), ops);
194166
} catch (IOException e) {
195-
log(Level.ERROR, "IO exception while trying to write updated config: " + e.getLocalizedMessage());
167+
log(Level.ERROR, "IO exception while trying to write updated " + kind + " config: " + e.getLocalizedMessage());
196168
} catch (ConfigException e) {
197169
log(Level.ERROR, e.getLocalizedMessage());
198170
}
@@ -203,14 +175,18 @@ private static <C extends ModConfig<C>> C initializeGenericConfig(Path configNam
203175
log(Level.ERROR, e.getLocalizedMessage());
204176
}
205177
} else {
206-
// Write default config if the file doesn't exist
207-
try (OutputStream output = Files.newOutputStream(filePath, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE);
208-
OutputStreamWriter writer = new OutputStreamWriter(new BufferedOutputStream(output))) {
209-
log(Level.INFO, "Writing default " + kind + " config.");
178+
try {
179+
Files.createDirectories(filePath.getParent());
180+
181+
// Write default config if the file doesn't exist
182+
try (OutputStream output = Files.newOutputStream(filePath, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE);
183+
OutputStreamWriter writer = new OutputStreamWriter(new BufferedOutputStream(output))) {
184+
log(Level.INFO, "Writing default " + kind + " config.");
210185

211-
ConfigIo.encodeConfig(writer, codec, latestDefault, ops);
186+
ConfigIo.encodeConfig(writer, codec, latestDefault, ops);
187+
}
212188
} catch (IOException e) {
213-
log(Level.ERROR, "IO exception while trying to write config: " + e.getLocalizedMessage());
189+
log(Level.ERROR, "IO exception while trying to write " + kind + " config: " + e.getLocalizedMessage());
214190
} catch (ConfigException e) {
215191
log(Level.ERROR, e.getLocalizedMessage());
216192
}
@@ -224,7 +200,7 @@ public static ClientConfig initializeClientConfig() {
224200
JsonOps.INSTANCE, "client");
225201
}
226202

227-
private static ServerConfig initializeServerConfig(RegistryWrapper.WrapperLookup wrapperLookup) {
203+
public static ServerConfig initializeServerConfig(RegistryWrapper.WrapperLookup wrapperLookup) {
228204
return initializeGenericConfig(Path.of("server.json"), ServerConfig.DEFAULT, ServerConfig.CODEC,
229205
RegistryOps.of(JsonOps.INSTANCE, wrapperLookup), "server");
230206
}

src/main/java/de/mschae23/grindenchantments/config/ClientConfig.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@
2525
import de.mschae23.config.api.ModConfig;
2626
import de.mschae23.grindenchantments.GrindEnchantmentsMod;
2727

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

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

@@ -58,7 +58,7 @@ public boolean shouldUpdate() {
5858
public String toString() {
5959
return "ClientConfig{" +
6060
"showLevelCost=" + this.showLevelCost +
61-
", useLocalIfUnsynced=" + this.useLocalIfUnsynced +
61+
", sync=" + this.sync +
6262
'}';
6363
}
6464
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (C) 2024 mschae23
3+
*
4+
* This file is part of Grind enchantments.
5+
*
6+
* Grind enchantments is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Lesser General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
20+
package de.mschae23.grindenchantments.config;
21+
22+
import com.mojang.serialization.Codec;
23+
import com.mojang.serialization.codecs.RecordCodecBuilder;
24+
25+
public record ClientSyncConfig(boolean useLocalIfUnsynced, boolean logReceivedConfig) {
26+
public static final Codec<ClientSyncConfig> CODEC = RecordCodecBuilder.create(instance -> instance.group(
27+
Codec.BOOL.fieldOf("use_local_server_config_if_unsynced").forGetter(ClientSyncConfig::useLocalIfUnsynced),
28+
Codec.BOOL.fieldOf("log_received_config").forGetter(ClientSyncConfig::logReceivedConfig)
29+
).apply(instance, instance.stable(ClientSyncConfig::new)));
30+
31+
public static final ClientSyncConfig DEFAULT = new ClientSyncConfig(true, false);
32+
33+
@Override
34+
public String toString() {
35+
return "ClientSyncConfig{" +
36+
"useLocalIfUnsynced=" + this.useLocalIfUnsynced +
37+
", logReceivedConfig=" + this.logReceivedConfig +
38+
'}';
39+
}
40+
}

src/main/java/de/mschae23/grindenchantments/config/DisenchantConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public String toString() {
5757
return "DisenchantConfig{" +
5858
"enabled=" + this.enabled +
5959
", consumeItem=" + this.consumeItem +
60-
", costFunction=" + this.costFunction +
60+
", costConfig=" + this.costFunction +
6161
'}';
6262
}
6363
}

src/main/java/de/mschae23/grindenchantments/config/MoveConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public static PacketCodec<PacketByteBuf, MoveConfig> createPacketCodec(PacketCod
5555
public String toString() {
5656
return "MoveConfig{" +
5757
"enabled=" + this.enabled +
58-
", costFunction=" + this.costFunction +
58+
", costConfig=" + this.costFunction +
5959
'}';
6060
}
6161
}

src/main/java/de/mschae23/grindenchantments/config/ResetRepairCostConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public String toString() {
9191
"enabled=" + this.enabled +
9292
", catalystItems=" + this.catalystItems +
9393
", requiresEnchantment=" + this.requiresEnchantment +
94-
", costFunction=" + this.costFunction +
94+
", costConfig=" + this.costFunction +
9595
'}';
9696
}
9797
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (C) 2024 mschae23
3+
*
4+
* This file is part of Grind enchantments.
5+
*
6+
* Grind enchantments is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Lesser General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
20+
package de.mschae23.grindenchantments.config.legacy.v1;
21+
22+
import com.mojang.serialization.Codec;
23+
import com.mojang.serialization.MapCodec;
24+
import com.mojang.serialization.codecs.RecordCodecBuilder;
25+
import de.mschae23.grindenchantments.cost.CostFunction;
26+
import de.mschae23.grindenchantments.cost.TransformCostFunction;
27+
28+
@Deprecated
29+
public record CostConfigV1(CostFunction function, double factor, double offset) {
30+
public static final MapCodec<CostConfigV1> CODEC = RecordCodecBuilder.mapCodec(instance -> instance.group(
31+
CostFunction.CODEC.fieldOf("count_mode").forGetter(CostConfigV1::function),
32+
Codec.DOUBLE.fieldOf("cost_factor").orElse(1.0).forGetter(CostConfigV1::factor),
33+
Codec.DOUBLE.fieldOf("cost_offset").orElse(0.0).forGetter(CostConfigV1::offset)
34+
).apply(instance, instance.stable(CostConfigV1::new)));
35+
36+
public CostFunction latest() {
37+
return new TransformCostFunction(this.function, this.factor, this.offset);
38+
}
39+
}

src/main/java/de/mschae23/grindenchantments/config/legacy/v1/DisenchantConfigV1.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,16 @@
2222
import com.mojang.serialization.Codec;
2323
import com.mojang.serialization.codecs.RecordCodecBuilder;
2424
import de.mschae23.grindenchantments.config.DisenchantConfig;
25-
import de.mschae23.grindenchantments.cost.CostFunction;
2625

2726
@Deprecated
28-
public record DisenchantConfigV1(boolean enabled, boolean consumeItem, CostFunction costFunction) {
27+
public record DisenchantConfigV1(boolean enabled, boolean consumeItem, CostConfigV1 costConfig) {
2928
public static final Codec<DisenchantConfigV1> CODEC = RecordCodecBuilder.create(instance -> instance.group(
3029
Codec.BOOL.fieldOf("enabled").forGetter(DisenchantConfigV1::enabled),
3130
Codec.BOOL.fieldOf("consume_enchanted_item").forGetter(DisenchantConfigV1::consumeItem),
32-
CostFunction.CODEC.fieldOf("cost_config").forGetter(DisenchantConfigV1::costFunction)
31+
CostConfigV1.CODEC.fieldOf("cost_config").forGetter(DisenchantConfigV1::costConfig)
3332
).apply(instance, instance.stable(DisenchantConfigV1::new)));
3433

3534
public DisenchantConfig latest() {
36-
return new DisenchantConfig(this.enabled, this.consumeItem, this.costFunction);
35+
return new DisenchantConfig(this.enabled, this.consumeItem, this.costConfig.latest());
3736
}
3837
}

0 commit comments

Comments
 (0)