From 79658bf3aa762d5042e77ab0e4ebc6a51f539a74 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sat, 11 Sep 2021 18:31:58 +0200 Subject: [PATCH] Real spectator mode in own teamarea Signed-off-by: Lixfel --- .../de/steamwar/fightsystem/fight/Fight.java | 24 ++++--- .../fightsystem/listener/TeamArea.java | 70 ++++++++++++------- 2 files changed, 57 insertions(+), 37 deletions(-) diff --git a/FightSystem_Core/src/de/steamwar/fightsystem/fight/Fight.java b/FightSystem_Core/src/de/steamwar/fightsystem/fight/Fight.java index 4033521..591e631 100644 --- a/FightSystem_Core/src/de/steamwar/fightsystem/fight/Fight.java +++ b/FightSystem_Core/src/de/steamwar/fightsystem/fight/Fight.java @@ -123,16 +123,7 @@ public class Fight { Bukkit.getScheduler().runTaskLater(FightSystem.getPlugin(), () -> { if(!player.isOnline()) return; - PacketContainer gm1packet = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.PLAYER_INFO); - gm1packet.getPlayerInfoAction().write(0, EnumWrappers.PlayerInfoAction.UPDATE_GAME_MODE); - List playerInfoActions = new ArrayList<>(); - playerInfoActions.add(new PlayerInfoData(WrappedGameProfile.fromPlayer(player), 1, EnumWrappers.NativeGameMode.CREATIVE, WrappedChatComponent.fromText(player.getDisplayName()))); - gm1packet.getPlayerInfoDataLists().write(0, playerInfoActions); - try { - ProtocolLibrary.getProtocolManager().sendServerPacket(player, gm1packet); - } catch (InvocationTargetException e) { - Bukkit.getLogger().log(Level.SEVERE, "Invocation target exception", e); - } + pseudoSpectator(player, true); }, 2); }else if(gameMode == GameMode.SURVIVAL) { for(Player currentPlayer : Bukkit.getServer().getOnlinePlayers()) { @@ -144,6 +135,19 @@ public class Fight { } } + public static void pseudoSpectator(Player player, boolean enable) { + PacketContainer gm1packet = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.PLAYER_INFO); + gm1packet.getPlayerInfoAction().write(0, EnumWrappers.PlayerInfoAction.UPDATE_GAME_MODE); + List playerInfoActions = new ArrayList<>(); + playerInfoActions.add(new PlayerInfoData(WrappedGameProfile.fromPlayer(player), 1, enable ? EnumWrappers.NativeGameMode.CREATIVE : EnumWrappers.NativeGameMode.SPECTATOR, WrappedChatComponent.fromText(player.getDisplayName()))); + gm1packet.getPlayerInfoDataLists().write(0, playerInfoActions); + try { + ProtocolLibrary.getProtocolManager().sendServerPacket(player, gm1packet); + } catch (InvocationTargetException e) { + Bukkit.getLogger().log(Level.SEVERE, "Invocation target exception", e); + } + } + public static int getMaxRank(){ /* MaxRank of 0 is Pubonly*/ if(Config.OnlyPublicSchematics){ diff --git a/FightSystem_Core/src/de/steamwar/fightsystem/listener/TeamArea.java b/FightSystem_Core/src/de/steamwar/fightsystem/listener/TeamArea.java index db88f9f..6a06a8e 100644 --- a/FightSystem_Core/src/de/steamwar/fightsystem/listener/TeamArea.java +++ b/FightSystem_Core/src/de/steamwar/fightsystem/listener/TeamArea.java @@ -26,15 +26,18 @@ import de.steamwar.fightsystem.fight.Fight; import de.steamwar.fightsystem.fight.FightTeam; import de.steamwar.fightsystem.states.FightState; import de.steamwar.fightsystem.states.StateDependentListener; +import de.steamwar.fightsystem.utils.Region; import net.md_5.bungee.api.chat.TextComponent; import org.bukkit.Bukkit; import org.bukkit.GameMode; -import org.bukkit.Location; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerMoveEvent; +import org.bukkit.event.player.PlayerQuitEvent; +import java.util.HashSet; +import java.util.Set; import java.util.logging.Level; public class TeamArea implements Listener { @@ -42,6 +45,8 @@ public class TeamArea implements Listener { private static final String DENY_TEAM = "§cDu darfst nicht zu den Teams"; private static final String DENY_ENTERN = "§cDu darfst nicht entern"; + private static final Set realSpectator = new HashSet<>(); + public TeamArea() { new StateDependentListener(ArenaMode.AntiTest, FightState.All, this); } @@ -52,37 +57,48 @@ public class TeamArea implements Listener { if(player == FightSystem.getEventLeiter()) return; - Location to = event.getTo(); - assert to != null; - FightTeam team = Fight.getPlayerTeam(player); + checkInTeamRegion(event, Fight.getBlueTeam(), player, team); + checkInTeamRegion(event, Fight.getRedTeam(), player, team); + } - if(Config.BlueExtendRegion.playerInRegion(to)){ - if(team == null) - reset(event, DENY_TEAM); - else if(team == Fight.getRedTeam() && (player.getGameMode() == GameMode.SPECTATOR || !team.canPlayerEntern(player))) - reset(event, DENY_ENTERN); - else - return; // Is allowed in area - checkInInnerArea(event.getPlayer(), to, Fight.getBlueTeam()); - }else if(Config.RedExtendRegion.playerInRegion(to)){ - if(team == null) - reset(event, DENY_TEAM); - else if(team == Fight.getBlueTeam() && (player.getGameMode() == GameMode.SPECTATOR || !team.canPlayerEntern(player))) - reset(event, DENY_ENTERN); - else - return; // Is allowed in area - checkInInnerArea(event.getPlayer(), to, Fight.getRedTeam()); - }else{ - if(team != null && player.getGameMode() != GameMode.SPECTATOR && !team.canPlayerEntern(player) && !team.getExtendRegion().in2dRegion(to)) - reset(event, DENY_ENTERN); + @EventHandler + public void playerQuit(PlayerQuitEvent e) { + realSpectator.remove(e.getPlayer()); + } + + private void checkInTeamRegion(PlayerMoveEvent event, FightTeam team, Player player, FightTeam playerTeam) { + boolean spectator = player.getGameMode() == GameMode.SPECTATOR; + if(!spectator && playerTeam != null && playerTeam.canPlayerEntern(player)) + return; // Player can entern + + Region region = team.getExtendRegion(); + boolean inRegion = region.playerInRegion(event.getTo()); + if(team == playerTeam) { + if(spectator) { + realSpectator(inRegion, player); + } else if (!playerTeam.canPlayerEntern(player) && !inRegion) { + reset(event, DENY_ENTERN); // Leaving prior to entern + } + return; // Always allowed in own region + } + + if(inRegion) { + reset(event, DENY_TEAM); // Not allowed in region + if(team.getSchemRegion().playerInRegion(event.getTo()) && Config.PreperationArea >= 5){ // Preventing false positives due to small extension + player.kickPlayer(null); + Bukkit.getLogger().log(Level.INFO, player.getName() + " ist in einen Teambereich eingedrungen."); + } } } - private void checkInInnerArea(Player player, Location to, FightTeam team){ - if(team.getSchemRegion().playerInRegion(to) && Config.PreperationArea >= 5){ // Preventing false positives due to small extension - player.kickPlayer(null); - Bukkit.getLogger().log(Level.INFO, player.getName() + " ist in einen Teambereich eingedrungen."); + private void realSpectator(boolean inRegion, Player player) { + if(inRegion && !realSpectator.contains(player)) { + Fight.pseudoSpectator(player, false); + realSpectator.add(player); + }else if(!inRegion && realSpectator.contains(player)) { + Fight.pseudoSpectator(player, true); + realSpectator.remove(player); } }