-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
200 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/main/java/de/mschae23/grindenchantments/GrindEnchantmentsClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/main/java/de/mschae23/grindenchantments/config/ClientSyncConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + | ||
'}'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/main/java/de/mschae23/grindenchantments/config/legacy/v1/CostConfigV1.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.