From d6bec5d88b71f3bcbe9ee9d9d044ef285c12fdf2 Mon Sep 17 00:00:00 2001 From: Auxilor Date: Sun, 27 Jun 2021 14:58:57 +0100 Subject: [PATCH] Added EcoPlugin#getProxy --- .../eco/core/AbstractPacketAdapter.java | 5 + .../java/com/willfp/eco/core/EcoPlugin.java | 95 ++++++++++++++++++- .../com/willfp/eco/internal/Internals.java | 4 + .../com/willfp/eco/spigot/EcoInternals.java | 10 ++ .../willfp/eco/spigot/EcoSpigotPlugin.java | 6 +- .../willfp/eco/spigot/InternalProxyUtils.java | 20 ---- .../eco/spigot/display/PacketAutoRecipe.java | 7 +- .../willfp/eco/spigot/display/PacketChat.java | 7 +- .../display/PacketOpenWindowMerchant.java | 5 +- 9 files changed, 120 insertions(+), 39 deletions(-) delete mode 100644 eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/InternalProxyUtils.java diff --git a/eco-api/src/main/java/com/willfp/eco/core/AbstractPacketAdapter.java b/eco-api/src/main/java/com/willfp/eco/core/AbstractPacketAdapter.java index 8139a860..8f5a9a65 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/AbstractPacketAdapter.java +++ b/eco-api/src/main/java/com/willfp/eco/core/AbstractPacketAdapter.java @@ -142,6 +142,11 @@ public abstract class AbstractPacketAdapter extends PacketAdapter { onSend(event.getPacket(), event.getPlayer(), event); } + @Override + public final EcoPlugin getPlugin() { + return (EcoPlugin) super.getPlugin(); + } + /** * Register the packet adapter with ProtocolLib. */ diff --git a/eco-api/src/main/java/com/willfp/eco/core/EcoPlugin.java b/eco-api/src/main/java/com/willfp/eco/core/EcoPlugin.java index 6b2b5625..65bffbe2 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/EcoPlugin.java +++ b/eco-api/src/main/java/com/willfp/eco/core/EcoPlugin.java @@ -1,10 +1,9 @@ package com.willfp.eco.core; import com.willfp.eco.core.command.AbstractCommand; +import com.willfp.eco.core.config.ConfigHandler; import com.willfp.eco.core.config.base.ConfigYml; import com.willfp.eco.core.config.base.LangYml; -import com.willfp.eco.core.config.ConfigHandler; -import com.willfp.eco.internal.config.updating.EcoConfigHandler; import com.willfp.eco.core.display.Display; import com.willfp.eco.core.display.DisplayModule; import com.willfp.eco.core.events.EventManager; @@ -14,9 +13,12 @@ import com.willfp.eco.core.factory.NamespacedKeyFactory; import com.willfp.eco.core.factory.RunnableFactory; import com.willfp.eco.core.integrations.IntegrationLoader; import com.willfp.eco.core.integrations.placeholder.PlaceholderManager; +import com.willfp.eco.core.proxy.AbstractProxy; import com.willfp.eco.core.scheduling.Scheduler; +import com.willfp.eco.internal.Internals; import com.willfp.eco.internal.UpdateChecker; import com.willfp.eco.internal.arrows.ArrowDataListener; +import com.willfp.eco.internal.config.updating.EcoConfigHandler; import com.willfp.eco.internal.events.EcoEventManager; import com.willfp.eco.internal.extensions.EcoExtensionLoader; import com.willfp.eco.internal.factory.EcoMetadataValueFactory; @@ -168,6 +170,12 @@ public abstract class EcoPlugin extends JavaPlugin { @Getter private boolean outdated = false; + /** + * If the plugin supports extensions. + */ + @Getter + private final boolean supportingExtensions; + /** * Create a new plugin without a specified color, proxy support, spigot, or bStats. */ @@ -197,7 +205,7 @@ public abstract class EcoPlugin extends JavaPlugin { } /** - * Create a new plugin without proxy support. + * Create a new plugin without proxy or extension support. * * @param resourceId The spigot resource ID for the plugin. * @param bStatsId The bStats resource ID for the plugin. @@ -210,7 +218,22 @@ public abstract class EcoPlugin extends JavaPlugin { } /** - * Create a new plugin. + * Create a new plugin without proxy support. + * + * @param resourceId The spigot resource ID for the plugin. + * @param bStatsId The bStats resource ID for the plugin. + * @param color The color of the plugin (used in messages, such as &a, &b) + * @param supportingExtensions If the plugin supports extensions. + */ + protected EcoPlugin(final int resourceId, + final int bStatsId, + @NotNull final String color, + final boolean supportingExtensions) { + this(resourceId, bStatsId, "", color, supportingExtensions); + } + + /** + * Create a new plugin without extension support. * * @param resourceId The spigot resource ID for the plugin. * @param bStatsId The bStats resource ID for the plugin. @@ -221,7 +244,24 @@ public abstract class EcoPlugin extends JavaPlugin { final int bStatsId, @NotNull final String proxyPackage, @NotNull final String color) { - this("", resourceId, bStatsId, proxyPackage, color); + this(resourceId, bStatsId, proxyPackage, color, false); + } + + /** + * Create a new plugin. + * + * @param resourceId The spigot resource ID for the plugin. + * @param bStatsId The bStats resource ID for the plugin. + * @param proxyPackage The package where proxy implementations are stored. + * @param color The color of the plugin (used in messages, such as &a, &b) + * @param supportingExtensions If the plugin supports extensions. + */ + protected EcoPlugin(final int resourceId, + final int bStatsId, + @NotNull final String proxyPackage, + @NotNull final String color, + final boolean supportingExtensions) { + this("", resourceId, bStatsId, proxyPackage, color, supportingExtensions); } /** @@ -241,11 +281,34 @@ public abstract class EcoPlugin extends JavaPlugin { final int bStatsId, @NotNull final String proxyPackage, @NotNull final String color) { + this(pluginName, resourceId, bStatsId, proxyPackage, color, false); + } + + /** + * Create a new plugin. + * + * @param pluginName The name of the plugin. + * @param resourceId The spigot resource ID for the plugin. + * @param bStatsId The bStats resource ID for the plugin. + * @param proxyPackage The package where proxy implementations are stored. + * @param color The color of the plugin (used in messages, such as &a, &b) + * @param supportingExtensions If the plugin supports extensions. + * @deprecated pluginName is redundant. + */ + @Deprecated + @SuppressWarnings("unused") + protected EcoPlugin(@NotNull final String pluginName, + final int resourceId, + final int bStatsId, + @NotNull final String proxyPackage, + @NotNull final String color, + final boolean supportingExtensions) { this.pluginName = this.getName(); this.resourceId = resourceId; this.bStatsId = bStatsId; this.proxyPackage = proxyPackage; this.color = color; + this.supportingExtensions = supportingExtensions; this.scheduler = new EcoScheduler(this); this.eventManager = new EcoEventManager(this); @@ -327,6 +390,17 @@ public abstract class EcoPlugin extends JavaPlugin { this.updatableClasses.forEach(clazz -> this.getConfigHandler().registerUpdatableClass(clazz)); + if (this.isSupportingExtensions()) { + this.getExtensionLoader().loadExtensions(); + + if (this.getExtensionLoader().getLoadedExtensions().isEmpty()) { + this.getLogger().info("&cNo extensions found"); + } else { + this.getLogger().info("Extensions Loaded:"); + this.getExtensionLoader().getLoadedExtensions().forEach(extension -> this.getLogger().info("- " + extension.getName() + " v" + extension.getVersion())); + } + } + this.enable(); this.getLogger().info(""); @@ -513,6 +587,17 @@ public abstract class EcoPlugin extends JavaPlugin { return null; } + /** + * Get the implementation of a specified proxy. + * + * @param proxyClass The proxy interface. + * @param The type of the proxy. + * @return The proxy implementation. + */ + public @NotNull final T getProxy(@NotNull final Class proxyClass) { + return Internals.getInstance().getProxy(this, proxyClass); + } + /** * Get the plugin's logger. * diff --git a/eco-api/src/main/java/com/willfp/eco/internal/Internals.java b/eco-api/src/main/java/com/willfp/eco/internal/Internals.java index b5b7bf5c..181ff49d 100644 --- a/eco-api/src/main/java/com/willfp/eco/internal/Internals.java +++ b/eco-api/src/main/java/com/willfp/eco/internal/Internals.java @@ -1,6 +1,7 @@ package com.willfp.eco.internal; import com.willfp.eco.core.EcoPlugin; +import com.willfp.eco.core.proxy.AbstractProxy; import org.jetbrains.annotations.NotNull; public abstract class Internals { @@ -12,6 +13,9 @@ public abstract class Internals { public abstract EcoPlugin getPlugin(); + public @NotNull abstract T getProxy(@NotNull EcoPlugin plugin, + @NotNull Class proxyClass); + public static Internals getInstance() { return internals; } diff --git a/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/EcoInternals.java b/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/EcoInternals.java index 3ee3a138..dcefa629 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/EcoInternals.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/EcoInternals.java @@ -1,6 +1,9 @@ package com.willfp.eco.spigot; +import com.willfp.eco.core.EcoPlugin; +import com.willfp.eco.core.proxy.AbstractProxy; import com.willfp.eco.internal.Internals; +import com.willfp.eco.proxy.util.ProxyFactory; import org.jetbrains.annotations.NotNull; public class EcoInternals extends Internals { @@ -14,4 +17,11 @@ public class EcoInternals extends Internals { public EcoSpigotPlugin getPlugin() { return plugin; } + + @Override + @NotNull + public T getProxy(@NotNull final EcoPlugin plugin, + @NotNull final Class proxyClass) { + return new ProxyFactory<>(plugin, proxyClass).getProxy(); + } } diff --git a/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/EcoSpigotPlugin.java b/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/EcoSpigotPlugin.java index 37d4337b..4c89e1c5 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/EcoSpigotPlugin.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/EcoSpigotPlugin.java @@ -71,13 +71,13 @@ public class EcoSpigotPlugin extends EcoPlugin { instance = this; Display.setFinalizeKey(this.getNamespacedKeyFactory().create("finalized")); - SkullProxy skullProxy = InternalProxyUtils.getProxy(SkullProxy.class); + SkullProxy skullProxy = instance.getProxy(SkullProxy.class); SkullUtils.initialize(skullProxy::setSkullTexture); - BlockBreakProxy blockBreakProxy = InternalProxyUtils.getProxy(BlockBreakProxy.class); + BlockBreakProxy blockBreakProxy = instance.getProxy(BlockBreakProxy.class); BlockUtils.initialize(blockBreakProxy::breakBlock); - TridentStackProxy tridentStackProxy = InternalProxyUtils.getProxy(TridentStackProxy.class); + TridentStackProxy tridentStackProxy = instance.getProxy(TridentStackProxy.class); TridentUtils.initialize(tridentStackProxy::getTridentStack); this.dataJson = new DataJson(this); diff --git a/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/InternalProxyUtils.java b/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/InternalProxyUtils.java deleted file mode 100644 index 9f0788a0..00000000 --- a/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/InternalProxyUtils.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.willfp.eco.spigot; - -import com.willfp.eco.core.proxy.AbstractProxy; -import com.willfp.eco.proxy.util.ProxyFactory; -import lombok.experimental.UtilityClass; -import org.jetbrains.annotations.NotNull; - -@UtilityClass -public class InternalProxyUtils { - /** - * Get the implementation of a specified proxy. - * - * @param proxyClass The proxy interface. - * @param The type of the proxy. - * @return The proxy implementation. - */ - public @NotNull T getProxy(@NotNull final Class proxyClass) { - return new ProxyFactory<>(EcoSpigotPlugin.getInstance(), proxyClass).getProxy(); - } -} diff --git a/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/display/PacketAutoRecipe.java b/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/display/PacketAutoRecipe.java index ecb67128..d3206444 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/display/PacketAutoRecipe.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/display/PacketAutoRecipe.java @@ -3,10 +3,9 @@ package com.willfp.eco.spigot.display; import com.comphenix.protocol.PacketType; import com.comphenix.protocol.ProtocolLibrary; import com.comphenix.protocol.events.PacketContainer; -import com.willfp.eco.proxy.proxies.AutoCraftProxy; -import com.willfp.eco.spigot.InternalProxyUtils; -import com.willfp.eco.core.EcoPlugin; import com.willfp.eco.core.AbstractPacketAdapter; +import com.willfp.eco.core.EcoPlugin; +import com.willfp.eco.proxy.proxies.AutoCraftProxy; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; @@ -34,7 +33,7 @@ public class PacketAutoRecipe extends AbstractPacketAdapter { } try { - InternalProxyUtils.getProxy(AutoCraftProxy.class).modifyPacket(packet.getHandle()); + this.getPlugin().getProxy(AutoCraftProxy.class).modifyPacket(packet.getHandle()); } catch (NoSuchFieldException | IllegalAccessException e) { e.printStackTrace(); } diff --git a/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/display/PacketChat.java b/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/display/PacketChat.java index 63b19799..78a8d2a8 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/display/PacketChat.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/display/PacketChat.java @@ -4,10 +4,9 @@ import com.comphenix.protocol.PacketType; import com.comphenix.protocol.events.ListenerPriority; import com.comphenix.protocol.events.PacketContainer; import com.comphenix.protocol.wrappers.WrappedChatComponent; -import com.willfp.eco.proxy.proxies.ChatComponentProxy; -import com.willfp.eco.spigot.InternalProxyUtils; -import com.willfp.eco.core.EcoPlugin; import com.willfp.eco.core.AbstractPacketAdapter; +import com.willfp.eco.core.EcoPlugin; +import com.willfp.eco.proxy.proxies.ChatComponentProxy; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; @@ -34,7 +33,7 @@ public class PacketChat extends AbstractPacketAdapter { return; } - WrappedChatComponent newComponent = WrappedChatComponent.fromHandle(InternalProxyUtils.getProxy(ChatComponentProxy.class).modifyComponent(component.getHandle())); + WrappedChatComponent newComponent = WrappedChatComponent.fromHandle(this.getPlugin().getProxy(ChatComponentProxy.class).modifyComponent(component.getHandle())); packet.getChatComponents().write(i, newComponent); } } diff --git a/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/display/PacketOpenWindowMerchant.java b/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/display/PacketOpenWindowMerchant.java index ea10c268..0f496550 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/display/PacketOpenWindowMerchant.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/display/PacketOpenWindowMerchant.java @@ -7,7 +7,6 @@ import com.comphenix.protocol.events.PacketEvent; import com.willfp.eco.core.AbstractPacketAdapter; import com.willfp.eco.core.EcoPlugin; import com.willfp.eco.proxy.proxies.VillagerTradeProxy; -import com.willfp.eco.spigot.InternalProxyUtils; import org.bukkit.NamespacedKey; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemFlag; @@ -35,7 +34,7 @@ public class PacketOpenWindowMerchant extends AbstractPacketAdapter { @NotNull final PacketEvent event) { List recipes = new ArrayList<>(); - if (((EcoPlugin) this.getPlugin()).getConfigYml().getBool("villager-display-fix")) { + if (this.getPlugin().getConfigYml().getBool("villager-display-fix")) { for (MerchantRecipe recipe : packet.getMerchantRecipeLists().read(0)) { ItemStack result = recipe.getResult(); ItemMeta meta = result.getItemMeta(); @@ -48,7 +47,7 @@ public class PacketOpenWindowMerchant extends AbstractPacketAdapter { } for (MerchantRecipe recipe : packet.getMerchantRecipeLists().read(0)) { - MerchantRecipe newRecipe = InternalProxyUtils.getProxy(VillagerTradeProxy.class).displayTrade(recipe); + MerchantRecipe newRecipe = this.getPlugin().getProxy(VillagerTradeProxy.class).displayTrade(recipe); recipes.add(newRecipe); }