DREAMFIRE Docs ← Back to site
Loading...
Searching...
No Matches
DreamPlaceholderManager.java
Go to the documentation of this file.
1/*
2 * MIT License
3 *
4 * Copyright (c) 2025 Dreamfire Studio
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24package com.dreamfirestudios.dreamcore.DreamPlaceholder;
25
26import me.clip.placeholderapi.expansion.PlaceholderExpansion;
27import org.bukkit.Bukkit;
28import org.bukkit.OfflinePlayer;
29import org.bukkit.entity.Player;
30import org.jetbrains.annotations.NotNull;
31import org.jetbrains.annotations.Nullable;
32
33import java.util.Arrays;
34import java.util.Map;
35import java.util.Objects;
36import java.util.concurrent.ConcurrentHashMap;
37
57public class DreamPlaceholderManager extends PlaceholderExpansion {
58
59 private final String identifier;
60 private final String author;
61 private final String version;
62
63 // key -> provider
64 private final Map<String, IDreamPlaceholder> placeholders = new ConcurrentHashMap<>();
65
72 public DreamPlaceholderManager(@NotNull String identifier,
73 @NotNull String author,
74 @NotNull String version) {
75 this.identifier = Objects.requireNonNull(identifier, "identifier").toLowerCase();
76 this.author = Objects.requireNonNull(author, "author");
77 this.version = Objects.requireNonNull(version, "version");
78 }
79
80 /* ----------------------------- Expansion meta ----------------------------- */
81
83 @Override public @NotNull String getIdentifier() { return identifier; }
85 @Override public @NotNull String getAuthor() { return author; }
87 @Override public @NotNull String getVersion() { return version; }
89 @Override public boolean canRegister() { return true; }
91 @Override public boolean persist() { return true; } // survive /papi reload
92
93 /* ----------------------------- Registration API ----------------------------- */
94
105 public boolean register(@NotNull IDreamPlaceholder provider) {
106 Objects.requireNonNull(provider, "provider");
107 final String key = provider.key().toLowerCase();
108 return placeholders.putIfAbsent(key, provider) == null;
109 }
110
116 public boolean unregister(@NotNull String key) {
117 Objects.requireNonNull(key, "key");
118 return placeholders.remove(key.toLowerCase()) != null;
119 }
120
124 public void clear() {
125 placeholders.clear();
126 }
127
128 /* ----------------------------- Request handling ----------------------------- */
129
136 @Override
137 public @Nullable String onRequest(OfflinePlayer offlinePlayer, @NotNull String params) {
138 return resolve(offlinePlayer, params);
139 }
140
147 @Override
148 public @Nullable String onPlaceholderRequest(Player player, @NotNull String params) {
149 return resolve(player, params);
150 }
151
159 private @NotNull String resolve(@Nullable OfflinePlayer offlinePlayer, @NotNull String rawParams) {
160 final String params = rawParams.trim();
161 if (params.isEmpty()) return "";
162
163 // Split either by ':' or '_' (first token is the key, the rest are args)
164 final String[] colonSplit = params.split(":");
165 final String[] parts = (colonSplit.length > 1) ? colonSplit : params.split("_");
166
167 final String key = parts[0].toLowerCase();
168 final String[] args = (parts.length > 1) ? Arrays.copyOfRange(parts, 1, parts.length) : new String[0];
169
170 final IDreamPlaceholder provider = placeholders.get(key);
171 if (provider == null) {
172 // Bukkit.getLogger().fine("[DreamPlaceholder] Unknown key: " + key + " (params=" + params + ")");
173 return "";
174 }
175
176 try {
177 return provider.resolve(offlinePlayer, args);
178 } catch (Exception ex) {
179 Bukkit.getLogger().warning("[DreamPlaceholder] Error resolving key '" + key + "' with args " +
180 Arrays.toString(args) + ": " + ex.getMessage());
181 return "";
182 }
183 }
184}
Central registry/dispatcher for DreamCore PlaceholderAPI placeholders.
DreamPlaceholderManager(@NotNull String identifier, @NotNull String author, @NotNull String version)
Constructs a new placeholder manager/expansion.
String onPlaceholderRequest(Player player, @NotNull String params)
PlaceholderAPI (legacy) hook for Player.
boolean register(@NotNull IDreamPlaceholder provider)
Registers a placeholder provider.
String onRequest(OfflinePlayer offlinePlayer, @NotNull String params)
PlaceholderAPI (modern) hook.
boolean unregister(@NotNull String key)
Unregisters a provider by key (case-insensitive).
Contract for a single DreamCore PlaceholderAPI provider.
String resolve(@Nullable OfflinePlayer player, @NotNull String[] args)
Resolves a placeholder value.