Skip to content

Commit 6b1c2f0

Browse files
feat: port to 1.21
1 parent 6b2deab commit 6b1c2f0

File tree

10 files changed

+100
-49
lines changed

10 files changed

+100
-49
lines changed

.github/workflows/build-release.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ jobs:
1111
!contains(github.event.head_commit.message, '[ciskip]')
1212
uses: nanite/workflows/.github/workflows/standard-modmuss.yml@v1.1
1313
with:
14-
java-version: 17
14+
java-version: 21
1515
changelog-file: "./CHANGELOG.md"
1616
release-to-github: false
1717
secrets:
1818
nanite-token: ${{ secrets.NANITE_DEPLOY }}
1919
curse-token: ${{ secrets.CURSE_DEPLOY }}
2020
github-token: ${{ secrets.GITHUB_TOKEN }}
21-
modrinth-token: ${{ secrets.MODRINTH_TOKEN }}
21+
modrinth-token: ${{ secrets.MODRINTH_TOKEN }}

.github/workflows/snapshot.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ jobs:
1212
uses: nanite/workflows/.github/workflows/base-java.yml@v1
1313
secrets:
1414
nanite-token: ${{ secrets.NANITE_DEPLOY }}
15+
java-version: 21
1516
with:
1617
use-snapshots: true
17-
gradle-tasks: build publish
18+
gradle-tasks: build publish

build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
plugins {
22
id "architectury-plugin" version "3.4-SNAPSHOT"
3-
id "dev.architectury.loom" version "1.5-SNAPSHOT" apply false
4-
id "me.modmuss50.mod-publish-plugin" version "0.4.5"
3+
id "dev.architectury.loom" version "1.6-SNAPSHOT" apply false
4+
id "me.modmuss50.mod-publish-plugin" version "0.5.1"
55
}
66

77
ext.isSnapshot = providers.environmentVariable("SNAPSHOT").map { it == "true" }.getOrElse(false)
@@ -39,7 +39,7 @@ allprojects {
3939

4040
tasks.withType(JavaCompile) {
4141
options.encoding = "UTF-8"
42-
options.release = 17
42+
options.release = 21
4343
}
4444

4545
java {
@@ -123,4 +123,4 @@ publishMods {
123123
accessToken = providers.environmentVariable("GITHUB_TOKEN")
124124
commitish = providers.environmentVariable("GITHUB_SHA").orElse("dryRun")
125125
}
126-
}
126+
}

common/src/main/java/dev/wuffs/squatgrow/SquatAction.java

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import dev.wuffs.squatgrow.config.SquatGrowConfig;
77
import net.minecraft.core.BlockPos;
88
import net.minecraft.core.Direction;
9+
import net.minecraft.core.Holder;
10+
import net.minecraft.core.component.DataComponents;
911
import net.minecraft.core.particles.ParticleTypes;
1012
import net.minecraft.server.level.ServerLevel;
1113
import net.minecraft.server.level.ServerPlayer;
@@ -14,11 +16,13 @@
1416
import net.minecraft.tags.ItemTags;
1517
import net.minecraft.tags.TagKey;
1618
import net.minecraft.util.Mth;
19+
import net.minecraft.world.InteractionHand;
1720
import net.minecraft.world.entity.EquipmentSlot;
1821
import net.minecraft.world.entity.player.Player;
1922
import net.minecraft.world.item.Item;
2023
import net.minecraft.world.item.ItemStack;
2124
import net.minecraft.world.item.enchantment.EnchantmentHelper;
25+
import net.minecraft.world.item.enchantment.ItemEnchantments;
2226
import net.minecraft.world.level.GameType;
2327
import net.minecraft.world.level.Level;
2428
import net.minecraft.world.level.block.Blocks;
@@ -66,7 +70,7 @@ public static Pair<Boolean, List<ItemStack>> passesRequirements(Player player) {
6670
// Let's check the correct things. First, the lighter of the two checks
6771
boolean passesEquipment = false;
6872
if (!requirements.equipmentRequirement.isEmpty()) {
69-
var matchingEquipment = matchingEquipmentItem(player, computedRequirements.equipmentRequirementStacks(), computedRequirements.equipmentRequirementTags());
73+
var matchingEquipment = matchingEquipmentItem(player.level(), player, computedRequirements.equipmentRequirementStacks(), computedRequirements.equipmentRequirementTags());
7074

7175
// This is safe to do as it will only increment if the equipment is found, and you can only have one item per slot
7276
if (matchingEquipment.size() == requirements.equipmentRequirement.size()) {
@@ -143,9 +147,7 @@ public static void grow(Level level, ServerPlayer player, List<ItemStack> itemsT
143147
if ((config.hoeTakesDamage || config.requirements.requiredItemTakesDamage) && didGrow && !itemsToDamage.isEmpty()) {
144148
var durabilityToApply = config.hoeTakesDamage ? 1 : config.requirements.durabilityDamage;
145149
for (ItemStack item : itemsToDamage) {
146-
item.hurtAndBreak(durabilityToApply, player, (playerEntity) -> {
147-
playerEntity.broadcastBreakEvent(player.getUsedItemHand());
148-
});
150+
item.hurtAndBreak(durabilityToApply, player, player.getUsedItemHand() == InteractionHand.MAIN_HAND ? EquipmentSlot.MAINHAND : EquipmentSlot.OFFHAND);
149151
}
150152
}
151153

@@ -209,62 +211,76 @@ private static ItemStack getMatchingHeldItem(Player player, List<ItemStack> item
209211
var offHand = player.getOffhandItem();
210212

211213
// Check the main hand first
212-
var matchingItem = compareItemToLists(mainHand, itemStacks, itemTags);
214+
var matchingItem = compareItemToLists(player.level(), mainHand, itemStacks, itemTags);
213215
if (!matchingItem.isEmpty()) {
214216
return matchingItem;
215217
}
216218

217219
// Check the offhand next
218-
return compareItemToLists(offHand, itemStacks, itemTags);
220+
return compareItemToLists(player.level(), offHand, itemStacks, itemTags);
219221
}
220222

221-
private static ItemStack compareItemToLists(ItemStack stack, List<ItemStack> itemStacks, List<TagKey<Item>> itemTags) {
223+
private static ItemStack compareItemToLists(Level level, ItemStack stack, List<ItemStack> itemStacks, List<TagKey<Item>> itemTags) {
222224
for (ItemStack item : itemStacks) {
223-
if (itemStackMatches(stack, item)) {
225+
if (itemStackMatches(level, stack, item)) {
224226
return stack;
225227
}
226228
}
227229

228230
for (TagKey<Item> tag : itemTags) {
229-
if (itemStackMatches(stack, tag)) {
231+
if (itemStackMatches(level, stack, tag)) {
230232
return stack;
231233
}
232234
}
233235

234236
return ItemStack.EMPTY;
235237
}
236238

237-
private static List<ItemStack> matchingEquipmentItem(Player player, Map<EquipmentSlot, ItemStack> equipmentStacks, Map<EquipmentSlot, TagKey<Item>> equipmentTags) {
239+
private static List<ItemStack> matchingEquipmentItem(Level level, Player player, Map<EquipmentSlot, ItemStack> equipmentStacks, Map<EquipmentSlot, TagKey<Item>> equipmentTags) {
238240
List<ItemStack> matchedItems = new ArrayList<>();
239241

240242
for (Map.Entry<EquipmentSlot, ItemStack> entry : equipmentStacks.entrySet()) {
241243
ItemStack itemBySlot = player.getItemBySlot(entry.getKey());
242-
if (itemStackMatches(itemBySlot, entry.getValue())) {
244+
if (itemStackMatches(level, itemBySlot, entry.getValue())) {
243245
matchedItems.add(itemBySlot);
244246
}
245247
}
246248

247249
for (Map.Entry<EquipmentSlot, TagKey<Item>> entry : equipmentTags.entrySet()) {
248250
ItemStack itemBySlot = player.getItemBySlot(entry.getKey());
249-
if (itemStackMatches(itemBySlot, entry.getValue())) {
251+
if (itemStackMatches(level, itemBySlot, entry.getValue())) {
250252
matchedItems.add(itemBySlot);
251253
}
252254
}
253255

254256
return matchedItems;
255257
}
256258

257-
private static boolean itemStackMatches(ItemStack stack, TagKey<Item> tag) {
259+
private static boolean itemStackMatches(Level level, ItemStack stack, TagKey<Item> tag) {
258260
if (computedEnchantment != null && stack.isEnchantable()) {
259-
return stack.is(tag) && EnchantmentHelper.getEnchantments(stack).containsKey(computedEnchantment);
261+
var enchantmentValue = computedEnchantment.get(level);
262+
if (enchantmentValue != null && stack.is(tag)) {
263+
ItemEnchantments itemEnchantments = stack.get(DataComponents.ENCHANTMENTS);
264+
if (itemEnchantments != null) {
265+
return stack.is(tag) && itemEnchantments.getLevel(Holder.direct(enchantmentValue)) > 0;
266+
}
267+
}
260268
}
261269

262270
return stack.is(tag);
263271
}
264272

265-
private static boolean itemStackMatches(ItemStack stack, ItemStack item) {
273+
private static boolean itemStackMatches(Level level, ItemStack stack, ItemStack item) {
266274
if (computedEnchantment != null && stack.isEnchantable()) {
267-
return stack.is(item.getItem()) && EnchantmentHelper.getEnchantments(stack).containsKey(computedEnchantment);
275+
var enchantmentValue = computedEnchantment.get(level);
276+
if (enchantmentValue != null) {
277+
if (stack.is(item.getItem())) {
278+
ItemEnchantments itemEnchantments = stack.get(DataComponents.ENCHANTMENTS);
279+
if (itemEnchantments != null) {
280+
return itemEnchantments.getLevel(Holder.direct(enchantmentValue)) > 0;
281+
}
282+
}
283+
}
268284
}
269285

270286
return stack.is(item.getItem());

common/src/main/java/dev/wuffs/squatgrow/SquatGrow.java

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88
import me.shedaniel.autoconfig.AutoConfig;
99
import me.shedaniel.autoconfig.ConfigHolder;
1010
import me.shedaniel.autoconfig.serializer.YamlConfigSerializer;
11+
import net.minecraft.core.Holder;
12+
import net.minecraft.core.RegistryAccess;
1113
import net.minecraft.core.registries.BuiltInRegistries;
1214
import net.minecraft.core.registries.Registries;
15+
import net.minecraft.resources.ResourceKey;
1316
import net.minecraft.resources.ResourceLocation;
1417
import net.minecraft.server.packs.PackType;
1518
import net.minecraft.server.packs.resources.ResourceManager;
@@ -21,6 +24,7 @@
2124
import net.minecraft.world.item.ItemStack;
2225
import net.minecraft.world.item.enchantment.Enchantment;
2326
import net.minecraft.world.item.enchantment.Enchantments;
27+
import net.minecraft.world.level.LevelAccessor;
2428
import net.minecraft.world.level.block.Block;
2529
import net.minecraft.world.level.block.state.BlockState;
2630
import org.apache.commons.lang3.tuple.Pair;
@@ -32,6 +36,7 @@
3236
import java.util.List;
3337
import java.util.Map;
3438
import java.util.Set;
39+
import java.util.function.Function;
3540
import java.util.stream.Collectors;
3641

3742
public class SquatGrow {
@@ -45,7 +50,7 @@ public class SquatGrow {
4550
public static final Set<String> wildcardCache = new HashSet<>();
4651

4752
public static ComputedRequirements computedRequirements = null;
48-
public static Enchantment computedEnchantment = null;
53+
public static LazyLevelDependentValue<Enchantment> computedEnchantment = null;
4954

5055
public static void init() {
5156
configHolder = AutoConfig.register(SquatGrowConfig.class, YamlConfigSerializer::new);
@@ -81,7 +86,7 @@ private static InteractionResult onConfigChanged(ConfigHolder<SquatGrowConfig> h
8186

8287
tagCache.addAll(newConfig.ignoreList.stream()
8388
.filter(e -> e.contains("#"))
84-
.map(e -> TagKey.create(Registries.BLOCK, new ResourceLocation(e.replace("#", ""))))
89+
.map(e -> TagKey.create(Registries.BLOCK, ResourceLocation.tryParse(e.replace("#", ""))))
8590
.collect(Collectors.toSet()));
8691

8792
wildcardCache.addAll(newConfig.ignoreList.stream().filter(e -> e.contains("*")).map(e -> e.split(":")[0])
@@ -95,11 +100,11 @@ private static InteractionResult onConfigChanged(ConfigHolder<SquatGrowConfig> h
95100
// This is kinda gross, but it does work so /shrug
96101
Map<EquipmentSlot, ItemStack> equipmentRequirementStacks = equipmentRequirement.entrySet().stream()
97102
.filter(e -> !e.getValue().contains("#"))
98-
.collect(Collectors.toMap(Map.Entry::getKey, e -> new ItemStack(BuiltInRegistries.ITEM.get(new ResourceLocation(e.getValue())))));
103+
.collect(Collectors.toMap(Map.Entry::getKey, e -> new ItemStack(BuiltInRegistries.ITEM.get(ResourceLocation.tryParse(e.getValue())))));
99104

100105
Map<EquipmentSlot, TagKey<Item>> equipmentRequirementTags = equipmentRequirement.entrySet().stream()
101106
.filter(e -> e.getValue().contains("#"))
102-
.collect(Collectors.toMap(Map.Entry::getKey, e -> TagKey.create(Registries.ITEM, new ResourceLocation(e.getValue().replace("#", "")))));
107+
.collect(Collectors.toMap(Map.Entry::getKey, e -> TagKey.create(Registries.ITEM, ResourceLocation.tryParse(e.getValue().replace("#", "")))));
103108

104109
computedRequirements = new ComputedRequirements(
105110
computedHeldEntries.getLeft(),
@@ -110,13 +115,20 @@ private static InteractionResult onConfigChanged(ConfigHolder<SquatGrowConfig> h
110115

111116
// This makes me want to puke, defaulted registries suck
112117
if (!newConfig.requirements.requiredEnchantment.isEmpty()) {
113-
ResourceLocation enchantmentRl = new ResourceLocation(newConfig.requirements.requiredEnchantment);
114-
Enchantment enchantment = BuiltInRegistries.ENCHANTMENT.get(enchantmentRl);
115-
// Default for the registry is fortune, we need to make that if we get fortune it matches the enchantmentRl
116-
if (enchantment != Enchantments.BLOCK_FORTUNE || enchantmentRl.equals(new ResourceLocation("minecraft:fortune"))) {
117-
// If the enchantment is not fortune or it is but we want fortune, set it
118-
computedEnchantment = enchantment;
119-
}
118+
ResourceLocation enchantmentRl = ResourceLocation.tryParse(newConfig.requirements.requiredEnchantment);
119+
computedEnchantment = new LazyLevelDependentValue<>(accessor -> {
120+
var key = ResourceKey.create(Registries.ENCHANTMENT, enchantmentRl);
121+
122+
try {
123+
RegistryAccess registryAccess = accessor.registryAccess();
124+
Holder.Reference<Enchantment> enchantmentHolder = registryAccess.lookupOrThrow(Registries.ENCHANTMENT).getOrThrow(key);
125+
return enchantmentHolder.value();
126+
} catch (Exception e) {
127+
LOGGER.error("Enchantment {} not found, falling back to null", enchantmentRl);
128+
computedEnchantment = null;
129+
return null;
130+
}
131+
});
120132
} else {
121133
computedEnchantment = null;
122134
}
@@ -144,14 +156,36 @@ private static boolean isBlockInIgnoreList(BlockState state) {
144156
private static Pair<List<ItemStack>, List<TagKey<Item>>> computeItemsAndTagsFromStringList(List<String> list) {
145157
List<ItemStack> stacks = list.stream()
146158
.filter(e -> !e.contains("#"))
147-
.map(e -> new ItemStack(BuiltInRegistries.ITEM.get(new ResourceLocation(e))))
159+
.map(e -> new ItemStack(BuiltInRegistries.ITEM.get(ResourceLocation.tryParse(e))))
148160
.toList();
149161

150162
List<TagKey<Item>> tags = list.stream()
151163
.filter(e -> e.contains("#"))
152-
.map(e -> TagKey.create(Registries.ITEM, new ResourceLocation(e.replace("#", ""))))
164+
.map(e -> TagKey.create(Registries.ITEM, ResourceLocation.tryParse(e.replace("#", ""))))
153165
.toList();
154166

155167
return Pair.of(stacks, tags);
156168
}
169+
170+
public static class LazyLevelDependentValue<T> {
171+
@Nullable
172+
private T value = null;
173+
private Function<LevelAccessor, T> supplier;
174+
175+
public LazyLevelDependentValue(Function<LevelAccessor, T> supplier) {
176+
this.supplier = supplier;
177+
}
178+
179+
public T get(LevelAccessor accessor) {
180+
if (value == null) {
181+
value = supplier.apply(accessor);
182+
}
183+
184+
if (value == null) {
185+
throw new IllegalStateException("Value not initialized");
186+
}
187+
188+
return value;
189+
}
190+
}
157191
}

common/src/main/java/dev/wuffs/squatgrow/actions/integrations/AE2Action.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import static dev.wuffs.squatgrow.SquatGrow.config;
1414

1515
public class AE2Action extends RandomTickableAction {
16-
private static final TagKey<Block> AE2_TAG = TagKey.create(Registries.BLOCK, new ResourceLocation("ae2", "growth_acceleratable"));
16+
private static final TagKey<Block> AE2_TAG = TagKey.create(Registries.BLOCK, ResourceLocation.fromNamespaceAndPath("ae2", "growth_acceleratable"));
1717

1818
@Override
1919
public BooleanSupplier isAvailable() {

common/src/main/java/dev/wuffs/squatgrow/actions/integrations/MysticalAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import static dev.wuffs.squatgrow.SquatGrow.config;
1414

1515
public class MysticalAction extends GrowCropAction {
16-
private static final TagKey<Block> MYSTICAL_TAG = TagKey.create(Registries.BLOCK, new ResourceLocation("mysticalagriculture", "crops"));
16+
private static final TagKey<Block> MYSTICAL_TAG = TagKey.create(Registries.BLOCK, ResourceLocation.fromNamespaceAndPath("mysticalagriculture", "crops"));
1717

1818
@Override
1919
public BooleanSupplier isAvailable() {

gradle.properties

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
org.gradle.jvmargs=-Xmx2048M
22

3-
minecraft_version=1.20.4
4-
enabled_platforms=fabric,forge,neoforge
3+
minecraft_version=1.21
4+
enabled_platforms=fabric,neoforge
55

66
archives_base_name=squatgrow
77
mod_version=6.1.1
88
maven_group=dev.wuffs
99

10-
architectury_version=11.1.17
10+
architectury_version=13.0.1
1111

12-
fabric_loader_version=0.15.7
13-
fabric_api_version=0.96.11+1.20.4
12+
fabric_loader_version=0.15.11
13+
fabric_api_version=0.100.1+1.21
1414

15-
forge_version=1.20.4-49.0.38
15+
#forge_version=1.20.4-49.0.38
1616

1717
# Neo
18-
neo_version=20.4.190
19-
neo_gradle=[7.0.60,)
18+
neo_version=21.0.8-beta
19+
neo_gradle=[7.0.143,)
2020

21-
cloth_config_version=13.0.121
21+
cloth_config_version=15.0.127
2222

2323
# Publishing
2424
curseforge_id=515698
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists

settings.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pluginManagement {
1111

1212
include("common")
1313
include("fabric")
14-
include("forge")
14+
//include("forge")
1515
include("neoforge")
1616

1717
rootProject.name = "squatgrow"

0 commit comments

Comments
 (0)