switch from using api to send players to sending a raw packet because its broken

This commit is contained in:
Pyrbu 2023-08-23 00:02:21 +02:00
parent f1c59d0b80
commit 4fbc8c661d
9 changed files with 35 additions and 62 deletions

@ -131,7 +131,6 @@ public class ZNpcsPlus extends JavaPlugin {
EntityPropertyRegistryImpl propertyRegistry = new EntityPropertyRegistryImpl(skinCache); EntityPropertyRegistryImpl propertyRegistry = new EntityPropertyRegistryImpl(skinCache);
MetadataFactory metadataFactory = setupMetadataFactory(); MetadataFactory metadataFactory = setupMetadataFactory();
PacketFactory packetFactory = setupPacketFactory(scheduler, metadataFactory, propertyRegistry); PacketFactory packetFactory = setupPacketFactory(scheduler, metadataFactory, propertyRegistry);
BungeeConnector bungeeConnector = new BungeeConnector(this);
ActionRegistry actionRegistry = new ActionRegistry(); ActionRegistry actionRegistry = new ActionRegistry();
NpcTypeRegistryImpl typeRegistry = new NpcTypeRegistryImpl(); NpcTypeRegistryImpl typeRegistry = new NpcTypeRegistryImpl();
@ -142,18 +141,17 @@ public class ZNpcsPlus extends JavaPlugin {
UserManager userManager = new UserManager(); UserManager userManager = new UserManager();
shutdownTasks.add(userManager::shutdown); shutdownTasks.add(userManager::shutdown);
DataImporterRegistry importerRegistry = new DataImporterRegistry(configManager, adventure, bungeeConnector, DataImporterRegistry importerRegistry = new DataImporterRegistry(configManager, adventure,
scheduler, packetFactory, textSerializer, typeRegistry, getDataFolder().getParentFile(), scheduler, packetFactory, textSerializer, typeRegistry, getDataFolder().getParentFile(),
propertyRegistry, skinCache); propertyRegistry, skinCache);
log(ChatColor.WHITE + " * Registerring components..."); log(ChatColor.WHITE + " * Registerring components...");
typeRegistry.registerDefault(packetEvents, propertyRegistry); typeRegistry.registerDefault(packetEvents, propertyRegistry);
actionRegistry.registerTypes(scheduler, adventure, bungeeConnector, textSerializer); actionRegistry.registerTypes(scheduler, adventure, textSerializer);
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);
getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
registerCommands(npcRegistry, skinCache, adventure, actionRegistry, registerCommands(npcRegistry, skinCache, adventure, actionRegistry,
typeRegistry, propertyRegistry, importerRegistry, configManager); typeRegistry, propertyRegistry, importerRegistry, configManager);

@ -7,7 +7,6 @@ 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;
@ -21,14 +20,14 @@ import java.util.Map;
public class DataImporterRegistry { public class DataImporterRegistry {
private final Map<String, LazyLoader<DataImporter>> importers = new HashMap<>(); private final Map<String, LazyLoader<DataImporter>> importers = new HashMap<>();
public DataImporterRegistry(ConfigManager configManager, BukkitAudiences adventure, BungeeConnector bungeeConnector, 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) { MojangSkinCache skinCache) {
register("znpcs", LazyLoader.of(() -> new ZNpcImporter(configManager, adventure, bungeeConnector, 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"))));
register("znpcsplus_legacy", LazyLoader.of(() -> new ZNpcImporter(configManager, adventure, bungeeConnector, 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"))));
/* register("citizens", LazyLoader.of(() -> new CitizensImporter(configManager, adventure, bungeeConnector, taskScheduler, /* register("citizens", LazyLoader.of(() -> new CitizensImporter(configManager, adventure, bungeeConnector, taskScheduler,
packetFactory, textSerializer, typeRegistry, propertyRegistry, skinCache, new File(pluginsFolder, "Citizens/saves.yml")))); */ packetFactory, textSerializer, typeRegistry, propertyRegistry, skinCache, new File(pluginsFolder, "Citizens/saves.yml")))); */

@ -8,7 +8,6 @@ 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 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;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
@ -21,7 +20,6 @@ import java.util.Collections;
public class CitizensImporter implements DataImporter { public class CitizensImporter implements DataImporter {
private final ConfigManager configManager; private final ConfigManager configManager;
private final BukkitAudiences adventure; private final BukkitAudiences adventure;
private final BungeeConnector bungeeConnector;
private final TaskScheduler scheduler; private final TaskScheduler scheduler;
private final PacketFactory packetFactory; private final PacketFactory packetFactory;
private final LegacyComponentSerializer textSerializer; private final LegacyComponentSerializer textSerializer;
@ -30,13 +28,12 @@ public class CitizensImporter implements DataImporter {
private final MojangSkinCache skinCache; private final MojangSkinCache skinCache;
private final File dataFile; private final File dataFile;
public CitizensImporter(ConfigManager configManager, BukkitAudiences adventure, BungeeConnector bungeeConnector, public CitizensImporter(ConfigManager configManager, BukkitAudiences adventure,
TaskScheduler taskScheduler, PacketFactory packetFactory, LegacyComponentSerializer textSerializer, TaskScheduler taskScheduler, PacketFactory packetFactory, LegacyComponentSerializer textSerializer,
NpcTypeRegistryImpl typeRegistry, EntityPropertyRegistryImpl propertyRegistry, MojangSkinCache skinCache, NpcTypeRegistryImpl typeRegistry, EntityPropertyRegistryImpl propertyRegistry, MojangSkinCache skinCache,
File dataFile) { File dataFile) {
this.configManager = configManager; this.configManager = configManager;
this.adventure = adventure; this.adventure = adventure;
this.bungeeConnector = bungeeConnector;
this.scheduler = taskScheduler; this.scheduler = taskScheduler;
this.packetFactory = packetFactory; this.packetFactory = packetFactory;
this.textSerializer = textSerializer; this.textSerializer = textSerializer;

@ -31,7 +31,6 @@ 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;
@ -47,7 +46,6 @@ import java.util.*;
public class ZNpcImporter implements DataImporter { public class ZNpcImporter implements DataImporter {
private final ConfigManager configManager; private final ConfigManager configManager;
private final BukkitAudiences adventure; private final BukkitAudiences adventure;
private final BungeeConnector bungeeConnector;
private final TaskScheduler taskScheduler; private final TaskScheduler taskScheduler;
private final PacketFactory packetFactory; private final PacketFactory packetFactory;
private final LegacyComponentSerializer textSerializer; private final LegacyComponentSerializer textSerializer;
@ -57,13 +55,12 @@ public class ZNpcImporter implements DataImporter {
private final File dataFile; private final File dataFile;
private final Gson gson; private final Gson gson;
public ZNpcImporter(ConfigManager configManager, BukkitAudiences adventure, BungeeConnector bungeeConnector, 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) {
this.configManager = configManager; this.configManager = configManager;
this.adventure = adventure; this.adventure = adventure;
this.bungeeConnector = bungeeConnector;
this.taskScheduler = taskScheduler; this.taskScheduler = taskScheduler;
this.packetFactory = packetFactory; this.packetFactory = packetFactory;
this.textSerializer = textSerializer; this.textSerializer = textSerializer;
@ -164,7 +161,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(bungeeConnector, parameter, clickType, cooldown * 1000L, 0); return new SwitchServerAction(parameter, clickType, cooldown * 1000L, 0);
} }
throw new IllegalArgumentException("Couldn't adapt znpcs click action: " + type); throw new IllegalArgumentException("Couldn't adapt znpcs click action: " + type);
} }

@ -7,7 +7,6 @@ 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;
@ -23,10 +22,10 @@ public class ActionRegistry {
public ActionRegistry() { public ActionRegistry() {
} }
public void registerTypes(TaskScheduler taskScheduler, BukkitAudiences adventure, BungeeConnector bungeeConnector, LegacyComponentSerializer textSerializer) { public void registerTypes(TaskScheduler taskScheduler, BukkitAudiences adventure, LegacyComponentSerializer textSerializer) {
register(new ConsoleCommandActionType(taskScheduler)); register(new ConsoleCommandActionType(taskScheduler));
register(new PlayerCommandActionType(taskScheduler)); register(new PlayerCommandActionType(taskScheduler));
register(new SwitchServerActionType(bungeeConnector)); register(new SwitchServerActionType());
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.BungeeConnector; import lol.pyr.znpcsplus.util.BungeeUtil;
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;
@ -11,18 +11,16 @@ import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
public class SwitchServerAction extends InteractionActionImpl { public class SwitchServerAction extends InteractionActionImpl {
private final BungeeConnector bungeeConnector;
private final String server; private final String server;
public SwitchServerAction(BungeeConnector bungeeConnector, String server, InteractionType interactionType, long cooldown, long delay) { public SwitchServerAction(String server, InteractionType interactionType, long cooldown, long delay) {
super(cooldown, delay, interactionType); super(cooldown, delay, interactionType);
this.bungeeConnector = bungeeConnector;
this.server = server; this.server = server;
} }
@Override @Override
public void run(Player player) { public void run(Player player) {
bungeeConnector.sendPlayer(player, server); BungeeUtil.connectPlayer(player, server);
} }
@Override @Override

@ -6,7 +6,6 @@ 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;
@ -14,12 +13,6 @@ 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();
@ -29,7 +22,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(bungeeConnector, 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"));
} }
@Override @Override
@ -53,7 +46,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(bungeeConnector, server, type, cooldown, delay); return new SwitchServerAction(server, type, cooldown, delay);
} }
@Override @Override

@ -1,28 +0,0 @@
package lol.pyr.znpcsplus.util;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class BungeeConnector {
private final Plugin plugin;
public BungeeConnector(Plugin plugin) {
this.plugin = plugin;
}
public void sendPlayer(Player player, String server) {
ByteArrayOutputStream b = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(b);
try {
out.writeUTF("Connect");
out.writeUTF(server);
} catch (IOException e) {
e.printStackTrace();
}
player.sendPluginMessage(this.plugin, "BungeeCord", b.toByteArray());
}
}

@ -0,0 +1,20 @@
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.ByteStreams;
import org.bukkit.entity.Player;
public class BungeeUtil {
public static void connectPlayer(Player player, String server) {
PacketEvents.getAPI().getPlayerManager().sendPacket(player, new WrapperPlayServerPluginMessage("BungeeCord", createMessage("Connect", server)));
}
@SuppressWarnings("UnstableApiUsage")
private static byte[] createMessage(String... parts) {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
for (String part : parts) out.writeUTF(part);
return out.toByteArray();
}
}