From 4b0f4328157fc0d22509830650741d73018b1b1b Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 26 Mar 2022 15:49:29 +0100 Subject: [PATCH] Optimize stuff --- src/de/steamwar/lobby/display/NPC.java | 37 +++++++++------------- src/de/steamwar/lobby/team/TeamPlayer.java | 6 ++-- 2 files changed, 19 insertions(+), 24 deletions(-) diff --git a/src/de/steamwar/lobby/display/NPC.java b/src/de/steamwar/lobby/display/NPC.java index 3d7e058..3672e3a 100644 --- a/src/de/steamwar/lobby/display/NPC.java +++ b/src/de/steamwar/lobby/display/NPC.java @@ -22,13 +22,8 @@ package de.steamwar.lobby.display; import com.comphenix.tinyprotocol.Reflection; import com.comphenix.tinyprotocol.TinyProtocol; import com.mojang.authlib.GameProfile; -import net.minecraft.server.v1_15_R1.DataWatcher; -import net.minecraft.server.v1_15_R1.DataWatcherRegistry; -import net.minecraft.server.v1_15_R1.EntityHuman; -import net.minecraft.server.v1_15_R1.PacketPlayOutEntityMetadata; import org.bukkit.Bukkit; import org.bukkit.Location; -import org.bukkit.craftbukkit.v1_15_R1.entity.CraftPlayer; import org.bukkit.entity.Player; import java.lang.reflect.Field; @@ -135,7 +130,6 @@ public class NPC { this.uuid = uuid; this.name = name; this.location = location; - byte yaw = (byte)(int)(location.getYaw() * 256.0 / 360.0); GameProfile profile = new GameProfile(uuid, name); addPlayerInfo = playerInfoPacket(addPlayer, profile); @@ -147,15 +141,14 @@ public class NPC { namedSpawn = namedSpawnConstructor.invoke(); namedSpawnEntity.set(namedSpawn, entityId); namedSpawnUUID.set(namedSpawn, uuid); - namedSpawnX.set(namedSpawn, location.getX()); - namedSpawnY.set(namedSpawn, location.getY()); - namedSpawnZ.set(namedSpawn, location.getZ()); - namedSpawnYaw.set(namedSpawn, yaw); - namedSpawnPitch.set(namedSpawn, (byte)(int)(location.getPitch() * 256.0 / 360.0)); headRotation = headRotationConstructor.invoke(); headRotationEntity.set(headRotation, entityId); - headRotationYaw.set(headRotation, yaw); + + move = movePacketConstructor.invoke(); + movePacketEntity.set(move, entityId); + + setPackets(location); display = new Displayable(location, this::show, this::hide, this::move); } @@ -165,11 +158,14 @@ public class NPC { return; } this.location = location; + setPackets(location); + display.setLocation(location); + } + + private void setPackets(Location location) { byte yaw = (byte)(int)(location.getYaw() * 256.0 / 360.0); byte pitch = (byte)(int)(location.getPitch() * 256.0 / 360.0); headRotationYaw.set(headRotation, yaw); - move = movePacketConstructor.invoke(); - movePacketEntity.set(move, entityId); movePacketX.set(move, location.getX()); movePacketY.set(move, location.getY()); movePacketZ.set(move, location.getZ()); @@ -181,10 +177,12 @@ public class NPC { namedSpawnZ.set(namedSpawn, location.getZ()); namedSpawnYaw.set(namedSpawn, yaw); namedSpawnPitch.set(namedSpawn, pitch); - display.setLocation(location); } private boolean isSimilarLocation(Location location) { + if (this.location == null) { + return false; + } if (Location.normalizeYaw(this.location.getYaw()) != Location.normalizeYaw(location.getYaw())) { return false; } @@ -197,10 +195,7 @@ public class NPC { if (Math.abs(this.location.getY() - location.getY()) > 0.1) { return false; } - if (Math.abs(this.location.getZ() - location.getZ()) > 0.1) { - return false; - } - return true; + return !(Math.abs(this.location.getZ() - location.getZ()) > 0.1); } public Location getLocation() { @@ -227,9 +222,7 @@ public class NPC { private void move(Player player) { TinyProtocol.instance.sendPacket(player, headRotation); - if (move != null) { - TinyProtocol.instance.sendPacket(player, move); - } + TinyProtocol.instance.sendPacket(player, move); } public void delete() { diff --git a/src/de/steamwar/lobby/team/TeamPlayer.java b/src/de/steamwar/lobby/team/TeamPlayer.java index 56fbe46..ea44726 100644 --- a/src/de/steamwar/lobby/team/TeamPlayer.java +++ b/src/de/steamwar/lobby/team/TeamPlayer.java @@ -110,7 +110,7 @@ public class TeamPlayer extends BasicListener { LobbySystem.getPlugin().getLogger().log(Level.INFO, "Loaded " + entities.size() + " team players"); Bukkit.getScheduler().runTaskTimer(LobbySystem.getPlugin(), () -> { count.incrementAndGet(); - if (count.get() % 20 * 60 * 60 == 0) { + if (count.get() % (20 * 60 * 60) == 0) { count.set(0); List steamwarUsers = SteamwarUser.getServerTeam(); AtomicInteger added = new AtomicInteger(); @@ -131,7 +131,9 @@ public class TeamPlayer extends BasicListener { entities.remove(name).delete(); removed.incrementAndGet(); }); - LobbySystem.getPlugin().getLogger().log(Level.INFO, "Loaded " + added.get() + " team players, removed " + removed.get() + " team players"); + if (added.get() > 0 || removed.get() > 0) { + LobbySystem.getPlugin().getLogger().log(Level.INFO, "Loaded " + added.get() + " team players, removed " + removed.get() + " team players"); + } } world.getEntitiesByClasses(Villager.class).forEach(entity -> { NPC npc = entities.get(entity.getName());