From 52aa9beb60a803139b6f9069b49068a3352b7382 Mon Sep 17 00:00:00 2001 From: M_Viper Date: Fri, 22 Aug 2025 11:59:56 +0000 Subject: [PATCH] Dateien nach "src/main/java/com/viper/PlaceholderSIGN/sign" hochladen --- .../PlaceholderSIGN/sign/PlaceholderSign.java | 138 ++++++++++++++++++ .../PlaceholderSIGN/sign/SignManager.java | 103 +++++++++++++ 2 files changed, 241 insertions(+) create mode 100644 src/main/java/com/viper/PlaceholderSIGN/sign/PlaceholderSign.java create mode 100644 src/main/java/com/viper/PlaceholderSIGN/sign/SignManager.java diff --git a/src/main/java/com/viper/PlaceholderSIGN/sign/PlaceholderSign.java b/src/main/java/com/viper/PlaceholderSIGN/sign/PlaceholderSign.java new file mode 100644 index 0000000..582f0f7 --- /dev/null +++ b/src/main/java/com/viper/PlaceholderSIGN/sign/PlaceholderSign.java @@ -0,0 +1,138 @@ +package com.viper.PlaceholderSIGN.sign; + +import com.viper.PlaceholderSIGN.Main; +import com.viper.PlaceholderSIGN.util.HexUtil; +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import me.clip.placeholderapi.PlaceholderAPI; +import net.md_5.bungee.api.ChatColor; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.block.Sign; +import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; +import org.bukkit.configuration.file.YamlConfiguration; + +public class PlaceholderSign { + private String id; + private String[] linesRaw = new String[0]; + private int[] linesScroll = new int[] { 0, 0, 0, 0 }; + private Sign s; + private List inside = new ArrayList<>(); + private Map cachedLines = new HashMap<>(); + public boolean deleted = false; + + public PlaceholderSign(String id, String[] linesRaw, Sign s) { + this.id = id; + this.linesRaw = linesRaw; + this.s = s; + Bukkit.getScheduler().scheduleSyncRepeatingTask((Plugin) Main.getInstance(), () -> { + if (this.deleted) return; + for (Player p : this.inside) { + sendUpdate(p, false); + } + }, Main.getInstance().getUpdateInterval(), Main.getInstance().getUpdateInterval()); + } + + public String getID() { + return this.id; + } + + public void save() { + File f = new File(Main.getInstance().getDataFolder(), "signs/" + this.id + ".yml"); + Location l = this.s.getLocation(); + YamlConfiguration yamlConfiguration = YamlConfiguration.loadConfiguration(f); + yamlConfiguration.set("rawLines", new ArrayList<>(Arrays.asList(this.linesRaw))); + yamlConfiguration.set("location", l.getWorld().getName() + "," + l.getBlockX() + "," + l.getBlockY() + "," + l.getBlockZ()); + try { + if (!f.exists()) { + f.createNewFile(); + } + yamlConfiguration.save(f); + } catch (IOException e) { + // Stille Behandlung der IOException + } + } + + public boolean isInside(Player p) { + if (p.getWorld().equals(this.s.getWorld()) && + this.s.getLocation().distanceSquared(p.getLocation()) <= Main.getInstance().getSignMinimumDistance() * Main.getInstance().getSignMinimumDistance()) { + if (!this.inside.contains(p)) { + this.inside.add(p); + cachedLines.put(p, new String[4]); + } + return true; + } + this.inside.remove(p); + cachedLines.remove(p); + return false; + } + + public Sign getSign() { + return this.s; + } + + public String[] getLinesRaw() { + return this.linesRaw; + } + + public void setLinesRaw(String[] lines) { + this.linesRaw = lines; + cachedLines.clear(); + } + + public void sendUpdate(Player p, boolean force) { + if (!this.s.getLocation().getChunk().isLoaded()) return; + + String[] cached = cachedLines.getOrDefault(p, new String[4]); + if (!force && cached[0] != null && cached[1] != null && cached[2] != null && cached[3] != null) { + p.sendSignChange(this.s.getLocation(), cached); + return; + } + + int scrolled = 0; + String[] temp = this.linesRaw; + List te = new ArrayList<>(); + for (int i = 0; i < temp.length; i++) { + String t = temp[i] == null ? "" : temp[i]; + t = ChatColor.translateAlternateColorCodes('&', t); + if (t.contains("%") && Main.getInstance().hasPAPI()) { + t = PlaceholderAPI.setPlaceholders(p, t); + } + if (Main.getInstance().hasRGB()) { + t = HexUtil.replaceHexColors('&', t); + } + if (t.length() > 16) { + int current = this.linesScroll[scrolled] + 1; + if (current >= t.length()) { + current = 0; + } + this.linesScroll[scrolled] = current; + int end = current + 15; + if (end > t.length()) { + end = t.length(); + } + StringBuilder f = new StringBuilder(); + for (int j = current; j < end && j < t.length(); j++) { + f.append(t.charAt(j)); + } + t = f.toString(); + } + scrolled++; + te.add(t); + } + String[] se = te.toArray(new String[0]); + cachedLines.put(p, se); + p.sendSignChange(this.s.getLocation(), se); + } + + public void forceUpdate(Player p) { + cachedLines.remove(p); // Cache explizit löschen + sendUpdate(p, true); + } +} \ No newline at end of file diff --git a/src/main/java/com/viper/PlaceholderSIGN/sign/SignManager.java b/src/main/java/com/viper/PlaceholderSIGN/sign/SignManager.java new file mode 100644 index 0000000..081902f --- /dev/null +++ b/src/main/java/com/viper/PlaceholderSIGN/sign/SignManager.java @@ -0,0 +1,103 @@ +package com.viper.PlaceholderSIGN.sign; + +import com.viper.PlaceholderSIGN.Main; +import java.io.File; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Random; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.block.BlockState; +import org.bukkit.block.Sign; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.entity.Player; + +public class SignManager { + private List ps = new ArrayList<>(); + private Random r; + + public List getSigns() { + return this.ps; + } + + public SignManager() { + this.r = new Random(); + File f = new File(Main.getInstance().getDataFolder(), "signs"); + if (!f.exists()) { + f.mkdir(); + } + for (File s : f.listFiles()) { + YamlConfiguration yamlConfiguration = YamlConfiguration.loadConfiguration(s); + String[] l = yamlConfiguration.getString("location").split(","); + String[] q = new String[yamlConfiguration.getStringList("rawLines").size()]; + Location signLoc = new Location(Bukkit.getWorld(l[0]), Double.parseDouble(l[1]), Double.parseDouble(l[2]), Double.parseDouble(l[3])); + if (signLoc != null && signLoc.getWorld() != null) { + BlockState temp = signLoc.getWorld().getBlockAt(signLoc).getState(); + if (temp instanceof Sign) { + this.ps.add(new PlaceholderSign(s.getName().replace(".yml", ""), yamlConfiguration.getStringList("rawLines").toArray(q), (Sign) temp)); + } + } + } + } + + private String genID() { + StringBuilder f = new StringBuilder(); + for (int i = 0; i < 14; i++) { + f.append(this.r.nextInt(9)); + } + return f.toString(); + } + + public void createSign(Sign s, List lines) { + if (!isPlaceholderSign(s)) { + while (lines.size() != 4) { + lines.add(""); + } + this.ps.add(new PlaceholderSign(genID(), lines.toArray(new String[0]), s)); + } + } + + public boolean isPlaceholderSign(Sign s) { + return (getPlaceholderSign(s) != null); + } + + public PlaceholderSign getPlaceholderSign(Sign s) { + for (PlaceholderSign p : this.ps) { + if (p.getSign().getLocation().equals(s.getLocation())) { + return p; + } + } + return null; + } + + public void saveAll() { + for (PlaceholderSign p : this.ps) { + p.save(); + } + } + + public void isInside(Player p) { + for (PlaceholderSign pz : this.ps) { + pz.isInside(p); + } + } + + public void removeSign(Sign s) { + if (isPlaceholderSign(s)) { + PlaceholderSign signs = getPlaceholderSign(s); + signs.deleted = true; + this.ps.remove(signs); + File f = new File(Main.getInstance().getDataFolder(), "signs/" + signs.getID() + ".yml"); + f.delete(); + } + } + + public void forceUpdate(Player p) { + for (PlaceholderSign pz : this.ps) { + if (pz.isInside(p)) { + pz.forceUpdate(p); + } + } + } +} \ No newline at end of file