From 1128e0be496c36fb11f7dec363fdd82818dd9eaa Mon Sep 17 00:00:00 2001 From: WakelessSloth56 Date: Fri, 23 Feb 2024 22:17:57 +0800 Subject: [PATCH] feat(game.ench): enchantment properties predicate --- .../HEnchantmentPropertiesPredicate.java | 130 ++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 src/main/java/org/auioc/mcmod/arnicalib/game/enchantment/HEnchantmentPropertiesPredicate.java diff --git a/src/main/java/org/auioc/mcmod/arnicalib/game/enchantment/HEnchantmentPropertiesPredicate.java b/src/main/java/org/auioc/mcmod/arnicalib/game/enchantment/HEnchantmentPropertiesPredicate.java new file mode 100644 index 0000000..9bd8f12 --- /dev/null +++ b/src/main/java/org/auioc/mcmod/arnicalib/game/enchantment/HEnchantmentPropertiesPredicate.java @@ -0,0 +1,130 @@ +/* + * Copyright (C) 2024 AUIOC.ORG + * + * This file is part of ArnicaLib, a mod made for Minecraft. + * + * ArnicaLib is free software: you can redistribute it and/or modify it under + * the terms of the GNU 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 General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +package org.auioc.mcmod.arnicalib.game.enchantment; + +import com.mojang.serialization.Codec; +import com.mojang.serialization.codecs.RecordCodecBuilder; +import net.minecraft.util.ExtraCodecs; +import net.minecraft.world.item.enchantment.Enchantment; +import org.auioc.mcmod.arnicalib.game.codec.EnumCodec; + +import java.util.Optional; +import java.util.function.Predicate; + +public record HEnchantmentPropertiesPredicate( + Optional rarity, + Optional curse, + Optional treasureOnly, + Optional tradeable, + Optional discoverable, + Optional allowedOnBooks +) implements Predicate { + + @Override + public boolean test(Enchantment ench) { + if (rarity.isPresent() && !rarity.get().equals(ench.getRarity())) { + return false; + } + if (curse.isPresent() && !curse.get().equals(ench.isCurse())) { + return false; + } + if (treasureOnly.isPresent() && !treasureOnly.get().equals(ench.isTreasureOnly())) { + return false; + } + if (tradeable.isPresent() && !tradeable.get().equals(ench.isTradeable())) { + return false; + } + if (discoverable.isPresent() && !discoverable.get().equals(ench.isDiscoverable())) { + return false; + } + if (allowedOnBooks.isPresent() && !allowedOnBooks.get().equals(ench.isAllowedOnBooks())) { + return false; + } + return true; + } + + // ============================================================================================================== // + + public static final Codec CODEC = RecordCodecBuilder.create( + instance -> instance.group( + ExtraCodecs.strictOptionalField(EnumCodec.byNameLowerCase(Enchantment.Rarity.class), "rarity").forGetter(HEnchantmentPropertiesPredicate::rarity), + ExtraCodecs.strictOptionalField(Codec.BOOL, "curse").forGetter(HEnchantmentPropertiesPredicate::curse), + ExtraCodecs.strictOptionalField(Codec.BOOL, "treasure_only").forGetter(HEnchantmentPropertiesPredicate::treasureOnly), + ExtraCodecs.strictOptionalField(Codec.BOOL, "tradeable").forGetter(HEnchantmentPropertiesPredicate::tradeable), + ExtraCodecs.strictOptionalField(Codec.BOOL, "discoverable").forGetter(HEnchantmentPropertiesPredicate::discoverable), + ExtraCodecs.strictOptionalField(Codec.BOOL, "allowed_on_books").forGetter(HEnchantmentPropertiesPredicate::allowedOnBooks) + ) + .apply(instance, HEnchantmentPropertiesPredicate::new) + ); + + // ============================================================================================================== // + + public static HEnchantmentPropertiesPredicate.Builder builder() { + return new Builder(); + } + + public static class Builder { + + private Optional rarity = Optional.empty(); + private Optional curse = Optional.empty(); + private Optional treasureOnly = Optional.empty(); + private Optional tradeable = Optional.empty(); + private Optional discoverable = Optional.empty(); + private Optional allowedOnBooks = Optional.empty(); + + private Builder() { } + + public Builder rarity(Enchantment.Rarity rarity) { + this.rarity = Optional.of(rarity); + return this; + } + + public Builder curse(boolean curse) { + this.curse = Optional.of(curse); + return this; + } + + public Builder treasureOnly(boolean treasureOnly) { + this.treasureOnly = Optional.of(treasureOnly); + return this; + } + + public Builder tradeable(boolean tradeable) { + this.tradeable = Optional.of(tradeable); + return this; + } + + public Builder discoverable(boolean discoverable) { + this.discoverable = Optional.of(discoverable); + return this; + } + + public Builder allowedOnBooks(boolean allowedOnBooks) { + this.allowedOnBooks = Optional.of(allowedOnBooks); + return this; + } + + public HEnchantmentPropertiesPredicate build() { + return new HEnchantmentPropertiesPredicate(rarity, curse, treasureOnly, tradeable, discoverable, allowedOnBooks); + } + + } + +}