Skip to content

Commit

Permalink
feat(game.ench): enchantment predicate
Browse files Browse the repository at this point in the history
  • Loading branch information
WakelessSloth56 committed Feb 23, 2024
1 parent 1128e0b commit 4ea8214
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ public class EnchantmentPredicates {

public static final Predicate<Enchantment> IS_VANILLA = (e) -> VanillaPredicates.ID.test(RegistryUtils.id(e));

public static final Predicate<Enchantment> IS_TREASURE_ONLY = (e) -> e.isTreasureOnly();
public static final Predicate<Enchantment> IS_CURSE = (e) -> e.isCurse();
public static final Predicate<Enchantment> IS_TRADEABLE = (e) -> e.isTradeable();
public static final Predicate<Enchantment> IS_DISCOVERABLE = (e) -> e.isDiscoverable();
public static final Predicate<Enchantment> IS_TREASURE_ONLY = Enchantment::isTreasureOnly;
public static final Predicate<Enchantment> IS_CURSE = Enchantment::isCurse;
public static final Predicate<Enchantment> IS_TRADEABLE = Enchantment::isTradeable;
public static final Predicate<Enchantment> IS_DISCOVERABLE = Enchantment::isDiscoverable;

public static final Predicate<Enchantment> IS_COMMON = (e) -> e.getRarity() == Rarity.COMMON;
public static final Predicate<Enchantment> IS_UNCOMMON = (e) -> e.getRarity() == Rarity.UNCOMMON;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* 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 <https://www.gnu.org/licenses/>.
*/

package org.auioc.mcmod.arnicalib.game.enchantment;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.advancements.critereon.MinMaxBounds;
import net.minecraft.core.HolderSet;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.util.ExtraCodecs;
import net.minecraft.world.item.enchantment.Enchantment;

import java.util.Map;
import java.util.Optional;

public record HEnchantmentPredicate(
Optional<HolderSet<Enchantment>> enchantments,
MinMaxBounds.Ints level,
Optional<HEnchantmentPropertiesPredicate> properties
) {

@SuppressWarnings("deprecation")
public boolean test(Enchantment ench, int lvl) {
if (level != MinMaxBounds.Ints.ANY && !level.matches(lvl)) {
return false;
}
if (enchantments.isPresent() && enchantments.get().contains(ench.builtInRegistryHolder())) {
return true;
}
if (properties.isPresent() && !properties.get().test(ench)) {
return false;
}
return true;
}

public boolean test(Map.Entry<Enchantment, Integer> enchEntry) {
return test(enchEntry.getKey(), enchEntry.getValue());
}

// ============================================================================================================== //

private static final Codec<HolderSet<Enchantment>> ENCHANTMENT_CODEC = BuiltInRegistries.ENCHANTMENT
.holderByNameCodec()
.listOf()
.xmap(HolderSet::direct, holderSet -> holderSet.stream().toList());

public static final Codec<HEnchantmentPredicate> CODEC = RecordCodecBuilder.create(
instance -> instance.group(
ExtraCodecs.strictOptionalField(ENCHANTMENT_CODEC, "enchantments").forGetter(HEnchantmentPredicate::enchantments),
ExtraCodecs.strictOptionalField(MinMaxBounds.Ints.CODEC, "level", MinMaxBounds.Ints.ANY).forGetter(HEnchantmentPredicate::level),
ExtraCodecs.strictOptionalField(HEnchantmentPropertiesPredicate.CODEC, "properties").forGetter(HEnchantmentPredicate::properties)
)
.apply(instance, HEnchantmentPredicate::new)
);

// ============================================================================================================== //

public static HEnchantmentPredicate.Builder builder() {
return new Builder();
}

public static class Builder {

private Optional<HolderSet<Enchantment>> enchantments = Optional.empty();
private MinMaxBounds.Ints level = MinMaxBounds.Ints.ANY;
private Optional<HEnchantmentPropertiesPredicate> properties = Optional.empty();

private Builder() { }

@SuppressWarnings("deprecation")
public Builder enchantments(Enchantment... enchantments) {
this.enchantments = Optional.of(HolderSet.direct(Enchantment::builtInRegistryHolder, enchantments));
return this;
}

public Builder level(MinMaxBounds.Ints level) {
this.level = level;
return this;
}

public Builder properties(HEnchantmentPropertiesPredicate properties) {
this.properties = Optional.of(properties);
return this;
}

public HEnchantmentPredicate build() {
return new HEnchantmentPredicate(enchantments, level, properties);
}

}

}

0 comments on commit 4ea8214

Please sign in to comment.