From 7145f6a83a6e7214f3a8665097030225cd4b2e54 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Thu, 3 Feb 2022 11:02:40 +0100 Subject: [PATCH] Prevent SWScoreboard instanciation if not necessary --- .../de/steamwar/core/FlatteningWrapper14.java | 5 ++--- .../de/steamwar/core/FlatteningWrapper8.java | 5 ++--- .../de/steamwar/core/FlatteningWrapper.java | 4 ++++ .../de/steamwar/scoreboard/SWScoreboard.java | 20 +++++++++---------- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/SpigotCore_14/src/de/steamwar/core/FlatteningWrapper14.java b/SpigotCore_14/src/de/steamwar/core/FlatteningWrapper14.java index 2092669..b903db6 100644 --- a/SpigotCore_14/src/de/steamwar/core/FlatteningWrapper14.java +++ b/SpigotCore_14/src/de/steamwar/core/FlatteningWrapper14.java @@ -20,7 +20,6 @@ package de.steamwar.core; import com.comphenix.tinyprotocol.Reflection; -import de.steamwar.scoreboard.SWScoreboard; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.inventory.ItemStack; @@ -222,7 +221,7 @@ public class FlatteningWrapper14 implements FlatteningWrapper.IFlatteningWrapper renamedLegacy.put("RECORD_12", Material.MUSIC_DISC_WAIT); } - private static final Reflection.FieldAccessor scoreboardName = Reflection.getField(SWScoreboard.scoreboardObjective, Reflection.getClass("{nms.network.chat}.IChatBaseComponent"), 0); + private static final Reflection.FieldAccessor scoreboardName = Reflection.getField(FlatteningWrapper.scoreboardObjective, Reflection.getClass("{nms.network.chat}.IChatBaseComponent"), 0); private static final Reflection.ConstructorInvoker chatComponentConstructor = Reflection.getConstructor(Reflection.getClass("{nms.network.chat}.ChatComponentText"), String.class); @Override @@ -231,7 +230,7 @@ public class FlatteningWrapper14 implements FlatteningWrapper.IFlatteningWrapper } private static final Class scoreActionEnum = Reflection.getClass("{nms.server}.ScoreboardServer$Action"); - private static final Reflection.FieldAccessor scoreAction = Reflection.getField(SWScoreboard.scoreboardScore, scoreActionEnum, 0); + private static final Reflection.FieldAccessor scoreAction = Reflection.getField(FlatteningWrapper.scoreboardScore, scoreActionEnum, 0); private static final Object scoreActionChange = scoreActionEnum.getEnumConstants()[0]; @Override diff --git a/SpigotCore_8/src/de/steamwar/core/FlatteningWrapper8.java b/SpigotCore_8/src/de/steamwar/core/FlatteningWrapper8.java index 371c799..feb679e 100644 --- a/SpigotCore_8/src/de/steamwar/core/FlatteningWrapper8.java +++ b/SpigotCore_8/src/de/steamwar/core/FlatteningWrapper8.java @@ -20,16 +20,15 @@ package de.steamwar.core; import com.comphenix.tinyprotocol.Reflection; -import de.steamwar.scoreboard.SWScoreboard; import org.bukkit.Material; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.SkullMeta; public class FlatteningWrapper8 implements FlatteningWrapper.IFlatteningWrapper { - private static final Reflection.FieldAccessor scoreboardName = Reflection.getField(SWScoreboard.scoreboardObjective, String.class, 1); + private static final Reflection.FieldAccessor scoreboardName = Reflection.getField(FlatteningWrapper.scoreboardObjective, String.class, 1); private static final Class scoreActionEnum = Reflection.getClass("{nms}.PacketPlayOutScoreboardScore$EnumScoreboardAction"); - private static final Reflection.FieldAccessor scoreAction = Reflection.getField(SWScoreboard.scoreboardScore, scoreActionEnum, 0); + private static final Reflection.FieldAccessor scoreAction = Reflection.getField(FlatteningWrapper.scoreboardScore, scoreActionEnum, 0); private static final Object scoreActionChange = scoreActionEnum.getEnumConstants()[0]; @Override diff --git a/SpigotCore_Main/src/de/steamwar/core/FlatteningWrapper.java b/SpigotCore_Main/src/de/steamwar/core/FlatteningWrapper.java index ca673c0..9da4b20 100644 --- a/SpigotCore_Main/src/de/steamwar/core/FlatteningWrapper.java +++ b/SpigotCore_Main/src/de/steamwar/core/FlatteningWrapper.java @@ -19,12 +19,16 @@ package de.steamwar.core; +import com.comphenix.tinyprotocol.Reflection; import org.bukkit.Material; import org.bukkit.inventory.ItemStack; public class FlatteningWrapper { private FlatteningWrapper() {} + public static final Class scoreboardObjective = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutScoreboardObjective"); + public static final Class scoreboardScore = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutScoreboardScore"); + public static final IFlatteningWrapper impl = VersionDependent.getVersionImpl(Core.getInstance()); public interface IFlatteningWrapper { diff --git a/SpigotCore_Main/src/de/steamwar/scoreboard/SWScoreboard.java b/SpigotCore_Main/src/de/steamwar/scoreboard/SWScoreboard.java index 9cde7bc..440e29a 100644 --- a/SpigotCore_Main/src/de/steamwar/scoreboard/SWScoreboard.java +++ b/SpigotCore_Main/src/de/steamwar/scoreboard/SWScoreboard.java @@ -32,17 +32,15 @@ import java.util.Map; public class SWScoreboard { private SWScoreboard() {} - public static final Class scoreboardObjective = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutScoreboardObjective"); - private static final Reflection.FieldAccessor scoreboardName = Reflection.getField(scoreboardObjective, String.class, 0); - private static final Reflection.FieldAccessor scoreboardAction = Reflection.getField(scoreboardObjective, int.class, 0); + private static final Reflection.FieldAccessor scoreboardName = Reflection.getField(FlatteningWrapper.scoreboardObjective, String.class, 0); + private static final Reflection.FieldAccessor scoreboardAction = Reflection.getField(FlatteningWrapper.scoreboardObjective, int.class, Core.getVersion() > 15 ? 3 : 0); private static final Class scoreboardDisplayEnum = Reflection.getClass("{nms.world.scores.criteria}.IScoreboardCriteria$EnumScoreboardHealthDisplay"); - private static final Reflection.FieldAccessor scoreboardDisplayType = Reflection.getField(scoreboardObjective, scoreboardDisplayEnum, 0); + private static final Reflection.FieldAccessor scoreboardDisplayType = Reflection.getField(FlatteningWrapper.scoreboardObjective, scoreboardDisplayEnum, 0); private static final Object displayTypeIntegers = scoreboardDisplayEnum.getEnumConstants()[0]; - public static final Class scoreboardScore = Reflection.getClass("{nms.network.protocol.game}.PacketPlayOutScoreboardScore"); - private static final Reflection.FieldAccessor scoreName = Reflection.getField(scoreboardScore, String.class, 0); - private static final Reflection.FieldAccessor scoreScoreboardName = Reflection.getField(scoreboardScore, String.class, 1); - private static final Reflection.FieldAccessor scoreValue = Reflection.getField(scoreboardScore, int.class, 0); + private static final Reflection.FieldAccessor scoreName = Reflection.getField(FlatteningWrapper.scoreboardScore, String.class, 0); + private static final Reflection.FieldAccessor scoreScoreboardName = Reflection.getField(FlatteningWrapper.scoreboardScore, String.class, 1); + private static final Reflection.FieldAccessor scoreValue = Reflection.getField(FlatteningWrapper.scoreboardScore, int.class, 0); private static final HashMap playerBoards = new HashMap<>(); //Object -> Scoreboard | Alle Versionen in der Map! private static int toggle = 0; // Scoreboard 0 updates while scoreboard 1 is presenting. toggle marks the current active scoreboard @@ -56,7 +54,7 @@ public class SWScoreboard { Reflection.FieldAccessor scoreboardDisplayName = Reflection.getField(scoreboardDisplayObjective, String.class, 0); Reflection.FieldAccessor scoreboardDisplaySlot = Reflection.getField(scoreboardDisplayObjective, int.class, 0); for(int id = 0; id < 2; id++) { - DELETE_SCOREBOARD[id] = Reflection.newInstance(scoreboardObjective); + DELETE_SCOREBOARD[id] = Reflection.newInstance(FlatteningWrapper.scoreboardObjective); scoreboardName.set(DELETE_SCOREBOARD[id], SIDEBAR + id); scoreboardAction.set(DELETE_SCOREBOARD[id], 1); //1 to remove @@ -100,7 +98,7 @@ public class SWScoreboard { } private static Object createSidebarPacket(String name){ - Object packet = Reflection.newInstance(scoreboardObjective); + Object packet = Reflection.newInstance(FlatteningWrapper.scoreboardObjective); scoreboardName.set(packet, SIDEBAR + toggle); scoreboardAction.set(packet, 0); //0 to create FlatteningWrapper.impl.setScoreboardTitle(packet, name); @@ -109,7 +107,7 @@ public class SWScoreboard { } private static Object createScorePacket(String name, int value){ - Object packet = Reflection.newInstance(scoreboardScore); + Object packet = Reflection.newInstance(FlatteningWrapper.scoreboardScore); scoreName.set(packet, name); scoreScoreboardName.set(packet, SIDEBAR + toggle); scoreValue.set(packet, value);