minor refactoring

This commit is contained in:
Pyrbu 2023-05-03 16:49:44 +01:00
parent 1f5bea5f8e
commit f993a521ed
6 changed files with 15 additions and 20 deletions

@ -1,7 +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 io.github.znetworkw.znpcservers.utility.VersionUtil;
import java.util.ArrayList; import java.util.ArrayList;
@ -32,7 +32,7 @@ public class ReflectionBuilder {
} }
public ReflectionBuilder withClassName(String className) { public ReflectionBuilder withClassName(String className) {
this.className.add(ReflectionPackage.join(reflectionPackage, Utils.versionNewer(17) ? additionalData : "", className)); this.className.add(ReflectionPackage.join(reflectionPackage, VersionUtil.isNewerThan(17) ? additionalData : "", className));
return this; return this;
} }

@ -1,6 +1,6 @@
package io.github.znetworkw.znpcservers.reflection; package io.github.znetworkw.znpcservers.reflection;
import io.github.znetworkw.znpcservers.utility.Utils; import io.github.znetworkw.znpcservers.utility.VersionUtil;
import lol.pyr.znpcsplus.ZNPCsPlus; import lol.pyr.znpcsplus.ZNPCsPlus;
import java.util.ArrayList; import java.util.ArrayList;
@ -38,7 +38,7 @@ public abstract class ReflectionLazyLoader<T> {
warn(getClass().getSimpleName() + " failed!"); warn(getClass().getSimpleName() + " failed!");
warn("Class Names: " + possibleClassNames); warn("Class Names: " + possibleClassNames);
warn("Reflection Type: " + getClass().getCanonicalName()); warn("Reflection Type: " + getClass().getCanonicalName());
warn("Bukkit Version: " + Utils.BUKKIT_VERSION + " (" + Utils.getBukkitPackage() + ")"); warn("Bukkit Version: " + VersionUtil.BUKKIT_VERSION + " (" + VersionUtil.getBukkitPackage() + ")");
printDebugInfo(this::warn); printDebugInfo(this::warn);
warn("Exception:"); warn("Exception:");
throwable.printStackTrace(); throwable.printStackTrace();

@ -1,6 +1,6 @@
package io.github.znetworkw.znpcservers.reflection; package io.github.znetworkw.znpcservers.reflection;
import io.github.znetworkw.znpcservers.utility.Utils; import io.github.znetworkw.znpcservers.utility.VersionUtil;
import java.util.Arrays; import java.util.Arrays;
import java.util.Objects; import java.util.Objects;
@ -12,15 +12,15 @@ import java.util.stream.Collectors;
* pre-1.17 had all of their classes "flattened" into one package. * pre-1.17 had all of their classes "flattened" into one package.
*/ */
public class ReflectionPackage { public class ReflectionPackage {
private static final boolean flattened = !Utils.versionNewer(17); private static final boolean flattened = !VersionUtil.isNewerThan(17);
public static final String BUKKIT = "org.bukkit.craftbukkit." + Utils.getBukkitPackage(); public static final String BUKKIT = "org.bukkit.craftbukkit." + VersionUtil.getBukkitPackage();
/** /**
* Check if the classes are flattened, if so we need to add the version string into the * Check if the classes are flattened, if so we need to add the version string into the
* package string which is another quirk of the old server jars. * package string which is another quirk of the old server jars.
*/ */
public static final String MINECRAFT = join("net.minecraft", flattened ? "server." + Utils.getBukkitPackage() : ""); public static final String MINECRAFT = join("net.minecraft", flattened ? "server." + VersionUtil.getBukkitPackage() : "");
public static final String ENTITY = flattened ? MINECRAFT : join(MINECRAFT, "world.entity"); public static final String ENTITY = flattened ? MINECRAFT : join(MINECRAFT, "world.entity");
// Simple method that joins all the non-null & non-empty arguments with a dot and returns the result // Simple method that joins all the non-null & non-empty arguments with a dot and returns the result

@ -4,7 +4,7 @@ import com.mojang.authlib.GameProfile;
import io.github.znetworkw.znpcservers.reflection.types.ClassReflection; import io.github.znetworkw.znpcservers.reflection.types.ClassReflection;
import io.github.znetworkw.znpcservers.reflection.types.FieldReflection; import io.github.znetworkw.znpcservers.reflection.types.FieldReflection;
import io.github.znetworkw.znpcservers.reflection.types.MethodReflection; import io.github.znetworkw.znpcservers.reflection.types.MethodReflection;
import io.github.znetworkw.znpcservers.utility.Utils; import io.github.znetworkw.znpcservers.utility.VersionUtil;
import lol.pyr.znpcsplus.util.FoliaUtil; import lol.pyr.znpcsplus.util.FoliaUtil;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
@ -42,7 +42,7 @@ public final class Reflections {
new ReflectionBuilder(ReflectionPackage.ENTITY) new ReflectionBuilder(ReflectionPackage.ENTITY)
.withClassName(ENTITY_CLASS) .withClassName(ENTITY_CLASS)
.withFieldName("entityCount") .withFieldName("entityCount")
.setStrict(!Utils.versionNewer(14))).staticValueModifier(int.class); .setStrict(!VersionUtil.isNewerThan(14))).staticValueModifier(int.class);
public static final ReflectionLazyLoader<AtomicInteger> ATOMIC_ENTITY_ID_FIELD = new FieldReflection( public static final ReflectionLazyLoader<AtomicInteger> ATOMIC_ENTITY_ID_FIELD = new FieldReflection(
new ReflectionBuilder(ReflectionPackage.ENTITY) new ReflectionBuilder(ReflectionPackage.ENTITY)
@ -51,7 +51,7 @@ public final class Reflections {
.withFieldName("d") .withFieldName("d")
.withFieldName("c") .withFieldName("c")
.withExpectResult(AtomicInteger.class) .withExpectResult(AtomicInteger.class)
.setStrict(Utils.versionNewer(14))).staticValueLoader(AtomicInteger.class); .setStrict(VersionUtil.isNewerThan(14))).staticValueLoader(AtomicInteger.class);
public static final Class<?> ASYNC_SCHEDULER_CLASS = new ClassReflection( public static final Class<?> ASYNC_SCHEDULER_CLASS = new ClassReflection(
new ReflectionBuilder("io.papermc.paper.threadedregions.scheduler") new ReflectionBuilder("io.papermc.paper.threadedregions.scheduler")

@ -1,9 +1,8 @@
package io.github.znetworkw.znpcservers.utility; package io.github.znetworkw.znpcservers.utility;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
public final class Utils { public final class VersionUtil {
public static final int BUKKIT_VERSION; public static final int BUKKIT_VERSION;
static { static {
@ -14,7 +13,7 @@ public final class Utils {
BUKKIT_VERSION = version; BUKKIT_VERSION = version;
} }
public static boolean versionNewer(int version) { public static boolean isNewerThan(int version) {
return (BUKKIT_VERSION >= version); return (BUKKIT_VERSION >= version);
} }
@ -26,8 +25,4 @@ public final class Utils {
String version = getBukkitPackage().replace("v", "").replace("R", ""); String version = getBukkitPackage().replace("v", "").replace("R", "");
return version.substring(2, version.length() - 2); return version.substring(2, version.length() - 2);
} }
public static String toColor(String string) {
return ChatColor.translateAlternateColorCodes('&', string);
}
} }

@ -3,7 +3,7 @@ package lol.pyr.znpcsplus.entity;
import com.github.retrooper.packetevents.protocol.entity.type.EntityType; import com.github.retrooper.packetevents.protocol.entity.type.EntityType;
import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes; import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes;
import io.github.znetworkw.znpcservers.reflection.Reflections; import io.github.znetworkw.znpcservers.reflection.Reflections;
import io.github.znetworkw.znpcservers.utility.Utils; import io.github.znetworkw.znpcservers.utility.VersionUtil;
import lol.pyr.znpcsplus.api.entity.PropertyHolder; import lol.pyr.znpcsplus.api.entity.PropertyHolder;
import lol.pyr.znpcsplus.packets.PacketFactory; import lol.pyr.znpcsplus.packets.PacketFactory;
import lol.pyr.znpcsplus.util.ZLocation; import lol.pyr.znpcsplus.util.ZLocation;
@ -63,7 +63,7 @@ public class PacketEntity {
} }
private static int reserveEntityID() { private static int reserveEntityID() {
if (Utils.versionNewer(14)) return Reflections.ATOMIC_ENTITY_ID_FIELD.get().incrementAndGet(); if (VersionUtil.isNewerThan(14)) return Reflections.ATOMIC_ENTITY_ID_FIELD.get().incrementAndGet();
else { else {
int id = Reflections.ENTITY_ID_MODIFIER.get(); int id = Reflections.ENTITY_ID_MODIFIER.get();
Reflections.ENTITY_ID_MODIFIER.set(id + 1); Reflections.ENTITY_ID_MODIFIER.set(id + 1);