Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event bus wip stuff #32

Merged
merged 22 commits into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
374 changes: 193 additions & 181 deletions src/main/java/org/violetmoon/zeta/Zeta.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
import org.violetmoon.zeta.advancement.modifier.WaxModifier;
import org.violetmoon.zeta.api.IAdvancementModifier;
import org.violetmoon.zeta.api.IAdvancementModifierDelegate;
import org.violetmoon.zeta.client.event.play.ZScreen;
import org.violetmoon.zeta.event.bus.LoadEvent;
import org.violetmoon.zeta.event.bus.PlayEvent;
import org.violetmoon.zeta.event.load.ZAddReloadListener;
import org.violetmoon.zeta.event.load.ZGatherAdvancementModifiers;

Expand All @@ -36,7 +38,10 @@
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.block.Block;
import org.violetmoon.zetaimplforge.api.ForgeZGatherAdvancementModifiers;
import org.violetmoon.zetaimplforge.client.event.play.ForgeZEarlyRender;

//TODO: make this zeta-independent
public class AdvancementModifierRegistry {
protected final Zeta zeta;
protected final Multimap<ResourceLocation, IAdvancementModifier> modifiers = HashMultimap.create();
Expand All @@ -59,11 +64,11 @@ public void addModifier(IAdvancementModifier mod) {
modifiers.put(r, mod);
}

@LoadEvent
@PlayEvent
public void addListeners(ZAddReloadListener event) {
if(!gatheredAddons) {
IAdvancementModifierDelegate delegateImpl = new DelegateImpl();
zeta.loadBus.fireExternal(new ZGatherAdvancementModifiers() {
zeta.loadBus.fire(new ZGatherAdvancementModifiers() {
@Override
public void register(IAdvancementModifier modifier) {
addModifier(modifier);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.violetmoon.zeta.client.event.load.ZAddItemColorHandlers;
import org.violetmoon.zeta.client.event.load.ZClientSetup;
import org.violetmoon.zeta.event.bus.LoadEvent;
import org.violetmoon.zeta.event.load.ZLoadComplete;
import org.violetmoon.zeta.registry.DyeablesRegistry;
import org.violetmoon.zeta.registry.RenderLayerRegistry;
import org.violetmoon.zeta.registry.ZetaRegistry;
Expand Down Expand Up @@ -56,32 +57,16 @@ public void registerItemColorHandlers(ZAddItemColorHandlers event) {
}
}

//these are in Post events to give other listeners a chance to populate .registerNamed
@LoadEvent
public void registerBlockColorsPost(ZAddBlockColorHandlers.Post event) {
registry.finalizeBlockColors((block, name) -> {
Function<Block, BlockColor> blockColorCreator = event.getNamedBlockColors().get(name);
if(blockColorCreator == null)
z.log.error("Unknown block color creator {} used on block {}", name, block);
else
event.register(blockColorCreator.apply(block), block);
});
public void registerRenderLayers(ZClientSetup event) {
z.renderLayerRegistry.finalize(this::doSetRenderLayer);
}

@LoadEvent
public void registerItemColorsPost(ZAddItemColorHandlers.Post event) {
registry.finalizeItemColors((item, name) -> {
Function<Item, ItemColor> itemColorCreator = event.getNamedItemColors().get(name);
if(itemColorCreator == null)
z.log.error("Unknown item color creator {} used on item {}", name, item);
else
event.register(itemColorCreator.apply(item), item);
});
}

//I hope this won't run on dedicated servers
@LoadEvent
public void registerRenderLayers(ZClientSetup event) {
z.renderLayerRegistry.finalize(this::doSetRenderLayer);
public void onLoadComplete(ZLoadComplete event){
z.registry.validateColorsProviders();
}

protected abstract void doSetRenderLayer(Block block, RenderLayerRegistry.Layer layer);
Expand Down
74 changes: 38 additions & 36 deletions src/main/java/org/violetmoon/zeta/client/ClientTicker.java
Original file line number Diff line number Diff line change
@@ -1,44 +1,46 @@
package org.violetmoon.zeta.client;

import net.minecraft.client.Minecraft;
import org.jetbrains.annotations.ApiStatus;
import org.violetmoon.zeta.client.event.play.ZClientTick;
import org.violetmoon.zeta.client.event.play.ZRenderGuiOverlay;
import org.violetmoon.zeta.client.event.play.ZRenderTick;
import org.violetmoon.zeta.event.bus.PlayEvent;
import org.violetmoon.zeta.event.bus.ZPhase;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screens.Screen;

//TODO: 1.21. replace with minecraft own ticker. Tbh this is legacy already and should be replaced with Minecraft.getPartialTicks()
@Deprecated
public final class ClientTicker {
public int ticksInGame = 0;
public float partialTicks = 0;
public float delta = 0;
public float total = 0;

@PlayEvent
public void onRenderTick(ZRenderTick event) {
if(event.isStartPhase())
partialTicks = event.getRenderTickTime();
else
endRenderTick();
}

@PlayEvent
public void onEndClientTick(ZClientTick event) {
if(event.getPhase() != ZPhase.END)
return;

Screen gui = Minecraft.getInstance().screen;
if(gui == null || !gui.isPauseScreen()) {
ticksInGame++;
partialTicks = 0;
}

endRenderTick();
}

public void endRenderTick() {
float oldTotal = total;
total = ticksInGame + partialTicks;
delta = total - oldTotal;
}

//no need to have more than 1 instance of this class. Ticks are always the same
public static final ClientTicker INSTANCE = new ClientTicker();

private ClientTicker() {
}

public float partialTicks = 0;
public float delta = 0;
public float total = 0;

public int ticksInGame = 0;

@ApiStatus.Internal
@PlayEvent
public void onRenderTick(ZRenderTick.Start event) {
partialTicks = Minecraft.getInstance().getPartialTick();
delta = Minecraft.getInstance().getDeltaFrameTime();
total = ticksInGame + partialTicks;
}

@ApiStatus.Internal
@PlayEvent
public void onEndClientTick(ZClientTick.End event) {
if (!Minecraft.getInstance().isPaused()) {
ticksInGame++;
}
}

@PlayEvent
public void pre(ZRenderGuiOverlay.ChatPanel.Pre event) {
int aa = 1;
}
}

This file was deleted.

18 changes: 1 addition & 17 deletions src/main/java/org/violetmoon/zeta/client/ZetaClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,18 @@
import net.minecraft.world.level.block.Block;

public abstract class ZetaClient implements IZeta {

public ZetaClient(Zeta zeta) {
this.zeta = zeta;
this.loadBus = zeta.loadBus;
this.playBus = zeta.playBus;

this.ticker = createClientTicker();
this.clientConfigManager = createClientConfigManager();
this.topLayerTooltipHandler = createTopLayerTooltipHandler();
this.clientRegistryExtension = createClientRegistryExtension();

loadBus.subscribe(clientRegistryExtension)
.subscribe(clientConfigManager);

playBus.subscribe(ticker)
.subscribe(topLayerTooltipHandler);

ZetaClientList.INSTANCE.register(this);
}

Expand All @@ -48,23 +44,13 @@ public ZetaClient(Zeta zeta) {

public ResourceLocation generalIcons = new ResourceLocation("zeta", "textures/gui/general_icons.png");

public final ClientTicker ticker;
public final ClientConfigManager clientConfigManager;
public final TopLayerTooltipHandler topLayerTooltipHandler;
public final ClientRegistryExtension clientRegistryExtension;

public ClientTicker createClientTicker() {
return new ClientTicker();
}

public ClientConfigManager createClientConfigManager() {
return new ClientConfigManager(this);
}

public TopLayerTooltipHandler createTopLayerTooltipHandler() {
return new TopLayerTooltipHandler();
}

//ummm ??
public void sendToServer(IZetaMessage msg) {
if(Minecraft.getInstance().getConnection() == null)
Expand All @@ -87,8 +73,6 @@ public void sendToServer(IZetaMessage msg) {
// The name is unwieldy on purpose, usages of this function should stick out.
public abstract @Nullable RegistryAccess hackilyGetCurrentClientLevelRegistryAccess();

public abstract void start();

@Override
public Zeta asZeta() {
return zeta;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Map;
import java.util.function.Function;

import org.violetmoon.zeta.Zeta;
import org.violetmoon.zeta.event.bus.IZetaLoadEvent;

import net.minecraft.client.color.block.BlockColor;
Expand All @@ -11,11 +12,6 @@

public interface ZAddBlockColorHandlers extends IZetaLoadEvent {
void register(BlockColor c, Block... blocks);
void registerNamed(Function<Block, BlockColor> c, String... names);
void registerNamed(Zeta myZeta, Function<Block, BlockColor> c, String... names);
BlockColors getBlockColors();

Post makePostEvent();
interface Post extends ZAddBlockColorHandlers {
Map<String, Function<Block, BlockColor>> getNamedBlockColors();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Map;
import java.util.function.Function;

import org.violetmoon.zeta.Zeta;
import org.violetmoon.zeta.event.bus.IZetaLoadEvent;

import net.minecraft.client.color.item.ItemColor;
Expand All @@ -12,11 +13,6 @@

public interface ZAddItemColorHandlers extends IZetaLoadEvent {
void register(ItemColor c, ItemLike... items);
void registerNamed(Function<Item, ItemColor> c, String... names);
void registerNamed(Zeta myZeta, Function<Item, ItemColor> c, String... names);
ItemColors getItemColors();

Post makePostEvent();
interface Post extends ZAddItemColorHandlers {
Map<String, Function<Item, ItemColor>> getNamedItemColors();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.violetmoon.zeta.client.event.load;

import java.util.function.Consumer;

import org.violetmoon.zeta.event.bus.IZetaLoadEvent;

import net.minecraft.server.packs.resources.PreparableReloadListener;

public interface ZRegisterClientReloadListener extends IZetaLoadEvent, Consumer<PreparableReloadListener> {
@Override
void accept(PreparableReloadListener bleh);
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package org.violetmoon.zeta.client.event.play;

import org.violetmoon.zeta.event.bus.IZetaPlayEvent;
import org.violetmoon.zeta.event.bus.ZPhase;

public interface ZClientTick extends IZetaPlayEvent {
ZPhase getPhase();

interface Start extends ZClientTick {
}

interface End extends ZClientTick {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

import org.violetmoon.zeta.event.bus.IZetaLoadEvent;

public class ZFirstClientTick implements IZetaLoadEvent { }
public interface ZFirstClientTick extends IZetaLoadEvent { }
Loading
Loading