-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented event system to simplify module classes and creation.
The system is not perfect, but it has worked well so far.
- Loading branch information
1 parent
80b3e50
commit eb7e926
Showing
72 changed files
with
801 additions
and
1,238 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package net.aoba.event; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import net.aoba.event.events.AbstractEvent; | ||
import net.aoba.event.listeners.AbstractListener; | ||
|
||
public class EventManager { | ||
|
||
private final HashMap<Class<AbstractListener>, ArrayList<AbstractListener>> listeners; | ||
|
||
public EventManager() { | ||
listeners = new HashMap<Class<AbstractListener>, ArrayList<AbstractListener>>(); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public <T extends AbstractListener> void AddListener(Class<T> object, AbstractListener listener) { | ||
try { | ||
ArrayList<AbstractListener> listOfListeners = listeners.get(object); | ||
if(listOfListeners == null) | ||
{ | ||
listOfListeners = new ArrayList<>(Arrays.asList(listener)); | ||
listeners.put((Class<AbstractListener>) object, listOfListeners); | ||
} | ||
listOfListeners.add(listener); | ||
}catch(Exception e) { | ||
System.out.println("Issue adding listener: " + object.getTypeName() + "..."); | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public <T extends AbstractListener> void RemoveListener(Class<T> object, AbstractListener listener) { | ||
try { | ||
ArrayList<AbstractListener> listOfListeners = listeners.get(object); | ||
if(listOfListeners != null) | ||
{ | ||
listOfListeners.remove(listener); | ||
} | ||
}catch(Exception e) { | ||
System.out.println("Issue removing listener: " + object.getTypeName() + "..."); | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public void Fire(AbstractEvent event) { | ||
ArrayList<? extends AbstractListener> listOfListeners = listeners.get(event.GetListenerClassType()); | ||
|
||
if(listOfListeners == null) { | ||
return; | ||
} | ||
|
||
event.Fire(listOfListeners); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package net.aoba.event.events; | ||
|
||
import java.util.ArrayList; | ||
|
||
import net.aoba.event.listeners.AbstractListener; | ||
|
||
public abstract class AbstractEvent { | ||
boolean isCancelled; | ||
|
||
public AbstractEvent() { | ||
isCancelled = false; | ||
} | ||
|
||
public boolean IsCancelled() { | ||
return isCancelled; | ||
} | ||
|
||
public abstract void Fire(ArrayList<? extends AbstractListener> listeners); | ||
public abstract <T extends AbstractListener> Class<T> GetListenerClassType(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package net.aoba.event.events; | ||
|
||
import java.util.ArrayList; | ||
|
||
import net.aoba.event.listeners.AbstractListener; | ||
import net.aoba.event.listeners.MouseScrollListener; | ||
|
||
public class MouseScrollEvent extends AbstractEvent{ | ||
private float horizontal; | ||
private float vertical; | ||
|
||
public MouseScrollEvent(float horizontal, float vertical) { | ||
super(); | ||
this.horizontal = horizontal; | ||
this.vertical = vertical; | ||
} | ||
|
||
public float GetVertical() { | ||
return vertical; | ||
} | ||
|
||
public float GetHorizontal() { | ||
return horizontal; | ||
} | ||
|
||
@Override | ||
public void Fire(ArrayList<? extends AbstractListener> listeners) { | ||
for(AbstractListener listener : listeners) { | ||
MouseScrollListener mouseScrollListener = (MouseScrollListener) listener; | ||
mouseScrollListener.OnMouseScroll(this); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public Class<MouseScrollListener> GetListenerClassType() { | ||
return MouseScrollListener.class; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/net/aoba/event/events/ReceivePacketEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package net.aoba.event.events; | ||
|
||
import java.util.ArrayList; | ||
import net.aoba.event.listeners.AbstractListener; | ||
import net.aoba.event.listeners.ReceivePacketListener; | ||
import net.minecraft.network.packet.Packet; | ||
|
||
public class ReceivePacketEvent extends AbstractEvent { | ||
|
||
private Packet<?> packet; | ||
|
||
public Packet<?> GetPacket(){ | ||
return packet; | ||
} | ||
|
||
public ReceivePacketEvent(Packet<?> packet) { | ||
this.packet = packet; | ||
} | ||
|
||
@Override | ||
public void Fire(ArrayList<? extends AbstractListener> listeners) { | ||
for(AbstractListener listener : listeners) { | ||
ReceivePacketListener readPacketListener = (ReceivePacketListener) listener; | ||
readPacketListener.OnReceivePacket(this); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public Class<ReceivePacketListener> GetListenerClassType() { | ||
return ReceivePacketListener.class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package net.aoba.event.events; | ||
|
||
import java.util.ArrayList; | ||
|
||
import net.aoba.event.listeners.AbstractListener; | ||
import net.aoba.event.listeners.RenderListener; | ||
import net.minecraft.client.util.math.MatrixStack; | ||
|
||
public class RenderEvent extends AbstractEvent { | ||
MatrixStack matrixStack; | ||
float partialTicks; | ||
|
||
public MatrixStack GetMatrixStack() { | ||
return matrixStack; | ||
} | ||
public float GetPartialTicks() { | ||
return partialTicks; | ||
} | ||
|
||
public RenderEvent(MatrixStack matrixStack, float partialTicks) { | ||
this.matrixStack = matrixStack; | ||
this.partialTicks = partialTicks; | ||
} | ||
|
||
@Override | ||
public void Fire(ArrayList<? extends AbstractListener> listeners) { | ||
for(AbstractListener listener : listeners) { | ||
RenderListener renderListener = (RenderListener) listener; | ||
renderListener.OnRender(this); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public Class<RenderListener> GetListenerClassType() { | ||
return RenderListener.class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package net.aoba.event.events; | ||
|
||
import java.util.ArrayList; | ||
|
||
import net.aoba.event.listeners.AbstractListener; | ||
import net.aoba.event.listeners.SendPacketListener; | ||
import net.minecraft.network.packet.Packet; | ||
|
||
public class SendPacketEvent extends AbstractEvent { | ||
|
||
private Packet<?> packet; | ||
|
||
public SendPacketEvent(Packet<?> packet) { | ||
this.packet = packet; | ||
} | ||
|
||
public Packet<?> GetPacket(){ | ||
return packet; | ||
} | ||
|
||
@Override | ||
public void Fire(ArrayList<? extends AbstractListener> listeners) { | ||
for(AbstractListener listener : listeners) { | ||
SendPacketListener sendPacketListener = (SendPacketListener) listener; | ||
sendPacketListener.OnSendPacket(this); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public Class<SendPacketListener> GetListenerClassType() { | ||
return SendPacketListener.class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package net.aoba.event.events; | ||
|
||
import java.util.ArrayList; | ||
|
||
import net.aoba.event.listeners.AbstractListener; | ||
import net.aoba.event.listeners.TickListener; | ||
|
||
public class TickEvent extends AbstractEvent { | ||
@Override | ||
public void Fire(ArrayList<? extends AbstractListener> listeners) { | ||
for(AbstractListener listener : listeners) { | ||
TickListener tickListener = (TickListener) listener; | ||
tickListener.OnUpdate(this); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public Class<TickListener> GetListenerClassType() { | ||
return TickListener.class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package net.aoba.event.listeners; | ||
|
||
public interface AbstractListener {} | ||
|
7 changes: 7 additions & 0 deletions
7
src/main/java/net/aoba/event/listeners/MouseScrollListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package net.aoba.event.listeners; | ||
|
||
import net.aoba.event.events.MouseScrollEvent; | ||
|
||
public interface MouseScrollListener extends AbstractListener { | ||
public abstract void OnMouseScroll(MouseScrollEvent event); | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/net/aoba/event/listeners/ReceivePacketListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package net.aoba.event.listeners; | ||
|
||
import net.aoba.event.events.ReceivePacketEvent; | ||
|
||
public interface ReceivePacketListener extends AbstractListener { | ||
public abstract void OnReceivePacket(ReceivePacketEvent readPacketEvent); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package net.aoba.event.listeners; | ||
|
||
import net.aoba.event.events.RenderEvent; | ||
|
||
public interface RenderListener extends AbstractListener { | ||
public abstract void OnRender(RenderEvent event); | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/net/aoba/event/listeners/SendPacketListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package net.aoba.event.listeners; | ||
|
||
import net.aoba.event.events.SendPacketEvent; | ||
|
||
public interface SendPacketListener extends AbstractListener { | ||
public abstract void OnSendPacket(SendPacketEvent event); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package net.aoba.event.listeners; | ||
|
||
import net.aoba.event.events.TickEvent; | ||
|
||
public interface TickListener extends AbstractListener { | ||
public abstract void OnUpdate(TickEvent event); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.