Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import org.bukkit.permissions.PermissionDefault;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginAwareness;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginLoadOrder;
import org.bukkit.plugin.PluginLoader;
import org.bukkit.plugin.PluginManager;
Expand Down Expand Up @@ -79,13 +78,13 @@ public static interface IPluginDescription {

public void onEnable();

public String getName();
public PluginInfo getInfo();

public String getKey();

public String getFullName();
public String getName();

public PluginDescriptionFile getDescription();
public String getFullName();

public String getPrefix();

Expand Down Expand Up @@ -140,4 +139,5 @@ public static interface IPluginDescription {

@Deprecated
public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args);

}
226 changes: 69 additions & 157 deletions core/src/main/java/com/lemonlightmc/moreplugins/base/PluginBase.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
package com.lemonlightmc.moreplugins.base;

import com.lemonlightmc.moreplugins.config.Config;
import com.lemonlightmc.moreplugins.messages.Logger;
import com.lemonlightmc.moreplugins.messages.MessageProvider;
import com.lemonlightmc.moreplugins.scheduler.Scheduler;
import com.lemonlightmc.moreplugins.utils.ResourceUtils;
import com.lemonlightmc.moreplugins.utils.StringUtils;
import com.lemonlightmc.moreplugins.version.Version;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.util.List;

import org.bukkit.Bukkit;
Expand All @@ -33,7 +24,7 @@
import org.bukkit.plugin.ServicesManager;
import org.bukkit.plugin.java.JavaPluginLoader;

public class PluginBase extends org.bukkit.plugin.PluginBase implements IPluginBase {
public abstract class PluginBase extends org.bukkit.plugin.PluginBase implements IPluginBase {

private boolean isEnabled = false;
private PluginLoader loader = null;
Expand All @@ -45,12 +36,12 @@ public class PluginBase extends org.bukkit.plugin.PluginBase implements IPluginB
private final File dataFolder = null;
private ClassLoader classLoader = null;
private boolean naggable = true;
private Config config = null;
private MessageProvider messageProvider = null;

private static PluginBase instance = null;

public PluginBase() {
super();
classLoader = this.getClass().getClassLoader();
this.server = Bukkit.getServer();
this.scheduler = new Scheduler();
Expand All @@ -67,7 +58,6 @@ protected PluginBase(
this.loader = loader;
this.file = file;
this.info = new PluginInfo(info);
this.config = Config.from(dataFolder.toPath().resolve("config.yml"));
}

public static boolean hasInstance() {
Expand All @@ -83,33 +73,9 @@ public static <I extends PluginBase> I getInstance() {
return (I) instance;
}

public static PluginLoader getInstancePluginLoader() {
return getInstance().loader;
}

public static PluginManager getInstancePluginManager() {
return getInstance().server.getPluginManager();
}

public static Server getInstanceServer() {
return getInstance().server;
}

public static ServicesManager getInstanceServicesManager() {
return getInstance().server.getServicesManager();
}

public static Scheduler getInstanceScheduler() {
return getInstance().scheduler;
}

public static MessageProvider getInstanceMessageProvider() {
return getInstance().messageProvider;
}

@Override
public String getFullName() {
return info.getFullName();
public PluginInfo getInfo() {
return info;
}

@Override
Expand All @@ -118,8 +84,8 @@ public String getKey() {
}

@Override
public PluginDescriptionFile getDescription() {
return info.descriptionFile;
public String getFullName() {
return info.getFullName();
}

@Override
Expand All @@ -132,6 +98,15 @@ public Version getVersion() {
return info.getVersion();
}

@Override
public PluginDescriptionFile getDescription() {
return info.descriptionFile;
}

protected File getFile() {
return file;
}

@Override
public File getDataFolder() {
return dataFolder;
Expand All @@ -145,20 +120,41 @@ public File getDataFile(final String... path) {
}

@Override
public PluginLoader getPluginLoader() {
return loader;
public boolean isEnabled() {
return isEnabled;
}

@Override
public PluginManager getPluginManager() {
return server.getPluginManager();
public void setEnabled(final boolean enabled) {
if (isEnabled != enabled) {
isEnabled = enabled;

if (isEnabled) {
onEnable();
} else {
onDisable();
}
}
}

@Override
public Server getServer() {
return server;
}

@Override
public PluginLoader getPluginLoader() {
return loader;
}

public ClassLoader getClassLoader() {
return classLoader;
}

@Override
public PluginManager getPluginManager() {
return server.getPluginManager();
}

@Override
public ServicesManager getServicesManager() {
return server.getServicesManager();
Expand All @@ -170,144 +166,61 @@ public Scheduler getScheduler() {
}

@Override
public MessageProvider getMessageProvider() {
return messageProvider;
public java.util.logging.Logger getLogger() {
return server.getLogger();
}

@Override
public boolean isEnabled() {
return isEnabled;
}

protected File getFile() {
return file;
public MessageProvider getMessageProvider() {
return messageProvider;
}

@Deprecated
@Override
public FileConfiguration getConfig() {
return config.getConfig();
throw new UnsupportedOperationException(
"FileConfiguration is not supported in PluginBase. Use Configurate instead.");
}

@Override
public void reloadConfig() {
config.reload();
// Configurate.reloadAll();
}

@Override
public void saveConfig() {
config.save();
// Configurate.saveAll();
}

@Override
public void loadConfig() {
config.load();
// Configurate.loadAll();
}

@Override
public void loadConfig(final File file) {
config.load(file);
// Configurate.load(file.getName());
}

@Override
public void saveDefaultConfig() {
config.createDefault();
}

protected Reader getTextResource(final String file) {
final InputStream in = getResource(file);

return in == null
? null
: new InputStreamReader(in, StandardCharsets.UTF_8);
}

@Override
public void saveResource(String resourcePath, final boolean replace) {
if (resourcePath == null || resourcePath.equals("")) {
throw new IllegalArgumentException(
"ResourcePath cannot be null or empty");
}

resourcePath = resourcePath.replace('\\', '/');
final InputStream in = getResource(resourcePath);
if (in == null) {
throw new IllegalArgumentException(
"The embedded resource '" +
resourcePath +
"' cannot be found in " +
file);
}

final File outFile = new File(dataFolder, resourcePath);
final int lastIndex = resourcePath.lastIndexOf('/');
final File outDir = new File(
dataFolder,
resourcePath.substring(0, lastIndex >= 0 ? lastIndex : 0));

if (!outDir.exists()) {
outDir.mkdirs();
}

try {
if (!outFile.exists() || replace) {
final OutputStream out = new FileOutputStream(outFile);
final byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.close();
in.close();
} else {
Logger.warn(
"Could not save " +
outFile.getName() +
" to " +
outFile +
" because " +
outFile.getName() +
" already exists.");
}
} catch (final IOException ex) {
Logger.warn("Could not save " + outFile.getName() + " to " + outFile);
ex.printStackTrace();
}
// Configurate.createDefault();
}

@Deprecated
@Override
public InputStream getResource(final String filename) {
if (filename == null) {
throw new IllegalArgumentException("Filename cannot be null");
}

try {
final URL url = getClassLoader().getResource(filename);
if (url == null) {
return null;
}

final URLConnection connection = url.openConnection();
connection.setUseCaches(false);
return connection.getInputStream();
} catch (final IOException ex) {
return null;
}
return ResourceUtils.getResourceStream(filename);
}

public ClassLoader getClassLoader() {
return classLoader;
}

public void setEnabled(final boolean enabled) {
if (isEnabled != enabled) {
isEnabled = enabled;

if (isEnabled) {
onEnable();
} else {
onDisable();
}
@Deprecated
@Override
public void saveResource(final String path, final boolean replace) {
final File file = ResourceUtils.getResourceFile(path);
if (file == null) {
return;
}
ResourceUtils.saveResource(file, new File(dataFolder, path));
}

@Deprecated
Expand Down Expand Up @@ -357,29 +270,28 @@ public String toString() {
return info.getFullName();
}

@Override
public java.util.logging.Logger getLogger() {
return server.getLogger();
}

@Override
@Deprecated
public List<String> onTabComplete(final CommandSender sender, final Command command, final String label,
final String[] args) {
throw new UnsupportedOperationException("Unimplemented method 'onTabComplete'");
throw new UnsupportedOperationException(
"onTabComplete is not supported in Main Plugin. Create Command with CommandAPI instead!");
}

@Override
@Deprecated
public boolean onCommand(final CommandSender sender, final Command command, final String label, final String[] args) {
throw new UnsupportedOperationException("Unimplemented method 'onCommand'");
throw new UnsupportedOperationException(
"onCommand is not supported in Main Plugin. Create Command with CommandAPI instead!");
}

@Deprecated
@Override
public ChunkGenerator getDefaultWorldGenerator(final String worldName, final String id) {
return null;
}

@Deprecated
@Override
public BiomeProvider getDefaultBiomeProvider(final String worldName, final String id) {
return null;
Expand Down
Loading
Loading