Skip to content

Commit 785c756

Browse files
committed
Bugfixes and new placeholders
1 parent 4999aca commit 785c756

File tree

7 files changed

+361
-20
lines changed

7 files changed

+361
-20
lines changed

gradle.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ org.gradle.jvmargs=-Xmx1G
44
# Fabric Properties
55
# check these on https://fabricmc.net/use
66
minecraft_version=1.17
7-
yarn_mappings=1.17+build.1
7+
yarn_mappings=1.17+build.13
88
loader_version=0.11.3
99

1010
# Mod Properties
11-
mod_version = 1.0.0-1.17
11+
mod_version = 1.0.1+1.17
1212
maven_group = eu.pb4
1313
archives_base_name = placeholder-api
1414

src/main/java/eu/pb4/placeholders/PlaceholderAPIMod.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import eu.pb4.placeholders.builtin.PlayerPlaceholders;
44
import eu.pb4.placeholders.builtin.ServerPlaceholders;
5+
import eu.pb4.placeholders.builtin.WorldPlaceholders;
56
import eu.pb4.placeholders.util.TextParserUtils;
67
import net.fabricmc.api.ModInitializer;
78

@@ -11,5 +12,6 @@ public void onInitialize() {
1112
TextParserUtils.register();
1213
ServerPlaceholders.register();
1314
PlayerPlaceholders.register();
15+
WorldPlaceholders.register();
1416
}
1517
}

src/main/java/eu/pb4/placeholders/builtin/PlayerPlaceholders.java

Lines changed: 110 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import eu.pb4.placeholders.PlaceholderResult;
55
import eu.pb4.placeholders.util.GeneralUtils;
66
import eu.pb4.placeholders.util.PlaceholderUtils;
7+
import net.minecraft.stat.Stat;
78
import net.minecraft.stat.Stats;
89
import net.minecraft.text.LiteralText;
910
import net.minecraft.util.Formatting;
@@ -75,15 +76,120 @@ public static void register() {
7576
});
7677

7778
PlaceholderAPI.register(new Identifier("player", "statistic"), (ctx) -> {
78-
if (ctx.hasPlayer()) {
79-
Identifier identifier = Identifier.tryParse(ctx.getArgument());
80-
if (identifier != null && ctx.hasArgument()) {
81-
return PlaceholderResult.value(String.valueOf(ctx.getPlayer().getStatHandler().getStat(Stats.CUSTOM.getOrCreateStat(identifier))));
79+
if (ctx.hasPlayer() && ctx.hasArgument()) {
80+
try {
81+
Identifier identifier = Identifier.tryParse(ctx.getArgument());
82+
if (identifier != null) {
83+
int x = ctx.getPlayer().getStatHandler().getStat(Stats.CUSTOM.getOrCreateStat(identifier));
84+
return PlaceholderResult.value(String.valueOf(x));
85+
}
86+
} catch (Exception e) {
87+
/* Into the void you go! */
8288
}
8389
return PlaceholderResult.invalid("Invalid statistic!");
8490
} else {
8591
return PlaceholderResult.invalid("No player!");
8692
}
8793
});
94+
95+
PlaceholderAPI.register(new Identifier("player", "pos_x"), (ctx) -> {
96+
if (ctx.hasPlayer()) {
97+
double value = ctx.getPlayer().getX();
98+
String format = "%.2f";
99+
100+
if (ctx.hasArgument()) {
101+
try {
102+
int x = Integer.getInteger(ctx.getArgument());
103+
format = "%." + x + "f";
104+
} catch (Exception e) {
105+
format = "%.2f";
106+
}
107+
}
108+
109+
return PlaceholderResult.value(String.format(format, value));
110+
} else {
111+
return PlaceholderResult.invalid("No player!");
112+
}
113+
});
114+
115+
PlaceholderAPI.register(new Identifier("player", "pos_y"), (ctx) -> {
116+
if (ctx.hasPlayer()) {
117+
double value = ctx.getPlayer().getY();
118+
String format = "%.2f";
119+
120+
if (ctx.hasArgument()) {
121+
try {
122+
int x = Integer.getInteger(ctx.getArgument());
123+
format = "%." + x + "f";
124+
} catch (Exception e) {
125+
format = "%.2f";
126+
}
127+
}
128+
129+
return PlaceholderResult.value(String.format(format, value));
130+
} else {
131+
return PlaceholderResult.invalid("No player!");
132+
}
133+
});
134+
135+
PlaceholderAPI.register(new Identifier("player", "pos_z"), (ctx) -> {
136+
if (ctx.hasPlayer()) {
137+
double value = ctx.getPlayer().getZ();
138+
String format = "%.2f";
139+
140+
if (ctx.hasArgument()) {
141+
try {
142+
int x = Integer.getInteger(ctx.getArgument());
143+
format = "%." + x + "f";
144+
} catch (Exception e) {
145+
format = "%.2f";
146+
}
147+
}
148+
149+
return PlaceholderResult.value(String.format(format, value));
150+
} else {
151+
return PlaceholderResult.invalid("No player!");
152+
}
153+
});
154+
155+
PlaceholderAPI.register(new Identifier("player", "uuid"), (ctx) -> {
156+
if (ctx.hasPlayer()) {
157+
return PlaceholderResult.value(ctx.getPlayer().getUuidAsString());
158+
} else {
159+
return PlaceholderResult.invalid("No player!");
160+
}
161+
});
162+
163+
PlaceholderAPI.register(new Identifier("player", "health"), (ctx) -> {
164+
if (ctx.hasPlayer()) {
165+
return PlaceholderResult.value(String.format("%.0f", ctx.getPlayer().getHealth()));
166+
} else {
167+
return PlaceholderResult.invalid("No player!");
168+
}
169+
});
170+
171+
PlaceholderAPI.register(new Identifier("player", "max_health"), (ctx) -> {
172+
if (ctx.hasPlayer()) {
173+
return PlaceholderResult.value(String.format("%.0f", ctx.getPlayer().getMaxHealth()));
174+
} else {
175+
return PlaceholderResult.invalid("No player!");
176+
}
177+
});
178+
179+
PlaceholderAPI.register(new Identifier("player", "hunger"), (ctx) -> {
180+
if (ctx.hasPlayer()) {
181+
return PlaceholderResult.value(String.format("%.0f", ctx.getPlayer().getHungerManager().getExhaustion()));
182+
} else {
183+
return PlaceholderResult.invalid("No player!");
184+
}
185+
});
186+
187+
PlaceholderAPI.register(new Identifier("player", "saturation"), (ctx) -> {
188+
if (ctx.hasPlayer()) {
189+
return PlaceholderResult.value(String.format("%.0f", ctx.getPlayer().getHungerManager().getSaturationLevel()));
190+
} else {
191+
return PlaceholderResult.invalid("No player!");
192+
}
193+
});
88194
}
89195
}

src/main/java/eu/pb4/placeholders/builtin/ServerPlaceholders.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
public class ServerPlaceholders {
1616
public static void register() {
17-
1817
PlaceholderAPI.register(new Identifier("server", "tps"), (ctx) -> {
1918
float tps = 1000/Math.max(ctx.getServer().getTickTime(), 50);
2019
String format = "%.1f";
@@ -55,7 +54,7 @@ public static void register() {
5554

5655

5756
PlaceholderAPI.register(new Identifier("server", "time"), (ctx) -> {
58-
SimpleDateFormat format = new SimpleDateFormat(ctx.getArgument());
57+
SimpleDateFormat format = new SimpleDateFormat(ctx.hasArgument() ? ctx.getArgument() : "HH:mm:ss");
5958
return PlaceholderResult.value(format.format(new Date(System.currentTimeMillis())));
6059
});
6160

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
package eu.pb4.placeholders.builtin;
2+
3+
import eu.pb4.placeholders.PlaceholderAPI;
4+
import eu.pb4.placeholders.PlaceholderResult;
5+
import net.minecraft.entity.SpawnGroup;
6+
import net.minecraft.server.world.ServerWorld;
7+
import net.minecraft.text.LiteralText;
8+
import net.minecraft.util.Identifier;
9+
import net.minecraft.world.SpawnHelper;
10+
11+
import java.util.ArrayList;
12+
import java.util.List;
13+
import java.util.Locale;
14+
15+
public class WorldPlaceholders {
16+
static final int CHUNK_AREA = (int)Math.pow(17.0D, 2.0D);
17+
18+
public static void register() {
19+
PlaceholderAPI.register(new Identifier("world", "time"), (ctx) -> {
20+
ServerWorld world;
21+
if (ctx.hasPlayer()) {
22+
world = ctx.getPlayer().getServerWorld();
23+
} else {
24+
world = ctx.getServer().getOverworld();
25+
}
26+
27+
long dayTime = (long) (world.getTimeOfDay() * 3.6 / 60);
28+
29+
return PlaceholderResult.value(String.format("%02d:%02d", (dayTime / 60 + 6) % 24, dayTime % 60));
30+
});
31+
32+
PlaceholderAPI.register(new Identifier("world", "time_alt"), (ctx) -> {
33+
ServerWorld world;
34+
if (ctx.hasPlayer()) {
35+
world = ctx.getPlayer().getServerWorld();
36+
} else {
37+
world = ctx.getServer().getOverworld();
38+
}
39+
40+
long dayTime = (long) (world.getTimeOfDay() * 3.6 / 60);
41+
long x = (dayTime / 60 + 6) % 24;
42+
long y = x % 12;
43+
if (y == 0) {
44+
y = 12;
45+
}
46+
return PlaceholderResult.value(String.format("%02d:%02d %s", y, dayTime % 60, x > 11 ? "PM" : "AM" ));
47+
});
48+
49+
PlaceholderAPI.register(new Identifier("world", "day"), (ctx) -> {
50+
ServerWorld world;
51+
if (ctx.hasPlayer()) {
52+
world = ctx.getPlayer().getServerWorld();
53+
} else {
54+
world = ctx.getServer().getOverworld();
55+
}
56+
57+
return PlaceholderResult.value("" + world.getTime() / 24000);
58+
});
59+
60+
PlaceholderAPI.register(new Identifier("world", "id"), (ctx) -> {
61+
ServerWorld world;
62+
if (ctx.hasPlayer()) {
63+
world = ctx.getPlayer().getServerWorld();
64+
} else {
65+
world = ctx.getServer().getOverworld();
66+
}
67+
68+
return PlaceholderResult.value(world.getRegistryKey().getValue().toString());
69+
});
70+
71+
PlaceholderAPI.register(new Identifier("world", "name"), (ctx) -> {
72+
ServerWorld world;
73+
if (ctx.hasPlayer()) {
74+
world = ctx.getPlayer().getServerWorld();
75+
} else {
76+
world = ctx.getServer().getOverworld();
77+
}
78+
List<String> parts = new ArrayList<>();
79+
{
80+
String[] words = world.getRegistryKey().getValue().getPath().split("_");
81+
for (String word : words) {
82+
String[] s = word.split("", 2);
83+
s[0] = s[0].toUpperCase(Locale.ROOT);
84+
parts.add(String.join("", s));
85+
}
86+
}
87+
return PlaceholderResult.value(String.join(" ", parts));
88+
});
89+
90+
91+
92+
PlaceholderAPI.register(new Identifier("world", "player_count"), (ctx) -> {
93+
ServerWorld world;
94+
if (ctx.hasPlayer()) {
95+
world = ctx.getPlayer().getServerWorld();
96+
} else {
97+
world = ctx.getServer().getOverworld();
98+
}
99+
100+
return PlaceholderResult.value("" + world.getPlayers().size());
101+
});
102+
103+
PlaceholderAPI.register(new Identifier("world", "mob_count"), (ctx) -> {
104+
ServerWorld world;
105+
if (ctx.hasPlayer()) {
106+
world = ctx.getPlayer().getServerWorld();
107+
} else {
108+
world = ctx.getServer().getOverworld();
109+
}
110+
111+
SpawnHelper.Info info = world.getChunkManager().getSpawnInfo();
112+
113+
SpawnGroup spawnGroup = null;
114+
if (ctx.hasArgument()) {
115+
spawnGroup = SpawnGroup.byName(ctx.getArgument());
116+
}
117+
118+
if (spawnGroup != null) {
119+
return PlaceholderResult.value("" + info.getGroupToCount().getInt(spawnGroup));
120+
} else {
121+
int x = 0;
122+
123+
for (int value : info.getGroupToCount().values()) {
124+
x += value;
125+
}
126+
return PlaceholderResult.value("" + x);
127+
}
128+
});
129+
130+
PlaceholderAPI.register(new Identifier("world", "mob_cap"), (ctx) -> {
131+
ServerWorld world;
132+
if (ctx.hasPlayer()) {
133+
world = ctx.getPlayer().getServerWorld();
134+
} else {
135+
world = ctx.getServer().getOverworld();
136+
}
137+
138+
SpawnHelper.Info info = world.getChunkManager().getSpawnInfo();
139+
140+
SpawnGroup spawnGroup = null;
141+
if (ctx.hasArgument()) {
142+
spawnGroup = SpawnGroup.byName(ctx.getArgument());
143+
}
144+
145+
if (spawnGroup != null) {
146+
return PlaceholderResult.value("" + spawnGroup.getCapacity() * info.getSpawningChunkCount() / CHUNK_AREA);
147+
} else {
148+
int x = 0;
149+
150+
for (SpawnGroup group : SpawnGroup.values()) {
151+
x += group.getCapacity();
152+
}
153+
return PlaceholderResult.value("" + x * info.getSpawningChunkCount() / CHUNK_AREA);
154+
}
155+
});
156+
}
157+
}

0 commit comments

Comments
 (0)