Skip to content

Commit

Permalink
chore: Initialize PacketEvents on load
Browse files Browse the repository at this point in the history
  • Loading branch information
LeeGodSRC committed Nov 30, 2024
1 parent c600fc7 commit cafe9b8
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,14 @@
import io.fairyproject.bukkit.listener.events.Events;
import io.fairyproject.bukkit.metadata.Metadata;
import io.fairyproject.bukkit.plugin.impl.RootJavaPluginIdentifier;
import io.fairyproject.bukkit.protocol.BukkitPacketEventsBuilder;
import io.fairyproject.bukkit.util.JavaPluginUtil;
import io.fairyproject.bukkit.util.SpigotUtil;
import io.fairyproject.container.PreInitialize;
import io.fairyproject.container.collection.ContainerObjCollector;
import io.fairyproject.log.Log;
import io.fairyproject.mc.protocol.MCProtocol;
import io.fairyproject.mc.protocol.PacketEventsBuilder;
import io.fairyproject.plugin.Plugin;
import io.fairyproject.plugin.PluginManager;
import io.fairyproject.util.URLClassLoaderAccess;
Expand Down Expand Up @@ -91,6 +94,7 @@ public FairyBukkitPlatform(File dataFolder) {
@Override
public void load(Plugin plugin) {
super.load(plugin);
this.loadProtocol();
}

@Override
Expand Down Expand Up @@ -127,6 +131,14 @@ public void onPostServicesInitial() {
Events.call(new PostServicesInitialEvent());
}

protected void loadProtocol() {
MCProtocol.loadProtocol(this.providePacketEventBuilder());
}

protected PacketEventsBuilder providePacketEventBuilder() {
return new BukkitPacketEventsBuilder(this);
}

@Override
public void saveResource(String name, boolean replace) {
PLUGIN.saveResource(name, replace);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package io.fairyproject.bukkit.protocol;

import com.github.retrooper.packetevents.PacketEventsAPI;
import com.github.retrooper.packetevents.settings.PacketEventsSettings;
import io.fairyproject.FairyPlatform;
import io.fairyproject.bukkit.FairyBukkitPlatform;
import io.fairyproject.mc.protocol.PacketEventsBuilder;
Expand All @@ -40,7 +41,8 @@ public class BukkitPacketEventsBuilder implements PacketEventsBuilder {
@Override
public PacketEventsAPI<?> build() {
PacketEventsAPI<Plugin> packetEventsAPI = SpigotPacketEventsBuilder.buildNoCache(FairyBukkitPlatform.PLUGIN);
packetEventsAPI.getSettings().reEncodeByDefault(false);
PacketEventsSettings settings = packetEventsAPI.getSettings();
settings.reEncodeByDefault(false);

return packetEventsAPI;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ public MCProtocol provideMCProtocol(
ContainerContext context,
MCPlayerRegistry playerRegistry,
MCVersionMappingRegistry versionMappingRegistry,
PacketEventsBuilder packetEventsBuilder,
PacketSender packetSender
) {
return new MCProtocol(context, playerRegistry, versionMappingRegistry, packetEventsBuilder, packetSender);
return new MCProtocol(context, playerRegistry, versionMappingRegistry, packetSender);
}

@InjectableComponent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,24 @@ public class MCProtocol {
private final ContainerContext context;
private final MCPlayerRegistry playerRegistry;
private final MCVersionMappingRegistry mappingRegistry;
private final PacketEventsBuilder packetEventsBuilder;
private final PacketSender packetSender;

private PacketEventsAPI<?> packetEvents;

private final Map<Class<?>, PacketListenerCommon> listenerCommonMap = new ConcurrentHashMap<>();

public static void loadProtocol(PacketEventsBuilder packetEventsBuilder) {
PacketEvents.setAPI(packetEventsBuilder.build());
PacketEvents.getAPI().load();

System.out.printf("Loaded PacketEvents with version %s on minecraft version %s%n", PacketEvents.getAPI().getVersion(), PacketEvents.getAPI().getServerManager().getVersion());
}

@PreInitialize
public void onPreInitialize() {
INSTANCE = this;

this.packetEvents = this.packetEventsBuilder.build();
PacketEvents.setAPI(this.packetEvents);
this.packetEvents.load();
this.packetEvents = PacketEvents.getAPI();

// automatically register PacketListener that are obj
this.registerPacketListenerObjectCollector();
Expand All @@ -82,8 +86,8 @@ public void onPreInitialize() {
@PostInitialize
public void onPostInitialize() {
this.packetEvents.getSettings()
.debug(false)
.bStats(false)
.debug(true)
.bStats(true)
.checkForUpdates(false)
.timeStampMode(TimeStampMode.MILLIS);
this.packetEvents.init();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.fairyproject.tests.bukkit;

import io.fairyproject.bukkit.FairyBukkitPlatform;
import io.fairyproject.mc.protocol.PacketEventsBuilder;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
Expand All @@ -24,4 +25,14 @@ public static void patchBukkitPlugin(JavaPlugin plugin) throws NoSuchFieldExcept
field.setAccessible(true);
field.set(null, plugin);
}

@Override
protected void loadProtocol() {
// Do nothing
}

@Override
public PacketEventsBuilder providePacketEventBuilder() {
throw new IllegalStateException("providePacketEventBuilder() should not be called in tests");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,35 @@

package io.fairyproject.tests.mc.configuration;

import io.fairyproject.container.ContainerContext;
import io.fairyproject.container.InjectableComponent;
import io.fairyproject.container.configuration.TestConfiguration;
import io.fairyproject.mc.MCServer;
import io.fairyproject.mc.protocol.MCProtocol;
import io.fairyproject.mc.protocol.PacketEventsBuilder;
import io.fairyproject.mc.protocol.packet.PacketSender;
import io.fairyproject.tests.mc.protocol.PacketSenderMock;
import io.fairyproject.mc.registry.player.MCPlayerRegistry;
import io.fairyproject.mc.version.MCVersionMappingRegistry;
import io.fairyproject.tests.mc.protocol.MockMCProtocol;
import io.fairyproject.tests.mc.protocol.MockPacketEventsBuilder;
import io.fairyproject.tests.mc.protocol.PacketSenderMock;

@TestConfiguration
public class MCProtocolTestConfiguration {

@InjectableComponent
public PacketEventsBuilder providePacketEventsBuilder(
MCServer mcServer,
MCVersionMappingRegistry versionMappingRegistry
) {
public PacketSender providePacketSender() {
return new PacketSenderMock();
}

@InjectableComponent
public PacketEventsBuilder providePacketEventsBuilder(MCServer mcServer, MCVersionMappingRegistry versionMappingRegistry) {
return new MockPacketEventsBuilder(mcServer, versionMappingRegistry);
}

@InjectableComponent
public PacketSender providePacketSender() {
return new PacketSenderMock();
public MCProtocol provideMCProtocol(ContainerContext context, MCPlayerRegistry mcPlayerRegistry, MCVersionMappingRegistry versionMappingRegistry, PacketSender packetSender, PacketEventsBuilder packetEventsBuilder) {
return new MockMCProtocol(context, mcPlayerRegistry, versionMappingRegistry, packetSender, packetEventsBuilder);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.fairyproject.tests.mc.protocol;

import io.fairyproject.container.ContainerContext;
import io.fairyproject.mc.protocol.MCProtocol;
import io.fairyproject.mc.protocol.PacketEventsBuilder;
import io.fairyproject.mc.protocol.packet.PacketSender;
import io.fairyproject.mc.registry.player.MCPlayerRegistry;
import io.fairyproject.mc.version.MCVersionMappingRegistry;

public class MockMCProtocol extends MCProtocol {

private final PacketEventsBuilder packetEventsBuilder;

public MockMCProtocol(ContainerContext context,
MCPlayerRegistry playerRegistry,
MCVersionMappingRegistry mappingRegistry,
PacketSender packetSender,
PacketEventsBuilder packetEventsBuilder) {
super(context, playerRegistry, mappingRegistry, packetSender);

this.packetEventsBuilder = packetEventsBuilder;
}

@Override
public void onPreInitialize() {
loadProtocol(packetEventsBuilder);

super.onPreInitialize();
}
}
2 changes: 1 addition & 1 deletion global.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version = 0.7.9b8-SNAPSHOT
version = 0.7.9b13-SNAPSHOT

0 comments on commit cafe9b8

Please sign in to comment.