refactor plugin hooks to use a central manager, add item support for stackers

This commit is contained in:
jascotty2 2019-08-26 13:05:49 -05:00
parent 45df3c9c4c
commit f909140b04
18 changed files with 529 additions and 424 deletions

30
pom.xml
View File

@ -21,19 +21,21 @@
<repositories>
<repository>
<id>private</id>
<url>http://68.183.129.248/artifactory/private/</url>
</repository>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
<url>https://repo.songoda.com/artifactory/private/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.14.4-R0.1-SNAPSHOT</version>
<groupId>com.destroystokyo.papermc</groupId>
<artifactId>paper</artifactId>
<version>1.14.4</version>
<scope>provided</scope>
</dependency>
<!--dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot</artifactId>
<version>1.14.4</version>
</dependency-->
<dependency>
<groupId>com.gmail.filoghost.holographicdisplays</groupId>
<artifactId>holographicdisplays-api</artifactId>
@ -52,7 +54,7 @@
</dependency>
<dependency>
<groupId>org.black_ixx</groupId>
<artifactId>playerpoints</artifactId>
<artifactId>PlayerPoints</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
@ -89,13 +91,19 @@
<dependency>
<groupId>com.sk89q.worldguard</groupId>
<artifactId>worldguard-bukkit</artifactId>
<version>7.0.1-SNAPSHOT</version>
<version>7.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sk89q.worldedit</groupId>
<artifactId>worldedit-bukkit</artifactId>
<version>7.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.songoda</groupId>
<artifactId>UltimateStacker</artifactId>
<version>1.2.8</version>
<version>1.9.6</version>
</dependency>
<dependency>
<groupId>com.bgsoftware</groupId>

View File

@ -1,89 +1,25 @@
package com.songoda.core.hooks;
import com.songoda.core.hooks.economies.Economy;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.plugin.PluginManager;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/**
* A convenience class for static access to an Economy HookManager
*/
public class EconomyManager {
private final static Map<EconomyType, Economy> registeredEconomies = new HashMap<>();
private static Economy defaultEcon = null;
private static final HookManager<Economy> manager = new HookManager(Economy.class);
/**
* Load all supported economy plugins. <br />
* Note: This method should be called in your plugin's onEnable() section
*/
public static void load() {
if (!registeredEconomies.isEmpty()) return;
PluginManager pluginManager = Bukkit.getPluginManager();
for (EconomyType type : EconomyType.values()) {
if (pluginManager.isPluginEnabled(type.plugin)) {
Economy econ = type.getInstance();
if(econ.isEnabled()) {
registeredEconomies.put(type, econ);
if (defaultEcon == null)
defaultEcon = econ;
}
}
}
manager.load();
}
/**
* Set the default economy to a different plugin, if that plugin exists.
* If the plugin is not loaded or supported, the previously defined default will be used. <br />
* NOTE: using a default economy assumes that this library is shaded
*
* @param name name of the plugin to use
*/
public static void setPreferredEconomy(String name) {
Economy econ = getEconomy(name);
if (econ != null)
defaultEcon = econ;
}
/**
* Set the default economy to a different plugin, if that plugin exists.
* If the plugin is not loaded or supported, the previously defined default will be used. <br />
* NOTE: using a default economy assumes that this library is shaded
*
* @param economy plugin to use
*/
public static void setPreferredEconomy(EconomyType economy) {
Economy econ = getEconomy(economy);
if (econ != null)
defaultEcon = econ;
}
/**
* Try to grab the handler for this specific economy plugin.
*
* @param name plugin to use
* @return returns null if plugin is not enabled
*/
public static Economy getEconomy(String name) {
if (name == null) return null;
final String plugin = name.trim();
return registeredEconomies.get(registeredEconomies.keySet().stream()
.filter(type -> type.plugin.equalsIgnoreCase(plugin))
.findFirst().orElse(null));
}
/**
* Try to grab the handler for this specific economy plugin.
*
* @param economy plugin to use
* @return returns null if plugin is not enabled
*/
public static Economy getEconomy(EconomyType economy) {
return registeredEconomies.get(economy);
public static HookManager getManager() {
return manager;
}
/**
@ -93,36 +29,7 @@ public class EconomyManager {
* @return returns null if no plugin enabled
*/
public static Economy getEconomy() {
return defaultEcon;
}
/**
* Grab a list of all supported economy plugins.
*
* @return an immutable collection of the loaded economy handler instances
*/
public static Collection<Economy> getRegisteredEconomies() {
return Collections.unmodifiableCollection(registeredEconomies.values());
}
/**
* Check to see if a specific economy plugin is enabled.
*
* @param name plugin to check
* @return true if this economy plugin is supported and loaded
*/
public static boolean isEnabled(String name) {
return getEconomy(name) != null;
}
/**
* Check to see if a specific economy plugin is enabled.
*
* @param economy plugin to check
* @return true if this economy plugin is supported and loaded
*/
public static boolean isEnabled(EconomyType economy) {
return registeredEconomies.containsKey(economy);
return manager.getCurrentHook();
}
/**
@ -132,7 +39,7 @@ public class EconomyManager {
* @return returns false if there are no supported economy plugins
*/
public static boolean isEnabled() {
return defaultEcon != null;
return manager.isEnabled();
}
/**
@ -142,7 +49,7 @@ public class EconomyManager {
* @return
*/
public static String getName() {
return defaultEcon != null ? defaultEcon.getName() : null;
return manager.getName();
}
/**
@ -154,7 +61,7 @@ public class EconomyManager {
* @return true if this player can have this amount withdrawn
*/
public static boolean hasBalance(OfflinePlayer player, double cost) {
return defaultEcon != null && defaultEcon.hasBalance(player, cost);
return manager.isEnabled() && manager.getCurrentHook().hasBalance(player, cost);
}
/**
@ -166,7 +73,7 @@ public class EconomyManager {
* @return true if the total amount was withdrawn successfully
*/
public static boolean withdrawBalance(OfflinePlayer player, double cost) {
return defaultEcon != null && defaultEcon.withdrawBalance(player, cost);
return manager.isEnabled() && manager.getCurrentHook().withdrawBalance(player, cost);
}
/**
@ -178,6 +85,6 @@ public class EconomyManager {
* @return true if the total amount was added successfully
*/
public static boolean deposit(OfflinePlayer player, double amount) {
return defaultEcon != null && defaultEcon.deposit(player, amount);
return manager.isEnabled() && manager.getCurrentHook().deposit(player, amount);
}
}

View File

@ -1,32 +0,0 @@
package com.songoda.core.hooks;
import com.songoda.core.hooks.economies.Economy;
import com.songoda.core.hooks.economies.VaultEconomy;
import com.songoda.core.hooks.economies.ReserveEconomy;
import com.songoda.core.hooks.economies.PlayerPointsEconomy;
import java.util.logging.Level;
import org.bukkit.Bukkit;
public enum EconomyType {
VAULT("Vault", VaultEconomy.class),
PLAYER_POINTS("Reserve", ReserveEconomy.class),
RESERVE("PlayerPoints", PlayerPointsEconomy.class);
public final String plugin;
protected final Class managerClass;
private EconomyType(String plugin, Class managerClass) {
this.plugin = plugin;
this.managerClass = managerClass;
}
protected Economy getInstance() {
try {
return (Economy) managerClass.newInstance();
} catch (InstantiationException | IllegalAccessException ex) {
Bukkit.getLogger().log(Level.SEVERE, "Unexpected Error while creating a new Economy Manager for " + name(), ex);
}
return null;
}
}

View File

@ -1,86 +1,25 @@
package com.songoda.core.hooks;
import com.songoda.core.hooks.EntityStackerType;
import com.songoda.core.hooks.stackers.Stacker;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.LivingEntity;
import org.bukkit.plugin.PluginManager;
import java.util.*;
/**
* A convenience class for static access to a Stacker HookManager
*/
public class EntityStackerManager {
private final static Map<EntityStackerType, Stacker> registeredStackers = new HashMap<>();
private static Stacker defaultStacker = null;
private static final HookManager<Stacker> manager = new HookManager(Stacker.class);
/**
* Load all supported Stacker plugins. <br />
* Load all supported economy plugins. <br />
* Note: This method should be called in your plugin's onEnable() section
*/
public static void load() {
if (!registeredStackers.isEmpty()) return;
PluginManager pluginManager = Bukkit.getPluginManager();
for (EntityStackerType type : EntityStackerType.values()) {
if (pluginManager.isPluginEnabled(type.plugin)) {
Stacker stacker = type.getInstance();
registeredStackers.put(type, stacker);
if (defaultStacker == null)
defaultStacker = stacker;
}
}
manager.load();
}
/**
* Set the default stacker to a different plugin, if that plugin exists.
* If the plugin is not loaded or supported, the previously defined default will be used. <br />
* NOTE: using a default stacker assumes that this library is shaded
*
* @param type stacker plugin to use
*/
public static void setPreferredStackerPlugin(EntityStackerType type) {
Stacker stacker = getStacker(type);
if (stacker != null)
defaultStacker = stacker;
}
/**
* Set the default stacker to a different plugin, if that plugin exists.
* If the plugin is not loaded or supported, the previously defined default will be used. <br />
* NOTE: using a default stacker assumes that this library is shaded
*
* @param name name of the plugin to use
*/
public static void setPreferredStackerPlugin(String name) {
Stacker stacker = getStacker(name);
if (stacker != null)
defaultStacker = stacker;
}
/**
* Try to grab the handler for this specific stacker plugin.
*
* @param name plugin to useH
* @return returns null if plugin is not enabled
*/
public static Stacker getStacker(String name) {
if (name == null) return null;
final String plugin = name.trim();
return registeredStackers.get(registeredStackers.keySet().stream()
.filter(type -> type.plugin.equalsIgnoreCase(plugin))
.findFirst().orElse(null));
}
/**
* Try to grab the handler for this specific stacker plugin.
*
* @param stacker plugin to use
* @return returns null if plugin is not enabled
*/
public static Stacker getStacker(EntityStackerType stacker) {
return registeredStackers.get(stacker);
public static HookManager getManager() {
return manager;
}
/**
@ -90,43 +29,32 @@ public class EntityStackerManager {
* @return returns null if no plugin enabled
*/
public static Stacker getStacker() {
return defaultStacker;
return manager.getCurrentHook();
}
/**
* Grab a list of all supported stacker plugins that are loaded.
*
* @return an immutable collection of the loaded stacker, handler instances
*/
public static Collection<Stacker> getRegisteredStackers() {
return Collections.unmodifiableCollection(registeredStackers.values());
}
public static boolean isStacked(LivingEntity entity) {
return defaultStacker != null && defaultStacker.isStacked(entity);
return manager.isEnabled() && manager.getCurrentHook().isStacked(entity);
}
public int getSize(LivingEntity entity) {
return defaultStacker != null ? defaultStacker.getSize(entity) : 1;
public static int getSize(LivingEntity entity) {
return manager.isEnabled() ? manager.getCurrentHook().getSize(entity) : 1;
}
public void removeOne(LivingEntity entity) {
public static void removeOne(LivingEntity entity) {
remove(entity, 1);
}
public void remove(LivingEntity entity, int amount) {
if (defaultStacker != null)
defaultStacker.remove(entity, amount);
public static void remove(LivingEntity entity, int amount) {
if (manager.isEnabled())
manager.getCurrentHook().remove(entity, amount);
}
public void addOne(LivingEntity entity) {
public static void addOne(LivingEntity entity) {
add(entity, 1);
}
public void add(LivingEntity entity, int amount) {
if (defaultStacker != null)
defaultStacker.add(entity, amount);
public static void add(LivingEntity entity, int amount) {
if (manager.isEnabled())
manager.getCurrentHook().add(entity, amount);
}
}

View File

@ -1,33 +0,0 @@
package com.songoda.core.hooks;
import com.songoda.core.hooks.stackers.StackMob;
import com.songoda.core.hooks.stackers.Stacker;
import com.songoda.core.hooks.stackers.UltimateStacker;
import com.songoda.core.hooks.stackers.WildStacker;
import org.bukkit.Bukkit;
import java.util.logging.Level;
public enum EntityStackerType {
ULTIMATE_STACKER("UltimateStacker", UltimateStacker.class),
WILD_STACKER("WildStacker", WildStacker.class),
STACK_MOB("StackMob", StackMob.class);
public final String plugin;
protected final Class managerClass;
private EntityStackerType(String plugin, Class managerClass) {
this.plugin = plugin;
this.managerClass = managerClass;
}
protected Stacker getInstance() {
try {
return (Stacker) managerClass.newInstance();
} catch (InstantiationException | IllegalAccessException ex) {
Bukkit.getLogger().log(Level.SEVERE, "Unexpected Error while creating a new Stacker Manager for " + name(), ex);
}
return null;
}
}

View File

@ -1,91 +1,26 @@
package com.songoda.core.hooks;
import com.songoda.core.hooks.holograms.Holograms;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* A convenience class for static access to a Holograms HookManager
*/
public class HologramManager {
private final static Map<HologramType, Holograms> registeredHolograms = new HashMap<>();
private static Holograms defaultHolo = null;
private static final HookManager<Holograms> manager = new HookManager(Holograms.class);
/**
* Load all supported hologram plugins. <br />
* Load all supported economy plugins. <br />
* Note: This method should be called in your plugin's onEnable() section
*
* @param javaPlugin the plugin owning the holograms
*/
public static void load(JavaPlugin javaPlugin) {
if (!registeredHolograms.isEmpty()) return;
PluginManager pluginManager = Bukkit.getPluginManager();
for (HologramType type : HologramType.values()) {
if (pluginManager.isPluginEnabled(type.plugin)) {
Holograms holo = type.getInstance(javaPlugin);
registeredHolograms.put(type, holo);
if (defaultHolo == null)
defaultHolo = holo;
}
}
public static void load() {
manager.load();
}
/**
* Set the default hologram to a different plugin, if that plugin exists.
* If the plugin is not loaded or supported, the previously defined default will be used. <br />
* NOTE: using a default hologram assumes that this library is shaded
*
* @param type hologram plugin to use
*/
public static void setPreferredHologramPlugin(HologramType type) {
Holograms holo = getHolograms(type);
if (holo != null)
defaultHolo = holo;
}
/**
* Set the default hologram to a different plugin, if that plugin exists.
* If the plugin is not loaded or supported, the previously defined default will be used. <br />
* NOTE: using a default hologram assumes that this library is shaded
*
* @param name name of the plugin to use
*/
public static void setPreferredHologramPlugin(String name) {
Holograms holo = getHolograms(name);
if (holo != null)
defaultHolo = holo;
}
/**
* Try to grab the handler for this specific hologram plugin.
*
* @param name plugin to useH
* @return returns null if plugin is not enabled
*/
public static Holograms getHolograms(String name) {
if (name == null) return null;
final String plugin = name.trim();
return registeredHolograms.get(registeredHolograms.keySet().stream()
.filter(type -> type.plugin.equalsIgnoreCase(plugin))
.findFirst().orElse(null));
}
/**
* Try to grab the handler for this specific hologram plugin.
*
* @param hologram plugin to use
* @return returns null if plugin is not enabled
*/
public static Holograms getHolograms(HologramType hologram) {
return registeredHolograms.get(hologram);
public static HookManager getManager() {
return manager;
}
/**
@ -95,45 +30,36 @@ public class HologramManager {
* @return returns null if no plugin enabled
*/
public static Holograms getHolograms() {
return defaultHolo;
}
/**
* Grab a list of all supported hologram plugins that are loaded.
*
* @return an immutable collection of the loaded hologram, handler instances
*/
public static Collection<Holograms> getRegisteredHolograms() {
return Collections.unmodifiableCollection(registeredHolograms.values());
return manager.getCurrentHook();
}
public static void createHologram(Location location, String line) {
if (defaultHolo != null)
defaultHolo.createHologram(location, line);
if (manager.isEnabled())
manager.getCurrentHook().createHologram(location, line);
}
public static void createHologram(Location location, List<String> lines) {
if (defaultHolo != null)
defaultHolo.createHologram(location, lines);
if (manager.isEnabled())
manager.getCurrentHook().createHologram(location, lines);
}
public static void removeHologram(Location location) {
if (defaultHolo != null)
defaultHolo.removeHologram(location);
if (manager.isEnabled())
manager.getCurrentHook().removeHologram(location);
}
public static void removeAllHolograms() {
if (defaultHolo != null)
defaultHolo.removeAllHolograms();
if (manager.isEnabled())
manager.getCurrentHook().removeAllHolograms();
}
public static void updateHologram(Location location, String line) {
if (defaultHolo != null)
defaultHolo.updateHologram(location, line);
if (manager.isEnabled())
manager.getCurrentHook().updateHologram(location, line);
}
public static void updateHologram(Location location, List<String> lines) {
if (defaultHolo != null)
defaultHolo.updateHologram(location, lines);
if (manager.isEnabled())
manager.getCurrentHook().updateHologram(location, lines);
}
}

View File

@ -1,34 +0,0 @@
package com.songoda.core.hooks;
import com.songoda.core.hooks.holograms.Holograms;
import com.songoda.core.hooks.holograms.HologramsHolograms;
import com.songoda.core.hooks.holograms.HolographicDisplaysHolograms;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import java.lang.reflect.InvocationTargetException;
import java.util.logging.Level;
public enum HologramType {
HOLOGRAPHIC_DISPLAYS("HolographicDisplays", HolographicDisplaysHolograms.class),
HOLOGRAMS("Holograms", HologramsHolograms.class)
;
public final String plugin;
protected final Class managerClass;
private HologramType(String plugin, Class managerClass) {
this.plugin = plugin;
this.managerClass = managerClass;
}
protected Holograms getInstance(JavaPlugin javaPlugin) {
try {
return (Holograms) managerClass.getDeclaredConstructor(JavaPlugin.class).newInstance(javaPlugin);
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException ex) {
Bukkit.getLogger().log(Level.SEVERE, "Unexpected Error while creating a new Hologram Manager for " + name(), ex);
}
return null;
}
}

View File

@ -0,0 +1,18 @@
package com.songoda.core.hooks;
public interface Hook {
/**
* Get the name of the plugin being used
*
* @return
*/
abstract String getName();
/**
* Check to see if the economy plugin being used is active
*
* @return true if the plugin is loaded and active
*/
abstract boolean isEnabled();
}

View File

@ -0,0 +1,162 @@
package com.songoda.core.hooks;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class HookManager<T extends Hook> {
private final Class typeClass;
private T defaultHook = null;
private boolean loaded = false;
private final Map<PluginHook, T> registeredHooks = new HashMap<>();
public HookManager(Class typeClass) {
this.typeClass = typeClass;
}
/**
* Load all supported plugins.
*/
public void load() {
if (!loaded) {
registeredHooks.putAll(PluginHook.loadHooks(typeClass).entrySet().stream()
.collect(Collectors.toMap(e -> e.getKey(), e -> (T) e.getValue())));
if (!registeredHooks.isEmpty()) {
defaultHook = (T) registeredHooks.values().iterator().next();
}
loaded = true;
}
}
/**
* Get the currently selected plugin hook. <br>
* If none were set, then the first one found is used.
*
* @return The instance of T that was created, or null if none available.
*/
public T getCurrentHook() {
return defaultHook;
}
/**
* Set the default hook to a different plugin, if that plugin exists. <br>
* If the plugin is not loaded or supported,
* the previously defined default will be used.
*
* @param name name of the plugin to use
* @return true if the default was set to this plugin
*/
public boolean setPreferredHook(String name) {
T hook = getHook(name);
if (hook != null) {
defaultHook = hook;
return true;
}
return false;
}
/**
* Set the default hook to a different plugin, if that plugin exists. <br />
* If the plugin is not loaded or supported,
* the previously defined default will be used.
*
* @param plugin plugin to use
* @return true if the default was set to this plugin
*/
public boolean setPreferredHook(PluginHook plugin) {
T hook = getHook(plugin);
if (hook != null) {
defaultHook = hook;
return true;
}
return false;
}
/**
* Try to grab the handler for this specific plugin hook.
*
* @param name plugin to use
* @return returns null if plugin is not enabled
*/
public T getHook(String name) {
if (name == null)
return null;
final String plugin = name.trim();
return (T) registeredHooks.get(registeredHooks.keySet().stream()
.filter(type -> type.plugin.equalsIgnoreCase(plugin))
.findFirst().orElse(null));
}
/**
* Try to grab the handler for this specific plugin hook.
*
* @param hook plugin to use
* @return returns null if plugin is not enabled
*/
public T getHook(PluginHook hook) {
return registeredHooks.get(hook);
}
/**
* Grab a list of all supported and loaded plugin hooks.
*
* @return an immutable collection of the loaded handler instances
*/
public Collection<T> getRegisteredHooks() {
return Collections.unmodifiableCollection(registeredHooks.values());
}
/**
* Get a list of all supported plugins that we can hook into.
*
* @return an immutable collection of plugin names that can be used.
*/
public List<String> getPossiblePlugins() {
return PluginHook.loadHooks(typeClass).values().stream()
.map(v -> v.getName())
.collect(Collectors.toList());
}
/**
* Check to see if a specific plugin hook is enabled.
*
* @param name plugin to check
* @return true if this plugin is supported and loaded
*/
public boolean isEnabled(String name) {
return getHook(name) != null;
}
/**
* Check to see if a specific plugin hook is enabled.
*
* @param hook plugin to check
* @return true if this plugin is supported and loaded
*/
public boolean isEnabled(PluginHook hook) {
return registeredHooks.containsKey(hook);
}
/**
* Check to see if there is a default hook loaded.
*
* @return returns false if there are no supported plugins loaded
*/
public boolean isEnabled() {
return defaultHook != null;
}
/**
* Get the name of the default plugin being hooked into.
*
* @return plugin name, or null if none enabled.
*/
public String getName() {
return defaultHook != null ? defaultHook.getName() : null;
}
}

View File

@ -0,0 +1,132 @@
package com.songoda.core.hooks;
import com.songoda.core.hooks.economies.Economy;
import com.songoda.core.hooks.economies.PlayerPointsEconomy;
import com.songoda.core.hooks.economies.ReserveEconomy;
import com.songoda.core.hooks.economies.VaultEconomy;
import com.songoda.core.hooks.stackers.StackMob;
import com.songoda.core.hooks.stackers.Stacker;
import com.songoda.core.hooks.stackers.UltimateStacker;
import com.songoda.core.hooks.stackers.WildStacker;
import com.songoda.core.hooks.holograms.Holograms;
import com.songoda.core.hooks.holograms.HologramsHolograms;
import com.songoda.core.hooks.holograms.HolographicDisplaysHolograms;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.logging.Level;
import java.util.stream.Collectors;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager;
public final class PluginHook <T extends Class> {
public static final PluginHook ECO_VAULT = new PluginHook(Economy.class, "Vault", VaultEconomy.class);
public static final PluginHook ECO_PLAYER_POINTS = new PluginHook(Economy.class, "PlayerPoints", PlayerPointsEconomy.class);
public static final PluginHook ECO_RESERVE = new PluginHook(Economy.class, "Reserve", ReserveEconomy.class);
public static final PluginHook STACKER_ULTIMATE = new PluginHook(Stacker.class, "UltimateStacker", UltimateStacker.class);
public static final PluginHook STACKER_WILD = new PluginHook(Stacker.class, "WildStacker", WildStacker.class);
public static final PluginHook STACKER_STACK_MOB = new PluginHook(Stacker.class, "StackMob", StackMob.class);
public static final PluginHook HOLO_DISPLAYS = new PluginHook(Holograms.class, "HolographicDisplays", HolographicDisplaysHolograms.class);
public static final PluginHook HOLO_HOLOGRAMS = new PluginHook(Holograms.class, "Holograms", HologramsHolograms.class);
/******* Start Manager stuff *******/
protected final T hookGeneric;
protected final String plugin;
protected final Class managerClass;
protected static Map<Class, PluginHook> hooks;
protected Constructor pluginConstructor;
private PluginHook(T type, String pluginName, Class handler) {
if (!handler.isAssignableFrom(Hook.class)) {
throw new RuntimeException("Tried to register a non-Hook plugin hook! " + pluginName + " -> " + handler.getName());
}
this.hookGeneric = type;
this.plugin = pluginName;
this.managerClass = handler;
if (hooks == null) {
hooks = new LinkedHashMap();
}
hooks.put(handler, this);
// Does this class have a plugin constructor?
try {
pluginConstructor = type.getDeclaredConstructor(Plugin.class);
} catch (NoSuchMethodException | SecurityException ex) {
// nope!
}
}
protected static Map<PluginHook, Hook> loadHooks(Class type) {
Map<PluginHook, Hook> loaded = new LinkedHashMap<>();
PluginManager pluginManager = Bukkit.getPluginManager();
for (PluginHook hook : getHooks(type)) {
if (pluginManager.isPluginEnabled(hook.plugin)) {
Hook handler = (Hook) hook.load();
if (handler != null && handler.isEnabled()) {
loaded.put(hook, handler);
}
}
}
return loaded;
}
protected static List<PluginHook> getHooks(Class type) {
return hooks.entrySet().parallelStream()
.filter(e -> e.getKey() == type || e.getValue().managerClass == type || type.isAssignableFrom(e.getKey()))
.map(Map.Entry::getValue)
.collect(Collectors.toList());
}
public String getPluginName() {
return plugin;
}
protected Object load() {
try {
return managerClass.cast(
pluginConstructor != null
? pluginConstructor.newInstance((Plugin) null)
: managerClass.getConstructor().newInstance());
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException ex) {
Bukkit.getLogger().log(Level.SEVERE, "Unexpected Error while creating a new Hook Manager for " + plugin, ex);
}
return null;
}
protected Object load(Plugin hookingPlugin) {
try {
return managerClass.cast(
pluginConstructor != null
? pluginConstructor.newInstance(hookingPlugin)
: managerClass.getConstructor().newInstance());
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException ex) {
Bukkit.getLogger().log(Level.SEVERE, "Unexpected Error while creating a new Hook Manager for " + plugin, ex);
}
return null;
}
@Override
public int hashCode() {
int hash = 3;
hash = 37 * hash + Objects.hashCode(this.plugin);
hash = 37 * hash + Objects.hashCode(this.managerClass);
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null || getClass() != obj.getClass()) {
return false;
}
final PluginHook<?> other = (PluginHook<?>) obj;
return Objects.equals(this.plugin, other.plugin)
&& Objects.equals(this.managerClass, other.managerClass);
}
}

View File

@ -1,21 +1,9 @@
package com.songoda.core.hooks.economies;
import com.songoda.core.hooks.Hook;
import org.bukkit.OfflinePlayer;
public interface Economy {
/**
* Get the name of the economy plugin being used
*
* @return
*/
String getName();
/**
* Check to see if the economy plugin being used is active
* @return true if the plugin is loaded and active
*/
boolean isEnabled();
public interface Economy extends Hook {
/**
* Check to see if a player has at least some balance available

View File

@ -1,12 +1,13 @@
package com.songoda.core.hooks.holograms;
import com.songoda.core.hooks.Hook;
import org.bukkit.Location;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.Collections;
import java.util.List;
public abstract class Holograms {
public abstract class Holograms implements Hook {
protected double xOffset = 0.5;
protected double yOffset = 0.5;
@ -40,8 +41,6 @@ public abstract class Holograms {
protected abstract double defaultHeightOffset();
public abstract String getName();
public void createHologram(Location location, String line) {
createHologram(location, Collections.singletonList(line));
}

View File

@ -26,6 +26,11 @@ public class HologramsHolograms extends Holograms {
return "Holograms";
}
@Override
public boolean isEnabled() {
return hologramPlugin.isEnabled();
}
@Override
protected double defaultHeightOffset() {
return 0.5;

View File

@ -18,6 +18,11 @@ public class HolographicDisplaysHolograms extends Holograms {
return "HolographicDisplays";
}
@Override
public boolean isEnabled() {
return true;
}
@Override
protected double defaultHeightOffset() {
return 1;

View File

@ -1,6 +1,7 @@
package com.songoda.core.hooks.stackers;
import org.bukkit.Bukkit;
import org.bukkit.entity.Item;
import org.bukkit.entity.LivingEntity;
import uk.antiperson.stackmob.api.EntityManager;
import uk.antiperson.stackmob.api.StackedEntity;
@ -10,8 +11,44 @@ public class StackMob extends Stacker {
private final EntityManager plugin;
public StackMob() {
this.plugin = new EntityManager((uk.antiperson.stackmob.StackMob)
Bukkit.getPluginManager().getPlugin("StackMob"));
this.plugin = new EntityManager((uk.antiperson.stackmob.StackMob) Bukkit.getPluginManager().getPlugin("StackMob"));
}
@Override
public String getName() {
return "StackMob";
}
@Override
public boolean isEnabled() {
return true;
}
@Override
public boolean supportsItemStacking() {
return false;
}
@Override
public boolean supportsEntityStacking() {
return true;
}
/**
* Don't do it.
*/
@Override
public void setItemAmount(Item item, int amount) {
// idk, if you ignored the warnings and still use this method, we can at least try to help out
item.getItemStack().setAmount(amount);
}
/**
* If you use this method, you're pretty lazy. Didn't you see supportsItemStacking()?
*/
@Override
public int getItemAmount(Item item) {
return item.getItemStack().getAmount();
}
@Override

View File

@ -1,8 +1,18 @@
package com.songoda.core.hooks.stackers;
import com.songoda.core.hooks.Hook;
import org.bukkit.entity.Item;
import org.bukkit.entity.LivingEntity;
public abstract class Stacker {
public abstract class Stacker implements Hook {
public abstract boolean supportsItemStacking();
public abstract boolean supportsEntityStacking();
public abstract void setItemAmount(Item item, int amount);
public abstract int getItemAmount(Item item);
public abstract boolean isStacked(LivingEntity entity);

View File

@ -1,14 +1,62 @@
package com.songoda.core.hooks.stackers;
import com.songoda.ultimatestacker.entity.EntityStack;
import com.songoda.ultimatestacker.utils.Methods;
import java.lang.reflect.Method;
import org.bukkit.entity.Item;
import org.bukkit.entity.LivingEntity;
public class UltimateStacker extends Stacker {
private final com.songoda.ultimatestacker.UltimateStacker plugin;
private boolean itemStackMethods = true;
private Method oldUltimateStacker_updateItemAmount;
public UltimateStacker() {
this.plugin = com.songoda.ultimatestacker.UltimateStacker.getInstance();
try {
oldUltimateStacker_updateItemAmount = com.songoda.ultimatestacker.utils.Methods.class.getDeclaredMethod("updateItemAmount", Item.class, int.class);
itemStackMethods = true;
} catch (NoSuchMethodException | SecurityException ex) {
}
}
@Override
public String getName() {
return "UltimateStacker";
}
@Override
public boolean isEnabled() {
return plugin.isEnabled();
}
@Override
public boolean supportsItemStacking() {
return true;
}
@Override
public boolean supportsEntityStacking() {
return true;
}
@Override
public void setItemAmount(Item item, int amount) {
if (itemStackMethods) {
try {
oldUltimateStacker_updateItemAmount.invoke(null, item, amount);
} catch (Exception ex) {
item.remove(); // not the best solution, but prevents duping
}
} else {
Methods.updateItemAmount(item, item.getItemStack(), amount);
}
}
@Override
public int getItemAmount(Item item) {
return Methods.getActualItemAmount(item);
}
@Override

View File

@ -2,10 +2,41 @@ package com.songoda.core.hooks.stackers;
import com.bgsoftware.wildstacker.api.WildStackerAPI;
import com.bgsoftware.wildstacker.api.objects.StackedEntity;
import org.bukkit.entity.Item;
import org.bukkit.entity.LivingEntity;
public class WildStacker extends Stacker {
@Override
public String getName() {
return "WildStacker";
}
@Override
public boolean isEnabled() {
return true;
}
@Override
public boolean supportsItemStacking() {
return true;
}
@Override
public boolean supportsEntityStacking() {
return true;
}
@Override
public void setItemAmount(Item item, int amount) {
WildStackerAPI.getStackedItem(item).setStackAmount(amount, true);
}
@Override
public int getItemAmount(Item item) {
return WildStackerAPI.getItemAmount(item);
}
@Override
public boolean isStacked(LivingEntity entity) {
return WildStackerAPI.getEntityAmount(entity) != 0;