Add injectable wrapper for plugin data folder.

This commit is contained in:
Jeremy Wood 2023-03-07 16:49:14 -05:00
parent 6362a97aaf
commit a57a83a047
No known key found for this signature in database
GPG Key ID: C5BAD04C77B91B4B
3 changed files with 40 additions and 0 deletions

View File

@ -1,5 +1,6 @@
package com.onarandombox.MultiverseCore.inject.binder;
import com.onarandombox.MultiverseCore.inject.wrapper.PluginDataFolder;
import org.bukkit.plugin.Plugin;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
import org.glassfish.hk2.utilities.binding.ScopedBindingBuilder;
@ -31,6 +32,7 @@ public abstract class PluginBinder<T extends Plugin> extends AbstractBinder {
var bindingBuilder = bindPlugin(getPlugin());
bindingBuilder.to(Plugin.class);
bind(plugin.getLogger()).to(Logger.class);
bind(PluginDataFolder.from(plugin)).to(PluginDataFolder.class);
}
private ScopedBindingBuilder<T> bindPlugin(T plugin) {

View File

@ -0,0 +1,25 @@
package com.onarandombox.MultiverseCore.inject.wrapper;
import org.bukkit.plugin.Plugin;
import java.io.File;
/**
* An extension of {@link File} that represents the data folder of a plugin.
*/
public class PluginDataFolder extends File {
/**
* Creates a new {@link PluginDataFolder} from the given plugin.
*
* @param plugin The plugin.
* @return The plugin data folder.
*/
public static PluginDataFolder from(Plugin plugin) {
return new PluginDataFolder(plugin.getDataFolder());
}
private PluginDataFolder(File dataFolder) {
super(dataFolder.getParentFile(), dataFolder.getName());
}
}

View File

@ -11,6 +11,7 @@ import com.onarandombox.MultiverseCore.api.SafeTTeleporter
import com.onarandombox.MultiverseCore.commandtools.MVCommandManager
import com.onarandombox.MultiverseCore.config.MVCoreConfigProvider
import com.onarandombox.MultiverseCore.economy.MVEconomist
import com.onarandombox.MultiverseCore.inject.wrapper.PluginDataFolder
import com.onarandombox.MultiverseCore.listeners.MVChatListener
import com.onarandombox.MultiverseCore.listeners.MVEntityListener
import com.onarandombox.MultiverseCore.listeners.MVPlayerListener
@ -28,8 +29,10 @@ import org.bukkit.Server
import org.bukkit.plugin.PluginManager
import org.junit.jupiter.api.Test
import org.mvplugins.multiverse.core.TestWithMockBukkit
import java.io.File
import java.util.logging.Logger
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import kotlin.test.assertSame
class InjectionTest : TestWithMockBukkit() {
@ -61,6 +64,16 @@ class InjectionTest : TestWithMockBukkit() {
assertNotNull(multiverseCore.getService(Logger::class.java));
}
@Test
fun `PluginDataFolder is available as a service`() {
assertNotNull(multiverseCore.getService(PluginDataFolder::class.java));
}
@Test
fun `File is not available as a service`() {
assertNull(multiverseCore.getService(File::class.java))
}
@Test
fun `AnchorManager is available as a service`() {
assertNotNull(multiverseCore.getService(AnchorManager::class.java))