First Event!

This commit is contained in:
EOT3000 2019-07-22 10:20:49 -04:00
parent 58819ea9ce
commit f0551727ca
12 changed files with 232 additions and 14 deletions

View File

@ -0,0 +1,32 @@
package org.geysermc.api.events;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* The annotation to put on all methods that are events.
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface EventHandler {
/**
* @return the order to execute events.
* @see EventPriority
*/
EventPriority value() default EventPriority.NORMAL;
/**
* When an eventHandler should be run.
* The names mostly explain.
*/
enum EventPriority {
FIRST,
NORMAL,
LAST,
READ_ONLY;
}
}

View File

@ -0,0 +1,8 @@
package org.geysermc.api.events;
/**
* A marker class which says that a specific class uses events.
* @see EventHandler
*/
public interface Listener {
}

View File

@ -0,0 +1,29 @@
package org.geysermc.api.events.player;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import java.net.InetSocketAddress;
@Getter
@Setter
public class PingEvent {
public PingEvent(InetSocketAddress address) {
this.address = address;
}
private InetSocketAddress address;
private String edition;
private String motd;
private int protocolVersion;
private String version;
private int playerCount;
private int maximumPlayerCount;
private long serverId;
private String subMotd;
private String gameType;
private boolean nintendoLimited;
}

View File

@ -33,6 +33,8 @@ import lombok.Setter;
* The first init point is the constructor, followed by onLoad, and finally onEnable.
*/
public class Plugin {
protected String name;
protected String version;
@Getter
@Setter
@ -60,9 +62,18 @@ public class Plugin {
}
/**
* Called when th server is reloaded
* Called when the server is reloaded
*/
public void onReload() {
}
public final String getName() {
return name;
}
@Override
public final String toString() {
return getName();
}
}

View File

@ -25,6 +25,8 @@
package org.geysermc.api.plugin;
import org.geysermc.api.events.Listener;
import java.util.Set;
public interface PluginManager {
@ -63,4 +65,23 @@ public interface PluginManager {
* @return a set of the loaded plugins
*/
Set<Plugin> getPlugins();
/**
* @param name The name of the plugin you want to get.
* @return The plugin with the String name in the parameters.
*/
Plugin getPluginByName(String name);
/**
* Registers a listener to be run when an event is executed
* @param plugin the plugin registering the listener
* @param listener the listener which will contain the event methods
*/
void registerEventListener(Plugin plugin, Listener listener);
/**
* Run an event
* @param o the event object.
*/
void runEvent(Object o);
}

View File

@ -0,0 +1,9 @@
package org.geysermc.api.session;
public interface AuthData {
void getXUID();
void getUUID();
void getName();
}

View File

@ -30,6 +30,7 @@ import com.nukkitx.protocol.bedrock.BedrockPong;
import com.nukkitx.protocol.bedrock.BedrockServerEventHandler;
import com.nukkitx.protocol.bedrock.BedrockServerSession;
import com.nukkitx.protocol.bedrock.v361.Bedrock_v361;
import org.geysermc.api.events.player.PingEvent;
import org.geysermc.connector.GeyserConnector;
import org.geysermc.connector.configuration.GeyserConfiguration;
import org.geysermc.connector.console.GeyserLogger;
@ -54,15 +55,14 @@ public class ConnectorServerEventHandler implements BedrockServerEventHandler {
@Override
public BedrockPong onQuery(InetSocketAddress inetSocketAddress) {
PingEvent pong = new PingEvent(inetSocketAddress);
GeyserLogger.DEFAULT.debug(inetSocketAddress + " has pinged you!");
GeyserConfiguration config = connector.getConfig();
BedrockPong pong = new BedrockPong();
pong.setEdition("MCPE");
pong.setGameType("Default");
pong.setNintendoLimited(false);
pong.setProtocolVersion(GeyserConnector.BEDROCK_PACKET_CODEC.getProtocolVersion());
pong.setVersion("1.12.0");
pong.setIpv4Port(19132);
if (connector.getConfig().isPingPassthrough()) {
ServerStatusInfo serverInfo = connector.getPassthroughThread().getInfo();
@ -79,7 +79,22 @@ public class ConnectorServerEventHandler implements BedrockServerEventHandler {
pong.setMotd(config.getBedrock().getMotd1());
pong.setSubMotd(config.getBedrock().getMotd2());
}
return pong;
BedrockPong c = new BedrockPong();
c.setEdition(pong.getEdition());
c.setGameType(pong.getGameType());
c.setNintendoLimited(pong.isNintendoLimited());
c.setProtocolVersion(pong.getProtocolVersion());
c.setVersion(pong.getVersion());
c.setMotd(pong.getMotd());
c.setSubMotd(pong.getSubMotd());
c.setPlayerCount(pong.getPlayerCount());
c.setMaximumPlayerCount(pong.getMaximumPlayerCount());
return c;
}
@Override

View File

@ -38,9 +38,11 @@ import com.nukkitx.protocol.PlayerSession;
import com.nukkitx.protocol.bedrock.BedrockServerSession;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.geysermc.api.session.AuthData;
import org.geysermc.connector.GeyserConnector;
import org.geysermc.connector.network.remote.RemoteJavaServer;
import org.geysermc.connector.network.translators.Registry;
import sun.security.krb5.internal.AuthorizationData;
import java.util.UUID;
@ -128,10 +130,10 @@ public class GeyserSession implements PlayerSession {
@Getter
@AllArgsConstructor
public class AuthenticationData {
public class AuthenticationData implements AuthData {
private String name;
private UUID uuid;
private String xboxUUID;
private UUID UUID;
private String XUID;
}
}

View File

@ -32,6 +32,7 @@ import org.geysermc.api.Connector;
import org.geysermc.api.plugin.Plugin;
import java.io.File;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
@ -51,7 +52,7 @@ public class GeyserPluginLoader extends ClassLoader {
}
for (File f : dir.listFiles()) {
if (!f.getName().endsWith(".jar"))
if (!f.getName().toLowerCase().endsWith(".jar"))
continue;
try {
@ -83,6 +84,19 @@ public class GeyserPluginLoader extends ClassLoader {
PluginYML yml = mapper.readValue(is, PluginYML.class);
is.close();
Plugin plugin = (Plugin) Class.forName(yml.main, true, this).newInstance();
Class cl = Plugin.class;
Field name = cl.getDeclaredField("name");
name.setAccessible(true);
Field version = cl.getDeclaredField("version");
version.setAccessible(true);
name.set(plugin, yml.name);
version.set(plugin, yml.version);
connector.getLogger().info("Loading plugin " + yml.name + " version " + yml.version);
connector.getPluginManager().loadPlugin(plugin);
} catch (Exception e) {

View File

@ -26,19 +26,23 @@
package org.geysermc.connector.plugin;
import lombok.Getter;
import org.geysermc.api.events.EventHandler;
import org.geysermc.api.events.Listener;
import org.geysermc.api.plugin.Plugin;
import org.geysermc.api.plugin.PluginManager;
import java.util.HashSet;
import java.util.Set;
import java.awt.*;
import java.lang.reflect.Method;
import java.util.*;
import java.util.List;
public class GeyserPluginManager implements PluginManager {
private final List<PluginListener> EVENTS = new ArrayList<>();
@Getter
private GeyserPluginLoader loader;
@Getter
private Set<Plugin> plugins = new HashSet<Plugin>();
private Map<String, Plugin> plugins = new HashMap<>();
public GeyserPluginManager(GeyserPluginLoader loader) {
this.loader = loader;
@ -46,7 +50,7 @@ public class GeyserPluginManager implements PluginManager {
public void loadPlugin(Plugin plugin) {
loader.loadPlugin(plugin);
plugins.add(plugin);
plugins.put(plugin.getName(), plugin);
}
public void unloadPlugin(Plugin plugin) {
@ -62,6 +66,42 @@ public class GeyserPluginManager implements PluginManager {
}
public Set<Plugin> getPlugins() {
return plugins;
return new HashSet<>(plugins.values());
}
@Override
public void registerEventListener(Plugin p, Listener l) {
try {
Class<? extends Listener> clazz = l.getClass();
for(Method m : clazz.getMethods()) {
if(m.getAnnotation(EventHandler.class) != null) {
PluginListener listener = new PluginListener();
listener.plugin = p;
listener.listener = l;
listener.clazz = m.getParameterTypes()[0];
listener.priority = m.getAnnotation(EventHandler.class).value();
listener.run = m;
EVENTS.add(listener);
}
}
} catch (Exception e) {
//
}
}
@Override
public void runEvent(Object o) {
for(EventHandler.EventPriority p : EventHandler.EventPriority.values()) {
for (PluginListener listener : EVENTS) {
listener.runIfNeeded(p, o);
}
}
}
@Override
public Plugin getPluginByName(String name) {
return plugins.get(name);
}
}

View File

@ -0,0 +1,28 @@
package org.geysermc.connector.plugin;
import org.geysermc.api.events.EventHandler;
import org.geysermc.api.events.Listener;
import org.geysermc.api.plugin.Plugin;
import org.geysermc.connector.console.GeyserLogger;
import java.lang.reflect.Method;
public class PluginListener {
Method run;
Plugin plugin;
Listener listener;
Class clazz;
EventHandler.EventPriority priority;
void runIfNeeded(EventHandler.EventPriority p, Object o) {
if(p.equals(priority) && clazz.isInstance(o)) {
try {
run.invoke(listener, o);
} catch (ReflectiveOperationException ex) {
GeyserLogger.DEFAULT.severe("Exception while trying to run event! Contact the maintainer of " + plugin.getName());
ex.printStackTrace();
}
}
}
}

View File

@ -649,5 +649,14 @@ public class GeyserUtils {
}
public static <T> boolean instanceOf(Class<T> clazz, Object o) {
try {
T t = (T) o;
return true;
} catch (Exception e) {
return false;
}
}
}