Last active
December 23, 2015 00:29
-
-
Save jurchiks/6553962 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import java.util.HashSet; | |
import java.util.Set; | |
import listeners.IBlockingAltListener; | |
import listeners.IBlockingListener; | |
import listeners.IItemReceiveListener; | |
import listeners.IPlayerDeathListener; | |
public enum EventDispatcher | |
{ | |
INSTANCE; | |
private final Set<IPlayerDeathListener> PLAYER_DEATH_LISTENERS = new HashSet<>(); | |
private final Set<IItemReceiveListener> ITEM_RECEIVE_LISTENERS = new HashSet<>(); | |
private final Set<IBlockingListener> BLOCKING_LISTENERS = new HashSet<>(); | |
private final Set<IBlockingAltListener> BLOCKING_ALT_LISTENERS = new HashSet<>(); | |
private EventDispatcher() | |
{ | |
} | |
public static void registerOnItemReceive(IItemReceiveListener listener) | |
{ | |
INSTANCE.ITEM_RECEIVE_LISTENERS.add(listener); | |
} | |
public void notifyItemReceive(Object player, Object item) | |
{ | |
for (IItemReceiveListener listener : ITEM_RECEIVE_LISTENERS) | |
{ | |
listener.onItemReceive(player, item); | |
} | |
} | |
public static void registerOnPlayerDeath(IPlayerDeathListener listener) | |
{ | |
INSTANCE.PLAYER_DEATH_LISTENERS.add(listener); | |
} | |
public void notifyPlayerDeath(Object victim, Object killer) | |
{ | |
for (IPlayerDeathListener listener : PLAYER_DEATH_LISTENERS) | |
{ | |
listener.onPlayerDeath(victim, killer); | |
} | |
} | |
public static void registerOnBlockingEvent(IBlockingListener listener) | |
{ | |
INSTANCE.BLOCKING_LISTENERS.add(listener); | |
} | |
public boolean notifyBlockingEvent(Object param) | |
{ | |
for (IBlockingListener listener : BLOCKING_LISTENERS) | |
{ | |
if (!listener.onBlockingEvent(param)) | |
{ | |
return false; | |
} | |
} | |
return true; | |
} | |
public static void registerOnBlockingAltEvent(IBlockingAltListener listener) | |
{ | |
INSTANCE.BLOCKING_ALT_LISTENERS.add(listener); | |
} | |
public boolean notifyBlockingAltEvent(Object param) | |
{ | |
boolean ret = true; | |
for (IBlockingAltListener listener : BLOCKING_ALT_LISTENERS) | |
{ | |
ret &= listener.onBlockingAltEvent(param); | |
} | |
return ret; | |
} | |
} |
This file contains hidden or 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
package listeners; | |
public interface IBlockingAltListener | |
{ | |
public boolean onBlockingAltEvent(Object param); | |
} |
This file contains hidden or 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
package listeners; | |
public interface IBlockingListener | |
{ | |
public boolean onBlockingEvent(Object param); | |
} |
This file contains hidden or 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
package listeners; | |
public interface IItemReceiveListener | |
{ | |
public void onItemReceive(Object player, Object item); | |
} |
This file contains hidden or 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
package listeners; | |
public interface IPlayerDeathListener | |
{ | |
public void onPlayerDeath(Object victim, Object killer); | |
} |
This file contains hidden or 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
public class Main | |
{ | |
public static void main(String[] args) | |
{ | |
// just here to start things going, normally somewhere well hidden | |
new Test(); | |
try | |
{ | |
Thread.sleep(1000); | |
} | |
catch (InterruptedException e) | |
{ | |
} | |
System.out.println("notifying player death"); | |
EventDispatcher.INSTANCE.notifyPlayerDeath("omg", "wtf"); | |
try | |
{ | |
Thread.sleep(1000); | |
} | |
catch (InterruptedException e) | |
{ | |
} | |
System.out.println("notifying item receive"); | |
EventDispatcher.INSTANCE.notifyItemReceive("omg", "12345"); | |
try | |
{ | |
Thread.sleep(1000); | |
} | |
catch (InterruptedException e) | |
{ | |
} | |
System.out.println("notifying some code-blocking event"); | |
if (!EventDispatcher.INSTANCE.notifyBlockingEvent("dafuq?")) | |
{ | |
System.out.println("one of the events returned false, it's very likely some listeners were not notified"); | |
} | |
try | |
{ | |
Thread.sleep(1000); | |
} | |
catch (InterruptedException e) | |
{ | |
} | |
System.out.println("notifying some alt-code-blocking event"); | |
if (!EventDispatcher.INSTANCE.notifyBlockingAltEvent("whatever")) | |
{ | |
System.out.println("all blocking alt events were notified, but one of them returned false"); | |
} | |
} | |
} |
This file contains hidden or 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
import listeners.IBlockingAltListener; | |
import listeners.IBlockingListener; | |
import listeners.IItemReceiveListener; | |
import listeners.IPlayerDeathListener; | |
public class Test implements IItemReceiveListener, IPlayerDeathListener, | |
IBlockingListener, IBlockingAltListener | |
{ | |
public Test() | |
{ | |
EventDispatcher.registerOnItemReceive(this); | |
EventDispatcher.registerOnPlayerDeath(this); | |
EventDispatcher.registerOnBlockingEvent(this); | |
EventDispatcher.registerOnBlockingAltEvent(this); | |
} | |
@Override | |
public void onPlayerDeath(Object victim, Object killer) | |
{ | |
System.out.println(victim + " was killed by " + killer); | |
} | |
@Override | |
public void onItemReceive(Object player, Object item) | |
{ | |
System.out.println(player + " received item " + item); | |
} | |
@Override | |
public boolean onBlockingEvent(Object param) | |
{ | |
System.out.println("Something blocking: " + param); | |
// block further execution of other listeners | |
return false; | |
} | |
@Override | |
public boolean onBlockingAltEvent(Object param) | |
{ | |
System.out.println("Something not quite blocking: " + param); | |
// block only code after all listeners have been notified | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment