SteamWar/BauSystem2.0
Archiviert
12
0

First TinyProtocol

Signed-off-by: yoyosource <yoyosource@nidido.de>
Dieser Commit ist enthalten in:
yoyosource 2021-10-12 20:30:36 +02:00
Ursprung 71fb547604
Commit 8ebad6c18e
2 geänderte Dateien mit 190 neuen und 38 gelöschten Zeilen

Datei anzeigen

@ -19,23 +19,20 @@
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 com.comphenix.tinyprotocol.Reflection;
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.bausystem.utils.ProtocolAPI;
import de.steamwar.command.SWCommand;
import de.steamwar.core.VersionedRunnable;
import lombok.Getter;
import org.bukkit.Bukkit;
import net.minecraft.server.v1_15_R1.PacketPlayOutPlayerInfo;
import org.bukkit.GameMode;
import org.bukkit.craftbukkit.v1_15_R1.entity.CraftPlayer;
import org.bukkit.entity.Player;
@ -46,42 +43,59 @@ import org.bukkit.event.player.PlayerGameModeChangeEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.event.player.PlayerToggleFlightEvent;
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;
import java.util.function.BiFunction;
@Linked(LinkageType.COMMAND)
@Linked(LinkageType.LISTENER)
public class NoClipCommand extends SWCommand implements Listener {
public static final Class<?> playerInfo = Reflection.getClass("{nms}.PacketPlayOutPlayerInfo");
private static final Reflection.FieldAccessor<PacketPlayOutPlayerInfo.EnumPlayerInfoAction> playerInfoAction = Reflection.getField(playerInfo, PacketPlayOutPlayerInfo.EnumPlayerInfoAction.class, 0);
private static final Reflection.FieldAccessor playerInfoDataList = Reflection.getField(playerInfo, "b", List.class);
public static final Class<?> gameStateChange = Reflection.getClass("{nms}.PacketPlayOutGameStateChange");
private static final Reflection.FieldAccessor<Integer> integerFieldAccessor = Reflection.getField(gameStateChange, int.class, 0);
private static final Reflection.FieldAccessor<Float> floatFieldAccessor = Reflection.getField(gameStateChange, float.class, 0);
public static final Class<?> position = Reflection.getClass("{nms}.PacketPlayInFlying$PacketPlayInPosition");
public static final Class<?> positionLook = Reflection.getClass("{nms}.PacketPlayInFlying$PacketPlayInPositionLook");
public static final Class<?> useItem = Reflection.getClass("{nms}.PacketPlayInUseItem");
public static final Class<?> blockDig = Reflection.getClass("{nms}.PacketPlayInBlockDig");
public static final Class<?> windowClick = Reflection.getClass("{nms}.PacketPlayInWindowClick");
@Getter
private static final List<Player> NOCLIPS = new ArrayList<>();
private static final Map<Player, Long> 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());
BiFunction<Player, Object, Object> first = (player, o) -> {
if (NOCLIPS.contains(player)) {
if (LAST_TICKS.getOrDefault(player, -1L).equals(TPSUtils.currentTick.get())) return o;
VersionedRunnable.call(new VersionedRunnable(() -> NoClipCommand_15.setGameModeInternal(player, GameMode.SPECTATOR), 15));
LAST_TICKS.put(player, TPSUtils.currentTick.get());
}
return o;
};
ProtocolAPI.setIncomingHandler(position, first);
ProtocolAPI.setIncomingHandler(positionLook, first);
BiFunction<Player, Object, Object> second = (player, o) -> {
if (NOCLIPS.contains(player)) {
if (LAST_TICKS.getOrDefault(player, -1L).equals(TPSUtils.currentTick.get())) return o;
VersionedRunnable.call(new VersionedRunnable(() -> NoClipCommand_15.setGameModeInternal(player, GameMode.SPECTATOR), 15));
LAST_TICKS.put(player, TPSUtils.currentTick.get());
}
});
ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(BauSystem.getInstance(), PacketType.Play.Client.USE_ITEM, PacketType.Play.Client.BLOCK_DIG, PacketType.Play.Client.WINDOW_CLICK) {
@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());
}
}
});
return o;
};
ProtocolAPI.setIncomingHandler(useItem, second);
ProtocolAPI.setIncomingHandler(blockDig, second);
ProtocolAPI.setIncomingHandler(windowClick, second);
}
@Register(help = true)
@ -93,23 +107,21 @@ public class NoClipCommand extends SWCommand implements Listener {
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);
Object playerInfoObject = Reflection.newInstance(playerInfo);
playerInfoAction.set(playerInfoObject, PacketPlayOutPlayerInfo.EnumPlayerInfoAction.UPDATE_GAME_MODE);
List<PlayerInfoData> 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);
playerInfoDataList.set(playerInfoObject, playerInfoActions);
Object gameStateChangeObject = Reflection.newInstance(gameStateChange);
integerFieldAccessor.set(gameStateChangeObject, 3);
floatFieldAccessor.set(gameStateChangeObject, 1F);
NOCLIPS.add(player);
BauSystem.MESSAGE.send("OTHER_NOCLIP_SLOT_INFO", 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");
}
ProtocolAPI.tinyProtocol.sendPacket(player, gameStateChangeObject);
ProtocolAPI.tinyProtocol.sendPacket(player, playerInfoObject);
}
}

Datei anzeigen

@ -0,0 +1,140 @@
/*
* 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.utils;
import com.comphenix.tinyprotocol.TinyProtocol;
import de.steamwar.bausystem.BauSystem;
import io.netty.channel.Channel;
import lombok.experimental.UtilityClass;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.UnaryOperator;
@UtilityClass
public class ProtocolAPI {
private static final Map<Class<?>, BiFunction<Player, Object, Object>> outgoingHandler = new HashMap<>();
private static final Map<Class<?>, BiFunction<Player, Object, Object>> incomingHandler = new HashMap<>();
public static final TinyProtocol tinyProtocol = new TinyProtocol(BauSystem.getInstance()) {
@Override
public Object onPacketOutAsync(Player receiver, Channel channel, Object packet) {
BiFunction<Player, Object, Object> handler = outgoingHandler.get(packet.getClass());
if (handler == null)
return packet;
return handler.apply(receiver, packet);
}
@Override
public Object onPacketInAsync(Player sender, Channel channel, Object packet) {
BiFunction<Player, Object, Object> handler = incomingHandler.get(packet.getClass());
if (handler == null)
return packet;
return handler.apply(sender, packet);
}
};
public static void setOutgoingHandler(Class<?> packetClass, BiFunction<Player, Object, Object> handler) {
outgoingHandler.put(packetClass, handler);
}
public static void removeOutgoingHandler(Class<?> packetClass) {
outgoingHandler.remove(packetClass);
}
public static void setIncomingHandler(Class<?> packetClass, BiFunction<Player, Object, Object> handler) {
incomingHandler.put(packetClass, handler);
}
public static void removeIncomingHandler(Class<?> packetClass) {
incomingHandler.remove(packetClass);
}
public static void broadcastPacket(Object packet) {
Bukkit.getOnlinePlayers().stream().map(tinyProtocol::getChannel).filter(tinyProtocol::hasInjected).forEach(channel -> tinyProtocol.sendPacket(channel, packet));
}
public static BiFunction<Object, UnaryOperator<Object>, Object> arrayCloneGenerator(Class<?> elementClass) {
return (array, worker) -> {
int length = Array.getLength(array);
Object result = Array.newInstance(elementClass, length);
for (int i = 0; i < length; i++)
Array.set(result, i, worker.apply(Array.get(array, i)));
return result;
};
}
public static UnaryOperator<Object> shallowCloneGenerator(Class<?> clazz) {
BiConsumer<Object, Object> filler = shallowFill(clazz);
return source -> {
Object clone;
try {
clone = clazz.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new IllegalStateException("Could not clone " + clazz.getName(), e);
}
filler.accept(source, clone);
return clone;
};
}
private static BiConsumer<Object, Object> shallowFill(Class<?> clazz) {
if (clazz == null)
return (source, clone) -> {
};
BiConsumer<Object, Object> superFiller = shallowFill(clazz.getSuperclass());
Field[] fds = clazz.getDeclaredFields();
List<Field> fields = new ArrayList<>();
for (Field field : fds) {
if (Modifier.isStatic(field.getModifiers()))
continue;
field.setAccessible(true);
fields.add(field);
}
return (source, clone) -> {
superFiller.accept(source, clone);
try {
for (Field field : fields) {
field.set(clone, field.get(source));
}
} catch (IllegalAccessException e) {
throw new IllegalStateException("Could not set field", e);
}
};
}
}