Skip to content

Commit

Permalink
Working packet cancler
Browse files Browse the repository at this point in the history
  • Loading branch information
connorslade committed Jun 28, 2024
1 parent b27c42c commit b36f18e
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 300 deletions.
18 changes: 0 additions & 18 deletions src/main/java/com/connorcode/sigmautils/commands/Dump.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.connorcode.sigmautils.SigmaUtils;
import com.connorcode.sigmautils.config.Config;
import com.connorcode.sigmautils.config.settings.*;
import com.connorcode.sigmautils.misc.util.NetworkUtils;
import com.connorcode.sigmautils.module.Category;
import com.connorcode.sigmautils.module.DocumentedEnum;
import com.connorcode.sigmautils.module.Module;
Expand Down Expand Up @@ -54,23 +53,6 @@ int execute(CommandContext<FabricClientCommandSource> context) throws IOExceptio
var dumps = directory.resolve("dump").toFile();
dumps.mkdirs();

if (resource.equals("packets")) {
StringBuilder out = new StringBuilder("# Generated with `/util dump packets` (only works in dev builds)\n");
var mr = FabricLoader.getInstance().getMappingResolver();
for (var i : NetworkUtils.getPackets()
.keySet()
.stream()
.sorted(Comparator.comparing(Class::getSimpleName))
.toList())
out.append(mr.unmapClassName("intermediary", i.getName()))
.append(" ")
.append(i.getSimpleName())
.append("\n");
Files.write(dumps.toPath().resolve("packets.txt"), out.toString().getBytes());
context.getSource().sendFeedback(Text.of("Dumped packets to " + dumps.toPath().resolve("packets.txt")));
return 0;
}

if (resource.equals("docs")) {
var docs = directory.resolve("dump").resolve("docs").toFile();
docs.mkdirs();
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/connorcode/sigmautils/config/ConfigGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ protected void init() {
}

public void render(DrawContext drawContext, int mouseX, int mouseY, float delta) {
int padding = getPadding();
this.renderBackground(drawContext, mouseX, mouseY, delta);

int padding = getPadding();
super.render(drawContext, mouseX, mouseY, delta);

for (int x = 0; x < Category.realValues().length; x++)
drawContext.drawCenteredTextWithShadow(textRenderer,
Text.of("§f§n§l" + Category.realValues()[x].toString()),
75 + padding + x * (150 + padding), padding, 0);

super.render(drawContext, mouseX, mouseY, delta);
}

public void close() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,24 @@

import com.connorcode.sigmautils.config.settings.DynamicListSetting;
import com.connorcode.sigmautils.misc.util.NetworkUtils;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.nbt.NbtElement;
import net.minecraft.nbt.NbtIntArray;
import net.minecraft.nbt.NbtList;
import net.minecraft.nbt.NbtString;
import net.minecraft.network.NetworkSide;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.PacketType;
import net.minecraft.network.state.PlayStateFactories;
import net.minecraft.util.Identifier;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class PacketList implements DynamicListSetting.ResourceManager<Class<? extends Packet<?>>> {
DynamicListSetting<Class<? extends Packet<?>>> setting;
Object2IntOpenHashMap<Class<? extends Packet<?>>> packets = null;
public class PacketList implements DynamicListSetting.ResourceManager<PacketType<? extends Packet<?>>> {
DynamicListSetting<PacketType<? extends Packet<?>>> setting;
NetworkSide side;

public PacketList(DynamicListSetting<Class<? extends Packet<?>>> setting, NetworkSide side) {
public PacketList(DynamicListSetting<PacketType<? extends Packet<?>>> setting, NetworkSide side) {
this.setting = setting;
this.side = side;
}
Expand All @@ -30,52 +31,46 @@ public int width() {
}

@Override
public List<Class<? extends Packet<?>>> getAllResources() {
return getPackets().keySet().stream().toList();
public List<PacketType<? extends Packet<?>>> getAllResources() {
var out = new ArrayList<PacketType<? extends Packet<?>>>();
(side == NetworkSide.SERVERBOUND ? PlayStateFactories.C2S : PlayStateFactories.S2C).forEachPacketType(((type, protocolId) -> out.add(type)));
return out;
}

@Override
public String getDisplay(Class<? extends Packet<?>> resource) {
return Optional.ofNullable(NetworkUtils.getPacketName(resource)).orElse(resource.getName());
public String getDisplay(PacketType<? extends Packet<?>> resource) {
return resource.id().toString();
}

@Override
public void render(Class<? extends Packet<?>> resource, Screen data, int x, int y) {
public void render(PacketType<? extends Packet<?>> resource, Screen data, int x, int y) {
SimpleList.render(setting, resource, getDisplay(resource), data, x, y, 0);
}

@Override
public boolean renderSelector(Class<? extends Packet<?>> resource, Screen data, int x, int y) {
public boolean renderSelector(PacketType<? extends Packet<?>> resource, Screen data, int x, int y) {
if (setting.value().contains(resource))
return false;
SimpleList.selector(setting, resource, getDisplay(resource), data, x, y, 0);
return true;
}

@Override
public NbtElement serialize(List<Class<? extends Packet<?>>> resources) {
var thisPackets = getPackets();
var list = resources.stream().mapToInt(thisPackets::getInt).toArray();
return new NbtIntArray(list);
public NbtElement serialize(List<PacketType<? extends Packet<?>>> resources) {
var out = new NbtList();
for (var resource : resources)
out.add(NbtString.of(resource.id().getPath()));
return out;
}

@Override
public List<Class<? extends Packet<?>>> deserialize(NbtElement nbt) {
if (!(nbt instanceof NbtIntArray resourceList))
public List<PacketType<? extends Packet<?>>> deserialize(NbtElement nbt) {
if (!(nbt instanceof NbtList resourceList))
return null;
var list = resourceList.stream().map(e -> getPacket(e.intValue())).toList();
return new ArrayList<>(list);
}

Class<? extends Packet<?>> getPacket(int id) {
var thisPackets = getPackets();
return thisPackets.keySet().stream().filter(e -> thisPackets.getInt(e) == id).findFirst().orElse(null);
}

Object2IntOpenHashMap<Class<? extends Packet<?>>> getPackets() {
if (packets == null)
this.packets = NetworkUtils.getPackets(side);
return packets;
var out = new ArrayList<PacketType<? extends Packet<?>>>();
for (var resource : resourceList)
out.add(NetworkUtils.getPacket(Identifier.of(resource.asString())));
return new ArrayList<>(out);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,28 @@
import com.connorcode.sigmautils.misc.TextStyle;
import com.mojang.authlib.exceptions.AuthenticationException;
import com.mojang.authlib.yggdrasil.YggdrasilMinecraftSessionService;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.MappingResolver;
import net.minecraft.network.NetworkSide;
import net.minecraft.network.packet.Packet;
import org.jetbrains.annotations.Nullable;
import net.minecraft.network.packet.PacketType;
import net.minecraft.network.state.PlayStateFactories;
import net.minecraft.util.Identifier;

import java.util.HashMap;
import java.util.UUID;

import static com.connorcode.sigmautils.SigmaUtils.client;

public class NetworkUtils {
static HashMap<String, String> packetNames = new HashMap<>();
static MappingResolver mappingResolver = FabricLoader.getInstance().getMappingResolver();
static HashMap<Identifier, PacketType<? extends Packet<?>>> packetNames = new HashMap<>();
private static AuthStatus authStatus = AuthStatus.Unknown;
private static long lastAuthStatusCheck = 0;

public static Object2IntOpenHashMap<Class<? extends Packet<?>>> getPackets(NetworkSide side) {
var packets = new Object2IntOpenHashMap<Class<? extends Packet<?>>>();
// TODO: Fix
// Arrays.stream(NetworkPhase.values())
// .map(state -> state.packetHandlers.get(side))
// .filter(Objects::nonNull)
// .forEach(handler -> packets.putAll(handler.backingHandler.packetIds));
return packets;
static {
PlayStateFactories.C2S.forEachPacketType((type, protocolId) -> packetNames.put(type.id(), type));
PlayStateFactories.S2C.forEachPacketType((type, protocolId) -> packetNames.put(type.id(), type));
}

public static Object2IntOpenHashMap<Class<? extends Packet<?>>> getPackets() {
var packets = new Object2IntOpenHashMap<Class<? extends Packet<?>>>();
packets.putAll(getPackets(NetworkSide.CLIENTBOUND));
packets.putAll(getPackets(NetworkSide.SERVERBOUND));
return packets;
}

public static String getPacketName(Class<? extends Packet<?>> packet) {
return getPacketName(packet.getName());
}

@Nullable
public static String getPacketName(String bytecodeName) {
var intermediary = mappingResolver.unmapClassName("intermediary", bytecodeName);
if (packetNames.isEmpty())
Util.loadResourceString("assets/sigma-utils/packets.txt")
.lines()
.filter(line -> !line.startsWith("#"))
.map(line -> line.split(" "))
.forEach(parts -> packetNames.put(parts[0], parts[1]));
return packetNames.get(intermediary);
public static PacketType<? extends Packet<?>> getPacket(Identifier id) {
return packetNames.get(id);
}

public static synchronized AuthStatus getAuthStatus() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@
public class EntityMixin {
@Inject(method = "isInvisible", at = @At("HEAD"), cancellable = true)
void isInvisible(CallbackInfoReturnable<Boolean> cir) {
if (!Config.getEnabled(ShowInvisibleEntities.class) ||
!ShowInvisibleEntities.isInvisible((Entity) (Object) this))
if (!Config.getEnabled(ShowInvisibleEntities.class) || ShowInvisibleEntities.isVisible((Entity) (Object) this))
return;
cir.setReturnValue(false);
}

@Inject(method = "isInvisibleTo", at = @At("HEAD"), cancellable = true)
void isInvisibleTo(PlayerEntity player, CallbackInfoReturnable<Boolean> cir) {
if (!Config.getEnabled(ShowInvisibleEntities.class) ||
!ShowInvisibleEntities.isInvisible((Entity) (Object) this))
if (!Config.getEnabled(ShowInvisibleEntities.class) || ShowInvisibleEntities.isVisible((Entity) (Object) this))
return;
cir.setReturnValue(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,53 +6,53 @@
import com.connorcode.sigmautils.event.EventHandler;
import com.connorcode.sigmautils.event.network.PacketReceiveEvent;
import com.connorcode.sigmautils.event.network.PacketSendEvent;
import com.connorcode.sigmautils.misc.util.NetworkUtils;
import com.connorcode.sigmautils.module.Module;
import com.connorcode.sigmautils.module.ModuleInfo;
import net.minecraft.network.NetworkSide;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.PacketType;
import net.minecraft.text.Text;

import static com.connorcode.sigmautils.SigmaUtils.client;

// Warning: When updating versions the packets may change and this won't be automatically updated, causing unexpected behaviour (gasp)
@ModuleInfo(description = "Cancels the specified packets.")
public class PacketCanceler extends Module {
DynamicListSetting<Class<? extends Packet<?>>> clientPackets =
DynamicListSetting<PacketType<? extends Packet<?>>> clientPackets =
new DynamicListSetting<>(PacketCanceler.class, "Client Packets",
PacketCanceler::getClientPacketManager).description("Packets to the client.").build();
DynamicListSetting<Class<? extends Packet<?>>> serverPackets =
DynamicListSetting<PacketType<? extends Packet<?>>> serverPackets =
new DynamicListSetting<>(PacketCanceler.class, "Server Packets",
PacketCanceler::getServerPacketManager).description("Packets to the server.").build();
BoolSetting debug = new BoolSetting(PacketCanceler.class, "Debug")
.description("Prints debug messages when a packet is canceled.")
.build();

static DynamicListSetting.ResourceManager<Class<? extends Packet<?>>> getClientPacketManager(DynamicListSetting<Class<? extends Packet<?>>> setting) {
static DynamicListSetting.ResourceManager<PacketType<? extends Packet<?>>> getClientPacketManager(DynamicListSetting<PacketType<? extends Packet<?>>> setting) {
return new PacketList(setting, NetworkSide.CLIENTBOUND);
}

static DynamicListSetting.ResourceManager<Class<? extends Packet<?>>> getServerPacketManager(DynamicListSetting<Class<? extends Packet<?>>> setting) {
static DynamicListSetting.ResourceManager<PacketType<? extends Packet<?>>> getServerPacketManager(DynamicListSetting<PacketType<? extends Packet<?>>> setting) {
return new PacketList(setting, NetworkSide.SERVERBOUND);
}

@EventHandler
void onPacketSend(PacketSendEvent packet) {
if (!enabled || !serverPackets.value().contains(packet.get().getClass())) return;
if (!enabled || !serverPackets.value().contains(packet.packet.getPacketId())) return;
sendDebug(NetworkSide.SERVERBOUND, packet.get());
packet.cancel();
}

@EventHandler
void onPacketReceive(PacketReceiveEvent packet) {
if (!enabled || !clientPackets.value().contains(packet.get().getClass())) return;
if (!enabled || !clientPackets.value().contains(packet.packet.getPacketId())) return;
sendDebug(NetworkSide.CLIENTBOUND, packet.get());
packet.cancel();
}

void sendDebug(NetworkSide side, Packet<?> packet) {
if (client.player == null || !debug.value()) return;
client.player.sendMessage(Text.of(String.format("[SIGMAUTILS::PacketCancel] Cancelling %s packet %s", side,
NetworkUtils.getPacketName(packet.getClass().getName()))), false);
client.player.sendMessage(Text.of(String.format("[SIGMAUTILS::PacketCancel] Cancelling %s packet %s", side, packet.getPacketId()
.id().getPath())), false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import com.connorcode.sigmautils.config.settings.DynamicListSetting;
import com.connorcode.sigmautils.config.settings.EnumSetting;
import com.connorcode.sigmautils.config.settings.NumberSetting;
import com.connorcode.sigmautils.config.settings.list.EntityList;
import com.connorcode.sigmautils.misc.util.Util;
import com.connorcode.sigmautils.module.Module;
Expand All @@ -13,16 +12,17 @@

@ModuleInfo(description = "Render all invisible entities")
public class ShowInvisibleEntities extends Module {
public static NumberSetting opacity = new NumberSetting(ShowInvisibleEntities.class, "Opacity", 0, 1).value(0.25)
.description("Opacity of invisible entities.")
.build();
// TODO: Show invis entities as translucent
// public static NumberSetting opacity = new NumberSetting(ShowInvisibleEntities.class, "Opacity", 0, 1).value(0.25)
// .description("Opacity of invisible entities.")
// .build();
static EnumSetting<Util.FilterType> filterType = Util.filterSetting(ShowInvisibleEntities.class).build();
static DynamicListSetting<EntityType<?>> entities =
new DynamicListSetting<>(ShowInvisibleEntities.class, "Entities", EntityList::new).description(
"Entities show while invisible. Whitelisting an entities will make it not render flipped.")
.build();

public static boolean isInvisible(Entity entity) {
public static boolean isVisible(Entity entity) {
var inList = entities.value().contains(entity.getType());
return filterType.value() == Util.FilterType.Whitelist == inList;
}
Expand Down
Loading

0 comments on commit b36f18e

Please sign in to comment.