Skip to content

Commit

Permalink
Clear event phase when reusing event object
Browse files Browse the repository at this point in the history
  • Loading branch information
embeddedt committed Jan 9, 2024
1 parent 3150ff9 commit c27bee1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.minecraft.world.biome.Biome;
import net.minecraftforge.event.terraingen.BiomeEvent;
import org.embeddedt.vintagefix.annotation.ClientOnlyMixin;
import org.embeddedt.vintagefix.util.EventUtils;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
Expand All @@ -18,6 +19,7 @@ public class MixinBiome {
@Unique
private void prepareEvent(BiomeEvent.BiomeColor event, int defaultColor) {
event.setNewColor(defaultColor);
EventUtils.clearPhase(event);
((AccessorBiomeColorEvent)event).setOriginalColor(defaultColor);
}

Expand Down
28 changes: 28 additions & 0 deletions src/main/java/org/embeddedt/vintagefix/util/EventUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.embeddedt.vintagefix.util;

import net.minecraftforge.fml.common.eventhandler.Event;
import net.minecraftforge.fml.common.eventhandler.EventPriority;
import net.minecraftforge.fml.relauncher.ReflectionHelper;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;

public class EventUtils {
private static final MethodHandle EVENT_PHASE;

static {
try {
EVENT_PHASE = MethodHandles.publicLookup().unreflectSetter(ReflectionHelper.findField(Event.class, "phase"));
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}

public static void clearPhase(Event e) {
try {
EVENT_PHASE.invokeExact(e, (EventPriority)null);
} catch(Throwable ex) {
throw new RuntimeException(ex);
}
}
}

0 comments on commit c27bee1

Please sign in to comment.