Skip to content

Commit 0b02a9b

Browse files
fancyholograms-v3: Save and load attached traits
1 parent ccc238b commit 0b02a9b

1 file changed

Lines changed: 52 additions & 2 deletions

File tree

plugins/fancyholograms/fh-api/src/main/java/de/oliver/fancyholograms/api/trait/HologramTraitTrait.java

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@
44
import de.oliver.fancyholograms.api.hologram.Hologram;
55
import org.bukkit.entity.Player;
66

7+
import java.io.IOException;
78
import java.util.ArrayList;
89
import java.util.List;
910

1011
@HologramTraitClass(traitName = "trait_trait")
1112
public class HologramTraitTrait extends HologramTrait {
1213

1314
private final List<HologramTrait> traits;
15+
private Configuration configuration;
1416

1517
public HologramTraitTrait(Hologram hologram) {
18+
this.configuration = new Configuration(new ArrayList<>());
1619
this.traits = new ArrayList<>();
1720
attachHologram(hologram);
1821
}
@@ -24,6 +27,13 @@ public void addTrait(HologramTrait trait) {
2427

2528
trait.attachHologram(hologram);
2629
this.traits.add(trait);
30+
this.configuration.traits().add(trait.getName());
31+
try {
32+
storage.set(hologram.getData().getName(), configuration);
33+
} catch (IOException e) {
34+
logger.error("Failed to save configuration for HologramTraitTrait");
35+
logger.error(e);
36+
}
2737
}
2838

2939
@Override
@@ -37,19 +47,53 @@ public void onAttach() {
3747

3848
try {
3949
HologramTrait trait = ti.clazz().getConstructor().newInstance();
40-
if (!new HologramTraitAttachedEvent(hologram, trait, false).callEvent()) {
50+
if (!new HologramTraitAttachedEvent(hologram, trait, true).callEvent()) {
4151
continue;
4252
}
4353

4454
trait.attachHologram(hologram);
4555
this.traits.add(trait);
4656
} catch (Exception e) {
47-
logger.error("Failed to instantiate trait " + ti.name());
57+
logger.error("Failed to instantiate default trait " + ti.name());
4858
logger.error(e);
4959
}
5060

5161
logger.debug("Attached default trait " + ti.name() + " to hologram " + hologram.getData().getName());
5262
}
63+
64+
// Attach all traits that are already attached to the hologram
65+
try {
66+
configuration = storage.get(hologram.getData().getName(), Configuration.class);
67+
} catch (IOException e) {
68+
logger.error("Failed to load configuration for HologramTraitTrait");
69+
logger.error(e);
70+
return;
71+
}
72+
if (configuration == null) {
73+
return;
74+
}
75+
76+
for (String traitName : configuration.traits()) {
77+
HologramTraitRegistry.TraitInfo traitInfo = api.getTraitRegistry().getTrait(traitName);
78+
if (traitInfo == null) {
79+
logger.warn("Trait " + traitName + " is not registered");
80+
continue;
81+
}
82+
83+
try {
84+
HologramTrait trait = traitInfo.clazz().getConstructor().newInstance();
85+
86+
if (!new HologramTraitAttachedEvent(hologram, trait, false).callEvent()) {
87+
return;
88+
}
89+
90+
trait.attachHologram(hologram);
91+
this.traits.add(trait);
92+
} catch (Exception e) {
93+
logger.error("Failed to instantiate trait " + traitName);
94+
logger.error(e);
95+
}
96+
}
5397
}
5498

5599
@Override
@@ -93,4 +137,10 @@ public void save() {
93137
trait.save();
94138
}
95139
}
140+
141+
record Configuration(
142+
List<String> traits
143+
) {
144+
145+
}
96146
}

0 commit comments

Comments
 (0)