Created
March 27, 2020 19:02
-
-
Save WizardlyBump17/eaeafc83ff6d6c9655694b4f4e897c8b to your computer and use it in GitHub Desktop.
Sistema de eventos igual o do Bukkit
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
package event; | |
public abstract class Event { | |
public String getName() { | |
return getClass().getSimpleName(); | |
} | |
} |
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
package event; | |
import java.lang.reflect.InvocationTargetException; | |
import java.lang.reflect.Method; | |
import java.lang.reflect.Parameter; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class EventManager { | |
public List<Listener> listeners = new ArrayList<Listener>(); | |
public void registerEvent(Listener listener) { | |
for(Method method : listener.getClass().getDeclaredMethods()) { | |
if(method.isAnnotationPresent(EventHandler.class)) { | |
listeners.add(listener); | |
break; | |
} | |
} | |
} | |
public void callEvent(Event event) { | |
for(Listener listener : listeners) { | |
for(Method method : listener.getClass().getDeclaredMethods()) { | |
try { | |
if(method.getParameterCount() == 1) { | |
if(method.isAnnotationPresent(EventHandler.class)) { | |
Parameter param = method.getParameters()[0]; | |
if(param.getType().equals(event.getClass())) method.invoke(listener, event); | |
} | |
} | |
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} | |
} |
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
package event; | |
public interface Listener { | |
} |
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
import event.EventManager; | |
import event.MaisEvent; | |
import event.OutroEvent; | |
import event.Print; | |
import event.PrintEvent; | |
public class Main { | |
public static void main(String[] args) { | |
PrintEvent evt = new PrintEvent("teste"); | |
OutroEvent evt2 = new OutroEvent("testado"); | |
MaisEvent evt3 = new MaisEvent("mais"); | |
EventManager manager = new EventManager(); | |
manager.registerEvent(new Print()); | |
manager.callEvent(evt); | |
manager.callEvent(evt2); | |
manager.callEvent(evt3); | |
} | |
} |
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
package event; | |
public class MaisEvent extends Event { | |
private String toPrint; | |
public MaisEvent(String toPrint) { | |
this.toPrint = toPrint; | |
} | |
public String getToPrint() { | |
return toPrint; | |
} | |
} |
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
package event; | |
public class OutroEvent extends Event { | |
private String toPrint; | |
public OutroEvent(String toPrint) { | |
this.toPrint = toPrint; | |
} | |
public String getToPrint() { | |
return toPrint; | |
} | |
} |
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
package event; | |
public class Print implements Listener { | |
@EventHandler | |
public void onPrint(PrintEvent evt) { | |
System.out.println("PrintEvent foi chamado! " + evt.getToPrint()); | |
} | |
@EventHandler | |
public void onOutro(OutroEvent evt) { | |
System.out.println("OutroEvent foi chamado! " + evt.getToPrint()); | |
} | |
@EventHandler | |
public void onMais(MaisEvent evt) { | |
System.out.println("MaisEvent foi chamado! " + evt.getToPrint()); | |
} | |
} |
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
package event; | |
public class PrintEvent extends Event { | |
private String toPrint; | |
public PrintEvent(String toPrint) { | |
this.toPrint = toPrint; | |
} | |
public String getToPrint() { | |
return toPrint; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment