9
0
mirror of https://github.com/HibiscusMC/HibiscusCommons.git synced 2025-12-19 15:09:26 +00:00

feat: slight hook and custom event overhaul

This commit is contained in:
LoJoSho
2023-12-28 11:40:32 -06:00
parent c87ceef33f
commit c49464d4bb
25 changed files with 224 additions and 32 deletions

View File

@@ -78,6 +78,8 @@ allprojects {
compileOnly("LibsDisguises:LibsDisguises:10.0.21") {
exclude("org.spigotmc", "spigot")
}
compileOnly("com.github.Xiao-MoMi:Custom-Fishing:2.0.6")
compileOnly("com.ticxo.modelengine:ModelEngine:R4.0.2")
// Lombok <3
annotationProcessor("org.projectlombok:lombok:1.18.28")

View File

@@ -4,7 +4,6 @@ import lombok.Getter;
import me.lojosho.hibiscuscommons.hooks.Hooks;
import me.lojosho.hibiscuscommons.nms.NMSHandlers;
import me.lojosho.hibiscuscommons.util.ServerUtils;
import org.bukkit.plugin.java.JavaPlugin;
public final class HibiscusCommonsPlugin extends HibiscusPlugin {

View File

@@ -3,7 +3,6 @@ package me.lojosho.hibiscuscommons;
import com.jeff_media.updatechecker.UpdateCheckSource;
import com.jeff_media.updatechecker.UpdateChecker;
import lombok.Getter;
import me.lojosho.hibiscuscommons.nms.NMSHandlers;
import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;

View File

@@ -0,0 +1,31 @@
package me.lojosho.hibiscuscommons.api.events;
import me.lojosho.hibiscuscommons.hooks.Hook;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;
public abstract class HibiscusHookEvent extends Event {
private static final HandlerList handlers = new HandlerList();
private final Hook hook;
public HibiscusHookEvent(Hook hook) {
this.hook = hook;
}
public Hook getHook() {
return hook;
}
@NotNull
@Override
public HandlerList getHandlers() {
return handlers;
}
@NotNull
public static HandlerList getHandlerList() {
return handlers;
}
}

View File

@@ -0,0 +1,19 @@
package me.lojosho.hibiscuscommons.api.events;
import me.lojosho.hibiscuscommons.hooks.Hook;
import org.bukkit.entity.Player;
public abstract class HibiscusHookPlayerEvent extends HibiscusHookEvent {
private final Player player;
public HibiscusHookPlayerEvent(Hook hook, Player player) {
super(hook);
this.player = player;
}
public Player getPlayer() {
return player;
}
}

View File

@@ -1,24 +1,18 @@
package me.lojosho.hibiscuscommons.api.events;
import me.lojosho.hibiscuscommons.hooks.Hook;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;
/**
* This is called when a hook is reloaded from a plugin. This is useful for plugins like ItemsAdder, which loads items async from the main thread.
*/
public class HibiscusHookReload extends Event {
public class HibiscusHookReload extends HibiscusHookEvent {
private static final HandlerList handlers = new HandlerList();
private final Hook hook;
public HibiscusHookReload(Hook hook) {
this.hook = hook;
}
public Hook getHook() {
return hook;
super(hook);
}
@NotNull
@@ -26,4 +20,9 @@ public class HibiscusHookReload extends Event {
public HandlerList getHandlers() {
return handlers;
}
@NotNull
public static HandlerList getHandlerList() {
return handlers;
}
}

View File

@@ -1,19 +1,19 @@
package me.lojosho.hibiscuscommons.api.events;
import me.lojosho.hibiscuscommons.hooks.Hook;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerEvent;
import org.jetbrains.annotations.NotNull;
public class HibiscusPlayerUnVanishEvent extends PlayerEvent implements Cancellable {
public class HibiscusPlayerUnVanishEvent extends HibiscusHookPlayerEvent implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private boolean cancel = false;
public HibiscusPlayerUnVanishEvent(Player player) {
super(player);
public HibiscusPlayerUnVanishEvent(Hook hook, Player player) {
super(hook, player);
}
@NotNull

View File

@@ -1,19 +1,19 @@
package me.lojosho.hibiscuscommons.api.events;
import me.lojosho.hibiscuscommons.hooks.Hook;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerEvent;
import org.jetbrains.annotations.NotNull;
public class HibiscusPlayerVanishEvent extends PlayerEvent implements Cancellable {
public class HibiscusPlayerVanishEvent extends HibiscusHookPlayerEvent implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private boolean cancel = false;
public HibiscusPlayerVanishEvent(Player player) {
super(player);
public HibiscusPlayerVanishEvent(Hook hook, Player player) {
super(hook, player);
}
@NotNull

View File

@@ -0,0 +1,28 @@
package me.lojosho.hibiscuscommons.api.events;
import me.lojosho.hibiscuscommons.hooks.Hook;
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
public class HibiscusPluginFishEvent extends HibiscusHookPlayerEvent {
private static final HandlerList handlers = new HandlerList();
private final ItemStack itemStack;
public HibiscusPluginFishEvent(@NotNull Hook hook, @NotNull Player who, @NotNull ItemStack itemStack) {
super(hook, who);
this.itemStack = itemStack;
}
@NotNull
@Override
public HandlerList getHandlers() {
return handlers;
}
@NotNull
public static HandlerList getHandlerList() {
return handlers;
}
}

View File

@@ -1,5 +1,6 @@
package me.lojosho.hibiscuscommons.hooks;
import org.bukkit.entity.Entity;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
@@ -12,6 +13,7 @@ public abstract class Hook implements Listener {
private final String id;
private boolean active = false;
private boolean itemHook = false;
private boolean entityHook = false;
public Hook(@NotNull String id, HookFlag... flags) {
this.id = id;
@@ -20,6 +22,9 @@ public abstract class Hook implements Listener {
case ITEM_SUPPORT:
setEnabledItemHook(true);
break;
case ENTITY_SUPPORT:
setEnabledEntityHook(true);
break;
}
}
Hooks.addHook(this);
@@ -90,6 +95,10 @@ public abstract class Hook implements Listener {
return itemHook;
}
public final boolean hasEnabledEntityHook() {
return entityHook;
}
/**
* Sets whether the method {@link #getItem(String)} should return a non-null value
* @param enabled true if {@link #getItem(String)} should return a non-null value, false otherwise
@@ -97,4 +106,16 @@ public abstract class Hook implements Listener {
public final void setEnabledItemHook(boolean enabled) {
itemHook = enabled;
}
public final void setEnabledEntityHook(boolean enabled) {
entityHook = enabled;
}
public String getItemString(@NotNull ItemStack itemStack) {
return null;
}
public String getEntityString(@NotNull Entity entity) {
return null;
}
}

View File

@@ -1,5 +1,6 @@
package me.lojosho.hibiscuscommons.hooks;
public enum HookFlag {
ITEM_SUPPORT
ITEM_SUPPORT,
ENTITY_SUPPORT,
}

View File

@@ -6,10 +6,10 @@ import me.lojosho.hibiscuscommons.HibiscusCommonsPlugin;
import me.lojosho.hibiscuscommons.hooks.items.*;
import me.lojosho.hibiscuscommons.hooks.misc.*;
import me.lojosho.hibiscuscommons.hooks.placeholders.HookPlaceholderAPI;
import me.lojosho.hibiscuscommons.util.MessagesUtil;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Entity;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -33,6 +33,8 @@ public class Hooks {
private static final HookMMOItems MMO_ITEMS_HOOK = new HookMMOItems();
private static final HookEco ECO_ITEMS_HOOK = new HookEco();
private static final HookPlaceholderAPI PAPI_HOOK = new HookPlaceholderAPI();
private static final HookCustomFishing CF_HOOK = new HookCustomFishing();
public static Hook getHook(@NotNull String id) {
return hooks.get(id.toLowerCase());
@@ -89,6 +91,28 @@ public class Hooks {
return hook.getItem(split[1]);
}
public static String getStringItem(ItemStack itemStack) {
for (Hook hook : hooks.values()) {
if (hook.hasEnabledItemHook()) {
String stringyItem = hook.getItemString(itemStack);
if (stringyItem == null) continue;
return hook.getId() + ":" + stringyItem;
}
}
return itemStack.getType().toString();
}
public static String getStringEntity(Entity entity) {
for (Hook hook : hooks.values()) {
if (Bukkit.getPluginManager().getPlugin(hook.getId()) != null && hook.hasEnabledEntityHook()) {
String stringyEntity = hook.getEntityString(entity);
if (stringyEntity != null) return hook.getId() + ":" + stringyEntity;
}
}
return entity.getType().toString().toUpperCase();
}
public static boolean isActiveHook(String id) {
Hook hook = getHook(id);
if (hook == null) return false;

View File

@@ -15,4 +15,13 @@ public class HookEco extends Hook {
public ItemStack getItem(@NotNull String itemId) {
return Items.lookup(itemId).getItem();
}
@Override
public String getItemString(ItemStack itemStack) {
if (itemStack == null) return null;
if (!itemStack.hasItemMeta()) return null;
if (!Items.isCustomItem(itemStack)) return null;
// This should work? I'm not sure if it will return the correct key
return Items.getCustomItem(itemStack).getKey().toString();
}
}

View File

@@ -45,4 +45,11 @@ public class HookItemAdder extends Hook {
this.enabled = true;
//HMCCosmeticsPlugin.setup();
}
public String getItemString(ItemStack itemStack) {
if (itemStack == null) return null;
if (!itemStack.hasItemMeta()) return null;
if (CustomStack.byItemStack(itemStack) == null) return null;
return CustomStack.byItemStack(itemStack).getId();
}
}

View File

@@ -1,8 +1,10 @@
package me.lojosho.hibiscuscommons.hooks.items;
import io.lumine.mythic.bukkit.MythicBukkit;
import io.lumine.mythic.core.mobs.ActiveMob;
import me.lojosho.hibiscuscommons.hooks.Hook;
import me.lojosho.hibiscuscommons.hooks.HookFlag;
import org.bukkit.entity.Entity;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
@@ -12,7 +14,7 @@ import org.jetbrains.annotations.NotNull;
@SuppressWarnings("SpellCheckingInspection")
public class HookMythic extends Hook {
public HookMythic() {
super("mythicmobs", HookFlag.ITEM_SUPPORT);
super("mythicmobs", HookFlag.ITEM_SUPPORT, HookFlag.ENTITY_SUPPORT);
}
/**
@@ -22,4 +24,13 @@ public class HookMythic extends Hook {
public ItemStack getItem(@NotNull String itemId) {
return MythicBukkit.inst().getItemManager().getItemStack(itemId);
}
@Override
public String getEntityString(Entity entity) {
ActiveMob mythicMob = MythicBukkit.inst().getMobManager().getActiveMob(entity.getUniqueId()).orElse(null);
if(mythicMob != null){
return mythicMob.getMobType();
}
return null;
}
}

View File

@@ -25,4 +25,12 @@ public class HookOraxen extends Hook {
if (builder == null) return null;
return builder.build();
}
@Override
public String getItemString(ItemStack itemStack) {
if (itemStack == null) return null;
if (!itemStack.hasItemMeta()) return null;
if (!OraxenItems.exists(itemStack)) return null;
return OraxenItems.getIdByItem(itemStack);
}
}

View File

@@ -20,13 +20,13 @@ public class HookCMI extends Hook {
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerVanish(@NotNull CMIPlayerVanishEvent event) {
HibiscusPlayerVanishEvent newEvent = new HibiscusPlayerVanishEvent(event.getPlayer());
HibiscusPlayerVanishEvent newEvent = new HibiscusPlayerVanishEvent(this, event.getPlayer());
Bukkit.getPluginManager().callEvent(newEvent);
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerShow(@NotNull CMIPlayerUnVanishEvent event) {
HibiscusPlayerUnVanishEvent newEvent = new HibiscusPlayerUnVanishEvent(event.getPlayer());
HibiscusPlayerUnVanishEvent newEvent = new HibiscusPlayerUnVanishEvent(this, event.getPlayer());
Bukkit.getPluginManager().callEvent(newEvent);
}
}

View File

@@ -0,0 +1,26 @@
package me.lojosho.hibiscuscommons.hooks.misc;
import me.lojosho.hibiscuscommons.api.events.HibiscusPluginFishEvent;
import me.lojosho.hibiscuscommons.hooks.Hook;
import net.momirealms.customfishing.api.event.FishingResultEvent;
import net.momirealms.customfishing.api.mechanic.loot.LootType;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.inventory.ItemStack;
public class HookCustomFishing extends Hook {
public HookCustomFishing() {
super("CustomFishing");
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerFish(FishingResultEvent event) {
// TODO: Finish this once I get a firmer response on how this api is suppose to work
if (!event.getLoot().getType().equals(LootType.ITEM)) return;
HibiscusPluginFishEvent newEvent = new HibiscusPluginFishEvent(this, event.getPlayer(), new ItemStack(Material.ICE));
Bukkit.getPluginManager().callEvent(newEvent);
}
}

View File

@@ -18,14 +18,14 @@ public class HookLibsDisguises extends Hook {
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerVanish(@NotNull DisguiseEvent event) {
if (!(event.getEntity() instanceof Player player)) return;
HibiscusPlayerVanishEvent newEvent = new HibiscusPlayerVanishEvent(player);
HibiscusPlayerVanishEvent newEvent = new HibiscusPlayerVanishEvent(this, player);
Bukkit.getPluginManager().callEvent(newEvent);
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerShow(@NotNull UndisguiseEvent event) {
if (!(event.getEntity() instanceof Player player)) return;
HibiscusPlayerVanishEvent newEvent = new HibiscusPlayerVanishEvent(player);
HibiscusPlayerVanishEvent newEvent = new HibiscusPlayerVanishEvent(this, player);
Bukkit.getPluginManager().callEvent(newEvent);
}
}

View File

@@ -1,10 +1,20 @@
package me.lojosho.hibiscuscommons.hooks.misc;
import com.ticxo.modelengine.api.ModelEngineAPI;
import com.ticxo.modelengine.api.model.ModeledEntity;
import me.lojosho.hibiscuscommons.hooks.Hook;
import org.bukkit.entity.Entity;
public class HookModelEngine extends Hook {
public HookModelEngine() {
super("ModelEngine");
}
@Override
public String getEntityString(Entity entity) {
ModeledEntity modeledEntity = ModelEngineAPI.getModeledEntity(entity);
if (modeledEntity == null || modeledEntity.getModels().isEmpty()) return null;
return modeledEntity.getModels().entrySet().stream().findFirst().get().getValue().getBlueprint().getName();
}
}

View File

@@ -22,13 +22,13 @@ public class HookPremiumVanish extends Hook {
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onPlayerVanish(@NotNull PlayerHideEvent event) {
HibiscusPlayerVanishEvent newEvent = new HibiscusPlayerVanishEvent(event.getPlayer());
HibiscusPlayerVanishEvent newEvent = new HibiscusPlayerVanishEvent(this, event.getPlayer());
Bukkit.getPluginManager().callEvent(newEvent);
}
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onPlayerShow(@NotNull PlayerShowEvent event) {
HibiscusPlayerUnVanishEvent newEvent = new HibiscusPlayerUnVanishEvent(event.getPlayer());
HibiscusPlayerUnVanishEvent newEvent = new HibiscusPlayerUnVanishEvent(this, event.getPlayer());
Bukkit.getPluginManager().callEvent(newEvent);
}
}

View File

@@ -22,13 +22,13 @@ public class HookSuperVanish extends Hook {
@EventHandler(priority = EventPriority.NORMAL)
public void onPlayerVanish(@NotNull PlayerHideEvent event) {
HibiscusPlayerVanishEvent newEvent = new HibiscusPlayerVanishEvent(event.getPlayer());
HibiscusPlayerVanishEvent newEvent = new HibiscusPlayerVanishEvent(this, event.getPlayer());
Bukkit.getPluginManager().callEvent(newEvent);
}
@EventHandler(priority = EventPriority.NORMAL)
public void onPlayerShow(@NotNull PlayerShowEvent event) {
HibiscusPlayerUnVanishEvent newEvent = new HibiscusPlayerUnVanishEvent(event.getPlayer());
HibiscusPlayerUnVanishEvent newEvent = new HibiscusPlayerUnVanishEvent(this, event.getPlayer());
Bukkit.getPluginManager().callEvent(newEvent);
}
}

View File

@@ -1,7 +1,7 @@
package me.lojosho.hibiscuscommons.hooks.placeholders;
import me.lojosho.hibiscuscommons.hooks.Hook;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import me.lojosho.hibiscuscommons.hooks.Hook;
import java.util.ArrayList;

View File

@@ -5,7 +5,6 @@ import me.lojosho.hibiscuscommons.HibiscusCommonsPlugin;
import org.jetbrains.annotations.Nullable;
import java.lang.reflect.InvocationTargetException;
import java.util.logging.Level;
public class NMSHandlers {

View File

@@ -2,7 +2,6 @@ package me.lojosho.hibiscuscommons.util;
import com.comphenix.protocol.wrappers.WrappedGameProfile;
import com.comphenix.protocol.wrappers.WrappedSignedProperty;
import me.lojosho.hibiscuscommons.nms.NMSHandler;
import me.lojosho.hibiscuscommons.nms.NMSHandlers;
import org.bukkit.Color;
import org.bukkit.entity.Player;