-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔥 Implemented
default
config options!
- Loading branch information
Showing
6 changed files
with
185 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.mattmx.nametags; | ||
|
||
import net.kyori.adventure.text.Component; | ||
import org.bukkit.command.Command; | ||
import org.bukkit.command.CommandExecutor; | ||
import org.bukkit.command.CommandSender; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class NameTagsCommand implements CommandExecutor { | ||
private final @NotNull NameTags plugin; | ||
|
||
public NameTagsCommand(@NotNull NameTags plugin) { | ||
this.plugin = plugin; | ||
} | ||
|
||
@Override | ||
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { | ||
this.plugin.reloadConfig(); | ||
sender.sendMessage(Component.text("Reloaded!")); | ||
return false; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/com/mattmx/nametags/config/ConfigDefaultsListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.mattmx.nametags.config; | ||
|
||
import com.mattmx.nametags.NameTags; | ||
import com.mattmx.nametags.entity.trait.RefreshTrait; | ||
import com.mattmx.nametags.event.NameTagEntityCreateEvent; | ||
import org.bukkit.configuration.ConfigurationSection; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.Listener; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class ConfigDefaultsListener implements Listener { | ||
private final @NotNull NameTags plugin; | ||
|
||
public ConfigDefaultsListener(@NotNull NameTags plugin) { | ||
this.plugin = plugin; | ||
|
||
NameTags.getInstance() | ||
.getEntityManager() | ||
.setDefaultProvider(((entity, meta) -> TextDisplayMetaConfiguration.applyMeta(section(), meta))); | ||
} | ||
|
||
private ConfigurationSection section() { | ||
return plugin.getConfig().getConfigurationSection("defaults"); | ||
} | ||
|
||
@EventHandler | ||
public void onCreate(@NotNull NameTagEntityCreateEvent event) { | ||
if (!(event.getNameTag().getBukkitEntity() instanceof Player player)) return; | ||
|
||
event.getNameTag() | ||
.getTraits() | ||
.getOrAddTrait(RefreshTrait.class, () -> | ||
RefreshTrait.ofSeconds( | ||
NameTags.getInstance(), | ||
2L, | ||
(entity) -> { | ||
TextDisplayMetaConfiguration.applyMeta(section(), entity.getMeta()); | ||
TextDisplayMetaConfiguration.applyTextMeta(section(), entity.getMeta(), player, player); | ||
entity.getPassenger().refresh(); | ||
} | ||
) | ||
); | ||
} | ||
|
||
} |
91 changes: 91 additions & 0 deletions
91
src/main/java/com/mattmx/nametags/config/TextDisplayMetaConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package com.mattmx.nametags.config; | ||
|
||
import com.mattmx.nametags.NameTags; | ||
import me.clip.placeholderapi.PlaceholderAPI; | ||
import me.tofaa.entitylib.meta.display.AbstractDisplayMeta; | ||
import me.tofaa.entitylib.meta.display.TextDisplayMeta; | ||
import net.kyori.adventure.text.Component; | ||
import net.kyori.adventure.text.format.NamedTextColor; | ||
import net.kyori.adventure.text.minimessage.MiniMessage; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.configuration.ConfigurationSection; | ||
import org.bukkit.entity.Player; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.awt.*; | ||
import java.util.Locale; | ||
import java.util.Objects; | ||
|
||
public class TextDisplayMetaConfiguration { | ||
|
||
public static void applyTextMeta(@NotNull ConfigurationSection section, @NotNull TextDisplayMeta to, @NotNull Player self, @NotNull Player sender) { | ||
Component text = section.getStringList("text") | ||
.stream() | ||
.map((line) -> convertToComponent(self, sender, line)) | ||
.reduce((a, b) -> a.append(Component.newline()).append(b)) | ||
.orElse(Component.empty()); | ||
|
||
if (!text.equals(to.getText())) { | ||
to.setText(text); | ||
} | ||
} | ||
|
||
public static void applyMeta(@NotNull ConfigurationSection section, @NotNull TextDisplayMeta to) { | ||
String backgroundColor = section.getString("background", "0x40000000"); | ||
int background; | ||
|
||
if (backgroundColor.equalsIgnoreCase("transparent")) { | ||
background = NameTags.TRANSPARENT; | ||
} else if (NamedTextColor.NAMES.value(backgroundColor) != null) { | ||
background = 0x40000000 | Objects.requireNonNull(NamedTextColor.NAMES.value(backgroundColor)).value(); | ||
} else if (backgroundColor.startsWith("#")) { | ||
background = new Color((int) Long.parseLong(backgroundColor.replace("#", ""), 16), true).getRGB(); | ||
} else { | ||
background = NameTags.TRANSPARENT; | ||
} | ||
|
||
if (background != to.getBackgroundColor()) { | ||
to.setBackgroundColor(background); | ||
} | ||
|
||
String billboardString = section.getString("billboard", "center"); | ||
AbstractDisplayMeta.BillboardConstraints billboard = AbstractDisplayMeta.BillboardConstraints.valueOf(billboardString.toUpperCase(Locale.ROOT)); | ||
if (billboard != to.getBillboardConstraints()) { | ||
to.setBillboardConstraints(billboard); | ||
} | ||
|
||
boolean shadow = section.getBoolean("shadow"); | ||
if (shadow != to.isShadow()) { | ||
to.setShadow(shadow); | ||
} | ||
|
||
String range = section.getString("range", "default"); | ||
float finalRange = range.equalsIgnoreCase("default") | ||
? (Bukkit.getSimulationDistance() * 16f) | ||
: Float.parseFloat(range); | ||
|
||
if (finalRange != to.getViewRange()) { | ||
to.setViewRange(finalRange); | ||
} | ||
|
||
String gap = section.getString("gap", "default"); | ||
float finalGap = gap.equalsIgnoreCase("default") | ||
? 0.2f | ||
: Float.parseFloat(gap); | ||
if (finalGap != to.getTranslation().y) { | ||
to.setTranslation(to.getTranslation().withY(finalGap)); | ||
} | ||
} | ||
|
||
private static Component convertToComponent(Player self, Player sending, String line) { | ||
String formatted = line; | ||
|
||
if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) { | ||
formatted = PlaceholderAPI.setRelationalPlaceholders(self, sending, formatted); | ||
formatted = PlaceholderAPI.setPlaceholders(self, formatted); | ||
} | ||
|
||
return MiniMessage.miniMessage().deserialize(formatted); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters