mirror of
https://github.com/FancyInnovations/FancyPlugins.git
synced 2025-12-06 07:43:36 +00:00
fancyholograms-v3: Add FileContentTrait for dynamic text updates from file
This commit is contained in:
@@ -29,6 +29,7 @@ import de.oliver.fancyholograms.storage.HologramStorage;
|
||||
import de.oliver.fancyholograms.storage.StorageMigrator;
|
||||
import de.oliver.fancyholograms.storage.json.JsonStorage;
|
||||
import de.oliver.fancyholograms.trait.HologramTraitRegistryImpl;
|
||||
import de.oliver.fancyholograms.trait.builtin.FileContentTrait;
|
||||
import de.oliver.fancyholograms.trait.builtin.MultiplePagesTrait;
|
||||
import de.oliver.fancyholograms.util.PluginUtils;
|
||||
import de.oliver.fancylib.FancyLib;
|
||||
@@ -190,6 +191,7 @@ public final class FancyHologramsPlugin extends JavaPlugin implements FancyHolog
|
||||
metrics.registerLegacy();
|
||||
|
||||
traitRegistry.register(MultiplePagesTrait.class);
|
||||
traitRegistry.register(FileContentTrait.class);
|
||||
|
||||
new StorageMigrator().migrate();
|
||||
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
package de.oliver.fancyholograms.trait.builtin;
|
||||
|
||||
import de.oliver.fancyholograms.api.data.TextHologramData;
|
||||
import de.oliver.fancyholograms.api.trait.HologramTrait;
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.List;
|
||||
|
||||
@ApiStatus.Experimental
|
||||
public class FileContentTrait extends HologramTrait {
|
||||
|
||||
private static final Configuration DEFAULT_CONFIG = new Configuration(
|
||||
"server.properties",
|
||||
1000
|
||||
);
|
||||
|
||||
private Configuration config;
|
||||
|
||||
public FileContentTrait() {
|
||||
super("file_content");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAttach() {
|
||||
if (!(hologram.getData() instanceof TextHologramData)) {
|
||||
throw new IllegalStateException("Hologram must be text hologram to use FileContentTrait");
|
||||
}
|
||||
|
||||
load();
|
||||
|
||||
if (config.refreshInterval > 0) {
|
||||
hologramThread.scheduleWithFixedDelay(
|
||||
this::updateHologram,
|
||||
0,
|
||||
config.refreshInterval(), java.util.concurrent.TimeUnit.MILLISECONDS);
|
||||
} else {
|
||||
updateHologram();
|
||||
}
|
||||
}
|
||||
|
||||
public void updateHologram() {
|
||||
Path path = Paths.get(config.filePath());
|
||||
if (!path.toFile().exists()) {
|
||||
logger.warn("File does not exist: " + config.filePath());
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
String content = Files.readString(path);
|
||||
List<String> lines = List.of(content.split("\\r?\\n"));
|
||||
((TextHologramData) hologram.getData()).setText(lines);
|
||||
} catch (IOException e) {
|
||||
logger.error("Failed to read file content of: " + config.filePath());
|
||||
logger.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
try {
|
||||
config = storage.get(hologram.getData().getName(), Configuration.class);
|
||||
} catch (IOException e) {
|
||||
logger.error("Failed to load configuration for FileContentTrait");
|
||||
logger.error(e);
|
||||
}
|
||||
if (config == null) {
|
||||
config = DEFAULT_CONFIG;
|
||||
save();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save() {
|
||||
try {
|
||||
storage.set(hologram.getData().getName(), config);
|
||||
} catch (IOException e) {
|
||||
logger.error("Failed to save configuration for FileContentTrait");
|
||||
logger.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
record Configuration(
|
||||
String filePath,
|
||||
long refreshInterval
|
||||
) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
package de.oliver.fancyholograms.trait.builtin;
|
||||
|
||||
import de.oliver.fancyholograms.api.data.TextHologramData;
|
||||
import de.oliver.fancyholograms.api.trait.DefaultTrait;
|
||||
import de.oliver.fancyholograms.api.trait.HologramTrait;
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
|
||||
@@ -11,7 +10,6 @@ import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@ApiStatus.Experimental
|
||||
@DefaultTrait
|
||||
public class MultiplePagesTrait extends HologramTrait {
|
||||
|
||||
private static final Configuration DEFAULT_CONFIG = new Configuration(
|
||||
|
||||
Reference in New Issue
Block a user