go back to using the bukkit bungee messaging since the bug with velocity seems to be fixed

This commit is contained in:
Pyrbu 2023-09-13 08:23:54 +02:00
parent 5cc5141105
commit b811526198
7 changed files with 42 additions and 24 deletions

@ -140,17 +140,18 @@ public class ZNpcsPlus extends JavaPlugin {
UserManager userManager = new UserManager(); UserManager userManager = new UserManager();
shutdownTasks.add(userManager::shutdown); shutdownTasks.add(userManager::shutdown);
BungeeConnector bungeeConnector = new BungeeConnector(this);
DataImporterRegistry importerRegistry = new DataImporterRegistry(configManager, adventure, DataImporterRegistry importerRegistry = new DataImporterRegistry(configManager, adventure,
scheduler, packetFactory, textSerializer, typeRegistry, getDataFolder().getParentFile(), scheduler, packetFactory, textSerializer, typeRegistry, getDataFolder().getParentFile(),
propertyRegistry, skinCache, npcRegistry); propertyRegistry, skinCache, npcRegistry, bungeeConnector);
log(ChatColor.WHITE + " * Registerring components..."); log(ChatColor.WHITE + " * Registerring components...");
BungeeUtil.registerChannel(this); bungeeConnector.registerChannel();
shutdownTasks.add(() -> BungeeUtil.unregisterChannel(this)); shutdownTasks.add(bungeeConnector::unregisterChannel);
typeRegistry.registerDefault(packetEvents, propertyRegistry); typeRegistry.registerDefault(packetEvents, propertyRegistry);
actionRegistry.registerTypes(scheduler, adventure, textSerializer); actionRegistry.registerTypes(scheduler, adventure, textSerializer, bungeeConnector);
packetEvents.getEventManager().registerListener(new InteractionPacketListener(userManager, npcRegistry, scheduler), PacketListenerPriority.MONITOR); packetEvents.getEventManager().registerListener(new InteractionPacketListener(userManager, npcRegistry, scheduler), PacketListenerPriority.MONITOR);
new Metrics(this, 18244); new Metrics(this, 18244);
pluginManager.registerEvents(new UserListener(userManager), this); pluginManager.registerEvents(new UserListener(userManager), this);

@ -9,6 +9,7 @@ import lol.pyr.znpcsplus.npc.NpcTypeRegistryImpl;
import lol.pyr.znpcsplus.packets.PacketFactory; import lol.pyr.znpcsplus.packets.PacketFactory;
import lol.pyr.znpcsplus.scheduling.TaskScheduler; import lol.pyr.znpcsplus.scheduling.TaskScheduler;
import lol.pyr.znpcsplus.skin.cache.MojangSkinCache; import lol.pyr.znpcsplus.skin.cache.MojangSkinCache;
import lol.pyr.znpcsplus.util.BungeeConnector;
import lol.pyr.znpcsplus.util.LazyLoader; import lol.pyr.znpcsplus.util.LazyLoader;
import net.kyori.adventure.platform.bukkit.BukkitAudiences; import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
@ -25,12 +26,12 @@ public class DataImporterRegistry {
public DataImporterRegistry(ConfigManager configManager, BukkitAudiences adventure, public DataImporterRegistry(ConfigManager configManager, BukkitAudiences adventure,
TaskScheduler taskScheduler, PacketFactory packetFactory, LegacyComponentSerializer textSerializer, TaskScheduler taskScheduler, PacketFactory packetFactory, LegacyComponentSerializer textSerializer,
NpcTypeRegistryImpl typeRegistry, File pluginsFolder, EntityPropertyRegistryImpl propertyRegistry, NpcTypeRegistryImpl typeRegistry, File pluginsFolder, EntityPropertyRegistryImpl propertyRegistry,
MojangSkinCache skinCache, NpcRegistryImpl npcRegistry) { MojangSkinCache skinCache, NpcRegistryImpl npcRegistry, BungeeConnector bungeeConnector) {
register("znpcs", LazyLoader.of(() -> new ZNpcImporter(configManager, adventure, taskScheduler, register("znpcs", LazyLoader.of(() -> new ZNpcImporter(configManager, adventure, taskScheduler,
packetFactory, textSerializer, typeRegistry, propertyRegistry, skinCache, new File(pluginsFolder, "ServersNPC/data.json")))); packetFactory, textSerializer, typeRegistry, propertyRegistry, skinCache, new File(pluginsFolder, "ServersNPC/data.json"), bungeeConnector)));
register("znpcsplus_legacy", LazyLoader.of(() -> new ZNpcImporter(configManager, adventure, taskScheduler, register("znpcsplus_legacy", LazyLoader.of(() -> new ZNpcImporter(configManager, adventure, taskScheduler,
packetFactory, textSerializer, typeRegistry, propertyRegistry, skinCache, new File(pluginsFolder, "ZNPCsPlusLegacy/data.json")))); packetFactory, textSerializer, typeRegistry, propertyRegistry, skinCache, new File(pluginsFolder, "ZNPCsPlusLegacy/data.json"), bungeeConnector)));
register("citizens", LazyLoader.of(() -> new CitizensImporter(configManager, adventure, taskScheduler, register("citizens", LazyLoader.of(() -> new CitizensImporter(configManager, adventure, taskScheduler,
packetFactory, textSerializer, typeRegistry, propertyRegistry, skinCache, new File(pluginsFolder, "Citizens/saves.yml"), npcRegistry))); packetFactory, textSerializer, typeRegistry, propertyRegistry, skinCache, new File(pluginsFolder, "Citizens/saves.yml"), npcRegistry)));
} }

@ -29,6 +29,7 @@ import lol.pyr.znpcsplus.skin.Skin;
import lol.pyr.znpcsplus.skin.cache.MojangSkinCache; import lol.pyr.znpcsplus.skin.cache.MojangSkinCache;
import lol.pyr.znpcsplus.skin.descriptor.FetchingDescriptor; import lol.pyr.znpcsplus.skin.descriptor.FetchingDescriptor;
import lol.pyr.znpcsplus.skin.descriptor.PrefetchedDescriptor; import lol.pyr.znpcsplus.skin.descriptor.PrefetchedDescriptor;
import lol.pyr.znpcsplus.util.BungeeConnector;
import lol.pyr.znpcsplus.util.ItemSerializationUtil; import lol.pyr.znpcsplus.util.ItemSerializationUtil;
import lol.pyr.znpcsplus.util.NpcLocation; import lol.pyr.znpcsplus.util.NpcLocation;
import net.kyori.adventure.platform.bukkit.BukkitAudiences; import net.kyori.adventure.platform.bukkit.BukkitAudiences;
@ -53,10 +54,11 @@ public class ZNpcImporter implements DataImporter {
private final MojangSkinCache skinCache; private final MojangSkinCache skinCache;
private final File dataFile; private final File dataFile;
private final Gson gson; private final Gson gson;
private final BungeeConnector bungeeConnector;
public ZNpcImporter(ConfigManager configManager, BukkitAudiences adventure, public ZNpcImporter(ConfigManager configManager, BukkitAudiences adventure,
TaskScheduler taskScheduler, PacketFactory packetFactory, LegacyComponentSerializer textSerializer, TaskScheduler taskScheduler, PacketFactory packetFactory, LegacyComponentSerializer textSerializer,
NpcTypeRegistryImpl typeRegistry, EntityPropertyRegistryImpl propertyRegistry, MojangSkinCache skinCache, File dataFile) { NpcTypeRegistryImpl typeRegistry, EntityPropertyRegistryImpl propertyRegistry, MojangSkinCache skinCache, File dataFile, BungeeConnector bungeeConnector) {
this.configManager = configManager; this.configManager = configManager;
this.adventure = adventure; this.adventure = adventure;
@ -67,6 +69,7 @@ public class ZNpcImporter implements DataImporter {
this.propertyRegistry = propertyRegistry; this.propertyRegistry = propertyRegistry;
this.skinCache = skinCache; this.skinCache = skinCache;
this.dataFile = dataFile; this.dataFile = dataFile;
this.bungeeConnector = bungeeConnector;
gson = new GsonBuilder() gson = new GsonBuilder()
.create(); .create();
} }
@ -161,7 +164,7 @@ public class ZNpcImporter implements DataImporter {
case "message": case "message":
return new MessageAction(adventure, parameter, clickType, textSerializer, cooldown * 1000L, 0); return new MessageAction(adventure, parameter, clickType, textSerializer, cooldown * 1000L, 0);
case "server": case "server":
return new SwitchServerAction(parameter, clickType, cooldown * 1000L, 0); return new SwitchServerAction(parameter, clickType, cooldown * 1000L, 0, bungeeConnector);
} }
throw new IllegalArgumentException("Couldn't adapt znpcs click action: " + type); throw new IllegalArgumentException("Couldn't adapt znpcs click action: " + type);
} }

@ -7,6 +7,7 @@ import lol.pyr.znpcsplus.interaction.playerchat.PlayerChatActionType;
import lol.pyr.znpcsplus.interaction.playercommand.PlayerCommandActionType; import lol.pyr.znpcsplus.interaction.playercommand.PlayerCommandActionType;
import lol.pyr.znpcsplus.interaction.switchserver.SwitchServerActionType; import lol.pyr.znpcsplus.interaction.switchserver.SwitchServerActionType;
import lol.pyr.znpcsplus.scheduling.TaskScheduler; import lol.pyr.znpcsplus.scheduling.TaskScheduler;
import lol.pyr.znpcsplus.util.BungeeConnector;
import net.kyori.adventure.platform.bukkit.BukkitAudiences; import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
@ -22,10 +23,10 @@ public class ActionRegistry {
public ActionRegistry() { public ActionRegistry() {
} }
public void registerTypes(TaskScheduler taskScheduler, BukkitAudiences adventure, LegacyComponentSerializer textSerializer) { public void registerTypes(TaskScheduler taskScheduler, BukkitAudiences adventure, LegacyComponentSerializer textSerializer, BungeeConnector bungeeConnector) {
register(new ConsoleCommandActionType(taskScheduler)); register(new ConsoleCommandActionType(taskScheduler));
register(new PlayerCommandActionType(taskScheduler)); register(new PlayerCommandActionType(taskScheduler));
register(new SwitchServerActionType()); register(new SwitchServerActionType(bungeeConnector));
register(new MessageActionType(adventure, textSerializer)); register(new MessageActionType(adventure, textSerializer));
register(new PlayerChatActionType(taskScheduler)); register(new PlayerChatActionType(taskScheduler));
} }

@ -3,7 +3,7 @@ package lol.pyr.znpcsplus.interaction.switchserver;
import lol.pyr.director.adventure.command.CommandContext; import lol.pyr.director.adventure.command.CommandContext;
import lol.pyr.znpcsplus.api.interaction.InteractionType; import lol.pyr.znpcsplus.api.interaction.InteractionType;
import lol.pyr.znpcsplus.interaction.InteractionActionImpl; import lol.pyr.znpcsplus.interaction.InteractionActionImpl;
import lol.pyr.znpcsplus.util.BungeeUtil; import lol.pyr.znpcsplus.util.BungeeConnector;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.ClickEvent; import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.event.HoverEvent; import net.kyori.adventure.text.event.HoverEvent;
@ -12,15 +12,17 @@ import org.bukkit.entity.Player;
public class SwitchServerAction extends InteractionActionImpl { public class SwitchServerAction extends InteractionActionImpl {
private final String server; private final String server;
private final BungeeConnector bungeeConnector;
public SwitchServerAction(String server, InteractionType interactionType, long cooldown, long delay) { public SwitchServerAction(String server, InteractionType interactionType, long cooldown, long delay, BungeeConnector bungeeConnector) {
super(cooldown, delay, interactionType); super(cooldown, delay, interactionType);
this.server = server; this.server = server;
this.bungeeConnector = bungeeConnector;
} }
@Override @Override
public void run(Player player) { public void run(Player player) {
BungeeUtil.connectPlayer(player, server); bungeeConnector.connectPlayer(player, server);
} }
@Override @Override

@ -6,6 +6,7 @@ import lol.pyr.znpcsplus.api.interaction.InteractionType;
import lol.pyr.znpcsplus.interaction.InteractionActionImpl; import lol.pyr.znpcsplus.interaction.InteractionActionImpl;
import lol.pyr.znpcsplus.interaction.InteractionActionType; import lol.pyr.znpcsplus.interaction.InteractionActionType;
import lol.pyr.znpcsplus.interaction.InteractionCommandHandler; import lol.pyr.znpcsplus.interaction.InteractionCommandHandler;
import lol.pyr.znpcsplus.util.BungeeConnector;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Base64; import java.util.Base64;
@ -13,6 +14,12 @@ import java.util.Collections;
import java.util.List; import java.util.List;
public class SwitchServerActionType implements InteractionActionType<SwitchServerAction>, InteractionCommandHandler { public class SwitchServerActionType implements InteractionActionType<SwitchServerAction>, InteractionCommandHandler {
private final BungeeConnector bungeeConnector;
public SwitchServerActionType(BungeeConnector bungeeConnector) {
this.bungeeConnector = bungeeConnector;
}
@Override @Override
public String serialize(SwitchServerAction obj) { public String serialize(SwitchServerAction obj) {
return Base64.getEncoder().encodeToString(obj.getServer().getBytes(StandardCharsets.UTF_8)) + ";" + obj.getCooldown() + ";" + obj.getInteractionType().name() + ";" + obj.getDelay(); return Base64.getEncoder().encodeToString(obj.getServer().getBytes(StandardCharsets.UTF_8)) + ";" + obj.getCooldown() + ";" + obj.getInteractionType().name() + ";" + obj.getDelay();
@ -22,7 +29,7 @@ public class SwitchServerActionType implements InteractionActionType<SwitchServe
public SwitchServerAction deserialize(String str) { public SwitchServerAction deserialize(String str) {
String[] split = str.split(";"); String[] split = str.split(";");
InteractionType type = split.length > 2 ? InteractionType.valueOf(split[2]) : InteractionType.ANY_CLICK; InteractionType type = split.length > 2 ? InteractionType.valueOf(split[2]) : InteractionType.ANY_CLICK;
return new SwitchServerAction(new String(Base64.getDecoder().decode(split[0]), StandardCharsets.UTF_8), type, Long.parseLong(split[1]), Long.parseLong(split.length > 3 ? split[3] : "0")); return new SwitchServerAction(new String(Base64.getDecoder().decode(split[0]), StandardCharsets.UTF_8), type, Long.parseLong(split[1]), Long.parseLong(split.length > 3 ? split[3] : "0"), bungeeConnector);
} }
@Override @Override
@ -46,7 +53,7 @@ public class SwitchServerActionType implements InteractionActionType<SwitchServe
long cooldown = (long) (context.parse(Double.class) * 1000D); long cooldown = (long) (context.parse(Double.class) * 1000D);
long delay = (long) (context.parse(Integer.class) * 1D); long delay = (long) (context.parse(Integer.class) * 1D);
String server = context.dumpAllArgs(); String server = context.dumpAllArgs();
return new SwitchServerAction(server, type, cooldown, delay); return new SwitchServerAction(server, type, cooldown, delay, bungeeConnector);
} }
@Override @Override

@ -1,32 +1,35 @@
package lol.pyr.znpcsplus.util; package lol.pyr.znpcsplus.util;
import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerPluginMessage;
import com.google.common.io.ByteArrayDataOutput; import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams; import com.google.common.io.ByteStreams;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
public class BungeeUtil { public class BungeeConnector {
private final static String CHANNEL_NAME = "BungeeCord"; private final static String CHANNEL_NAME = "BungeeCord";
private final Plugin plugin;
public static void connectPlayer(Player player, String server) { public BungeeConnector(Plugin plugin) {
PacketEvents.getAPI().getPlayerManager().sendPacket(player, new WrapperPlayServerPluginMessage(CHANNEL_NAME, createMessage("Connect", server))); this.plugin = plugin;
}
public void connectPlayer(Player player, String server) {
player.sendPluginMessage(plugin, CHANNEL_NAME, createMessage("Connect", server));
} }
@SuppressWarnings("UnstableApiUsage") @SuppressWarnings("UnstableApiUsage")
private static byte[] createMessage(String... parts) { private byte[] createMessage(String... parts) {
ByteArrayDataOutput out = ByteStreams.newDataOutput(); ByteArrayDataOutput out = ByteStreams.newDataOutput();
for (String part : parts) out.writeUTF(part); for (String part : parts) out.writeUTF(part);
return out.toByteArray(); return out.toByteArray();
} }
public static void registerChannel(Plugin plugin) { public void registerChannel() {
Bukkit.getMessenger().registerOutgoingPluginChannel(plugin, CHANNEL_NAME); Bukkit.getMessenger().registerOutgoingPluginChannel(plugin, CHANNEL_NAME);
} }
public static void unregisterChannel(Plugin plugin) { public void unregisterChannel() {
Bukkit.getMessenger().unregisterOutgoingPluginChannel(plugin, CHANNEL_NAME); Bukkit.getMessenger().unregisterOutgoingPluginChannel(plugin, CHANNEL_NAME);
} }
} }