Skip to content

Commit

Permalink
shifting around plugin support shit
Browse files Browse the repository at this point in the history
  • Loading branch information
casperwtf committed Jan 29, 2024
1 parent 679014c commit 47c3fd9
Show file tree
Hide file tree
Showing 16 changed files with 194 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/main/java/net/crashcraft/crashclaim/CrashClaim.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public void onLoad() {

this.crashUtils = new CrashUtils(this);
this.pluginSupport = new PluginSupportManager(this); // Enable plugin support
this.pluginSupport.onLoad(); // Load all plugin support
}

@Override
Expand Down Expand Up @@ -114,7 +115,6 @@ public void onEnable() {
PacketListenerPriority.LOW);
PacketEvents.getAPI().init();


if (PaperLib.isPaper()) {
getLogger().info("Using extra protections provided by the paper api");
Bukkit.getPluginManager().registerEvents(new PaperListener(manager, visualizationManager), this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.crashcraft.crashclaim.menus.list.ClaimListMenu;
import net.crashcraft.crashclaim.permissions.PermissionHelper;
import net.crashcraft.crashclaim.pluginsupport.PluginSupport;
import net.crashcraft.crashclaim.pluginsupport.PluginSupportLoader;
import org.bukkit.Location;
import org.bukkit.entity.Player;

Expand Down Expand Up @@ -166,6 +167,10 @@ public PermissionHelper getPermissionHelper(){
return PermissionHelper.getPermissionHelper();
}

public void registerHook(PluginSupportLoader pluginSupportLoader){
crashClaim.getPluginSupportManager().register(pluginSupportLoader);
}

public void registerHook(PluginSupport pluginSupport){
crashClaim.getPluginSupportManager().register(pluginSupport);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static <T> List<T> getServices(Class<T> clazz, List<T> def) {
final List<T> services = new ArrayList<>();
ServiceLoader.load(clazz).iterator().forEachRemaining(services::add);
if (services.isEmpty()) {
services.addAll(def);
return def;
}
return services;
}
Expand All @@ -46,7 +46,7 @@ public static <T> List<T> getServices(Class<T> clazz, ClassLoader loader, List<T
final List<T> services = new ArrayList<>();
ServiceLoader.load(clazz, loader).iterator().forEachRemaining(services::add);
if (services.isEmpty()) {
services.addAll(def);
return def;
}
return services;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.bukkit.plugin.Plugin;

public interface PluginSupport {
boolean isUnSupportedVersion(String version);
boolean isUnsupportedVersion(String version);

boolean canLoad();

Expand All @@ -28,5 +28,5 @@ default boolean canInteract(Player player, Location location) {

default GroupSettings getPlayerGroupSettings(Player player) {
return null;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;

import java.util.List;
import java.util.Set;

public class PluginSupportDistributor implements PluginSupport {
Expand All @@ -17,7 +16,7 @@ public PluginSupportDistributor(PluginSupportManager manager){
}

@Override
public boolean isUnSupportedVersion(String version) {
public boolean isUnsupportedVersion(String version) {
throw new RuntimeException("Unsupported Operation.");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package net.crashcraft.crashclaim.pluginsupport;

import org.bukkit.plugin.Plugin;

public interface PluginSupportLoader {
boolean isUnsupportedVersion(String version);

boolean canLoad();

String getPluginName();

void load(Plugin plugin);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,16 @@ public PluginSupportManager(CrashClaim crashClaim) {
allSupport = new ArrayList<>();
enabledSupport = new HashSet<>();

for (PluginSupport service : ServiceUtil.getServices(PluginSupport.class)) {
supportDistributor = new PluginSupportDistributor(this);
}

public void onLoad() {
List<PluginSupportLoader> services = ServiceUtil.getServices(PluginSupportLoader.class, crashClaim.getClass().getClassLoader());

logger.info("Found " + services.size() + " plugin support services.");
for (PluginSupportLoader service : services) {
register(service);
}

supportDistributor = new PluginSupportDistributor(this);
}

public void onEnable(){
Expand All @@ -50,6 +55,23 @@ public void onDisable(){
enabledSupport.clear();
}

public void register(PluginSupportLoader pluginSupportLoader){
if (!pluginSupportLoader.canLoad()) {
logger.warning("Plugin support for " + pluginSupportLoader.getPluginName() + " is not enabled.");
return;
}

Plugin plugin = Bukkit.getPluginManager().getPlugin(pluginSupportLoader.getPluginName());
String version = plugin.getDescription().getVersion();

if (pluginSupportLoader.isUnsupportedVersion(version)) {
logger.warning("Plugin [" + pluginSupportLoader.getPluginName() + "] was found but version " + version + ", is not supported.");
return;
}

pluginSupportLoader.load(plugin);
}

public void register(PluginSupport pluginSupport){
if (allSupport.contains(pluginSupport)) {
logger.warning("Plugin support for " + pluginSupport.getPluginName() + " is already registered.");
Expand All @@ -59,13 +81,14 @@ public void register(PluginSupport pluginSupport){
allSupport.add(pluginSupport);

if (!pluginSupport.canLoad()) {
logger.warning("Not enabling plugin support for " + pluginSupport.getPluginName() + ", plugin is not enabled.");
return;
}

Plugin plugin = Bukkit.getPluginManager().getPlugin(pluginSupport.getPluginName());
String version = plugin.getDescription().getVersion();

if (pluginSupport.isUnSupportedVersion(version)) {
if (pluginSupport.isUnsupportedVersion(version)) {
logger.warning("Plugin [" + pluginSupport.getPluginName() + "] was found but version " + version + ", is not supported.");
return;
}
Expand Down Expand Up @@ -95,7 +118,7 @@ public void onPluginEnable(PluginEnableEvent event) {

String version = plugin.getDescription().getVersion();

if (service.isUnSupportedVersion(version)) {
if (service.isUnsupportedVersion(version)) {
logger.warning("Plugin [" + service.getPluginName() + "] was found but version " + version + ", is not supported.");
continue;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package net.crashcraft.crashclaim.pluginsupport.loader;

import com.google.auto.service.AutoService;
import net.crashcraft.crashclaim.CrashClaim;
import net.crashcraft.crashclaim.pluginsupport.PluginSupportLoader;
import net.crashcraft.crashclaim.pluginsupport.plugins.DeluxeSellwandSupport;
import net.crashcraft.crashclaim.pluginsupport.plugins.LuckPermsSupport;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;

@AutoService(PluginSupportLoader.class)
public class DeluxeSellwandSupportLoader implements PluginSupportLoader {

@Override
public boolean isUnsupportedVersion(String version) {
return false;
}

@Override
public boolean canLoad() {
return Bukkit.getPluginManager().getPlugin(getPluginName()) != null;
}

@Override
public String getPluginName() {
return "DeluxeSellwands";
}

@Override
public void load(Plugin plugin) {
CrashClaim.getPlugin().getPluginSupportManager().register(new DeluxeSellwandSupport());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package net.crashcraft.crashclaim.pluginsupport.loader;

import com.google.auto.service.AutoService;
import net.crashcraft.crashclaim.CrashClaim;
import net.crashcraft.crashclaim.pluginsupport.PluginSupportLoader;
import net.crashcraft.crashclaim.pluginsupport.plugins.LuckPermsSupport;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;

@AutoService(PluginSupportLoader.class)
public class LuckPermSupportLoader implements PluginSupportLoader {

@Override
public boolean isUnsupportedVersion(String version) {
return false;
}

@Override
public boolean canLoad() {
return Bukkit.getPluginManager().getPlugin(getPluginName()) != null;
}

@Override
public String getPluginName() {
return "LuckPerms";
}

@Override
public void load(Plugin plugin) {
CrashClaim.getPlugin().getPluginSupportManager().register(new LuckPermsSupport());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package net.crashcraft.crashclaim.pluginsupport.loader;

import com.google.auto.service.AutoService;
import net.crashcraft.crashclaim.CrashClaim;
import net.crashcraft.crashclaim.pluginsupport.PluginSupportLoader;
import net.crashcraft.crashclaim.pluginsupport.plugins.QuickShopHikariSupport;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;

@AutoService(PluginSupportLoader.class)
public class QuickShopHikariSupportLoader implements PluginSupportLoader {
@Override
public boolean isUnsupportedVersion(String version) {
return false;
}

@Override
public boolean canLoad() {
return Bukkit.getPluginManager().getPlugin(getPluginName()) != null;
}

@Override
public String getPluginName() {
return "QuickShop-Hikari";
}

@Override
public void load(Plugin plugin) {
CrashClaim.getPlugin().getPluginSupportManager().register(new QuickShopHikariSupport());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package net.crashcraft.crashclaim.pluginsupport.loader;

import com.google.auto.service.AutoService;
import net.crashcraft.crashclaim.CrashClaim;
import net.crashcraft.crashclaim.pluginsupport.PluginSupportLoader;
import net.crashcraft.crashclaim.pluginsupport.plugins.WorldGuardSupport;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;

@AutoService(PluginSupportLoader.class)
public class WorldGuardSupportLoader implements PluginSupportLoader {
@Override
public boolean isUnsupportedVersion(String version) {
return (int) version.charAt(0) < 7;
}

@Override
public boolean canLoad() {
return Bukkit.getPluginManager().getPlugin(getPluginName()) != null;
}

@Override
public String getPluginName() {
return "WorldGuard";
}

@Override
public void load(Plugin plugin) {
CrashClaim.getPlugin().getPluginSupportManager().register(new WorldGuardSupport());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package net.crashcraft.crashclaim.pluginsupport.plugins;

import com.google.auto.service.AutoService;

import dev.norska.dsw.api.DeluxeSellwandPreSellEvent;
import net.crashcraft.crashclaim.CrashClaim;
import net.crashcraft.crashclaim.localization.Localization;
Expand All @@ -18,19 +16,18 @@
import org.bukkit.inventory.BlockInventoryHolder;
import org.bukkit.plugin.Plugin;

@AutoService(PluginSupport.class)
public class DeluxeSellwandSupport implements PluginSupport, Listener {

private VisualizationManager visual;

@Override
public boolean isUnSupportedVersion(String version) {
public boolean isUnsupportedVersion(String version) {
return false;
}

@Override
public boolean canLoad() {
return Bukkit.getPluginManager().isPluginEnabled(getPluginName());
return Bukkit.getPluginManager().getPlugin(getPluginName()) != null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
package net.crashcraft.crashclaim.pluginsupport.plugins;

import com.google.auto.service.AutoService;
import net.crashcraft.crashclaim.config.GlobalConfig;
import net.crashcraft.crashclaim.config.GroupSettings;
import net.crashcraft.crashclaim.pluginsupport.PluginSupport;
import net.luckperms.api.LuckPerms;
import net.luckperms.api.LuckPermsProvider;
import net.luckperms.api.model.PermissionHolder;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;

@AutoService(PluginSupport.class)
public class LuckPermsSupport implements PluginSupport {
private LuckPerms luckPerms;

@Override
public boolean isUnSupportedVersion(String version) {
public boolean isUnsupportedVersion(String version) {
return false;
}

@Override
public boolean canLoad() {
return Bukkit.getPluginManager().isPluginEnabled(getPluginName());
return Bukkit.getPluginManager().getPlugin(getPluginName()) != null;
}

@Override
Expand Down
Loading

0 comments on commit 47c3fd9

Please sign in to comment.