From be653754a7504af6b519496022df415e60333753 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Tue, 10 Dec 2024 00:01:47 +0100 Subject: [PATCH 1/6] Better Hull Hider Algorithm Cardinal visibility with diagonal branching. --- .../de/steamwar/fightsystem/utils/Hull.java | 156 ++++++++---------- 1 file changed, 72 insertions(+), 84 deletions(-) diff --git a/FightSystem/FightSystem_Core/src/de/steamwar/fightsystem/utils/Hull.java b/FightSystem/FightSystem_Core/src/de/steamwar/fightsystem/utils/Hull.java index 05ff3fc8..86eda93d 100644 --- a/FightSystem/FightSystem_Core/src/de/steamwar/fightsystem/utils/Hull.java +++ b/FightSystem/FightSystem_Core/src/de/steamwar/fightsystem/utils/Hull.java @@ -24,6 +24,9 @@ import de.steamwar.entity.REntity; import de.steamwar.fightsystem.Config; import de.steamwar.fightsystem.FightSystem; import de.steamwar.fightsystem.fight.FightTeam; +import de.steamwar.techhider.BlockIds; +import it.unimi.dsi.fastutil.ints.Int2IntMap; +import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap; import lombok.AllArgsConstructor; import lombok.Getter; import org.bukkit.Location; @@ -38,6 +41,40 @@ import java.util.logging.Level; public class Hull { + private static final boolean DEBUG = false; + private static final int AIR = BlockIds.impl.materialToId(Material.AIR); + private static final int RED_GLASS = BlockIds.impl.materialToId(Material.STONE); + private static final int GLASS = BlockIds.impl.materialToId(Material.GLASS); + + private static final IntVector[] DIRECTIONS = new IntVector[]{ + new IntVector(-1, -1, -1), + new IntVector(-1, -1, 0), + new IntVector(-1, -1, 1), + new IntVector(-1, 0, -1), + new IntVector(-1, 0, 0), + new IntVector(-1, 0, 1), + new IntVector(-1, 1, -1), + new IntVector(-1, 1, 0), + new IntVector(-1, 1, 1), + new IntVector(0, -1, -1), + new IntVector(0, -1, 0), + new IntVector(0, -1, 1), + new IntVector(0, 0, -1), + new IntVector(0, 0, 1), + new IntVector(0, 1, -1), + new IntVector(0, 1, 0), + new IntVector(0, 1, 1), + new IntVector(1, -1, -1), + new IntVector(1, -1, 0), + new IntVector(1, -1, 1), + new IntVector(1, 0, -1), + new IntVector(1, 0, 0), + new IntVector(1, 0, 1), + new IntVector(1, 1, -1), + new IntVector(1, 1, 0), + new IntVector(1, 1, 1), + }; + private static boolean isOccluding(Material material) { return material.isOccluding() || Config.HiddenBlocks.contains(material); } @@ -47,7 +84,7 @@ public class Hull { private final BitSet occluding; private final BitSet visibility; - private final Map> blockVisibility = new HashMap<>(); + private final Int2IntMap visibilityDirections = new Int2IntOpenHashMap(); // Contains the visible directions of each occluding visible block private final Set uncoveredSurface = new HashSet<>(); private final HashSet players = new HashSet<>(); @@ -59,39 +96,6 @@ public class Hull { this.groundVisible = region.getMinY() != Config.PlayerRegion.getMinY(); this.occluding = new BitSet(region.volume()); this.visibility = new BitSet(region.volume()); - - IntVector[] directions; - if (groundVisible) { - directions = new IntVector[]{ - new IntVector(1, 0, 0), - new IntVector(-1, 0, 0), - new IntVector(0, 1, 0), - new IntVector(0, -1, 0), - new IntVector(0, 0, 1), - new IntVector(0, 0, -1) - }; - } else { - directions = new IntVector[]{ - new IntVector(1, 0, 0), - new IntVector(-1, 0, 0), - new IntVector(0, -1, 0), - new IntVector(0, 0, 1), - new IntVector(0, 0, -1) - }; - } - - // Generate quadrants for each direction - for (IntVector direction : directions) { - Map map = new HashMap<>(); - for (int z = (direction.z == 0 ? -1 : 0); z <= 1; z += 2) { - for (int y = (direction.y == 0 ? -1 : 0); y <= 1; y += 2) { - for (int x = (direction.x == 0 ? -1 : 0); x <= 1; x += 2) { - map.put(new IntVector(x, y, z), new BitSet(region.volume())); - } - } - } - blockVisibility.put(direction, map); - } } public boolean blockPrecise(Player player, int chunkX, int chunkY, int chunkZ) { @@ -123,7 +127,7 @@ public class Hull { public void checkEntity(Entity entity) { Location location = entity.getLocation(); - if(region.inRegion(location) && !visibility.get(new IntVector(location).toId(region))) { //TODO more precise + if(region.inRegion(location) && !visibility.get(new IntVector(location).toId(region))) { if(entities.add(entity)) { for(Player player : players) BlockIdWrapper.impl.untrackEntity(player, entity.getEntityId()); @@ -142,7 +146,7 @@ public class Hull { public void checkREntity(REntity entity) { Location location = new Location(Config.world, entity.getX(), entity.getY(), entity.getZ()); - if(region.inRegion(location) && !visibility.get(new IntVector(location).toId(region))) { //TODO more precise + if(region.inRegion(location) && !visibility.get(new IntVector(location).toId(region))) { if(rentities.add(entity)) entity.hide(true); } else { @@ -158,11 +162,7 @@ public class Hull { public void initialize() { visibility.clear(); occluding.clear(); - uncoveredSurface.clear(); - for (Map direction : blockVisibility.values()) { - for (BitSet set : direction.values()) - set.clear(); - } + visibilityDirections.clear(); long start = System.currentTimeMillis(); region.forEach((x, y, z) -> { @@ -170,12 +170,15 @@ public class Hull { if (isOccluding(Config.world.getBlockAt(x, y, z).getType())) occluding.set(block.toId(region)); }); - forEachBorder((root, direction) -> { - for (Map.Entry quadrant : blockVisibility.get(direction).entrySet()) { - checkBlock(new NullList<>(), root, direction, quadrant.getKey(), quadrant.getValue()); - } - }); + forEachBorder((root, direction) -> updateBlocks(new NullList<>(), root, direction)); FightSystem.getPlugin().getLogger().log(Level.INFO, () -> "[HullHider] initialisation finished: " + (System.currentTimeMillis() - start) + " ms, visible blocks: " + visibility.cardinality()); + + if(DEBUG) { + region.forEach((x, y, z) -> { + int id = new IntVector(x, y, z).toId(region); + BlockIdWrapper.impl.setBlock(Config.world, x, y + Config.BlueExtendRegion.getSizeY(), z, visibility.get(id) ? (occluding.get(id) ? RED_GLASS : AIR) : GLASS); + }); + } } public void updateBlockVisibility(Block b, Material changedType) { @@ -187,15 +190,15 @@ public class Hull { if (!occluding.get(id) || isOccluding(changedType)) return; - List uncovered = new ArrayList<>(); occluding.clear(id); - for (Map.Entry> direction : blockVisibility.entrySet()) { - for (Map.Entry quadrant : direction.getValue().entrySet()) { - if (quadrant.getValue().get(id)) { - quadrant.getValue().clear(id); - checkBlock(uncovered, root, direction.getKey(), quadrant.getKey(), quadrant.getValue()); - } - } + if(!visibility.get(id)) + return; + + List uncovered = new ArrayList<>(); + int directions = visibilityDirections.remove(id); + for(IntVector direction : DIRECTIONS) { + if((directionId(direction) & directions) != 0) + updateBlocks(uncovered, root, direction); } if(uncovered.isEmpty()) @@ -205,7 +208,7 @@ public class Hull { Iterator it = entities.iterator(); while(it.hasNext()) { Entity entity = it.next(); - if(uncoveredSet.contains(new IntVector(entity.getLocation()))) { //TODO more precise + if(uncoveredSet.contains(new IntVector(entity.getLocation()))) { it.remove(); for(Player player : players) BlockIdWrapper.impl.trackEntity(player, entity.getEntityId()); @@ -215,7 +218,7 @@ public class Hull { Iterator rit = rentities.iterator(); while(rit.hasNext()) { REntity entity = rit.next(); - if(uncoveredSet.contains(new IntVector(new Location(Config.world, entity.getX(), entity.getY(), entity.getZ())))) { //TODO more precise + if(uncoveredSet.contains(new IntVector(new Location(Config.world, entity.getX(), entity.getY(), entity.getZ())))) { rit.remove(); entity.hide(false); } @@ -266,48 +269,33 @@ public class Hull { } } - private void checkBlock(List uncovered, IntVector block, IntVector direction, IntVector quadrant, BitSet quadVisibility) { + private void updateBlocks(List uncovered, IntVector block, IntVector direction) { if (block.notInRegion(region)) return; int id = block.toId(region); - if (quadVisibility.get(id)) - return; - - quadVisibility.set(id); if (!visibility.get(id)) { visibility.set(id); uncovered.add(block); } - if (occluding.get(id)) + if (occluding.get(id)) { + visibilityDirections.compute(id, (pos, v) -> (v == null ? 0 : v) + directionId(direction)); + return; + } + + updateBlocks(uncovered, block.add(direction), direction); + if(direction.x*direction.x + direction.y*direction.y + direction.z*direction.z > 1) // If direction diagonal return; - IntVector neighbour = block.add(direction); - checkBlock(uncovered, neighbour, direction, quadrant, quadVisibility); - boolean neigbourTransparent = boundedNonOccluding(neighbour); - boolean diagonalReachable = false; - if (direction.x == 0 && (neigbourTransparent || boundedNonOccluding(block.add(quadrant.x, 0, 0)))) { - checkBlock(uncovered, neighbour.add(quadrant.x, 0, 0), direction, quadrant, quadVisibility); - diagonalReachable = boundedNonOccluding(neighbour.add(quadrant.x, 0, 0)); + for(IntVector branchDirection : DIRECTIONS) { + if(!branchDirection.equals(direction) && direction.x*branchDirection.x + direction.y*branchDirection.y + direction.z*branchDirection.z == 1) // If branch direction diagonal pointing towards direction + updateBlocks(uncovered, block.add(branchDirection), branchDirection); } - - if (direction.y == 0 && (neigbourTransparent || boundedNonOccluding(block.add(0, quadrant.y, 0)))) { - checkBlock(uncovered, neighbour.add(0, quadrant.y, 0), direction, quadrant, quadVisibility); - diagonalReachable = diagonalReachable || boundedNonOccluding(neighbour.add(0, quadrant.y, 0)); - } - - if (direction.z == 0 && (neigbourTransparent || boundedNonOccluding(block.add(0, 0, quadrant.z)))) { - checkBlock(uncovered, neighbour.add(0, 0, quadrant.z), direction, quadrant, quadVisibility); - diagonalReachable = diagonalReachable || boundedNonOccluding(neighbour.add(0, 0, quadrant.z)); - } - - if (diagonalReachable) - checkBlock(uncovered, neighbour.add(quadrant), direction, quadrant, quadVisibility); } - private boolean boundedNonOccluding(IntVector block) { - return !(block.notInRegion(region) || occluding.get(block.toId(region))); + private int directionId(IntVector v) { + return 9*(v.z+1) + 3*(v.y+1) + (v.x+1); } From b1c0e36cee98bd968a9b64e17425906d1007aaae Mon Sep 17 00:00:00 2001 From: Lixfel Date: Tue, 10 Dec 2024 00:44:26 +0100 Subject: [PATCH 2/6] Remove diagonal diagonals, branch only in enemy direction --- .../src/de/steamwar/fightsystem/utils/Hull.java | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/FightSystem/FightSystem_Core/src/de/steamwar/fightsystem/utils/Hull.java b/FightSystem/FightSystem_Core/src/de/steamwar/fightsystem/utils/Hull.java index 86eda93d..e69a7986 100644 --- a/FightSystem/FightSystem_Core/src/de/steamwar/fightsystem/utils/Hull.java +++ b/FightSystem/FightSystem_Core/src/de/steamwar/fightsystem/utils/Hull.java @@ -47,15 +47,11 @@ public class Hull { private static final int GLASS = BlockIds.impl.materialToId(Material.GLASS); private static final IntVector[] DIRECTIONS = new IntVector[]{ - new IntVector(-1, -1, -1), new IntVector(-1, -1, 0), - new IntVector(-1, -1, 1), new IntVector(-1, 0, -1), new IntVector(-1, 0, 0), new IntVector(-1, 0, 1), - new IntVector(-1, 1, -1), new IntVector(-1, 1, 0), - new IntVector(-1, 1, 1), new IntVector(0, -1, -1), new IntVector(0, -1, 0), new IntVector(0, -1, 1), @@ -64,15 +60,11 @@ public class Hull { new IntVector(0, 1, -1), new IntVector(0, 1, 0), new IntVector(0, 1, 1), - new IntVector(1, -1, -1), new IntVector(1, -1, 0), - new IntVector(1, -1, 1), new IntVector(1, 0, -1), new IntVector(1, 0, 0), new IntVector(1, 0, 1), - new IntVector(1, 1, -1), new IntVector(1, 1, 0), - new IntVector(1, 1, 1), }; private static boolean isOccluding(Material material) { @@ -81,6 +73,7 @@ public class Hull { private final Region region; private final boolean groundVisible; + private final IntVector primaryDirection; private final BitSet occluding; private final BitSet visibility; @@ -94,6 +87,7 @@ public class Hull { public Hull(FightTeam team) { this.region = team.getSchemRegion(); this.groundVisible = region.getMinY() != Config.PlayerRegion.getMinY(); + this.primaryDirection = new IntVector(0, 0, team.isBlue() == (Config.BlueToRedZ > 0) ? -1 : 1); this.occluding = new BitSet(region.volume()); this.visibility = new BitSet(region.volume()); } @@ -280,12 +274,12 @@ public class Hull { } if (occluding.get(id)) { - visibilityDirections.compute(id, (pos, v) -> (v == null ? 0 : v) + directionId(direction)); + visibilityDirections.compute(id, (pos, v) -> (v == null ? 0 : v) | directionId(direction)); return; } updateBlocks(uncovered, block.add(direction), direction); - if(direction.x*direction.x + direction.y*direction.y + direction.z*direction.z > 1) // If direction diagonal + if(!direction.equals(primaryDirection)) return; for(IntVector branchDirection : DIRECTIONS) { From c50bb64516c89471229f67c0cd5ec1cf4906dc07 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Tue, 10 Dec 2024 01:59:16 +0100 Subject: [PATCH 3/6] Performance improvements --- .../de/steamwar/fightsystem/utils/Hull.java | 53 +++++-------------- 1 file changed, 12 insertions(+), 41 deletions(-) diff --git a/FightSystem/FightSystem_Core/src/de/steamwar/fightsystem/utils/Hull.java b/FightSystem/FightSystem_Core/src/de/steamwar/fightsystem/utils/Hull.java index e69a7986..fd0113c0 100644 --- a/FightSystem/FightSystem_Core/src/de/steamwar/fightsystem/utils/Hull.java +++ b/FightSystem/FightSystem_Core/src/de/steamwar/fightsystem/utils/Hull.java @@ -24,8 +24,6 @@ import de.steamwar.entity.REntity; import de.steamwar.fightsystem.Config; import de.steamwar.fightsystem.FightSystem; import de.steamwar.fightsystem.fight.FightTeam; -import de.steamwar.techhider.BlockIds; -import it.unimi.dsi.fastutil.ints.Int2IntMap; import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap; import lombok.AllArgsConstructor; import lombok.Getter; @@ -38,35 +36,10 @@ import org.bukkit.entity.Player; import java.util.*; import java.util.function.BiConsumer; import java.util.logging.Level; +import java.util.stream.IntStream; public class Hull { - private static final boolean DEBUG = false; - private static final int AIR = BlockIds.impl.materialToId(Material.AIR); - private static final int RED_GLASS = BlockIds.impl.materialToId(Material.STONE); - private static final int GLASS = BlockIds.impl.materialToId(Material.GLASS); - - private static final IntVector[] DIRECTIONS = new IntVector[]{ - new IntVector(-1, -1, 0), - new IntVector(-1, 0, -1), - new IntVector(-1, 0, 0), - new IntVector(-1, 0, 1), - new IntVector(-1, 1, 0), - new IntVector(0, -1, -1), - new IntVector(0, -1, 0), - new IntVector(0, -1, 1), - new IntVector(0, 0, -1), - new IntVector(0, 0, 1), - new IntVector(0, 1, -1), - new IntVector(0, 1, 0), - new IntVector(0, 1, 1), - new IntVector(1, -1, 0), - new IntVector(1, 0, -1), - new IntVector(1, 0, 0), - new IntVector(1, 0, 1), - new IntVector(1, 1, 0), - }; - private static boolean isOccluding(Material material) { return material.isOccluding() || Config.HiddenBlocks.contains(material); } @@ -74,10 +47,11 @@ public class Hull { private final Region region; private final boolean groundVisible; private final IntVector primaryDirection; + private final IntVector[] branchDirections; private final BitSet occluding; private final BitSet visibility; - private final Int2IntMap visibilityDirections = new Int2IntOpenHashMap(); // Contains the visible directions of each occluding visible block + private final Int2IntOpenHashMap visibilityDirections = new Int2IntOpenHashMap(); // Contains the visible directions of each occluding visible block private final Set uncoveredSurface = new HashSet<>(); private final HashSet players = new HashSet<>(); @@ -90,6 +64,12 @@ public class Hull { this.primaryDirection = new IntVector(0, 0, team.isBlue() == (Config.BlueToRedZ > 0) ? -1 : 1); this.occluding = new BitSet(region.volume()); this.visibility = new BitSet(region.volume()); + + branchDirections = IntStream.range(0, 27) + .mapToObj(v -> new IntVector(v%3 -1, (v/3)%3 -1, v/9 -1)) + .filter(v -> v.x*primaryDirection.x + v.y*primaryDirection.y + v.z*primaryDirection.z == 1) // Pointing towards primary direction + .filter(v -> v.x*v.x + v.y*v.y + v.z*v.z == 2) // Diagonal + .toArray(IntVector[]::new); } public boolean blockPrecise(Player player, int chunkX, int chunkY, int chunkZ) { @@ -166,13 +146,6 @@ public class Hull { }); forEachBorder((root, direction) -> updateBlocks(new NullList<>(), root, direction)); FightSystem.getPlugin().getLogger().log(Level.INFO, () -> "[HullHider] initialisation finished: " + (System.currentTimeMillis() - start) + " ms, visible blocks: " + visibility.cardinality()); - - if(DEBUG) { - region.forEach((x, y, z) -> { - int id = new IntVector(x, y, z).toId(region); - BlockIdWrapper.impl.setBlock(Config.world, x, y + Config.BlueExtendRegion.getSizeY(), z, visibility.get(id) ? (occluding.get(id) ? RED_GLASS : AIR) : GLASS); - }); - } } public void updateBlockVisibility(Block b, Material changedType) { @@ -190,7 +163,7 @@ public class Hull { List uncovered = new ArrayList<>(); int directions = visibilityDirections.remove(id); - for(IntVector direction : DIRECTIONS) { + for(IntVector direction : branchDirections) { if((directionId(direction) & directions) != 0) updateBlocks(uncovered, root, direction); } @@ -282,10 +255,8 @@ public class Hull { if(!direction.equals(primaryDirection)) return; - for(IntVector branchDirection : DIRECTIONS) { - if(!branchDirection.equals(direction) && direction.x*branchDirection.x + direction.y*branchDirection.y + direction.z*branchDirection.z == 1) // If branch direction diagonal pointing towards direction - updateBlocks(uncovered, block.add(branchDirection), branchDirection); - } + for(IntVector branchDirection : branchDirections) + updateBlocks(uncovered, block.add(branchDirection), branchDirection); } private int directionId(IntVector v) { From 7ce5e319b5378140bebcd4df59bef970261e5d55 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Tue, 10 Dec 2024 11:08:44 +0100 Subject: [PATCH 4/6] Reduce unnecessary branch propagation by early abort with primary direction visibility. --- .../src/de/steamwar/fightsystem/utils/Hull.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/FightSystem/FightSystem_Core/src/de/steamwar/fightsystem/utils/Hull.java b/FightSystem/FightSystem_Core/src/de/steamwar/fightsystem/utils/Hull.java index fd0113c0..a7f6f170 100644 --- a/FightSystem/FightSystem_Core/src/de/steamwar/fightsystem/utils/Hull.java +++ b/FightSystem/FightSystem_Core/src/de/steamwar/fightsystem/utils/Hull.java @@ -51,6 +51,7 @@ public class Hull { private final BitSet occluding; private final BitSet visibility; + private final BitSet primaryVisible; private final Int2IntOpenHashMap visibilityDirections = new Int2IntOpenHashMap(); // Contains the visible directions of each occluding visible block private final Set uncoveredSurface = new HashSet<>(); @@ -64,6 +65,7 @@ public class Hull { this.primaryDirection = new IntVector(0, 0, team.isBlue() == (Config.BlueToRedZ > 0) ? -1 : 1); this.occluding = new BitSet(region.volume()); this.visibility = new BitSet(region.volume()); + this.primaryVisible = new BitSet(region.volume()); branchDirections = IntStream.range(0, 27) .mapToObj(v -> new IntVector(v%3 -1, (v/3)%3 -1, v/9 -1)) @@ -137,6 +139,7 @@ public class Hull { visibility.clear(); occluding.clear(); visibilityDirections.clear(); + primaryVisible.clear(); long start = System.currentTimeMillis(); region.forEach((x, y, z) -> { @@ -250,11 +253,14 @@ public class Hull { visibilityDirections.compute(id, (pos, v) -> (v == null ? 0 : v) | directionId(direction)); return; } + if(primaryVisible.get(id)) + return; updateBlocks(uncovered, block.add(direction), direction); if(!direction.equals(primaryDirection)) return; + primaryVisible.set(id); for(IntVector branchDirection : branchDirections) updateBlocks(uncovered, block.add(branchDirection), branchDirection); } From f81b95e39ff2ecfd153c66f9ec6d1afd005faa57 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Tue, 10 Dec 2024 12:57:21 +0100 Subject: [PATCH 5/6] Fix direction id. --- .../src/de/steamwar/fightsystem/utils/Hull.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/FightSystem/FightSystem_Core/src/de/steamwar/fightsystem/utils/Hull.java b/FightSystem/FightSystem_Core/src/de/steamwar/fightsystem/utils/Hull.java index a7f6f170..0ec723cc 100644 --- a/FightSystem/FightSystem_Core/src/de/steamwar/fightsystem/utils/Hull.java +++ b/FightSystem/FightSystem_Core/src/de/steamwar/fightsystem/utils/Hull.java @@ -69,7 +69,7 @@ public class Hull { branchDirections = IntStream.range(0, 27) .mapToObj(v -> new IntVector(v%3 -1, (v/3)%3 -1, v/9 -1)) - .filter(v -> v.x*primaryDirection.x + v.y*primaryDirection.y + v.z*primaryDirection.z == 1) // Pointing towards primary direction + .filter(v -> v.x*primaryDirection.x + v.y*primaryDirection.y + v.z*primaryDirection.z >= 0) // Not pointing away from primary direction .filter(v -> v.x*v.x + v.y*v.y + v.z*v.z == 2) // Diagonal .toArray(IntVector[]::new); } @@ -87,6 +87,7 @@ public class Hull { } public void addPlayer(Player player) { + System.out.println("Added " + player + " to " + region.centerZ()); if(players.add(player)) { for(Entity entity : entities) BlockIdWrapper.impl.untrackEntity(player, entity.getEntityId()); @@ -94,6 +95,7 @@ public class Hull { } public void removePlayer(Player player, boolean activeRemoval) { + System.out.println("Removed " + player + " from " + region.centerZ()); if(players.remove(player) && activeRemoval) { for(Entity entity : entities) BlockIdWrapper.impl.trackEntity(player, entity.getEntityId()); @@ -266,7 +268,7 @@ public class Hull { } private int directionId(IntVector v) { - return 9*(v.z+1) + 3*(v.y+1) + (v.x+1); + return 1 << (9*(v.z+1) + 3*(v.y+1) + (v.x+1)); } From a1e77e571bf1a5a808afae8786628129ac725cf3 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Wed, 11 Dec 2024 09:52:05 +0100 Subject: [PATCH 6/6] Fix entity hider. --- .../fightsystem/utils/BlockIdWrapper14.java | 9 ++-- FightSystem/FightSystem_18/build.gradle.kts | 1 + .../fightsystem/utils/BlockIdWrapper18.java | 45 +++++++++++++++++++ .../fightsystem/utils/BlockIdWrapper8.java | 9 ++-- .../fightsystem/utils/BlockIdWrapper.java | 5 ++- .../de/steamwar/fightsystem/utils/Hull.java | 12 +++-- 6 files changed, 64 insertions(+), 17 deletions(-) create mode 100644 FightSystem/FightSystem_18/src/de/steamwar/fightsystem/utils/BlockIdWrapper18.java diff --git a/FightSystem/FightSystem_14/src/de/steamwar/fightsystem/utils/BlockIdWrapper14.java b/FightSystem/FightSystem_14/src/de/steamwar/fightsystem/utils/BlockIdWrapper14.java index f2330e12..5030cb04 100644 --- a/FightSystem/FightSystem_14/src/de/steamwar/fightsystem/utils/BlockIdWrapper14.java +++ b/FightSystem/FightSystem_14/src/de/steamwar/fightsystem/utils/BlockIdWrapper14.java @@ -26,6 +26,7 @@ import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import org.bukkit.Material; import org.bukkit.World; import org.bukkit.block.Block; +import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import java.util.Map; @@ -72,16 +73,16 @@ public class BlockIdWrapper14 implements BlockIdWrapper { private static final Class entityTracker = Reflection.getClass("{nms.server.level}.PlayerChunkMap$EntityTracker"); private static final Reflection.MethodInvoker updatePlayer = Reflection.getMethod(entityTracker, Core.getVersion() > 15 ? "b" : "updatePlayer", entityPlayer); @Override - public void trackEntity(Player player, int entity) { - Object tracker = trackers.get(entity); + public void trackEntity(Player player, Entity entity) { + Object tracker = trackers.get(entity.getEntityId()); if(tracker != null) updatePlayer.invoke(tracker, getPlayer.invoke(player)); } private static final Reflection.MethodInvoker clearPlayer = Reflection.getMethod(entityTracker, Core.getVersion() > 15 ? "a" : "clear", entityPlayer); @Override - public void untrackEntity(Player player, int entity) { - Object tracker = trackers.get(entity); + public void untrackEntity(Player player, Entity entity) { + Object tracker = trackers.get(entity.getEntityId()); if(tracker != null) clearPlayer.invoke(tracker, getPlayer.invoke(player)); } diff --git a/FightSystem/FightSystem_18/build.gradle.kts b/FightSystem/FightSystem_18/build.gradle.kts index fa12144d..b140d148 100644 --- a/FightSystem/FightSystem_18/build.gradle.kts +++ b/FightSystem/FightSystem_18/build.gradle.kts @@ -24,6 +24,7 @@ plugins { dependencies { compileOnly(project(":SpigotCore", "default")) compileOnly(project(":FightSystem:FightSystem_Core", "default")) + compileOnly(project(":FightSystem:FightSystem_14", "default")) compileOnly(libs.spigotapi) diff --git a/FightSystem/FightSystem_18/src/de/steamwar/fightsystem/utils/BlockIdWrapper18.java b/FightSystem/FightSystem_18/src/de/steamwar/fightsystem/utils/BlockIdWrapper18.java new file mode 100644 index 00000000..d1ad4197 --- /dev/null +++ b/FightSystem/FightSystem_18/src/de/steamwar/fightsystem/utils/BlockIdWrapper18.java @@ -0,0 +1,45 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2024 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.fightsystem.utils; + +import com.comphenix.tinyprotocol.Reflection; +import org.bukkit.entity.Entity; +import org.bukkit.entity.Player; + +import java.util.Collections; +import java.util.Map; +import java.util.Set; +import java.util.UUID; + +public class BlockIdWrapper18 extends BlockIdWrapper14 { + + private static final Reflection.FieldAccessor hiddenEntities = Reflection.getField(Reflection.getClass("{obc}.entity.CraftPlayer"), Map.class, 0, UUID.class, Set.class); + @Override + public void trackEntity(Player player, Entity entity) { + hiddenEntities.get(player).remove(entity.getUniqueId()); + super.trackEntity(player, entity); + } + + @Override + public void untrackEntity(Player player, Entity entity) { + hiddenEntities.get(player).put(entity.getUniqueId(), Collections.emptySet()); + super.untrackEntity(player, entity); + } +} diff --git a/FightSystem/FightSystem_8/src/de/steamwar/fightsystem/utils/BlockIdWrapper8.java b/FightSystem/FightSystem_8/src/de/steamwar/fightsystem/utils/BlockIdWrapper8.java index d330cfbc..402f8e60 100644 --- a/FightSystem/FightSystem_8/src/de/steamwar/fightsystem/utils/BlockIdWrapper8.java +++ b/FightSystem/FightSystem_8/src/de/steamwar/fightsystem/utils/BlockIdWrapper8.java @@ -24,6 +24,7 @@ import de.steamwar.fightsystem.Config; import org.bukkit.Material; import org.bukkit.World; import org.bukkit.block.Block; +import org.bukkit.entity.Entity; import org.bukkit.entity.Player; public class BlockIdWrapper8 implements BlockIdWrapper { @@ -57,16 +58,16 @@ public class BlockIdWrapper8 implements BlockIdWrapper { private static final Reflection.MethodInvoker get = Reflection.getTypedMethod(intHashMap, "get", Object.class, int.class); private static final Reflection.MethodInvoker updatePlayer = Reflection.getMethod(entityTrackerEntry, "updatePlayer", entityPlayer); @Override - public void trackEntity(Player player, int entity) { - Object tracker = get.invoke(trackers, entity); + public void trackEntity(Player player, Entity entity) { + Object tracker = get.invoke(trackers, entity.getEntityId()); if(tracker != null) updatePlayer.invoke(tracker, getPlayer.invoke(player)); } private static final Reflection.MethodInvoker clearPlayer = Reflection.getMethod(entityTrackerEntry, "a", entityPlayer); @Override - public void untrackEntity(Player player, int entity) { - Object tracker = get.invoke(trackers, entity); + public void untrackEntity(Player player, Entity entity) { + Object tracker = get.invoke(trackers, entity.getEntityId()); if(tracker != null) clearPlayer.invoke(tracker, getPlayer.invoke(player)); } diff --git a/FightSystem/FightSystem_Core/src/de/steamwar/fightsystem/utils/BlockIdWrapper.java b/FightSystem/FightSystem_Core/src/de/steamwar/fightsystem/utils/BlockIdWrapper.java index a6b825db..354ff9fd 100644 --- a/FightSystem/FightSystem_Core/src/de/steamwar/fightsystem/utils/BlockIdWrapper.java +++ b/FightSystem/FightSystem_Core/src/de/steamwar/fightsystem/utils/BlockIdWrapper.java @@ -25,6 +25,7 @@ import de.steamwar.fightsystem.FightSystem; import org.bukkit.Material; import org.bukkit.World; import org.bukkit.block.Block; +import org.bukkit.entity.Entity; import org.bukkit.entity.Player; public interface BlockIdWrapper { @@ -41,6 +42,6 @@ public interface BlockIdWrapper { int blockToId(Block block); void setBlock(World world, int x, int y, int z, int blockState); - void trackEntity(Player player, int entity); - void untrackEntity(Player player, int entity); + void trackEntity(Player player, Entity entity); + void untrackEntity(Player player, Entity entity); } diff --git a/FightSystem/FightSystem_Core/src/de/steamwar/fightsystem/utils/Hull.java b/FightSystem/FightSystem_Core/src/de/steamwar/fightsystem/utils/Hull.java index 0ec723cc..5b145c1d 100644 --- a/FightSystem/FightSystem_Core/src/de/steamwar/fightsystem/utils/Hull.java +++ b/FightSystem/FightSystem_Core/src/de/steamwar/fightsystem/utils/Hull.java @@ -87,18 +87,16 @@ public class Hull { } public void addPlayer(Player player) { - System.out.println("Added " + player + " to " + region.centerZ()); if(players.add(player)) { for(Entity entity : entities) - BlockIdWrapper.impl.untrackEntity(player, entity.getEntityId()); + BlockIdWrapper.impl.untrackEntity(player, entity); } } public void removePlayer(Player player, boolean activeRemoval) { - System.out.println("Removed " + player + " from " + region.centerZ()); if(players.remove(player) && activeRemoval) { for(Entity entity : entities) - BlockIdWrapper.impl.trackEntity(player, entity.getEntityId()); + BlockIdWrapper.impl.trackEntity(player, entity); // techhider triggers block change sending } } @@ -108,12 +106,12 @@ public class Hull { if(region.inRegion(location) && !visibility.get(new IntVector(location).toId(region))) { if(entities.add(entity)) { for(Player player : players) - BlockIdWrapper.impl.untrackEntity(player, entity.getEntityId()); + BlockIdWrapper.impl.untrackEntity(player, entity); } } else { if(entities.remove(entity)) { for(Player player : players) - BlockIdWrapper.impl.trackEntity(player, entity.getEntityId()); + BlockIdWrapper.impl.trackEntity(player, entity); } } } @@ -183,7 +181,7 @@ public class Hull { if(uncoveredSet.contains(new IntVector(entity.getLocation()))) { it.remove(); for(Player player : players) - BlockIdWrapper.impl.trackEntity(player, entity.getEntityId()); + BlockIdWrapper.impl.trackEntity(player, entity); } }