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. * The first init point is the constructor, followed by onLoad, and finally onEnable.
*/ */
public class Plugin { public class Plugin {
protected String name;
protected String version;
@Getter @Getter
@Setter @Setter
@ -60,9 +62,18 @@ public class Plugin {
} }
/** /**
* Called when th server is reloaded * Called when the server is reloaded
*/ */
public void onReload() { 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; package org.geysermc.api.plugin;
import org.geysermc.api.events.Listener;
import java.util.Set; import java.util.Set;
public interface PluginManager { public interface PluginManager {
@ -63,4 +65,23 @@ public interface PluginManager {
* @return a set of the loaded plugins * @return a set of the loaded plugins
*/ */
Set<Plugin> getPlugins(); 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.BedrockServerEventHandler;
import com.nukkitx.protocol.bedrock.BedrockServerSession; import com.nukkitx.protocol.bedrock.BedrockServerSession;
import com.nukkitx.protocol.bedrock.v361.Bedrock_v361; import com.nukkitx.protocol.bedrock.v361.Bedrock_v361;
import org.geysermc.api.events.player.PingEvent;
import org.geysermc.connector.GeyserConnector; import org.geysermc.connector.GeyserConnector;
import org.geysermc.connector.configuration.GeyserConfiguration; import org.geysermc.connector.configuration.GeyserConfiguration;
import org.geysermc.connector.console.GeyserLogger; import org.geysermc.connector.console.GeyserLogger;
@ -54,15 +55,14 @@ public class ConnectorServerEventHandler implements BedrockServerEventHandler {
@Override @Override
public BedrockPong onQuery(InetSocketAddress inetSocketAddress) { public BedrockPong onQuery(InetSocketAddress inetSocketAddress) {
PingEvent pong = new PingEvent(inetSocketAddress);
GeyserLogger.DEFAULT.debug(inetSocketAddress + " has pinged you!"); GeyserLogger.DEFAULT.debug(inetSocketAddress + " has pinged you!");
GeyserConfiguration config = connector.getConfig(); GeyserConfiguration config = connector.getConfig();
BedrockPong pong = new BedrockPong();
pong.setEdition("MCPE"); pong.setEdition("MCPE");
pong.setGameType("Default"); pong.setGameType("Default");
pong.setNintendoLimited(false); pong.setNintendoLimited(false);
pong.setProtocolVersion(GeyserConnector.BEDROCK_PACKET_CODEC.getProtocolVersion()); pong.setProtocolVersion(GeyserConnector.BEDROCK_PACKET_CODEC.getProtocolVersion());
pong.setVersion("1.12.0"); pong.setVersion("1.12.0");
pong.setIpv4Port(19132);
if (connector.getConfig().isPingPassthrough()) { if (connector.getConfig().isPingPassthrough()) {
ServerStatusInfo serverInfo = connector.getPassthroughThread().getInfo(); ServerStatusInfo serverInfo = connector.getPassthroughThread().getInfo();
@ -79,7 +79,22 @@ public class ConnectorServerEventHandler implements BedrockServerEventHandler {
pong.setMotd(config.getBedrock().getMotd1()); pong.setMotd(config.getBedrock().getMotd1());
pong.setSubMotd(config.getBedrock().getMotd2()); 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 @Override

View file

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

View file

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

View file

@ -26,19 +26,23 @@
package org.geysermc.connector.plugin; package org.geysermc.connector.plugin;
import lombok.Getter; 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.Plugin;
import org.geysermc.api.plugin.PluginManager; import org.geysermc.api.plugin.PluginManager;
import java.util.HashSet; import java.awt.*;
import java.util.Set; import java.lang.reflect.Method;
import java.util.*;
import java.util.List;
public class GeyserPluginManager implements PluginManager { public class GeyserPluginManager implements PluginManager {
private final List<PluginListener> EVENTS = new ArrayList<>();
@Getter @Getter
private GeyserPluginLoader loader; private GeyserPluginLoader loader;
@Getter private Map<String, Plugin> plugins = new HashMap<>();
private Set<Plugin> plugins = new HashSet<Plugin>();
public GeyserPluginManager(GeyserPluginLoader loader) { public GeyserPluginManager(GeyserPluginLoader loader) {
this.loader = loader; this.loader = loader;
@ -46,7 +50,7 @@ public class GeyserPluginManager implements PluginManager {
public void loadPlugin(Plugin plugin) { public void loadPlugin(Plugin plugin) {
loader.loadPlugin(plugin); loader.loadPlugin(plugin);
plugins.add(plugin); plugins.put(plugin.getName(), plugin);
} }
public void unloadPlugin(Plugin plugin) { public void unloadPlugin(Plugin plugin) {
@ -62,6 +66,42 @@ public class GeyserPluginManager implements PluginManager {
} }
public Set<Plugin> getPlugins() { 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;
}
}
} }