From e0659c1bb2b3fe09cafb91bdb601e9b90781fea3 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sun, 10 Mar 2024 19:35:31 +0100 Subject: [PATCH] Bugfixes, Performance improvements Signed-off-by: Lixfel --- .../fightsystem/utils/HullHiderWrapper18.java | 14 ++-- .../fightsystem/countdown/Countdown.java | 5 -- .../countdown/EnternCountdown.java | 9 +-- .../steamwar/fightsystem/fight/FightTeam.java | 7 +- .../de/steamwar/fightsystem/utils/Hull.java | 4 +- .../steamwar/fightsystem/utils/HullHider.java | 39 +++++++---- .../de/steamwar/fightsystem/utils/Region.java | 2 + .../fightsystem/utils/TechHiderWrapper.java | 70 +++++++++++-------- 8 files changed, 85 insertions(+), 65 deletions(-) diff --git a/FightSystem_18/src/de/steamwar/fightsystem/utils/HullHiderWrapper18.java b/FightSystem_18/src/de/steamwar/fightsystem/utils/HullHiderWrapper18.java index 0781fe4..97413f7 100644 --- a/FightSystem_18/src/de/steamwar/fightsystem/utils/HullHiderWrapper18.java +++ b/FightSystem_18/src/de/steamwar/fightsystem/utils/HullHiderWrapper18.java @@ -33,7 +33,7 @@ import java.util.List; public class HullHiderWrapper18 implements HullHiderWrapper { - private static final Reflection.MethodInvoker getState = Reflection.getTypedMethod(Reflection.getClass("{obc}.CraftBlockData"), "getState", IBlockData.class); + private static final Reflection.MethodInvoker getState = Reflection.getTypedMethod(Reflection.getClass("{obc}.block.data.CraftBlockData"), "getState", IBlockData.class); @Override public Object generateBlockChangePacket(List changes) { Object[] blockdata = new Object[changes.size()]; @@ -48,15 +48,21 @@ public class HullHiderWrapper18 implements HullHiderWrapper { private Object generateBlockChangePacket(List changes, Object[] blockdata) { if(changes.size() > 1) { + Hull.IntVector section = changes.get(0); + //section = new Hull.IntVector(ProtocolUtils.posToChunk(section.getX()), ProtocolUtils.posToChunk(section.getY()), ProtocolUtils.posToChunk(section.getZ())); + section = new Hull.IntVector(section.getX() >> 4, section.getY() >> 4, section.getZ() >> 4); + int xOffset = 16*section.getX(); + int yOffset = 16*section.getY(); + int zOffset = 16*section.getZ(); + short[] pos = new short[changes.size()]; for(int i = 0; i < changes.size(); i++) { Hull.IntVector change = changes.get(i); - pos[i] = (short) ((change.getX()%16) << 8 + (change.getZ()%16) << 4 + change.getY()%16); + pos[i] = (short) (((change.getX()-xOffset) << 8) + ((change.getZ()-zOffset) << 4) + (change.getY()-yOffset)); } - Hull.IntVector section = changes.get(0); - return constructMultiBlockChange(new Hull.IntVector(section.getX() >> 4, section.getY() >> 4, section.getZ() >> 4), pos, blockdata); + return constructMultiBlockChange(section, pos, blockdata); } else { Hull.IntVector pos = changes.get(0); return new PacketPlayOutBlockChange(new BlockPosition(pos.getX(), pos.getY(), pos.getZ()), (IBlockData) blockdata[0]); diff --git a/FightSystem_Core/src/de/steamwar/fightsystem/countdown/Countdown.java b/FightSystem_Core/src/de/steamwar/fightsystem/countdown/Countdown.java index 46c8ace..e96c6b1 100644 --- a/FightSystem_Core/src/de/steamwar/fightsystem/countdown/Countdown.java +++ b/FightSystem_Core/src/de/steamwar/fightsystem/countdown/Countdown.java @@ -83,8 +83,6 @@ public abstract class Countdown { } for(Countdown countdown : new ArrayList<>(currentCountdowns)) { - if(countdown.time - smallestTime <= 1) - countdown.prepareFinish(); countdown.time -= smallestTime; countdown.show(); } @@ -104,8 +102,6 @@ public abstract class Countdown { Bukkit.getOnlinePlayers().forEach(p -> sendCountdownMessage(p, message, time / divisor, appendix)); } - protected void prepareFinish() {} - public int getTimeLeft(){ return time; } @@ -124,7 +120,6 @@ public abstract class Countdown { broadcast("COUNTDOWN_SECONDS", 1); break; case 1: - prepareFinish(); broadcast("COUNTDOWN_SECOND", 1); break; case 0: diff --git a/FightSystem_Core/src/de/steamwar/fightsystem/countdown/EnternCountdown.java b/FightSystem_Core/src/de/steamwar/fightsystem/countdown/EnternCountdown.java index 5581fe8..55bcbe3 100644 --- a/FightSystem_Core/src/de/steamwar/fightsystem/countdown/EnternCountdown.java +++ b/FightSystem_Core/src/de/steamwar/fightsystem/countdown/EnternCountdown.java @@ -22,8 +22,10 @@ package de.steamwar.fightsystem.countdown; import de.steamwar.fightsystem.Config; import de.steamwar.fightsystem.FightSystem; import de.steamwar.fightsystem.events.BoardingEvent; +import de.steamwar.fightsystem.fight.Fight; import de.steamwar.fightsystem.fight.FightPlayer; import de.steamwar.fightsystem.utils.Message; +import de.steamwar.fightsystem.utils.Region; import de.steamwar.fightsystem.utils.SWSound; import de.steamwar.techhider.ProtocolUtils; import net.md_5.bungee.api.ChatMessageType; @@ -61,15 +63,10 @@ public class EnternCountdown extends Countdown { FightSystem.getMessage().sendPrefixless("ENTERN_ALLOWED", fightPlayer.getEntity(), ChatMessageType.ACTION_BAR); fightPlayer.ifPlayer(player -> { FightSystem.getHullHider().updatePlayer(player); - FightSystem.getTechHider().reloadChunks(player, chunkPos, false); + FightSystem.getTechHider().reloadChunks(player, Fight.getOpposite(fightPlayer.getTeam()).getExtendRegion(), Region.EMPTY); }); } - @Override - protected void prepareFinish() { - chunkPos = FightSystem.getTechHider().prepareChunkReload(fightPlayer.getEntity(), false); - } - @Override protected void broadcast(String message, int divisor) { fightPlayer.ifPlayer(player -> { diff --git a/FightSystem_Core/src/de/steamwar/fightsystem/fight/FightTeam.java b/FightSystem_Core/src/de/steamwar/fightsystem/fight/FightTeam.java index 4a2b1be..853646f 100644 --- a/FightSystem_Core/src/de/steamwar/fightsystem/fight/FightTeam.java +++ b/FightSystem_Core/src/de/steamwar/fightsystem/fight/FightTeam.java @@ -40,7 +40,6 @@ import de.steamwar.fightsystem.winconditions.Winconditions; import de.steamwar.inventory.SWItem; import de.steamwar.sql.SchematicNode; import de.steamwar.sql.SteamwarUser; -import de.steamwar.techhider.ProtocolUtils; import lombok.Getter; import net.md_5.bungee.api.ChatMessageType; import org.bukkit.*; @@ -262,7 +261,6 @@ public class FightTeam { } public void addMember(LivingEntity entity, SteamwarUser user, boolean silent) { - final List chunksToReload = FightSystem.getTechHider().prepareChunkReload(entity, false); FightPlayer fightPlayer = getFightPlayer(entity) != null ? getFightPlayer(entity) : new FightPlayer(entity, user, this); fightPlayer.revive(); players.put(entity.getUniqueId(), fightPlayer); @@ -291,7 +289,7 @@ public class FightTeam { if(FightState.Running.contains(FightState.getFightState())) fightPlayer.startEnternCountdown(Wincondition.getTimeOverCountdown()); - fightPlayer.ifPlayer(player -> FightSystem.getTechHider().reloadChunks(player, chunksToReload, false)); + fightPlayer.ifPlayer(player -> FightSystem.getTechHider().reloadChunks(player, Config.ArenaRegion, fightPlayer.canEntern() ? Region.EMPTY : Fight.getOpposite(this).getExtendRegion())); if(isLeaderless()) setLeader(fightPlayer, silent); @@ -304,7 +302,6 @@ public class FightTeam { Bukkit.getPluginManager().callEvent(new TeamLeaveEvent(fightPlayer)); fightPlayer.ifPlayer(PersonalKitCreator::closeIfInKitCreator); - List chunksToReload = FightSystem.getTechHider().prepareChunkReload(entity, true); players.remove(entity.getUniqueId()); team.removeEntry(entity.getName()); Permanent.getSpectatorTeam().addEntry(entity.getName()); @@ -322,7 +319,7 @@ public class FightTeam { if(player.isOnline()){ FightSystem.getHullHider().updatePlayer(player); - FightSystem.getTechHider().reloadChunks(player, chunksToReload, true); + FightSystem.getTechHider().reloadChunks(player, Config.ArenaRegion, Fight.getOpposite(this).getExtendRegion()); if(ArenaMode.VariableTeams.contains(Config.mode)) HotbarKit.SPECTATOR_KIT.loadToPlayer(player); diff --git a/FightSystem_Core/src/de/steamwar/fightsystem/utils/Hull.java b/FightSystem_Core/src/de/steamwar/fightsystem/utils/Hull.java index 2e95a7b..f310fa3 100644 --- a/FightSystem_Core/src/de/steamwar/fightsystem/utils/Hull.java +++ b/FightSystem_Core/src/de/steamwar/fightsystem/utils/Hull.java @@ -49,7 +49,7 @@ public class Hull { private final Map> blockVisibility = new HashMap<>(); private final Set uncoveredSurface = new HashSet<>(); - private final Set players = new HashSet<>(); + private final HashSet players = new HashSet<>(); private final Set entities = new HashSet<>(); private final Set rentities = new HashSet<>(); @@ -94,7 +94,7 @@ public class Hull { } public boolean isBlockHidden(Player player, int x, int y, int z) { - return players.contains(player) && region.inRegion(x, y, z) && !visibility.get(((y - region.getMinY()) * region.getSizeZ() + (z - region.getMinZ())) * region.getSizeX() + (x - region.getMinX())); + return region.inRegion(x, y, z) && players.contains(player) && !visibility.get(((y - region.getMinY()) * region.getSizeZ() + (z - region.getMinZ())) * region.getSizeX() + (x - region.getMinX())); } public boolean isLocationHidden(Player player, Location location) { diff --git a/FightSystem_Core/src/de/steamwar/fightsystem/utils/HullHider.java b/FightSystem_Core/src/de/steamwar/fightsystem/utils/HullHider.java index 108dce2..259433f 100644 --- a/FightSystem_Core/src/de/steamwar/fightsystem/utils/HullHider.java +++ b/FightSystem_Core/src/de/steamwar/fightsystem/utils/HullHider.java @@ -57,14 +57,18 @@ import java.util.function.Function; public class HullHider implements Listener { public static final boolean ENABLED = TechHiderWrapper.ENABLED && Core.getVersion() >= 18; - private final Map hulls = new HashMap<>(); + private final Map hullMap = new HashMap<>(); + private final Hull[] hulls; private final Map, BiFunction> packetHiders = new HashMap<>(); public HullHider() { - if(!ENABLED) + if(!ENABLED) { + hulls = new Hull[0]; return; + } - Fight.teams().forEach(team -> hulls.put(team, new Hull(team))); + Fight.teams().forEach(team -> hullMap.put(team, new Hull(team))); + hulls = hullMap.values().toArray(new Hull[0]); packetHiders.put(packetPlayOutWorldEvent, this::worldEventHider); packetHiders.put(packetPlayOutExplosion, this::explosionHider); @@ -94,7 +98,7 @@ public class HullHider implements Listener { if(!ENABLED) return; - hulls.get(team).initialize(); + hullMap.get(team).initialize(); } @@ -114,7 +118,7 @@ public class HullHider implements Listener { FightTeam team = Fight.getPlayerTeam(player); FightPlayer fp = Fight.getFightPlayer(player); - for(Map.Entry hull : hulls.entrySet()) { + for(Map.Entry hull : hullMap.entrySet()) { if(hull.getKey() == team || (fp != null && fp.canEntern())) { hull.getValue().removePlayer(player, true); } else { @@ -124,7 +128,8 @@ public class HullHider implements Listener { } private void removePlayer(Player player, boolean activeRemoval) { - hulls.values().forEach(hull -> hull.removePlayer(player, activeRemoval)); + for (Hull hull : hulls) + hull.removePlayer(player, activeRemoval); } @@ -135,7 +140,7 @@ public class HullHider implements Listener { } public void blockUpdate(Block block, Material changedType) { - for (Hull hull : hulls.values()) + for (Hull hull : hulls) hull.updateBlockVisibility(block, changedType); } @@ -143,7 +148,7 @@ public class HullHider implements Listener { if(!ENABLED) return false; - for (Hull hull : hulls.values()) + for (Hull hull : hulls) if(hull.isBlockHidden(player, x, y, z)) return true; @@ -153,30 +158,34 @@ public class HullHider implements Listener { @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onSpawn(EntitySpawnEvent e) { - hulls.values().forEach(hull -> hull.checkEntity(e.getEntity())); + for (Hull hull : hulls) + hull.checkEntity(e.getEntity()); } private void onTick() { Recording.iterateOverEntities(Objects::nonNull, entity -> { - for (Hull hull : hulls.values()) + for (Hull hull : hulls) hull.checkEntity(entity); }); - for (Hull hull : hulls.values()) + for (Hull hull : hulls) hull.sendUncoveredBlocks(); } @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onDeath(EntityDeathEvent e) { - hulls.values().forEach(hull -> hull.removeEntity(e.getEntity())); + for(Hull hull : hulls) + hull.removeEntity(e.getEntity()); } public void updateREntity(REntity e) { - hulls.values().forEach(hull -> hull.checkREntity(e)); + for(Hull hull : hulls) + hull.checkREntity(e); } public void despawnREntity(REntity e) { - hulls.values().forEach(hull -> hull.removeREntity(e)); + for(Hull hull : hulls) + hull.removeREntity(e); } @@ -213,7 +222,7 @@ public class HullHider implements Listener { } private Object packetHider(Player player, Object packet, Location location) { - for(Hull hull : hulls.values()) { + for(Hull hull : hulls) { if(hull.isLocationHidden(player, location)) return null; } diff --git a/FightSystem_Core/src/de/steamwar/fightsystem/utils/Region.java b/FightSystem_Core/src/de/steamwar/fightsystem/utils/Region.java index b789641..6a55979 100644 --- a/FightSystem_Core/src/de/steamwar/fightsystem/utils/Region.java +++ b/FightSystem_Core/src/de/steamwar/fightsystem/utils/Region.java @@ -31,6 +31,8 @@ import java.util.function.ObjIntConsumer; @AllArgsConstructor public class Region { + public static final Region EMPTY = Region.fromSize(-10000, -10000, -10000, 0, 0, 0); + public static Region withExtension(int minX, int minY, int minZ, int sizeX, int sizeY, int sizeZ, int extendX, int extendY, int extendZ) { return Region.fromSize(minX - extendX, minY - extendY, minZ - extendZ, sizeX + extendX * 2, sizeY + extendY * 2, sizeZ + extendZ * 2); diff --git a/FightSystem_Core/src/de/steamwar/fightsystem/utils/TechHiderWrapper.java b/FightSystem_Core/src/de/steamwar/fightsystem/utils/TechHiderWrapper.java index b47b29c..6168706 100644 --- a/FightSystem_Core/src/de/steamwar/fightsystem/utils/TechHiderWrapper.java +++ b/FightSystem_Core/src/de/steamwar/fightsystem/utils/TechHiderWrapper.java @@ -22,32 +22,36 @@ package de.steamwar.fightsystem.utils; import de.steamwar.core.CraftbukkitWrapper; import de.steamwar.fightsystem.Config; import de.steamwar.fightsystem.FightSystem; +import de.steamwar.fightsystem.events.BoardingEvent; +import de.steamwar.fightsystem.events.TeamLeaveEvent; +import de.steamwar.fightsystem.events.TeamSpawnEvent; import de.steamwar.fightsystem.fight.Fight; import de.steamwar.fightsystem.fight.FightTeam; import de.steamwar.fightsystem.states.FightState; import de.steamwar.fightsystem.states.StateDependent; -import de.steamwar.techhider.ProtocolUtils; +import de.steamwar.fightsystem.states.StateDependentListener; import de.steamwar.techhider.TechHider; -import org.bukkit.Bukkit; import org.bukkit.Material; -import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerQuitEvent; -import java.util.ArrayList; import java.util.Collections; -import java.util.List; +import java.util.concurrent.ConcurrentHashMap; -public class TechHiderWrapper extends StateDependent implements TechHider.LocationEvaluator { +public class TechHiderWrapper extends StateDependent implements TechHider.LocationEvaluator, Listener { public static final boolean ENABLED = !Config.OnlyPublicSchematics && !Config.test() && Config.TechhiderActive; - private final Region empty = Region.fromSize(-10000, -10000, -10000, 0, 0, 0); - + private final ConcurrentHashMap hiddenRegion = new ConcurrentHashMap<>(); private final TechHider techHider; public TechHiderWrapper() { super(ENABLED, FightState.Schem); techHider = new TechHider(this, Material.getMaterial(Config.ObfuscateWith), Config.HiddenBlocks, Config.HiddenBlockEntities); + + new StateDependentListener(ENABLED, FightState.Schem, this); register(); } @@ -59,30 +63,41 @@ public class TechHiderWrapper extends StateDependent implements TechHider.Locati @Override public void disable() { techHider.disable(); + hiddenRegion.clear(); } - public List prepareChunkReload(LivingEntity p, boolean hide) { - if(!ENABLED || !(p instanceof Player)) - return Collections.emptyList(); + @EventHandler + public void teamJoin(TeamSpawnEvent e) { + e.getFightPlayer().ifPlayer(player -> hiddenRegion.put(player, getHiddenRegion(player))); + } - List chunksToReload = new ArrayList<>(); - Config.ArenaRegion.forEachChunk((x, z) -> { - if(skipChunk((Player) p, x, z) == hide) - chunksToReload.add(new ProtocolUtils.ChunkPos(x, z)); + @EventHandler + public void boarding(BoardingEvent e) { + e.getFightPlayer().ifPlayer(player -> hiddenRegion.put(player, getHiddenRegion(player))); + } + + @EventHandler + public void teamLeave(TeamLeaveEvent e) { + e.getFightPlayer().ifPlayer(player -> { + if(player.isOnline()) + hiddenRegion.put(player, getHiddenRegion(player)); }); - return chunksToReload; } - public void reloadChunks(Player p, List chunksToReload, boolean hide) { - if(!ENABLED || !FightState.Schem.contains(FightState.getFightState())) + @EventHandler + public void playerQuit(PlayerQuitEvent e) { + Player player = e.getPlayer(); + hiddenRegion.remove(player); + } + + public void reloadChunks(Player player, Region region, Region exclusion) { + if(!ENABLED || !FightState.Schem.contains(FightState.getFightState()) || !player.isOnline()) return; - Bukkit.getScheduler().runTaskLater(FightSystem.getPlugin(), () -> { - for(ProtocolUtils.ChunkPos chunk : chunksToReload){ - if(skipChunk(p, chunk.x(), chunk.z()) != hide) - CraftbukkitWrapper.impl.sendChunk(p, chunk.x(), chunk.z()); - } - }, 40); + region.forEachChunk((chunkX, chunkZ) -> { + if(exclusion.chunkOutside(chunkX, chunkZ)) + CraftbukkitWrapper.impl.sendChunk(player, chunkX, chunkZ); + }); } @Override @@ -92,13 +107,12 @@ public class TechHiderWrapper extends StateDependent implements TechHider.Locati @Override public boolean skipChunkSection(Player player, int chunkX, int chunkY, int chunkZ) { - //TODO chunkhider skip sections return getHiddenRegion(player).chunkSectionOutside(chunkX, chunkY, chunkZ); } @Override public TechHider.State check(Player player, int x, int y, int z) { - if(getHiddenRegion(player).inRegion(x, y, z)) { + if(hiddenRegion.computeIfAbsent(player, this::getHiddenRegion).inRegion(x, y, z)) { if(FightSystem.getHullHider().isBlockHidden(player, x, y, z)) { return TechHider.State.HIDE; } else { @@ -111,14 +125,14 @@ public class TechHiderWrapper extends StateDependent implements TechHider.Locati private Region getHiddenRegion(Player player) { if(Config.isReferee(player)) - return empty; + return Region.EMPTY; FightTeam team = Fight.getPlayerTeam(player); if(team == null) return Config.ArenaRegion; if(team.canPlayerEntern(player)) - return empty; + return Region.EMPTY; return Fight.getOpposite(team).getExtendRegion(); }