Skip to content

Commit

Permalink
Use split configs
Browse files Browse the repository at this point in the history
  • Loading branch information
mschae23 committed Nov 30, 2024
1 parent cb28bf4 commit c0826f8
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,13 @@
import net.minecraft.registry.RegistryOps;
import net.minecraft.registry.RegistryWrapper;
import net.minecraft.util.Identifier;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
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.ClientLoginConnectionEvents;
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;
import net.fabricmc.fabric.api.networking.v1.ServerConfigurationNetworking;
import net.fabricmc.loader.api.FabricLoader;
import com.google.gson.JsonElement;
import com.mojang.serialization.Codec;
Expand All @@ -56,7 +54,7 @@
import de.mschae23.grindenchantments.impl.DisenchantOperation;
import de.mschae23.grindenchantments.impl.MoveOperation;
import de.mschae23.grindenchantments.impl.ResetRepairCostOperation;
import de.mschae23.grindenchantments.network.s2c.ServerConfigS2CPayload;
import de.mschae23.grindenchantments.config.sync.ServerConfigS2CPayload;
import de.mschae23.grindenchantments.registry.GrindEnchantmentsRegistries;
import io.github.fourmisain.taxfreelevels.TaxFreeLevels;
import org.apache.logging.log4j.Level;
Expand All @@ -68,6 +66,7 @@ public class GrindEnchantmentsMod implements ModInitializer {
public static final String MODID = "grindenchantments";
public static final Logger LOGGER = LogManager.getLogger("Grind Enchantments");

@Deprecated
private static GrindEnchantmentsConfigV3 LEGACY_CONFIG = GrindEnchantmentsConfigV3.DEFAULT;
@Nullable
private static ServerConfig SERVER_CONFIG = null;
Expand All @@ -76,9 +75,11 @@ public class GrindEnchantmentsMod implements ModInitializer {

@Override
public void onInitialize() {
// Singleplayer
ServerLifecycleEvents.SERVER_STARTING.register(server -> readServerConfig(server.getRegistryManager())
.ifPresent(config -> SERVER_CONFIG = config));
// Singleplayer or on server
ServerLifecycleEvents.SERVER_STARTING.register(server -> {
SERVER_CONFIG = readServerConfig(server.getRegistryManager()).orElse(ServerConfig.DEFAULT);
SERVER_CONFIG.validateRegistryEntries(server.getRegistryManager());
});
ServerLifecycleEvents.SERVER_STOPPING.register(server -> SERVER_CONFIG = null);

// Multiplayer
Expand All @@ -90,11 +91,19 @@ public void onInitialize() {
ClientConfigurationNetworking.registerGlobalReceiver(ServerConfigS2CPayload.ID, (payload, context) -> {
//noinspection resource
context.client().execute(() -> {
log(Level.INFO, "Received server config");
// TODO
});
});
});
ServerConfigurationConnectionEvents.CONFIGURE.register((handler, server) -> {
if (ServerConfigurationNetworking.canSend(handler, ServerConfigS2CPayload.ID)) {
log(Level.INFO, "Sent server config");
ServerConfigurationNetworking.send(handler, new ServerConfigS2CPayload());
}
});

//noinspection deprecation
LEGACY_CONFIG = readLegacyConfig().orElse(GrindEnchantmentsConfigV3.DEFAULT);

GrindEnchantmentsRegistries.init();
Expand Down
113 changes: 79 additions & 34 deletions src/main/java/de/mschae23/grindenchantments/config/FilterConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,24 @@

package de.mschae23.grindenchantments.config;

import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
import net.minecraft.component.type.ItemEnchantmentsComponent;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.item.Item;
import net.minecraft.registry.RegistryCodecs;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.registry.RegistryWrapper;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.registry.entry.RegistryEntryList;
import net.minecraft.registry.tag.EnchantmentTags;
import net.minecraft.util.Identifier;
import net.minecraft.util.dynamic.Codecs;
import com.mojang.datafixers.util.Pair;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import de.mschae23.grindenchantments.GrindEnchantmentsMod;
import org.apache.logging.log4j.Level;

public record FilterConfig(boolean enabled, ItemConfig item, EnchantmentConfig enchantment, FilterAction curses) {
public static final Codec<FilterConfig> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Expand All @@ -40,32 +48,41 @@ public record FilterConfig(boolean enabled, ItemConfig item, EnchantmentConfig e

public static final FilterConfig DEFAULT = new FilterConfig(true, ItemConfig.DEFAULT, EnchantmentConfig.DEFAULT, FilterAction.IGNORE);

public ItemEnchantmentsComponent filter(ItemEnchantmentsComponent enchantments) {
if (!this.enabled) {
return enchantments;
}

ItemEnchantmentsComponent.Builder builder = new ItemEnchantmentsComponent.Builder(enchantments);

private boolean shouldDeny(ItemEnchantmentsComponent.Builder builder) {
if (this.curses == FilterAction.DENY) {
for (RegistryEntry<Enchantment> entry : builder.getEnchantments()) {
if (entry.isIn(EnchantmentTags.CURSE)) {
return ItemEnchantmentsComponent.DEFAULT;
return true;
}
}
}

if (this.enchantment.action == FilterAction.DENY) {
for (RegistryEntry<Enchantment> entry : builder.getEnchantments()) {
if (this.enchantment.enchantments.contains(entry)) {
return ItemEnchantmentsComponent.DEFAULT;
if (entry.getKey().map(key -> this.enchantment.enchantments.contains(key.getValue())).orElse(false)) {
return true;
}
}
}

return false;
}

public ItemEnchantmentsComponent filter(ItemEnchantmentsComponent enchantments) {
if (!this.enabled) {
return enchantments;
}

ItemEnchantmentsComponent.Builder builder = new ItemEnchantmentsComponent.Builder(enchantments);

if (this.shouldDeny(builder)) {
return ItemEnchantmentsComponent.DEFAULT;
}

builder.remove(enchantment ->
((this.curses == FilterAction.IGNORE) && enchantment.isIn(EnchantmentTags.CURSE))
|| ((this.enchantment.action == FilterAction.IGNORE) == this.enchantment.enchantments.contains(enchantment)));
|| ((this.enchantment.action == FilterAction.IGNORE) == enchantment.getKey().map(key ->
this.enchantment.enchantments.contains(key.getValue())).orElse(false)));

return builder.build();
}
Expand All @@ -77,44 +94,72 @@ public ItemEnchantmentsComponent filterReversed(ItemEnchantmentsComponent enchan

ItemEnchantmentsComponent.Builder builder = new ItemEnchantmentsComponent.Builder(enchantments);

if (this.curses == FilterAction.DENY) {
for (RegistryEntry<Enchantment> entry : builder.getEnchantments()) {
if (entry.isIn(EnchantmentTags.CURSE)) {
return ItemEnchantmentsComponent.DEFAULT;
}
}
}

if (this.enchantment.action == FilterAction.DENY) {
for (RegistryEntry<Enchantment> entry : builder.getEnchantments()) {
if (this.enchantment.enchantments.contains(entry)) {
return ItemEnchantmentsComponent.DEFAULT;
}
}
if (this.shouldDeny(builder)) {
return ItemEnchantmentsComponent.DEFAULT;
}

builder.remove(enchantment ->
((this.curses == FilterAction.ALLOW) || !enchantment.isIn(EnchantmentTags.CURSE))
&& ((this.enchantment.action == FilterAction.ALLOW) == this.enchantment.enchantments.contains(enchantment)));
&& ((this.enchantment.action == FilterAction.ALLOW) == enchantment.getKey().map(key ->
this.enchantment.enchantments.contains(key.getValue())).orElse(false)));

return builder.build();
}

public record ItemConfig(RegistryEntryList<Item> items, FilterAction action) {
public void validateRegistryEntries(RegistryWrapper.WrapperLookup wrapperLookup) {
this.item.validateRegistryEntries(wrapperLookup);
this.enchantment.validateRegistryEntries(wrapperLookup);
}

public record ItemConfig(List<Identifier> items, FilterAction action) {
public static final Codec<ItemConfig> CODEC = RecordCodecBuilder.create(instance -> instance.group(
RegistryCodecs.entryList(RegistryKeys.ITEM).fieldOf("enchantments").forGetter(ItemConfig::items),
Codecs.listOrSingle(Identifier.CODEC).fieldOf("enchantments").forGetter(ItemConfig::items),
FilterAction.NON_IGNORE_CODEC.fieldOf("action").forGetter(ItemConfig::action)
).apply(instance, instance.stable(ItemConfig::new)));

public static final ItemConfig DEFAULT = new ItemConfig(RegistryEntryList.empty(), FilterAction.DENY);
public static final ItemConfig DEFAULT = new ItemConfig(List.of(), FilterAction.DENY);

public void validateRegistryEntries(RegistryWrapper.WrapperLookup wrapperLookup) {
Optional<? extends RegistryWrapper.Impl<Item>> registryWrapperOpt = wrapperLookup.getOptional(RegistryKeys.ITEM);

if (registryWrapperOpt.isEmpty()) {
GrindEnchantmentsMod.log(Level.WARN, "Item registry is not present");
return;
}

RegistryWrapper.Impl<Item> registryWrapper = registryWrapperOpt.get();

this.items.stream()
.map(item -> Pair.of(item, registryWrapper.getOptional(RegistryKey.of(RegistryKeys.ITEM, item))))
.flatMap(result -> result.getSecond().isEmpty() ? Stream.of(result.getFirst()) : Stream.empty())
.map(Identifier::toString)
.forEach(item -> GrindEnchantmentsMod.log(Level.WARN, "Filter config contains unknown item: " + item));
}
}

public record EnchantmentConfig(RegistryEntryList<Enchantment> enchantments, FilterAction action) {
public record EnchantmentConfig(List<Identifier> enchantments, FilterAction action) {
public static final Codec<EnchantmentConfig> CODEC = RecordCodecBuilder.create(instance -> instance.group(
RegistryCodecs.entryList(RegistryKeys.ENCHANTMENT).fieldOf("enchantments").forGetter(EnchantmentConfig::enchantments),
Codecs.listOrSingle(Identifier.CODEC).fieldOf("enchantments").forGetter(EnchantmentConfig::enchantments),
FilterAction.CODEC.fieldOf("action").forGetter(EnchantmentConfig::action)
).apply(instance, instance.stable(EnchantmentConfig::new)));

public static final EnchantmentConfig DEFAULT = new EnchantmentConfig(RegistryEntryList.empty(), FilterAction.IGNORE);
public static final EnchantmentConfig DEFAULT = new EnchantmentConfig(List.of(), FilterAction.IGNORE);

public void validateRegistryEntries(RegistryWrapper.WrapperLookup wrapperLookup) {
Optional<? extends RegistryWrapper.Impl<Enchantment>> registryWrapperOpt = wrapperLookup.getOptional(RegistryKeys.ENCHANTMENT);

if (registryWrapperOpt.isEmpty()) {
GrindEnchantmentsMod.log(Level.WARN, "Enchantment registry is not present");
return;
}

RegistryWrapper.Impl<Enchantment> registryWrapper = registryWrapperOpt.get();

this.enchantments.stream()
.map(enchantment -> Pair.of(enchantment, registryWrapper.getOptional(RegistryKey.of(RegistryKeys.ENCHANTMENT, enchantment))))
.flatMap(result -> result.getSecond().isEmpty() ? Stream.of(result.getFirst()) : Stream.empty())
.map(Identifier::toString)
.forEach(item -> GrindEnchantmentsMod.log(Level.WARN, "Filter config contains unknown enchantment: " + item));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,52 @@

package de.mschae23.grindenchantments.config;

import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
import net.minecraft.item.Item;
import net.minecraft.item.Items;
import net.minecraft.registry.RegistryCodecs;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.registry.entry.RegistryEntryList;
import net.minecraft.registry.RegistryWrapper;
import net.minecraft.util.Identifier;
import net.minecraft.util.dynamic.Codecs;
import com.mojang.datafixers.util.Pair;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import de.mschae23.grindenchantments.GrindEnchantmentsMod;
import de.mschae23.grindenchantments.cost.AverageCountCostFunction;
import de.mschae23.grindenchantments.cost.CostFunction;
import de.mschae23.grindenchantments.cost.CountLevelsCostFunction;
import de.mschae23.grindenchantments.cost.TransformCostFunction;
import org.apache.logging.log4j.Level;

public record ResetRepairCostConfig(boolean enabled, RegistryEntryList<Item> catalystItems, boolean requiresEnchantment, CostFunction costFunction) {
public record ResetRepairCostConfig(boolean enabled, List<Identifier> catalystItems, boolean requiresEnchantment, CostFunction costFunction) {
public static final Codec<ResetRepairCostConfig> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.BOOL.fieldOf("enabled").forGetter(ResetRepairCostConfig::enabled),
RegistryCodecs.entryList(RegistryKeys.ITEM).fieldOf("catalyst_items").forGetter(ResetRepairCostConfig::catalystItems),
Codecs.listOrSingle(Identifier.CODEC).fieldOf("catalyst_items").forGetter(ResetRepairCostConfig::catalystItems),
Codec.BOOL.fieldOf("requires_enchantment").forGetter(ResetRepairCostConfig::requiresEnchantment),
CostFunction.TYPE_CODEC.fieldOf("cost_function").forGetter(ResetRepairCostConfig::costFunction)
).apply(instance, instance.stable(ResetRepairCostConfig::new)));

@SuppressWarnings("deprecation")
public static final ResetRepairCostConfig DEFAULT = new ResetRepairCostConfig(false,
RegistryEntryList.of(Items.DIAMOND.getRegistryEntry()), true,
List.of(Identifier.ofVanilla("diamond")), true,
// Intentionally no filter function
new TransformCostFunction(new AverageCountCostFunction(new CountLevelsCostFunction(1.0, 4.0)), 1.5, 4.0));

public void validateRegistryEntries(RegistryWrapper.WrapperLookup wrapperLookup) {
Optional<? extends RegistryWrapper.Impl<Item>> registryWrapperOpt = wrapperLookup.getOptional(RegistryKeys.ITEM);

if (registryWrapperOpt.isEmpty()) {
GrindEnchantmentsMod.log(Level.WARN, "Item registry is not present");
return;
}

RegistryWrapper.Impl<Item> registryWrapper = registryWrapperOpt.get();

this.catalystItems.stream()
.map(item -> Pair.of(item, registryWrapper.getOptional(RegistryKey.of(RegistryKeys.ITEM, item))))
.flatMap(result -> result.getSecond().isEmpty() ? Stream.of(result.getFirst()) : Stream.empty())
.map(Identifier::toString)
.forEach(item -> GrindEnchantmentsMod.log(Level.WARN, "Reset repair cost config contains unknown catalyst item: " + item));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package de.mschae23.grindenchantments.config;

import net.minecraft.registry.RegistryWrapper;
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import de.mschae23.config.api.ModConfig;
Expand Down Expand Up @@ -55,4 +56,9 @@ public ServerConfig latest() {
public boolean shouldUpdate() {
return true;
}

public void validateRegistryEntries(RegistryWrapper.WrapperLookup wrapperLookup) {
this.filter.validateRegistryEntries(wrapperLookup);
this.resetRepairCost.validateRegistryEntries(wrapperLookup);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package de.mschae23.grindenchantments.network.s2c;
package de.mschae23.grindenchantments.config.sync;

import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.codec.PacketCodec;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import de.mschae23.grindenchantments.GrindEnchantmentsMod;
import de.mschae23.grindenchantments.config.FilterAction;
import de.mschae23.grindenchantments.config.FilterConfig;
import de.mschae23.grindenchantments.config.legacy.v3.GrindEnchantmentsConfigV3;
import de.mschae23.grindenchantments.config.ServerConfig;
import de.mschae23.grindenchantments.event.ApplyLevelCostEvent;
import de.mschae23.grindenchantments.event.GrindstoneEvents;
import org.jetbrains.annotations.NotNull;
Expand All @@ -52,14 +52,14 @@ public boolean canInsert(ItemStack stack, ItemStack other, int slotId) {

ItemStack enchantedItemStack = input1.hasEnchantments() ? input1 : input2;

GrindEnchantmentsConfigV3 config = GrindEnchantmentsMod.getConfig();
ServerConfig config = GrindEnchantmentsMod.getServerConfig();
return transferEnchantmentsToBook(enchantedItemStack, config.filter());
}

@Override
public boolean canTakeResult(ItemStack input1, ItemStack input2, PlayerEntity player, RegistryWrapper.WrapperLookup wrapperLookup) {
if (isDisenchantOperation(input1, input2)) {
GrindEnchantmentsConfigV3 config = GrindEnchantmentsMod.getConfig();
ServerConfig config = GrindEnchantmentsMod.getServerConfig();

boolean stack1Book = input1.getItem() == Items.BOOK;
ItemStack enchantedItemStack = stack1Book ? input2 : input1;
Expand All @@ -77,7 +77,7 @@ public boolean onTakeResult(ItemStack input1, ItemStack input2, ItemStack result
return false;
}

GrindEnchantmentsConfigV3 config = GrindEnchantmentsMod.getConfig();
ServerConfig config = GrindEnchantmentsMod.getServerConfig();

boolean stack1Book = input1.getItem() == Items.BOOK;
ItemStack enchantedItemStack = stack1Book ? input2 : input1;
Expand Down Expand Up @@ -105,7 +105,7 @@ public boolean onTakeResult(ItemStack input1, ItemStack input2, ItemStack result
@Override
public int getLevelCost(ItemStack input1, ItemStack input2, PlayerEntity player, RegistryWrapper.WrapperLookup wrapperLookup) {
if (isDisenchantOperation(input1, input2)) {
GrindEnchantmentsConfigV3 config = GrindEnchantmentsMod.getConfig();
ServerConfig config = GrindEnchantmentsMod.getServerConfig();
boolean stack1Book = input1.getItem() == Items.BOOK;
ItemStack enchantedItemStack = stack1Book ? input2 : input1;

Expand All @@ -121,7 +121,7 @@ private static int debugLevelCost(@SuppressWarnings("unused") String location, i
}

public static boolean isDisenchantOperation(ItemStack input1, ItemStack input2) {
if (!GrindEnchantmentsMod.getConfig().disenchant().enabled())
if (!GrindEnchantmentsMod.getServerConfig().disenchant().enabled())
return false;

return input1.hasEnchantments() && input2.getItem() == Items.BOOK
Expand All @@ -135,7 +135,7 @@ public static boolean canTakeResult(@SuppressWarnings("unused") ItemStack input1

public static ItemStack transferEnchantmentsToBook(ItemStack source, FilterConfig filter) {
if (filter.enabled() && filter.item().action() != FilterAction.IGNORE
&& (filter.item().action() == FilterAction.DENY) == filter.item().items().contains(source.getRegistryEntry())) {
&& (filter.item().action() == FilterAction.DENY) == source.getRegistryEntry().getKey().map(key -> filter.item().items().contains(key.getValue())).orElse(false)) {
return ItemStack.EMPTY;
}

Expand Down
Loading

0 comments on commit c0826f8

Please sign in to comment.