Skip to content

Commit 86656dc

Browse files
committed
example ESP module with packet handlers
1 parent 43ccbd7 commit 86656dc

5 files changed

Lines changed: 137 additions & 0 deletions

File tree

src/main/java/org/example/ExampleConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@ public static class ExampleModuleConfig {
1515
public boolean enabled = true;
1616
public int delayTicks = 250;
1717
}
18+
19+
public boolean esp = false;
1820
}

src/main/java/org/example/ExamplePlugin.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import com.zenith.api.PluginAPI;
55
import com.zenith.api.ZenithProxyPlugin;
66
import org.example.command.ExampleCommand;
7+
import org.example.command.ExampleESPCommand;
8+
import org.example.module.ExampleESPModule;
79
import org.example.module.ExampleModule;
810
import org.slf4j.Logger;
911

@@ -29,7 +31,9 @@ public void onLoad(PluginAPI pluginAPI) {
2931
// initialize any configurations before modules or commands might need to read them
3032
PLUGIN_CONFIG = pluginAPI.registerConfig("example-plugin", ExampleConfig.class);
3133
pluginAPI.registerModule(new ExampleModule());
34+
pluginAPI.registerModule(new ExampleESPModule());
3235
pluginAPI.registerCommand(new ExampleCommand());
36+
pluginAPI.registerCommand(new ExampleESPCommand());
3337
LOG.info("Example Plugin loaded!");
3438
}
3539
}

src/main/java/org/example/command/ExampleCommand.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
import com.zenith.command.brigadier.CommandCategory;
77
import com.zenith.command.brigadier.CommandContext;
88
import com.zenith.discord.Embed;
9+
import org.example.module.ExampleModule;
910

1011
import static com.mojang.brigadier.arguments.IntegerArgumentType.getInteger;
1112
import static com.mojang.brigadier.arguments.IntegerArgumentType.integer;
13+
import static com.zenith.Shared.MODULE;
1214
import static com.zenith.command.brigadier.ToggleArgumentType.getToggle;
1315
import static com.zenith.command.brigadier.ToggleArgumentType.toggle;
1416
import static org.example.ExamplePlugin.PLUGIN_CONFIG;
@@ -34,6 +36,8 @@ public LiteralArgumentBuilder<CommandContext> register() {
3436
return command("examplePlugin")
3537
.then(argument("toggle", toggle()).executes(c -> {
3638
PLUGIN_CONFIG.moduleConfig.enabled = getToggle(c, "toggle");
39+
// make sure to sync so the module is actually toggled
40+
MODULE.get(ExampleModule.class).syncEnabledFromConfig();
3741
c.getSource().getEmbed()
3842
.title("Example Plugin " + toggleStrCaps(PLUGIN_CONFIG.moduleConfig.enabled));
3943
return OK;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package org.example.command;
2+
3+
import com.google.common.collect.Lists;
4+
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
5+
import com.zenith.Proxy;
6+
import com.zenith.command.Command;
7+
import com.zenith.command.CommandUsage;
8+
import com.zenith.command.brigadier.CommandCategory;
9+
import com.zenith.command.brigadier.CommandContext;
10+
import org.example.ExamplePlugin;
11+
import org.example.module.ExampleESPModule;
12+
import org.geysermc.mcprotocollib.protocol.data.game.entity.metadata.EntityMetadata;
13+
import org.geysermc.mcprotocollib.protocol.data.game.entity.metadata.MetadataType;
14+
import org.geysermc.mcprotocollib.protocol.data.game.entity.metadata.type.ByteEntityMetadata;
15+
import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.entity.ClientboundSetEntityDataPacket;
16+
17+
import static com.zenith.Shared.CACHE;
18+
import static com.zenith.Shared.MODULE;
19+
import static com.zenith.command.brigadier.ToggleArgumentType.getToggle;
20+
import static com.zenith.command.brigadier.ToggleArgumentType.toggle;
21+
22+
public class ExampleESPCommand extends Command {
23+
@Override
24+
public CommandUsage commandUsage() {
25+
return CommandUsage.builder()
26+
.name("esp")
27+
.category(CommandCategory.MODULE)
28+
.description("Renders the spectral effect around all entities")
29+
.usageLines("on/off")
30+
.build();
31+
}
32+
33+
@Override
34+
public LiteralArgumentBuilder<CommandContext> register() {
35+
return command("esp")
36+
.then(argument("toggle", toggle()).executes(c -> {
37+
ExamplePlugin.PLUGIN_CONFIG.esp = getToggle(c, "toggle");
38+
// make sure to sync so the module is actually toggled
39+
MODULE.get(ExampleESPModule.class).syncEnabledFromConfig();
40+
var player = Proxy.getInstance().getActivePlayer();
41+
if (player != null) {
42+
// resend all entity metadata from cache
43+
CACHE.getEntityCache().getEntities().values().forEach(e -> {
44+
EntityMetadata<?, ?> toSend;
45+
toSend = e.getMetadata().get(0);
46+
if (toSend == null)
47+
toSend = new ByteEntityMetadata(0, MetadataType.BYTE, (byte) 0);
48+
player.sendAsync(new ClientboundSetEntityDataPacket(e.getEntityId(), Lists.newArrayList(toSend)));
49+
});
50+
}
51+
c.getSource().getEmbed()
52+
.title("ESP " + toggleStrCaps(ExamplePlugin.PLUGIN_CONFIG.esp))
53+
.primaryColor();
54+
return 1;
55+
}));
56+
}
57+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package org.example.module;
2+
3+
import com.zenith.module.Module;
4+
import com.zenith.network.registry.PacketHandler;
5+
import com.zenith.network.registry.PacketHandlerCodec;
6+
import com.zenith.network.registry.PacketHandlerStateCodec;
7+
import com.zenith.network.server.ServerSession;
8+
import org.example.ExamplePlugin;
9+
import org.geysermc.mcprotocollib.protocol.data.ProtocolState;
10+
import org.geysermc.mcprotocollib.protocol.data.game.entity.metadata.EntityMetadata;
11+
import org.geysermc.mcprotocollib.protocol.data.game.entity.metadata.MetadataType;
12+
import org.geysermc.mcprotocollib.protocol.data.game.entity.metadata.type.ByteEntityMetadata;
13+
import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.entity.ClientboundSetEntityDataPacket;
14+
15+
import java.util.ArrayList;
16+
17+
public class ExampleESPModule extends Module {
18+
@Override
19+
public boolean enabledSetting() {
20+
return ExamplePlugin.PLUGIN_CONFIG.esp;
21+
}
22+
23+
@Override
24+
public PacketHandlerCodec registerServerPacketHandlerCodec() {
25+
return PacketHandlerCodec.builder()
26+
.setId("esp")
27+
.setPriority(1000)
28+
.state(ProtocolState.GAME, PacketHandlerStateCodec.<ServerSession>builder()
29+
.registerInbound(ClientboundSetEntityDataPacket.class, new GlowingEntityMetadataPacketHandler())
30+
// or with in-line lambda:
31+
// .registerOutbound(ClientboundSetEntityDataPacket.class, (ClientboundSetEntityDataPacket packet, ServerSession session) -> {
32+
// ClientboundSetEntityDataPacket p = packet;
33+
// ...more impl...
34+
// return p;
35+
// })
36+
.build())
37+
.build();
38+
}
39+
40+
41+
// this can also be moved to a separate class file
42+
public static class GlowingEntityMetadataPacketHandler implements PacketHandler<ClientboundSetEntityDataPacket, ServerSession> {
43+
@Override
44+
public ClientboundSetEntityDataPacket apply(final ClientboundSetEntityDataPacket packet, final ServerSession session) {
45+
ClientboundSetEntityDataPacket p = packet;
46+
var metadata = packet.getMetadata();
47+
boolean edited = false;
48+
for (int i = 0; i < metadata.size(); i++) {
49+
final EntityMetadata<?, ?> entityMetadata = metadata.get(i);
50+
// https://minecraft.wiki/w/Minecraft_Wiki:Projects/wiki.vg_merge/Entity_metadata#Entity
51+
if (entityMetadata.getId() == 0 && entityMetadata.getType() == MetadataType.BYTE) {
52+
ByteEntityMetadata byteMetadata = (ByteEntityMetadata) entityMetadata;
53+
var newMetadata = new ByteEntityMetadata(0, MetadataType.BYTE, (byte) (byteMetadata.getPrimitiveValue() | 0x40));
54+
var newMetadataList = new ArrayList<>(metadata);
55+
newMetadataList.set(i, newMetadata);
56+
p = packet.withMetadata(newMetadataList);
57+
edited = true;
58+
break;
59+
}
60+
}
61+
if (!edited) {
62+
var newMetadata = new ArrayList<EntityMetadata<?, ?>>(metadata.size() + 1);
63+
newMetadata.addAll(packet.getMetadata());
64+
newMetadata.add(new ByteEntityMetadata(0, MetadataType.BYTE, (byte) 0x40));
65+
p = packet.withMetadata(newMetadata);
66+
}
67+
return p;
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)