diff --git a/framework/platforms/bukkit-platform/src/main/java/io/fairyproject/bukkit/FairyBukkitPlatform.java b/framework/platforms/bukkit-platform/src/main/java/io/fairyproject/bukkit/FairyBukkitPlatform.java index 58df40ef..b2c3fcaf 100644 --- a/framework/platforms/bukkit-platform/src/main/java/io/fairyproject/bukkit/FairyBukkitPlatform.java +++ b/framework/platforms/bukkit-platform/src/main/java/io/fairyproject/bukkit/FairyBukkitPlatform.java @@ -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; @@ -91,6 +94,7 @@ public FairyBukkitPlatform(File dataFolder) { @Override public void load(Plugin plugin) { super.load(plugin); + this.loadProtocol(); } @Override @@ -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); diff --git a/framework/platforms/bukkit-platform/src/main/java/io/fairyproject/bukkit/configuration/BukkitProtocolConfiguration.java b/framework/platforms/bukkit-platform/src/main/java/io/fairyproject/bukkit/configuration/BukkitProtocolConfiguration.java deleted file mode 100644 index 14bce096..00000000 --- a/framework/platforms/bukkit-platform/src/main/java/io/fairyproject/bukkit/configuration/BukkitProtocolConfiguration.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * MIT License - * - * Copyright (c) 2022 Fairy Project - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package io.fairyproject.bukkit.configuration; - -import io.fairyproject.FairyPlatform; -import io.fairyproject.bukkit.protocol.BukkitPacketEventsBuilder; -import io.fairyproject.container.InjectableComponent; -import io.fairyproject.container.configuration.Configuration; -import io.fairyproject.mc.protocol.PacketEventsBuilder; - -@Configuration -public class BukkitProtocolConfiguration { - - @InjectableComponent - public PacketEventsBuilder providePacketEventsBuilder(FairyPlatform platform) { - return new BukkitPacketEventsBuilder(platform); - } - -} diff --git a/framework/platforms/bukkit-platform/src/main/java/io/fairyproject/bukkit/protocol/BukkitPacketEventsBuilder.java b/framework/platforms/bukkit-platform/src/main/java/io/fairyproject/bukkit/protocol/BukkitPacketEventsBuilder.java index dff9f236..4d78422f 100644 --- a/framework/platforms/bukkit-platform/src/main/java/io/fairyproject/bukkit/protocol/BukkitPacketEventsBuilder.java +++ b/framework/platforms/bukkit-platform/src/main/java/io/fairyproject/bukkit/protocol/BukkitPacketEventsBuilder.java @@ -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; @@ -40,7 +41,8 @@ public class BukkitPacketEventsBuilder implements PacketEventsBuilder { @Override public PacketEventsAPI build() { PacketEventsAPI packetEventsAPI = SpigotPacketEventsBuilder.buildNoCache(FairyBukkitPlatform.PLUGIN); - packetEventsAPI.getSettings().reEncodeByDefault(false); + PacketEventsSettings settings = packetEventsAPI.getSettings(); + settings.reEncodeByDefault(false); return packetEventsAPI; } diff --git a/framework/platforms/mc-platform/src/main/java/io/fairyproject/mc/configuration/MCProtocolConfiguration.java b/framework/platforms/mc-platform/src/main/java/io/fairyproject/mc/configuration/MCProtocolConfiguration.java index cfa49deb..3e3259b6 100644 --- a/framework/platforms/mc-platform/src/main/java/io/fairyproject/mc/configuration/MCProtocolConfiguration.java +++ b/framework/platforms/mc-platform/src/main/java/io/fairyproject/mc/configuration/MCProtocolConfiguration.java @@ -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 diff --git a/framework/platforms/mc-platform/src/main/java/io/fairyproject/mc/protocol/MCProtocol.java b/framework/platforms/mc-platform/src/main/java/io/fairyproject/mc/protocol/MCProtocol.java index 870e57ce..5c6c6dbb 100644 --- a/framework/platforms/mc-platform/src/main/java/io/fairyproject/mc/protocol/MCProtocol.java +++ b/framework/platforms/mc-platform/src/main/java/io/fairyproject/mc/protocol/MCProtocol.java @@ -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, 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(); @@ -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(); diff --git a/framework/tests/bukkit-tests/src/main/java/io/fairyproject/tests/bukkit/FairyBukkitTestingPlatform.java b/framework/tests/bukkit-tests/src/main/java/io/fairyproject/tests/bukkit/FairyBukkitTestingPlatform.java index f66e726c..2cc174f1 100644 --- a/framework/tests/bukkit-tests/src/main/java/io/fairyproject/tests/bukkit/FairyBukkitTestingPlatform.java +++ b/framework/tests/bukkit-tests/src/main/java/io/fairyproject/tests/bukkit/FairyBukkitTestingPlatform.java @@ -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; @@ -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"); + } } diff --git a/framework/tests/mc-tests/src/main/java/io/fairyproject/tests/mc/configuration/MCProtocolTestConfiguration.java b/framework/tests/mc-tests/src/main/java/io/fairyproject/tests/mc/configuration/MCProtocolTestConfiguration.java index f3dc9af7..6ce3221e 100644 --- a/framework/tests/mc-tests/src/main/java/io/fairyproject/tests/mc/configuration/MCProtocolTestConfiguration.java +++ b/framework/tests/mc-tests/src/main/java/io/fairyproject/tests/mc/configuration/MCProtocolTestConfiguration.java @@ -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); } } diff --git a/framework/tests/mc-tests/src/main/java/io/fairyproject/tests/mc/protocol/MockMCProtocol.java b/framework/tests/mc-tests/src/main/java/io/fairyproject/tests/mc/protocol/MockMCProtocol.java new file mode 100644 index 00000000..c65d72c5 --- /dev/null +++ b/framework/tests/mc-tests/src/main/java/io/fairyproject/tests/mc/protocol/MockMCProtocol.java @@ -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(); + } +} diff --git a/global.properties b/global.properties index ecf5f844..8f72c121 100644 --- a/global.properties +++ b/global.properties @@ -1 +1 @@ -version = 0.7.9b8-SNAPSHOT \ No newline at end of file +version = 0.7.9b13-SNAPSHOT \ No newline at end of file