Skip to content

Commit

Permalink
[MCTCommon/Events] Improve event firing and listening (#199)
Browse files Browse the repository at this point in the history
- New event listener registering
- New event firing
  • Loading branch information
ScribbleTAS authored Feb 23, 2024
2 parents f3b345e + 4df8d73 commit dc9334d
Show file tree
Hide file tree
Showing 27 changed files with 585 additions and 420 deletions.
137 changes: 17 additions & 120 deletions src/main/java/com/minecrafttas/mctcommon/events/EventClient.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.minecrafttas.mctcommon.events;

import com.minecrafttas.mctcommon.MCTCommon;
import com.minecrafttas.mctcommon.events.EventListenerRegistry.EventBase;
import com.minecrafttas.mctcommon.server.Client;
import com.mojang.authlib.GameProfile;
Expand All @@ -9,13 +8,19 @@
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.client.gui.GuiScreen;

/**
* Contains all events fired on the client side
*
* @author Scribble
*/
public interface EventClient {

/**
* Fired when a gui is opened (Minecraft#displayGuiScreen)
* @author Scribble
*
*/
@FunctionalInterface
public static interface EventOpenGui extends EventBase {

/**
Expand All @@ -24,143 +29,87 @@ public static interface EventOpenGui extends EventBase {
* @return
*/
public GuiScreen onOpenGui(GuiScreen gui);

public static GuiScreen fireOpenGuiEvent(GuiScreen gui) {
MCTCommon.LOGGER.trace(MCTCommon.Event, "Firing OpenGuiEvent");
for (EventBase eventListener : EventListenerRegistry.getEventListeners()) {
if(eventListener instanceof EventOpenGui) {
EventOpenGui event = (EventOpenGui) eventListener;
GuiScreen newGui = event.onOpenGui(gui);
if(newGui != gui) {
return newGui;
}
}
}
return gui;
}
}

/**
* Fired when the integrated server is launched
* @author Scribble
*
*/
@FunctionalInterface
public static interface EventLaunchIntegratedServer extends EventBase {

/**
* Fired when the integrated server is launched
*/
public void onLaunchIntegratedServer();

public static void fireOnLaunchIntegratedServer() {
MCTCommon.LOGGER.trace(MCTCommon.Event, "Firing LaunchIntegratedServer");
for (EventBase eventListener : EventListenerRegistry.getEventListeners()) {
if(eventListener instanceof EventLaunchIntegratedServer) {
EventLaunchIntegratedServer event = (EventLaunchIntegratedServer) eventListener;
event.onLaunchIntegratedServer();
}
}
}
}

/**
* Fired when the world is done loading, before the player joined the world
* @author Scribble
*
*/
@FunctionalInterface
public static interface EventDoneLoadingWorld extends EventBase {

/**
* Fired when the world is done loading, before the player joined the world
*/
public void onDoneLoadingWorld();

public static void fireOnDoneLoadingWorld() {
MCTCommon.LOGGER.trace(MCTCommon.Event, "Firing DoneLoadingWorld");
for (EventBase eventListener : EventListenerRegistry.getEventListeners()) {
if(eventListener instanceof EventDoneLoadingWorld) {
EventDoneLoadingWorld event = (EventDoneLoadingWorld) eventListener;
event.onDoneLoadingWorld();
}
}
}
}

/**
* Fired when the client ticks
* @author Scribble
*
*/
@FunctionalInterface
public static interface EventClientTick extends EventBase {

/**
* Fired when the client ticks
* @param mc The ticking Minecraft instance
*/
public void onClientTick(Minecraft mc);

public static void fireOnClientTick(Minecraft mc) {
for (EventBase eventListener : EventListenerRegistry.getEventListeners()) {
if(eventListener instanceof EventClientTick) {
EventClientTick event = (EventClientTick) eventListener;
event.onClientTick(mc);
}
}
}
}

/**
* Fires after the client is initialised
* @author Scribble
*
*/
@FunctionalInterface
public static interface EventClientInit extends EventBase {

/**
* Fires after the client is initialised
* @param mc The initialized Minecraft instance
*/
public void onClientInit(Minecraft mc);

public static void fireOnClientInit(Minecraft mc) {
MCTCommon.LOGGER.trace(MCTCommon.Event, "Firing ClientInit");
for (EventBase eventListener : EventListenerRegistry.getEventListeners()) {
if(eventListener instanceof EventClientInit) {
EventClientInit event = (EventClientInit) eventListener;
event.onClientInit(mc);
}
}
}
}

/**
* Fired when when the client runs a game loop, which is tick independent
* @author Scribble
*
*/
@FunctionalInterface
public static interface EventClientGameLoop extends EventBase {

/**
* Fired when when the client runs a game loop, which is tick independent
* @param mc The Minecraft instance that is looping
*/
public void onRunClientGameLoop(Minecraft mc);

public static void fireOnClientGameLoop(Minecraft mc) {
for (EventBase eventListener : EventListenerRegistry.getEventListeners()) {
if(eventListener instanceof EventClientGameLoop) {
EventClientGameLoop event = (EventClientGameLoop) eventListener;
event.onRunClientGameLoop(mc);
}
}
}
}

/**
* Fired when the camera is updated
* @author Scribble
*
*/
@FunctionalInterface
public static interface EventCamera extends EventBase {

/**
Expand All @@ -170,19 +119,6 @@ public static interface EventCamera extends EventBase {
*/
public CameraData onCameraEvent(CameraData dataIn);

public static CameraData fireCameraEvent(CameraData dataIn) {
for (EventBase eventListener : EventListenerRegistry.getEventListeners()) {
if(eventListener instanceof EventCamera) {
EventCamera event = (EventCamera) eventListener;
CameraData data = event.onCameraEvent(dataIn);
if(!data.equals(dataIn)) {
return data;
}
}
}
return dataIn;
}

public static class CameraData{
public float pitch;
public float yaw;
Expand Down Expand Up @@ -214,95 +150,56 @@ public boolean equals(Object obj) {
* @author Scribble
*
*/
@FunctionalInterface
public static interface EventPlayerLeaveClientSide extends EventBase {

/**
* Fired when a player leaves a server or a world
* @param player The player that leaves the server or the world
*/
public void onPlayerLeaveClientSide(EntityPlayerSP player);

public static void firePlayerLeaveClientSide(EntityPlayerSP player) {
MCTCommon.LOGGER.trace(MCTCommon.Event, "Firing PlayerLeaveClientSideEvent");
for (EventBase eventListener : EventListenerRegistry.getEventListeners()) {
if(eventListener instanceof EventPlayerLeaveClientSide) {
EventPlayerLeaveClientSide event = (EventPlayerLeaveClientSide) eventListener;
event.onPlayerLeaveClientSide(player);
}
}
}
}

/**
* Fired when a player joins a server or a world
* @author Scribble
*
*/
@FunctionalInterface
public static interface EventPlayerJoinedClientSide extends EventBase {

/**
* Fired when a player joins a server or a world
* @param player The player that joins the server or the world
*/
public void onPlayerJoinedClientSide(EntityPlayerSP player);

public static void firePlayerJoinedClientSide(EntityPlayerSP player) {
MCTCommon.LOGGER.trace(MCTCommon.Event, "Firing PlayerJoinedClientSide");
for (EventBase eventListener : EventListenerRegistry.getEventListeners()) {
if(eventListener instanceof EventPlayerJoinedClientSide) {
EventPlayerJoinedClientSide event = (EventPlayerJoinedClientSide) eventListener;
event.onPlayerJoinedClientSide(player);
}
}
}

}

/**
* Fired when a different player other than yourself joins a server or a world
* @author Scribble
*
*/
@FunctionalInterface
public static interface EventOtherPlayerJoinedClientSide extends EventBase {

/**
* Fired when a different player other than yourself joins a server or a world
* @param player The game profile of the player that joins the server or the world
* @param profile The game profile of the player that joins the server or the world
*/
public void onOtherPlayerJoinedClientSide(GameProfile profile);


public static void fireOtherPlayerJoinedClientSide(GameProfile profile) {
MCTCommon.LOGGER.trace(MCTCommon.Event, "Firing OtherPlayerJoinedClientSide");
for (EventBase eventListener : EventListenerRegistry.getEventListeners()) {
if(eventListener instanceof EventOtherPlayerJoinedClientSide) {
EventOtherPlayerJoinedClientSide event = (EventOtherPlayerJoinedClientSide) eventListener;
event.onOtherPlayerJoinedClientSide(profile);
}
}
}

}

/**
* Fired when the connection to the custom server was closed on the client side.
*/
@FunctionalInterface
public static interface EventDisconnectClient extends EventBase {

/**
* Fired when the connection to the custom server was closed on the client side.
* @param client The client that is disconnecting
*/
public void onDisconnectClient(Client client);

public static void fireDisconnectClient(Client client) {
MCTCommon.LOGGER.trace(MCTCommon.Event, "Firing EventDisconnectClient");
for (EventBase eventListener : EventListenerRegistry.getEventListeners()) {
if(eventListener instanceof EventDisconnectClient) {
EventDisconnectClient event = (EventDisconnectClient) eventListener;
event.onDisconnectClient(client);
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.minecrafttas.mctcommon.events;

public class EventException extends RuntimeException {

public EventException(String message, Class<? extends EventListenerRegistry.EventBase> eventClass) {
super(eventClass.getName() + ": " + message);
}

public EventException(String message, Class<? extends EventListenerRegistry.EventBase> eventClass, Throwable cause) {
super(eventClass.getName() + ": " + message, cause);
}

public EventException(Class<? extends EventListenerRegistry.EventBase> eventClass, Throwable cause) {
super(eventClass.getName(), cause);
}
}
Loading

0 comments on commit dc9334d

Please sign in to comment.