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

[MCTCommon/Events] Improve event firing and listening #199

Merged
merged 3 commits into from
Feb 23, 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
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
Loading