From ce34f7cc1a800e78cecbe10465fa4291cb83e446 Mon Sep 17 00:00:00 2001 From: JFronny Date: Sun, 16 Jul 2023 15:41:00 +0200 Subject: [PATCH] feat(villagers): Use feature implementation by enjarai --- build.gradle.kts | 8 ++-- .../io/gitlab/jfronny/yescheat/Plugin.java | 38 +++++++------------ .../io/gitlab/jfronny/yescheat/YesCheat.java | 35 +++++++++++++++++ .../mixin/VillagersFollowEmeralds.java | 26 ------------- .../mixin/VillagersFollowEmeralds1.java | 36 ++++++++++++++++++ .../mixin/VillagersFollowEmeralds2.java | 20 ++++++++++ src/main/resources/YesCheat.mixins.json | 13 ++++--- .../tags/items/villager_temptations.json | 8 ++++ src/main/resources/fabric.mod.json | 1 + 9 files changed, 125 insertions(+), 60 deletions(-) create mode 100644 src/main/java/io/gitlab/jfronny/yescheat/YesCheat.java delete mode 100644 src/main/java/io/gitlab/jfronny/yescheat/mixin/VillagersFollowEmeralds.java create mode 100644 src/main/java/io/gitlab/jfronny/yescheat/mixin/VillagersFollowEmeralds1.java create mode 100644 src/main/java/io/gitlab/jfronny/yescheat/mixin/VillagersFollowEmeralds2.java create mode 100644 src/main/resources/data/yescheat/tags/items/villager_temptations.json diff --git a/build.gradle.kts b/build.gradle.kts index 5bf3731..f92b67f 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -2,18 +2,18 @@ plugins { id("jfmod") version "1.5-SNAPSHOT" } -group = "io.gitlab.jfronny" +allprojects { group = "io.gitlab.jfronny" } base.archivesName = "yescheat" jfMod { minecraftVersion = "1.20.1" yarn("build.9") loaderVersion = "0.14.21" - libJfVersion = "3.9.0" + libJfVersion = "3.9.2" modrinth { projectId = "yescheat" - requiredDependencies.add("libjf") + requiredDependencies.addAll("libjf", "fabric-api") } } @@ -21,7 +21,7 @@ dependencies { val fabricVersion = "0.85.0+${jfMod.minecraftVersion.get()}" modImplementation("io.gitlab.jfronny.libjf:libjf-config-core-v1:${jfMod.libJfVersion.get()}") - include(modImplementation(fabricApi.module("fabric-transitive-access-wideners-v1", fabricVersion))!!) + modImplementation("net.fabricmc.fabric-api:fabric-api:0.85.0+${jfMod.minecraftVersion.get()}") // Dev env modLocalRuntime("io.gitlab.jfronny.libjf:libjf-config-ui-tiny-v1:${jfMod.libJfVersion.get()}") diff --git a/src/main/java/io/gitlab/jfronny/yescheat/Plugin.java b/src/main/java/io/gitlab/jfronny/yescheat/Plugin.java index 63d88fb..21631bd 100644 --- a/src/main/java/io/gitlab/jfronny/yescheat/Plugin.java +++ b/src/main/java/io/gitlab/jfronny/yescheat/Plugin.java @@ -1,13 +1,11 @@ package io.gitlab.jfronny.yescheat; -import io.gitlab.jfronny.yescheat.mixin.*; -import io.gitlab.jfronny.yescheat.mixin.UncapEnchants; -import io.gitlab.jfronny.yescheat.mixin.UnlockMendingInfinity; import org.objectweb.asm.tree.ClassNode; import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin; import org.spongepowered.asm.mixin.extensibility.IMixinInfo; -import java.util.*; +import java.util.List; +import java.util.Set; public class Plugin implements IMixinConfigPlugin { @Override @@ -19,32 +17,24 @@ public class Plugin implements IMixinConfigPlugin { return null; } - @SuppressWarnings("ReferenceToMixin") @Override public boolean shouldApplyMixin(String targetClassName, String mixinClassName) { final String prefix = "io.gitlab.jfronny.yescheat.mixin."; if (!mixinClassName.startsWith(prefix)) throw new IllegalArgumentException("Mixin in unexpected package: " + mixinClassName); mixinClassName = mixinClassName.substring(prefix.length()); - if (Objects.equals(mixinClassName, "UnblockChests")) - return Cfg.unblockChests; - else if (Objects.equals(mixinClassName, "UncapEnchants")) - return Cfg.uncapEnchants; - else if (Objects.equals(mixinClassName, "IgnoreEula")) - return true; - else if (Objects.equals(mixinClassName, "DistantContainers3x3") - || Objects.equals(mixinClassName, "DistantContainerXx9")) - return Cfg.distantContainers; - else if (Objects.equals(mixinClassName, "UnlockMendingInfinity")) - return Cfg.unlockMending; - else if (Objects.equals(mixinClassName, "DistantBreaking")) - return Cfg.distantBreaking; - else if (Objects.equals(mixinClassName, "RemoveRubberbanding")) - return Cfg.antiRubberband; - else if (Objects.equals(mixinClassName, "VillagersFollowEmeralds")) - return Cfg.villagersFollowEmeralds; - else - throw new IllegalArgumentException("Unrecognized mixin: " + mixinClassName + "! This should never happen"); + if (mixinClassName.startsWith("Debug")) return true; + return switch (mixinClassName) { + case "UnblockChests" -> Cfg.unblockChests; + case "UncapEnchants" -> Cfg.uncapEnchants; + case "IgnoreEula" -> true; + case "DistantContainers3x3", "DistantContainerXx9" -> Cfg.distantContainers; + case "UnlockMendingInfinity" -> Cfg.unlockMending; + case "DistantBreaking" -> Cfg.distantBreaking; + case "RemoveRubberbanding" -> Cfg.antiRubberband; + case "VillagersFollowEmeralds1", "VillagersFollowEmeralds2" -> Cfg.villagersFollowEmeralds; + default -> throw new IllegalArgumentException("Unrecognized mixin: " + mixinClassName + "! This should never happen"); + }; } @Override diff --git a/src/main/java/io/gitlab/jfronny/yescheat/YesCheat.java b/src/main/java/io/gitlab/jfronny/yescheat/YesCheat.java new file mode 100644 index 0000000..84a22ef --- /dev/null +++ b/src/main/java/io/gitlab/jfronny/yescheat/YesCheat.java @@ -0,0 +1,35 @@ +package io.gitlab.jfronny.yescheat; + +import net.fabricmc.api.ModInitializer; +import net.minecraft.entity.ai.brain.sensor.SensorType; +import net.minecraft.entity.ai.brain.sensor.TemptationsSensor; +import net.minecraft.recipe.Ingredient; +import net.minecraft.registry.*; +import net.minecraft.registry.tag.TagKey; +import net.minecraft.util.Identifier; + +public class YesCheat implements ModInitializer { + public static final String MOD_ID = "yescheat"; + + private static final Identifier TEMPTATIONS_ID = new Identifier(MOD_ID, "villager_temptations"); + public static SensorType VILLAGER_TEMPTATIONS; + + @Override + public void onInitialize() { + System.out.println("Ae"); + if (Cfg.villagersFollowEmeralds) { + villagersFollowEmeraldsInit(); + Registry.register(Registries.SENSOR_TYPE, TEMPTATIONS_ID, VILLAGER_TEMPTATIONS); + } + } + + public static synchronized void villagersFollowEmeraldsInit() { + if (!Cfg.villagersFollowEmeralds) throw new IllegalStateException("villagersFollowEmeralds is not enabled but its initializer is called"); + if (VILLAGER_TEMPTATIONS == null) { + VILLAGER_TEMPTATIONS = + new SensorType<>(() -> new TemptationsSensor((Ingredient.fromTag( + TagKey.of(RegistryKeys.ITEM, TEMPTATIONS_ID) + )))); + } + } +} diff --git a/src/main/java/io/gitlab/jfronny/yescheat/mixin/VillagersFollowEmeralds.java b/src/main/java/io/gitlab/jfronny/yescheat/mixin/VillagersFollowEmeralds.java deleted file mode 100644 index ca2800b..0000000 --- a/src/main/java/io/gitlab/jfronny/yescheat/mixin/VillagersFollowEmeralds.java +++ /dev/null @@ -1,26 +0,0 @@ -package io.gitlab.jfronny.yescheat.mixin; - -import net.minecraft.entity.EntityType; -import net.minecraft.entity.ai.goal.TemptGoal; -import net.minecraft.entity.passive.MerchantEntity; -import net.minecraft.entity.passive.VillagerEntity; -import net.minecraft.item.Items; -import net.minecraft.recipe.Ingredient; -import net.minecraft.village.VillagerType; -import net.minecraft.world.World; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - -@Mixin(VillagerEntity.class) -public abstract class VillagersFollowEmeralds extends MerchantEntity { - public VillagersFollowEmeralds(EntityType entityType, World world) { - super(entityType, world); - } - - @Inject(method = "(Lnet/minecraft/entity/EntityType;Lnet/minecraft/world/World;Lnet/minecraft/village/VillagerType;)V", at = @At(value = "TAIL")) - private void inject(EntityType entityType, World world, VillagerType type, CallbackInfo ci) { - this.goalSelector.add(2, new TemptGoal(this, .4D, Ingredient.ofItems(Items.EMERALD_BLOCK, Items.EMERALD_ORE, Items.DEEPSLATE_EMERALD_ORE), false)); - } -} diff --git a/src/main/java/io/gitlab/jfronny/yescheat/mixin/VillagersFollowEmeralds1.java b/src/main/java/io/gitlab/jfronny/yescheat/mixin/VillagersFollowEmeralds1.java new file mode 100644 index 0000000..3eda451 --- /dev/null +++ b/src/main/java/io/gitlab/jfronny/yescheat/mixin/VillagersFollowEmeralds1.java @@ -0,0 +1,36 @@ +package io.gitlab.jfronny.yescheat.mixin; + +import com.google.common.collect.ImmutableList; +import io.gitlab.jfronny.yescheat.YesCheat; +import net.minecraft.entity.ai.brain.MemoryModuleType; +import net.minecraft.entity.ai.brain.sensor.Sensor; +import net.minecraft.entity.ai.brain.sensor.SensorType; +import net.minecraft.entity.passive.VillagerEntity; +import org.spongepowered.asm.mixin.*; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import java.util.ArrayList; + +@Mixin(VillagerEntity.class) +public abstract class VillagersFollowEmeralds1 { + @Mutable @Shadow @Final private static ImmutableList> MEMORY_MODULES; + @Mutable @Shadow @Final private static ImmutableList>> SENSORS; + + @Inject(method = "", at = @At("TAIL")) + private static void addRequiredMemoryModulesAndSensors(CallbackInfo ci) { + YesCheat.villagersFollowEmeraldsInit(); + + var newMemoryModules = new ArrayList<>(MEMORY_MODULES); + newMemoryModules.add(MemoryModuleType.TEMPTATION_COOLDOWN_TICKS); + newMemoryModules.add(MemoryModuleType.IS_TEMPTED); + newMemoryModules.add(MemoryModuleType.TEMPTING_PLAYER); + newMemoryModules.add(MemoryModuleType.IS_PANICKING); + MEMORY_MODULES = ImmutableList.copyOf(newMemoryModules); + + var newSensors = new ArrayList<>(SENSORS); + newSensors.add(YesCheat.VILLAGER_TEMPTATIONS); + SENSORS = ImmutableList.copyOf(newSensors); + } +} diff --git a/src/main/java/io/gitlab/jfronny/yescheat/mixin/VillagersFollowEmeralds2.java b/src/main/java/io/gitlab/jfronny/yescheat/mixin/VillagersFollowEmeralds2.java new file mode 100644 index 0000000..1c9f0cb --- /dev/null +++ b/src/main/java/io/gitlab/jfronny/yescheat/mixin/VillagersFollowEmeralds2.java @@ -0,0 +1,20 @@ +package io.gitlab.jfronny.yescheat.mixin; + +import com.mojang.datafixers.util.Pair; +import net.minecraft.entity.ai.brain.MemoryModuleType; +import net.minecraft.entity.ai.brain.task.*; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.ModifyArg; + +@Mixin(VillagerTaskListProvider.class) +public abstract class VillagersFollowEmeralds2 { + @ModifyArg(method = "createCoreTasks", at = @At(value = "INVOKE", target = "Lcom/google/common/collect/ImmutableList;of(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;[Ljava/lang/Object;)Lcom/google/common/collect/ImmutableList;", remap = false)) + private static Object[] addTemptationAsCoreTask(Object[] original) { + Object[] modified = new Object[original.length + 2]; + System.arraycopy(original, 0, modified, 0, original.length); + modified[original.length] = Pair.of(0, new TemptTask(e -> 0.5f)); + modified[original.length + 1] = Pair.of(0, new TemptationCooldownTask(MemoryModuleType.TEMPTATION_COOLDOWN_TICKS)); + return modified; + } +} diff --git a/src/main/resources/YesCheat.mixins.json b/src/main/resources/YesCheat.mixins.json index de5a746..173ece2 100644 --- a/src/main/resources/YesCheat.mixins.json +++ b/src/main/resources/YesCheat.mixins.json @@ -5,15 +5,16 @@ "compatibilityLevel": "JAVA_16", "plugin": "io.gitlab.jfronny.yescheat.Plugin", "mixins": [ - "UnblockChests", - "UncapEnchants", - "IgnoreEula", + "DistantBreaking", "DistantContainers3x3", "DistantContainerXx9", - "UnlockMendingInfinity", + "IgnoreEula", "RemoveRubberbanding", - "DistantBreaking", - "VillagersFollowEmeralds" + "UnblockChests", + "UncapEnchants", + "UnlockMendingInfinity", + "VillagersFollowEmeralds1", + "VillagersFollowEmeralds2" ], "injectors": { "defaultRequire": 1 diff --git a/src/main/resources/data/yescheat/tags/items/villager_temptations.json b/src/main/resources/data/yescheat/tags/items/villager_temptations.json new file mode 100644 index 0000000..109a750 --- /dev/null +++ b/src/main/resources/data/yescheat/tags/items/villager_temptations.json @@ -0,0 +1,8 @@ +{ + "values": [ + "minecraft:emerald_block", + "minecraft:emerald_ore", + "minecraft:deepslate_emerald_ore", + "#c:emeralds" + ] +} \ No newline at end of file diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 47e3ad5..6915ce8 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -15,6 +15,7 @@ "icon": "assets/yescheat/icon.png", "environment": "*", "entrypoints": { + "main": ["io.gitlab.jfronny.yescheat.YesCheat"], "libjf:config": ["io.gitlab.jfronny.yescheat.JFC_Cfg"] }, "mixins": [