Skip to content

Commit

Permalink
no more nested render hud events
Browse files Browse the repository at this point in the history
  • Loading branch information
MehVahdJukaar committed Aug 3, 2024
1 parent 398dc1b commit 1c078bd
Show file tree
Hide file tree
Showing 21 changed files with 346 additions and 287 deletions.
2 changes: 1 addition & 1 deletion build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ mod_name=Zeta
mc_version=1.20.1
mapping_channel=official
mod_id=zeta
build_number=20-event23
build_number=20-event27
dir_output=../Build Output/Zeta/
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package org.violetmoon.zeta.event.play.loading;

import net.minecraftforge.eventbus.api.Event;
import org.violetmoon.zeta.config.ConfigFlagManager;
import org.violetmoon.zeta.event.bus.IZetaPlayEvent;

import java.util.Objects;

public interface ZGatherAdditionalFlags extends IZetaPlayEvent {

ConfigFlagManager flagManager();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import net.minecraftforge.event.TickEvent.ClientTickEvent;

public class ForgeZClientTick implements ZClientTick {
private final ClientTickEvent e;
public final ClientTickEvent e;

public ForgeZClientTick(ClientTickEvent e) {
this.e = e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import net.minecraftforge.client.event.RenderGuiOverlayEvent;
import net.minecraftforge.client.gui.overlay.ForgeGui;

public class ForgeZRenderGuiOverlay extends Event implements ZRenderGuiOverlay {
private final RenderGuiOverlayEvent e;
public class ForgeZRenderGuiOverlay implements ZRenderGuiOverlay {
public final RenderGuiOverlayEvent e;

public ForgeZRenderGuiOverlay(RenderGuiOverlayEvent e) {
this.e = e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import net.minecraftforge.client.event.RenderLivingEvent;

public abstract class ForgeZRenderLiving implements ZRenderLiving {
protected final RenderLivingEvent<?, ?> e;
public final RenderLivingEvent<?, ?> e;

public ForgeZRenderLiving(RenderLivingEvent<?, ?> e) {
this.e = e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
import net.minecraft.client.renderer.entity.player.PlayerRenderer;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.client.event.RenderPlayerEvent;
import org.violetmoon.zeta.event.bus.IZetaPlayEvent;

public abstract class ForgeZRenderPlayer implements ZRenderPlayer {
private final RenderPlayerEvent e;
public final RenderPlayerEvent e;

public ForgeZRenderPlayer(RenderPlayerEvent e) {
this.e = e;
Expand All @@ -35,12 +36,14 @@ public ForgeZRenderPlayer(RenderPlayerEvent e) {
public Player getEntity() {return e.getEntity();}

public static class Pre extends ForgeZRenderPlayer implements ZRenderPlayer.Pre {

public Pre(RenderPlayerEvent.Pre e) {
super(e);
}
}

public static class Post extends ForgeZRenderPlayer implements ZRenderPlayer.Post {
public static class Post extends ForgeZRenderPlayer implements ZRenderPlayer.Post, IZetaPlayEvent {

public Post(RenderPlayerEvent.Post e) {
super(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.util.Objects;

public class ForgeZRenderTick implements ZRenderTick {
private final TickEvent.RenderTickEvent e;
public final TickEvent.RenderTickEvent e;

public ForgeZRenderTick(TickEvent.RenderTickEvent e) {
this.e = e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import net.minecraftforge.client.event.ScreenEvent;

public class ForgeZScreen implements ZScreen {
private final ScreenEvent e;
public final ScreenEvent e;

public ForgeZScreen(ScreenEvent e) {
this.e = e;
Expand Down Expand Up @@ -58,7 +58,7 @@ public Post(ScreenEvent.Init.Post e) {
}

public static class Render extends ForgeZScreen implements ZScreen.Render {
private final ScreenEvent.Render e;
public final ScreenEvent.Render e;

public Render(ScreenEvent.Render e) {
super(e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.violetmoon.zetaimplforge.event;

import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import net.minecraftforge.client.event.RenderGuiOverlayEvent;
import net.minecraftforge.client.gui.overlay.VanillaGuiOverlay;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.eventbus.api.Event;
import net.minecraftforge.eventbus.api.EventPriority;
Expand Down Expand Up @@ -88,17 +90,41 @@ private <T extends Z> F createForgeEvent(@NotNull Z event, Function<T, ? extends
// Creates the Forge Event Consumer that will be registered with Forge Event Bus
private <F2 extends F, Z2 extends Z> Consumer<F2> createForgeConsumer(
MethodHandle zetaEventConsumer, Function<F2, Z2> forgeToZetaFunc, Class<?> zetaEventBaseClass, Class<?> forgeEventClass) {

//special cases fo forge events that are to be sub divided into phases

// for gui overlay event
VanillaGuiOverlay overlay = guessGuiOverlayFromClassName(zetaEventBaseClass, forgeEventClass.getSuperclass());
if (overlay != null) {
//here we know that phase must not be null
return event -> {
try {
RenderGuiOverlayEvent rge = (RenderGuiOverlayEvent) event;
if (rge.getOverlay() != overlay.type()) return;
zetaEventConsumer.invoke(forgeToZetaFunc.apply(event));
} catch (Throwable e) {
throw new RuntimeException(e);
}
};
}

//hack for tick events
Phase phase = Phase.guessFromClassName(zetaEventBaseClass, forgeEventClass);
if (phase != Phase.NONE) {
return event -> {
try {
//luckily this phase madness will go away with new neoforge
TickEvent te = (TickEvent) event;
if ((phase == Phase.START) ^ (te.phase == TickEvent.Phase.START)) return;
zetaEventConsumer.invoke(forgeToZetaFunc.apply(event));
} catch (Throwable e) {
throw new RuntimeException(e);
}
};
}

return event -> {
try {
//luckily this phase madness will go away with new neoforge
if (phase != Phase.NONE) {
TickEvent te = (TickEvent) event;
if (phase == Phase.START && te.phase != TickEvent.Phase.START) return;
if (phase == Phase.END && te.phase != TickEvent.Phase.END) return;
}
zetaEventConsumer.invoke(forgeToZetaFunc.apply(event));
} catch (Throwable e) {
throw new RuntimeException(e);
Expand All @@ -107,24 +133,47 @@ private <F2 extends F, Z2 extends Z> Consumer<F2> createForgeConsumer(
}

// for generic events
public <S extends Z, C extends Z> void registerSubClassWithGeneric(Class<C> baseZetaEventClass, Class<S> forgeZetaEventClass, Class<?> genericClass) {
registerSubClass(baseZetaEventClass, forgeZetaEventClass);
// auto register ones are deprecated. Register manually, its faster and requires no reflection hacks
@Deprecated
public <S extends Z, C extends Z> void registerWrapperWithGeneric(Class<C> baseZetaEventClass, Class<S> forgeZetaEventClass, Class<?> genericClass) {
registerWrapper(baseZetaEventClass, forgeZetaEventClass);
generics.put(baseZetaEventClass, genericClass);
}

public <ZF extends Z, ZB extends Z> void registerSubClass(Class<ZB> baseZetaEventClass, Class<ZF> forgeZetaEventClass) {
@Deprecated
public <ZF extends Z, ZB extends Z> void registerWrapper(Class<ZB> baseZetaEventClass, Class<ZF> forgeZetaEventClass) {

registerWrapper(baseZetaEventClass, forgeZetaEventClass, null);
}

@Deprecated
public <ZF extends Z, ZB extends Z> void registerWrapperWithGeneric(Class<ZB> baseZetaEventClass, Class<ZF> forgeZetaEventClass,
Function<? extends F, ZF> constructor, Class<?> genericClass) {
registerWrapper(baseZetaEventClass, forgeZetaEventClass, constructor);
generics.put(baseZetaEventClass, genericClass);
}

registerSubClass(baseZetaEventClass, forgeZetaEventClass, null);
//register known ones
public <ZF extends Z, ZB extends Z, F2 extends F> void registerWrapper(Class<ZB> baseZetaEventClass, Class<F2> forgeEventClass,
Function<F2, ZF> constructor, Function<ZF, ? extends F> unwrapper) {
zetaToForgeEventClassHack.put(baseZetaEventClass, forgeEventClass);
forgeToZetaMap.put(baseZetaEventClass, constructor);
zetaToForgeMap.put(baseZetaEventClass, unwrapper);
}

public <ZF extends Z, ZB extends Z> void registerSubClassWithGeneric(Class<ZB> baseZetaEventClass, Class<ZF> forgeZetaEventClass,
Function<? extends F, ZF> constructor, Class<?> genericClass) {
registerSubClass(baseZetaEventClass, forgeZetaEventClass, constructor);
public <ZF extends Z, ZB extends Z, F2 extends F> void registerWrapperWithGenerics(Class<ZB> baseZetaEventClass, Class<F2> forgeEventClass,
Function<F2, ZF> constructor, Function<ZF, ? extends F> unwrapper,
Class<?> genericClass) {
zetaToForgeEventClassHack.put(baseZetaEventClass, forgeEventClass);
forgeToZetaMap.put(baseZetaEventClass, constructor);
zetaToForgeMap.put(baseZetaEventClass, unwrapper);
generics.put(baseZetaEventClass, genericClass);
}

public <ZF extends Z, ZB extends Z> void registerSubClass(Class<ZB> baseZetaEventClass, Class<ZF> forgeZetaEventClass,
@Nullable Function<? extends F, ZF> constructor) {

@Deprecated
public <ZF extends Z, ZB extends Z> void registerWrapper(Class<ZB> baseZetaEventClass, Class<ZF> forgeZetaEventClass,
@Nullable Function<? extends F, ZF> constructor) {
Object old1;
Object old2 = null;
boolean isNoWrapper = false;
Expand Down Expand Up @@ -268,7 +317,7 @@ private <Z2 extends Z, F2 extends F> Function<Z2, F2> findWrappedForgeEvent(Clas
return null;
}

throw new RuntimeException("No wrapped forge Event found for Zeta event class " + zetaEventClass);
throw new RuntimeException("No e forge Event found for Zeta event class " + zetaEventClass);
}

private <Z2 extends Z, F2 extends F> Function<F2, Z2> findWrappedZetaEvent(Class<Z2> zetaEventClass, Class<? extends Z> baseZetaEventClass) {
Expand All @@ -285,7 +334,7 @@ private <Z2 extends Z, F2 extends F> Function<F2, Z2> findWrappedZetaEvent(Class
};
}

throw new RuntimeException("No wrapped Zeta Event found for Zeta event class " + zetaEventClass);
throw new RuntimeException("No e Zeta Event found for Zeta event class " + zetaEventClass);
}

public static Field findFieldInClassHierarchy(Class<?> clazz, Predicate<Field> predicate) {
Expand Down Expand Up @@ -345,12 +394,28 @@ private static Phase guessFromClassName(Class<?> zetaEventClass, Class<?> forgeC
}
}

private static final Map<Class<?>, VanillaGuiOverlay> GUI_OVERLAY_CACHE = new ConcurrentHashMap<>();

private static final Map<Class<?>, EventPriority> CACHE = new ConcurrentHashMap<>();
@Nullable
private static VanillaGuiOverlay guessGuiOverlayFromClassName(Class<?> zetaEventClass, Class<?> forgeClass) {
if (forgeClass.isAssignableFrom(RenderGuiOverlayEvent.class)) return null;
return GUI_OVERLAY_CACHE.computeIfAbsent(zetaEventClass, zec -> {
String simpleName = zec.getSimpleName();
for (VanillaGuiOverlay overlay : VanillaGuiOverlay.values()) {
if (simpleName.toUpperCase().equals(overlay.name().replace("_", ""))) {

Check warning on line 405 in src/main/java/org/violetmoon/zetaimplforge/event/ForgeEventsRemapper.java

View workflow job for this annotation

GitHub Actions / PMD

Error Prone UnnecessaryCaseChange

Using equalsIgnoreCase() is cleaner than using toUpperCase/toLowerCase().equals().
return overlay;
}
}
return null;
});
}


private static final Map<Class<?>, EventPriority> PRIORITY_CACHE = new ConcurrentHashMap<>();

private static EventPriority guessPriorityFromClassName(Class<?> zetaEventClass) {
return CACHE.computeIfAbsent(zetaEventClass, cl -> {
String simpleName = zetaEventClass.getSimpleName();
return PRIORITY_CACHE.computeIfAbsent(zetaEventClass, zec -> {
String simpleName = zec.getSimpleName();
for (EventPriority p : EventPriority.values()) {
String name = WordUtils.capitalizeFully(p.name().toLowerCase());
if (simpleName.endsWith(name)) {
Expand Down
Loading

0 comments on commit 1c078bd

Please sign in to comment.