Skip to content

Commit dd6d105

Browse files
WunderoWundero
Wundero
authored and
Wundero
committed
Attempting to use annotations
Hope this works.
2 parents 2326630 + ec6eecc commit dd6d105

37 files changed

+3585
-2762
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# PlaceholderAPI [![Build Status](https://travis-ci.org/rojo8399/PlaceholderAPI.svg?branch=master)](https://travis-ci.org/rojo8399/PlaceholderAPI) [![Awesome](https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg)](https://github.com/sindresorhus/awesome)
1+
# PlaceholderAPI [![Build Status](https://travis-ci.org/rojo8399/PlaceholderAPI.svg?branch=annotations)](https://travis-ci.org/rojo8399/PlaceholderAPI) [![Awesome](https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg)](https://github.com/sindresorhus/awesome)
22
[![forthebadge](http://forthebadge.com/images/badges/made-with-crayons.svg)](http://forthebadge.com) [![forthebadge](http://forthebadge.com/images/badges/fuck-it-ship-it.svg)](http://forthebadge.com)
33

44
If you were sent here from another plugin, simply download this plugin and install it in your mods folder. After the first start of this plugin, you will need to enable the default expansions in the config file to be able to use those placeholders. Example if you want Player placeholders, open the config and set <code>Player</code> to <code>True</code>. Now you can use %player_name% as a placeholder. This same method applies for every available expansion.

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44
}
55

66
group = 'me.rojo8399.placeholderapi'
7-
version = '3.12'
7+
version = '4.0'
88
description = 'An API for all of your placeholders.'
99

1010
compileJava.options.encoding = 'UTF-8'

src/main/java/me/rojo8399/placeholderapi/PlaceholderAPIPlugin.java

Lines changed: 113 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,17 @@ of this software and associated documentation files (the "Software"), to deal
2727
import java.io.IOException;
2828
import java.nio.file.Files;
2929
import java.nio.file.Path;
30+
import java.time.format.DateTimeFormatter;
31+
import java.util.ArrayList;
3032
import java.util.HashMap;
33+
import java.util.HashSet;
34+
import java.util.List;
3135
import java.util.Set;
3236
import java.util.stream.Collectors;
3337

3438
import org.slf4j.Logger;
3539
import org.spongepowered.api.Game;
40+
import org.spongepowered.api.Sponge;
3641
import org.spongepowered.api.asset.Asset;
3742
import org.spongepowered.api.command.CommandException;
3843
import org.spongepowered.api.command.CommandResult;
@@ -64,15 +69,10 @@ of this software and associated documentation files (the "Software"), to deal
6469
import me.rojo8399.placeholderapi.configs.Config;
6570
import me.rojo8399.placeholderapi.configs.JavascriptManager;
6671
import me.rojo8399.placeholderapi.configs.Messages;
67-
import me.rojo8399.placeholderapi.expansions.CurrencyExpansion;
68-
import me.rojo8399.placeholderapi.expansions.DateTimeExpansion;
69-
import me.rojo8399.placeholderapi.expansions.Expansion;
70-
import me.rojo8399.placeholderapi.expansions.JavascriptExpansion;
71-
import me.rojo8399.placeholderapi.expansions.PlayerExpansion;
72-
import me.rojo8399.placeholderapi.expansions.RankExpansion;
73-
import me.rojo8399.placeholderapi.expansions.ServerExpansion;
74-
import me.rojo8399.placeholderapi.expansions.SoundExpansion;
75-
import me.rojo8399.placeholderapi.expansions.StatisticExpansion;
72+
import me.rojo8399.placeholderapi.placeholder.Expansion;
73+
import me.rojo8399.placeholderapi.placeholder.ExpansionBuilder;
74+
import me.rojo8399.placeholderapi.placeholder.Store;
75+
import me.rojo8399.placeholderapi.placeholder.impl.Defaults;
7676
import ninja.leaping.configurate.ConfigurationNode;
7777
import ninja.leaping.configurate.commented.CommentedConfigurationNode;
7878
import ninja.leaping.configurate.hocon.HoconConfigurationLoader;
@@ -85,7 +85,7 @@ public class PlaceholderAPIPlugin {
8585

8686
public static final String PLUGIN_ID = "placeholderapi";
8787
public static final String PLUGIN_NAME = "PlaceholderAPI";
88-
public static final String PLUGIN_VERSION = "3.12";
88+
public static final String PLUGIN_VERSION = "4.0";
8989
private static PlaceholderAPIPlugin instance;
9090

9191
@Inject
@@ -116,9 +116,14 @@ public class PlaceholderAPIPlugin {
116116
private ConfigurationNode msgRoot;
117117
private Messages msgs;
118118
private ConfigurationLoader<CommentedConfigurationNode> msgloader;
119+
private DateTimeFormatter formatter;
119120

120121
private PlaceholderService s;
121122

123+
public DateTimeFormatter formatter() {
124+
return formatter;
125+
}
126+
122127
public ConfigurationNode getRootConfig() {
123128
return root;
124129
}
@@ -201,6 +206,7 @@ public void onGamePreInitializationEvent(GamePreInitializationEvent event)
201206
}
202207
}
203208
Messages.init(msgs);
209+
this.formatter = DateTimeFormatter.ofPattern(config.dateFormat);
204210
}
205211

206212
@Listener
@@ -236,36 +242,119 @@ public CommandResult execute(CommandSource src, CommandContext args) throws Comm
236242

237243
}
238244

245+
private Set<Object> alreadyRegistered = new HashSet<>();
246+
247+
public void registerListeners(Object object) {
248+
if (alreadyRegistered.contains(object)) {
249+
return;
250+
}
251+
Sponge.getEventManager().registerListeners(this, object);
252+
alreadyRegistered.add(object);
253+
}
254+
255+
public void unregisterListeners(Object object) {
256+
Sponge.getEventManager().unregisterListeners(object);
257+
if (alreadyRegistered.contains(object)) {
258+
alreadyRegistered.remove(object);
259+
}
260+
}
261+
239262
@Listener
240263
public void onGameStartingServerEvent(GameStartingServerEvent event) {
241264
registerPlaceholders();
242265
metrics.addCustomChart(new Metrics.SimpleBarChart("placeholders") {
243266
@Override
244267
public HashMap<String, Integer> getValues(HashMap<String, Integer> valueMap) {
245-
Set<Expansion> exp = s.getExpansions();
246-
if (exp.isEmpty()) {
268+
List<String> rids = Store.get().ids(true);
269+
List<String> ids = Store.get().ids(false);
270+
if (rids.isEmpty() && ids.isEmpty()) {
247271
HashMap<String, Integer> out = new HashMap<>();
248272
out.put("none", 1);
249273
return out;
250274
}
251-
return (HashMap<String, Integer>) exp.stream()
252-
.collect(Collectors.toMap(Expansion::getIdentifier, e -> 1));
275+
List<String> exp = new ArrayList<>();
276+
rids.forEach(e -> exp.add("rel_" + e));
277+
ids.forEach(exp::add);
278+
return (HashMap<String, Integer>) exp.stream().collect(Collectors.toMap(e -> e, e -> 1));
253279
}
254280
});
255281
}
256282

257283
public void registerPlaceholders() {
258-
s.registerPlaceholder(new JavascriptExpansion(jsm));
259-
s.registerPlaceholder(new PlayerExpansion());
260-
s.registerPlaceholder(new ServerExpansion());
261-
s.registerPlaceholder(new SoundExpansion());
262-
s.registerPlaceholder(new RankExpansion());
263-
if (game.getServiceManager().provide(EconomyService.class).isPresent()) {
264-
s.registerPlaceholder(
265-
new CurrencyExpansion(game.getServiceManager().provideUnchecked(EconomyService.class)));
284+
EconomyService ex = game.getServiceManager().provide(EconomyService.class).orElse(null);
285+
Defaults handle = new Defaults(ex, this.jsm);
286+
ExpansionBuilder.loadAll(handle, this).stream().map(builder -> {
287+
switch (builder.getId()) {
288+
case "player": {
289+
if (builder.isRelational()) {
290+
return builder.description(Messages.get().placeholder.relplayerdesc.value)
291+
.tokens("distance", "audible", "visible", "distance_x", "distance_y", "distance_z")
292+
.version("2.0");
293+
} else {
294+
return builder.description(Messages.get().placeholder.playerdesc.value)
295+
.tokens(null, "prefix", "suffix", "option_[option]", "permission_[permission]", "name",
296+
"displayname", "uuid", "can_fly", "world", "ping", "language", "flying", "health",
297+
"max_health", "food", "saturation", "gamemode", "x", "y", "z", "direction", "exp",
298+
"exp_total", "exp_to_next", "level", "first_join", "fly_speed", "max_air",
299+
"remaining_air", "item_in_main_hand", "item_in_off_hand", "walk_speed",
300+
"time_played", "time_played_ticks", "time_played_seconds", "time_played_minutes",
301+
"time_played_hours", "time_played_days")
302+
.version("2.0");
303+
}
304+
}
305+
case "rank": {
306+
if (builder.isRelational()) {
307+
return builder.description(Messages.get().placeholder.relrankdesc.value)
308+
.tokens("greater_than", "less_than").version("1.0");
309+
} else {
310+
return builder.description(Messages.get().placeholder.rankdesc.value)
311+
.tokens(null, "prefix", "suffix", "name", "permission_[permission]", "option_[option]")
312+
.version("2.0");
313+
}
314+
}
315+
case "javascript":
316+
return builder.description(Messages.get().placeholder.jsdesc.value).tokens(jsm.getScriptNames())
317+
.reloadFunction(e -> {
318+
try {
319+
jsm.reloadScripts();
320+
} catch (Exception exc) {
321+
getLogger().warn("Error reloading JavaScript placeholders!");
322+
exc.printStackTrace();
323+
return false;
324+
}
325+
e.setTokens(jsm.getScriptNames());
326+
return true;
327+
}).version("2.0");
328+
case "economy":
329+
return builder.description(Messages.get().placeholder.curdesc.value)
330+
.tokens("", "[currency]", "balance", "balance_[currency]", "bal_format_[currency]",
331+
"bal_format", "display", "display_[currency]", "plural_display_[currency]",
332+
"symbol_[currency]", "plural_display", "symbol")
333+
.version("2.0");
334+
case "server":
335+
return builder
336+
.description(Messages.get().placeholder.serverdesc.value).tokens("unique_players", "online",
337+
"max_players", "motd", "cores", "tps", "ram_used", "ram_free", "ram_total", "ram_max")
338+
.version("2.0");
339+
case "sound":
340+
return builder.description(Messages.get().placeholder.sounddesc.value)
341+
.tokens("[sound]-[volume]-[pitch]").version("2.0");
342+
case "statistic":
343+
return builder.description(Messages.get().placeholder.statdesc.value).version("2.0");
344+
case "time":
345+
return builder.description(Messages.get().placeholder.timedesc.value).tokens("").version("2.0");
346+
}
347+
return builder;
348+
}).forEach(t -> {
349+
try {
350+
t.author("Wundero").plugin(this).buildAndRegister();
351+
} catch (Exception e) {
352+
e.printStackTrace();
353+
}
354+
});
355+
if (ex == null) {
356+
Store.get().get("economy", false).ifPresent(Expansion::disable);
266357
}
267-
s.registerPlaceholder(new DateTimeExpansion());
268-
s.registerPlaceholder(new StatisticExpansion());
269358
}
270359

271360
@Listener

0 commit comments

Comments
 (0)