Skip to content

Commit

Permalink
Backport min_level and max_level options
Browse files Browse the repository at this point in the history
  • Loading branch information
DrexHD committed May 31, 2024
1 parent 5db626b commit 742ceb0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Fixed
- Incomplete EmeraldsForVillagerTypeItem causing generation errors
- Backport: `villagerconfig:enchant_randomly` has `min_level` and `max_level` options

## [4.1.2] - 2023-11-23
### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,16 @@ public class EnchantRandomlyLootFunction extends LootItemConditionalFunction {

private final List<Enchantment> include;
private final List<Enchantment> exclude;
private final int minLevel;
private final int maxLevel;
private final boolean tradeEnchantments;

EnchantRandomlyLootFunction(LootItemCondition[] conditions, Collection<Enchantment> include, Collection<Enchantment> exclude, boolean tradeEnchantments) {
EnchantRandomlyLootFunction(LootItemCondition[] conditions, Collection<Enchantment> include, Collection<Enchantment> exclude, int minLevel, int maxLevel, boolean tradeEnchantments) {
super(conditions);
this.include = ImmutableList.copyOf(include);
this.exclude = ImmutableList.copyOf(exclude);
this.minLevel = minLevel;
this.maxLevel = maxLevel;
this.tradeEnchantments = tradeEnchantments;
}

Expand Down Expand Up @@ -70,8 +74,9 @@ private List<Enchantment> getEnchantments(ItemStack stack) {
return stream.filter(enchantment -> isBook || enchantment.canEnchant(stack)).toList();
}

private static ItemStack addEnchantmentToStack(ItemStack stack, Enchantment enchantment, RandomSource random, LootContext context) {
private ItemStack addEnchantmentToStack(ItemStack stack, Enchantment enchantment, RandomSource random, LootContext context) {
int level = Mth.nextInt(random, enchantment.getMinLevel(), enchantment.getMaxLevel());
level = Mth.clamp(level, this.minLevel, this.maxLevel);
if (stack.is(Items.BOOK)) {
stack = new ItemStack(Items.ENCHANTED_BOOK);
EnchantedBookItem.addEnchantment(stack, new EnchantmentInstance(enchantment, level));
Expand All @@ -92,7 +97,7 @@ private static ItemStack addEnchantmentToStack(ItemStack stack, Enchantment ench
}

public static class Builder
extends LootItemConditionalFunction.Builder<EnchantRandomlyLootFunction.Builder> {
extends LootItemConditionalFunction.Builder<EnchantRandomlyLootFunction.Builder> {

private final Set<Enchantment> include = Sets.newHashSet();
private final Set<Enchantment> exclude = Sets.newHashSet();
Expand All @@ -115,7 +120,7 @@ public Builder tradeEnchantments() {

@Override
public @NotNull LootItemFunction build() {
return new EnchantRandomlyLootFunction(this.getConditions(), include, exclude, tradeEnchantments);
return new EnchantRandomlyLootFunction(this.getConditions(), include, exclude, 0, Integer.MAX_VALUE, tradeEnchantments);
}

@Override
Expand All @@ -125,14 +130,21 @@ public Builder tradeEnchantments() {
}

public static class Serializer
extends LootItemConditionalFunction.Serializer<EnchantRandomlyLootFunction> {
extends LootItemConditionalFunction.Serializer<EnchantRandomlyLootFunction> {

@Override
public void serialize(@NotNull JsonObject jsonObject, @NotNull EnchantRandomlyLootFunction enchantRandomlyLootFunction, @NotNull JsonSerializationContext jsonSerializationContext) {
super.serialize(jsonObject, enchantRandomlyLootFunction, jsonSerializationContext);
addEnchantments(jsonObject, enchantRandomlyLootFunction.include, "include");
addEnchantments(jsonObject, enchantRandomlyLootFunction.exclude, "exclude");
jsonObject.addProperty("trade_enchantments", enchantRandomlyLootFunction.tradeEnchantments); }
if (enchantRandomlyLootFunction.minLevel != 0) {
jsonObject.addProperty("min_level", enchantRandomlyLootFunction.minLevel);
}
if (enchantRandomlyLootFunction.minLevel != Integer.MAX_VALUE) {
jsonObject.addProperty("max_level", enchantRandomlyLootFunction.maxLevel);
}
jsonObject.addProperty("trade_enchantments", enchantRandomlyLootFunction.tradeEnchantments);
}

private static void addEnchantments(JsonObject jsonObject, List<Enchantment> enchantments, String key) {
if (!enchantments.isEmpty()) {
Expand All @@ -152,8 +164,10 @@ private static void addEnchantments(JsonObject jsonObject, List<Enchantment> enc
public @NotNull EnchantRandomlyLootFunction deserialize(@NotNull JsonObject jsonObject, @NotNull JsonDeserializationContext context, LootItemCondition @NotNull [] lootConditions) {
List<Enchantment> include = getEnchantments(jsonObject, "include");
List<Enchantment> exclude = getEnchantments(jsonObject, "exclude");
int minLevel = GsonHelper.getAsInt(jsonObject, "min_level", 0);
int maxLevel = GsonHelper.getAsInt(jsonObject, "max_level", Integer.MAX_VALUE);
boolean tradeEnchantments = GsonHelper.getAsBoolean(jsonObject, "trade_enchantments", false);
return new EnchantRandomlyLootFunction(lootConditions, include, exclude, tradeEnchantments);
return new EnchantRandomlyLootFunction(lootConditions, include, exclude, minLevel, maxLevel, tradeEnchantments);
}

private static List<Enchantment> getEnchantments(JsonObject jsonObject, String key) {
Expand Down

0 comments on commit 742ceb0

Please sign in to comment.