Skip to content

Commit 1f97a89

Browse files
committed
Implement /mv worldborder commands
1 parent db97b6e commit 1f97a89

File tree

5 files changed

+292
-11
lines changed

5 files changed

+292
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
package org.mvplugins.multiverse.core.commands;
2+
3+
import co.aikar.commands.annotation.Default;
4+
import co.aikar.commands.annotation.Flags;
5+
import co.aikar.commands.annotation.Optional;
6+
import co.aikar.commands.annotation.Subcommand;
7+
import co.aikar.commands.annotation.Syntax;
8+
import io.vavr.control.Try;
9+
import org.bukkit.WorldBorder;
10+
import org.jvnet.hk2.annotations.Service;
11+
import org.mvplugins.multiverse.core.command.MVCommandIssuer;
12+
import org.mvplugins.multiverse.core.locale.MVCorei18n;
13+
import org.mvplugins.multiverse.core.locale.message.MessageReplacement.Replace;
14+
import org.mvplugins.multiverse.core.world.LoadedMultiverseWorld;
15+
16+
import java.util.function.Consumer;
17+
18+
import static org.mvplugins.multiverse.core.locale.message.MessageReplacement.replace;
19+
20+
@Service
21+
@Subcommand("worldborder")
22+
final class WorldBorderCommand extends CoreCommand {
23+
24+
@Subcommand("add")
25+
void onWorldBorderAdd(
26+
MVCommandIssuer issuer,
27+
28+
@Syntax("<size>")
29+
double size,
30+
31+
@Optional
32+
@Default("0")
33+
@Syntax("[time]")
34+
int time,
35+
36+
@Flags("resolve=issuerAware")
37+
@Syntax("[world]")
38+
LoadedMultiverseWorld world
39+
) {
40+
worldBorderAction(issuer, world, worldBorder -> {
41+
onWorldBorderSet(issuer, worldBorder.getSize() + size, time, world);
42+
});
43+
}
44+
45+
@Subcommand("center")
46+
void onWorldBorderCenter(
47+
MVCommandIssuer issuer,
48+
49+
@Syntax("[x]")
50+
double x,
51+
52+
@Syntax("[z]")
53+
double z,
54+
55+
@Flags("resolve=issuerAware")
56+
@Syntax("[world]")
57+
LoadedMultiverseWorld world
58+
) {
59+
worldBorderAction(issuer, world, worldBorder -> {
60+
if (worldBorder.getCenter().getX() == x && worldBorder.getCenter().getZ() == z) {
61+
issuer.sendMessage(MVCorei18n.WORLDBORDER_CENTER_NOTHINGCHANGED,
62+
Replace.WORLD.with(world.getAliasOrName()));
63+
return;
64+
}
65+
worldBorder.setCenter(x, z);
66+
issuer.sendMessage(MVCorei18n.WORLDBORDER_CENTER_SUCCESS,
67+
replace("{x}").with(worldBorder.getCenter().getX()),
68+
replace("{z}").with(worldBorder.getCenter().getZ()),
69+
Replace.WORLD.with(world.getAliasOrName()));
70+
});
71+
}
72+
73+
@Subcommand("damage amount")
74+
void onWorldBorderDamageAmount(
75+
MVCommandIssuer issuer,
76+
77+
@Syntax("<damage>")
78+
double damage,
79+
80+
@Flags("resolve=issuerAware")
81+
@Syntax("[world]")
82+
LoadedMultiverseWorld world
83+
) {
84+
worldBorderAction(issuer, world, worldBorder -> {
85+
if (worldBorder.getDamageAmount() == damage) {
86+
issuer.sendMessage(MVCorei18n.WORLDBORDER_DAMAGEAMOUNT_NOTHINGCHANGED,
87+
Replace.WORLD.with(world.getAliasOrName()));
88+
return;
89+
}
90+
worldBorder.setDamageAmount(damage);
91+
issuer.sendMessage(MVCorei18n.WORLDBORDER_DAMAGEAMOUNT_SUCCESS,
92+
replace("{amount}").with(worldBorder.getDamageAmount()),
93+
Replace.WORLD.with(world.getAliasOrName()));
94+
});
95+
}
96+
97+
@Subcommand("damage buffer")
98+
void onWorldBorderDamageBuffer(
99+
MVCommandIssuer issuer,
100+
101+
@Syntax("<distance>")
102+
double distance,
103+
104+
@Flags("resolve=issuerAware")
105+
@Syntax("[world]")
106+
LoadedMultiverseWorld world
107+
) {
108+
worldBorderAction(issuer, world, worldBorder -> {
109+
if (worldBorder.getDamageBuffer() == distance) {
110+
issuer.sendMessage(MVCorei18n.WORLDBORDER_DAMAGEBUFFER_NOTHINGCHANGED,
111+
Replace.WORLD.with(world.getAliasOrName()));
112+
return;
113+
}
114+
worldBorder.setDamageBuffer(distance);
115+
issuer.sendMessage(MVCorei18n.WORLDBORDER_DAMAGEBUFFER_SUCCESS,
116+
replace("{distance}").with(worldBorder.getDamageBuffer()),
117+
Replace.WORLD.with(world.getAliasOrName()));
118+
});
119+
}
120+
121+
@Subcommand("get")
122+
void onWorldBorderGet(
123+
MVCommandIssuer issuer,
124+
125+
@Flags("resolve=issuerAware")
126+
@Syntax("[world]")
127+
LoadedMultiverseWorld world
128+
) {
129+
worldBorderAction(issuer, world, worldBorder -> {
130+
issuer.sendMessage(MVCorei18n.WORLDBORDER_GET_SIZE,
131+
replace("{size}").with(worldBorder.getSize()),
132+
Replace.WORLD.with(world.getAliasOrName()));
133+
});
134+
}
135+
136+
@Subcommand("set")
137+
void onWorldBorderSet(
138+
MVCommandIssuer issuer,
139+
140+
@Syntax("<size>")
141+
double size,
142+
143+
@Optional
144+
@Default("0")
145+
@Syntax("[time]")
146+
int time,
147+
148+
@Flags("resolve=issuerAware")
149+
@Syntax("[world]")
150+
LoadedMultiverseWorld world
151+
) {
152+
worldBorderAction(issuer, world, worldBorder -> {
153+
double previousSize = worldBorder.getSize();
154+
if (previousSize == size) {
155+
issuer.sendMessage(MVCorei18n.WORLDBORDER_SET_NOTHINGCHANGED,
156+
Replace.WORLD.with(world.getAliasOrName()));
157+
return;
158+
}
159+
worldBorder.setSize(size, time);
160+
if (time <= 0) {
161+
issuer.sendMessage(MVCorei18n.WORLDBORDER_SET_IMMEDIATE,
162+
replace("{size}").with(worldBorder.getSize()),
163+
Replace.WORLD.with(world.getAliasOrName()));
164+
} else {
165+
issuer.sendMessage(previousSize > size ? MVCorei18n.WORLDBORDER_SET_GROWING : MVCorei18n.WORLDBORDER_SET_SHRINKING,
166+
replace("{size}").with(size),
167+
replace("{time}").with(time),
168+
Replace.WORLD.with(world.getAliasOrName()));
169+
}
170+
});
171+
}
172+
173+
@Subcommand("warning distance")
174+
void onWorldBorderWarningDistance(
175+
MVCommandIssuer issuer,
176+
177+
@Syntax("<distance>")
178+
int distance,
179+
180+
@Flags("resolve=issuerAware")
181+
@Syntax("[world]")
182+
LoadedMultiverseWorld world
183+
) {
184+
worldBorderAction(issuer, world, worldBorder -> {
185+
if (worldBorder.getWarningDistance() == distance) {
186+
issuer.sendMessage(MVCorei18n.WORLDBORDER_WARNINGDISTANCE_NOTHINGCHANGED,
187+
Replace.WORLD.with(world.getAliasOrName()));
188+
return;
189+
}
190+
worldBorder.setWarningDistance(distance);
191+
issuer.sendMessage(MVCorei18n.WORLDBORDER_WARNINGDISTANCE_SUCCESS,
192+
replace("{distance}").with(worldBorder.getWarningDistance()),
193+
Replace.WORLD.with(world.getAliasOrName()));
194+
});
195+
}
196+
197+
@Subcommand("warning time")
198+
void onWorldBorderWarningTime(
199+
MVCommandIssuer issuer,
200+
201+
@Syntax("<time>")
202+
int time,
203+
204+
@Flags("resolve=issuerAware")
205+
@Syntax("[world]")
206+
LoadedMultiverseWorld world
207+
) {
208+
worldBorderAction(issuer, world, worldBorder -> {
209+
if (worldBorder.getWarningTime() == time) {
210+
issuer.sendMessage(MVCorei18n.WORLDBORDER_WARNINGTIME_NOTHINGCHANGED,
211+
Replace.WORLD.with(world.getAliasOrName()));
212+
return;
213+
}
214+
worldBorder.setWarningTime(time);
215+
issuer.sendMessage(MVCorei18n.WORLDBORDER_WARNINGTIME_SUCCESS,
216+
replace("{time}").with(worldBorder.getWarningTime()),
217+
Replace.WORLD.with(world.getAliasOrName()));
218+
});
219+
}
220+
221+
private void worldBorderAction(MVCommandIssuer issuer, LoadedMultiverseWorld world, Consumer<WorldBorder> worldBorderAction) {
222+
Try.run(() -> world.getWorldBorder().peek(worldBorderAction))
223+
.onFailure(error -> issuer.sendError(error.getLocalizedMessage()));
224+
}
225+
}

src/main/java/org/mvplugins/multiverse/core/locale/MVCorei18n.java

+28-5
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,12 @@ public enum MVCorei18n implements MessageKeyProvider {
205205
// /mv usage
206206
USAGE_DESCRIPTION,
207207

208+
// /mv version
209+
VERSION_DESCRIPTION,
210+
VERSION_MV,
211+
VERSION_AUTHORS,
212+
VERSION_SECRETCODE,
213+
208214
// /mv who
209215
// /mv whoall
210216
WHO_DESCRIPTION,
@@ -214,11 +220,28 @@ public enum MVCorei18n implements MessageKeyProvider {
214220
WHO_EMPTY,
215221
WHO_HEADER,
216222

217-
// /mv version
218-
VERSION_DESCRIPTION,
219-
VERSION_MV,
220-
VERSION_AUTHORS,
221-
VERSION_SECRETCODE,
223+
// /mv worldborder
224+
WORLDBORDER_CENTER_NOTHINGCHANGED,
225+
WORLDBORDER_CENTER_SUCCESS,
226+
227+
WORLDBORDER_DAMAGEAMOUNT_NOTHINGCHANGED,
228+
WORLDBORDER_DAMAGEAMOUNT_SUCCESS,
229+
230+
WORLDBORDER_DAMAGEBUFFER_NOTHINGCHANGED,
231+
WORLDBORDER_DAMAGEBUFFER_SUCCESS,
232+
233+
WORLDBORDER_GET_SIZE,
234+
235+
WORLDBORDER_SET_NOTHINGCHANGED,
236+
WORLDBORDER_SET_IMMEDIATE,
237+
WORLDBORDER_SET_SHRINKING,
238+
WORLDBORDER_SET_GROWING,
239+
240+
WORLDBORDER_WARNINGDISTANCE_NOTHINGCHANGED,
241+
WORLDBORDER_WARNINGDISTANCE_SUCCESS,
242+
243+
WORLDBORDER_WARNINGTIME_NOTHINGCHANGED,
244+
WORLDBORDER_WARNINGTIME_SUCCESS,
222245

223246
// commands error
224247
COMMANDS_ERROR_PLAYERSONLY,

src/main/java/org/mvplugins/multiverse/core/world/LoadedMultiverseWorld.java

+10
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.bukkit.Bukkit;
99
import org.bukkit.Location;
1010
import org.bukkit.World;
11+
import org.bukkit.WorldBorder;
1112
import org.bukkit.WorldType;
1213
import org.bukkit.entity.Player;
1314
import org.jetbrains.annotations.NotNull;
@@ -159,6 +160,15 @@ public Option<List<Player>> getPlayers() {
159160
return getBukkitWorld().map(World::getPlayers);
160161
}
161162

163+
/**
164+
* Get the world border configuration for this world.
165+
*
166+
* @return World border configuration
167+
*/
168+
public Option<WorldBorder> getWorldBorder() {
169+
return getBukkitWorld().map(World::getWorldBorder);
170+
}
171+
162172
/**
163173
* {@inheritDoc}
164174
*/

src/main/resources/multiverse-core_en.properties

+28-5
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,12 @@ mv-core.unload.success=&aWorld '{world}' unloaded!
188188
# /mv usage
189189
mv-core.usage.description=Show Multiverse-Core command usage.
190190

191+
# /mv version
192+
mv-core.version.description=Displays version and authors
193+
mv-core.version.mv=Multiverse Core Version &fv{version}
194+
mv-core.version.authors=Multiverse Core Authors &f{authors}
195+
mv-core.version.secretcode=Special Code: &fFRN002
196+
191197
# /mv who
192198
# /mv whoall
193199
mv-core.who.description=Lists the players in the world specified
@@ -197,11 +203,28 @@ mv-core.who.flags.description=Filter - only shows entries matching this. Page -
197203
mv-core.who.empty=&7&oempty
198204
mv-core.who.header=&b====[ Multiverse World Players List ]====
199205

200-
# /mv version
201-
mv-core.version.description=Displays version and authors
202-
mv-core.version.mv=Multiverse Core Version &fv{version}
203-
mv-core.version.authors=Multiverse Core Authors &f{authors}
204-
mv-core.version.secretcode=Special Code: &fFRN002
206+
# /mv worldborder
207+
mv-core.worldborder.center.nothingchanged=&cNothing changed. The world border is already centered there for world '&6{world}&c'.
208+
mv-core.worldborder.center.success=&rSet the center of the world border to &6{x}&r, &6{z}&r for world '&6{world}&r'.
209+
210+
mv-core.worldborder.damageamount.nothingchanged=&cNothing changed. The world border damage is already that amount for world '&6{world}&c'.
211+
mv-core.worldborder.damageamount.success=&rSet the world border damage to {amount} per block each second for world '&6{world}&r'.
212+
213+
mv-core.worldborder.damagebuffer.nothingchanged=&cNothing changed. The world border damage buffer is already that distance for world '&6{world}&c'.
214+
mv-core.worldborder.damagebuffer.success=&rSet the world border damage buffer to {distance} block(s) for world '&6{world}&r'.
215+
216+
mv-core.worldborder.get.size=&rThe world border is currently {size} block(s) wide for world '&6{world}&r'.
217+
218+
mv-core.worldborder.set.nothingchanged=&cNothing changed. The world border is already that size for world '&6{world}&c'.
219+
mv-core.worldborder.set.immediate=&rSet the world border to {size} block(s) wide for world '&6{world}&r'.
220+
mv-core.worldborder.set.shrinking=&rShrinking the world border to {size} block(s) wide over {time} second(s) for world '&6{world}&r'.
221+
mv-core.worldborder.set.growing=&rGrowing the world border to {size} block(s) wide over {time} second(s) for world '&6{world}&r'.
222+
223+
mv-core.worldborder.warningdistance.nothingchanged=&cNothing changed. The world border warning is already that distance for world '&6{world}&c'.
224+
mv-core.worldborder.warningdistance.success=&rSet the world border warning distance to {distance} block(s) for world '&6{world}&r'.
225+
226+
mv-core.worldborder.warningtime.nothingchanged=&cNothing changed. The world border warning is already that amount of time for world '&6{world}&c'.
227+
mv-core.worldborder.warningtime.success=&rSet the world border warning time to {time} second(s) for world '&6{world}&r'.
205228

206229
# commands error
207230
mv-core.commands.error.playersonly=&cThis command can only be used by players

src/test/java/org/mvplugins/multiverse/core/inject/InjectionTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class InjectionTest : TestWithMockBukkit() {
7777
@Test
7878
fun `Commands are available as services`() {
7979
val commands = serviceLocator.getAllActiveServices(CoreCommand::class.java)
80-
assertEquals(56, commands.size)
80+
assertEquals(57, commands.size)
8181
}
8282

8383
@Test

0 commit comments

Comments
 (0)