mirror of
https://github.com/FancyInnovations/FancyPlugins.git
synced 2025-12-06 07:43:36 +00:00
fancyholograms-v3: Save and load attached traits
This commit is contained in:
@@ -4,6 +4,7 @@ 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;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -11,8 +12,10 @@ import java.util.List;
|
|||||||
public class HologramTraitTrait extends HologramTrait {
|
public class HologramTraitTrait extends HologramTrait {
|
||||||
|
|
||||||
private final List<HologramTrait> traits;
|
private final List<HologramTrait> traits;
|
||||||
|
private Configuration configuration;
|
||||||
|
|
||||||
public HologramTraitTrait(Hologram hologram) {
|
public HologramTraitTrait(Hologram hologram) {
|
||||||
|
this.configuration = new Configuration(new ArrayList<>());
|
||||||
this.traits = new ArrayList<>();
|
this.traits = new ArrayList<>();
|
||||||
attachHologram(hologram);
|
attachHologram(hologram);
|
||||||
}
|
}
|
||||||
@@ -24,6 +27,13 @@ public class HologramTraitTrait extends HologramTrait {
|
|||||||
|
|
||||||
trait.attachHologram(hologram);
|
trait.attachHologram(hologram);
|
||||||
this.traits.add(trait);
|
this.traits.add(trait);
|
||||||
|
this.configuration.traits().add(trait.getName());
|
||||||
|
try {
|
||||||
|
storage.set(hologram.getData().getName(), configuration);
|
||||||
|
} catch (IOException e) {
|
||||||
|
logger.error("Failed to save configuration for HologramTraitTrait");
|
||||||
|
logger.error(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -37,19 +47,53 @@ public class HologramTraitTrait extends HologramTrait {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
HologramTrait trait = ti.clazz().getConstructor().newInstance();
|
HologramTrait trait = ti.clazz().getConstructor().newInstance();
|
||||||
if (!new HologramTraitAttachedEvent(hologram, trait, false).callEvent()) {
|
if (!new HologramTraitAttachedEvent(hologram, trait, true).callEvent()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
trait.attachHologram(hologram);
|
trait.attachHologram(hologram);
|
||||||
this.traits.add(trait);
|
this.traits.add(trait);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.error("Failed to instantiate trait " + ti.name());
|
logger.error("Failed to instantiate default trait " + ti.name());
|
||||||
logger.error(e);
|
logger.error(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.debug("Attached default trait " + ti.name() + " to hologram " + hologram.getData().getName());
|
logger.debug("Attached default trait " + ti.name() + " to hologram " + hologram.getData().getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Attach all traits that are already attached to the hologram
|
||||||
|
try {
|
||||||
|
configuration = storage.get(hologram.getData().getName(), Configuration.class);
|
||||||
|
} catch (IOException e) {
|
||||||
|
logger.error("Failed to load configuration for HologramTraitTrait");
|
||||||
|
logger.error(e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (configuration == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (String traitName : configuration.traits()) {
|
||||||
|
HologramTraitRegistry.TraitInfo traitInfo = api.getTraitRegistry().getTrait(traitName);
|
||||||
|
if (traitInfo == null) {
|
||||||
|
logger.warn("Trait " + traitName + " is not registered");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
HologramTrait trait = traitInfo.clazz().getConstructor().newInstance();
|
||||||
|
|
||||||
|
if (!new HologramTraitAttachedEvent(hologram, trait, false).callEvent()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
trait.attachHologram(hologram);
|
||||||
|
this.traits.add(trait);
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("Failed to instantiate trait " + traitName);
|
||||||
|
logger.error(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -93,4 +137,10 @@ public class HologramTraitTrait extends HologramTrait {
|
|||||||
trait.save();
|
trait.save();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
record Configuration(
|
||||||
|
List<String> traits
|
||||||
|
) {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user