Skip to content

Commit 2643288

Browse files
committed
cleanup and addn'l documentation
1 parent 0e5a3cb commit 2643288

8 files changed

Lines changed: 48 additions & 29 deletions

File tree

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Plugins are only supported on the `java` ZenithProxy release channel (i.e. not `
1010

1111
Place plugin jars in the `plugins` folder inside the same folder as the ZenithProxy launcher.
1212

13-
Restart ZenithProxy to load plugins, loading after launch or hot reloading is not supported.
13+
Restart ZenithProxy to load plugins. Loading plugins after launch or hot reloading is not supported.
1414

1515
## Creating Plugins
1616

@@ -41,9 +41,24 @@ I recommend looking at existing modules and commands for examples.
4141

4242
Execute the Gradle `build` task: `./gradlew build` - or double-click the task in Intellij
4343

44+
The built plugin jar will be in the `build/libs` directory.
45+
4446
### Testing Plugins
4547

4648
Execute the `run` task: `./gradlew run` - or double-click the task in Intellij
4749

4850
This will run ZenithProxy with your plugin loaded in the `run` directory.
4951

52+
### New Plugin Checklist
53+
54+
1. Edit `gradle.properties`:
55+
- `plugin_name` - Name of your plugin, used in the plugin jar name (e.g. `ExamplePlugin`)
56+
- `maven_group` - Java package for your project (e.g. `com.github.rfresh2`)
57+
1. Move files to your new corresponding package / maven group:
58+
- Example: `src/main/java/org/example` -> `src/main/java/com/github/rfresh2`
59+
- First create the new package in `src/main/java`. Then click and drag original subpackages/classes to your new one
60+
- Do this with Intellij to avoid manually editing all the source files
61+
- You must also move folders/package for the `src/main/templates` folder
62+
- Also make sure to update the package import at the very top of `BuiltConstants.java`, it will not be done automatically
63+
1. Edit `ExamplePlugin.java`, or remove it and create a new main class
64+
- Make sure to update the `@Plugin` annotation

build.gradle.kts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ zenithProxyPlugin {
1616

1717
repositories {
1818
mavenLocal() // if developing against a locally published zenith build
19+
maven("https://maven.2b2t.vc/releases") {
20+
description = "ZenithProxy Releases and Dependencies"
21+
}
1922
maven("https://maven.2b2t.vc/remote") {
2023
description = "Dependencies used by ZenithProxy"
2124
}
22-
maven("https://maven.2b2t.vc/releases") {
23-
description = "ZenithProxy Releases"
24-
}
2525
}
2626

2727
dependencies {

gradle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
plugin_version=1.0.0
2+
plugin_name=ZenithProxyExamplePlugin
23
mc=1.21.0
34
maven_group=org.example
45

settings.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ pluginManagement {
44
gradlePluginPortal()
55
}
66
}
7+
8+
rootProject.name = ext.properties["plugin_name"] as String

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
url = "https://github.com/rfresh2/ZenithProxyExamplePlugin",
1717
authors = {"rfresh2"},
1818
mcVersions = {"1.21.0"} // to indicate any MC version: @Plugin(mcVersions = "*")
19+
// if you touch packet classes, you almost certainly need to pin to a single mc version
1920
)
2021
public class ExamplePlugin implements ZenithProxyPlugin {
21-
// public static for easy access from modules and commands
22+
// public static for simple access from modules and commands
2223
// or alternatively, you could pass these around in constructors
2324
public static ExampleConfig PLUGIN_CONFIG;
2425
public static Logger LOG;

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,22 @@ public LiteralArgumentBuilder<CommandContext> register() {
3939
// make sure to sync so the module is actually toggled
4040
MODULE.get(ExampleModule.class).syncEnabledFromConfig();
4141
c.getSource().getEmbed()
42+
// if no title is set, no embed response will be sent
43+
// other properties like fields can be left unset without issues
4244
.title("Example Plugin " + toggleStrCaps(PLUGIN_CONFIG.exampleModule.enabled));
43-
return OK;
4445
}))
4546
.then(literal("delay").then(argument("ticks", integer(0)).executes(c -> {
4647
PLUGIN_CONFIG.exampleModule.delayTicks = getInteger(c, "ticks");
4748
c.getSource().getEmbed()
4849
.title("Delay Set");
49-
return OK;
5050
})));
5151
}
5252

5353
@Override
5454
public void defaultEmbed(Embed embed) {
5555
embed
5656
.primaryColor()
57-
.addField("Enabled", toggleStr(PLUGIN_CONFIG.exampleModule.enabled), false)
58-
.addField("Delay", PLUGIN_CONFIG.exampleModule.delayTicks + " ticks", false);
57+
.addField("Enabled", toggleStr(PLUGIN_CONFIG.exampleModule.enabled))
58+
.addField("Delay", PLUGIN_CONFIG.exampleModule.delayTicks + " ticks");
5959
}
6060
}

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ public CommandUsage commandUsage() {
2626
.name("esp")
2727
.category(CommandCategory.MODULE)
2828
.description("Renders the spectral effect around all entities")
29-
.usageLines("on/off")
29+
.usageLines(
30+
"on/off"
31+
)
3032
.build();
3133
}
3234

@@ -37,21 +39,25 @@ public LiteralArgumentBuilder<CommandContext> register() {
3739
ExamplePlugin.PLUGIN_CONFIG.esp = getToggle(c, "toggle");
3840
// make sure to sync so the module is actually toggled
3941
MODULE.get(ExampleESPModule.class).syncEnabledFromConfig();
40-
var player = Proxy.getInstance().getActivePlayer();
41-
if (player != null) {
42-
// resend all entity metadata from cache
42+
// array of sessions with the controlling player and any spectators
43+
var sessions = Proxy.getInstance().getActiveConnections().getArray();
44+
if (sessions.length > 0) {
45+
// resend entity metadata for every cached entity
46+
// if the module is now enabled, our outbound packet handler will modify the packets and add the metadata value
47+
// otherwise, this is resyncing the original metadata to players (i.e. removing the effect)
4348
CACHE.getEntityCache().getEntities().values().forEach(e -> {
4449
EntityMetadata<?, ?> toSend;
4550
toSend = e.getMetadata().get(0);
4651
if (toSend == null)
4752
toSend = new ByteEntityMetadata(0, MetadataTypes.BYTE, (byte) 0);
48-
player.sendAsync(new ClientboundSetEntityDataPacket(e.getEntityId(), Lists.newArrayList(toSend)));
53+
for (int i = 0; i < sessions.length; i++) {
54+
sessions[i].sendAsync(new ClientboundSetEntityDataPacket(e.getEntityId(), Lists.newArrayList(toSend)));
55+
}
4956
});
5057
}
5158
c.getSource().getEmbed()
5259
.title("ESP " + toggleStrCaps(ExamplePlugin.PLUGIN_CONFIG.esp))
5360
.primaryColor();
54-
return 1;
5561
}));
5662
}
5763
}

src/main/java/org/example/module/ExampleESPModule.java

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,29 +45,23 @@ public PacketHandlerCodec registerServerPacketHandlerCodec() {
4545
public static class GlowingEntityMetadataPacketHandler implements PacketHandler<ClientboundSetEntityDataPacket, ServerSession> {
4646
@Override
4747
public ClientboundSetEntityDataPacket apply(final ClientboundSetEntityDataPacket packet, final ServerSession session) {
48-
ClientboundSetEntityDataPacket p = packet;
4948
var metadata = packet.getMetadata();
50-
boolean edited = false;
5149
for (int i = 0; i < metadata.size(); i++) {
5250
final EntityMetadata<?, ?> entityMetadata = metadata.get(i);
5351
// https://minecraft.wiki/w/Minecraft_Wiki:Projects/wiki.vg_merge/Entity_metadata#Entity
54-
if (entityMetadata.getId() == 0 && entityMetadata.getType() == MetadataTypes.BYTE) {
55-
ByteEntityMetadata byteMetadata = (ByteEntityMetadata) entityMetadata;
52+
if (entityMetadata.getId() == 0 && entityMetadata instanceof ByteEntityMetadata byteMetadata) {
53+
// found the metadata id we want to edit
5654
var newMetadata = new ByteEntityMetadata(0, MetadataTypes.BYTE, (byte) (byteMetadata.getPrimitiveValue() | 0x40));
55+
// copy to avoid mutating potentially cached data
5756
var newMetadataList = new ArrayList<>(metadata);
5857
newMetadataList.set(i, newMetadata);
59-
p = packet.withMetadata(newMetadataList);
60-
edited = true;
61-
break;
58+
return packet.withMetadata(newMetadataList);
6259
}
6360
}
64-
if (!edited) {
65-
var newMetadata = new ArrayList<EntityMetadata<?, ?>>(metadata.size() + 1);
66-
newMetadata.addAll(packet.getMetadata());
67-
newMetadata.add(new ByteEntityMetadata(0, MetadataTypes.BYTE, (byte) 0x40));
68-
p = packet.withMetadata(newMetadata);
69-
}
70-
return p;
61+
// metadata id wasn't present, so we need to add it
62+
var newMetadata = new ArrayList<>(packet.getMetadata());
63+
newMetadata.addFirst(new ByteEntityMetadata(0, MetadataTypes.BYTE, (byte) 0x40));
64+
return packet.withMetadata(newMetadata);
7165
}
7266
}
7367
}

0 commit comments

Comments
 (0)