Commit 78b8aff8 authored by Ethan's avatar Ethan 💬
Browse files

add packet listener, improve pom

parent 2154ceea
......@@ -9,10 +9,12 @@
<version>1.0</version>
<packaging>jar</packaging>
<name>MMOActionBar</name>
<description>Allows for advanced controlling of the action bar.</description>
<properties>
<dir>${project.build.directory}</dir>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
......@@ -61,7 +63,7 @@
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<outputDirectory>C:\Users\Ethan\Desktop\TestServer\1.15.2\plugins</outputDirectory>
<outputDirectory>${dir}</outputDirectory>
</configuration>
</plugin>
</plugins>
......
package com.ehhthan.mmoactionbar.comp.placeholderapi;
public class PlaceholderApiComp {
}
package com.ehhthan.mmoactionbar.comp.protocollib;
import com.comphenix.protocol.PacketType.Play.Server;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.ProtocolManager;
import com.comphenix.protocol.events.ListenerPriority;
import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketEvent;
import com.comphenix.protocol.wrappers.EnumWrappers.ChatType;
import com.ehhthan.mmoactionbar.MMOActionBar;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.bungeecord.BungeeComponentSerializer;
import net.kyori.adventure.text.serializer.craftbukkit.MinecraftComponentSerializer;
import net.md_5.bungee.api.chat.BaseComponent;
public class PacketListener {
private final ProtocolManager manager;
public PacketListener() {
this.manager = ProtocolLibrary.getProtocolManager();
registerActionBarListeners();
}
private void registerActionBarListeners() {
/*
* Actionbars can be sent via two different packets so
* we need to listen for both of them.
*/
manager.addPacketListener(new PacketAdapter(MMOActionBar.getInstance(), ListenerPriority.HIGHEST, Server.CHAT) {
@Override
public void onPacketSending(PacketEvent event) {
if ((event.getPacketType() == Server.CHAT) &&
event.getPacket().getChatTypes().read(0) == ChatType.GAME_INFO) { // Game Info is the Actionbar
/*
* In minecraft's infinite wisdom, the components for an action bar are
* either in field 0 in json or field 1 in a BaseComponent[] instance.
*
* These are then deserialized into adventure components.
*/
if (event.getPacket().getModifier().read(0) != null)
rerouteComponent(MinecraftComponentSerializer.get().deserialize(
event.getPacket().getModifier().read(0)), event);
else if (event.getPacket().getModifier().read(1) != null)
rerouteComponent(BungeeComponentSerializer.get().deserialize(
(BaseComponent[])event.getPacket().getModifier().read(1)), event);
}
}
});
manager.addPacketListener(new PacketAdapter(MMOActionBar.getInstance(), ListenerPriority.HIGHEST, Server.TITLE) {
@Override
public void onPacketSending(PacketEvent event) {
if (event.getPacketType() == Server.TITLE &&
event.getPacket().getModifier().read(0).toString().equalsIgnoreCase("ACTIONBAR")) {
rerouteComponent(MinecraftComponentSerializer.get().deserialize(event.getPacket().getModifier().read(1)), event);
}
}
});
}
private void rerouteComponent(Component component, PacketEvent event) {
if (component.insertion() == null || !component.insertion().equalsIgnoreCase("MMOBARS")) {
event.setCancelled(true);
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment