add Packet events

This commit is contained in:
Bixilon 2020-10-04 23:22:16 +02:00
parent 5afb62d1ad
commit 07c1c75e40
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
6 changed files with 135 additions and 28 deletions

View File

@ -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
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
```
Dependency:
```xml
<dependency>
<groupId>de.bixilon.gitlab.bixilon</groupId>
<artifactId>minosoft</artifactId>
<version>master-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>de.bixilon.gitlab.bixilon</groupId>
<artifactId>minosoft</artifactId>
<version>master-SNAPSHOT</version>
</dependency>
```
Create a Main class, here is an example

View File

@ -39,4 +39,12 @@ public class EventListener {
public void onOpenSignEditor(OpenSignEditorEvent event) {
}
@Unsafe
public void onPacketSend(PacketSendEvent event) {
}
@Unsafe
public void onPacketReceive(PacketReceiveEvent event) {
}
}

View File

@ -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 <https://www.gnu.org/licenses/>.
*
* 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);
}
}

View File

@ -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 <https://www.gnu.org/licenses/>.
*
* 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);
}
}

View File

@ -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 <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.modding.event.events;
public @interface Unsafe {
}

View File

@ -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;
}
}