Skip to content

Commit 16d8c4a

Browse files
committed
Implement config option to only take first enchantment
1 parent 65529aa commit 16d8c4a

File tree

10 files changed

+62
-29
lines changed

10 files changed

+62
-29
lines changed

gradle.properties

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ org.gradle.jvmargs=-Xmx1G
33

44
# Fabric Properties
55
# check these on https://fabricmc.net/versions.html
6-
minecraft_version=1.21.9-rc1
7-
yarn_mappings=1.21.9-rc1+build.1
8-
loader_version=0.17.2
9-
loom_version=1.11-SNAPSHOT
6+
minecraft_version=1.21.11-pre2
7+
yarn_mappings=1.21.11-pre2+build.1
8+
loader_version=0.18.1
9+
loom_version=1.13-SNAPSHOT
1010

1111
# Mod Properties
12-
mod_version = 4.0.1
12+
mod_version = 4.1.0
1313
maven_group = de.mschae23.minecraft.mod
1414
archives_base_name = grind-enchantments
1515

1616
# Dependencies
17-
fabric_api_version=0.133.11+1.21.9
17+
fabric_api_version=0.139.1+1.21.11
1818
codec_config_api_version=3.0.2+1.21.4
1919
tax_free_levels_version=1.4.7-fabric-1.21.1

src/main/java/de/mschae23/grindenchantments/GrindEnchantments.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,20 @@
2424
import net.minecraft.component.type.ItemEnchantmentsComponent;
2525
import net.minecraft.component.type.LoreComponent;
2626
import net.minecraft.component.type.NbtComponent;
27+
import net.minecraft.enchantment.Enchantment;
2728
import net.minecraft.enchantment.EnchantmentHelper;
2829
import net.minecraft.item.ItemStack;
2930
import net.minecraft.nbt.NbtCompound;
3031
import net.minecraft.registry.RegistryWrapper;
32+
import net.minecraft.registry.entry.RegistryEntry;
3133
import net.minecraft.text.MutableText;
3234
import net.minecraft.text.Text;
3335
import net.minecraft.util.Formatting;
3436
import de.mschae23.grindenchantments.config.DedicatedServerConfig;
3537
import de.mschae23.grindenchantments.config.FilterConfig;
3638
import de.mschae23.grindenchantments.cost.CostFunction;
39+
import de.mschae23.grindenchantments.impl.MoveOperation;
40+
import it.unimi.dsi.fastutil.objects.ObjectIntPair;
3741

3842
public class GrindEnchantments {
3943
public static int getLevelCost(ItemStack stack, CostFunction costFunction, FilterConfig filter, RegistryWrapper.WrapperLookup wrapperLookup) {
@@ -43,8 +47,22 @@ public static int getLevelCost(ItemStack stack, CostFunction costFunction, Filte
4347
return (int) Math.ceil(cost);
4448
}
4549

46-
public static ItemEnchantmentsComponent getEnchantments(ItemStack stack, FilterConfig filter) {
47-
return filter.filter(EnchantmentHelper.getEnchantments(stack));
50+
public static ItemEnchantmentsComponent getEnchantments(ItemStack stack, FilterConfig filter, boolean onlyTakeFirst, RegistryWrapper.WrapperLookup wrapperLookup) {
51+
ItemEnchantmentsComponent enchantments = filter.filter(EnchantmentHelper.getEnchantments(stack));
52+
53+
if (onlyTakeFirst) {
54+
ObjectIntPair<RegistryEntry<Enchantment>> firstEnchantment = MoveOperation.getFirstEnchantment(enchantments, false, wrapperLookup);
55+
56+
if (firstEnchantment == null) {
57+
return ItemEnchantmentsComponent.DEFAULT;
58+
} else {
59+
ItemEnchantmentsComponent.Builder builder = new ItemEnchantmentsComponent.Builder(ItemEnchantmentsComponent.DEFAULT);
60+
builder.add(firstEnchantment.left(), firstEnchantment.rightInt());
61+
return builder.build();
62+
}
63+
} else {
64+
return enchantments;
65+
}
4866
}
4967

5068
public static ItemStack addLevelCostComponent(ItemStack stack, IntSupplier cost, boolean canTakeItem, DedicatedServerConfig config) {

src/main/java/de/mschae23/grindenchantments/config/DisenchantConfig.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,25 @@
3030
import com.mojang.serialization.Codec;
3131
import com.mojang.serialization.codecs.RecordCodecBuilder;
3232

33-
public record DisenchantConfig(boolean enabled, boolean consumeItem, CostFunction costFunction) {
33+
public record DisenchantConfig(boolean enabled, boolean consumeItem, boolean onlyTakeFirst, CostFunction costFunction) {
3434
public static final Codec<DisenchantConfig> CODEC = RecordCodecBuilder.create(instance -> instance.group(
3535
Codec.BOOL.fieldOf("enabled").forGetter(DisenchantConfig::enabled),
3636
Codec.BOOL.fieldOf("consume_enchanted_item").forGetter(DisenchantConfig::consumeItem),
37+
Codec.BOOL.fieldOf("only_take_first").orElse(false).forGetter(DisenchantConfig::onlyTakeFirst),
3738
CostFunction.CODEC.fieldOf("cost_function").forGetter(DisenchantConfig::costFunction)
3839
).apply(instance, instance.stable(DisenchantConfig::new)));
3940

40-
public static final DisenchantConfig DEFAULT = new DisenchantConfig(true, false,
41+
public static final DisenchantConfig DEFAULT = new DisenchantConfig(true, false, false,
4142
new FilterCostFunction(new TransformCostFunction(new CountMinPowerCostFunction(), 0.3, 8.0)));
4243

43-
public static final DisenchantConfig DISABLED = new DisenchantConfig(false, false,
44+
public static final DisenchantConfig DISABLED = new DisenchantConfig(false, false, false,
4445
new CountLevelsCostFunction(1.0, 1.0));
4546

4647
public static PacketCodec<PacketByteBuf, DisenchantConfig> createPacketCodec(PacketCodec<PacketByteBuf, CostFunction> costFunctionCodec) {
4748
return PacketCodec.tuple(
4849
PacketCodecs.BOOLEAN, DisenchantConfig::enabled,
4950
PacketCodecs.BOOLEAN, DisenchantConfig::consumeItem,
51+
PacketCodecs.BOOLEAN, DisenchantConfig::onlyTakeFirst,
5052
costFunctionCodec, DisenchantConfig::costFunction,
5153
DisenchantConfig::new
5254
);
@@ -57,6 +59,7 @@ public String toString() {
5759
return "DisenchantConfig{" +
5860
"enabled=" + this.enabled +
5961
", consumeItem=" + this.consumeItem +
62+
", onlyTakeFirst=" + this.onlyTakeFirst +
6063
", costConfig=" + this.costFunction +
6164
'}';
6265
}

src/main/java/de/mschae23/grindenchantments/config/FilterConfig.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,20 @@
3232
import net.minecraft.registry.RegistryKeys;
3333
import net.minecraft.registry.RegistryWrapper;
3434
import net.minecraft.registry.entry.RegistryEntry;
35+
import net.minecraft.registry.entry.RegistryEntryList;
3536
import net.minecraft.registry.tag.EnchantmentTags;
3637
import net.minecraft.util.Identifier;
3738
import net.minecraft.util.dynamic.Codecs;
3839
import com.mojang.datafixers.util.Pair;
3940
import com.mojang.serialization.Codec;
4041
import com.mojang.serialization.codecs.RecordCodecBuilder;
4142
import de.mschae23.grindenchantments.GrindEnchantmentsMod;
43+
import de.mschae23.grindenchantments.impl.MoveOperation;
44+
import it.unimi.dsi.fastutil.objects.Object2IntMap;
45+
import it.unimi.dsi.fastutil.objects.ObjectIntPair;
46+
4247
import org.apache.logging.log4j.Level;
48+
import org.jetbrains.annotations.Nullable;
4349

4450
public record FilterConfig(boolean enabled, ItemConfig item, EnchantmentConfig enchantment, FilterAction curses) {
4551
public static final Codec<FilterConfig> CODEC = RecordCodecBuilder.create(instance -> instance.group(
@@ -101,7 +107,7 @@ public ItemEnchantmentsComponent filter(ItemEnchantmentsComponent enchantments)
101107
return builder.build();
102108
}
103109

104-
public ItemEnchantmentsComponent filterReversed(ItemEnchantmentsComponent enchantments) {
110+
public ItemEnchantmentsComponent filterReversed(ItemEnchantmentsComponent enchantments, boolean onlyRemoveFirst, RegistryWrapper.WrapperLookup wrapperLookup) {
105111
if (!this.enabled) {
106112
return ItemEnchantmentsComponent.DEFAULT;
107113
}
@@ -112,10 +118,15 @@ public ItemEnchantmentsComponent filterReversed(ItemEnchantmentsComponent enchan
112118
return ItemEnchantmentsComponent.DEFAULT;
113119
}
114120

121+
ItemEnchantmentsComponent normalOrderFilteredEnchantments = onlyRemoveFirst ? filter(enchantments) : null;
122+
ObjectIntPair<RegistryEntry<Enchantment>> firstEnchantment = onlyRemoveFirst ? MoveOperation.getFirstEnchantment(normalOrderFilteredEnchantments, false, wrapperLookup) : null;
123+
115124
builder.remove(enchantment ->
116125
((this.curses == FilterAction.ALLOW) || !enchantment.isIn(EnchantmentTags.CURSE))
117126
&& ((this.enchantment.action == FilterAction.ALLOW) == enchantment.getKey().map(key ->
118-
this.enchantment.enchantments.contains(key.getValue())).orElse(false)));
127+
this.enchantment.enchantments.contains(key.getValue())).orElse(false))
128+
&& (!onlyRemoveFirst || enchantment.getKey().flatMap(key -> firstEnchantment.left().getKey().map(firstEnchantmentKey ->
129+
key.getValue().equals(firstEnchantmentKey.getValue()))).orElse(false)));
119130

120131
return builder.build();
121132
}

src/main/java/de/mschae23/grindenchantments/config/legacy/v1/DisenchantConfigV1.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ public record DisenchantConfigV1(boolean enabled, boolean consumeItem, CostConfi
3232
).apply(instance, instance.stable(DisenchantConfigV1::new)));
3333

3434
public DisenchantConfig latest() {
35-
return new DisenchantConfig(this.enabled, this.consumeItem, this.costConfig.latest());
35+
return new DisenchantConfig(this.enabled, this.consumeItem, false, this.costConfig.latest());
3636
}
3737
}

src/main/java/de/mschae23/grindenchantments/config/legacy/v2/GrindEnchantmentsConfigV2.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public record GrindEnchantmentsConfigV2(DisenchantConfig disenchant, MoveConfig
5959
@Override
6060
public GrindEnchantmentsConfigV3 latest() {
6161
return new GrindEnchantmentsConfigV3(
62-
new DisenchantConfig(this.disenchant.enabled(), this.disenchant.consumeItem(),
62+
new DisenchantConfig(this.disenchant.enabled(), this.disenchant.consumeItem(), false,
6363
new FilterCostFunction(this.disenchant.costFunction())),
6464
new MoveConfig(this.move.enabled(),
6565
new FilterCostFunction(this.move.costFunction())),

src/main/java/de/mschae23/grindenchantments/config/sync/ServerConfigS2CPayload.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public Id<? extends CustomPayload> getId() {
4040
public static PacketCodec<PacketByteBuf, ServerConfigS2CPayload> createPacketCodec(PacketCodec<PacketByteBuf, CostFunction> costFunctionCodec) {
4141
return PacketCodec.tuple(
4242
// Version field for forward compatibility
43-
PacketCodecs.BYTE, payload -> (byte) 1,
43+
PacketCodecs.BYTE, payload -> (byte) 2,
4444
ServerConfig.createPacketCodec(costFunctionCodec), ServerConfigS2CPayload::config,
4545
(version, config) -> new ServerConfigS2CPayload(config)
4646
);

src/main/java/de/mschae23/grindenchantments/cost/FirstEnchantmentCostFunction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public record FirstEnchantmentCostFunction(CostFunction function) implements Cos
3939

4040
@Override
4141
public double getCost(ItemEnchantmentsComponent enchantments, FilterConfig filter, RegistryWrapper.WrapperLookup wrapperLookup) {
42-
ObjectIntPair<RegistryEntry<Enchantment>> firstEnchantment = MoveOperation.getFirstEnchantment(enchantments, wrapperLookup);
42+
ObjectIntPair<RegistryEntry<Enchantment>> firstEnchantment = MoveOperation.getFirstEnchantment(enchantments, false, wrapperLookup);
4343

4444
if (firstEnchantment == null) {
4545
return 1.0;

src/main/java/de/mschae23/grindenchantments/impl/DisenchantOperation.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public boolean canInsert(ItemStack stack, ItemStack other, int slotId) {
5353
ItemStack enchantedItemStack = input1.hasEnchantments() ? input1 : input2;
5454

5555
ServerConfig config = GrindEnchantmentsMod.getServerConfig();
56-
return transferEnchantmentsToBook(enchantedItemStack, config.filter());
56+
return transferEnchantmentsToBook(enchantedItemStack, config.filter(), wrapperLookup);
5757
}
5858

5959
@Override
@@ -89,7 +89,7 @@ public boolean onTakeResult(ItemStack input1, ItemStack input2, ItemStack result
8989
}
9090

9191
input.setStack(stack1Book ? 1 : 0, config.disenchant().consumeItem() ?
92-
ItemStack.EMPTY : grind(enchantedItemStack, config.filter()));
92+
ItemStack.EMPTY : grind(enchantedItemStack, config.filter(), wrapperLookup));
9393

9494
if (bookItemStack.getCount() == 1)
9595
input.setStack(stack1Book ? 0 : 1, ItemStack.EMPTY);
@@ -133,20 +133,20 @@ public static boolean canTakeResult(@SuppressWarnings("unused") ItemStack input1
133133
return player.getAbilities().creativeMode || player.experienceLevel >= cost.getAsInt();
134134
}
135135

136-
public static ItemStack transferEnchantmentsToBook(ItemStack source, FilterConfig filter) {
136+
public static ItemStack transferEnchantmentsToBook(ItemStack source, FilterConfig filter, RegistryWrapper.WrapperLookup wrapperLookup) {
137137
if (filter.enabled() && filter.item().action() != FilterAction.IGNORE
138138
&& (filter.item().action() == FilterAction.DENY) == source.getRegistryEntry().getKey().map(key -> filter.item().items().contains(key.getValue())).orElse(false)) {
139139
return ItemStack.EMPTY;
140140
}
141141

142-
ItemEnchantmentsComponent enchantments = GrindEnchantments.getEnchantments(source, filter);
142+
ItemEnchantmentsComponent enchantments = GrindEnchantments.getEnchantments(source, filter, GrindEnchantmentsMod.getServerConfig().disenchant().onlyTakeFirst(), wrapperLookup);
143143

144144
if (enchantments.isEmpty())
145145
return ItemStack.EMPTY;
146146

147147
int repairCost = 0;
148148

149-
for(int i = 0; i < enchantments.getSize(); i++) {
149+
for (int i = 0; i < enchantments.getSize(); i++) {
150150
repairCost = AnvilScreenHandler.getNextCost(repairCost);
151151
}
152152

@@ -156,10 +156,11 @@ public static ItemStack transferEnchantmentsToBook(ItemStack source, FilterConfi
156156
return book;
157157
}
158158

159-
private static ItemStack grind(ItemStack stack, FilterConfig filter) {
159+
private static ItemStack grind(ItemStack stack, FilterConfig filter, RegistryWrapper.WrapperLookup wrapperLookup) {
160160
ItemStack stack2 = stack.copy();
161+
// TODO only remove first
161162
ItemEnchantmentsComponent enchantments = filter.filterReversed(stack2.getOrDefault(EnchantmentHelper.getEnchantmentsComponentType(stack2),
162-
ItemEnchantmentsComponent.DEFAULT));
163+
ItemEnchantmentsComponent.DEFAULT), GrindEnchantmentsMod.getServerConfig().disenchant().onlyTakeFirst(), wrapperLookup);
163164
stack2.set(EnchantmentHelper.getEnchantmentsComponentType(stack2), enchantments);
164165

165166
if (stack2.isOf(Items.ENCHANTED_BOOK) && enchantments.isEmpty()) {

src/main/java/de/mschae23/grindenchantments/impl/MoveOperation.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ public boolean canInsert(ItemStack stack, ItemStack other, int slotId) {
6565
return ItemStack.EMPTY;
6666
}
6767

68-
ItemEnchantmentsComponent enchantments = GrindEnchantments.getEnchantments(input1, filter);
69-
ObjectIntPair<RegistryEntry<Enchantment>> firstEnchantment = getFirstEnchantment(enchantments, wrapperLookup);
68+
ItemEnchantmentsComponent enchantments = GrindEnchantments.getEnchantments(input1, filter, false, wrapperLookup);
69+
ObjectIntPair<RegistryEntry<Enchantment>> firstEnchantment = getFirstEnchantment(enchantments, true, wrapperLookup);
7070

7171
if (firstEnchantment == null) {
7272
return ItemStack.EMPTY;
@@ -125,7 +125,7 @@ public boolean onTakeResult(ItemStack input1, ItemStack input2, ItemStack result
125125
FilterConfig filter = config.filter();
126126

127127
ItemEnchantmentsComponent enchantments = EnchantmentHelper.getEnchantments(input1);
128-
ObjectIntPair<RegistryEntry<Enchantment>> firstEnchantment = getFirstEnchantment(filter.filter(enchantments), wrapperLookup);
128+
ObjectIntPair<RegistryEntry<Enchantment>> firstEnchantment = getFirstEnchantment(filter.filter(enchantments), true, wrapperLookup);
129129

130130
if (firstEnchantment == null) {
131131
return false;
@@ -182,8 +182,8 @@ public static boolean canTakeResult(@SuppressWarnings("unused") ItemStack input1
182182
}
183183

184184
@Nullable
185-
public static ObjectIntPair<RegistryEntry<Enchantment>> getFirstEnchantment(ItemEnchantmentsComponent enchantments, RegistryWrapper.WrapperLookup wrapperLookup) {
186-
if (enchantments.getSize() < 2) {
185+
public static ObjectIntPair<RegistryEntry<Enchantment>> getFirstEnchantment(ItemEnchantmentsComponent enchantments, boolean requireTwo, RegistryWrapper.WrapperLookup wrapperLookup) {
186+
if (requireTwo && enchantments.getSize() < 2) {
187187
return null;
188188
}
189189

0 commit comments

Comments
 (0)