From 727fa8a67d86523fa0465ca65e254980f0a472d1 Mon Sep 17 00:00:00 2001 From: KingRainbow44 Date: Tue, 26 Apr 2022 13:52:10 -0400 Subject: [PATCH 01/21] Overhaul event handling (designed by WetABQ#3417) --- .../emu/grasscutter/plugin/PluginManager.java | 44 +++++----- .../server/event/EventHandler.java | 82 +++++++++++++++++-- .../server/event/HandlerPriority.java | 18 ++++ .../grasscutter/server/event/Listener.java | 7 -- 4 files changed, 115 insertions(+), 36 deletions(-) create mode 100644 src/main/java/emu/grasscutter/server/event/HandlerPriority.java delete mode 100644 src/main/java/emu/grasscutter/server/event/Listener.java diff --git a/src/main/java/emu/grasscutter/plugin/PluginManager.java b/src/main/java/emu/grasscutter/plugin/PluginManager.java index 2c6b9f23d..f63e2381d 100644 --- a/src/main/java/emu/grasscutter/plugin/PluginManager.java +++ b/src/main/java/emu/grasscutter/plugin/PluginManager.java @@ -3,9 +3,8 @@ package emu.grasscutter.plugin; import emu.grasscutter.Grasscutter; import emu.grasscutter.server.event.Event; import emu.grasscutter.server.event.EventHandler; -import emu.grasscutter.server.event.Listener; +import emu.grasscutter.server.event.HandlerPriority; import emu.grasscutter.utils.Utils; -import org.reflections.Reflections; import java.io.File; import java.io.InputStreamReader; @@ -15,13 +14,14 @@ import java.net.URLClassLoader; import java.util.*; import java.util.jar.JarEntry; import java.util.jar.JarFile; +import java.util.stream.Stream; /** * Manages the server's plugins & the event system. */ public final class PluginManager { private final Map plugins = new HashMap<>(); - private final Map> listeners = new HashMap<>(); + private final List> listeners = new LinkedList<>(); public PluginManager() { this.loadPlugins(); // Load all plugins from the plugins directory. @@ -68,7 +68,7 @@ public final class PluginManager { JarEntry entry = entries.nextElement(); if(entry.isDirectory() || !entry.getName().endsWith(".class")) continue; String className = entry.getName().replace(".class", "").replace("/", "."); - Class clazz = loader.loadClass(className); + loader.loadClass(className); } Class pluginClass = loader.loadClass(pluginConfig.mainClass); @@ -129,11 +129,10 @@ public final class PluginManager { /** * Registers a plugin's event listener. - * @param plugin The plugin instance. * @param listener The event listener. */ - public void registerListener(Plugin plugin, Listener listener) { - this.listeners.computeIfAbsent(plugin, k -> new ArrayList<>()).add(listener); + public void registerListener(EventHandler listener) { + this.listeners.add(listener); } /** @@ -141,23 +140,24 @@ public final class PluginManager { * @param event The event to invoke. */ public void invokeEvent(Event event) { - this.listeners.values().stream() - .flatMap(Collection::stream) - .forEach(listener -> this.invokeOnListener(listener, event)); + Stream> handlers = this.listeners.stream() + .filter(handler -> event.getClass().isInstance(event)); + handlers.filter(handler -> handler.getPriority() == HandlerPriority.HIGH) + .toList().forEach(handler -> this.invokeHandler(event, handler)); + handlers.filter(handler -> handler.getPriority() == HandlerPriority.NORMAL) + .toList().forEach(handler -> this.invokeHandler(event, handler)); + handlers.filter(handler -> handler.getPriority() == HandlerPriority.LOW) + .toList().forEach(handler -> this.invokeHandler(event, handler)); } - + /** - * Attempts to invoke the event on the provided listener. + * Performs logic checks then invokes the provided event handler. + * @param event The event passed through to the handler. + * @param handler The handler to invoke. */ - private void invokeOnListener(Listener listener, Event event) { - try { - Class listenerClass = listener.getClass(); - Method[] methods = listenerClass.getMethods(); - for (Method method : methods) { - if(!method.isAnnotationPresent(EventHandler.class)) return; - if(!method.getParameterTypes()[0].isAssignableFrom(event.getClass())) return; - method.invoke(listener, event); - } - } catch (Exception ignored) { } + private void invokeHandler(Event event, EventHandler handler) { + if(!event.isCanceled() || + (event.isCanceled() && handler.ignoresCanceled()) + ) handler.getCallback().accept(event); } } \ No newline at end of file diff --git a/src/main/java/emu/grasscutter/server/event/EventHandler.java b/src/main/java/emu/grasscutter/server/event/EventHandler.java index d924933f2..ec0eaffe5 100644 --- a/src/main/java/emu/grasscutter/server/event/EventHandler.java +++ b/src/main/java/emu/grasscutter/server/event/EventHandler.java @@ -1,11 +1,79 @@ package emu.grasscutter.server.event; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; +import emu.grasscutter.Grasscutter; -/** - * Declares a class as an event listener/handler. - */ -@Retention(RetentionPolicy.RUNTIME) -public @interface EventHandler { +import java.util.function.Consumer; + +public final class EventHandler { + private E event; + + private Consumer listener; + private HandlerPriority priority; + private boolean handleCanceled; + + /** + * Gets which event this handler is handling. + * @return An event class. + */ + public Event handles() { + return this.event; + } + + /** + * Returns the callback for the handler. + * @return A consumer callback. + */ + public Consumer getCallback() { + return this.listener; + } + + /** + * Returns the handler's priority. + * @return The priority of the handler. + */ + public HandlerPriority getPriority() { + return this.priority; + } + + /** + * Returns if the handler will ignore cancelled events. + * @return The ignore cancelled state. + */ + public boolean ignoresCanceled() { + return this.handleCanceled; + } + + /** + * Sets the callback method for when the event is invoked. + * @param listener An event handler method. + * @return Method chaining. + */ + public EventHandler listener(Consumer listener) { + this.listener = listener; return this; + } + + /** + * Changes the handler's priority in handling events. + * @param priority The priority of the handler. + * @return Method chaining. + */ + public EventHandler priority(HandlerPriority priority) { + this.priority = priority; return this; + } + + /** + * Sets if the handler will ignore cancelled events. + * @param ignore If the handler should ignore cancelled events. + * @return Method chaining. + */ + public EventHandler ignore(boolean ignore) { + this.handleCanceled = ignore; return this; + } + + /** + * Registers the handler into the PluginManager. + */ + public void register() { + Grasscutter.getPluginManager().registerListener(this); + } } \ No newline at end of file diff --git a/src/main/java/emu/grasscutter/server/event/HandlerPriority.java b/src/main/java/emu/grasscutter/server/event/HandlerPriority.java new file mode 100644 index 000000000..18891879f --- /dev/null +++ b/src/main/java/emu/grasscutter/server/event/HandlerPriority.java @@ -0,0 +1,18 @@ +package emu.grasscutter.server.event; + +public enum HandlerPriority { + /** + * The handler will be called before every other handler. + */ + HIGH, + + /** + * The handler will be called the same time as other handlers. + */ + NORMAL, + + /** + * The handler will be called after every other handler. + */ + LOW +} diff --git a/src/main/java/emu/grasscutter/server/event/Listener.java b/src/main/java/emu/grasscutter/server/event/Listener.java deleted file mode 100644 index 2949cfe4a..000000000 --- a/src/main/java/emu/grasscutter/server/event/Listener.java +++ /dev/null @@ -1,7 +0,0 @@ -package emu.grasscutter.server.event; - -/** - * Implementing this interface declares a class as an event listener. - */ -public interface Listener { -} From 26db170a1eca88f1760b7eab61a507ac003ba1ba Mon Sep 17 00:00:00 2001 From: KingRainbow44 Date: Tue, 26 Apr 2022 14:08:48 -0400 Subject: [PATCH 02/21] Fix Javadoc & Update version --- build.gradle | 2 +- .../java/emu/grasscutter/plugin/PluginManager.java | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/build.gradle b/build.gradle index 1be86626c..e5bab797d 100644 --- a/build.gradle +++ b/build.gradle @@ -18,7 +18,7 @@ plugins { } group = 'tech.xigam' -version = '1.0.0-dev' +version = '1.0.1-dev' sourceCompatibility = 17 targetCompatibility = 17 diff --git a/src/main/java/emu/grasscutter/plugin/PluginManager.java b/src/main/java/emu/grasscutter/plugin/PluginManager.java index f63e2381d..82d71ca6c 100644 --- a/src/main/java/emu/grasscutter/plugin/PluginManager.java +++ b/src/main/java/emu/grasscutter/plugin/PluginManager.java @@ -17,11 +17,11 @@ import java.util.jar.JarFile; import java.util.stream.Stream; /** - * Manages the server's plugins & the event system. + * Manages the server's plugins and the event system. */ public final class PluginManager { private final Map plugins = new HashMap<>(); - private final List> listeners = new LinkedList<>(); + private final List listeners = new LinkedList<>(); public PluginManager() { this.loadPlugins(); // Load all plugins from the plugins directory. @@ -131,7 +131,7 @@ public final class PluginManager { * Registers a plugin's event listener. * @param listener The event listener. */ - public void registerListener(EventHandler listener) { + public void registerListener(EventHandler listener) { this.listeners.add(listener); } @@ -140,8 +140,8 @@ public final class PluginManager { * @param event The event to invoke. */ public void invokeEvent(Event event) { - Stream> handlers = this.listeners.stream() - .filter(handler -> event.getClass().isInstance(event)); + Stream handlers = this.listeners.stream() + .filter(handler -> handler.handles().isInstance(event)); handlers.filter(handler -> handler.getPriority() == HandlerPriority.HIGH) .toList().forEach(handler -> this.invokeHandler(event, handler)); handlers.filter(handler -> handler.getPriority() == HandlerPriority.NORMAL) @@ -155,7 +155,7 @@ public final class PluginManager { * @param event The event passed through to the handler. * @param handler The handler to invoke. */ - private void invokeHandler(Event event, EventHandler handler) { + private void invokeHandler(Event event, EventHandler handler) { if(!event.isCanceled() || (event.isCanceled() && handler.ignoresCanceled()) ) handler.getCallback().accept(event); From 60c93932ad16f6d162ce5984a84dda955cfd1353 Mon Sep 17 00:00:00 2001 From: KingRainbow44 Date: Tue, 26 Apr 2022 14:09:00 -0400 Subject: [PATCH 03/21] Fix handlers --- .../grasscutter/server/event/EventHandler.java | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/main/java/emu/grasscutter/server/event/EventHandler.java b/src/main/java/emu/grasscutter/server/event/EventHandler.java index ec0eaffe5..4522cc2c6 100644 --- a/src/main/java/emu/grasscutter/server/event/EventHandler.java +++ b/src/main/java/emu/grasscutter/server/event/EventHandler.java @@ -4,8 +4,12 @@ import emu.grasscutter.Grasscutter; import java.util.function.Consumer; -public final class EventHandler { - private E event; +public final class EventHandler { + private final Class event; + + public EventHandler(Class event) { + this.event = event; + } private Consumer listener; private HandlerPriority priority; @@ -15,7 +19,7 @@ public final class EventHandler { * Gets which event this handler is handling. * @return An event class. */ - public Event handles() { + public Class handles() { return this.event; } @@ -48,7 +52,7 @@ public final class EventHandler { * @param listener An event handler method. * @return Method chaining. */ - public EventHandler listener(Consumer listener) { + public EventHandler listener(Consumer listener) { this.listener = listener; return this; } @@ -57,7 +61,7 @@ public final class EventHandler { * @param priority The priority of the handler. * @return Method chaining. */ - public EventHandler priority(HandlerPriority priority) { + public EventHandler priority(HandlerPriority priority) { this.priority = priority; return this; } @@ -66,7 +70,7 @@ public final class EventHandler { * @param ignore If the handler should ignore cancelled events. * @return Method chaining. */ - public EventHandler ignore(boolean ignore) { + public EventHandler ignore(boolean ignore) { this.handleCanceled = ignore; return this; } From 9c28ceb8bf10de4f1feb5c623a19cc6a96dc9dbc Mon Sep 17 00:00:00 2001 From: KingRainbow44 Date: Tue, 26 Apr 2022 19:57:32 -0400 Subject: [PATCH 04/21] Fix handler creation --- .../server/event/EventHandler.java | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/main/java/emu/grasscutter/server/event/EventHandler.java b/src/main/java/emu/grasscutter/server/event/EventHandler.java index 4522cc2c6..f24bc2dee 100644 --- a/src/main/java/emu/grasscutter/server/event/EventHandler.java +++ b/src/main/java/emu/grasscutter/server/event/EventHandler.java @@ -5,9 +5,22 @@ import emu.grasscutter.Grasscutter; import java.util.function.Consumer; public final class EventHandler { - private final Class event; + private final Class event; + + /** + * Creates an instance of {@link EventHandler} for the specified event. + * @param event The event to handle. + * @return An instance of {@link EventHandler}. + */ + public static EventHandler forEvent(Class event) { + return new EventHandler(event); + } - public EventHandler(Class event) { + /** + * @deprecated Will be replaced with a private constructor instead. Use {@link #forEvent(Class)} instead. + */ + @Deprecated(forRemoval = true, since = "1.0.1") + public EventHandler(Class event) { this.event = event; } @@ -19,7 +32,7 @@ public final class EventHandler { * Gets which event this handler is handling. * @return An event class. */ - public Class handles() { + public Class handles() { return this.event; } From 7f5cd1065d2c880f87c8dbf10dd95e3ea01471d1 Mon Sep 17 00:00:00 2001 From: KingRainbow44 Date: Tue, 26 Apr 2022 19:57:44 -0400 Subject: [PATCH 05/21] Fix handler invocation --- .../emu/grasscutter/plugin/PluginManager.java | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/main/java/emu/grasscutter/plugin/PluginManager.java b/src/main/java/emu/grasscutter/plugin/PluginManager.java index 82d71ca6c..b82cf6e90 100644 --- a/src/main/java/emu/grasscutter/plugin/PluginManager.java +++ b/src/main/java/emu/grasscutter/plugin/PluginManager.java @@ -140,13 +140,19 @@ public final class PluginManager { * @param event The event to invoke. */ public void invokeEvent(Event event) { - Stream handlers = this.listeners.stream() - .filter(handler -> handler.handles().isInstance(event)); - handlers.filter(handler -> handler.getPriority() == HandlerPriority.HIGH) - .toList().forEach(handler -> this.invokeHandler(event, handler)); - handlers.filter(handler -> handler.getPriority() == HandlerPriority.NORMAL) - .toList().forEach(handler -> this.invokeHandler(event, handler)); - handlers.filter(handler -> handler.getPriority() == HandlerPriority.LOW) + EnumSet.allOf(HandlerPriority.class) + .forEach(priority -> this.checkAndFilter(event, priority)); + } + + /** + * Check an event to handlers for the priority. + * @param event The event being called. + * @param priority The priority to call for. + */ + private void checkAndFilter(Event event, HandlerPriority priority) { + this.listeners.stream() + .filter(handler -> handler.handles().isInstance(event)) + .filter(handler -> handler.getPriority() == priority) .toList().forEach(handler -> this.invokeHandler(event, handler)); } From f1623e9eab4602dd8f4efef008592c675cd61d63 Mon Sep 17 00:00:00 2001 From: KingRainbow44 Date: Tue, 26 Apr 2022 20:28:13 -0400 Subject: [PATCH 06/21] Use a custom consumer instead of the built-in one --- .../emu/grasscutter/plugin/PluginManager.java | 4 ++-- .../grasscutter/server/event/EventHandler.java | 15 +++++---------- .../java/emu/grasscutter/utils/EventConsumer.java | 7 +++++++ 3 files changed, 14 insertions(+), 12 deletions(-) create mode 100644 src/main/java/emu/grasscutter/utils/EventConsumer.java diff --git a/src/main/java/emu/grasscutter/plugin/PluginManager.java b/src/main/java/emu/grasscutter/plugin/PluginManager.java index b82cf6e90..53ac467f3 100644 --- a/src/main/java/emu/grasscutter/plugin/PluginManager.java +++ b/src/main/java/emu/grasscutter/plugin/PluginManager.java @@ -4,6 +4,7 @@ import emu.grasscutter.Grasscutter; import emu.grasscutter.server.event.Event; import emu.grasscutter.server.event.EventHandler; import emu.grasscutter.server.event.HandlerPriority; +import emu.grasscutter.utils.EventConsumer; import emu.grasscutter.utils.Utils; import java.io.File; @@ -14,7 +15,6 @@ import java.net.URLClassLoader; import java.util.*; import java.util.jar.JarEntry; import java.util.jar.JarFile; -import java.util.stream.Stream; /** * Manages the server's plugins and the event system. @@ -164,6 +164,6 @@ public final class PluginManager { private void invokeHandler(Event event, EventHandler handler) { if(!event.isCanceled() || (event.isCanceled() && handler.ignoresCanceled()) - ) handler.getCallback().accept(event); + ) handler.getCallback().consume(event); } } \ No newline at end of file diff --git a/src/main/java/emu/grasscutter/server/event/EventHandler.java b/src/main/java/emu/grasscutter/server/event/EventHandler.java index f24bc2dee..14da6f6a1 100644 --- a/src/main/java/emu/grasscutter/server/event/EventHandler.java +++ b/src/main/java/emu/grasscutter/server/event/EventHandler.java @@ -1,8 +1,7 @@ package emu.grasscutter.server.event; import emu.grasscutter.Grasscutter; - -import java.util.function.Consumer; +import emu.grasscutter.utils.EventConsumer; public final class EventHandler { private final Class event; @@ -16,15 +15,11 @@ public final class EventHandler { return new EventHandler(event); } - /** - * @deprecated Will be replaced with a private constructor instead. Use {@link #forEvent(Class)} instead. - */ - @Deprecated(forRemoval = true, since = "1.0.1") - public EventHandler(Class event) { + private EventHandler(Class event) { this.event = event; } - private Consumer listener; + private EventConsumer listener; private HandlerPriority priority; private boolean handleCanceled; @@ -40,7 +35,7 @@ public final class EventHandler { * Returns the callback for the handler. * @return A consumer callback. */ - public Consumer getCallback() { + public EventConsumer getCallback() { return this.listener; } @@ -65,7 +60,7 @@ public final class EventHandler { * @param listener An event handler method. * @return Method chaining. */ - public EventHandler listener(Consumer listener) { + public EventHandler listener(EventConsumer listener) { this.listener = listener; return this; } diff --git a/src/main/java/emu/grasscutter/utils/EventConsumer.java b/src/main/java/emu/grasscutter/utils/EventConsumer.java new file mode 100644 index 000000000..ae8bb5a9b --- /dev/null +++ b/src/main/java/emu/grasscutter/utils/EventConsumer.java @@ -0,0 +1,7 @@ +package emu.grasscutter.utils; + +import emu.grasscutter.server.event.Event; + +public interface EventConsumer { + void consume(T event); +} From 7ac31d999144d00ff1fff50cefcca21812625d15 Mon Sep 17 00:00:00 2001 From: KingRainbow44 Date: Tue, 26 Apr 2022 20:47:28 -0400 Subject: [PATCH 07/21] Add dispatch to server hook & create instance --- src/main/java/emu/grasscutter/Grasscutter.java | 4 ++++ .../emu/grasscutter/plugin/api/ServerHook.java | 14 +++++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/main/java/emu/grasscutter/Grasscutter.java b/src/main/java/emu/grasscutter/Grasscutter.java index dcd6a3b6f..1cc133580 100644 --- a/src/main/java/emu/grasscutter/Grasscutter.java +++ b/src/main/java/emu/grasscutter/Grasscutter.java @@ -9,6 +9,7 @@ import java.net.InetSocketAddress; import emu.grasscutter.command.CommandMap; import emu.grasscutter.plugin.PluginManager; +import emu.grasscutter.plugin.api.ServerHook; import emu.grasscutter.utils.Utils; import org.reflections.Reflections; import org.slf4j.LoggerFactory; @@ -77,6 +78,9 @@ public final class Grasscutter { dispatchServer = new DispatchServer(); gameServer = new GameServer(new InetSocketAddress(getConfig().getGameServerOptions().Ip, getConfig().getGameServerOptions().Port)); + // Create server hook instance. + new ServerHook(gameServer, dispatchServer); + // Start servers. if(getConfig().RunMode.equalsIgnoreCase("HYBRID")) { dispatchServer.start(); diff --git a/src/main/java/emu/grasscutter/plugin/api/ServerHook.java b/src/main/java/emu/grasscutter/plugin/api/ServerHook.java index 34ceb25f4..0ba4d7f17 100644 --- a/src/main/java/emu/grasscutter/plugin/api/ServerHook.java +++ b/src/main/java/emu/grasscutter/plugin/api/ServerHook.java @@ -1,6 +1,7 @@ package emu.grasscutter.plugin.api; import emu.grasscutter.game.GenshinPlayer; +import emu.grasscutter.server.dispatch.DispatchServer; import emu.grasscutter.server.game.GameServer; import java.util.LinkedList; @@ -11,7 +12,8 @@ import java.util.List; */ public final class ServerHook { private static ServerHook instance; - private final GameServer server; + private final GameServer gameServer; + private final DispatchServer dispatchServer; /** * Gets the server hook instance. @@ -23,10 +25,12 @@ public final class ServerHook { /** * Hooks into a server. - * @param server The server to hook into. + * @param gameServer The game server to hook into. + * @param dispatchServer The dispatch server to hook into. */ - public ServerHook(GameServer server) { - this.server = server; + public ServerHook(GameServer gameServer, DispatchServer dispatchServer) { + this.gameServer = gameServer; + this.dispatchServer = dispatchServer; instance = this; } @@ -36,6 +40,6 @@ public final class ServerHook { * @return Players connected to the server. */ public List getOnlinePlayers() { - return new LinkedList<>(this.server.getPlayers().values()); + return new LinkedList<>(this.gameServer.getPlayers().values()); } } \ No newline at end of file From cf7fae8d062f6159c6fb4958d921b6b4cfec2c22 Mon Sep 17 00:00:00 2001 From: KingRainbow44 Date: Tue, 26 Apr 2022 20:47:45 -0400 Subject: [PATCH 08/21] woo back to generics --- .../emu/grasscutter/plugin/PluginManager.java | 9 +++-- .../server/event/EventHandler.java | 37 ++++++++----------- .../emu/grasscutter/utils/EventConsumer.java | 4 +- 3 files changed, 22 insertions(+), 28 deletions(-) diff --git a/src/main/java/emu/grasscutter/plugin/PluginManager.java b/src/main/java/emu/grasscutter/plugin/PluginManager.java index 53ac467f3..d436b54d7 100644 --- a/src/main/java/emu/grasscutter/plugin/PluginManager.java +++ b/src/main/java/emu/grasscutter/plugin/PluginManager.java @@ -21,7 +21,7 @@ import java.util.jar.JarFile; */ public final class PluginManager { private final Map plugins = new HashMap<>(); - private final List listeners = new LinkedList<>(); + private final List> listeners = new LinkedList<>(); public PluginManager() { this.loadPlugins(); // Load all plugins from the plugins directory. @@ -131,7 +131,7 @@ public final class PluginManager { * Registers a plugin's event listener. * @param listener The event listener. */ - public void registerListener(EventHandler listener) { + public void registerListener(EventHandler listener) { this.listeners.add(listener); } @@ -161,9 +161,10 @@ public final class PluginManager { * @param event The event passed through to the handler. * @param handler The handler to invoke. */ - private void invokeHandler(Event event, EventHandler handler) { + @SuppressWarnings("unchecked") + private void invokeHandler(Event event, EventHandler handler) { if(!event.isCanceled() || (event.isCanceled() && handler.ignoresCanceled()) - ) handler.getCallback().consume(event); + ) handler.getCallback().consume((T) event); } } \ No newline at end of file diff --git a/src/main/java/emu/grasscutter/server/event/EventHandler.java b/src/main/java/emu/grasscutter/server/event/EventHandler.java index 14da6f6a1..be6b1c452 100644 --- a/src/main/java/emu/grasscutter/server/event/EventHandler.java +++ b/src/main/java/emu/grasscutter/server/event/EventHandler.java @@ -3,39 +3,32 @@ package emu.grasscutter.server.event; import emu.grasscutter.Grasscutter; import emu.grasscutter.utils.EventConsumer; -public final class EventHandler { - private final Class event; +import java.lang.reflect.ParameterizedType; - /** - * Creates an instance of {@link EventHandler} for the specified event. - * @param event The event to handle. - * @return An instance of {@link EventHandler}. - */ - public static EventHandler forEvent(Class event) { - return new EventHandler(event); - } - - private EventHandler(Class event) { - this.event = event; - } - - private EventConsumer listener; +public final class EventHandler { + private final Class eventClass; + private EventConsumer listener; private HandlerPriority priority; private boolean handleCanceled; + + @SuppressWarnings("unchecked") + public EventHandler() { + this.eventClass = (Class) ((ParameterizedType) (getClass().getGenericSuperclass())).getActualTypeArguments()[0]; + } /** * Gets which event this handler is handling. * @return An event class. */ - public Class handles() { - return this.event; + public Class handles() { + return this.eventClass; } /** * Returns the callback for the handler. * @return A consumer callback. */ - public EventConsumer getCallback() { + public EventConsumer getCallback() { return this.listener; } @@ -60,7 +53,7 @@ public final class EventHandler { * @param listener An event handler method. * @return Method chaining. */ - public EventHandler listener(EventConsumer listener) { + public EventHandler listener(EventConsumer listener) { this.listener = listener; return this; } @@ -69,7 +62,7 @@ public final class EventHandler { * @param priority The priority of the handler. * @return Method chaining. */ - public EventHandler priority(HandlerPriority priority) { + public EventHandler priority(HandlerPriority priority) { this.priority = priority; return this; } @@ -78,7 +71,7 @@ public final class EventHandler { * @param ignore If the handler should ignore cancelled events. * @return Method chaining. */ - public EventHandler ignore(boolean ignore) { + public EventHandler ignore(boolean ignore) { this.handleCanceled = ignore; return this; } diff --git a/src/main/java/emu/grasscutter/utils/EventConsumer.java b/src/main/java/emu/grasscutter/utils/EventConsumer.java index ae8bb5a9b..baa531944 100644 --- a/src/main/java/emu/grasscutter/utils/EventConsumer.java +++ b/src/main/java/emu/grasscutter/utils/EventConsumer.java @@ -2,6 +2,6 @@ package emu.grasscutter.utils; import emu.grasscutter.server.event.Event; -public interface EventConsumer { - void consume(T event); +public interface EventConsumer { + void consume(T event); } From 54658afe68ec99b05550766b8635640a0246f4ac Mon Sep 17 00:00:00 2001 From: KingRainbow44 Date: Tue, 26 Apr 2022 20:54:56 -0400 Subject: [PATCH 09/21] gave up, Pass class as argument to constructor --- .../java/emu/grasscutter/server/event/EventHandler.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/main/java/emu/grasscutter/server/event/EventHandler.java b/src/main/java/emu/grasscutter/server/event/EventHandler.java index be6b1c452..6611b758c 100644 --- a/src/main/java/emu/grasscutter/server/event/EventHandler.java +++ b/src/main/java/emu/grasscutter/server/event/EventHandler.java @@ -3,17 +3,14 @@ package emu.grasscutter.server.event; import emu.grasscutter.Grasscutter; import emu.grasscutter.utils.EventConsumer; -import java.lang.reflect.ParameterizedType; - public final class EventHandler { private final Class eventClass; private EventConsumer listener; private HandlerPriority priority; private boolean handleCanceled; - @SuppressWarnings("unchecked") - public EventHandler() { - this.eventClass = (Class) ((ParameterizedType) (getClass().getGenericSuperclass())).getActualTypeArguments()[0]; + public EventHandler(Class eventClass) { + this.eventClass = eventClass; } /** From 34c358669b5cc25038fddd4ea2a7c012dd3da9a6 Mon Sep 17 00:00:00 2001 From: KingRainbow44 Date: Tue, 26 Apr 2022 21:24:09 -0400 Subject: [PATCH 10/21] Added getting resources from the JAR --- .../java/emu/grasscutter/plugin/Plugin.java | 30 +++++++++++++++++-- .../emu/grasscutter/plugin/PluginManager.java | 8 ++--- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/main/java/emu/grasscutter/plugin/Plugin.java b/src/main/java/emu/grasscutter/plugin/Plugin.java index a3160d7c7..42858c247 100644 --- a/src/main/java/emu/grasscutter/plugin/Plugin.java +++ b/src/main/java/emu/grasscutter/plugin/Plugin.java @@ -1,13 +1,20 @@ package emu.grasscutter.plugin; import emu.grasscutter.Grasscutter; +import emu.grasscutter.plugin.api.ServerHook; import emu.grasscutter.server.game.GameServer; +import java.io.InputStream; +import java.net.URLClassLoader; + /** * The base class for all plugins to extend. */ public abstract class Plugin { + private final ServerHook server = ServerHook.getInstance(); + private PluginIdentifier identifier; + private URLClassLoader classLoader; /** * This method is reflected into. @@ -15,9 +22,11 @@ public abstract class Plugin { * Set plugin variables. * @param identifier The plugin's identifier. */ - private void initializePlugin(PluginIdentifier identifier) { + private void initializePlugin(PluginIdentifier identifier, URLClassLoader classLoader) { if(this.identifier == null) this.identifier = identifier; + if(this.classLoader == null) + this.classLoader = classLoader; else Grasscutter.getLogger().warn(this.identifier.name + " had a reinitialization attempt."); } @@ -55,7 +64,24 @@ public abstract class Plugin { * @return A server instance. */ public final GameServer getServer() { - return Grasscutter.getGameServer(); + return this.server.getGameServer(); + } + + /** + * Returns an input stream for a resource in the JAR file. + * @param resourceName The name of the resource. + * @return An input stream. + */ + public final InputStream getResource(String resourceName) { + return this.classLoader.getResourceAsStream(resourceName); + } + + /** + * Returns the server hook. + * @return A server hook singleton. + */ + public final ServerHook getHandle() { + return this.server; } /* Called when the plugin is first loaded. */ diff --git a/src/main/java/emu/grasscutter/plugin/PluginManager.java b/src/main/java/emu/grasscutter/plugin/PluginManager.java index d436b54d7..0c11aadf0 100644 --- a/src/main/java/emu/grasscutter/plugin/PluginManager.java +++ b/src/main/java/emu/grasscutter/plugin/PluginManager.java @@ -73,7 +73,7 @@ public final class PluginManager { Class pluginClass = loader.loadClass(pluginConfig.mainClass); Plugin pluginInstance = (Plugin) pluginClass.getDeclaredConstructor().newInstance(); - this.loadPlugin(pluginInstance, PluginIdentifier.fromPluginConfig(pluginConfig)); + this.loadPlugin(pluginInstance, PluginIdentifier.fromPluginConfig(pluginConfig), loader); fileReader.close(); // Close the file reader. } catch (ClassNotFoundException ignored) { @@ -89,14 +89,14 @@ public final class PluginManager { * Load the specified plugin. * @param plugin The plugin instance. */ - private void loadPlugin(Plugin plugin, PluginIdentifier identifier) { + private void loadPlugin(Plugin plugin, PluginIdentifier identifier, URLClassLoader classLoader) { Grasscutter.getLogger().info("Loading plugin: " + identifier.name); // Add the plugin's identifier. try { Class pluginClass = Plugin.class; - Method method = pluginClass.getDeclaredMethod("initializePlugin", PluginIdentifier.class); - method.setAccessible(true); method.invoke(plugin, identifier); method.setAccessible(false); + Method method = pluginClass.getDeclaredMethod("initializePlugin", PluginIdentifier.class, URLClassLoader.class); + method.setAccessible(true); method.invoke(plugin, identifier, classLoader); method.setAccessible(false); } catch (Exception ignored) { Grasscutter.getLogger().warn("Failed to add plugin identifier: " + identifier.name); } From 5b351b955cb3b304cc4995dcf354357eecd31074 Mon Sep 17 00:00:00 2001 From: KingRainbow44 Date: Tue, 26 Apr 2022 21:24:37 -0400 Subject: [PATCH 11/21] Add `get` methods to the server hook --- .../grasscutter/plugin/api/ServerHook.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/main/java/emu/grasscutter/plugin/api/ServerHook.java b/src/main/java/emu/grasscutter/plugin/api/ServerHook.java index 0ba4d7f17..8346903f4 100644 --- a/src/main/java/emu/grasscutter/plugin/api/ServerHook.java +++ b/src/main/java/emu/grasscutter/plugin/api/ServerHook.java @@ -1,5 +1,7 @@ package emu.grasscutter.plugin.api; +import emu.grasscutter.command.Command; +import emu.grasscutter.command.CommandHandler; import emu.grasscutter.game.GenshinPlayer; import emu.grasscutter.server.dispatch.DispatchServer; import emu.grasscutter.server.game.GameServer; @@ -35,6 +37,20 @@ public final class ServerHook { instance = this; } + /** + * @return The game server. + */ + public GameServer getGameServer() { + return this.gameServer; + } + + /** + * @return The dispatch server. + */ + public DispatchServer getDispatchServer() { + return this.dispatchServer; + } + /** * Gets all online players. * @return Players connected to the server. @@ -42,4 +58,16 @@ public final class ServerHook { public List getOnlinePlayers() { return new LinkedList<>(this.gameServer.getPlayers().values()); } + + /** + * Registers a command to the {@link emu.grasscutter.command.CommandMap}. + * @param handler The command handler. + */ + public void registerCommand(CommandHandler handler) { + Class clazz = handler.getClass(); + if(!clazz.isAnnotationPresent(Command.class)) + throw new IllegalArgumentException("Command handler must be annotated with @Command."); + Command commandData = clazz.getAnnotation(Command.class); + this.gameServer.getCommandMap().registerCommand(commandData.label(), handler); + } } \ No newline at end of file From e2d6e695dcad27186414d262535c6aee155fb492 Mon Sep 17 00:00:00 2001 From: KingRainbow44 Date: Thu, 28 Apr 2022 21:54:51 -0400 Subject: [PATCH 12/21] Bug fixes & class updates --- src/main/java/emu/grasscutter/game/Account.java | 2 +- src/main/java/emu/grasscutter/plugin/api/ServerHook.java | 8 ++++---- .../emu/grasscutter/server/dispatch/DispatchServer.java | 8 ++++---- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/main/java/emu/grasscutter/game/Account.java b/src/main/java/emu/grasscutter/game/Account.java index dbd6ef854..97ad428fe 100644 --- a/src/main/java/emu/grasscutter/game/Account.java +++ b/src/main/java/emu/grasscutter/game/Account.java @@ -104,7 +104,7 @@ public class Account { } public boolean hasPermission(String permission) { - return this.permissions.contains(permission) || this.permissions.contains("*") ? true : false; + return this.permissions.contains(permission) || this.permissions.contains("*"); } public boolean removePermission(String permission) { diff --git a/src/main/java/emu/grasscutter/plugin/api/ServerHook.java b/src/main/java/emu/grasscutter/plugin/api/ServerHook.java index 8346903f4..a37abfb62 100644 --- a/src/main/java/emu/grasscutter/plugin/api/ServerHook.java +++ b/src/main/java/emu/grasscutter/plugin/api/ServerHook.java @@ -2,7 +2,7 @@ package emu.grasscutter.plugin.api; import emu.grasscutter.command.Command; import emu.grasscutter.command.CommandHandler; -import emu.grasscutter.game.GenshinPlayer; +import emu.grasscutter.game.player.Player; import emu.grasscutter.server.dispatch.DispatchServer; import emu.grasscutter.server.game.GameServer; @@ -33,7 +33,7 @@ public final class ServerHook { public ServerHook(GameServer gameServer, DispatchServer dispatchServer) { this.gameServer = gameServer; this.dispatchServer = dispatchServer; - + instance = this; } @@ -50,12 +50,12 @@ public final class ServerHook { public DispatchServer getDispatchServer() { return this.dispatchServer; } - + /** * Gets all online players. * @return Players connected to the server. */ - public List getOnlinePlayers() { + public List getOnlinePlayers() { return new LinkedList<>(this.gameServer.getPlayers().values()); } diff --git a/src/main/java/emu/grasscutter/server/dispatch/DispatchServer.java b/src/main/java/emu/grasscutter/server/dispatch/DispatchServer.java index 99c5e4f8d..e8fd5a4ea 100644 --- a/src/main/java/emu/grasscutter/server/dispatch/DispatchServer.java +++ b/src/main/java/emu/grasscutter/server/dispatch/DispatchServer.java @@ -209,7 +209,7 @@ public final class DispatchServer { return null; } - private KeyManagerFactory createKeyManagerFactory(File keystore, String password) throws Exception { + private KeyManagerFactory createKeyManagerFactory(File keystore, String password) { char[] pass = password.toCharArray(); KeyManagerFactory kmf = null; @@ -220,8 +220,8 @@ public final class DispatchServer { kmf = KeyManagerFactory.getInstance("SunX509"); kmf.init(ks, pass); - } catch (Exception e) { - throw e; + } catch (Exception exception) { + Grasscutter.getLogger().error("Unable to load keystore.", exception); } return kmf; @@ -257,7 +257,7 @@ public final class DispatchServer { server = this.safelyCreateServer(this.getAddress()); } - HttpsServer httpsServer = null; + HttpsServer httpsServer; try { httpsServer = HttpsServer.create(getAddress(), 0); From d1ec5b35531cfab3bbed1b7c46d10ca6c2db753d Mon Sep 17 00:00:00 2001 From: KingRainbow44 Date: Thu, 28 Apr 2022 22:01:40 -0400 Subject: [PATCH 13/21] Add plugin data folders --- .../java/emu/grasscutter/plugin/Plugin.java | 28 +++++++++++++++---- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/main/java/emu/grasscutter/plugin/Plugin.java b/src/main/java/emu/grasscutter/plugin/Plugin.java index 42858c247..97fc5fd77 100644 --- a/src/main/java/emu/grasscutter/plugin/Plugin.java +++ b/src/main/java/emu/grasscutter/plugin/Plugin.java @@ -4,6 +4,7 @@ import emu.grasscutter.Grasscutter; import emu.grasscutter.plugin.api.ServerHook; import emu.grasscutter.server.game.GameServer; +import java.io.File; import java.io.InputStream; import java.net.URLClassLoader; @@ -15,6 +16,7 @@ public abstract class Plugin { private PluginIdentifier identifier; private URLClassLoader classLoader; + private File dataFolder; /** * This method is reflected into. @@ -23,11 +25,19 @@ public abstract class Plugin { * @param identifier The plugin's identifier. */ private void initializePlugin(PluginIdentifier identifier, URLClassLoader classLoader) { - if(this.identifier == null) - this.identifier = identifier; - if(this.classLoader == null) - this.classLoader = classLoader; - else Grasscutter.getLogger().warn(this.identifier.name + " had a reinitialization attempt."); + if(this.identifier != null) { + Grasscutter.getLogger().warn(this.identifier.name + " had a reinitialization attempt."); + return; + } + + this.identifier = identifier; + this.classLoader = classLoader; + this.dataFolder = new File(Grasscutter.getConfig().PLUGINS_FOLDER, identifier.name); + + if(!this.dataFolder.exists() && !this.dataFolder.mkdirs()) { + Grasscutter.getLogger().warn("Failed to create plugin data folder for " + this.identifier.name); + return; + } } /** @@ -76,6 +86,14 @@ public abstract class Plugin { return this.classLoader.getResourceAsStream(resourceName); } + /** + * Returns a directory where plugins can store data files. + * @return A directory on the file system. + */ + public final File getDataFolder() { + return this.dataFolder; + } + /** * Returns the server hook. * @return A server hook singleton. From f37ab461398ca62f88d92ed4406435327371b208 Mon Sep 17 00:00:00 2001 From: KingRainbow44 Date: Thu, 28 Apr 2022 22:12:41 -0400 Subject: [PATCH 14/21] Player join & quit event --- .../emu/grasscutter/game/player/Player.java | 4 ++++ .../server/event/game/PlayerJoinEvent.java | 17 +++++++++++++++++ .../server/event/game/PlayerQuitEvent.java | 16 ++++++++++++++++ .../recv/HandlerSetPlayerBornDataReq.java | 9 +++++++-- 4 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 src/main/java/emu/grasscutter/server/event/game/PlayerJoinEvent.java create mode 100644 src/main/java/emu/grasscutter/server/event/game/PlayerQuitEvent.java diff --git a/src/main/java/emu/grasscutter/game/player/Player.java b/src/main/java/emu/grasscutter/game/player/Player.java index f440fcb1b..32e583fc5 100644 --- a/src/main/java/emu/grasscutter/game/player/Player.java +++ b/src/main/java/emu/grasscutter/game/player/Player.java @@ -37,6 +37,7 @@ import emu.grasscutter.net.proto.PlayerWorldLocationInfoOuterClass; import emu.grasscutter.net.proto.ProfilePictureOuterClass.ProfilePicture; import emu.grasscutter.net.proto.SocialDetailOuterClass.SocialDetail; import emu.grasscutter.net.proto.SocialShowAvatarInfoOuterClass; +import emu.grasscutter.server.event.game.PlayerQuitEvent; import emu.grasscutter.server.game.GameServer; import emu.grasscutter.server.game.GameSession; import emu.grasscutter.server.packet.send.*; @@ -1026,6 +1027,9 @@ public class Player { this.save(); this.getTeamManager().saveAvatars(); this.getFriendsList().save(); + + // Call quit event. + PlayerQuitEvent event = new PlayerQuitEvent(this); event.call(); } public enum SceneLoadState { diff --git a/src/main/java/emu/grasscutter/server/event/game/PlayerJoinEvent.java b/src/main/java/emu/grasscutter/server/event/game/PlayerJoinEvent.java new file mode 100644 index 000000000..1683acce8 --- /dev/null +++ b/src/main/java/emu/grasscutter/server/event/game/PlayerJoinEvent.java @@ -0,0 +1,17 @@ +package emu.grasscutter.server.event.game; + +import emu.grasscutter.game.player.Player; +import emu.grasscutter.server.event.Cancellable; +import emu.grasscutter.server.event.types.GameEvent; + +public final class PlayerJoinEvent extends GameEvent implements Cancellable { + private final Player player; + + public PlayerJoinEvent(Player player) { + this.player = player; + } + + public Player getPlayer() { + return this.player; + } +} diff --git a/src/main/java/emu/grasscutter/server/event/game/PlayerQuitEvent.java b/src/main/java/emu/grasscutter/server/event/game/PlayerQuitEvent.java new file mode 100644 index 000000000..df730442d --- /dev/null +++ b/src/main/java/emu/grasscutter/server/event/game/PlayerQuitEvent.java @@ -0,0 +1,16 @@ +package emu.grasscutter.server.event.game; + +import emu.grasscutter.game.player.Player; +import emu.grasscutter.server.event.types.GameEvent; + +public final class PlayerQuitEvent extends GameEvent { + private final Player player; + + public PlayerQuitEvent(Player player) { + this.player = player; + } + + public Player getPlayer() { + return this.player; + } +} diff --git a/src/main/java/emu/grasscutter/server/packet/recv/HandlerSetPlayerBornDataReq.java b/src/main/java/emu/grasscutter/server/packet/recv/HandlerSetPlayerBornDataReq.java index 52c52c280..77dc5988b 100644 --- a/src/main/java/emu/grasscutter/server/packet/recv/HandlerSetPlayerBornDataReq.java +++ b/src/main/java/emu/grasscutter/server/packet/recv/HandlerSetPlayerBornDataReq.java @@ -13,6 +13,7 @@ import emu.grasscutter.net.packet.Opcodes; import emu.grasscutter.net.packet.PacketOpcodes; import emu.grasscutter.net.proto.SetPlayerBornDataReqOuterClass.SetPlayerBornDataReq; import emu.grasscutter.net.packet.PacketHandler; +import emu.grasscutter.server.event.game.PlayerJoinEvent; import emu.grasscutter.server.game.GameSession; import emu.grasscutter.server.game.GameSession.SessionState; @@ -25,7 +26,7 @@ public class HandlerSetPlayerBornDataReq extends PacketHandler { // Sanity checks int avatarId = req.getAvatarId(); - int startingSkillDepot = 0; + int startingSkillDepot; if (avatarId == GameConstants.MAIN_CHARACTER_MALE) { startingSkillDepot = 504; } else if (avatarId == GameConstants.MAIN_CHARACTER_FEMALE) { @@ -91,6 +92,10 @@ public class HandlerSetPlayerBornDataReq extends PacketHandler { Grasscutter.getLogger().error("Error creating player object: ", e); session.close(); } - } + // Call join event. + PlayerJoinEvent event = new PlayerJoinEvent(player); event.call(); + if(event.isCanceled()) // If event is not cancelled, continue. + session.close(); + } } From 3ef052d7b1ed2b2c99b0ad7e4a4b4d20bb0aa0ea Mon Sep 17 00:00:00 2001 From: KingRainbow44 Date: Thu, 28 Apr 2022 22:12:57 -0400 Subject: [PATCH 15/21] Move events --- .../emu/grasscutter/server/event/types/GameEvent.java | 10 ++++++++++ .../server/event/{ => types}/ServerEvent.java | 4 +++- 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 src/main/java/emu/grasscutter/server/event/types/GameEvent.java rename src/main/java/emu/grasscutter/server/event/{ => types}/ServerEvent.java (80%) diff --git a/src/main/java/emu/grasscutter/server/event/types/GameEvent.java b/src/main/java/emu/grasscutter/server/event/types/GameEvent.java new file mode 100644 index 000000000..61c8daa73 --- /dev/null +++ b/src/main/java/emu/grasscutter/server/event/types/GameEvent.java @@ -0,0 +1,10 @@ +package emu.grasscutter.server.event.types; + +import emu.grasscutter.server.event.Event; + +/** + * An event that is related to the game. + */ +public abstract class GameEvent extends Event { + +} \ No newline at end of file diff --git a/src/main/java/emu/grasscutter/server/event/ServerEvent.java b/src/main/java/emu/grasscutter/server/event/types/ServerEvent.java similarity index 80% rename from src/main/java/emu/grasscutter/server/event/ServerEvent.java rename to src/main/java/emu/grasscutter/server/event/types/ServerEvent.java index 5e73afdec..5191bbf22 100644 --- a/src/main/java/emu/grasscutter/server/event/ServerEvent.java +++ b/src/main/java/emu/grasscutter/server/event/types/ServerEvent.java @@ -1,4 +1,6 @@ -package emu.grasscutter.server.event; +package emu.grasscutter.server.event.types; + +import emu.grasscutter.server.event.Event; /** * An event that is related to the internals of the server. From b811300742903339120745bfc609199f009a5394 Mon Sep 17 00:00:00 2001 From: KingRainbow44 Date: Thu, 28 Apr 2022 22:13:19 -0400 Subject: [PATCH 16/21] Move events (imports) --- .../java/emu/grasscutter/server/event/game/SendPacketEvent.java | 2 +- .../java/emu/grasscutter/server/event/game/ServerTickEvent.java | 2 +- .../emu/grasscutter/server/event/internal/ServerStartEvent.java | 2 +- .../emu/grasscutter/server/event/internal/ServerStopEvent.java | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/emu/grasscutter/server/event/game/SendPacketEvent.java b/src/main/java/emu/grasscutter/server/event/game/SendPacketEvent.java index b78e0d0c8..e5dd4982c 100644 --- a/src/main/java/emu/grasscutter/server/event/game/SendPacketEvent.java +++ b/src/main/java/emu/grasscutter/server/event/game/SendPacketEvent.java @@ -2,7 +2,7 @@ package emu.grasscutter.server.event.game; import emu.grasscutter.net.packet.BasePacket; import emu.grasscutter.server.event.Cancellable; -import emu.grasscutter.server.event.ServerEvent; +import emu.grasscutter.server.event.types.ServerEvent; import emu.grasscutter.server.game.GameSession; public final class SendPacketEvent extends ServerEvent implements Cancellable { diff --git a/src/main/java/emu/grasscutter/server/event/game/ServerTickEvent.java b/src/main/java/emu/grasscutter/server/event/game/ServerTickEvent.java index 8ca7c8379..d1c12d02d 100644 --- a/src/main/java/emu/grasscutter/server/event/game/ServerTickEvent.java +++ b/src/main/java/emu/grasscutter/server/event/game/ServerTickEvent.java @@ -1,6 +1,6 @@ package emu.grasscutter.server.event.game; -import emu.grasscutter.server.event.ServerEvent; +import emu.grasscutter.server.event.types.ServerEvent; public final class ServerTickEvent extends ServerEvent { public ServerTickEvent() { diff --git a/src/main/java/emu/grasscutter/server/event/internal/ServerStartEvent.java b/src/main/java/emu/grasscutter/server/event/internal/ServerStartEvent.java index fd950a002..3a050d750 100644 --- a/src/main/java/emu/grasscutter/server/event/internal/ServerStartEvent.java +++ b/src/main/java/emu/grasscutter/server/event/internal/ServerStartEvent.java @@ -1,6 +1,6 @@ package emu.grasscutter.server.event.internal; -import emu.grasscutter.server.event.ServerEvent; +import emu.grasscutter.server.event.types.ServerEvent; import java.time.OffsetDateTime; diff --git a/src/main/java/emu/grasscutter/server/event/internal/ServerStopEvent.java b/src/main/java/emu/grasscutter/server/event/internal/ServerStopEvent.java index 5d105e3b3..c4e7ca8cf 100644 --- a/src/main/java/emu/grasscutter/server/event/internal/ServerStopEvent.java +++ b/src/main/java/emu/grasscutter/server/event/internal/ServerStopEvent.java @@ -1,6 +1,6 @@ package emu.grasscutter.server.event.internal; -import emu.grasscutter.server.event.ServerEvent; +import emu.grasscutter.server.event.types.ServerEvent; import java.time.OffsetDateTime; From 2e119efb86aeb26e8af988eb64665f04830d77da Mon Sep 17 00:00:00 2001 From: KingRainbow44 Date: Thu, 28 Apr 2022 22:21:30 -0400 Subject: [PATCH 17/21] Formatting changes --- .../command/commands/CoopCommand.java | 3 ++- .../command/commands/GiveAllCommand.java | 12 +++------ .../command/commands/SendMailCommand.java | 11 +++----- ...llCommand.java => TeleportAllCommand.java} | 10 ++++--- .../server/dispatch/DispatchServer.java | 27 ++++++++----------- .../event/dispatch/QueryAllRegionsEvent.java | 2 +- .../dispatch/QueryCurrentRegionEvent.java | 2 +- .../server/event/game/ReceivePacketEvent.java | 2 +- .../grasscutter/server/game/GameServer.java | 2 +- .../emu/grasscutter/task/TaskHandler.java | 18 +++---------- .../emu/grasscutter/task/tasks/MoonCard.java | 7 +++-- 11 files changed, 36 insertions(+), 60 deletions(-) rename src/main/java/emu/grasscutter/command/commands/{TpallCommand.java => TeleportAllCommand.java} (75%) diff --git a/src/main/java/emu/grasscutter/command/commands/CoopCommand.java b/src/main/java/emu/grasscutter/command/commands/CoopCommand.java index 0dec6839e..633c2c8c1 100644 --- a/src/main/java/emu/grasscutter/command/commands/CoopCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/CoopCommand.java @@ -8,13 +8,14 @@ import java.util.List; @Command(label = "coop", usage = "coop", description = "Forces someone to join the world of others", permission = "server.coop") -public class CoopCommand implements CommandHandler { +public final class CoopCommand implements CommandHandler { @Override public void execute(Player sender, List args) { if (args.size() < 2) { CommandHandler.sendMessage(sender, "Usage: coop "); return; } + try { int tid = Integer.parseInt(args.get(0)); int hostId = Integer.parseInt(args.get(1)); diff --git a/src/main/java/emu/grasscutter/command/commands/GiveAllCommand.java b/src/main/java/emu/grasscutter/command/commands/GiveAllCommand.java index 648828643..804f13919 100644 --- a/src/main/java/emu/grasscutter/command/commands/GiveAllCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/GiveAllCommand.java @@ -15,7 +15,7 @@ import java.util.*; @Command(label = "giveall", usage = "giveall [player] [amount]", description = "Gives all items", aliases = {"givea"}, permission = "player.giveall", threading = true) -public class GiveAllCommand implements CommandHandler { +public final class GiveAllCommand implements CommandHandler { @Override public void execute(Player sender, List args) { @@ -142,16 +142,11 @@ public class GiveAllCommand implements CommandHandler { } } - if (testItemsList.contains(itemId)) { - return true; - } - - return false; + return testItemsList.contains(itemId); } static class Range { - private int min; - private int max; + private final int min, max; public Range(int min, int max) { if(min > max){ @@ -159,6 +154,7 @@ public class GiveAllCommand implements CommandHandler { max ^= min; min ^= max; } + this.min = min; this.max = max; } diff --git a/src/main/java/emu/grasscutter/command/commands/SendMailCommand.java b/src/main/java/emu/grasscutter/command/commands/SendMailCommand.java index 35cedab9a..031358d45 100644 --- a/src/main/java/emu/grasscutter/command/commands/SendMailCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/SendMailCommand.java @@ -6,24 +6,20 @@ import emu.grasscutter.command.CommandHandler; import emu.grasscutter.database.DatabaseHelper; import emu.grasscutter.game.mail.Mail; import emu.grasscutter.game.player.Player; -import emu.grasscutter.server.packet.send.PacketMailChangeNotify; -import java.time.Instant; -import java.util.ArrayList; import java.util.HashMap; import java.util.List; -import java.util.Locale; @Command(label = "sendmail", usage = "sendmail [templateId]", description = "Sends mail to the specified user. The usage of this command changes based on it's composition state.", permission = "server.sendmail") -public class SendMailCommand implements CommandHandler { +public final class SendMailCommand implements CommandHandler { // TODO: You should be able to do /sendmail and then just send subsequent messages until you finish // However, due to the current nature of the command system, I don't think this is possible without rewriting // the command system (again). For now this will do // Key = User that is constructing the mail. - private static HashMap mailBeingConstructed = new HashMap(); + private static final HashMap mailBeingConstructed = new HashMap(); // Yes this is awful and I hate it. @Override @@ -48,7 +44,6 @@ public class SendMailCommand implements CommandHandler { default -> { if (DatabaseHelper.getPlayerById(Integer.parseInt(args.get(0))) != null) { mailBuilder = new MailBuilder(Integer.parseInt(args.get(0)), new Mail()); - break; } else { CommandHandler.sendMessage(sender, "The user with an id of '" + args.get(0) + "' does not exist"); return; @@ -73,7 +68,7 @@ public class SendMailCommand implements CommandHandler { } case "finish" -> { if (mailBuilder.constructionStage == 3) { - if (mailBuilder.sendToAll == false) { + if (!mailBuilder.sendToAll) { Grasscutter.getGameServer().getPlayerByUid(mailBuilder.recipient, true).sendMail(mailBuilder.mail); CommandHandler.sendMessage(sender, "Message sent to user " + mailBuilder.recipient + "!"); } else { diff --git a/src/main/java/emu/grasscutter/command/commands/TpallCommand.java b/src/main/java/emu/grasscutter/command/commands/TeleportAllCommand.java similarity index 75% rename from src/main/java/emu/grasscutter/command/commands/TpallCommand.java rename to src/main/java/emu/grasscutter/command/commands/TeleportAllCommand.java index 20236c729..bd4a8a06e 100644 --- a/src/main/java/emu/grasscutter/command/commands/TpallCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/TeleportAllCommand.java @@ -9,23 +9,25 @@ import java.util.List; @Command(label = "tpall", usage = "tpall", description = "Teleports all players in your world to your position", permission = "player.tpall") -public class TpallCommand implements CommandHandler { +public final class TeleportAllCommand implements CommandHandler { @Override public void execute(Player sender, List args) { if (sender == null) { CommandHandler.sendMessage(null, "Run this command in-game."); return; } + if (!sender.getWorld().isMultiplayer()) { CommandHandler.sendMessage(sender, "You only can use this command in MP mode."); return; } - for (Player gp : sender.getWorld().getPlayers()) { - if (gp.equals(sender)) + + for (Player player : sender.getWorld().getPlayers()) { + if (player.equals(sender)) continue; Position pos = sender.getPos(); - gp.getWorld().transferPlayerToScene(gp, sender.getSceneId(), pos); + player.getWorld().transferPlayerToScene(player, sender.getSceneId(), pos); } } } diff --git a/src/main/java/emu/grasscutter/server/dispatch/DispatchServer.java b/src/main/java/emu/grasscutter/server/dispatch/DispatchServer.java index e8fd5a4ea..f2443dfea 100644 --- a/src/main/java/emu/grasscutter/server/dispatch/DispatchServer.java +++ b/src/main/java/emu/grasscutter/server/dispatch/DispatchServer.java @@ -30,6 +30,7 @@ import java.net.BindException; import java.net.InetSocketAddress; import java.net.URI; import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; import java.security.KeyStore; import java.util.*; @@ -243,10 +244,9 @@ public final class DispatchServer { try { kmf = createKeyManagerFactory(keystoreFile, "123456"); Grasscutter.getLogger().warn( - "[Dispatch] The default keystore password was loaded successfully. Please consider setting the password to 123456 in config.json."); + "[Dispatch] The default keystore password was loaded successfully. Please consider setting the password to '123456' in config.json."); } catch (Exception e2) { - Grasscutter.getLogger().warn("[Dispatch] Error while loading keystore!"); - e2.printStackTrace(); + Grasscutter.getLogger().warn("[Dispatch] Error while loading keystore!", e2); } } } @@ -339,10 +339,6 @@ public final class DispatchServer { // added. account = DatabaseHelper.createAccountWithId(requestData.account, 0); - for (String permission : Grasscutter.getConfig().getDispatchOptions().defaultPermissions) { - account.addPermission(permission); - } - if (account != null) { responseData.message = "OK"; responseData.data.account.uid = account.getId(); @@ -352,6 +348,9 @@ public final class DispatchServer { Grasscutter.getLogger() .info(String.format("[Dispatch] Client %s failed to log in: Account %s created", t.getRemoteAddress(), responseData.data.account.uid)); + for (String permission : Grasscutter.getConfig().getDispatchOptions().defaultPermissions) { + account.addPermission(permission); + } } else { responseData.retcode = -201; responseData.message = "Username not found, create failed."; @@ -575,15 +574,11 @@ public final class DispatchServer { if (next > last) { int eqPos = qs.indexOf('=', last); - try { - if (eqPos < 0 || eqPos > next) { - result.put(URLDecoder.decode(qs.substring(last, next), "utf-8"), ""); - } else { - result.put(URLDecoder.decode(qs.substring(last, eqPos), "utf-8"), - URLDecoder.decode(qs.substring(eqPos + 1, next), "utf-8")); - } - } catch (UnsupportedEncodingException e) { - throw new RuntimeException(e); // will never happen, utf-8 support is mandatory for java + if (eqPos < 0 || eqPos > next) { + result.put(URLDecoder.decode(qs.substring(last, next), StandardCharsets.UTF_8), ""); + } else { + result.put(URLDecoder.decode(qs.substring(last, eqPos), StandardCharsets.UTF_8), + URLDecoder.decode(qs.substring(eqPos + 1, next), StandardCharsets.UTF_8)); } } last = next + 1; diff --git a/src/main/java/emu/grasscutter/server/event/dispatch/QueryAllRegionsEvent.java b/src/main/java/emu/grasscutter/server/event/dispatch/QueryAllRegionsEvent.java index 8595f6221..bb4864692 100644 --- a/src/main/java/emu/grasscutter/server/event/dispatch/QueryAllRegionsEvent.java +++ b/src/main/java/emu/grasscutter/server/event/dispatch/QueryAllRegionsEvent.java @@ -1,6 +1,6 @@ package emu.grasscutter.server.event.dispatch; -import emu.grasscutter.server.event.ServerEvent; +import emu.grasscutter.server.event.types.ServerEvent; public final class QueryAllRegionsEvent extends ServerEvent { private String regionList; diff --git a/src/main/java/emu/grasscutter/server/event/dispatch/QueryCurrentRegionEvent.java b/src/main/java/emu/grasscutter/server/event/dispatch/QueryCurrentRegionEvent.java index d6a20b2df..d963d74a7 100644 --- a/src/main/java/emu/grasscutter/server/event/dispatch/QueryCurrentRegionEvent.java +++ b/src/main/java/emu/grasscutter/server/event/dispatch/QueryCurrentRegionEvent.java @@ -1,6 +1,6 @@ package emu.grasscutter.server.event.dispatch; -import emu.grasscutter.server.event.ServerEvent; +import emu.grasscutter.server.event.types.ServerEvent; public final class QueryCurrentRegionEvent extends ServerEvent { private String regionInfo; diff --git a/src/main/java/emu/grasscutter/server/event/game/ReceivePacketEvent.java b/src/main/java/emu/grasscutter/server/event/game/ReceivePacketEvent.java index 51109c720..e64a6305d 100644 --- a/src/main/java/emu/grasscutter/server/event/game/ReceivePacketEvent.java +++ b/src/main/java/emu/grasscutter/server/event/game/ReceivePacketEvent.java @@ -1,7 +1,7 @@ package emu.grasscutter.server.event.game; import emu.grasscutter.server.event.Cancellable; -import emu.grasscutter.server.event.ServerEvent; +import emu.grasscutter.server.event.types.ServerEvent; import emu.grasscutter.server.game.GameSession; public final class ReceivePacketEvent extends ServerEvent implements Cancellable { diff --git a/src/main/java/emu/grasscutter/server/game/GameServer.java b/src/main/java/emu/grasscutter/server/game/GameServer.java index 9452b3993..c66a22e53 100644 --- a/src/main/java/emu/grasscutter/server/game/GameServer.java +++ b/src/main/java/emu/grasscutter/server/game/GameServer.java @@ -17,7 +17,7 @@ import emu.grasscutter.game.world.World; import emu.grasscutter.net.packet.PacketHandler; import emu.grasscutter.net.proto.SocialDetailOuterClass.SocialDetail; import emu.grasscutter.netty.KcpServer; -import emu.grasscutter.server.event.ServerEvent; +import emu.grasscutter.server.event.types.ServerEvent; import emu.grasscutter.server.event.game.ServerTickEvent; import emu.grasscutter.server.event.internal.ServerStartEvent; import emu.grasscutter.server.event.internal.ServerStopEvent; diff --git a/src/main/java/emu/grasscutter/task/TaskHandler.java b/src/main/java/emu/grasscutter/task/TaskHandler.java index ea6fe22cd..bf2b9850a 100644 --- a/src/main/java/emu/grasscutter/task/TaskHandler.java +++ b/src/main/java/emu/grasscutter/task/TaskHandler.java @@ -3,24 +3,12 @@ package emu.grasscutter.task; import org.quartz.*; @PersistJobDataAfterExecution -public class TaskHandler implements Job { - +public abstract class TaskHandler implements Job { public void restartExecute() throws JobExecutionException { execute(null); } - public void onEnable() { - - } - - public void onDisable() { - - } - - @Override - public void execute(JobExecutionContext context) throws JobExecutionException { - // TODO Auto-generated method stub - - } + public abstract void onEnable(); + public abstract void onDisable(); } diff --git a/src/main/java/emu/grasscutter/task/tasks/MoonCard.java b/src/main/java/emu/grasscutter/task/tasks/MoonCard.java index 51088f46d..2d51151c1 100644 --- a/src/main/java/emu/grasscutter/task/tasks/MoonCard.java +++ b/src/main/java/emu/grasscutter/task/tasks/MoonCard.java @@ -4,22 +4,21 @@ import emu.grasscutter.Grasscutter; import emu.grasscutter.task.Task; import emu.grasscutter.task.TaskHandler; - import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; @Task(taskName = "MoonCard", taskCronExpression = "0 0 0 * * ?", triggerName = "MoonCardTrigger") // taskCronExpression: Fixed time period: 0:0:0 every day (twenty-four hour system) -public class MoonCard extends TaskHandler { +public final class MoonCard extends TaskHandler { @Override public void onEnable() { - Grasscutter.getLogger().info("[Task] MoonCard task enabled."); + Grasscutter.getLogger().debug("[Task] MoonCard task enabled."); } @Override public void onDisable() { - Grasscutter.getLogger().info("[Task] MoonCard task disabled."); + Grasscutter.getLogger().debug("[Task] MoonCard task disabled."); } @Override From 7e4021889cef29fd5adfcbde797f37f9e0184d2f Mon Sep 17 00:00:00 2001 From: KingRainbow44 Date: Thu, 28 Apr 2022 22:21:55 -0400 Subject: [PATCH 18/21] Player events + mail receive event --- .../emu/grasscutter/game/player/Player.java | 7 +++++- .../event/player/PlayerReceiveMailEvent.java | 24 +++++++++++++++++++ .../server/event/types/PlayerEvent.java | 19 +++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 src/main/java/emu/grasscutter/server/event/player/PlayerReceiveMailEvent.java create mode 100644 src/main/java/emu/grasscutter/server/event/types/PlayerEvent.java diff --git a/src/main/java/emu/grasscutter/game/player/Player.java b/src/main/java/emu/grasscutter/game/player/Player.java index 32e583fc5..46205d01d 100644 --- a/src/main/java/emu/grasscutter/game/player/Player.java +++ b/src/main/java/emu/grasscutter/game/player/Player.java @@ -38,6 +38,7 @@ import emu.grasscutter.net.proto.ProfilePictureOuterClass.ProfilePicture; import emu.grasscutter.net.proto.SocialDetailOuterClass.SocialDetail; import emu.grasscutter.net.proto.SocialShowAvatarInfoOuterClass; import emu.grasscutter.server.event.game.PlayerQuitEvent; +import emu.grasscutter.server.event.player.PlayerReceiveMailEvent; import emu.grasscutter.server.game.GameServer; import emu.grasscutter.server.game.GameSession; import emu.grasscutter.server.packet.send.*; @@ -726,9 +727,13 @@ public class Player { public List getAllMail() { return this.mail; } public void sendMail(Mail message) { + // Call mail receive event. + PlayerReceiveMailEvent event = new PlayerReceiveMailEvent(this, message); event.call(); + if(event.isCanceled()) return; message = event.getMessage(); + this.mail.add(message); this.save(); - Grasscutter.getLogger().info("Mail sent to user [" + this.getUid() + ":" + this.getNickname() + "]!"); + Grasscutter.getLogger().debug("Mail sent to user [" + this.getUid() + ":" + this.getNickname() + "]!"); if(this.isOnline()) { this.sendPacket(new PacketMailChangeNotify(this, message)); } // TODO: setup a way for the mail notification to show up when someone receives mail when they were offline diff --git a/src/main/java/emu/grasscutter/server/event/player/PlayerReceiveMailEvent.java b/src/main/java/emu/grasscutter/server/event/player/PlayerReceiveMailEvent.java new file mode 100644 index 000000000..c38740ae4 --- /dev/null +++ b/src/main/java/emu/grasscutter/server/event/player/PlayerReceiveMailEvent.java @@ -0,0 +1,24 @@ +package emu.grasscutter.server.event.player; + +import emu.grasscutter.game.mail.Mail; +import emu.grasscutter.game.player.Player; +import emu.grasscutter.server.event.Cancellable; +import emu.grasscutter.server.event.types.PlayerEvent; + +public final class PlayerReceiveMailEvent extends PlayerEvent implements Cancellable { + private Mail message; + + public PlayerReceiveMailEvent(Player player, Mail message) { + super(player); + + this.message = message; + } + + public void setMessage(Mail message) { + this.message = message; + } + + public Mail getMessage() { + return this.message; + } +} diff --git a/src/main/java/emu/grasscutter/server/event/types/PlayerEvent.java b/src/main/java/emu/grasscutter/server/event/types/PlayerEvent.java new file mode 100644 index 000000000..eb55f2d58 --- /dev/null +++ b/src/main/java/emu/grasscutter/server/event/types/PlayerEvent.java @@ -0,0 +1,19 @@ +package emu.grasscutter.server.event.types; + +import emu.grasscutter.game.player.Player; +import emu.grasscutter.server.event.Event; + +/** + * An event that is related to player interactions. + */ +public abstract class PlayerEvent extends Event { + protected final Player player; + + public PlayerEvent(Player player) { + this.player = player; + } + + public Player getPlayer() { + return this.player; + } +} From a3e22dd7e1814fafd0ada2def00b2bebce032617 Mon Sep 17 00:00:00 2001 From: KingRainbow44 Date: Sat, 30 Apr 2022 16:07:12 -0400 Subject: [PATCH 19/21] Create player creation event & move join event to player --- .../emu/grasscutter/game/player/Player.java | 6 +++++ .../event/game/PlayerCreationEvent.java | 27 +++++++++++++++++++ .../recv/HandlerSetPlayerBornDataReq.java | 13 ++++----- 3 files changed, 38 insertions(+), 8 deletions(-) create mode 100644 src/main/java/emu/grasscutter/server/event/game/PlayerCreationEvent.java diff --git a/src/main/java/emu/grasscutter/game/player/Player.java b/src/main/java/emu/grasscutter/game/player/Player.java index 46205d01d..68035a39f 100644 --- a/src/main/java/emu/grasscutter/game/player/Player.java +++ b/src/main/java/emu/grasscutter/game/player/Player.java @@ -37,6 +37,7 @@ import emu.grasscutter.net.proto.PlayerWorldLocationInfoOuterClass; import emu.grasscutter.net.proto.ProfilePictureOuterClass.ProfilePicture; import emu.grasscutter.net.proto.SocialDetailOuterClass.SocialDetail; import emu.grasscutter.net.proto.SocialShowAvatarInfoOuterClass; +import emu.grasscutter.server.event.game.PlayerJoinEvent; import emu.grasscutter.server.event.game.PlayerQuitEvent; import emu.grasscutter.server.event.player.PlayerReceiveMailEvent; import emu.grasscutter.server.game.GameServer; @@ -1014,6 +1015,11 @@ public class Player { // First notify packets sent this.setHasSentAvatarDataNotify(true); + + // Call join event. + PlayerJoinEvent event = new PlayerJoinEvent(this); event.call(); + if(event.isCanceled()) // If event is not cancelled, continue. + session.close(); } public void onLogout() { diff --git a/src/main/java/emu/grasscutter/server/event/game/PlayerCreationEvent.java b/src/main/java/emu/grasscutter/server/event/game/PlayerCreationEvent.java new file mode 100644 index 000000000..2049f4661 --- /dev/null +++ b/src/main/java/emu/grasscutter/server/event/game/PlayerCreationEvent.java @@ -0,0 +1,27 @@ +package emu.grasscutter.server.event.game; + +import emu.grasscutter.game.player.Player; +import emu.grasscutter.server.event.types.GameEvent; +import emu.grasscutter.server.game.GameSession; + +public final class PlayerCreationEvent extends GameEvent { + private final GameSession session; + private Class playerClass; + + public PlayerCreationEvent(GameSession session, Class playerClass) { + this.session = session; + this.playerClass = playerClass; + } + + public GameSession getSession() { + return this.session; + } + + public void setPlayerClass(Class playerClass) { + this.playerClass = playerClass; + } + + public Class getPlayerClass() { + return this.playerClass; + } +} diff --git a/src/main/java/emu/grasscutter/server/packet/recv/HandlerSetPlayerBornDataReq.java b/src/main/java/emu/grasscutter/server/packet/recv/HandlerSetPlayerBornDataReq.java index 77dc5988b..25e6157ae 100644 --- a/src/main/java/emu/grasscutter/server/packet/recv/HandlerSetPlayerBornDataReq.java +++ b/src/main/java/emu/grasscutter/server/packet/recv/HandlerSetPlayerBornDataReq.java @@ -13,7 +13,7 @@ import emu.grasscutter.net.packet.Opcodes; import emu.grasscutter.net.packet.PacketOpcodes; import emu.grasscutter.net.proto.SetPlayerBornDataReqOuterClass.SetPlayerBornDataReq; import emu.grasscutter.net.packet.PacketHandler; -import emu.grasscutter.server.event.game.PlayerJoinEvent; +import emu.grasscutter.server.event.game.PlayerCreationEvent; import emu.grasscutter.server.game.GameSession; import emu.grasscutter.server.game.GameSession.SessionState; @@ -40,8 +40,10 @@ public class HandlerSetPlayerBornDataReq extends PacketHandler { nickname = "Traveler"; } - // Create character - Player player = new Player(session); + // Call creation event. + PlayerCreationEvent event = new PlayerCreationEvent(session, Player.class); event.call(); + // Create player instance from event. + Player player = event.getPlayerClass().getDeclaredConstructor(GameSession.class).newInstance(session); player.setNickname(nickname); try { @@ -92,10 +94,5 @@ public class HandlerSetPlayerBornDataReq extends PacketHandler { Grasscutter.getLogger().error("Error creating player object: ", e); session.close(); } - - // Call join event. - PlayerJoinEvent event = new PlayerJoinEvent(player); event.call(); - if(event.isCanceled()) // If event is not cancelled, continue. - session.close(); } } From e8d60c6edf00aff6a43d33d8cd76f6b1becb3920 Mon Sep 17 00:00:00 2001 From: KingRainbow44 Date: Sat, 30 Apr 2022 16:11:00 -0400 Subject: [PATCH 20/21] Move join & quit to player event --- .../emu/grasscutter/game/player/Player.java | 5 ++--- .../server/event/game/PlayerJoinEvent.java | 17 ----------------- .../server/event/game/PlayerQuitEvent.java | 16 ---------------- .../server/event/player/PlayerJoinEvent.java | 11 +++++++++++ .../server/event/player/PlayerQuitEvent.java | 11 +++++++++++ 5 files changed, 24 insertions(+), 36 deletions(-) delete mode 100644 src/main/java/emu/grasscutter/server/event/game/PlayerJoinEvent.java delete mode 100644 src/main/java/emu/grasscutter/server/event/game/PlayerQuitEvent.java create mode 100644 src/main/java/emu/grasscutter/server/event/player/PlayerJoinEvent.java create mode 100644 src/main/java/emu/grasscutter/server/event/player/PlayerQuitEvent.java diff --git a/src/main/java/emu/grasscutter/game/player/Player.java b/src/main/java/emu/grasscutter/game/player/Player.java index 68035a39f..2637fb10c 100644 --- a/src/main/java/emu/grasscutter/game/player/Player.java +++ b/src/main/java/emu/grasscutter/game/player/Player.java @@ -21,7 +21,6 @@ import emu.grasscutter.game.inventory.Inventory; import emu.grasscutter.game.mail.Mail; import emu.grasscutter.game.props.ActionReason; import emu.grasscutter.game.props.PlayerProperty; -import emu.grasscutter.game.shop.ShopInfo; import emu.grasscutter.game.shop.ShopLimit; import emu.grasscutter.game.world.Scene; import emu.grasscutter.game.world.World; @@ -37,8 +36,8 @@ import emu.grasscutter.net.proto.PlayerWorldLocationInfoOuterClass; import emu.grasscutter.net.proto.ProfilePictureOuterClass.ProfilePicture; import emu.grasscutter.net.proto.SocialDetailOuterClass.SocialDetail; import emu.grasscutter.net.proto.SocialShowAvatarInfoOuterClass; -import emu.grasscutter.server.event.game.PlayerJoinEvent; -import emu.grasscutter.server.event.game.PlayerQuitEvent; +import emu.grasscutter.server.event.player.PlayerJoinEvent; +import emu.grasscutter.server.event.player.PlayerQuitEvent; import emu.grasscutter.server.event.player.PlayerReceiveMailEvent; import emu.grasscutter.server.game.GameServer; import emu.grasscutter.server.game.GameSession; diff --git a/src/main/java/emu/grasscutter/server/event/game/PlayerJoinEvent.java b/src/main/java/emu/grasscutter/server/event/game/PlayerJoinEvent.java deleted file mode 100644 index 1683acce8..000000000 --- a/src/main/java/emu/grasscutter/server/event/game/PlayerJoinEvent.java +++ /dev/null @@ -1,17 +0,0 @@ -package emu.grasscutter.server.event.game; - -import emu.grasscutter.game.player.Player; -import emu.grasscutter.server.event.Cancellable; -import emu.grasscutter.server.event.types.GameEvent; - -public final class PlayerJoinEvent extends GameEvent implements Cancellable { - private final Player player; - - public PlayerJoinEvent(Player player) { - this.player = player; - } - - public Player getPlayer() { - return this.player; - } -} diff --git a/src/main/java/emu/grasscutter/server/event/game/PlayerQuitEvent.java b/src/main/java/emu/grasscutter/server/event/game/PlayerQuitEvent.java deleted file mode 100644 index df730442d..000000000 --- a/src/main/java/emu/grasscutter/server/event/game/PlayerQuitEvent.java +++ /dev/null @@ -1,16 +0,0 @@ -package emu.grasscutter.server.event.game; - -import emu.grasscutter.game.player.Player; -import emu.grasscutter.server.event.types.GameEvent; - -public final class PlayerQuitEvent extends GameEvent { - private final Player player; - - public PlayerQuitEvent(Player player) { - this.player = player; - } - - public Player getPlayer() { - return this.player; - } -} diff --git a/src/main/java/emu/grasscutter/server/event/player/PlayerJoinEvent.java b/src/main/java/emu/grasscutter/server/event/player/PlayerJoinEvent.java new file mode 100644 index 000000000..59769c1c8 --- /dev/null +++ b/src/main/java/emu/grasscutter/server/event/player/PlayerJoinEvent.java @@ -0,0 +1,11 @@ +package emu.grasscutter.server.event.player; + +import emu.grasscutter.game.player.Player; +import emu.grasscutter.server.event.Cancellable; +import emu.grasscutter.server.event.types.PlayerEvent; + +public final class PlayerJoinEvent extends PlayerEvent implements Cancellable { + public PlayerJoinEvent(Player player) { + super(player); + } +} diff --git a/src/main/java/emu/grasscutter/server/event/player/PlayerQuitEvent.java b/src/main/java/emu/grasscutter/server/event/player/PlayerQuitEvent.java new file mode 100644 index 000000000..4ac1b2f3b --- /dev/null +++ b/src/main/java/emu/grasscutter/server/event/player/PlayerQuitEvent.java @@ -0,0 +1,11 @@ +package emu.grasscutter.server.event.player; + +import emu.grasscutter.game.player.Player; +import emu.grasscutter.server.event.types.GameEvent; +import emu.grasscutter.server.event.types.PlayerEvent; + +public final class PlayerQuitEvent extends PlayerEvent { + public PlayerQuitEvent(Player player) { + super(player); + } +} From d5dee559bcc536472f222d3f97731771142cf854 Mon Sep 17 00:00:00 2001 From: KingRainbow44 Date: Sat, 30 Apr 2022 16:11:35 -0400 Subject: [PATCH 21/21] Update to `1.0.2-dev` --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index f44d47967..0304bf8a6 100644 --- a/build.gradle +++ b/build.gradle @@ -37,7 +37,7 @@ sourceCompatibility = JavaVersion.VERSION_17 targetCompatibility = JavaVersion.VERSION_17 group = 'tech.xigam' -version = '1.0.1-dev' +version = '1.0.2-dev' sourceCompatibility = 17 targetCompatibility = 17