diff --git a/FightSystem_14/build.gradle b/FightSystem_14/build.gradle index 29ff288..1d2a18c 100644 --- a/FightSystem_14/build.gradle +++ b/FightSystem_14/build.gradle @@ -46,6 +46,8 @@ dependencies { implementation project(":FightSystem_9") implementation project(":FightSystem_8") + compileOnly 'it.unimi.dsi:fastutil:8.5.6' + compileOnly swdep("Spigot-1.14") compileOnly swdep("WorldEdit-1.15") compileOnly swdep("SpigotCore") diff --git a/FightSystem_14/src/de/steamwar/fightsystem/utils/BlockIdWrapper14.java b/FightSystem_14/src/de/steamwar/fightsystem/utils/BlockIdWrapper14.java index 934d9b6..f2330e1 100644 --- a/FightSystem_14/src/de/steamwar/fightsystem/utils/BlockIdWrapper14.java +++ b/FightSystem_14/src/de/steamwar/fightsystem/utils/BlockIdWrapper14.java @@ -21,18 +21,31 @@ package de.steamwar.fightsystem.utils; import com.comphenix.tinyprotocol.Reflection; import de.steamwar.core.Core; +import de.steamwar.fightsystem.Config; +import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import org.bukkit.Material; import org.bukkit.World; import org.bukkit.block.Block; +import org.bukkit.entity.Player; + +import java.util.Map; public class BlockIdWrapper14 implements BlockIdWrapper { - private static final Class worldServer = Reflection.getClass("{nms.server.level}.WorldServer"); private static final Class chunkProviderServer = Reflection.getClass("{nms.server.level}.ChunkProviderServer"); + private static final Reflection.MethodInvoker getChunkProvider = Reflection.getTypedMethod(worldServer, null, chunkProviderServer); + private static final Class playerChunkMap = Reflection.getClass("{nms.server.level}.PlayerChunkMap"); + private static final Reflection.FieldAccessor getPlayerChunkMap = Reflection.getField(chunkProviderServer, playerChunkMap, 0); + private static final Reflection.FieldAccessor entityTrackers = Core.getVersion() > 15 ? Reflection.getField(playerChunkMap, Int2ObjectMap.class, 0) : Reflection.getField(playerChunkMap, org.bukkit.craftbukkit.libs.it.unimi.dsi.fastutil.ints.Int2ObjectMap.class, 0); private static final Class block = Reflection.getClass("{nms.world.level.block}.Block"); private static final Class iBlockData = Reflection.getClass("{nms.world.level.block.state}.IBlockData"); private static final Class blockPosition = Reflection.getClass("{nms.core}.BlockPosition"); + private final Map trackers; + public BlockIdWrapper14() { + trackers = entityTrackers.get(getPlayerChunkMap.get(getChunkProvider.invoke(getWorldHandle.invoke(Config.world)))); + } + private static final Reflection.MethodInvoker getCombinedId = Reflection.getTypedMethod(block, null, int.class, iBlockData); private static final Reflection.MethodInvoker getNMS = Reflection.getTypedMethod(Reflection.getClass("{obc}.block.CraftBlock"), "getNMS", iBlockData); @Override @@ -41,11 +54,9 @@ public class BlockIdWrapper14 implements BlockIdWrapper { } private static final Reflection.MethodInvoker getByCombinedId = Reflection.getTypedMethod(block, null, iBlockData, int.class); - private static final Reflection.MethodInvoker getWorldHandle = Reflection.getTypedMethod(Reflection.getClass("{obc}.CraftWorld"), "getHandle", worldServer); private static final Reflection.ConstructorInvoker newBlockPosition = Reflection.getConstructor(blockPosition, int.class, int.class, int.class); private static final Reflection.MethodInvoker getTypeAndData = Reflection.getMethod(worldServer, null, blockPosition, iBlockData, int.class); private static final Reflection.MethodInvoker removeTileEntity = Reflection.getMethod(worldServer, Core.getVersion() > 15 ? "m" : "removeTileEntity", blockPosition); - private static final Reflection.MethodInvoker getChunkProvider = Reflection.getTypedMethod(worldServer, null, chunkProviderServer); private static final Reflection.MethodInvoker flagDirty = Reflection.getMethod(chunkProviderServer, null, blockPosition); @Override public void setBlock(World world, int x, int y, int z, int blockState) { @@ -58,6 +69,23 @@ public class BlockIdWrapper14 implements BlockIdWrapper { flagDirty.invoke(getChunkProvider.invoke(nworld), pos); } + 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); + 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); + if(tracker != null) + clearPlayer.invoke(tracker, getPlayer.invoke(player)); + } + private static final Reflection.MethodInvoker getMaterialByBlock = Reflection.getTypedMethod(Reflection.getClass("{obc}.util.CraftMagicNumbers"), "getMaterial", Material.class, block); private static final Reflection.MethodInvoker getBlockByBlockData = Reflection.getTypedMethod(iBlockData, null, block); @Override 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..8f81cf9 --- /dev/null +++ b/FightSystem_18/src/de/steamwar/fightsystem/utils/HullHiderWrapper18.java @@ -0,0 +1,82 @@ +/* + * 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 org.bukkit.block.data.BlockData; + +import java.util.ArrayList; +import java.util.List; + + +public class HullHiderWrapper18 implements HullHiderWrapper { + + private static final Reflection.MethodInvoker getState = Reflection.getTypedMethod(Reflection.getClass("{obc}.block.data.CraftBlockData"), "getState", IBlockData.class); + @Override + public Object generateBlockChangePacket(List changes) { + List blockdata = new ArrayList<>(changes.size()); + + changes.removeIf(change -> { + BlockData data = Config.world.getBlockData(change.getX(), change.getY(), change.getZ()); + boolean unchanged = data.getMaterial() == Config.ObfuscateWith || Config.HiddenBlocks.contains(data.getMaterial()); + if(!unchanged) + blockdata.add(getState.invoke(data)); + return unchanged; + }); + + if(changes.isEmpty()) + return null; + + return generateBlockChangePacket(changes, blockdata.toArray()); + } + + private Object generateBlockChangePacket(List changes, Object[] blockdata) { + if(changes.size() > 1) { + Hull.IntVector section = changes.get(0); + 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()-xOffset) << 8) + ((change.getZ()-zOffset) << 4) + (change.getY()-yOffset)); + } + + 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]); + } + } + + 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_8/src/de/steamwar/fightsystem/utils/BlockIdWrapper8.java b/FightSystem_8/src/de/steamwar/fightsystem/utils/BlockIdWrapper8.java index cbf186a..d330cfb 100644 --- a/FightSystem_8/src/de/steamwar/fightsystem/utils/BlockIdWrapper8.java +++ b/FightSystem_8/src/de/steamwar/fightsystem/utils/BlockIdWrapper8.java @@ -19,11 +19,25 @@ package de.steamwar.fightsystem.utils; +import com.comphenix.tinyprotocol.Reflection; +import de.steamwar.fightsystem.Config; import org.bukkit.Material; import org.bukkit.World; import org.bukkit.block.Block; +import org.bukkit.entity.Player; public class BlockIdWrapper8 implements BlockIdWrapper { + + private static final Class entityTracker = Reflection.getClass("{nms}.EntityTracker"); + private static final Reflection.FieldAccessor getEntityTracker = Reflection.getField(worldServer, entityTracker, 0); + private static final Class intHashMap = Reflection.getClass("{nms}.IntHashMap"); + private static final Reflection.FieldAccessor getTrackedEntities = Reflection.getField(entityTracker, intHashMap, 0); + + private final Object trackers; + public BlockIdWrapper8() { + trackers = getTrackedEntities.get(getEntityTracker.get(getWorldHandle.invoke(Config.world))); + } + @Override @SuppressWarnings("deprecation") public int blockToId(Block block) { @@ -39,6 +53,24 @@ public class BlockIdWrapper8 implements BlockIdWrapper { world.getBlockAt(x, y, z).setTypeIdAndData(blockState >> 4, (byte)(blockState & 0b1111), false); } + private static final Class entityTrackerEntry = Reflection.getClass("{nms}.EntityTrackerEntry"); + 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); + 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); + if(tracker != null) + clearPlayer.invoke(tracker, getPlayer.invoke(player)); + } + @Override @SuppressWarnings("deprecation") public Material idToMaterial(int blockState) { diff --git a/FightSystem_8/src/de/steamwar/fightsystem/utils/HullHiderWrapper8.java b/FightSystem_8/src/de/steamwar/fightsystem/utils/HullHiderWrapper8.java new file mode 100644 index 0000000..524842e --- /dev/null +++ b/FightSystem_8/src/de/steamwar/fightsystem/utils/HullHiderWrapper8.java @@ -0,0 +1,55 @@ +/* + * 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 org.bukkit.Material; + +import java.util.List; + +public class HullHiderWrapper8 implements HullHiderWrapper { + + private static final Reflection.ConstructorInvoker newMultiBlockChange = Reflection.getConstructor("{nms}.PacketPlayOutMultiBlockChange", int.class, short[].class, Reflection.getClass("{nms}.Chunk")); + private static final Reflection.MethodInvoker getHandle = Reflection.getMethod("{obc}.CraftChunk", "getHandle"); + @Override + public Object generateBlockChangePacket(List changes) { + changes.removeIf(change -> { + Material material = Config.world.getBlockAt(change.getX(), change.getY(), change.getZ()).getType(); + return material == Config.ObfuscateWith || Config.HiddenBlocks.contains(material); + }); + + if(changes.isEmpty()) + return null; + + Hull.IntVector chunk = changes.get(0); + chunk = new Hull.IntVector(chunk.getX() >> 4, chunk.getY() >> 4, chunk.getZ() >> 4); + int xOffset = 16*chunk.getX(); + int zOffset = 16*chunk.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()-xOffset) << 12) + ((change.getZ()-zOffset) << 8) + change.getY()); + } + + return newMultiBlockChange.invoke(pos.length, pos, getHandle.invoke(Config.world.getChunkAt(chunk.getX(), chunk.getZ()))); + } +} diff --git a/FightSystem_Core/src/de/steamwar/fightsystem/Config.java b/FightSystem_Core/src/de/steamwar/fightsystem/Config.java index dba60ee..d58ebbb 100644 --- a/FightSystem_Core/src/de/steamwar/fightsystem/Config.java +++ b/FightSystem_Core/src/de/steamwar/fightsystem/Config.java @@ -21,10 +21,7 @@ package de.steamwar.fightsystem; import de.steamwar.fightsystem.utils.Region; import de.steamwar.fightsystem.winconditions.Winconditions; -import de.steamwar.sql.Event; -import de.steamwar.sql.EventFight; -import de.steamwar.sql.SteamwarUser; -import de.steamwar.sql.Team; +import de.steamwar.sql.*; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; @@ -122,11 +119,11 @@ public class Config { public static final boolean TechhiderActive; public static final Set HiddenBlocks; public static final Set HiddenBlockEntities; - public static final String ObfuscateWith; + public static final Material ObfuscateWith; //event parameter - private static final int EventKampfID; public static final EventFight EventKampf; + private static final Set Referees; public static final int EventTeamBlueID; public static final int EventTeamRedID; public static final boolean BothTeamsPublic; @@ -141,10 +138,14 @@ public class Config { //replay system parameter public static final String spectateIP = "127.0.0.1"; - public static final int spectatePort = 2222; + public static final int SpectatePort; public static final int ReplayID; static{ + CheckSchemID = Integer.parseInt(System.getProperty("checkSchemID", "0")); + PrepareSchemID = Integer.parseInt(System.getProperty("prepareSchemID", "0")); + ReplayID = Integer.parseInt(System.getProperty("replay", "0")); + String configFile = System.getProperty("config", "config.yml"); if(!new File(FightSystem.getPlugin().getDataFolder(), configFile).exists()) { FightSystem.getPlugin().saveDefaultConfig(); @@ -213,7 +214,7 @@ public class Config { ForbiddenItems = Collections.unmodifiableSet(config.getStringList("Kits.ForbiddenItems").stream().map(Material::valueOf).collect(Collectors.toSet())); TechhiderActive = config.getBoolean("Techhider.Active", false); - ObfuscateWith = config.getString("Techhider.ObfuscateWith", "end_stone").toUpperCase(); + ObfuscateWith = Material.getMaterial(config.getString("Techhider.ObfuscateWith", "end_stone").toUpperCase()); HiddenBlocks = config.getStringList("Techhider.HiddenBlocks").stream().map(String::toUpperCase).map(Material::getMaterial).collect(Collectors.toSet()); HiddenBlockEntities = Collections.unmodifiableSet(new HashSet<>(config.getStringList("Techhider.HiddenBlockEntities"))); @@ -317,17 +318,17 @@ public class Config { ArenaRegion = Region.withExtension(arenaMinX, blueCornerY, arenaMinZ, arenaMaxX - arenaMinX, schemsizeY, arenaMaxZ - arenaMinZ, 0, PreperationArea, 0); PlayerRegion = new Region(arenaMinX, underBorder, arenaMinZ, arenaMaxX, world.getMaxHeight(), arenaMaxZ); - EventKampfID = Integer.parseInt(System.getProperty("fightID", "0")); - if(EventKampfID >= 1){ - EventFight eventFight = EventFight.get(EventKampfID); - if(eventFight == null){ + int eventKampfID = Integer.parseInt(System.getProperty("fightID", "0")); + if(eventKampfID >= 1){ + EventKampf = EventFight.get(eventKampfID); + if(EventKampf == null){ Bukkit.getLogger().log(Level.SEVERE, "Failed to load EventFight"); Bukkit.shutdown(); } - assert eventFight != null; - Team team1 = Team.get(eventFight.getTeamBlue()); - Team team2 = Team.get(eventFight.getTeamRed()); + assert EventKampf != null; + Team team1 = Team.get(EventKampf.getTeamBlue()); + Team team2 = Team.get(EventKampf.getTeamRed()); if(team1 == null || team2 == null){ Bukkit.getLogger().log(Level.SEVERE, "Failed to load Team"); @@ -343,9 +344,11 @@ public class Config { EventTeamBlueID = team1.getTeamId(); EventTeamRedID = team2.getTeamId(); BothTeamsPublic = EventTeamRedID == 0 && EventTeamBlueID == 0; - EventKampf = eventFight; + SpectatePort = EventKampf.getSpectatePort(); + LiveReplay = SpectatePort != 0; + Referees = Referee.get(Config.EventKampf.getEventID()); - Event event = Event.get(eventFight.getEventID()); + Event event = Event.get(EventKampf.getEventID()); if(BothTeamsPublic) { OnlyPublicSchematics = true; MaximumTeamMembers = Integer.MAX_VALUE; @@ -353,7 +356,6 @@ public class Config { OnlyPublicSchematics = event.publicSchemsOnly(); MaximumTeamMembers = event.getMaximumTeamMembers(); } - LiveReplay = event.spectateSystem(); }else{ //No event TeamRedColor = config.getString("Red.Prefix", "§c"); @@ -365,8 +367,10 @@ public class Config { EventTeamRedID = 0; EventKampf = null; BothTeamsPublic = true; + Referees = Collections.emptySet(); MaximumTeamMembers = Integer.MAX_VALUE; LiveReplay = false; + SpectatePort = -ReplayID; } String blueLeader = System.getProperty("blueLeader", null); @@ -380,17 +384,13 @@ public class Config { else RedLeader = null; - CheckSchemID = Integer.parseInt(System.getProperty("checkSchemID", "0")); - PrepareSchemID = Integer.parseInt(System.getProperty("prepareSchemID", "0")); - ReplayID = Integer.parseInt(System.getProperty("replay", "0")); - if(CheckSchemID != 0){ mode = ArenaMode.CHECK; }else if(PrepareSchemID != 0){ mode = ArenaMode.PREPARE; - }else if(EventKampfID >= 1){ + }else if(eventKampfID >= 1){ mode = ArenaMode.EVENT; - }else if(EventKampfID == -1){ + }else if(eventKampfID == -1){ mode = ArenaMode.TEST; }else if(ReplayID != 0){ mode = ArenaMode.REPLAY; @@ -403,15 +403,13 @@ public class Config { return ArenaMode.Test.contains(mode); } public static boolean replayserver(){ - return ReplayID == -1; + return ReplayID < 0; } public static boolean blueNegZ(){ return BlueToRedZ > 0; } public static boolean isReferee(Player player) { - if(EventKampf == null) - return false; - return SteamwarUser.get(player.getUniqueId()).getId() == EventKampf.getKampfleiter(); + return Referees.contains(SteamwarUser.get(player.getUniqueId()).getId()); } } 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/FightSchematic.java b/FightSystem_Core/src/de/steamwar/fightsystem/fight/FightSchematic.java index e9ab14c..5e84ccd 100644 --- a/FightSystem_Core/src/de/steamwar/fightsystem/fight/FightSchematic.java +++ b/FightSystem_Core/src/de/steamwar/fightsystem/fight/FightSchematic.java @@ -153,6 +153,8 @@ public class FightSchematic extends StateDependent { ); FightSystem.getHullHider().initialize(team); team.getPlayers().forEach(fightPlayer -> fightPlayer.ifAI(ai -> ai.schematic(clipboard))); + if(ArenaMode.Check.contains(Config.mode) && !team.isBlue()) + replaceSync(Material.TNT, Material.RED_WOOL); Bukkit.getScheduler().runTaskLater(FightSystem.getPlugin(), freezer::disable, 3); } diff --git a/FightSystem_Core/src/de/steamwar/fightsystem/fight/FightTeam.java b/FightSystem_Core/src/de/steamwar/fightsystem/fight/FightTeam.java index cf1de1e..334f3f8 100644 --- a/FightSystem_Core/src/de/steamwar/fightsystem/fight/FightTeam.java +++ b/FightSystem_Core/src/de/steamwar/fightsystem/fight/FightTeam.java @@ -41,7 +41,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.*; @@ -204,13 +203,16 @@ public class FightTeam { schematic.reset(); Set playerSet = new HashSet<>(players.keySet()); - for(UUID uuid : playerSet){ + playerSet.removeIf(uuid -> { Player player = Bukkit.getPlayer(uuid); - if(player == null) + if(player == null) { removePlayer(players.get(uuid).getEntity()); - } + return true; + } + + return false; + }); FightPlayer leaderBackup = leader; - playerSet.removeIf(uuid -> Bukkit.getPlayer(uuid) == null); players.clear(); leader = null; @@ -262,7 +264,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 +292,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 +305,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 +322,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/fight/FightWorld.java b/FightSystem_Core/src/de/steamwar/fightsystem/fight/FightWorld.java index 6527e51..efcc16a 100644 --- a/FightSystem_Core/src/de/steamwar/fightsystem/fight/FightWorld.java +++ b/FightSystem_Core/src/de/steamwar/fightsystem/fight/FightWorld.java @@ -21,6 +21,7 @@ package de.steamwar.fightsystem.fight; import de.steamwar.fightsystem.ArenaMode; import de.steamwar.fightsystem.Config; +import de.steamwar.fightsystem.FightSystem; import de.steamwar.fightsystem.listener.Recording; import de.steamwar.fightsystem.states.FightState; import de.steamwar.fightsystem.states.StateDependent; @@ -55,7 +56,7 @@ public class FightWorld extends StateDependent { @Override public void disable() { - resetWorld(); + Bukkit.getScheduler().runTask(FightSystem.getPlugin(), FightWorld::resetWorld); //Delay to prevent raceconditions with techhider and hullhider } public static void forceLoad(){ diff --git a/FightSystem_Core/src/de/steamwar/fightsystem/listener/ArenaBorder.java b/FightSystem_Core/src/de/steamwar/fightsystem/listener/ArenaBorder.java index 8f9c287..4c0daa4 100644 --- a/FightSystem_Core/src/de/steamwar/fightsystem/listener/ArenaBorder.java +++ b/FightSystem_Core/src/de/steamwar/fightsystem/listener/ArenaBorder.java @@ -39,13 +39,13 @@ import org.bukkit.event.player.PlayerQuitEvent; public class ArenaBorder implements Listener { - private final Border spectatorBorder = new Border(Config.PlayerRegion, true, 5, "NO_ARENA_LEAVING"); - private final Border playerBorder = new Border(Config.PlayerRegion.to2d(), true, 5, "NO_ARENA_LEAVING"); + private final Border spectatorBorder = new Border(Config.PlayerRegion, true, 5, "NO_ARENA_LEAVING", "ArenaBorder.spectatorBorder"); + private final Border playerBorder = new Border(Config.PlayerRegion.to2d(), true, 5, "NO_ARENA_LEAVING", "ArenaBorder.playerBorder"); public ArenaBorder() { new StateDependentListener(ArenaMode.All, FightState.All, this); new StateDependentTask(ArenaMode.All, FightState.Running, this::damage, 2, 2); - new StateDependentListener(!Config.GroundWalkable, FightState.AntiIngame, new Listener() { + new StateDependentListener(!Config.GroundWalkable, FightState.AntiRunning, new Listener() { @EventHandler public void onMove(PlayerMoveEvent e) { Player player = e.getPlayer(); diff --git a/FightSystem_Core/src/de/steamwar/fightsystem/listener/Border.java b/FightSystem_Core/src/de/steamwar/fightsystem/listener/Border.java index 6f8bddd..6ab6605 100644 --- a/FightSystem_Core/src/de/steamwar/fightsystem/listener/Border.java +++ b/FightSystem_Core/src/de/steamwar/fightsystem/listener/Border.java @@ -27,29 +27,30 @@ import de.steamwar.fightsystem.states.StateDependentTask; import de.steamwar.fightsystem.utils.FlatteningWrapper; import de.steamwar.fightsystem.utils.Region; import net.md_5.bungee.api.ChatMessageType; +import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.entity.Player; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; +import java.util.*; +import java.util.logging.Level; public class Border { - private final Map> ghostBarriers = new HashMap<>(); - private final Map lastLocation = new HashMap<>(); + private final Map> ghostBarriers = new HashMap<>(); + private final Map lastLocation = new HashMap<>(); private final boolean contain; private final String resetMessage; + private final String name; private final Region region; private final int ghostRange; private final int ghostSize; - public Border(Region region, boolean contain, int ghostRange, String resetMessage) { + public Border(Region region, boolean contain, int ghostRange, String resetMessage, String name) { this.contain = contain; this.resetMessage = resetMessage; + this.name = name; this.region = region; this.ghostRange = ghostRange; this.ghostSize = 2*ghostRange + 1; @@ -58,20 +59,22 @@ public class Border { } public void addPlayer(Player player) { - if(ghostBarriers.containsKey(player)) + if(ghostBarriers.containsKey(player.getUniqueId())) return; - ghostBarriers.put(player, new HashSet<>()); - lastLocation.put(player, player.getLocation()); + ghostBarriers.put(player.getUniqueId(), new HashSet<>()); + lastLocation.put(player.getUniqueId(), player.getLocation()); + FightSystem.getPlugin().getLogger().log(Level.INFO, () -> player.getName() + " was added to border " + name); } public boolean contains(Player player) { - return ghostBarriers.containsKey(player); + return ghostBarriers.containsKey(player.getUniqueId()); } public void removePlayer(Player player) { - lastLocation.remove(player); - Set blocks = ghostBarriers.remove(player); + FightSystem.getPlugin().getLogger().log(Level.INFO, () -> player.getName() + " was removed from border " + name); + lastLocation.remove(player.getUniqueId()); + Set blocks = ghostBarriers.remove(player.getUniqueId()); if(blocks == null || !player.isOnline()) return; @@ -80,11 +83,18 @@ public class Border { } private void run() { - for(Map.Entry> entry : ghostBarriers.entrySet()) { - Player player = entry.getKey(); + List offline = new ArrayList<>(); + for(Map.Entry> entry : ghostBarriers.entrySet()) { + UUID uuid = entry.getKey(); + Player player = Bukkit.getPlayer(uuid); + if(player == null) { + offline.add(uuid); + continue; + } + Location location = player.getLocation(); if(region.playerInRegion(location) != contain) { - player.teleport(lastLocation.get(player)); + player.teleport(lastLocation.get(uuid)); FightSystem.getMessage().sendPrefixless(resetMessage, player, ChatMessageType.ACTION_BAR); return; } @@ -117,6 +127,10 @@ public class Border { lastLocation.put(entry.getKey(), location); } + + offline.forEach(uuid -> FightSystem.getPlugin().getLogger().log(Level.INFO, () -> uuid + " was removed from border " + name)); + offline.forEach(ghostBarriers::remove); + offline.forEach(lastLocation::remove); } private void borderIteration(Player player, Set ghostBlocks, Region border) { diff --git a/FightSystem_Core/src/de/steamwar/fightsystem/listener/TeamArea.java b/FightSystem_Core/src/de/steamwar/fightsystem/listener/TeamArea.java index cf241bd..2720b25 100644 --- a/FightSystem_Core/src/de/steamwar/fightsystem/listener/TeamArea.java +++ b/FightSystem_Core/src/de/steamwar/fightsystem/listener/TeamArea.java @@ -52,8 +52,8 @@ public class TeamArea implements Listener { public TeamArea(FightTeam team) { this.team = team; - this.spectatorBorder = new Border(team.getExtendRegion(), false, 5, "NO_TEAMAREA"); - this.bordingBorder = new Border(team.getExtendRegion().to2d(), true, 1, "NO_ENTERN"); + this.spectatorBorder = new Border(team.getExtendRegion(), false, 5, "NO_TEAMAREA", team.getName() + ".spectatorBorder"); + this.bordingBorder = new Border(team.getExtendRegion().to2d(), true, 1, "NO_ENTERN", team.getName() + ".boardingBorder"); new StateDependentListener(ArenaMode.AntiTest, FightState.All, this); new StateDependentTask(ArenaMode.AntiTest, FightState.TeamFix, this::realSpectatorCheck, 1, 1); diff --git a/FightSystem_Core/src/de/steamwar/fightsystem/listener/WaterRemover.java b/FightSystem_Core/src/de/steamwar/fightsystem/listener/WaterRemover.java index 05d6d2a..c89fcab 100644 --- a/FightSystem_Core/src/de/steamwar/fightsystem/listener/WaterRemover.java +++ b/FightSystem_Core/src/de/steamwar/fightsystem/listener/WaterRemover.java @@ -73,8 +73,13 @@ public class WaterRemover implements Listener { FightTeam spawn = tnt.remove(event.getEntity().getEntityId()); if(spawn != null && !spawn.getExtendRegion().inRegion(event.getLocation())) { Block b = event.getLocation().getBlock(); - checkBlock(b); - checkNeighbours(b); + for(int y = -1; y <= 1; y++) { + for(int z = -1; z <= 1; z++) { + for(int x = -1; x <= 1; x++) { + checkBlock(b.getRelative(x, y, z)); + } + } + } } event.blockList().forEach(this::checkNeighbours); @@ -90,7 +95,7 @@ public class WaterRemover implements Listener { } private void checkBlock(Block b) { - //do not remove outside teamareasevent.getLocation().getBlock() + //do not remove outside teamareas if(!Config.BlueExtendRegion.inRegion(b) && !Config.RedExtendRegion.inRegion(b)) return; diff --git a/FightSystem_Core/src/de/steamwar/fightsystem/record/LiveRecorder.java b/FightSystem_Core/src/de/steamwar/fightsystem/record/LiveRecorder.java index b7531d3..55fef03 100644 --- a/FightSystem_Core/src/de/steamwar/fightsystem/record/LiveRecorder.java +++ b/FightSystem_Core/src/de/steamwar/fightsystem/record/LiveRecorder.java @@ -41,7 +41,7 @@ public class LiveRecorder extends StateDependent implements Recorder { @Override public void enable() { try { - socket = new Socket(Config.spectateIP, Config.spectatePort); + socket = new Socket(Config.spectateIP, Config.SpectatePort); socket.setSoTimeout(1); // Wait a maximum of 1ms on a blocking operation (flush) socket.setSoLinger(true, 1); // Wait a maximum of 1ms on disable socket.setTcpNoDelay(true); // Don't wait on ack diff --git a/FightSystem_Core/src/de/steamwar/fightsystem/record/LiveServer.java b/FightSystem_Core/src/de/steamwar/fightsystem/record/LiveServer.java index b58f0fa..5bb6308 100644 --- a/FightSystem_Core/src/de/steamwar/fightsystem/record/LiveServer.java +++ b/FightSystem_Core/src/de/steamwar/fightsystem/record/LiveServer.java @@ -33,7 +33,7 @@ public class LiveServer { private final ServerSocket socket; public LiveServer() throws IOException { - socket = new ServerSocket(Config.spectatePort); + socket = new ServerSocket(Config.SpectatePort); Bukkit.getScheduler().runTaskAsynchronously(FightSystem.getPlugin(), this::acceptConnections); } diff --git a/FightSystem_Core/src/de/steamwar/fightsystem/record/PacketProcessor.java b/FightSystem_Core/src/de/steamwar/fightsystem/record/PacketProcessor.java index cb1dddb..6296e37 100644 --- a/FightSystem_Core/src/de/steamwar/fightsystem/record/PacketProcessor.java +++ b/FightSystem_Core/src/de/steamwar/fightsystem/record/PacketProcessor.java @@ -89,7 +89,7 @@ public class PacketProcessor implements Listener { private final BukkitTask task; private final LinkedList syncList = new LinkedList<>(); private final Set hiddenBlockIds = Config.HiddenBlocks.stream().flatMap(m -> BlockIds.impl.materialToAllIds(m).stream()).collect(Collectors.toSet()); - private final int obfuscateWith = BlockIds.impl.materialToId(Material.getMaterial(Config.ObfuscateWith.toUpperCase())); + private final int obfuscateWith = BlockIds.impl.materialToId(Config.ObfuscateWith); private final FreezeWorld freezer = new FreezeWorld(); private final REntityServer entityServer = new REntityServer(); private final Map entities = new HashMap<>(); diff --git a/FightSystem_Core/src/de/steamwar/fightsystem/utils/BlockIdWrapper.java b/FightSystem_Core/src/de/steamwar/fightsystem/utils/BlockIdWrapper.java index 399e63d..a6b825d 100644 --- a/FightSystem_Core/src/de/steamwar/fightsystem/utils/BlockIdWrapper.java +++ b/FightSystem_Core/src/de/steamwar/fightsystem/utils/BlockIdWrapper.java @@ -19,16 +19,28 @@ package de.steamwar.fightsystem.utils; +import com.comphenix.tinyprotocol.Reflection; import de.steamwar.core.VersionDependent; import de.steamwar.fightsystem.FightSystem; import org.bukkit.Material; import org.bukkit.World; import org.bukkit.block.Block; +import org.bukkit.entity.Player; public interface BlockIdWrapper { + Class worldServer = Reflection.getClass("{nms.server.level}.WorldServer"); + Reflection.MethodInvoker getWorldHandle = Reflection.getTypedMethod(Reflection.getClass("{obc}.CraftWorld"), "getHandle", worldServer); + + Class craftPlayer = Reflection.getClass("{obc}.entity.CraftPlayer"); + Class entityPlayer = Reflection.getClass("{nms.server.level}.EntityPlayer"); + Reflection.MethodInvoker getPlayer = Reflection.getTypedMethod(craftPlayer, "getHandle", entityPlayer); + BlockIdWrapper impl = VersionDependent.getVersionImpl(FightSystem.getPlugin()); Material idToMaterial(int blockState); 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); } diff --git a/FightSystem_Core/src/de/steamwar/fightsystem/utils/Hull.java b/FightSystem_Core/src/de/steamwar/fightsystem/utils/Hull.java index 118e564..05ff3fc 100644 --- a/FightSystem_Core/src/de/steamwar/fightsystem/utils/Hull.java +++ b/FightSystem_Core/src/de/steamwar/fightsystem/utils/Hull.java @@ -19,10 +19,13 @@ 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.AllArgsConstructor; +import lombok.Getter; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Block; @@ -45,8 +48,9 @@ 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 HashSet players = new HashSet<>(); private final Set entities = new HashSet<>(); private final Set rentities = new HashSet<>(); @@ -90,23 +94,30 @@ public class Hull { } } + public boolean blockPrecise(Player player, int chunkX, int chunkY, int chunkZ) { + return players.contains(player) && !region.chunkSectionOutside(chunkX, chunkY, chunkZ); + } + + public boolean isBlockHidden(Player player, int x, int y, int z) { + 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) { return players.contains(player) && region.inRegion(location) && !visibility.get(new IntVector(location).toId(region)); } - @SuppressWarnings("deprecation") public void addPlayer(Player player) { if(players.add(player)) { for(Entity entity : entities) - player.hideEntity(FightSystem.getPlugin(), entity); + BlockIdWrapper.impl.untrackEntity(player, entity.getEntityId()); } } - @SuppressWarnings("deprecation") public void removePlayer(Player player, boolean activeRemoval) { if(players.remove(player) && activeRemoval) { for(Entity entity : entities) - player.showEntity(FightSystem.getPlugin(), entity); + BlockIdWrapper.impl.trackEntity(player, entity.getEntityId()); + // techhider triggers block change sending } } @@ -115,12 +126,12 @@ public class Hull { if(region.inRegion(location) && !visibility.get(new IntVector(location).toId(region))) { //TODO more precise if(entities.add(entity)) { for(Player player : players) - player.hideEntity(FightSystem.getPlugin(), entity); + BlockIdWrapper.impl.untrackEntity(player, entity.getEntityId()); } } else { if(entities.remove(entity)) { for(Player player : players) - player.showEntity(FightSystem.getPlugin(), entity); + BlockIdWrapper.impl.trackEntity(player, entity.getEntityId()); } } } @@ -147,6 +158,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(); @@ -166,7 +178,6 @@ public class Hull { FightSystem.getPlugin().getLogger().log(Level.INFO, () -> "[HullHider] initialisation finished: " + (System.currentTimeMillis() - start) + " ms, visible blocks: " + visibility.cardinality()); } - @SuppressWarnings("deprecation") public void updateBlockVisibility(Block b, Material changedType) { IntVector root = new IntVector(b.getX(), b.getY(), b.getZ()); if (root.notInRegion(region)) @@ -197,7 +208,7 @@ public class Hull { if(uncoveredSet.contains(new IntVector(entity.getLocation()))) { //TODO more precise it.remove(); for(Player player : players) - player.showEntity(FightSystem.getPlugin(), entity); + BlockIdWrapper.impl.trackEntity(player, entity.getEntityId()); } } @@ -209,6 +220,26 @@ 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()); + if(packet == null) + continue; + + players.forEach(player -> TinyProtocol.instance.sendPacket(player, packet)); + } } private void forEachBorder(BiConsumer f) { @@ -280,17 +311,13 @@ public class Hull { } - private static class IntVector { + @Getter + @AllArgsConstructor + public static class IntVector { private final int x; private final int y; private final int z; - public IntVector(int x, int y, int z) { - this.x = x; - this.y = y; - this.z = z; - } - public IntVector(Location location) { this.x = location.getBlockX(); this.y = location.getBlockY(); diff --git a/FightSystem_Core/src/de/steamwar/fightsystem/utils/HullHider.java b/FightSystem_Core/src/de/steamwar/fightsystem/utils/HullHider.java index 06363e3..c613a4a 100644 --- a/FightSystem_Core/src/de/steamwar/fightsystem/utils/HullHider.java +++ b/FightSystem_Core/src/de/steamwar/fightsystem/utils/HullHider.java @@ -56,13 +56,18 @@ import java.util.function.Function; public class HullHider implements Listener { - private 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) - Fight.teams().forEach(team -> hulls.put(team, new Hull(team))); + if(!TechHiderWrapper.ENABLED) { + hulls = new Hull[0]; + return; + } + + 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); @@ -71,8 +76,8 @@ public class HullHider implements Listener { if(Core.getVersion() >= 9 && Core.getVersion() < 18) posHiderGenerator("{nms.network.protocol.game}.PacketPlayOutCustomSoundEffect", int.class, 8.0); - new StateDependentListener(ENABLED, FightState.Schem, this); - new StateDependent(ENABLED, FightState.Schem) { + new StateDependentListener(TechHiderWrapper.ENABLED, FightState.Schem, this); + new StateDependent(TechHiderWrapper.ENABLED, FightState.Schem) { @Override public void enable() { packetHiders.forEach(TinyProtocol.instance::addFilter); @@ -85,14 +90,14 @@ public class HullHider implements Listener { packetHiders.forEach(TinyProtocol.instance::removeFilter); } }.register(); - new StateDependentTask(ENABLED, FightState.Schem, this::onTick, 0, 1); + new StateDependentTask(TechHiderWrapper.ENABLED, FightState.Schem, this::onTick, 0, 1); } public void initialize(FightTeam team) { - if(!ENABLED) + if(!TechHiderWrapper.ENABLED) return; - hulls.get(team).initialize(); + hullMap.get(team).initialize(); } @@ -107,13 +112,13 @@ public class HullHider implements Listener { } public void updatePlayer(Player player) { - if(!ENABLED) + if(!TechHiderWrapper.ENABLED) return; FightTeam team = Fight.getPlayerTeam(player); FightPlayer fp = Fight.getFightPlayer(player); - for(Map.Entry hull : hulls.entrySet()) { - if(hull.getKey() == team || (fp != null && fp.canEntern())) { + for(Map.Entry hull : hullMap.entrySet()) { + if(Config.isReferee(player) || hull.getKey() == team || (fp != null && fp.canEntern())) { hull.getValue().removePlayer(player, true); } else { hull.getValue().addPlayer(player); @@ -122,7 +127,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); } @@ -133,34 +139,62 @@ 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); } + public boolean isBlockHidden(Player player, int x, int y, int z) { + if(!TechHiderWrapper.ENABLED) + return false; + + for (Hull hull : hulls) + if(hull.isBlockHidden(player, x, y, z)) + return true; + + return false; + } + + public boolean blockPrecise(Player player, int chunkX, int chunkY, int chunkZ) { + if(!TechHiderWrapper.ENABLED) + return false; + + for (Hull hull : hulls) + if(hull.blockPrecise(player, chunkX, chunkY, chunkZ)) + return true; + + return false; + } @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) + 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); } @@ -197,7 +231,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/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 807822a..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); @@ -80,6 +82,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++) @@ -117,7 +125,7 @@ public class Region { } public boolean playerInRegion(Location location){ - return in2dRegion(location) && minY <= location.getY() && location.getY() + 1.8 < maxY; + return in2dRegion(location) && minY <= location.getY() && location.getY() < maxY; } public boolean in2dRegion(Block block){ diff --git a/FightSystem_Core/src/de/steamwar/fightsystem/utils/TechHiderWrapper.java b/FightSystem_Core/src/de/steamwar/fightsystem/utils/TechHiderWrapper.java index 9638292..9e97afa 100644 --- a/FightSystem_Core/src/de/steamwar/fightsystem/utils/TechHiderWrapper.java +++ b/FightSystem_Core/src/de/steamwar/fightsystem/utils/TechHiderWrapper.java @@ -22,29 +22,34 @@ 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 { +public class TechHiderWrapper extends StateDependent implements TechHider.LocationEvaluator, Listener { public static final boolean ENABLED = !Config.OnlyPublicSchematics && !Config.test() && Config.TechhiderActive; + + private final ConcurrentHashMap hiddenRegion = new ConcurrentHashMap<>(); 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, Config.ObfuscateWith, Config.HiddenBlocks, Config.HiddenBlockEntities); + + new StateDependentListener(ENABLED, FightState.Schem, this); register(); } @@ -56,43 +61,82 @@ public class TechHiderWrapper extends StateDependent { @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(bypass((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(bypass(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); + }); } - 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) { + return getHiddenRegion(player).chunkSectionOutside(chunkX, chunkY, chunkZ); + } + + @Override + public TechHider.State check(Player player, int x, int y, int z) { + if(hiddenRegion.computeIfAbsent(player, this::getHiddenRegion).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; } } + + @Override + public boolean blockPrecise(Player player, int chunkX, int chunkY, int chunkZ) { + return FightSystem.getHullHider().blockPrecise(player, chunkX, chunkY, chunkZ); + } + + private Region getHiddenRegion(Player player) { + if(Config.isReferee(player)) + return Region.EMPTY; + + FightTeam team = Fight.getPlayerTeam(player); + if(team == null) + return Config.ArenaRegion; + + if(team.canPlayerEntern(player)) + return Region.EMPTY; + + return Fight.getOpposite(team).getExtendRegion(); + } }