mirror of
https://github.com/FancyInnovations/FancyPlugins.git
synced 2025-12-06 07:43:36 +00:00
fancyholograms-v3: Load and save config for multiple pages
This commit is contained in:
@@ -1,29 +1,33 @@
|
||||
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;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@ApiStatus.Experimental
|
||||
@DefaultTrait
|
||||
public class MultiplePagesTrait extends HologramTrait {
|
||||
|
||||
private static final int NEXT_PAGE_DELAY_SECONDS = 5;
|
||||
private static final Configuration DEFAULT_CONFIG = new Configuration(
|
||||
Mode.CYCLE,
|
||||
1000,
|
||||
List.of(
|
||||
new Page(List.of("Page 1", "Line 1", "Line 2")),
|
||||
new Page(List.of("Page 2", "Line 1", "Line 2"))
|
||||
));
|
||||
|
||||
private final List<Page> pages;
|
||||
private Configuration config;
|
||||
private int currentPageIdx;
|
||||
|
||||
public MultiplePagesTrait() {
|
||||
super("multiple_pages");
|
||||
this.pages = new ArrayList<>();
|
||||
this.currentPageIdx = 0;
|
||||
|
||||
this.pages.add(new Page(List.of("Page 1", "Line 1", "Line 2")));
|
||||
this.pages.add(new Page(List.of("Page 2", "Line 1", "Line 2")));
|
||||
this.pages.add(new Page(List.of("Page 3", "Line 1", "Line 2")));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -32,22 +36,56 @@ public class MultiplePagesTrait extends HologramTrait {
|
||||
throw new IllegalStateException("Hologram must be text hologram to use MultiplePagesTrait");
|
||||
}
|
||||
|
||||
load();
|
||||
|
||||
hologramThread.scheduleWithFixedDelay(() -> {
|
||||
Page currentPage = pages.get(currentPageIdx);
|
||||
Page currentPage = config.pages().get(currentPageIdx);
|
||||
td.setText(new ArrayList<>(currentPage.lines()));
|
||||
|
||||
currentPageIdx = (currentPageIdx + 1) % pages.size(); // cycle through pages
|
||||
}, 0, NEXT_PAGE_DELAY_SECONDS, TimeUnit.SECONDS);
|
||||
currentPageIdx = switch (config.mode()) {
|
||||
case CYCLE -> (currentPageIdx + 1) % config.pages().size();
|
||||
case RANDOM ->(int) (Math.random() * config.pages().size());
|
||||
default -> currentPageIdx;
|
||||
};
|
||||
}, 0, config.cycleDelay(), TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save() {
|
||||
//TODO save pages to data
|
||||
try {
|
||||
storage.set(hologram.getData().getName(), config);
|
||||
} catch (IOException e) {
|
||||
logger.error("Failed to save configuration for MultiplePagesTrait");
|
||||
logger.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
//TODO load pages from data
|
||||
try {
|
||||
config = storage.get(hologram.getData().getName(), Configuration.class);
|
||||
} catch (IOException e) {
|
||||
logger.error("Failed to load configuration for MultiplePagesTrait");
|
||||
logger.error(e);
|
||||
}
|
||||
if (config == null) {
|
||||
config = DEFAULT_CONFIG;
|
||||
save();
|
||||
}
|
||||
}
|
||||
|
||||
enum Mode {
|
||||
MANUAL,
|
||||
CYCLE,
|
||||
RANDOM,
|
||||
}
|
||||
|
||||
record Configuration(
|
||||
Mode mode,
|
||||
long cycleDelay,
|
||||
List<Page> pages
|
||||
) {
|
||||
|
||||
}
|
||||
|
||||
record Page(List<String> lines) {
|
||||
|
||||
Reference in New Issue
Block a user