1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-20 03:35:51 +00:00

Refactoring for third-party integrations

This commit is contained in:
TheBusyBiscuit 2020-12-08 21:07:48 +01:00
parent f0b78dd356
commit 2e13cd2e88
8 changed files with 183 additions and 99 deletions

View File

@ -2,21 +2,17 @@ package io.github.thebusybiscuit.slimefun4.core.services.plugins;
import java.util.Optional; import java.util.Optional;
import java.util.function.Function; import java.util.function.Function;
import java.util.logging.Level;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault; import javax.annotation.ParametersAreNonnullByDefault;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.event.Event;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import com.gmail.nossr50.events.fake.FakeBlockBreakEvent;
import io.github.thebusybiscuit.slimefun4.api.SlimefunAddon; import io.github.thebusybiscuit.slimefun4.api.SlimefunAddon;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
import me.mrCookieSlime.Slimefun.api.Slimefun; import io.github.thebusybiscuit.slimefun4.integrations.IntegrationsManager;
/** /**
* This Service holds all interactions and hooks with third-party {@link Plugin Plugins} * This Service holds all interactions and hooks with third-party {@link Plugin Plugins}
@ -29,20 +25,16 @@ import me.mrCookieSlime.Slimefun.api.Slimefun;
* @see SlimefunPlugin * @see SlimefunPlugin
* *
*/ */
public class ThirdPartyPluginService { public class ThirdPartyPluginService extends IntegrationsManager {
private final SlimefunPlugin plugin;
private boolean initialized = false;
private boolean isExoticGardenInstalled = false;
private boolean isChestTerminalInstalled = false;
private boolean isMcMMOInstalled = false;
/** /**
* This gets overridden if ExoticGarden is loaded * This gets overridden if ExoticGarden is loaded
*/ */
private Function<Block, Optional<ItemStack>> exoticGardenIntegration = b -> Optional.empty(); private Function<Block, Optional<ItemStack>> exoticGardenIntegration = b -> Optional.empty();
private boolean isChestTerminalInstalled = false;
private boolean isExoticGardenInstalled = false;
/** /**
* This initializes the {@link ThirdPartyPluginService} * This initializes the {@link ThirdPartyPluginService}
* *
@ -50,77 +42,14 @@ public class ThirdPartyPluginService {
* Our instance of {@link SlimefunPlugin} * Our instance of {@link SlimefunPlugin}
*/ */
public ThirdPartyPluginService(@Nonnull SlimefunPlugin plugin) { public ThirdPartyPluginService(@Nonnull SlimefunPlugin plugin) {
this.plugin = plugin; super(plugin);
} }
/** @Override
* This method initializes all third party integrations.
*/
public void start() { public void start() {
if (initialized) { super.start();
throw new UnsupportedOperationException("Third Party Integrations have already been initialized!");
}
initialized = true; plugin.getServer().getScheduler().runTask(plugin, () -> isChestTerminalInstalled = isPluginInstalled("ChestTerminal"));
if (isPluginInstalled("PlaceholderAPI")) {
try {
PlaceholderAPIIntegration hook = new PlaceholderAPIIntegration(plugin);
hook.register();
} catch (Exception | LinkageError x) {
String version = plugin.getServer().getPluginManager().getPlugin("PlaceholderAPI").getDescription().getVersion();
Slimefun.getLogger().log(Level.WARNING, "Maybe consider updating PlaceholderAPI or Slimefun?");
Slimefun.getLogger().log(Level.WARNING, x, () -> "Failed to hook into PlaceholderAPI v" + version);
}
}
// WorldEdit Hook to clear Slimefun Data upon //set 0 //cut or any other equivalent
if (isPluginInstalled("WorldEdit")) {
try {
Class.forName("com.sk89q.worldedit.extent.Extent");
new WorldEditIntegration();
} catch (Exception | LinkageError x) {
String version = plugin.getServer().getPluginManager().getPlugin("WorldEdit").getDescription().getVersion();
Slimefun.getLogger().log(Level.WARNING, "Maybe consider updating WorldEdit or Slimefun?");
Slimefun.getLogger().log(Level.WARNING, x, () -> "Failed to hook into WorldEdit v" + version);
}
}
// mcMMO Integration
if (isPluginInstalled("mcMMO")) {
try {
new McMMOIntegration(plugin);
isMcMMOInstalled = true;
} catch (Exception | LinkageError x) {
String version = plugin.getServer().getPluginManager().getPlugin("mcMMO").getDescription().getVersion();
Slimefun.getLogger().log(Level.WARNING, "Maybe consider updating mcMMO or Slimefun?");
Slimefun.getLogger().log(Level.WARNING, x, () -> "Failed to hook into mcMMO v" + version);
}
}
/*
* These Items are not marked as soft-dependencies and
* therefore need to be loaded after the Server has finished
* loading all plugins
*/
plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, () -> {
if (isPluginInstalled("ClearLag")) {
new ClearLagIntegration(plugin);
}
isChestTerminalInstalled = isPluginInstalled("ChestTerminal");
});
}
private boolean isPluginInstalled(@Nonnull String hook) {
if (plugin.getServer().getPluginManager().isPluginEnabled(hook)) {
Slimefun.getLogger().log(Level.INFO, "Hooked into Plugin: {0}", hook);
return true;
} else {
return false;
}
} }
@ParametersAreNonnullByDefault @ParametersAreNonnullByDefault
@ -143,18 +72,4 @@ public class ThirdPartyPluginService {
return exoticGardenIntegration.apply(block); return exoticGardenIntegration.apply(block);
} }
/**
* This checks if one of our third party integrations faked an {@link Event}.
* Faked {@link Event Events} should be ignored in our logic.
*
* @param event
* The {@link Event} to test
*
* @return Whether this is a fake event
*/
public boolean isEventFaked(@Nonnull Event event) {
// This can be changed to "FakeEvent" in a later version
return isMcMMOInstalled && event instanceof FakeBlockBreakEvent;
}
} }

View File

@ -1,4 +1,4 @@
package io.github.thebusybiscuit.slimefun4.core.services.plugins; package io.github.thebusybiscuit.slimefun4.integrations;
import java.util.Iterator; import java.util.Iterator;

View File

@ -0,0 +1,158 @@
package io.github.thebusybiscuit.slimefun4.integrations;
import java.util.logging.Level;
import javax.annotation.Nonnull;
import org.bukkit.event.Event;
import org.bukkit.plugin.Plugin;
import com.gmail.nossr50.events.fake.FakeBlockBreakEvent;
import io.github.thebusybiscuit.slimefun4.api.SlimefunAddon;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
import me.mrCookieSlime.Slimefun.api.Slimefun;
/**
* This Service holds all interactions and hooks with third-party {@link Plugin Plugins}
* that are not necessarily a dependency or a {@link SlimefunAddon}.
*
* Integration with these plugins happens inside Slimefun itself.
*
* @author TheBusyBiscuit
*
* @see SlimefunPlugin
*
*/
public class IntegrationsManager {
protected final SlimefunPlugin plugin;
private boolean isPlaceholderAPIInstalled = false;
private boolean isWorldEditInstalled = false;
private boolean isMcMMOInstalled = false;
private boolean isClearLagInstalled = false;
private boolean isItemsAdderInstalled = false;
/**
* This initializes the {@link IntegrationsManager}
*
* @param plugin
* Our instance of {@link SlimefunPlugin}
*/
public IntegrationsManager(@Nonnull SlimefunPlugin plugin) {
this.plugin = plugin;
}
/**
* This method initializes all third party integrations.
*/
public void start() {
// PlaceholderAPI hook to provide playerholders from Slimefun.
if (isPluginInstalled("PlaceholderAPI")) {
try {
PlaceholderAPIIntegration hook = new PlaceholderAPIIntegration(plugin);
hook.register();
isPlaceholderAPIInstalled = true;
} catch (Exception | LinkageError x) {
String version = plugin.getServer().getPluginManager().getPlugin("PlaceholderAPI").getDescription().getVersion();
Slimefun.getLogger().log(Level.WARNING, "Maybe consider updating PlaceholderAPI or Slimefun?");
Slimefun.getLogger().log(Level.WARNING, x, () -> "Failed to hook into PlaceholderAPI v" + version);
}
}
// WorldEdit Hook to clear Slimefun Data upon //set 0 //cut or any other equivalent
if (isPluginInstalled("WorldEdit")) {
try {
Class.forName("com.sk89q.worldedit.extent.Extent");
new WorldEditIntegration();
isWorldEditInstalled = true;
} catch (Exception | LinkageError x) {
String version = plugin.getServer().getPluginManager().getPlugin("WorldEdit").getDescription().getVersion();
Slimefun.getLogger().log(Level.WARNING, "Maybe consider updating WorldEdit or Slimefun?");
Slimefun.getLogger().log(Level.WARNING, x, () -> "Failed to hook into WorldEdit v" + version);
}
}
// mcMMO Integration
if (isPluginInstalled("mcMMO")) {
try {
new McMMOIntegration(plugin);
isMcMMOInstalled = true;
} catch (Exception | LinkageError x) {
String version = plugin.getServer().getPluginManager().getPlugin("mcMMO").getDescription().getVersion();
Slimefun.getLogger().log(Level.WARNING, "Maybe consider updating mcMMO or Slimefun?");
Slimefun.getLogger().log(Level.WARNING, x, () -> "Failed to hook into mcMMO v" + version);
}
}
// ItemsAdder Integration
if (isPluginInstalled("ItemsAdder")) {
try {
isItemsAdderInstalled = true;
} catch (Exception | LinkageError x) {
String version = plugin.getServer().getPluginManager().getPlugin("ItemsAdder").getDescription().getVersion();
Slimefun.getLogger().log(Level.WARNING, "Maybe consider updating ItemsAdder or Slimefun?");
Slimefun.getLogger().log(Level.WARNING, x, () -> "Failed to hook into ItemsAdder v" + version);
}
}
// ClearLag integration (to prevent display items from getting deleted)
if (isPluginInstalled("ClearLag")) {
try {
new ClearLagIntegration(plugin);
isClearLagInstalled = true;
} catch (Exception | LinkageError x) {
String version = plugin.getServer().getPluginManager().getPlugin("ClearLag").getDescription().getVersion();
Slimefun.getLogger().log(Level.WARNING, "Maybe consider updating ClearLag or Slimefun?");
Slimefun.getLogger().log(Level.WARNING, x, () -> "Failed to hook into ClearLag v" + version);
}
}
}
protected boolean isPluginInstalled(@Nonnull String hook) {
if (plugin.getServer().getPluginManager().isPluginEnabled(hook)) {
Slimefun.getLogger().log(Level.INFO, "Hooked into Plugin: {0}", hook);
return true;
} else {
return false;
}
}
/**
* This checks if one of our third party integrations faked an {@link Event}.
* Faked {@link Event Events} should be ignored in our logic.
*
* @param event
* The {@link Event} to test
*
* @return Whether this is a fake event
*/
public boolean isEventFaked(@Nonnull Event event) {
// This can be changed to "FakeEvent" in a later version
return isMcMMOInstalled && event instanceof FakeBlockBreakEvent;
}
public boolean isPlaceholderAPIInstalled() {
return isPlaceholderAPIInstalled;
}
public boolean isWorldEditInstalled() {
return isWorldEditInstalled;
}
public boolean isMcMMOInstalled() {
return isMcMMOInstalled;
}
public boolean isClearLagInstalled() {
return isClearLagInstalled;
}
public boolean isItemsAdderInstalled() {
return isItemsAdderInstalled;
}
}

View File

@ -1,4 +1,4 @@
package io.github.thebusybiscuit.slimefun4.core.services.plugins; package io.github.thebusybiscuit.slimefun4.integrations;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;

View File

@ -1,4 +1,4 @@
package io.github.thebusybiscuit.slimefun4.core.services.plugins; package io.github.thebusybiscuit.slimefun4.integrations;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;

View File

@ -1,4 +1,4 @@
package io.github.thebusybiscuit.slimefun4.core.services.plugins; package io.github.thebusybiscuit.slimefun4.integrations;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;

View File

@ -0,0 +1,4 @@
/**
* This package holds classes which are related to integrations between Slimefun and Third-Party plugins.
*/
package io.github.thebusybiscuit.slimefun4.integrations;

View File

@ -5,7 +5,14 @@ description: Slimefun basically turns your entire Server into a FTB modpack with
website: https://github.com/Slimefun website: https://github.com/Slimefun
main: io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin main: io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin
softdepend: [CS-CoreLib, PlaceholderAPI, WorldEdit, EmeraldEnchants, mcMMO]
softdepend:
- CS-CoreLib
- PlaceholderAPI
- WorldEdit
- ClearLag
- mcMMO
- ItemsAdder
api-version: '1.14' api-version: '1.14'