Skip to content

Commit

Permalink
feat: port to 1.21
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelHillcox committed Jun 15, 2024
1 parent 6b2deab commit 6b1c2f0
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 49 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ jobs:
!contains(github.event.head_commit.message, '[ciskip]')
uses: nanite/workflows/.github/workflows/[email protected]
with:
java-version: 17
java-version: 21
changelog-file: "./CHANGELOG.md"
release-to-github: false
secrets:
nanite-token: ${{ secrets.NANITE_DEPLOY }}
curse-token: ${{ secrets.CURSE_DEPLOY }}
github-token: ${{ secrets.GITHUB_TOKEN }}
modrinth-token: ${{ secrets.MODRINTH_TOKEN }}
modrinth-token: ${{ secrets.MODRINTH_TOKEN }}
3 changes: 2 additions & 1 deletion .github/workflows/snapshot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ jobs:
uses: nanite/workflows/.github/workflows/base-java.yml@v1
secrets:
nanite-token: ${{ secrets.NANITE_DEPLOY }}
java-version: 21
with:
use-snapshots: true
gradle-tasks: build publish
gradle-tasks: build publish
8 changes: 4 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
plugins {
id "architectury-plugin" version "3.4-SNAPSHOT"
id "dev.architectury.loom" version "1.5-SNAPSHOT" apply false
id "me.modmuss50.mod-publish-plugin" version "0.4.5"
id "dev.architectury.loom" version "1.6-SNAPSHOT" apply false
id "me.modmuss50.mod-publish-plugin" version "0.5.1"
}

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

tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
options.release = 17
options.release = 21
}

java {
Expand Down Expand Up @@ -123,4 +123,4 @@ publishMods {
accessToken = providers.environmentVariable("GITHUB_TOKEN")
commitish = providers.environmentVariable("GITHUB_SHA").orElse("dryRun")
}
}
}
48 changes: 32 additions & 16 deletions common/src/main/java/dev/wuffs/squatgrow/SquatAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import dev.wuffs.squatgrow.config.SquatGrowConfig;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.Holder;
import net.minecraft.core.component.DataComponents;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
Expand All @@ -14,11 +16,13 @@
import net.minecraft.tags.ItemTags;
import net.minecraft.tags.TagKey;
import net.minecraft.util.Mth;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraft.world.item.enchantment.ItemEnchantments;
import net.minecraft.world.level.GameType;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Blocks;
Expand Down Expand Up @@ -66,7 +70,7 @@ public static Pair<Boolean, List<ItemStack>> passesRequirements(Player player) {
// Let's check the correct things. First, the lighter of the two checks
boolean passesEquipment = false;
if (!requirements.equipmentRequirement.isEmpty()) {
var matchingEquipment = matchingEquipmentItem(player, computedRequirements.equipmentRequirementStacks(), computedRequirements.equipmentRequirementTags());
var matchingEquipment = matchingEquipmentItem(player.level(), player, computedRequirements.equipmentRequirementStacks(), computedRequirements.equipmentRequirementTags());

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

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

// Check the main hand first
var matchingItem = compareItemToLists(mainHand, itemStacks, itemTags);
var matchingItem = compareItemToLists(player.level(), mainHand, itemStacks, itemTags);
if (!matchingItem.isEmpty()) {
return matchingItem;
}

// Check the offhand next
return compareItemToLists(offHand, itemStacks, itemTags);
return compareItemToLists(player.level(), offHand, itemStacks, itemTags);
}

private static ItemStack compareItemToLists(ItemStack stack, List<ItemStack> itemStacks, List<TagKey<Item>> itemTags) {
private static ItemStack compareItemToLists(Level level, ItemStack stack, List<ItemStack> itemStacks, List<TagKey<Item>> itemTags) {
for (ItemStack item : itemStacks) {
if (itemStackMatches(stack, item)) {
if (itemStackMatches(level, stack, item)) {
return stack;
}
}

for (TagKey<Item> tag : itemTags) {
if (itemStackMatches(stack, tag)) {
if (itemStackMatches(level, stack, tag)) {
return stack;
}
}

return ItemStack.EMPTY;
}

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

for (Map.Entry<EquipmentSlot, ItemStack> entry : equipmentStacks.entrySet()) {
ItemStack itemBySlot = player.getItemBySlot(entry.getKey());
if (itemStackMatches(itemBySlot, entry.getValue())) {
if (itemStackMatches(level, itemBySlot, entry.getValue())) {
matchedItems.add(itemBySlot);
}
}

for (Map.Entry<EquipmentSlot, TagKey<Item>> entry : equipmentTags.entrySet()) {
ItemStack itemBySlot = player.getItemBySlot(entry.getKey());
if (itemStackMatches(itemBySlot, entry.getValue())) {
if (itemStackMatches(level, itemBySlot, entry.getValue())) {
matchedItems.add(itemBySlot);
}
}

return matchedItems;
}

private static boolean itemStackMatches(ItemStack stack, TagKey<Item> tag) {
private static boolean itemStackMatches(Level level, ItemStack stack, TagKey<Item> tag) {
if (computedEnchantment != null && stack.isEnchantable()) {
return stack.is(tag) && EnchantmentHelper.getEnchantments(stack).containsKey(computedEnchantment);
var enchantmentValue = computedEnchantment.get(level);
if (enchantmentValue != null && stack.is(tag)) {
ItemEnchantments itemEnchantments = stack.get(DataComponents.ENCHANTMENTS);
if (itemEnchantments != null) {
return stack.is(tag) && itemEnchantments.getLevel(Holder.direct(enchantmentValue)) > 0;
}
}
}

return stack.is(tag);
}

private static boolean itemStackMatches(ItemStack stack, ItemStack item) {
private static boolean itemStackMatches(Level level, ItemStack stack, ItemStack item) {
if (computedEnchantment != null && stack.isEnchantable()) {
return stack.is(item.getItem()) && EnchantmentHelper.getEnchantments(stack).containsKey(computedEnchantment);
var enchantmentValue = computedEnchantment.get(level);
if (enchantmentValue != null) {
if (stack.is(item.getItem())) {
ItemEnchantments itemEnchantments = stack.get(DataComponents.ENCHANTMENTS);
if (itemEnchantments != null) {
return itemEnchantments.getLevel(Holder.direct(enchantmentValue)) > 0;
}
}
}
}

return stack.is(item.getItem());
Expand Down
60 changes: 47 additions & 13 deletions common/src/main/java/dev/wuffs/squatgrow/SquatGrow.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
import me.shedaniel.autoconfig.AutoConfig;
import me.shedaniel.autoconfig.ConfigHolder;
import me.shedaniel.autoconfig.serializer.YamlConfigSerializer;
import net.minecraft.core.Holder;
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.PackType;
import net.minecraft.server.packs.resources.ResourceManager;
Expand All @@ -21,6 +24,7 @@
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.world.item.enchantment.Enchantments;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import org.apache.commons.lang3.tuple.Pair;
Expand All @@ -32,6 +36,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

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

public static ComputedRequirements computedRequirements = null;
public static Enchantment computedEnchantment = null;
public static LazyLevelDependentValue<Enchantment> computedEnchantment = null;

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

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

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

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

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

// This makes me want to puke, defaulted registries suck
if (!newConfig.requirements.requiredEnchantment.isEmpty()) {
ResourceLocation enchantmentRl = new ResourceLocation(newConfig.requirements.requiredEnchantment);
Enchantment enchantment = BuiltInRegistries.ENCHANTMENT.get(enchantmentRl);
// Default for the registry is fortune, we need to make that if we get fortune it matches the enchantmentRl
if (enchantment != Enchantments.BLOCK_FORTUNE || enchantmentRl.equals(new ResourceLocation("minecraft:fortune"))) {
// If the enchantment is not fortune or it is but we want fortune, set it
computedEnchantment = enchantment;
}
ResourceLocation enchantmentRl = ResourceLocation.tryParse(newConfig.requirements.requiredEnchantment);
computedEnchantment = new LazyLevelDependentValue<>(accessor -> {
var key = ResourceKey.create(Registries.ENCHANTMENT, enchantmentRl);

try {
RegistryAccess registryAccess = accessor.registryAccess();
Holder.Reference<Enchantment> enchantmentHolder = registryAccess.lookupOrThrow(Registries.ENCHANTMENT).getOrThrow(key);
return enchantmentHolder.value();
} catch (Exception e) {
LOGGER.error("Enchantment {} not found, falling back to null", enchantmentRl);
computedEnchantment = null;
return null;
}
});
} else {
computedEnchantment = null;
}
Expand Down Expand Up @@ -144,14 +156,36 @@ private static boolean isBlockInIgnoreList(BlockState state) {
private static Pair<List<ItemStack>, List<TagKey<Item>>> computeItemsAndTagsFromStringList(List<String> list) {
List<ItemStack> stacks = list.stream()
.filter(e -> !e.contains("#"))
.map(e -> new ItemStack(BuiltInRegistries.ITEM.get(new ResourceLocation(e))))
.map(e -> new ItemStack(BuiltInRegistries.ITEM.get(ResourceLocation.tryParse(e))))
.toList();

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

return Pair.of(stacks, tags);
}

public static class LazyLevelDependentValue<T> {
@Nullable
private T value = null;
private Function<LevelAccessor, T> supplier;

public LazyLevelDependentValue(Function<LevelAccessor, T> supplier) {
this.supplier = supplier;
}

public T get(LevelAccessor accessor) {
if (value == null) {
value = supplier.apply(accessor);
}

if (value == null) {
throw new IllegalStateException("Value not initialized");
}

return value;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import static dev.wuffs.squatgrow.SquatGrow.config;

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

@Override
public BooleanSupplier isAvailable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import static dev.wuffs.squatgrow.SquatGrow.config;

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

@Override
public BooleanSupplier isAvailable() {
Expand Down
18 changes: 9 additions & 9 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
org.gradle.jvmargs=-Xmx2048M

minecraft_version=1.20.4
enabled_platforms=fabric,forge,neoforge
minecraft_version=1.21
enabled_platforms=fabric,neoforge

archives_base_name=squatgrow
mod_version=6.1.1
maven_group=dev.wuffs

architectury_version=11.1.17
architectury_version=13.0.1

fabric_loader_version=0.15.7
fabric_api_version=0.96.11+1.20.4
fabric_loader_version=0.15.11
fabric_api_version=0.100.1+1.21

forge_version=1.20.4-49.0.38
#forge_version=1.20.4-49.0.38

# Neo
neo_version=20.4.190
neo_gradle=[7.0.60,)
neo_version=21.0.8-beta
neo_gradle=[7.0.143,)

cloth_config_version=13.0.121
cloth_config_version=15.0.127

# Publishing
curseforge_id=515698
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pluginManagement {

include("common")
include("fabric")
include("forge")
//include("forge")
include("neoforge")

rootProject.name = "squatgrow"

0 comments on commit 6b1c2f0

Please sign in to comment.