Skip to content

Commit 1651eed

Browse files
committed
Properly constrain allowed enchantments in /cenchant and /cfish
1 parent a889667 commit 1651eed

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

src/main/java/net/earthcomputer/clientcommands/command/CEnchantCommand.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
import net.earthcomputer.clientcommands.features.PlayerRandCracker;
99
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
1010
import net.minecraft.command.CommandException;
11+
import net.minecraft.enchantment.Enchantment;
1112
import net.minecraft.enchantment.EnchantmentLevelEntry;
13+
import net.minecraft.item.Item;
14+
import net.minecraft.item.Items;
1215
import net.minecraft.text.MutableText;
1316
import net.minecraft.text.Text;
1417
import net.minecraft.util.Formatting;
@@ -26,10 +29,14 @@ public static void register(CommandDispatcher<FabricClientCommandSource> dispatc
2629
dispatcher.register(literal("cenchant")
2730
.then(literal("--simulate")
2831
.redirect(cenchant, ctx -> withFlags(ctx.getSource(), FLAG_SIMULATE, true)))
29-
.then(argument("itemAndEnchantmentsPredicate", itemAndEnchantmentsPredicate().withEnchantmentPredicate(ench -> !ench.isTreasure()))
32+
.then(argument("itemAndEnchantmentsPredicate", itemAndEnchantmentsPredicate().withEnchantmentPredicate(CEnchantCommand::enchantmentPredicate).constrainMaxLevel())
3033
.executes(ctx -> cenchant(ctx.getSource(), getItemAndEnchantmentsPredicate(ctx, "itemAndEnchantmentsPredicate")))));
3134
}
3235

36+
private static boolean enchantmentPredicate(Item item, Enchantment ench) {
37+
return !ench.isTreasure() && ench.isAvailableForRandomSelection() && (item == Items.BOOK || ench.target.isAcceptableItem(item));
38+
}
39+
3340
private static int cenchant(FabricClientCommandSource source, ItemAndEnchantmentsPredicate itemAndEnchantmentsPredicate) throws CommandException {
3441
if (!Configs.getEnchantingPrediction()) {
3542
Text text = Text.translatable("commands.cenchant.needEnchantingPrediction")

src/main/java/net/earthcomputer/clientcommands/command/FishCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public static void register(CommandDispatcher<FabricClientCommandSource> dispatc
4747
.then(argument("goal", clientItemPredicate(registryAccess))
4848
.executes(ctx -> addGoal(ctx.getSource(), getClientItemPredicate(ctx, "goal")))))
4949
.then(literal("add-enchanted-goal")
50-
.then(argument("goal", withString(itemAndEnchantmentsPredicate().withItemPredicate(ENCHANTABLE_ITEMS::contains)))
50+
.then(argument("goal", withString(itemAndEnchantmentsPredicate().withItemPredicate(ENCHANTABLE_ITEMS::contains).withEnchantmentPredicate((item, ench) -> ench.isAvailableForRandomSelection()).constrainMaxLevel()))
5151
.executes(ctx -> addEnchantedGoal(ctx.getSource(), getWithString(ctx, "goal", ItemAndEnchantmentsPredicate.class)))))
5252
.then(literal("remove-goal")
5353
.then(argument("index", integer(1))

src/main/java/net/earthcomputer/clientcommands/command/arguments/ItemAndEnchantmentsPredicateArgumentType.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import java.util.*;
2727
import java.util.concurrent.CompletableFuture;
28+
import java.util.function.BiPredicate;
2829
import java.util.function.Consumer;
2930
import java.util.function.Predicate;
3031

@@ -37,7 +38,8 @@ public class ItemAndEnchantmentsPredicateArgumentType implements ArgumentType<It
3738
private static final DynamicCommandExceptionType ID_INVALID_EXCEPTION = new DynamicCommandExceptionType(id -> Text.translatable("argument.item.id.invalid", id));
3839

3940
private Predicate<Item> itemPredicate = item -> true;
40-
private Predicate<Enchantment> enchantmentPredicate = ench -> true;
41+
private BiPredicate<Item, Enchantment> enchantmentPredicate = (item, ench) -> true;
42+
private boolean constrainMaxLevel = false;
4143

4244
private ItemAndEnchantmentsPredicateArgumentType() {
4345
}
@@ -51,11 +53,16 @@ public ItemAndEnchantmentsPredicateArgumentType withItemPredicate(Predicate<Item
5153
return this;
5254
}
5355

54-
public ItemAndEnchantmentsPredicateArgumentType withEnchantmentPredicate(Predicate<Enchantment> predicate) {
56+
public ItemAndEnchantmentsPredicateArgumentType withEnchantmentPredicate(BiPredicate<Item, Enchantment> predicate) {
5557
this.enchantmentPredicate = predicate;
5658
return this;
5759
}
5860

61+
public ItemAndEnchantmentsPredicateArgumentType constrainMaxLevel() {
62+
this.constrainMaxLevel = true;
63+
return this;
64+
}
65+
5966
public static ItemAndEnchantmentsPredicate getItemAndEnchantmentsPredicate(CommandContext<?> context, String name) {
6067
return context.getArgument(name, ItemAndEnchantmentsPredicate.class);
6168
}
@@ -223,7 +230,7 @@ private Option parseWithWithoutExactly() throws CommandSyntaxException {
223230
private Enchantment parseEnchantment(boolean suggest, Option option, ItemStack stack) throws CommandSyntaxException {
224231
List<Enchantment> allowedEnchantments = new ArrayList<>();
225232
nextEnchantment: for (Enchantment ench : Registries.ENCHANTMENT) {
226-
boolean skip = (!ench.isAcceptableItem(stack) && stack.getItem() != Items.BOOK) || !enchantmentPredicate.test(ench);
233+
boolean skip = (!ench.isAcceptableItem(stack) && stack.getItem() != Items.BOOK) || !enchantmentPredicate.test(stack.getItem(), ench);
227234
if (skip) {
228235
continue;
229236
}
@@ -278,7 +285,7 @@ private Enchantment parseEnchantment(boolean suggest, Option option, ItemStack s
278285

279286
private int parseEnchantmentLevel(boolean suggest, Option option, ItemStack stack, Enchantment enchantment) throws CommandSyntaxException {
280287
int maxLevel;
281-
{
288+
if (constrainMaxLevel) {
282289
int enchantability = stack.getItem().getEnchantability();
283290
int level = 30 + 1 + enchantability / 4 + enchantability / 4;
284291
level += Math.round(level * 0.15f);
@@ -287,6 +294,8 @@ private int parseEnchantmentLevel(boolean suggest, Option option, ItemStack stac
287294
break;
288295
}
289296
}
297+
} else {
298+
maxLevel = enchantment.getMaxLevel();
290299
}
291300

292301
List<Integer> allowedLevels = new ArrayList<>();

0 commit comments

Comments
 (0)