From e6597d9ffc554403c52d1d5afed3b722217a708a Mon Sep 17 00:00:00 2001 From: Majrusz Date: Sat, 3 Jun 2023 17:28:01 +0200 Subject: [PATCH] Merged 4.2.0 release --- build.gradle | 1 + gradle.properties | 2 +- src/main/java/com/mlib/Utility.java | 20 ++++++--- .../contexts/OnFishingTimeSet.java | 43 +++++++++++++++++++ .../java/com/mlib/mixin/MixinFishingHook.java | 23 ++++++++++ src/main/resources/mixins.mlib.json | 1 + 6 files changed, 84 insertions(+), 6 deletions(-) create mode 100644 src/main/java/com/mlib/gamemodifiers/contexts/OnFishingTimeSet.java create mode 100644 src/main/java/com/mlib/mixin/MixinFishingHook.java diff --git a/build.gradle b/build.gradle index 0f800380..95bbd2f2 100644 --- a/build.gradle +++ b/build.gradle @@ -97,6 +97,7 @@ processResources { filesMatching('META-INF/mods.toml') { expand project.properties } + outputs.upToDateWhen { false } } dependencies { diff --git a/gradle.properties b/gradle.properties index 81a962b6..664cee2a 100644 --- a/gradle.properties +++ b/gradle.properties @@ -6,4 +6,4 @@ versions_minecraft=1.18.2 versions_minecraft_range=[1.18.2,1.19) versions_forge=40.2.0 versions_forge_range=[40.2.0,) -versions_mod=4.1.0 \ No newline at end of file +versions_mod=4.2.0 \ No newline at end of file diff --git a/src/main/java/com/mlib/Utility.java b/src/main/java/com/mlib/Utility.java index 0f660d25..f6a32326 100644 --- a/src/main/java/com/mlib/Utility.java +++ b/src/main/java/com/mlib/Utility.java @@ -10,6 +10,7 @@ import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.enchantment.Enchantment; import net.minecraft.world.level.Level; +import net.minecraftforge.fml.DistExecutor; import net.minecraftforge.fml.loading.FMLEnvironment; import net.minecraftforge.fml.util.thread.SidedThreadGroups; import net.minecraftforge.registries.ForgeRegistries; @@ -132,12 +133,21 @@ public static boolean isDevBuild() { public static void profile( String sectionName, Runnable runnable ) { ProfilerFiller profiler = getProfiler(); - profiler.push( sectionName ); - runnable.run(); - profiler.pop(); + if( profiler != null ) { + profiler.push( sectionName ); + runnable.run(); + profiler.pop(); + } else { + runnable.run(); + } } - public static ProfilerFiller getProfiler() { - return isServerSide() ? ServerLifecycleHooks.getCurrentServer().getProfiler() : Minecraft.getInstance().getProfiler(); + @Nullable + private static ProfilerFiller getProfiler() { + return DistExecutor.unsafeRunForDist( ()->()->{ + return Minecraft.getInstance() != null ? Minecraft.getInstance().getProfiler() : null; + }, ()->()->{ + return ServerLifecycleHooks.getCurrentServer() != null ? ServerLifecycleHooks.getCurrentServer().getProfiler() : null; + } ); } } diff --git a/src/main/java/com/mlib/gamemodifiers/contexts/OnFishingTimeSet.java b/src/main/java/com/mlib/gamemodifiers/contexts/OnFishingTimeSet.java new file mode 100644 index 00000000..a577ac34 --- /dev/null +++ b/src/main/java/com/mlib/gamemodifiers/contexts/OnFishingTimeSet.java @@ -0,0 +1,43 @@ +package com.mlib.gamemodifiers.contexts; + +import com.mlib.gamemodifiers.Condition; +import com.mlib.gamemodifiers.Context; +import com.mlib.gamemodifiers.Contexts; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.entity.projectile.FishingHook; + +import javax.annotation.Nullable; +import java.util.function.Consumer; + +public class OnFishingTimeSet { + public static Context< Data > listen( Consumer< Data > consumer ) { + return Contexts.get( Data.class ).add( consumer ); + } + + public static Data dispatch( FishingHook hook, int timeUntilLured ) { + return Contexts.get( Data.class ).dispatch( new Data( hook, timeUntilLured ) ); + } + + public static Condition< Data > hasPlayer() { + return new Condition<>( data->data.player != null ); + } + + public static class Data { + public FishingHook hook; + public final int original; + public int ticks; + @Nullable + public Player player; + + public Data( FishingHook hook, int original ) { + this.hook = hook; + this.original = original; + this.ticks = original; + this.player = hook.getPlayerOwner(); + } + + public int getTicks() { + return Math.max( this.ticks, 1 ); + } + } +} diff --git a/src/main/java/com/mlib/mixin/MixinFishingHook.java b/src/main/java/com/mlib/mixin/MixinFishingHook.java new file mode 100644 index 00000000..2ffc5b3a --- /dev/null +++ b/src/main/java/com/mlib/mixin/MixinFishingHook.java @@ -0,0 +1,23 @@ +package com.mlib.mixin; + +import com.mlib.ObfuscationGetter; +import com.mlib.gamemodifiers.contexts.OnFishingTimeSet; +import net.minecraft.world.entity.projectile.FishingHook; +import org.objectweb.asm.Opcodes; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Redirect; + +@Mixin( FishingHook.class ) +public abstract class MixinFishingHook { + private static final ObfuscationGetter.Field< FishingHook, Integer > TIME_UNTIL_LURED = new ObfuscationGetter.Field<>( FishingHook.class, "f_37090_" ); + + @Shadow( aliases = { "this$0" } ) + @Redirect( method = "catchingFish (Lnet/minecraft/core/BlockPos;)V", at = @At( value = "FIELD", target = "Lnet/minecraft/world/entity/projectile/FishingHook;timeUntilLured:I", opcode = Opcodes.PUTFIELD, ordinal = 3 ) ) + private void catchingFish( FishingHook hook, int timeUntilLured ) { + if( timeUntilLured > 0 ) { + TIME_UNTIL_LURED.set( hook, OnFishingTimeSet.dispatch( hook, timeUntilLured ).getTicks() ); + } + } +} diff --git a/src/main/resources/mixins.mlib.json b/src/main/resources/mixins.mlib.json index e68be45c..ee57192f 100644 --- a/src/main/resources/mixins.mlib.json +++ b/src/main/resources/mixins.mlib.json @@ -5,6 +5,7 @@ "mixins": [ "MixinArrow", "MixinDifficultyInstance", + "MixinFishingHook", "MixinItemStack", "MixinLivingEntity", "MixinLootTableSerializer",