Files
src
build.gradle
settings.gradle
Code
5 463
src/main/java/com/kodari/hologram/HologramPlugin.java
package com.kodari.hologram;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Color;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Display;
import org.bukkit.entity.Player;
import org.bukkit.entity.TextDisplay;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.world.ChunkLoadEvent;
import org.bukkit.event.world.WorldLoadEvent;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.Transformation;
import org.joml.Vector3f;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
public final class HologramPlugin extends JavaPlugin implements TabExecutor, Listener {
private static final Pattern HEX_COLOR = Pattern.compile("&#([A-Fa-f0-9]{6})");
private final Map<String, List<TextDisplay>> displays = new HashMap<>();
private final Map<String, ConfigurationSection> holograms = new HashMap<>();
private final Map<String, String> externalNames = new HashMap<>();
private YamlConfiguration data;
private File dataFile;
private String externalFingerprint = "";
@Override
public void onEnable() {
getDataFolder().mkdirs();
saveDefaultConfig();
File directory = new File(getDataFolder(), "holograms");
directory.mkdirs();
dataFile = new File(directory, "hologramlar.yml");
if (!dataFile.exists()) {
File old = new File(getDataFolder(), "hologramlar.yml");
if (!old.isFile()) old = new File(getDataFolder().getParentFile(), "KodariHologram/hologramlar.yml");
if (old.isFile()) {
try {
Files.copy(old.toPath(), dataFile.toPath());
} catch (IOException e) {
getLogger().severe("Eski hologramlar taşınamadı: " + e.getMessage());
}
}
}
data = YamlConfiguration.loadConfiguration(dataFile);
getCommand("hologram").setExecutor(this);
getCommand("hologram").setTabCompleter(this);
Bukkit.getPluginManager().registerEvents(this, this);
reloadDisplays();
Bukkit.getScheduler().runTask(this, this::reloadDisplays);
Bukkit.getScheduler().runTaskTimer(this, () -> {
String fingerprint = fingerprint();
if (!fingerprint.equals(externalFingerprint)) {
reloadDisplays();
}
}, 100L, 100L);
}
@Override
public void onDisable() {
clearDisplays();
}
@EventHandler
public void onWorldLoad(WorldLoadEvent event) {
Bukkit.getScheduler().runTask(this, this::reloadDisplays);
}
@EventHandler
public void onChunkLoad(ChunkLoadEvent event) {
World world = event.getWorld();
int chunkX = event.getChunk().getX();
int chunkZ = event.getChunk().getZ();
Bukkit.getScheduler().runTask(this, () -> {
if (!world.isChunkLoaded(chunkX, chunkZ)) return;
for (Map.Entry<String, ConfigurationSection> entry : holograms.entrySet()) {
Location loc = location(entry.getValue());
if (loc == null || !world.equals(loc.getWorld())
|| (loc.getBlockX() >> 4) != chunkX || (loc.getBlockZ() >> 4) != chunkZ) continue;
List<TextDisplay> display = displays.get(entry.getKey());
if (display == null || display.stream().anyMatch(entity -> !entity.isValid())) spawn(entry.getKey(), entry.getValue());
}
});
}
private void clearDisplays() {
displays.values().forEach(list -> list.forEach(TextDisplay::remove));
displays.clear();
}
private void save() {
try {
data.save(dataFile);
} catch (IOException e) {
getLogger().severe("Hologramlar kaydedilemedi: " + e.getMessage());
}
}
private void reloadDisplays() {
clearDisplays();
holograms.clear();
externalNames.clear();
data = YamlConfiguration.loadConfiguration(dataFile);
ConfigurationSection own = data.getConfigurationSection("hologramlar");
if (own != null) {
for (String name : own.getKeys(false)) {
ConfigurationSection section = own.getConfigurationSection(name);
if (section != null) spawn("yerel:" + name, section);
}
}
for (File file : externalFiles()) {
YamlConfiguration yaml = YamlConfiguration.loadConfiguration(file);
if (yaml.contains("location") || (yaml.contains("world") && yaml.contains("x"))) {
String id = "dis:" + file.getAbsolutePath();
externalNames.put(id, file.getName().replaceFirst("(?i)\\.(yml|yaml)$", ""));
spawn(id, yaml);
} else {
ConfigurationSection root = yaml.getConfigurationSection("holograms");
if (root == null) root = yaml;
for (String key : root.getKeys(false)) {
ConfigurationSection section = root.getConfigurationSection(key);
if (section != null) {
String id = "dis:" + file.getAbsolutePath() + ":" + key;
externalNames.put(id, key);
spawn(id, section);
}
}
}
}
externalFingerprint = fingerprint();
}
private List<File> externalFiles() {
File source = new File(getDataFolder(), "holograms");
if (source.isFile()) return Collections.singletonList(source);
if (!source.isDirectory()) return Collections.emptyList();
try (Stream<Path> paths = Files.walk(source.toPath())) {
return paths.filter(Files::isRegularFile)
.filter(path -> !path.toFile().equals(dataFile))
.filter(path -> path.toString().toLowerCase(Locale.ROOT).matches(".*\\.(yml|yaml)$"))
.sorted().map(Path::toFile).toList();
} catch (IOException e) {
getLogger().warning("Dış hologram klasörü okunamadı: " + e.getMessage());
return Collections.emptyList();
}
}
private String fingerprint() {
StringBuilder result = new StringBuilder();
for (File file : externalFiles()) {
try {
result.append(file.getAbsolutePath()).append(':').append(java.util.Base64.getEncoder().encodeToString(java.security.MessageDigest.getInstance("SHA-256").digest(Files.readAllBytes(file.toPath())))).append(';');
} catch (IOException | java.security.NoSuchAlgorithmException e) {
getLogger().warning("Hologram dosyası okunamadı: " + file + ": " + e.getMessage());
}
}
return result.toString();
}
private Location location(ConfigurationSection section) {
if (section.isLocation("location")) return section.getLocation("location");
ConfigurationSection loc = section.getConfigurationSection("location");
if (loc == null) loc = section;
String worldName = loc.getString("world");
if (worldName != null && loc.contains("x") && loc.contains("y") && loc.contains("z")) {
World world = Bukkit.getWorld(worldName);
if (world != null) return new Location(world, loc.getDouble("x"), loc.getDouble("y"), loc.getDouble("z"));
}
String value = section.getString("location");
if (value != null) {
String[] parts = value.split("[;,:]");
if (parts.length >= 4) {
World world = Bukkit.getWorld(parts[0].trim());
try {
if (world != null) return new Location(world, Double.parseDouble(parts[1].trim()), Double.parseDouble(parts[2].trim()), Double.parseDouble(parts[3].trim()));
} catch (NumberFormatException ignored) {
return null;
}
}
