From e3535c379a737ef7e000894b3aa8b9baa68bb6e8 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Thu, 7 Mar 2024 12:54:13 +0100 Subject: [PATCH] Block HullHider Untested Signed-off-by: Lixfel --- FightSystem_18/build.gradle | 1 + .../fightsystem/utils/HullHiderWrapper18.java | 69 +++++++++++++++++++ FightSystem_20/build.gradle | 1 + .../fightsystem/utils/HullHiderWrapper20.java | 32 +++++++++ .../de/steamwar/fightsystem/utils/Hull.java | 31 ++++++++- .../steamwar/fightsystem/utils/HullHider.java | 22 +++++- .../fightsystem/utils/HullHiderWrapper.java | 31 +++++++++ .../de/steamwar/fightsystem/utils/Region.java | 6 ++ .../fightsystem/utils/TechHiderWrapper.java | 55 +++++++++++---- 9 files changed, 230 insertions(+), 18 deletions(-) create mode 100644 FightSystem_18/src/de/steamwar/fightsystem/utils/HullHiderWrapper18.java create mode 100644 FightSystem_20/src/de/steamwar/fightsystem/utils/HullHiderWrapper20.java create mode 100644 FightSystem_Core/src/de/steamwar/fightsystem/utils/HullHiderWrapper.java diff --git a/FightSystem_18/build.gradle b/FightSystem_18/build.gradle index 91b8b08..7b0940e 100644 --- a/FightSystem_18/build.gradle +++ b/FightSystem_18/build.gradle @@ -48,6 +48,7 @@ dependencies { compileOnly 'com.mojang:datafixerupper:4.0.26' compileOnly 'io.netty:netty-all:4.1.68.Final' compileOnly 'com.mojang:authlib:1.5.25' + compileOnly 'it.unimi.dsi:fastutil:8.5.6' compileOnly swdep("Spigot-1.18") compileOnly swdep("WorldEdit-1.15") diff --git a/FightSystem_18/src/de/steamwar/fightsystem/utils/HullHiderWrapper18.java b/FightSystem_18/src/de/steamwar/fightsystem/utils/HullHiderWrapper18.java new file mode 100644 index 0000000..0781fe4 --- /dev/null +++ b/FightSystem_18/src/de/steamwar/fightsystem/utils/HullHiderWrapper18.java @@ -0,0 +1,69 @@ +/* + * 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 de.steamwar.fightsystem.Config; +import it.unimi.dsi.fastutil.shorts.Short2ObjectArrayMap; +import net.minecraft.core.BlockPosition; +import net.minecraft.core.SectionPosition; +import net.minecraft.network.protocol.game.PacketPlayOutBlockChange; +import net.minecraft.network.protocol.game.PacketPlayOutMultiBlockChange; +import net.minecraft.world.level.block.state.IBlockData; + +import java.util.List; + + +public class HullHiderWrapper18 implements HullHiderWrapper { + + private static final Reflection.MethodInvoker getState = Reflection.getTypedMethod(Reflection.getClass("{obc}.CraftBlockData"), "getState", IBlockData.class); + @Override + public Object generateBlockChangePacket(List changes) { + Object[] blockdata = new Object[changes.size()]; + + for(int i = 0; i < changes.size(); i++) { + Hull.IntVector change = changes.get(i); + blockdata[i] = getState.invoke(Config.world.getBlockData(change.getX(), change.getY(), change.getZ())); + } + + return generateBlockChangePacket(changes, blockdata); + } + + private Object generateBlockChangePacket(List changes, Object[] blockdata) { + if(changes.size() > 1) { + 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); + } + + Hull.IntVector section = changes.get(0); + return constructMultiBlockChange(new Hull.IntVector(section.getX() >> 4, section.getY() >> 4, section.getZ() >> 4), pos, blockdata); + } else { + Hull.IntVector pos = changes.get(0); + return new PacketPlayOutBlockChange(new BlockPosition(pos.getX(), pos.getY(), pos.getZ()), (IBlockData) blockdata[0]); + } + } + + protected Object constructMultiBlockChange(Hull.IntVector section, short[] pos, Object[] blockdata) { + return new PacketPlayOutMultiBlockChange(SectionPosition.a(section.getX(), section.getY(), section.getZ()), new Short2ObjectArrayMap<>(pos, blockdata, blockdata.length), false); + } +} diff --git a/FightSystem_20/build.gradle b/FightSystem_20/build.gradle index ca641a3..51b5737 100644 --- a/FightSystem_20/build.gradle +++ b/FightSystem_20/build.gradle @@ -46,6 +46,7 @@ dependencies { implementation project(":FightSystem_18") compileOnly 'org.spigotmc:spigot-api:1.20-R0.1-SNAPSHOT' + compileOnly 'it.unimi.dsi:fastutil:8.5.6' compileOnly swdep("Spigot-1.20") } diff --git a/FightSystem_20/src/de/steamwar/fightsystem/utils/HullHiderWrapper20.java b/FightSystem_20/src/de/steamwar/fightsystem/utils/HullHiderWrapper20.java new file mode 100644 index 0000000..48bf0ad --- /dev/null +++ b/FightSystem_20/src/de/steamwar/fightsystem/utils/HullHiderWrapper20.java @@ -0,0 +1,32 @@ +/* + * 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 it.unimi.dsi.fastutil.shorts.Short2ObjectArrayMap; +import net.minecraft.core.SectionPosition; +import net.minecraft.network.protocol.game.PacketPlayOutMultiBlockChange; + +public class HullHiderWrapper20 extends HullHiderWrapper18 { + + @Override + protected Object constructMultiBlockChange(Hull.IntVector section, short[] pos, Object[] blockdata) { + return new PacketPlayOutMultiBlockChange(SectionPosition.a(section.getX(), section.getY(), section.getZ()), new Short2ObjectArrayMap<>(pos, blockdata, blockdata.length)); + } +} diff --git a/FightSystem_Core/src/de/steamwar/fightsystem/utils/Hull.java b/FightSystem_Core/src/de/steamwar/fightsystem/utils/Hull.java index 118e564..2e95a7b 100644 --- a/FightSystem_Core/src/de/steamwar/fightsystem/utils/Hull.java +++ b/FightSystem_Core/src/de/steamwar/fightsystem/utils/Hull.java @@ -19,10 +19,12 @@ package de.steamwar.fightsystem.utils; +import com.comphenix.tinyprotocol.TinyProtocol; import de.steamwar.entity.REntity; import de.steamwar.fightsystem.Config; import de.steamwar.fightsystem.FightSystem; import de.steamwar.fightsystem.fight.FightTeam; +import lombok.Getter; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Block; @@ -45,6 +47,7 @@ public class Hull { private final BitSet occluding; private final BitSet visibility; private final Map> blockVisibility = new HashMap<>(); + private final Set uncoveredSurface = new HashSet<>(); private final Set players = new HashSet<>(); private final Set entities = new HashSet<>(); @@ -90,6 +93,10 @@ 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())); + } + public boolean isLocationHidden(Player player, Location location) { return players.contains(player) && region.inRegion(location) && !visibility.get(new IntVector(location).toId(region)); } @@ -107,6 +114,7 @@ public class Hull { if(players.remove(player) && activeRemoval) { for(Entity entity : entities) player.showEntity(FightSystem.getPlugin(), entity); + // techhider triggers block change sending } } @@ -147,6 +155,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(); @@ -209,6 +218,23 @@ public class Hull { entity.hide(false); } } + + uncoveredSurface.addAll(uncoveredSet); + uncoveredSurface.remove(root); + } + + public void sendUncoveredBlocks() { + Map> sectionWise = new HashMap<>(); + + for(IntVector uncovered : uncoveredSurface) { + sectionWise.computeIfAbsent(new IntVector(uncovered.x >> 4, uncovered.y >> 4, uncovered.z >> 4), section -> new ArrayList<>()).add(uncovered); + } + uncoveredSurface.clear(); + + for (Map.Entry> entry : sectionWise.entrySet()) { + Object packet = HullHiderWrapper.impl.generateBlockChangePacket(entry.getValue()); + players.forEach(player -> TinyProtocol.instance.sendPacket(player, packet)); + } } private void forEachBorder(BiConsumer f) { @@ -280,9 +306,12 @@ public class Hull { } - private static class IntVector { + public static class IntVector { + @Getter private final int x; + @Getter private final int y; + @Getter private final int z; public IntVector(int x, int y, int z) { diff --git a/FightSystem_Core/src/de/steamwar/fightsystem/utils/HullHider.java b/FightSystem_Core/src/de/steamwar/fightsystem/utils/HullHider.java index 06363e3..108dce2 100644 --- a/FightSystem_Core/src/de/steamwar/fightsystem/utils/HullHider.java +++ b/FightSystem_Core/src/de/steamwar/fightsystem/utils/HullHider.java @@ -56,13 +56,15 @@ import java.util.function.Function; public class HullHider implements Listener { - private static final boolean ENABLED = TechHiderWrapper.ENABLED && Core.getVersion() >= 18; + public static final boolean ENABLED = TechHiderWrapper.ENABLED && Core.getVersion() >= 18; private final Map hulls = new HashMap<>(); private final Map, BiFunction> packetHiders = new HashMap<>(); public HullHider() { - if(ENABLED) - Fight.teams().forEach(team -> hulls.put(team, new Hull(team))); + if(!ENABLED) + return; + + Fight.teams().forEach(team -> hulls.put(team, new Hull(team))); packetHiders.put(packetPlayOutWorldEvent, this::worldEventHider); packetHiders.put(packetPlayOutExplosion, this::explosionHider); @@ -137,6 +139,17 @@ public class HullHider implements Listener { hull.updateBlockVisibility(block, changedType); } + public boolean isBlockHidden(Player player, int x, int y, int z) { + if(!ENABLED) + return false; + + for (Hull hull : hulls.values()) + if(hull.isBlockHidden(player, x, y, z)) + return true; + + return false; + } + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onSpawn(EntitySpawnEvent e) { @@ -148,6 +161,9 @@ public class HullHider implements Listener { for (Hull hull : hulls.values()) hull.checkEntity(entity); }); + + for (Hull hull : hulls.values()) + hull.sendUncoveredBlocks(); } @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) diff --git a/FightSystem_Core/src/de/steamwar/fightsystem/utils/HullHiderWrapper.java b/FightSystem_Core/src/de/steamwar/fightsystem/utils/HullHiderWrapper.java new file mode 100644 index 0000000..17a5195 --- /dev/null +++ b/FightSystem_Core/src/de/steamwar/fightsystem/utils/HullHiderWrapper.java @@ -0,0 +1,31 @@ +/* + * 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 de.steamwar.core.VersionDependent; +import de.steamwar.fightsystem.FightSystem; + +import java.util.List; + +public interface HullHiderWrapper { + HullHiderWrapper impl = VersionDependent.getVersionImpl(FightSystem.getPlugin()); + + Object generateBlockChangePacket(List changes); +} diff --git a/FightSystem_Core/src/de/steamwar/fightsystem/utils/Region.java b/FightSystem_Core/src/de/steamwar/fightsystem/utils/Region.java index 025daf3..b789641 100644 --- a/FightSystem_Core/src/de/steamwar/fightsystem/utils/Region.java +++ b/FightSystem_Core/src/de/steamwar/fightsystem/utils/Region.java @@ -80,6 +80,12 @@ public class Region { getMinChunkZ() > cZ || cZ > getMaxChunkZ(); } + public boolean chunkSectionOutside(int cX, int cY, int cZ) { + return getMinChunkX() > cX || cX > getMaxChunkX() || + ProtocolUtils.posToChunk(minY) > cY || cY > ProtocolUtils.posToChunk(maxY) || + getMinChunkZ() > cZ || cZ > getMaxChunkZ(); + } + public void forEachChunk(ObjIntConsumer executor) { for(int x = getMinChunkX(); x <= getMaxChunkX(); x++) for(int z = getMinChunkZ(); z <= getMaxChunkZ(); z++) diff --git a/FightSystem_Core/src/de/steamwar/fightsystem/utils/TechHiderWrapper.java b/FightSystem_Core/src/de/steamwar/fightsystem/utils/TechHiderWrapper.java index 9638292..b47b29c 100644 --- a/FightSystem_Core/src/de/steamwar/fightsystem/utils/TechHiderWrapper.java +++ b/FightSystem_Core/src/de/steamwar/fightsystem/utils/TechHiderWrapper.java @@ -37,14 +37,17 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; -public class TechHiderWrapper extends StateDependent { +public class TechHiderWrapper extends StateDependent implements TechHider.LocationEvaluator { 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 TechHider techHider; public TechHiderWrapper() { super(ENABLED, FightState.Schem); - techHider = new TechHider(this::bypass, Material.getMaterial(Config.ObfuscateWith), Config.HiddenBlocks, Config.HiddenBlockEntities); + techHider = new TechHider(this, Material.getMaterial(Config.ObfuscateWith), Config.HiddenBlocks, Config.HiddenBlockEntities); register(); } @@ -64,7 +67,7 @@ public class TechHiderWrapper extends StateDependent { List chunksToReload = new ArrayList<>(); Config.ArenaRegion.forEachChunk((x, z) -> { - if(bypass((Player) p, x, z) == hide) + if(skipChunk((Player) p, x, z) == hide) chunksToReload.add(new ProtocolUtils.ChunkPos(x, z)); }); return chunksToReload; @@ -76,23 +79,47 @@ public class TechHiderWrapper extends StateDependent { Bukkit.getScheduler().runTaskLater(FightSystem.getPlugin(), () -> { for(ProtocolUtils.ChunkPos chunk : chunksToReload){ - if(bypass(p, chunk.x(), chunk.z()) != hide) + if(skipChunk(p, chunk.x(), chunk.z()) != hide) CraftbukkitWrapper.impl.sendChunk(p, chunk.x(), chunk.z()); } }, 40); } - private boolean bypass(Player p, int chunkX, int chunkZ){ - if(Config.isReferee(p)) - return true; + @Override + public boolean skipChunk(Player player, int chunkX, int chunkZ) { + return getHiddenRegion(player).chunkOutside(chunkX, chunkZ); + } - FightTeam ft = Fight.getPlayerTeam(p); - if(ft == null){ - return Config.ArenaRegion.chunkOutside(chunkX, chunkZ); - }else if(ft.isBlue()){ - return ft.canPlayerEntern(p) || Config.RedExtendRegion.chunkOutside(chunkX, chunkZ); - }else{ - return ft.canPlayerEntern(p) || Config.BlueExtendRegion.chunkOutside(chunkX, chunkZ); + @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(FightSystem.getHullHider().isBlockHidden(player, x, y, z)) { + return TechHider.State.HIDE; + } else { + return TechHider.State.CHECK; + } + } else { + return TechHider.State.SKIP; } } + + private Region getHiddenRegion(Player player) { + if(Config.isReferee(player)) + return empty; + + FightTeam team = Fight.getPlayerTeam(player); + if(team == null) + return Config.ArenaRegion; + + if(team.canPlayerEntern(player)) + return empty; + + return Fight.getOpposite(team).getExtendRegion(); + } }