Added support for log managers starting with CoreProtect.

This commit is contained in:
Brianna 2020-08-28 17:45:42 -05:00
parent 4062871bba
commit f45034435c
6 changed files with 199 additions and 37 deletions

View File

@ -78,15 +78,15 @@
</includes>
</artifactSet>
<filters>
<filter>
<artifact>*:*</artifact>
<includes>
<include>com/</include>
<include>META-INF/MANIFEST.MF</include>
<include>META-INF/maven/com.songoda/SongodaCore/</include>
</includes>
</filter>
</filters>
<filter>
<artifact>*:*</artifact>
<includes>
<include>com/</include>
<include>META-INF/MANIFEST.MF</include>
<include>META-INF/maven/com.songoda/SongodaCore/</include>
</includes>
</filter>
</filters>
</configuration>
</execution>
</executions>
@ -325,6 +325,12 @@
<version>2.1.50</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net</groupId>
<artifactId>coreprotect</artifactId>
<version>2.17.5</version>
<scope>provided</scope>
</dependency>
<!-- End Plugin Hooks -->
<dependency>
<groupId>com.googlecode.json-simple</groupId>

View File

@ -23,14 +23,7 @@ public class HookManager<T extends Hook> {
* Load all supported plugins.
*/
public void load() {
if (!loaded) {
registeredHooks.putAll(PluginHook.loadHooks(typeClass, null).entrySet().stream()
.collect(Collectors.toMap(e -> e.getKey(), e -> (T) e.getValue())));
if (!registeredHooks.isEmpty()) {
defaultHook = (T) registeredHooks.values().iterator().next();
}
loaded = true;
}
load(null);
}
/**
@ -40,7 +33,7 @@ public class HookManager<T extends Hook> {
public void load(Plugin hookingPlugin) {
if (!loaded) {
registeredHooks.putAll(PluginHook.loadHooks(typeClass, hookingPlugin).entrySet().stream()
.collect(Collectors.toMap(e -> e.getKey(), e -> (T) e.getValue())));
.collect(Collectors.toMap(Map.Entry::getKey, e -> (T) e.getValue())));
if (!registeredHooks.isEmpty()) {
defaultHook = (T) registeredHooks.values().iterator().next();
}

View File

@ -0,0 +1,92 @@
package com.songoda.core.hooks;
import com.songoda.core.hooks.log.Log;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.block.Block;
/**
* A convenience class for static access to a Log HookManager
*/
public class LogManager {
private static final HookManager<Log> manager = new HookManager(Log.class);
/**
* Load all supported log plugins. <br />
* Note: This method should be called in your plugin's onEnable() section
*/
public static void load() {
manager.load();
}
public static HookManager getManager() {
return manager;
}
/**
* Grab the default log plugin. <br />
* NOTE: using a default log assumes that this library is shaded
*
* @return returns null if no plugin enabled
*/
public static Log getLog() {
return manager.getCurrentHook();
}
/**
* Check to see if there is a default log loaded. <br />
* NOTE: using a default log assumes that this library is shaded
*
* @return returns false if there are no supported log plugins
*/
public static boolean isEnabled() {
return manager.isEnabled();
}
/**
* Get the name of the log plugin being used. <br />
* NOTE: using a default log assumes that this library is shaded
*
* @return
*/
public static String getName() {
return manager.getName();
}
/**
* Log the placement of a block. <br />
* NOTE: using a default log assumes that this library is shaded
*
* @param player player to commit action
* @param block the block that is placed
*/
public static void logPlacement(OfflinePlayer player, Block block) {
if (manager.isEnabled())
manager.getCurrentHook().logPlacement(player, block);
}
/**
* Log the removal of a block. <br />
* NOTE: using a default log assumes that this library is shaded
*
* @param player player to commit actionremvedplaced
*/
public static void logRemoval(OfflinePlayer player, Block block) {
if (manager.isEnabled())
manager.getCurrentHook().logRemoval(player, block);
}
/**
* Log a player interaction. <br />
* NOTE: using a default log assumes that this library is shaded
*
* @param player player to commit action
* @param location the location that is interacted with
*/
public static void logInteraction(OfflinePlayer player, Location location) {
if (manager.isEnabled())
manager.getCurrentHook().logInteraction(player, location);
}
}

View File

@ -5,13 +5,19 @@ 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.holograms.CMIHolograms;
import com.songoda.core.hooks.holograms.Holograms;
import com.songoda.core.hooks.holograms.HologramsHolograms;
import com.songoda.core.hooks.holograms.HolographicDisplaysHolograms;
import com.songoda.core.hooks.log.CoreProtectLog;
import com.songoda.core.hooks.log.Log;
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 org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.LinkedHashMap;
@ -20,21 +26,19 @@ 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 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);
public static final PluginHook HOLO_CMI = new PluginHook(Holograms.class, "CMI", CMIHolograms.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);
public static final PluginHook HOLO_CMI = new PluginHook(Holograms.class, "CMI", CMIHolograms.class);
public static final PluginHook LOG_CORE_PROTECT = new PluginHook(Log.class, "CoreProtect", CoreProtectLog.class);
/******* Start Manager stuff *******/
@ -71,11 +75,11 @@ public final class PluginHook <T extends Class> {
* NOTE: The class passed MUST extend Hook. <br>
* Permissible constructors are empty () or (org.bukkit.plugin.Plugin) <br>
* Each plugin defined must use a different handler class.
*
*
* @param <T>
* @param type Generic hook type for this plugin
* @param type Generic hook type for this plugin
* @param pluginName Plugin name
* @param handler Specific class that will handle this plugin, if enabled.
* @param handler Specific class that will handle this plugin, if enabled.
* @return instance of the PluginHook that was added
*/
public static <T extends Class> PluginHook addHook(T type, String pluginName, Class handler) {
@ -87,6 +91,7 @@ public final class PluginHook <T extends Class> {
PluginManager pluginManager = Bukkit.getPluginManager();
for (PluginHook hook : getHooks(type)) {
System.out.println("Looking at " + hook.plugin);
if (pluginManager.isPluginEnabled(hook.plugin)) {
Hook handler = (Hook) (plugin != null ? hook.load(plugin) : hook.load());
if (handler != null && handler.isEnabled()) {

View File

@ -0,0 +1,51 @@
package com.songoda.core.hooks.log;
import com.songoda.core.compatibility.ServerVersion;
import net.coreprotect.CoreProtect;
import net.coreprotect.CoreProtectAPI;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.block.Block;
public class CoreProtectLog extends Log {
private CoreProtectAPI api;
private boolean useDeprecatedMethod = ServerVersion.isServerVersionBelow(ServerVersion.V1_12);
public CoreProtectLog() {
this.api = CoreProtect.getInstance().getAPI();
}
@Override
public String getName() {
return "CoreProtect";
}
@Override
public boolean isEnabled() {
return api.isEnabled();
}
@Override
public void logPlacement(OfflinePlayer player, Block block) {
if (this.useDeprecatedMethod) {
this.api.logPlacement(player.getName(), block.getLocation(), block.getType(), block.getData());
} else {
this.api.logPlacement(player.getName(), block.getLocation(), block.getType(), block.getBlockData());
}
}
@Override
public void logRemoval(OfflinePlayer player, Block block) {
if (this.useDeprecatedMethod) {
this.api.logRemoval(player.getName(), block.getLocation(), block.getType(), block.getData());
} else {
this.api.logRemoval(player.getName(), block.getLocation(), block.getType(), block.getBlockData());
}
}
@Override
public void logInteraction(OfflinePlayer player, Location location) {
this.api.logInteraction(player.getName(), location);
}
}

View File

@ -0,0 +1,15 @@
package com.songoda.core.hooks.log;
import com.songoda.core.hooks.Hook;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.block.Block;
public abstract class Log implements Hook {
public abstract void logPlacement(OfflinePlayer player, Block block);
public abstract void logRemoval(OfflinePlayer player, Block block);
public abstract void logInteraction(OfflinePlayer player, Location location);
}