From 07c1c75e40ef3f91bbb41e77bf95b664a06ef027 Mon Sep 17 00:00:00 2001 From: Bixilon Date: Sun, 4 Oct 2020 23:22:16 +0200 Subject: [PATCH] add Packet events --- doc/Modding.md | 26 ++++++------ .../minosoft/modding/event/EventListener.java | 8 ++++ .../event/events/PacketReceiveEvent.java | 36 +++++++++++++++++ .../modding/event/events/PacketSendEvent.java | 36 +++++++++++++++++ .../minosoft/modding/event/events/Unsafe.java | 17 ++++++++ .../minosoft/protocol/network/Connection.java | 40 ++++++++++++------- 6 files changed, 135 insertions(+), 28 deletions(-) create mode 100644 src/main/java/de/bixilon/minosoft/modding/event/events/PacketReceiveEvent.java create mode 100644 src/main/java/de/bixilon/minosoft/modding/event/events/PacketSendEvent.java create mode 100644 src/main/java/de/bixilon/minosoft/modding/event/events/Unsafe.java diff --git a/doc/Modding.md b/doc/Modding.md index bfec2dda4..718d10284 100644 --- a/doc/Modding.md +++ b/doc/Modding.md @@ -47,8 +47,8 @@ In your jar file (the mod) must be a file called `mod.json`. - `mainClass` the Main class of your mod (self explaining). The main class needs to extent the abstract class `MinosoftMod`. **Required** - `loading` Loading attributes. **Optional** - `priority` should the mod be loaded at the beginning or at the end. Possible values are `LOWEST`, `LOW`, `NORMAL`, `HIGH`, `HIGHEST` **Optional** -- `dependencies` Used if you need an other mod to work **Optional** - - `hard` These mods are **needed** to work. If the loading fails, your mod is not getting loaded and an warning message is being displayed. **Optional** +- `dependencies` Used if you need another mod to work **Optional** + - `hard` These mods are **needed** to work. If the loading fails, your mod is not getting loaded, and a warning message is being displayed. **Optional** - `soft` These mods are **optional** to work. Both use the following format: **Optional** - `uuid` the uuid of the mod to load. **Required** - `version` Specifies the version you need to load. **Optional** @@ -74,20 +74,20 @@ If your start function needs much time, you can set the loading priority in the Add Minosoft to your maven dependencies with Repository: ```xml - - - jitpack.io - https://jitpack.io - - + + + jitpack.io + https://jitpack.io + + ``` Dependency: ```xml - - de.bixilon.gitlab.bixilon - minosoft - master-SNAPSHOT - + + de.bixilon.gitlab.bixilon + minosoft + master-SNAPSHOT + ``` Create a Main class, here is an example diff --git a/src/main/java/de/bixilon/minosoft/modding/event/EventListener.java b/src/main/java/de/bixilon/minosoft/modding/event/EventListener.java index 88d6e4b09..3814c924a 100644 --- a/src/main/java/de/bixilon/minosoft/modding/event/EventListener.java +++ b/src/main/java/de/bixilon/minosoft/modding/event/EventListener.java @@ -39,4 +39,12 @@ public class EventListener { public void onOpenSignEditor(OpenSignEditorEvent event) { } + + @Unsafe + public void onPacketSend(PacketSendEvent event) { + } + + @Unsafe + public void onPacketReceive(PacketReceiveEvent event) { + } } diff --git a/src/main/java/de/bixilon/minosoft/modding/event/events/PacketReceiveEvent.java b/src/main/java/de/bixilon/minosoft/modding/event/events/PacketReceiveEvent.java new file mode 100644 index 000000000..7ddf2cecc --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/modding/event/events/PacketReceiveEvent.java @@ -0,0 +1,36 @@ +/* + * Codename Minosoft + * Copyright (C) 2020 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.modding.event.events; + +import de.bixilon.minosoft.modding.event.EventListener; +import de.bixilon.minosoft.protocol.network.Connection; +import de.bixilon.minosoft.protocol.packets.ClientboundPacket; + +public class PacketReceiveEvent extends Event { + private final ClientboundPacket packet; + + public PacketReceiveEvent(Connection connection, ClientboundPacket packet) { + super(connection); + this.packet = packet; + } + + public ClientboundPacket getPacket() { + return packet; + } + + @Override + public void handle(EventListener listener) { + listener.onPacketReceive(this); + } +} diff --git a/src/main/java/de/bixilon/minosoft/modding/event/events/PacketSendEvent.java b/src/main/java/de/bixilon/minosoft/modding/event/events/PacketSendEvent.java new file mode 100644 index 000000000..75dea29fb --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/modding/event/events/PacketSendEvent.java @@ -0,0 +1,36 @@ +/* + * Codename Minosoft + * Copyright (C) 2020 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.modding.event.events; + +import de.bixilon.minosoft.modding.event.EventListener; +import de.bixilon.minosoft.protocol.network.Connection; +import de.bixilon.minosoft.protocol.packets.ServerboundPacket; + +public class PacketSendEvent extends Event { + private final ServerboundPacket packet; + + public PacketSendEvent(Connection connection, ServerboundPacket packet) { + super(connection); + this.packet = packet; + } + + public ServerboundPacket getPacket() { + return packet; + } + + @Override + public void handle(EventListener listener) { + listener.onPacketSend(this); + } +} diff --git a/src/main/java/de/bixilon/minosoft/modding/event/events/Unsafe.java b/src/main/java/de/bixilon/minosoft/modding/event/events/Unsafe.java new file mode 100644 index 000000000..966889e74 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/modding/event/events/Unsafe.java @@ -0,0 +1,17 @@ +/* + * Codename Minosoft + * Copyright (C) 2020 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.modding.event.events; + +public @interface Unsafe { +} diff --git a/src/main/java/de/bixilon/minosoft/protocol/network/Connection.java b/src/main/java/de/bixilon/minosoft/protocol/network/Connection.java index 3d34b83fe..af54e3917 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/network/Connection.java +++ b/src/main/java/de/bixilon/minosoft/protocol/network/Connection.java @@ -29,6 +29,8 @@ import de.bixilon.minosoft.modding.event.EventListener; import de.bixilon.minosoft.modding.event.EventManager; import de.bixilon.minosoft.modding.event.events.CancelableEvent; import de.bixilon.minosoft.modding.event.events.Event; +import de.bixilon.minosoft.modding.event.events.PacketReceiveEvent; +import de.bixilon.minosoft.modding.event.events.PacketSendEvent; import de.bixilon.minosoft.ping.ServerListPing; import de.bixilon.minosoft.protocol.modding.channels.DefaultPluginChannels; import de.bixilon.minosoft.protocol.modding.channels.PluginChannelHandler; @@ -189,8 +191,25 @@ public class Connection { return player; } - public void sendPacket(ServerboundPacket p) { - network.sendPacket(p); + public void sendPacket(ServerboundPacket packet) { + PacketSendEvent event = new PacketSendEvent(this, packet); + if (fireEvent(event)) { + return; + } + network.sendPacket(packet); + } + + /** + * @param event The event to fire + * @return if the event has been cancelled or not + */ + public boolean fireEvent(Event event) { + Minosoft.eventManagers.forEach((eventManager -> eventManager.getGlobalEventListeners().forEach(event::handle))); + eventListeners.forEach(event::handle); + if (event instanceof CancelableEvent) { + return ((CancelableEvent) event).isCancelled(); + } + return false; } void startHandlingThread() { @@ -204,6 +223,10 @@ public class Connection { } try { packet.log(); + PacketReceiveEvent event = new PacketReceiveEvent(this, packet); + if (fireEvent(event)) { + continue; + } packet.handle(getHandler()); } catch (Exception e) { if (Log.getLevel().ordinal() >= LogLevels.DEBUG.ordinal()) { @@ -426,17 +449,4 @@ public class Connection { public void unregisterEvents(EventManager... eventManagers) { this.eventManagers.removeAll(Arrays.asList(eventManagers)); } - - /** - * @param event The event to fire - * @return if the event has been cancelled or not - */ - public boolean fireEvent(Event event) { - Minosoft.eventManagers.forEach((eventManager -> eventManager.getGlobalEventListeners().forEach(event::handle))); - eventListeners.forEach(event::handle); - if (event instanceof CancelableEvent) { - return ((CancelableEvent) event).isCancelled(); - } - return false; - } }