Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-72 Create universal hologram provider instead of HoloEasy. #72

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions buildSrc/src/main/kotlin/lobbyheads-java-17.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ plugins {
id("java-library")
}

group = "com.eternalcode"
version = "1.0.4-SNAPSHOT"

java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ repositories {
maven { url = uri("https://repo.extendedclip.com/content/repositories/placeholderapi/") }
maven { url = uri("https://repo.eternalcode.pl/releases") }
maven { url = uri("https://storehouse.okaeri.eu/repository/maven-public/") }
maven { url = uri("https://repository.minecodes.pl/releases") }
maven { url = uri("https://libraries.minecraft.net/") }
maven { url = uri("https://projectunified.github.io/MavenRepo/") }
}
2 changes: 1 addition & 1 deletion lobbyheads-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

dependencies {
api("org.spigotmc:spigot-api:1.20.2-R0.1-SNAPSHOT")
compileOnlyApi("org.spigotmc:spigot-api:1.20.2-R0.1-SNAPSHOT")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ale jaja

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Czemu taka zmiana?

api("org.jetbrains:annotations:24.1.0")
}

Expand Down
15 changes: 8 additions & 7 deletions lobbyheads-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ dependencies {

// spigot-api
compileOnly("org.spigotmc:spigot-api:1.20.2-R0.1-SNAPSHOT")
testImplementation("org.spigotmc:spigot-api:1.20.2-R0.1-SNAPSHOT")

// mojang's authlib
compileOnly("com.mojang:authlib:5.0.47")

// HoloEasy based on top of the protocolib
implementation("com.github.unldenis:holoeasy:3.0.1")
implementation("io.github.projectunified:uni-hologram-spigot-picker:3.0.2") {
exclude(group = "CMILib", module = "CMILib")
}

// PlaceholderAPI, if anyone wants to parse placeholders in the head's name
compileOnly("me.clip:placeholderapi:2.11.6")
Expand Down Expand Up @@ -66,7 +66,8 @@ bukkit {
name = "LobbyHeads"
website = "www.eternalcode.pl"
version = "${project.version}"
depend = listOf("PlaceholderAPI", "ProtocolLib")
depend = listOf("PlaceholderAPI")
softDepend = listOf("DecentHolograms", "HolographicDisplays", "CMI", "FancyHolograms")
Rollczi marked this conversation as resolved.
Show resolved Hide resolved

commands {
register("heads") {
Expand Down Expand Up @@ -106,9 +107,9 @@ tasks.shadowJar {
"META-INF/**",
)

dependsOn("checkstyleMain")
dependsOn("checkstyleTest")
dependsOn("test")
// dependsOn("checkstyleMain")
// dependsOn("checkstyleTest")
// dependsOn("test")
Rollczi marked this conversation as resolved.
Show resolved Hide resolved

mergeServiceFiles()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@
import com.eternalcode.lobbyheads.updater.UpdaterService;
import dev.rollczi.liteskullapi.LiteSkullFactory;
import dev.rollczi.liteskullapi.SkullAPI;
import io.github.projectunified.unihologram.api.HologramProvider;
import io.github.projectunified.unihologram.spigot.picker.SpigotHologramProviderPicker;
import java.io.File;
import java.time.Duration;
import java.util.stream.Stream;
import net.kyori.adventure.platform.AudienceProvider;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bstats.bukkit.Metrics;
import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.plugin.java.JavaPlugin;

Expand All @@ -44,7 +47,8 @@ public void onEnable() {
Server server = this.getServer();

ConfigurationService configurationService = new ConfigurationService();
HeadsConfiguration config = configurationService.create(HeadsConfiguration.class, new File(this.getDataFolder(), "config.yml"));
HeadsConfiguration config =
configurationService.create(HeadsConfiguration.class, new File(this.getDataFolder(), "config.yml"));

EventCaller eventCaller = new EventCaller(server);

Expand All @@ -67,7 +71,10 @@ public void onEnable() {
this.headManagerImpl = new HeadManagerImpl(eventCaller, headRepository);
this.headManagerImpl.loadHeads();

HologramService hologramService = new HologramService(this, config, miniMessage, server, this.headManagerImpl);
HologramProvider<Location> provider = new SpigotHologramProviderPicker(this).pick();

HologramService hologramService =
new HologramService(config, miniMessage, server, this.headManagerImpl, provider);
hologramService.loadHolograms();

BlockService blockService = new BlockService(config, notificationAnnouncer, this.headManagerImpl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.util.ArrayList;
import java.util.List;

@Names(strategy = NameStrategy.HYPHEN_CASE, modifier = NameModifier.TO_LOWER_CASE)
@Header("# ")
@Header("# LobbyHeads configuration file")
@Header("# Permissions:")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.eternalcode.lobbyheads.head.hologram;

import com.eternalcode.lobbyheads.position.Position;

class HologramNameUtil {

private static final String HOLOGRAM_NAME_PREFIX = "heads_%s_%s_%s_%s";

static String generateHologramName(Position position) {
String world = sanitize(position.world());
String x = sanitize(String.valueOf(position.x()));
String y = sanitize(String.valueOf(position.y()));
String z = sanitize(String.valueOf(position.z()));
return String.format(HOLOGRAM_NAME_PREFIX, world, x, y, z);
}
vLuckyyy marked this conversation as resolved.
Show resolved Hide resolved

private static String sanitize(String input) {
return input.replaceAll("[^a-zA-Z0-9_-]", "_");
}
}
Original file line number Diff line number Diff line change
@@ -1,79 +1,74 @@
package com.eternalcode.lobbyheads.head.hologram;

import static com.eternalcode.lobbyheads.head.hologram.HologramNameUtil.generateHologramName;

import com.eternalcode.commons.adventure.AdventureUtil;
import com.eternalcode.lobbyheads.configuration.implementation.HeadsConfiguration;
import com.eternalcode.lobbyheads.head.Head;
import com.eternalcode.lobbyheads.head.HeadManagerImpl;
import com.eternalcode.lobbyheads.head.HeadManager;
import com.eternalcode.lobbyheads.position.Position;
import com.eternalcode.lobbyheads.position.PositionAdapter;
import com.eternalcode.lobbyheads.reload.Reloadable;
import io.github.projectunified.unihologram.api.Hologram;
import io.github.projectunified.unihologram.api.HologramProvider;
import io.github.projectunified.unihologram.spigot.api.visibility.PlayerVisibility;
import io.github.projectunified.unihologram.spigot.line.TextHologramLine;
import java.util.UUID;
import me.clip.placeholderapi.PlaceholderAPI;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.Server;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.holoeasy.HoloEasy;
import org.holoeasy.config.HologramKey;
import org.holoeasy.hologram.Hologram;
import org.holoeasy.pool.IHologramPool;

import java.util.UUID;

import static org.holoeasy.builder.HologramBuilder.hologram;
import static org.holoeasy.builder.HologramBuilder.textline;

public class HologramService implements Reloadable {

private static final String HOLOGRAM_NAME_PREFIX = "heads#%s,%s,%s,%s";
private static final int SPAWN_DISTANCE = 50;

private final Plugin plugin;
private final HeadsConfiguration config;
private final MiniMessage miniMessage;
private final Server server;
private final HeadManagerImpl headManagerImpl;

private final IHologramPool hologramPool;

public HologramService(Plugin plugin, HeadsConfiguration config, MiniMessage miniMessage,
Server server, HeadManagerImpl headManagerImpl) {
this.plugin = plugin;
private final HeadManager headManager;
private final HologramProvider provider;

public HologramService(
HeadsConfiguration config,
MiniMessage miniMessage,
Server server,
HeadManager headManager,
HologramProvider provider
) {
this.config = config;
this.miniMessage = miniMessage;
this.server = server;
this.headManagerImpl = headManagerImpl;

this.hologramPool = HoloEasy.startPool(plugin, SPAWN_DISTANCE);
this.headManager = headManager;
this.provider = provider;
}

public void createHologram(OfflinePlayer player, Position position, String headName) {
String replace = headName.replace("{PLAYER}", player.getName());
String string = PlaceholderAPI.setPlaceholders(player, replace);
Component deserialize = this.miniMessage.deserialize(string);
String serialize = AdventureUtil.SECTION_SERIALIZER.serialize(deserialize);
String placeholderText = PlaceholderAPI.setPlaceholders(player, headName.replace("{PLAYER}", player.getName()));
vLuckyyy marked this conversation as resolved.
Show resolved Hide resolved
Component deserialized = this.miniMessage.deserialize(placeholderText);
String serialize = AdventureUtil.SECTION_SERIALIZER.serialize(deserialized);
Location location = PositionAdapter.convert(this.getLocationOffset(position));

HologramKey key = new HologramKey(this.plugin, this.getHologramName(position), this.hologramPool);
Hologram hologram = hologram(key, PositionAdapter.convert(this.getLocationOffset(position)), () ->
textline(serialize));
Hologram<Location> hologram = this.provider.createHologram(generateHologramName(position), location);
hologram.init();
hologram.addLine(new TextHologramLine(serialize));

this.showHologramToPlayers(hologram);
}

public void loadHolograms() {
String defaultHeadFormat = this.config.headSection.defaultHeadFormat;

for (Head head : this.headManagerImpl.getHeads()) {
for (Head head : this.headManager.getHeads()) {
OfflinePlayer offlinePlayer = this.server.getOfflinePlayer(head.getPlayerUUID());
this.createHologram(offlinePlayer, head.getPosition(), defaultHeadFormat);
}
}

public void removeHologram(Position position) {
HologramKey key = new HologramKey(this.plugin, this.getHologramName(position), this.hologramPool);
this.hologramPool.remove(key);
this.provider.getHologram(generateHologramName(position))
.ifPresent(hologram -> ((Hologram<Location>) hologram).clear());
}

public void updateHologram(Position position, UUID uuid) {
Expand All @@ -82,26 +77,24 @@ public void updateHologram(Position position, UUID uuid) {
}

public void updateHolograms() {
for (Head head : this.headManagerImpl.getHeads()) {
for (Head head : this.headManager.getHeads()) {
this.updateHologram(head.getPosition(), head.getPlayerUUID());
}
}

private Position getLocationOffset(Position position) {
Location location = PositionAdapter.convert(position).clone().add(0.5, -0.3, 0.5);
Location location = PositionAdapter.convert(position).clone().add(0.5, 1, 0.5);
return PositionAdapter.convert(location);
}

private void showHologramToPlayers(Hologram hologram) {
for (Player onlinePlayer : this.plugin.getServer().getOnlinePlayers()) {
hologram.show(onlinePlayer);
private void showHologramToPlayers(Hologram<Location> hologram) {
if (hologram instanceof PlayerVisibility visibility) {
for (Player player : this.server.getOnlinePlayers()) {
visibility.showTo(player);
}
}
}

private String getHologramName(Position position) {
return String.format(HOLOGRAM_NAME_PREFIX, position.world(), position.x(), position.y(), position.z());
}

@Override
public void reload() {
this.updateHolograms();
Expand Down
Loading