Update from Git Manager GUI
This commit is contained in:
134
src/main/java/de/nexuslobby/modules/hologram/HologramModule.java
Normal file
134
src/main/java/de/nexuslobby/modules/hologram/HologramModule.java
Normal file
@@ -0,0 +1,134 @@
|
||||
package de.nexuslobby.modules.hologram;
|
||||
|
||||
import de.nexuslobby.NexusLobby;
|
||||
import de.nexuslobby.api.Module;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerChangedWorldEvent;
|
||||
import org.bukkit.event.player.PlayerInteractEntityEvent;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class HologramModule implements Module, Listener {
|
||||
|
||||
private final Map<String, NexusHologram> holograms = new ConcurrentHashMap<>();
|
||||
private File file;
|
||||
private FileConfiguration config;
|
||||
|
||||
@Override
|
||||
public String getName() { return "Holograms"; }
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
loadConfig();
|
||||
loadHolograms();
|
||||
Bukkit.getPluginManager().registerEvents(this, NexusLobby.getInstance());
|
||||
|
||||
Bukkit.getScheduler().runTaskTimer(NexusLobby.getInstance(), () -> {
|
||||
for (Player player : Bukkit.getOnlinePlayers()) {
|
||||
holograms.values().forEach(h -> h.renderForPlayer(player));
|
||||
}
|
||||
}, 20L, 5L);
|
||||
}
|
||||
|
||||
private void loadConfig() {
|
||||
file = new File(NexusLobby.getInstance().getDataFolder(), "holograms.yml");
|
||||
config = YamlConfiguration.loadConfiguration(file);
|
||||
}
|
||||
|
||||
private void loadHolograms() {
|
||||
holograms.values().forEach(NexusHologram::removeAll);
|
||||
holograms.clear();
|
||||
for (String id : config.getKeys(false)) {
|
||||
World world = Bukkit.getWorld(config.getString(id + ".world", "world"));
|
||||
if (world == null) continue;
|
||||
Location loc = new Location(world, config.getDouble(id + ".x"), config.getDouble(id + ".y"), config.getDouble(id + ".z"));
|
||||
|
||||
List<String> pages;
|
||||
if (config.isList(id + ".text")) {
|
||||
pages = config.getStringList(id + ".text");
|
||||
} else {
|
||||
pages = new ArrayList<>();
|
||||
pages.add(config.getString(id + ".text", "No Text"));
|
||||
}
|
||||
|
||||
holograms.put(id, new NexusHologram(id, loc, pages));
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onInteract(PlayerInteractEntityEvent event) {
|
||||
// Wir prüfen, ob auf ein Interaction-Entity geklickt wurde
|
||||
for (NexusHologram holo : holograms.values()) {
|
||||
if (holo.isInteractionEntity(event.getRightClicked().getUniqueId())) {
|
||||
holo.nextPage(event.getPlayer());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onQuit(PlayerQuitEvent event) {
|
||||
holograms.values().forEach(h -> h.removeForPlayer(event.getPlayer()));
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onWorldChange(PlayerChangedWorldEvent event) {
|
||||
holograms.values().forEach(h -> h.removeForPlayer(event.getPlayer()));
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onJoin(PlayerJoinEvent event) {
|
||||
// Cleanup alter Entities für den Joiner
|
||||
Bukkit.getScheduler().runTaskLater(NexusLobby.getInstance(), () -> {
|
||||
event.getPlayer().getWorld().getEntities().forEach(entity -> {
|
||||
if (entity.getCustomName() != null && entity.getCustomName().startsWith("nexus_h_")) {
|
||||
if (!entity.getCustomName().endsWith("_" + event.getPlayer().getName())) {
|
||||
event.getPlayer().hideEntity(NexusLobby.getInstance(), entity);
|
||||
}
|
||||
}
|
||||
});
|
||||
}, 5L);
|
||||
}
|
||||
|
||||
public void createHologram(String id, Location loc, List<String> pages) {
|
||||
config.set(id + ".world", loc.getWorld().getName());
|
||||
config.set(id + ".x", loc.getX());
|
||||
config.set(id + ".y", loc.getY());
|
||||
config.set(id + ".z", loc.getZ());
|
||||
config.set(id + ".text", pages);
|
||||
try { config.save(file); } catch (IOException e) { e.printStackTrace(); }
|
||||
|
||||
NexusHologram holo = new NexusHologram(id, loc, pages);
|
||||
holograms.put(id, holo);
|
||||
}
|
||||
|
||||
public void removeHologram(String id) {
|
||||
NexusHologram holo = holograms.remove(id);
|
||||
if (holo != null) holo.removeAll();
|
||||
config.set(id, null);
|
||||
try { config.save(file); } catch (IOException e) { e.printStackTrace(); }
|
||||
}
|
||||
|
||||
public Set<String> getHologramIds() { return config.getKeys(false); }
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
holograms.values().forEach(NexusHologram::removeAll);
|
||||
holograms.clear();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user