From df84e5b252182278ee860d54789a1496c9ad971c Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Thu, 3 Jun 2021 23:54:21 +0200 Subject: [PATCH] Finalize NoClip Command Signed-off-by: Chaoscaot --- .../features/other/NoClipCommand_15.java | 51 ++++++++++ .../features/other/NoClipCommand.java | 93 +++++++++++++++---- 2 files changed, 125 insertions(+), 19 deletions(-) create mode 100644 BauSystem_15/src/de/steamwar/bausystem/features/other/NoClipCommand_15.java diff --git a/BauSystem_15/src/de/steamwar/bausystem/features/other/NoClipCommand_15.java b/BauSystem_15/src/de/steamwar/bausystem/features/other/NoClipCommand_15.java new file mode 100644 index 00000000..f5c44fe1 --- /dev/null +++ b/BauSystem_15/src/de/steamwar/bausystem/features/other/NoClipCommand_15.java @@ -0,0 +1,51 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2021 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.bausystem.features.other; + +import net.minecraft.server.v1_15_R1.EnumGamemode; +import net.minecraft.server.v1_15_R1.PlayerInteractManager; +import org.bukkit.GameMode; +import org.bukkit.craftbukkit.v1_15_R1.entity.CraftPlayer; +import org.bukkit.entity.Player; + +import java.lang.reflect.Field; + +public class NoClipCommand_15 { + + private static Field gameModeField; + + public static void setGameModeInternal(Player player, GameMode gameMode) { + CraftPlayer craftPlayer = (CraftPlayer) player; + EnumGamemode enumGamemode = EnumGamemode.getById(gameMode.getValue()); + try { + checkCache(); + gameModeField.set(craftPlayer.getHandle().playerInteractManager, enumGamemode); + } catch (NoSuchFieldException | IllegalAccessException e) { + throw new SecurityException("Could not find Field?"); + } + } + + private static void checkCache() throws NoSuchFieldException { + if (gameModeField == null) { + gameModeField = PlayerInteractManager.class.getDeclaredField("gamemode"); + gameModeField.setAccessible(true); + } + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/other/NoClipCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/other/NoClipCommand.java index d98e1cee..b544a97f 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/other/NoClipCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/other/NoClipCommand.java @@ -21,49 +21,104 @@ package de.steamwar.bausystem.features.other; import com.comphenix.protocol.PacketType; import com.comphenix.protocol.ProtocolLibrary; +import com.comphenix.protocol.events.PacketAdapter; import com.comphenix.protocol.events.PacketContainer; +import com.comphenix.protocol.events.PacketEvent; 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.bausystem.BauSystem; +import de.steamwar.bausystem.features.tpslimit.TPSUtils; import de.steamwar.bausystem.linkage.LinkageType; import de.steamwar.bausystem.linkage.Linked; import de.steamwar.command.SWCommand; +import de.steamwar.core.VersionedRunnable; import org.bukkit.Bukkit; import org.bukkit.GameMode; import org.bukkit.craftbukkit.v1_15_R1.entity.CraftPlayer; import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.block.BlockCanBuildEvent; +import org.bukkit.event.player.PlayerGameModeChangeEvent; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.logging.Level; @Linked(LinkageType.COMMAND) -public class NoClipCommand extends SWCommand { +@Linked(LinkageType.LISTENER) +public class NoClipCommand extends SWCommand implements Listener { + + private static final List NOCLIPS = new ArrayList<>(); + private static final Map LAST_TICKS = new HashMap<>(); + protected NoClipCommand() { super("noclip", "nc"); + ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(BauSystem.getInstance(), PacketType.Play.Client.POSITION, PacketType.Play.Client.POSITION_LOOK) { + @Override + public void onPacketReceiving(PacketEvent event) { + if (NOCLIPS.contains(event.getPlayer())) { + if (LAST_TICKS.getOrDefault(event.getPlayer(), -1L).equals(TPSUtils.currentTick.get())) return; + VersionedRunnable.call(new VersionedRunnable(() -> NoClipCommand_15.setGameModeInternal(event.getPlayer(), GameMode.SPECTATOR), 15)); + LAST_TICKS.put(event.getPlayer(), TPSUtils.currentTick.get()); + } + } + }); + ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(BauSystem.getInstance(), PacketType.Play.Client.USE_ITEM, PacketType.Play.Client.BLOCK_DIG) { + @Override + public void onPacketReceiving(PacketEvent event) { + if (NOCLIPS.contains(event.getPlayer())) { + VersionedRunnable.call(new VersionedRunnable(() -> NoClipCommand_15.setGameModeInternal(event.getPlayer(), GameMode.CREATIVE), 15)); + LAST_TICKS.put(event.getPlayer(), TPSUtils.currentTick.get()); + } + } + }); } @Register(help = true) public void genericCommand(Player player, String... args) { - player.setGameMode(GameMode.SPECTATOR); - ((CraftPlayer) player).getHandle().abilities.mayBuild = true; - ((CraftPlayer) player).getHandle().abilities.canInstantlyBuild = true; - //player.sendMessage(((CraftPlayer) player).getHandle().noclip + ""); - PacketContainer gm3packet = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.PLAYER_INFO); - gm3packet.getPlayerInfoAction().write(0, EnumWrappers.PlayerInfoAction.UPDATE_GAME_MODE); - List playerInfoActions = new ArrayList<>(); - playerInfoActions.add(new PlayerInfoData(WrappedGameProfile.fromPlayer(player), 1, EnumWrappers.NativeGameMode.SPECTATOR, WrappedChatComponent.fromText(player.getDisplayName()))); - gm3packet.getPlayerInfoDataLists().write(0, playerInfoActions); - PacketContainer gm1packet = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.GAME_STATE_CHANGE); - gm1packet.getIntegers().write(0, 3); - gm1packet.getFloat().write(0, 1F); - try { - ProtocolLibrary.getProtocolManager().sendServerPacket(player, gm1packet); - ProtocolLibrary.getProtocolManager().sendServerPacket(player, gm3packet); - } catch (InvocationTargetException e) { - Bukkit.getLogger().log(Level.SEVERE, "Invocation target exception", e); + if (NOCLIPS.contains(player)) { + NOCLIPS.remove(player); + player.setGameMode(GameMode.CREATIVE); + } else { + player.setGameMode(GameMode.SPECTATOR); + ((CraftPlayer) player).getHandle().abilities.mayBuild = true; + ((CraftPlayer) player).getHandle().abilities.canInstantlyBuild = true; + PacketContainer gm3packet = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.PLAYER_INFO); + gm3packet.getPlayerInfoAction().write(0, EnumWrappers.PlayerInfoAction.UPDATE_GAME_MODE); + List playerInfoActions = new ArrayList<>(); + playerInfoActions.add(new PlayerInfoData(WrappedGameProfile.fromPlayer(player), 1, EnumWrappers.NativeGameMode.SPECTATOR, WrappedChatComponent.fromText(player.getDisplayName()))); + gm3packet.getPlayerInfoDataLists().write(0, playerInfoActions); + PacketContainer gm1packet = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.GAME_STATE_CHANGE); + gm1packet.getIntegers().write(0, 3); + gm1packet.getFloat().write(0, 1F); + NOCLIPS.add(player); + try { + ProtocolLibrary.getProtocolManager().sendServerPacket(player, gm1packet); + ProtocolLibrary.getProtocolManager().sendServerPacket(player, gm3packet); + } catch (InvocationTargetException e) { + Bukkit.getLogger().log(Level.SEVERE, "Invocation target exception", e); + player.performCommand("noclip"); + } } } -} + + @EventHandler(ignoreCancelled = true) + public void onPlayerGameModeChange(PlayerGameModeChangeEvent event) { + if (NOCLIPS.contains(event.getPlayer())) { + event.setCancelled(true); + } + } + + @EventHandler(ignoreCancelled = true) + public void onBlock(BlockCanBuildEvent event) { + if (NOCLIPS.contains(event.getPlayer())) { + event.setBuildable(true); + } + } +} \ No newline at end of file