SteamWar/BauSystem2.0
Archiviert
12
0

Finalize NoClip Command

Signed-off-by: Chaoscaot <chaoscaot444@gmail.com>
Dieser Commit ist enthalten in:
Chaoscaot 2021-06-03 23:54:21 +02:00
Ursprung 4a9f3c3b53
Commit df84e5b252
2 geänderte Dateien mit 125 neuen und 19 gelöschten Zeilen

Datei anzeigen

@ -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 <https://www.gnu.org/licenses/>.
*/
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);
}
}
}

Datei anzeigen

@ -21,49 +21,104 @@ package de.steamwar.bausystem.features.other;
import com.comphenix.protocol.PacketType; import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.ProtocolLibrary; import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketContainer; import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.events.PacketEvent;
import com.comphenix.protocol.wrappers.EnumWrappers; import com.comphenix.protocol.wrappers.EnumWrappers;
import com.comphenix.protocol.wrappers.PlayerInfoData; import com.comphenix.protocol.wrappers.PlayerInfoData;
import com.comphenix.protocol.wrappers.WrappedChatComponent; import com.comphenix.protocol.wrappers.WrappedChatComponent;
import com.comphenix.protocol.wrappers.WrappedGameProfile; 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.LinkageType;
import de.steamwar.bausystem.linkage.Linked; import de.steamwar.bausystem.linkage.Linked;
import de.steamwar.command.SWCommand; import de.steamwar.command.SWCommand;
import de.steamwar.core.VersionedRunnable;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.GameMode; import org.bukkit.GameMode;
import org.bukkit.craftbukkit.v1_15_R1.entity.CraftPlayer; import org.bukkit.craftbukkit.v1_15_R1.entity.CraftPlayer;
import org.bukkit.entity.Player; 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.lang.reflect.InvocationTargetException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.logging.Level; import java.util.logging.Level;
@Linked(LinkageType.COMMAND) @Linked(LinkageType.COMMAND)
public class NoClipCommand extends SWCommand { @Linked(LinkageType.LISTENER)
public class NoClipCommand extends SWCommand implements Listener {
private static final List<Player> NOCLIPS = new ArrayList<>();
private static final Map<Player, Long> LAST_TICKS = new HashMap<>();
protected NoClipCommand() { protected NoClipCommand() {
super("noclip", "nc"); 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) @Register(help = true)
public void genericCommand(Player player, String... args) { public void genericCommand(Player player, String... args) {
player.setGameMode(GameMode.SPECTATOR); if (NOCLIPS.contains(player)) {
((CraftPlayer) player).getHandle().abilities.mayBuild = true; NOCLIPS.remove(player);
((CraftPlayer) player).getHandle().abilities.canInstantlyBuild = true; player.setGameMode(GameMode.CREATIVE);
//player.sendMessage(((CraftPlayer) player).getHandle().noclip + ""); } else {
PacketContainer gm3packet = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.PLAYER_INFO); player.setGameMode(GameMode.SPECTATOR);
gm3packet.getPlayerInfoAction().write(0, EnumWrappers.PlayerInfoAction.UPDATE_GAME_MODE); ((CraftPlayer) player).getHandle().abilities.mayBuild = true;
List<PlayerInfoData> playerInfoActions = new ArrayList<>(); ((CraftPlayer) player).getHandle().abilities.canInstantlyBuild = true;
playerInfoActions.add(new PlayerInfoData(WrappedGameProfile.fromPlayer(player), 1, EnumWrappers.NativeGameMode.SPECTATOR, WrappedChatComponent.fromText(player.getDisplayName()))); PacketContainer gm3packet = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.PLAYER_INFO);
gm3packet.getPlayerInfoDataLists().write(0, playerInfoActions); gm3packet.getPlayerInfoAction().write(0, EnumWrappers.PlayerInfoAction.UPDATE_GAME_MODE);
PacketContainer gm1packet = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.GAME_STATE_CHANGE); List<PlayerInfoData> playerInfoActions = new ArrayList<>();
gm1packet.getIntegers().write(0, 3); playerInfoActions.add(new PlayerInfoData(WrappedGameProfile.fromPlayer(player), 1, EnumWrappers.NativeGameMode.SPECTATOR, WrappedChatComponent.fromText(player.getDisplayName())));
gm1packet.getFloat().write(0, 1F); gm3packet.getPlayerInfoDataLists().write(0, playerInfoActions);
try { PacketContainer gm1packet = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.GAME_STATE_CHANGE);
ProtocolLibrary.getProtocolManager().sendServerPacket(player, gm1packet); gm1packet.getIntegers().write(0, 3);
ProtocolLibrary.getProtocolManager().sendServerPacket(player, gm3packet); gm1packet.getFloat().write(0, 1F);
} catch (InvocationTargetException e) { NOCLIPS.add(player);
Bukkit.getLogger().log(Level.SEVERE, "Invocation target exception", e); 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);
}
}
}