Update from Git Manager GUI

This commit is contained in:
2026-02-23 13:06:59 +01:00
parent c8d4578fa6
commit 02811bafbd
9 changed files with 1050 additions and 174 deletions

View File

@@ -0,0 +1,181 @@
package de.ticketsystem.manager;
import de.ticketsystem.TicketPlugin;
import de.ticketsystem.model.FaqEntry;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File;
import java.io.IOException;
import java.util.*;
/**
* Manages FAQ entries stored in faqs.yml.
*
* Admins can add, edit and delete FAQs in-game.
* All changes are saved immediately to faqs.yml.
*
* faqs.yml layout:
*
* faqs:
* 1:
* question: "Wie erstelle ich ein Ticket?"
* answer: "Nutze /ticket create [Kategorie] [Beschreibung]."
* 2:
* question: "..."
* answer: "..."
*/
public class FaqManager {
private final TicketPlugin plugin;
private final File faqFile;
private YamlConfiguration faqConfig;
private final List<FaqEntry> entries = new ArrayList<>();
private int nextId = 1;
public FaqManager(TicketPlugin plugin) {
this.plugin = plugin;
this.faqFile = new File(plugin.getDataFolder(), "faqs.yml");
load();
}
// ─────────────────────────── Loading & Saving ───────────────────────────
private void load() {
entries.clear();
nextId = 1;
if (!faqFile.exists()) {
try {
faqFile.getParentFile().mkdirs();
faqFile.createNewFile();
} catch (IOException e) {
plugin.getLogger().severe("[FaqManager] Konnte faqs.yml nicht erstellen: " + e.getMessage());
}
faqConfig = new YamlConfiguration();
loadDefaults();
save();
return;
}
faqConfig = YamlConfiguration.loadConfiguration(faqFile);
ConfigurationSection section = faqConfig.getConfigurationSection("faqs");
if (section != null) {
for (String key : section.getKeys(false)) {
try {
int id = Integer.parseInt(key);
String question = faqConfig.getString("faqs." + key + ".question", "");
String answer = faqConfig.getString("faqs." + key + ".answer", "");
if (!question.isBlank() && !answer.isBlank()) {
entries.add(new FaqEntry(id, question, answer));
if (id >= nextId) nextId = id + 1;
}
} catch (NumberFormatException ignored) {}
}
}
entries.sort(Comparator.comparingInt(FaqEntry::getId));
if (plugin.isDebug()) {
plugin.getLogger().info("[FaqManager] " + entries.size() + " FAQ(s) geladen.");
}
}
/** Writes the example FAQs into a freshly created faqs.yml. */
private void loadDefaults() {
writeEntry(1, "Wie erstelle ich ein Ticket?",
"Nutze den Befehl /ticket create [Kategorie] [Prio][Beschreibung] um ein neues Ticket zu erstellen.");
writeEntry(2, "Wie lange dauert die Bearbeitung?",
"Unser Support-Team bearbeitet Tickets so schnell wie möglich. Bitte habe etwas Geduld.");
writeEntry(3, "Kann ich mein Ticket löschen?",
"Ja! Öffne /ticket list und klicke auf dein Ticket, um es aus der Übersicht zu entfernen.");
writeEntry(4, "Wie kann ich meinen Support bewerten?",
"Nach dem Schließen eines Tickets kannst du mit /ticket rate <ID> good/bad eine Bewertung abgeben.");
nextId = 5;
// Sync entries list with what we just wrote
entries.add(new FaqEntry(1, "Wie erstelle ich ein Ticket?",
"Nutze den Befehl /ticket create [Kategorie] [Beschreibung] um ein neues Ticket zu erstellen."));
entries.add(new FaqEntry(2, "Wie lange dauert die Bearbeitung?",
"Unser Support-Team bearbeitet Tickets so schnell wie möglich. Bitte habe etwas Geduld."));
entries.add(new FaqEntry(3, "Kann ich mein Ticket löschen?",
"Ja! Öffne /ticket list und klicke auf dein Ticket, um es aus der Übersicht zu entfernen."));
entries.add(new FaqEntry(4, "Wie kann ich meinen Support bewerten?",
"Nach dem Schließen eines Tickets kannst du mit /ticket rate <ID> good/bad eine Bewertung abgeben."));
}
private void writeEntry(int id, String question, String answer) {
faqConfig.set("faqs." + id + ".question", question);
faqConfig.set("faqs." + id + ".answer", answer);
}
private void save() {
try {
faqConfig.save(faqFile);
} catch (IOException e) {
plugin.getLogger().severe("[FaqManager] Konnte faqs.yml nicht speichern: " + e.getMessage());
}
}
// ─────────────────────────── Public API ────────────────────────────────
/** Returns an unmodifiable view of all FAQ entries in ID order. */
public List<FaqEntry> getAll() {
return Collections.unmodifiableList(entries);
}
/** Looks up an entry by its numeric ID. Returns null if not found. */
public FaqEntry getById(int id) {
return entries.stream().filter(e -> e.getId() == id).findFirst().orElse(null);
}
/**
* Adds a new FAQ entry and saves immediately.
*
* @param question The question text.
* @param answer The answer text.
* @return The newly created {@link FaqEntry}.
*/
public FaqEntry add(String question, String answer) {
int id = nextId++;
FaqEntry entry = new FaqEntry(id, question, answer);
entries.add(entry);
writeEntry(id, question, answer);
save();
return entry;
}
/**
* Edits an existing FAQ entry and saves immediately.
*
* @return true if the entry was found and updated, false otherwise.
*/
public boolean edit(int id, String question, String answer) {
FaqEntry entry = getById(id);
if (entry == null) return false;
entry.setQuestion(question);
entry.setAnswer(answer);
writeEntry(id, question, answer);
save();
return true;
}
/**
* Deletes a FAQ entry and saves immediately.
*
* @return true if the entry was found and deleted, false otherwise.
*/
public boolean delete(int id) {
FaqEntry entry = getById(id);
if (entry == null) return false;
entries.remove(entry);
faqConfig.set("faqs." + id, null);
save();
return true;
}
/** Reloads FAQs from faqs.yml without restarting the server. */
public void reload() {
load();
}
}