mirror of
https://github.com/FancyInnovations/FancyPlugins.git
synced 2025-12-06 07:43:36 +00:00
fancyholograms-v3: Add HologramTraitAttachedEvent
This commit is contained in:
@@ -0,0 +1,40 @@
|
|||||||
|
package de.oliver.fancyholograms.api.events;
|
||||||
|
|
||||||
|
import de.oliver.fancyholograms.api.hologram.Hologram;
|
||||||
|
import de.oliver.fancyholograms.api.trait.HologramTrait;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.event.HandlerList;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
public class HologramTraitAttachedEvent extends HologramEvent {
|
||||||
|
|
||||||
|
private static final HandlerList handlerList = new HandlerList();
|
||||||
|
|
||||||
|
@NotNull private final HologramTrait trait;
|
||||||
|
private final boolean isDefaultTrait;
|
||||||
|
|
||||||
|
|
||||||
|
public HologramTraitAttachedEvent(@NotNull final Hologram hologram, @NotNull final HologramTrait trait, boolean isDefaultTrait) {
|
||||||
|
super(hologram, !Bukkit.isPrimaryThread());
|
||||||
|
this.trait = trait;
|
||||||
|
this.isDefaultTrait = isDefaultTrait;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static HandlerList getHandlerList() {
|
||||||
|
return handlerList;
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NotNull HologramTrait getTrait() {
|
||||||
|
return trait;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isDefaultTrait() {
|
||||||
|
return isDefaultTrait;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull HandlerList getHandlers() {
|
||||||
|
return handlerList;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -50,6 +50,8 @@ public abstract class HologramTrait {
|
|||||||
|
|
||||||
this.hologram = hologram;
|
this.hologram = hologram;
|
||||||
this.storage = new JDB("plugins/FancyHolograms/data/traits/" + name);
|
this.storage = new JDB("plugins/FancyHolograms/data/traits/" + name);
|
||||||
|
|
||||||
|
onAttach();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -75,7 +77,6 @@ public abstract class HologramTrait {
|
|||||||
* Called when the hologram is registered in the registry.
|
* Called when the hologram is registered in the registry.
|
||||||
*/
|
*/
|
||||||
public void onRegister() {
|
public void onRegister() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package de.oliver.fancyholograms.api.trait;
|
package de.oliver.fancyholograms.api.trait;
|
||||||
|
|
||||||
|
import de.oliver.fancyholograms.api.events.HologramTraitAttachedEvent;
|
||||||
import de.oliver.fancyholograms.api.hologram.Hologram;
|
import de.oliver.fancyholograms.api.hologram.Hologram;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
@@ -15,17 +16,20 @@ public class HologramTraitTrait extends HologramTrait {
|
|||||||
super("trait");
|
super("trait");
|
||||||
this.traits = new ArrayList<>();
|
this.traits = new ArrayList<>();
|
||||||
attachHologram(hologram);
|
attachHologram(hologram);
|
||||||
onAttach();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addTrait(HologramTrait trait) {
|
public void addTrait(HologramTrait trait) {
|
||||||
this.traits.add(trait);
|
if (!new HologramTraitAttachedEvent(hologram, trait, false).callEvent()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
trait.attachHologram(hologram);
|
trait.attachHologram(hologram);
|
||||||
trait.onAttach();
|
this.traits.add(trait);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onAttach() {
|
public void onAttach() {
|
||||||
|
// Attach all default traits to the hologram
|
||||||
Set<Class<? extends HologramTrait>> registeredTraits = api.getTraitRegistry().getRegisteredTraits();
|
Set<Class<? extends HologramTrait>> registeredTraits = api.getTraitRegistry().getRegisteredTraits();
|
||||||
for (Class<? extends HologramTrait> traitClass : registeredTraits) {
|
for (Class<? extends HologramTrait> traitClass : registeredTraits) {
|
||||||
if (!traitClass.isAnnotationPresent(DefaultTrait.class)) {
|
if (!traitClass.isAnnotationPresent(DefaultTrait.class)) {
|
||||||
@@ -34,8 +38,11 @@ public class HologramTraitTrait extends HologramTrait {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
HologramTrait trait = traitClass.getConstructor().newInstance();
|
HologramTrait trait = traitClass.getConstructor().newInstance();
|
||||||
|
if (!new HologramTraitAttachedEvent(hologram, trait, false).callEvent()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
trait.attachHologram(hologram);
|
trait.attachHologram(hologram);
|
||||||
trait.onAttach();
|
|
||||||
this.traits.add(trait);
|
this.traits.add(trait);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.error("Failed to instantiate trait " + traitClass.getName());
|
logger.error("Failed to instantiate trait " + traitClass.getName());
|
||||||
|
|||||||
Reference in New Issue
Block a user