mirror of
https://github.com/songoda/UltimateTimber.git
synced 2024-11-29 05:16:29 +01:00
Refactor hooking system
This commit is contained in:
parent
9610cebb39
commit
82e8c064f4
@ -15,6 +15,7 @@ import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import com.songoda.ultimatetimber.commands.CommandHandler;
|
||||
import com.songoda.ultimatetimber.configurations.DefaultConfig;
|
||||
import com.songoda.ultimatetimber.hooks.HookManager;
|
||||
import com.songoda.ultimatetimber.hooks.JobsRebornHook;
|
||||
import com.songoda.ultimatetimber.hooks.McMMOHook;
|
||||
import com.songoda.ultimatetimber.treefall.CustomLoot;
|
||||
@ -77,26 +78,9 @@ public class UltimateTimber extends JavaPlugin {
|
||||
this.reloadValidWorlds();
|
||||
|
||||
/*
|
||||
Check for McMMO
|
||||
Hook into supported plugins
|
||||
*/
|
||||
if (Bukkit.getPluginManager().isPluginEnabled("mcMMO")) {
|
||||
if (McMMOHook.setEnabled()) {
|
||||
console.sendMessage("Hooks: Hooked into mcMMO");
|
||||
} else {
|
||||
console.sendMessage("Hooks: Unable to hook with mcMMO, the version installed is not supported!");
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Check for Jobs Reborn
|
||||
*/
|
||||
if (Bukkit.getPluginManager().isPluginEnabled("Jobs")) {
|
||||
if (JobsRebornHook.setEnabled()) {
|
||||
console.sendMessage("Hooks: Hooked into Jobs Reborn");
|
||||
} else {
|
||||
console.sendMessage("Hooks: Unable to hook with Jobs Reborn, the version installed is not supported!");
|
||||
}
|
||||
}
|
||||
HookManager.getInstance().hook();
|
||||
|
||||
/*
|
||||
Register command executor and tab completer
|
||||
|
@ -0,0 +1,77 @@
|
||||
package com.songoda.ultimatetimber.hooks;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class HookManager {
|
||||
|
||||
private static HookManager instance;
|
||||
|
||||
private Set<TimberHook> hooks;
|
||||
|
||||
private HookManager() {
|
||||
this.hooks = new HashSet<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the instance of the HookManager
|
||||
*
|
||||
* @return The instance of the HookManager
|
||||
*/
|
||||
public static HookManager getInstance() {
|
||||
if (instance == null)
|
||||
instance = new HookManager();
|
||||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hooks into compatible plugins
|
||||
*/
|
||||
public void hook() {
|
||||
this.tryHook("mcMMO", McMMOHook.class);
|
||||
this.tryHook("Jobs", JobsRebornHook.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to hook into a compatible plugin
|
||||
*
|
||||
* @param pluginName The name of the plugin
|
||||
* @param hookClass The hook class
|
||||
*/
|
||||
private void tryHook(String pluginName, Class<? extends TimberHook> hookClass) {
|
||||
if (!Bukkit.getPluginManager().isPluginEnabled(pluginName))
|
||||
return;
|
||||
|
||||
try {
|
||||
this.hooks.add(hookClass.newInstance());
|
||||
Bukkit.getConsoleSender().sendMessage(String.format("Hooks: Hooked into %s!", pluginName));
|
||||
} catch (Exception ex) {
|
||||
Bukkit.getConsoleSender().sendMessage(String.format("Hooks: Unable to hook with %s, the version installed is not supported!", pluginName));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies the loaded hooks
|
||||
*
|
||||
* @param player The player to apply the hook for
|
||||
* @param treeBlocks The blocks of the tree that were broken
|
||||
*/
|
||||
public void applyHooks(Player player, HashSet<Block> treeBlocks) {
|
||||
Set<TimberHook> invalidHooks = new HashSet<>();
|
||||
for (TimberHook hook : this.hooks) {
|
||||
try {
|
||||
hook.apply(player, treeBlocks);
|
||||
} catch (Exception ex) {
|
||||
invalidHooks.add(hook);
|
||||
}
|
||||
}
|
||||
|
||||
for (TimberHook hook : invalidHooks)
|
||||
this.hooks.remove(hook);
|
||||
}
|
||||
|
||||
}
|
@ -4,7 +4,6 @@ import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.GameMode;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.entity.Player;
|
||||
@ -15,52 +14,23 @@ import com.gamingmesh.jobs.container.ActionType;
|
||||
import com.gamingmesh.jobs.container.JobsPlayer;
|
||||
import com.songoda.ultimatetimber.utils.WoodToLogConverter;
|
||||
|
||||
public class JobsRebornHook {
|
||||
public class JobsRebornHook implements TimberHook {
|
||||
|
||||
private static boolean enabled = false;
|
||||
|
||||
/**
|
||||
* Updates a Player's Woodcutter job based on the number of logs they broke
|
||||
*
|
||||
* @param player The Player to update
|
||||
* @param treeBlocks The tree blocks that were broken
|
||||
*/
|
||||
public static void updateWoodcutterJob(Player player, HashSet<Block> treeBlocks) {
|
||||
if (!enabled) return;
|
||||
if (player.getGameMode().equals(GameMode.CREATIVE)) return;
|
||||
@Override
|
||||
public void apply(Player player, HashSet<Block> treeBlocks) throws Exception {
|
||||
if (player.getGameMode().equals(GameMode.CREATIVE))
|
||||
return;
|
||||
|
||||
try {
|
||||
// Replicate the same code that Jobs Reborn uses
|
||||
JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayer(player);
|
||||
if (jPlayer == null)
|
||||
return;
|
||||
|
||||
for (Block log : getLogs(treeBlocks)) {
|
||||
BlockActionInfo bInfo = new BlockActionInfo(log, ActionType.BREAK);
|
||||
Jobs.action(jPlayer, bInfo, log);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
Bukkit.getLogger().warning("[UltimateTimber] Warning: The version of Jobs Reborn you are using is not compatible with UltimateTimber. Disabling hook...");
|
||||
enabled = false;
|
||||
// Replicate the same code that Jobs Reborn uses
|
||||
JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayer(player);
|
||||
if (jPlayer == null)
|
||||
return;
|
||||
|
||||
Set<Block> logs = treeBlocks.stream().filter(x -> WoodToLogConverter.convert(x.getType()).name().endsWith("LOG")).collect(Collectors.toSet());
|
||||
for (Block log : logs) {
|
||||
BlockActionInfo bInfo = new BlockActionInfo(log, ActionType.BREAK);
|
||||
Jobs.action(jPlayer, bInfo, log);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the hook to enabled
|
||||
*/
|
||||
public static boolean setEnabled() {
|
||||
enabled = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Separates out the logs from the rest of the tree blocks
|
||||
*
|
||||
* @param treeBlocks The set of blocks in the tree that was broken
|
||||
* @return A set of the logs in the tree blocks
|
||||
*/
|
||||
private static Set<Block> getLogs(HashSet<Block> treeBlocks) {
|
||||
return treeBlocks.stream().filter(x -> WoodToLogConverter.convert(x.getType()).name().endsWith("LOG")).collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -3,7 +3,6 @@ package com.songoda.ultimatetimber.hooks;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.HashSet;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.GameMode;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.block.Block;
|
||||
@ -13,34 +12,12 @@ import com.gmail.nossr50.api.ExperienceAPI;
|
||||
import com.gmail.nossr50.config.experience.ExperienceConfig;
|
||||
import com.songoda.ultimatetimber.utils.WoodToLogConverter;
|
||||
|
||||
public class McMMOHook {
|
||||
public class McMMOHook implements TimberHook {
|
||||
|
||||
private static boolean enabled = false;
|
||||
private static Enum<?> woodcuttingEnum;
|
||||
private static Method getXpMethod;
|
||||
private Enum<?> woodcuttingEnum;
|
||||
private Method getXpMethod;
|
||||
|
||||
/**
|
||||
* Updates a Player's Wood Cutting skill based on the number of logs they broke
|
||||
*
|
||||
* @param player The Player to update
|
||||
* @param treeBlocks The tree blocks that were broken
|
||||
*/
|
||||
public static void updateWoodCuttingSkill(Player player, HashSet<Block> treeBlocks) {
|
||||
if (!enabled) return;
|
||||
if (player.getGameMode().equals(GameMode.CREATIVE)) return;
|
||||
|
||||
try {
|
||||
ExperienceAPI.addXP(player, "woodcutting", getLogXP(treeBlocks), "pve");
|
||||
} catch (Exception ex) {
|
||||
Bukkit.getLogger().warning("[UltimateTimber] Warning: The version of mcMMO you are using is not compatible with UltimateTimber. Disabling hook...");
|
||||
enabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the hook to enabled
|
||||
*/
|
||||
public static boolean setEnabled() {
|
||||
public McMMOHook() throws NoSuchMethodException, SecurityException, ClassNotFoundException {
|
||||
try { // Try to find mcMMO Overhaul
|
||||
Class<?> primarySkillTypeClass = Class.forName("com.gmail.nossr50.datatypes.skills.PrimarySkillType");
|
||||
for (Object enumValue : primarySkillTypeClass.getEnumConstants()) {
|
||||
@ -52,39 +29,31 @@ public class McMMOHook {
|
||||
}
|
||||
getXpMethod = ExperienceConfig.class.getMethod("getXp", woodcuttingEnum.getClass(), Material.class);
|
||||
} catch (Exception ex) {
|
||||
try { // Try to find mcMMO Classic
|
||||
Class<?> skillTypeClass = Class.forName("com.gmail.nossr50.datatypes.skills.SkillType");
|
||||
for (Object enumValue : skillTypeClass.getEnumConstants()) {
|
||||
Enum<?> skillTypeEnum = (Enum<?>) enumValue;
|
||||
if (skillTypeEnum.name().equals("WOODCUTTING")) {
|
||||
woodcuttingEnum = skillTypeEnum;
|
||||
break;
|
||||
}
|
||||
Class<?> skillTypeClass = Class.forName("com.gmail.nossr50.datatypes.skills.SkillType");
|
||||
for (Object enumValue : skillTypeClass.getEnumConstants()) {
|
||||
Enum<?> skillTypeEnum = (Enum<?>) enumValue;
|
||||
if (skillTypeEnum.name().equals("WOODCUTTING")) {
|
||||
woodcuttingEnum = skillTypeEnum;
|
||||
break;
|
||||
}
|
||||
getXpMethod = ExperienceConfig.class.getMethod("getXp", woodcuttingEnum.getClass(), Material.class);
|
||||
} catch (Exception ex2) {
|
||||
return false;
|
||||
}
|
||||
getXpMethod = ExperienceConfig.class.getMethod("getXp", woodcuttingEnum.getClass(), Material.class);
|
||||
}
|
||||
|
||||
enabled = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the XP that a player would obtain from breaking the logs in the tree
|
||||
*
|
||||
* @param treeBlocks The potential log blocks
|
||||
* @return The XP a player should gain
|
||||
*/
|
||||
private static int getLogXP(HashSet<Block> treeBlocks) throws Exception {
|
||||
|
||||
@Override
|
||||
public void apply(Player player, HashSet<Block> treeBlocks) throws Exception {
|
||||
if (player.getGameMode().equals(GameMode.CREATIVE))
|
||||
return;
|
||||
|
||||
int xp = 0;
|
||||
for (Block block : treeBlocks) {
|
||||
Material material = WoodToLogConverter.convert(block.getType());
|
||||
if (!material.name().endsWith("LOG")) continue;
|
||||
xp += (int) getXpMethod.invoke(ExperienceConfig.getInstance(), woodcuttingEnum, material);
|
||||
}
|
||||
return xp;
|
||||
|
||||
ExperienceAPI.addXP(player, "woodcutting", xp, "pve");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,15 @@
|
||||
package com.songoda.ultimatetimber.hooks;
|
||||
|
||||
import java.util.HashSet;
|
||||
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public interface TimberHook {
|
||||
|
||||
/**
|
||||
* Applies the hook
|
||||
*/
|
||||
public void apply(Player player, HashSet<Block> treeBlocks) throws Exception;
|
||||
|
||||
}
|
@ -15,6 +15,7 @@ import com.songoda.ultimatetimber.UltimateTimber;
|
||||
import com.songoda.ultimatetimber.configurations.DefaultConfig;
|
||||
import com.songoda.ultimatetimber.events.TreeFallEvent;
|
||||
import com.songoda.ultimatetimber.events.TreeFellEvent;
|
||||
import com.songoda.ultimatetimber.hooks.HookManager;
|
||||
import com.songoda.ultimatetimber.hooks.JobsRebornHook;
|
||||
import com.songoda.ultimatetimber.hooks.McMMOHook;
|
||||
|
||||
@ -64,9 +65,8 @@ public class TreeFallListener implements Listener {
|
||||
if (config.getBoolean(DefaultConfig.DELETE_BROKEN_LOG))
|
||||
TreeReplant.replaceOriginalBlock(block);
|
||||
|
||||
// Add to hooks
|
||||
McMMOHook.updateWoodCuttingSkill(event.getPlayer(), blocks);
|
||||
JobsRebornHook.updateWoodcutterJob(event.getPlayer(), blocks);
|
||||
// Apply hooks
|
||||
HookManager.getInstance().applyHooks(event.getPlayer(), blocks);
|
||||
|
||||
if (config.getBoolean(DefaultConfig.ACCURATE_AXE_DURABILITY))
|
||||
AxeDurability.adjustAxeDamage(blocks, event.getPlayer());
|
||||
|
Loading…
Reference in New Issue
Block a user