| 1 | package com.jellypudding.offlineclient.modules.misc;
|
| 2 |
|
| 3 | import com.jellypudding.offlineclient.event.Subscribe;
|
| 4 | import com.jellypudding.offlineclient.event.events.PacketReceiveEvent;
|
| 5 | import com.jellypudding.offlineclient.event.events.PacketSendEvent;
|
| 6 | import com.jellypudding.offlineclient.module.Category;
|
| 7 | import com.jellypudding.offlineclient.module.Module;
|
| 8 | import com.jellypudding.offlineclient.setting.ChoiceListSetting;
|
| 9 | import com.jellypudding.offlineclient.util.PacketNames;
|
| 10 | import net.minecraft.network.protocol.Packet;
|
| 11 | import net.minecraft.network.protocol.PacketType;
|
| 12 |
|
| 13 | import java.util.HashSet;
|
| 14 | import java.util.Set;
|
| 15 | import java.util.concurrent.atomic.AtomicInteger;
|
| 16 | import java.util.function.Function;
|
| 17 |
|
| 18 | // Both lists offer every packet kind the game knows. Login and configuration
|
| 19 | // packets can be dropped before a world exists.
|
| 20 | public final class PacketCanceller extends Module {
|
| 21 |
|
| 22 | private final ChoiceListSetting outgoing = new ChoiceListSetting("Outgoing",
|
| 23 | "Packets to drop on the way to the server. Click to pick them.",
|
| 24 | PacketNames::outgoing)
|
| 25 | .onChange(this::resolve);
|
| 26 | private final ChoiceListSetting incoming = new ChoiceListSetting("Incoming",
|
| 27 | "Packets to drop before the game handles them. Click to pick them.",
|
| 28 | PacketNames::incoming)
|
| 29 | .onChange(this::resolve);
|
| 30 |
|
| 31 | // The picked names resolved to types. Read from the netty thread and replaced whole.
|
| 32 | private volatile Set<PacketType<?>> dropOutgoing = Set.of();
|
| 33 | private volatile Set<PacketType<?>> dropIncoming = Set.of();
|
| 34 |
|
| 35 | private final AtomicInteger dropped = new AtomicInteger();
|
| 36 |
|
| 37 | public PacketCanceller() {
|
| 38 | super("PacketCanceller", "Drops the kinds of packet you pick. Hides an action from the server or ignores what it sends.",
|
| 39 | Category.MISC);
|
| 40 | addSettings(outgoing, incoming);
|
| 41 | searchTags("packet filter", "block packets");
|
| 42 | }
|
| 43 |
|
| 44 | @Override
|
| 45 | public String getSuffix() {
|
| 46 | int count = dropped.get();
|
| 47 | return count == 0 ? null : count + " dropped";
|
| 48 | }
|
| 49 |
|
| 50 | @Override
|
| 51 | protected void onEnable() {
|
| 52 | dropped.set(0);
|
| 53 | resolve();
|
| 54 | }
|
| 55 |
|
| 56 | @Override
|
| 57 | protected void onDisable() {
|
| 58 | dropped.set(0);
|
| 59 | }
|
| 60 |
|
| 61 | private void resolve() {
|
| 62 | dropOutgoing = resolve(outgoing.getValue(), PacketNames::outgoing);
|
| 63 | dropIncoming = resolve(incoming.getValue(), PacketNames::incoming);
|
| 64 | }
|
| 65 |
|
| 66 | private static Set<PacketType<?>> resolve(Set<String> names,
|
| 67 | Function<String, PacketType<?>> lookup) {
|
| 68 | Set<PacketType<?>> types = new HashSet<>();
|
| 69 | for (String name : names) {
|
| 70 | PacketType<?> type = lookup.apply(name);
|
| 71 | if (type != null) {
|
| 72 | types.add(type);
|
| 73 | }
|
| 74 | }
|
| 75 | return Set.copyOf(types);
|
| 76 | }
|
| 77 |
|
| 78 | @Subscribe(priority = 200)
|
| 79 | private void onPacketSend(PacketSendEvent event) {
|
| 80 | Packet<?> packet = event.getPacket();
|
| 81 | if (dropOutgoing.contains(packet.type())) {
|
| 82 | event.cancel();
|
| 83 | dropped.incrementAndGet();
|
| 84 | }
|
| 85 | }
|
| 86 |
|
| 87 | @Subscribe(priority = 200)
|
| 88 | private void onPacketReceive(PacketReceiveEvent event) {
|
| 89 | Packet<?> packet = event.getPacket();
|
| 90 | if (dropIncoming.contains(packet.type())) {
|
| 91 | event.cancel();
|
| 92 | dropped.incrementAndGet();
|
| 93 | }
|
| 94 | }
|
| 95 | }
|
| 96 |
|