make it all uwu pretty... and also fix versions below 1.17 but who cares about that

This commit is contained in:
Pyrbu 2023-04-20 23:41:58 +01:00
parent d9bde3d442
commit 86b653e2bf
5 changed files with 58 additions and 41 deletions

@ -23,6 +23,7 @@ public abstract class BaseReflection <T> {
for (String classes : className) { for (String classes : className) {
try { try {
this.BUILDER_CLASS = Class.forName(classes); this.BUILDER_CLASS = Class.forName(classes);
break;
} catch (ClassNotFoundException ignored) { } catch (ClassNotFoundException ignored) {
} }
} }

@ -1,6 +1,7 @@
package io.github.znetworkw.znpcservers.reflection; package io.github.znetworkw.znpcservers.reflection;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import io.github.znetworkw.znpcservers.utility.Utils;
import java.util.ArrayList; import java.util.ArrayList;
@ -25,12 +26,12 @@ public class ReflectionBuilder {
} }
public ReflectionBuilder withClassName(String className) { public ReflectionBuilder withClassName(String className) {
this.className.add(ReflectionPackage.join(reflectionPackage, additionalData, className)); this.className.add(ReflectionPackage.join(reflectionPackage, Utils.versionNewer(17) ? additionalData : "", className));
return this; return this;
} }
public ReflectionBuilder withClassName(Class<?> clazz) { public ReflectionBuilder withClassName(Class<?> clazz) {
className.add(clazz.getName()); if (clazz != null) className.add(clazz.getName());
return this; return this;
} }

@ -7,22 +7,24 @@ import java.util.Objects;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class ReflectionPackage { public class ReflectionPackage {
public static final String BUKKIT = fixBasePackage("org.bukkit.craftbukkit." + Utils.getBukkitPackage()); private static final boolean flattened = !Utils.versionNewer(17);
public static final String MINECRAFT = fixBasePackage("net.minecraft");
public static final String NETWORK = join(MINECRAFT, "network"); public static final String BUKKIT = "org.bukkit.craftbukkit." + Utils.getBukkitPackage();
public static final String PROTOCOL = join(MINECRAFT, "network.protocol"); public static final String MINECRAFT = join("net.minecraft", flattened ? "server." + Utils.getBukkitPackage() : "");
public static final String CHAT = join(MINECRAFT, "network.chat");
public static final String PACKET = join(MINECRAFT, "network.protocol.game"); public static final String NETWORK = flattened ? MINECRAFT : join(MINECRAFT, "network");
public static final String SYNCHER = join(MINECRAFT, "network.syncher"); public static final String PROTOCOL = flattened ? MINECRAFT : join(MINECRAFT, "network.protocol");
public static final String ENTITY = join(MINECRAFT, "world.entity"); public static final String CHAT = flattened ? MINECRAFT : join(MINECRAFT, "network.chat");
public static final String WORLD_ENTITY_PLAYER = join(MINECRAFT, "world.entity.player"); public static final String PACKET = flattened ? MINECRAFT : join(MINECRAFT, "network.protocol.game");
public static final String ITEM = join(MINECRAFT, "world.item"); public static final String SYNCHER = flattened ? MINECRAFT : join(MINECRAFT, "network.syncher");
public static final String WORLD_LEVEL = join(MINECRAFT, "world.level"); public static final String ENTITY = flattened ? MINECRAFT : join(MINECRAFT, "world.entity");
public static final String WORLD_SCORES = join(MINECRAFT, "world.scores"); public static final String WORLD_ENTITY_PLAYER = flattened ? MINECRAFT : join(MINECRAFT, "world.entity.player");
public static final String SERVER_LEVEL = join(MINECRAFT, "server.level"); public static final String ITEM = flattened ? MINECRAFT : join(MINECRAFT, "world.item");
public static final String SERVER_NETWORK = join(MINECRAFT, "server.network"); public static final String WORLD_LEVEL = flattened ? MINECRAFT : join(MINECRAFT, "world.level");
public static final String SERVER = join(MINECRAFT, "server"); public static final String WORLD_SCORES = flattened ? MINECRAFT : join(MINECRAFT, "world.scores");
public static final String SERVER_LEVEL = flattened ? MINECRAFT : join(MINECRAFT, "server.level");
public static final String SERVER_NETWORK = flattened ? MINECRAFT : join(MINECRAFT, "server.network");
public static final String SERVER = flattened ? MINECRAFT : join(MINECRAFT, "server");
public static String join(String... parts) { public static String join(String... parts) {
return Arrays.stream(parts) return Arrays.stream(parts)
@ -30,8 +32,4 @@ public class ReflectionPackage {
.filter(p -> p.length() != 0) .filter(p -> p.length() != 0)
.collect(Collectors.joining(".")); .collect(Collectors.joining("."));
} }
private static String fixBasePackage(String packageName) {
return Utils.versionNewer(17) ? packageName : (packageName + (packageName.contains("minecraft") ? (".server." + Utils.getBukkitPackage()) : ""));
}
} }

@ -47,7 +47,7 @@ public class ZUser {
} catch (IllegalAccessException | InvocationTargetException e) { } catch (IllegalAccessException | InvocationTargetException e) {
throw new IllegalStateException("can't create user for player " + uuid.toString(), e.getCause()); throw new IllegalStateException("can't create user for player " + uuid.toString(), e.getCause());
} }
if (!tryRegisterChannel()) ZNPCsPlus.SCHEDULER.runTaskTimer(new ChannelRegistrationFallbackTask(this), 3); if (tryRegisterChannel() != null) ZNPCsPlus.SCHEDULER.runTaskTimer(new ChannelRegistrationFallbackTask(this), 3);
} }
private static class ChannelRegistrationFallbackTask extends BukkitRunnable { private static class ChannelRegistrationFallbackTask extends BukkitRunnable {
@ -62,7 +62,8 @@ public class ZUser {
@Override @Override
public void run() { public void run() {
if (!player.isOnline() || user.tryRegisterChannel()) { Exception ex = user.tryRegisterChannel();
if (!player.isOnline() || ex == null) {
cancel(); cancel();
return; return;
} }
@ -72,19 +73,20 @@ public class ZUser {
.append(Component.text("Couldn't inject interaction detector to channel", NamedTextColor.WHITE)).appendNewline() .append(Component.text("Couldn't inject interaction detector to channel", NamedTextColor.WHITE)).appendNewline()
.append(Component.text("Please report this at https://github.com/Pyrbu/ZNPCsPlus", NamedTextColor.WHITE))); .append(Component.text("Please report this at https://github.com/Pyrbu/ZNPCsPlus", NamedTextColor.WHITE)));
ZNPCsPlus.LOGGER.severe("Couldn't inject interaction detector to channel for player " + player.getName() + " (" + player.getUniqueId() + ")"); ZNPCsPlus.LOGGER.severe("Couldn't inject interaction detector to channel for player " + player.getName() + " (" + player.getUniqueId() + ")");
ex.printStackTrace();
} }
} }
private boolean tryRegisterChannel() { private Exception tryRegisterChannel() {
try { try {
Channel channel = (Channel) ReflectionCache.CHANNEL_FIELD.get().get(ReflectionCache.NETWORK_MANAGER_FIELD.get().get(this.playerConnection)); Channel channel = (Channel) ReflectionCache.CHANNEL_FIELD.get().get(ReflectionCache.NETWORK_MANAGER_FIELD.get().get(this.playerConnection));
if (channel.pipeline().names().contains("npc_interact")) channel.pipeline().remove("npc_interact"); if (channel.pipeline().names().contains("npc_interact")) channel.pipeline().remove("npc_interact");
channel.pipeline().addAfter("decoder", "npc_interact", new ZNPCSocketDecoder()); channel.pipeline().addAfter("decoder", "npc_interact", new ZNPCSocketDecoder());
return true; return null;
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
throw new RuntimeException("illegal access exception while trying to register npc_interact channel"); throw new RuntimeException("illegal access exception while trying to register npc_interact channel");
} catch (NoSuchElementException e) { } catch (NoSuchElementException e) {
return false; return e;
} }
} }

@ -22,6 +22,7 @@ import io.github.znetworkw.znpcservers.utility.location.ZLocation;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.bstats.bukkit.Metrics; import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
@ -70,30 +71,57 @@ public class ZNPCsPlus extends JavaPlugin {
PATH_FOLDER = new File(PLUGIN_FOLDER, "paths"); PATH_FOLDER = new File(PLUGIN_FOLDER, "paths");
} }
@SuppressWarnings("deprecation")
@Override @Override
public void onEnable() { public void onEnable() {
Logger serverLogger = getServer().getLogger();
serverLogger.info(ChatColor.YELLOW + " ___ __ __ __");
serverLogger.info(ChatColor.YELLOW + " _/ |\\ | |__) | (__` " + ChatColor.GOLD + "__|__ " + ChatColor.YELLOW + getDescription().getName() + " " + ChatColor.GOLD + "v" + getDescription().getVersion());
serverLogger.info(ChatColor.YELLOW + " /__ | \\| | |__ .__) " + ChatColor.GOLD + " | " + ChatColor.GRAY + "Maintained with " + ChatColor.RED + "\u2764 " + ChatColor.GRAY + " by Pyr#6969");
serverLogger.info("");
if (Bukkit.getPluginManager().isPluginEnabled("ServersNPC")) { if (Bukkit.getPluginManager().isPluginEnabled("ServersNPC")) {
LOGGER.severe("Detected old version of ZNPCs! Disabling the plugin..."); serverLogger.info(ChatColor.DARK_RED + " * Detected old version of ZNPCs! Disabling the plugin...");
serverLogger.info("");
Bukkit.getPluginManager().disablePlugin(this); Bukkit.getPluginManager().disablePlugin(this);
return; return;
} }
checkOldFolder(); long before = System.currentTimeMillis();
File oldFolder = new File(PLUGIN_FOLDER.getParent(), "ServersNPC");
if (!PLUGIN_FOLDER.exists() && oldFolder.exists()) {
serverLogger.info(ChatColor.WHITE + " * Converting old ZNPCs files...");
try {
FileUtils.moveDirectory(oldFolder, PLUGIN_FOLDER);
} catch (IOException e) {
serverLogger.info(ChatColor.RED + " * Failed to convert old ZNPCs files" + (e.getMessage() == null ? "" : " due to " + e.getMessage()));
}
}
PLUGIN_FOLDER.mkdirs(); PLUGIN_FOLDER.mkdirs();
PATH_FOLDER.mkdirs(); PATH_FOLDER.mkdirs();
serverLogger.info(ChatColor.WHITE + " * Loading paths...");
loadAllPaths(); loadAllPaths();
serverLogger.info(ChatColor.WHITE + " * Registering components...");
getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord"); getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
new Metrics(this, PLUGIN_ID); new Metrics(this, PLUGIN_ID);
new DefaultCommand(); new DefaultCommand();
SCHEDULER = new SchedulerUtils(this); SCHEDULER = new SchedulerUtils(this);
BUNGEE_UTILS = new BungeeUtils(this); BUNGEE_UTILS = new BungeeUtils(this);
Bukkit.getOnlinePlayers().forEach(ZUser::find); Bukkit.getOnlinePlayers().forEach(ZUser::find);
serverLogger.info(ChatColor.WHITE + " * Starting tasks...");
new NPCPositionTask(this); new NPCPositionTask(this);
new NPCVisibilityTask(this); new NPCVisibilityTask(this);
new NPCSaveTask(this, ConfigurationConstants.SAVE_DELAY); new NPCSaveTask(this, ConfigurationConstants.SAVE_DELAY);
new PlayerListener(this); new PlayerListener(this);
new InventoryListener(this); new InventoryListener(this);
enabled = true; enabled = true;
serverLogger.info(ChatColor.WHITE + " * Loading complete! (" + (System.currentTimeMillis() - before) + "ms)");
serverLogger.info("");
} }
@Override @Override
@ -112,17 +140,4 @@ public class ZNPCsPlus extends JavaPlugin {
abstractTypeWriter.load(); abstractTypeWriter.load();
} }
} }
private void checkOldFolder() {
if (PLUGIN_FOLDER.exists()) return;
File oldFolder = new File(PLUGIN_FOLDER.getParent(), "ServersNPC");
if (!oldFolder.exists()) return;
LOGGER.info("Detected old ZNPCs files and no new ones present, converting...");
try {
FileUtils.moveDirectory(oldFolder, PLUGIN_FOLDER);
} catch (IOException e) {
LOGGER.severe("Failed to convert old ZNPCs files:");
e.printStackTrace();
}
}
} }