|
| 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 | +} |
0 commit comments