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

More refactoring

This commit is contained in:
TheBusyBiscuit 2020-12-09 12:39:24 +01:00
parent 88c81a9cc2
commit 7d9e726102
6 changed files with 119 additions and 61 deletions

View File

@ -110,6 +110,7 @@ import io.github.thebusybiscuit.slimefun4.implementation.setup.SlimefunItemSetup
import io.github.thebusybiscuit.slimefun4.implementation.tasks.ArmorTask;
import io.github.thebusybiscuit.slimefun4.implementation.tasks.SlimefunStartupTask;
import io.github.thebusybiscuit.slimefun4.implementation.tasks.TickerTask;
import io.github.thebusybiscuit.slimefun4.integrations.IntegrationsManager;
import io.github.thebusybiscuit.slimefun4.utils.tags.SlimefunTag;
import io.papermc.lib.PaperLib;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.abstractItems.AContainer;
@ -662,12 +663,31 @@ public final class SlimefunPlugin extends JavaPlugin implements SlimefunAddon {
return instance.blockDataService;
}
public static PerWorldSettingsService getWorldSettingsService() {
return instance.worldSettingsService;
}
/**
* This method has been renamed.
*
* @deprecated Please use {@link #getIntegrations()} instead.
*
* @return the {@link ThirdPartyPluginService}
*/
@Deprecated
public static ThirdPartyPluginService getThirdPartySupportService() {
return instance.thirdPartySupportService;
}
public static PerWorldSettingsService getWorldSettingsService() {
return instance.worldSettingsService;
/**
* This returns our instance of {@link IntegrationsManager}.
* This is responsible for managing any integrations with third party {@link Plugin plugins}.
*
* @return Our instance of {@link IntegrationsManager}
*/
@Nonnull
public static IntegrationsManager getIntegrations() {
return instance.thirdPartySupportService;
}
/**

View File

@ -23,7 +23,13 @@ import me.minebuilders.clearlag.events.EntityRemoveEvent;
*/
class ClearLagIntegration implements Listener {
private final SlimefunPlugin plugin;
ClearLagIntegration(@Nonnull SlimefunPlugin plugin) {
this.plugin = plugin;
}
public void register() {
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}

View File

@ -1,5 +1,6 @@
package io.github.thebusybiscuit.slimefun4.integrations;
import java.util.function.Consumer;
import java.util.logging.Level;
import javax.annotation.Nonnull;
@ -29,8 +30,16 @@ import me.mrCookieSlime.Slimefun.api.Slimefun;
*/
public class IntegrationsManager {
/**
* This is our instance of {@link SlimefunPlugin}.
*/
protected final SlimefunPlugin plugin;
/**
* This boolean determines whether {@link #start()} was run.
*/
private boolean isEnabled = false;
private boolean isPlaceholderAPIInstalled = false;
private boolean isWorldEditInstalled = false;
private boolean isMcMMOInstalled = false;
@ -47,72 +56,52 @@ public class IntegrationsManager {
this.plugin = plugin;
}
/**
* This method returns whether the {@link IntegrationsManager} was enabled yet.
*
* @return Whether this {@link IntegrationsManager} has been enabled already.
*/
public boolean isEnabled() {
return isEnabled;
}
/**
* 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);
}
if (isEnabled) {
// Prevent double-registration
throw new UnsupportedOperationException("All integrations have already been loaded.");
} else {
isEnabled = true;
}
// PlaceholderAPI hook to provide playerholders from Slimefun.
load("PlaceholderAPI", integration -> {
new PlaceholderAPIIntegration(plugin).register();
isPlaceholderAPIInstalled = true;
});
// 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);
}
}
load("WorldEdit", integration -> {
new WorldEditIntegration().register();
isWorldEditInstalled = true;
});
// 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);
}
}
load("mcMMO", integration -> {
new McMMOIntegration(plugin).register();
isMcMMOInstalled = true;
});
// 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);
}
}
load("ClearLag", integration -> {
new ClearLagIntegration(plugin).register();
isClearLagInstalled = true;
});
// ItemsAdder Integration (custom blocks)
load("ItemsAdder", integration -> isItemsAdderInstalled = true);
}
protected boolean isPluginInstalled(@Nonnull String hook) {
@ -124,6 +113,32 @@ public class IntegrationsManager {
}
}
/**
* This method loads an integration with a {@link Plugin} of the specified name.
* If that {@link Plugin} is installed and enabled, the provided callback will be run.
*
* @param pluginName
* The name of this {@link Plugin}
* @param consumer
* The callback to run if that {@link Plugin} is installed and enabled
*/
protected void load(@Nonnull String pluginName, @Nonnull Consumer<Plugin> consumer) {
Plugin integration = plugin.getServer().getPluginManager().getPlugin(pluginName);
if (integration != null && integration.isEnabled()) {
String version = integration.getDescription().getVersion();
Slimefun.getLogger().log(Level.INFO, "Hooked into Plugin: {0} v{1}", new Object[] { pluginName, version });
try {
// Run our callback
consumer.accept(integration);
} catch (Exception | LinkageError x) {
Slimefun.getLogger().log(Level.WARNING, "Maybe consider updating {0} or Slimefun?", pluginName);
Slimefun.getLogger().log(Level.WARNING, x, () -> "Failed to hook into " + pluginName + " v" + version);
}
}
}
/**
* This checks if one of our third party integrations faked an {@link Event}.
* Faked {@link Event Events} should be ignored in our logic.
@ -147,6 +162,7 @@ public class IntegrationsManager {
*
* @return Whether a different custom {@link Block} exists at that {@link Location}
*/
@SuppressWarnings("deprecation")
public boolean isCustomBlock(@Nonnull Block block) {
return isItemsAdderInstalled && ItemsAdder.isCustomBlock(block);
}

View File

@ -23,7 +23,13 @@ import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
*/
class McMMOIntegration implements Listener {
private final SlimefunPlugin plugin;
McMMOIntegration(@Nonnull SlimefunPlugin plugin) {
this.plugin = plugin;
}
public void register() {
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}

View File

@ -24,6 +24,16 @@ import me.mrCookieSlime.Slimefun.api.BlockStorage;
class WorldEditIntegration {
WorldEditIntegration() {
try {
// This ensures that we are using a version which supports Extents
Class.forName("com.sk89q.worldedit.extent.Extent");
} catch (ClassNotFoundException e) {
// Re-throw the exception for the IntegrationsManager to catch
throw new IllegalStateException(e.getMessage());
}
}
public void register() {
WorldEdit.getInstance().getEventBus().register(this);
}

View File

@ -1,10 +1,12 @@
name: Slimefun
version: ${project.version}
author: The Slimefun 4 Community
description: Slimefun basically turns your entire Server into a FTB modpack without installing a single mod
website: https://github.com/Slimefun
description: Slimefun basically turns your entire Server into a FTB modpack without installing a single mod
main: io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin
api-version: '1.14'
softdepend:
- CS-CoreLib
@ -14,12 +16,10 @@ softdepend:
- mcMMO
- ItemsAdder
api-version: '1.14'
commands:
slimefun:
description: Slimefun command
aliases: sf
description: Slimefun base command
usage: You either forgot to install CS-CoreLib or you installed an unsupported version.
permissions: