From 78d8b25ade31f037c15293c172058ef034635695 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Wed, 28 Oct 2020 11:07:17 +0100 Subject: [PATCH] New spectator mode --- pom.xml | 7 ++++ .../spectatesystem/listener/JoinListener.java | 33 +++++++++++++++++++ src/plugin.yml | 3 +- 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 70e94a6..319d3de 100644 --- a/pom.xml +++ b/pom.xml @@ -59,5 +59,12 @@ system ${main.basedir}/lib/WorldEdit-1.15.jar + + steamwar + ProtocolLib + 1.0 + system + ${main.basedir}/lib/ProtocolLib.jar + \ No newline at end of file diff --git a/src/de/steamwar/spectatesystem/listener/JoinListener.java b/src/de/steamwar/spectatesystem/listener/JoinListener.java index 02b25d2..7506fb9 100644 --- a/src/de/steamwar/spectatesystem/listener/JoinListener.java +++ b/src/de/steamwar/spectatesystem/listener/JoinListener.java @@ -19,23 +19,56 @@ package de.steamwar.spectatesystem.listener; +import com.comphenix.protocol.PacketType; +import com.comphenix.protocol.ProtocolLibrary; +import com.comphenix.protocol.events.PacketContainer; +import com.comphenix.protocol.wrappers.EnumWrappers; +import com.comphenix.protocol.wrappers.PlayerInfoData; +import com.comphenix.protocol.wrappers.WrappedChatComponent; +import com.comphenix.protocol.wrappers.WrappedGameProfile; import de.steamwar.scoreboard.SWScoreboard; import de.steamwar.spectatesystem.Config; import de.steamwar.spectatesystem.SpectateSystem; import de.steamwar.spectatesystem.elements.REntity; +import org.bukkit.Bukkit; import org.bukkit.GameMode; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerQuitEvent; +import java.lang.reflect.InvocationTargetException; +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Level; + public class JoinListener extends BasicListener { @EventHandler public void onJoin(PlayerJoinEvent e){ Player player = e.getPlayer(); REntity.playerJoins(player); + player.setGameMode(GameMode.SPECTATOR); + for(Player currentPlayer : Bukkit.getServer().getOnlinePlayers()) { + if(currentPlayer.getUniqueId() != player.getUniqueId() && currentPlayer.getGameMode() == GameMode.SPECTATOR) { + currentPlayer.hidePlayer(SpectateSystem.get(), player); + player.hidePlayer(SpectateSystem.get(), currentPlayer); + } + } + Bukkit.getScheduler().runTaskLater(SpectateSystem.get(), () -> { + 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 ex) { + Bukkit.getLogger().log(Level.SEVERE, "Invocation target exception", ex); + } + }, 2); + player.getInventory().clear(); player.teleport(Config.SpecSpawn); SWScoreboard.createScoreboard(player, SpectateSystem.getScoreboard()); diff --git a/src/plugin.yml b/src/plugin.yml index 9a4ff66..8482c49 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -7,4 +7,5 @@ main: de.steamwar.spectatesystem.SpectateSystem load: POSTWORLD depends: - SpigotCore - - WorldEdit \ No newline at end of file + - WorldEdit + - ProtocolLib \ No newline at end of file