Skip to content

Commit a56cd82

Browse files
author
BuildTools
committed
Cleaned up code in the Sponge edition
1 parent 99b6c2f commit a56cd82

18 files changed

Lines changed: 259 additions & 418 deletions

StructureBoxes-Core/src/main/java/io/github/eirikh1996/structureboxes/command/Command.java

Lines changed: 0 additions & 12 deletions
This file was deleted.

StructureBoxes-Sponge/build.gradle.kts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import org.spongepowered.gradle.plugin.config.PluginLoaders
66

77
plugins {
88
id("buildlogic.java-conventions")
9+
id("com.gradleup.shadow") version "8.3.5"
910
id("java-library")
1011
id("org.spongepowered.gradle.plugin") version "2.0.2"
1112
}
@@ -34,13 +35,22 @@ sponge {
3435
}
3536
}
3637

38+
tasks.shadowJar {
39+
archiveBaseName.set("StructureBoxes-Bukkit")
40+
archiveClassifier.set("")
41+
archiveVersion.set("")
42+
dependencies {
43+
include(project(":structureboxes-core"))
44+
}
45+
}
46+
3747
dependencies {
3848
api(project(":structureboxes-core"))
39-
api("com.sk89q.worldedit:worldedit-sponge:7.2.1")
4049
compileOnly("org.bstats:bstats-sponge:3.1.0")
4150
compileOnly(files(
4251
"/../libs/sponge/movecraft-0.4.1.jar",
4352
"/../libs/sponge/EagleFactions-1.2.0-API-11.0.0.jar",
53+
"/../libs/sponge/worldedit-sponge-api14-7.3.12-SNAPSHOT-dist.jar"
4454
))
4555
}
4656

StructureBoxes-Sponge/src/main/java/io/github/eirikh1996/structureboxes/StructureBoxes.java

Lines changed: 87 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,22 @@
5454
import org.spongepowered.api.SystemSubject;
5555
import org.spongepowered.api.block.BlockType;
5656
import org.spongepowered.api.block.BlockTypes;
57+
import org.spongepowered.api.command.Command;
58+
import org.spongepowered.api.command.CommandCompletion;
59+
import org.spongepowered.api.command.parameter.Parameter;
5760
import org.spongepowered.api.config.ConfigDir;
5861
import org.spongepowered.api.config.ConfigManager;
5962
import org.spongepowered.api.config.DefaultConfig;
6063
import org.spongepowered.api.entity.living.player.Player;
6164
import org.spongepowered.api.entity.living.player.server.ServerPlayer;
6265
import org.spongepowered.api.event.Listener;
6366
import org.spongepowered.api.event.lifecycle.LoadedGameEvent;
67+
import org.spongepowered.api.event.lifecycle.RegisterCommandEvent;
6468
import org.spongepowered.api.event.lifecycle.StartedEngineEvent;
6569
import org.spongepowered.api.event.lifecycle.StartingEngineEvent;
6670
import org.spongepowered.api.util.Ticks;
6771
import org.spongepowered.api.util.Tristate;
6872
import org.spongepowered.api.world.server.ServerLocation;
69-
import org.spongepowered.api.world.server.ServerWorld;
7073
import org.spongepowered.configurate.CommentedConfigurationNode;
7174
import org.spongepowered.configurate.ConfigurationNode;
7275
import org.spongepowered.configurate.hocon.HoconConfigurationLoader;
@@ -122,6 +125,7 @@ public Path getConfigDir() {
122125
private SystemSubject console;
123126
@Inject private PluginContainer container;
124127

128+
125129
@Listener
126130
public void onGameLoaded(LoadedGameEvent event) {
127131
instance = this;
@@ -178,6 +182,11 @@ public void onServerStarting(StartingEngineEvent<Server> event) {
178182
logger.error(I18nSupport.getInternationalisedString("Startup - Error reading WE config"));
179183
e.printStackTrace();
180184
}
185+
186+
Sponge.eventManager().registerListeners(container, new BlockListener());
187+
if (Sponge.metricsConfigManager().collectionState(container) != Tristate.TRUE && !Settings.Metrics) {
188+
return;
189+
}
181190
final boolean noRegionProvider = !regionProviderFound;
182191
metrics.addCustomChart(new AdvancedPie("region_providers", () -> {
183192
Map<String, Integer> valueMap = new HashMap<>();
@@ -194,13 +203,80 @@ public void onServerStarting(StartingEngineEvent<Server> event) {
194203
}));
195204
metrics.addCustomChart(new SimplePie("localisation", () -> Settings.locale));
196205

197-
if (Sponge.metricsConfigManager().collectionState(container) != Tristate.TRUE && !Settings.Metrics) {
198-
metrics.shutdown();
199-
}
206+
200207
//Register listener
201-
Sponge.eventManager().registerListeners(container, new BlockListener());
208+
209+
202210

203211

212+
}
213+
214+
@Listener
215+
public void onRegisterCommand(RegisterCommandEvent<Command.Parameterized> event) {
216+
//Register commands
217+
//Create command
218+
Command.Parameterized createCommand = Command.builder()
219+
.permission("structureboxes.create")
220+
.addParameter(Parameter.string().completer((context, input) -> {
221+
final List<CommandCompletion> completions = new ArrayList<>();
222+
if (!(context.cause().root() instanceof ServerPlayer)) {
223+
return completions;
224+
}
225+
final String[] files = instance.getWorldEditHandler().getSchemDir().list(((dir, name) -> name.endsWith(".schematic") || name.endsWith(".schem")));
226+
if (files == null)
227+
return completions;
228+
for (String file : files) {
229+
if (!input.isEmpty() && !file.startsWith(input))
230+
continue;
231+
completions.add(CommandCompletion.of(file.replace(".schematic", "").replace(".schem", "")));
232+
}
233+
return completions;
234+
}).build())
235+
.executor(new StructureBoxCreateCommand())
236+
.build();
237+
238+
//undo command
239+
Command.Parameterized undoCommand = Command.builder()
240+
.executor(new StructureBoxUndoCommand())
241+
.permission("structureboxes.undo")
242+
.build();
243+
244+
245+
//reload command
246+
Command.Parameterized reloadCommand = Command.builder()
247+
.executor(new StructureBoxReloadCommand())
248+
.permission("structureboxes.reload")
249+
.build();
250+
251+
//sessions command
252+
Command.Parameterized sessionsCommand = Command.builder()
253+
.addParameter(Parameter.integerNumber().key(Parameter.key("page", Integer.class)).build())
254+
.addParameter(Parameter.string().key(Parameter.key("player|-a", String.class))
255+
.requiredPermission("structurebox.sessions.others")
256+
.completer((context, input) -> {
257+
final List<String> args = new ArrayList<>();
258+
Sponge.server().onlinePlayers().forEach((p) -> args.add(p.name()));
259+
args.add("-a");
260+
final List<CommandCompletion> completions = new ArrayList<>();
261+
args.forEach((arg) -> {
262+
if (arg.toLowerCase().startsWith(input.toLowerCase())) {
263+
completions.add(CommandCompletion.of(arg));
264+
}
265+
});
266+
return completions;
267+
}).build())
268+
.executor(new StructureBoxSessionsCommand())
269+
.build();
270+
271+
Command.Parameterized structureBoxCommand = Command.builder()
272+
.executor(new StructureBoxCommand(container))
273+
.addChild(createCommand, "create", "cr", "c")
274+
.addChild(undoCommand, "undo", "u" , "ud")
275+
.addChild(reloadCommand, "reload", "r", "rl")
276+
.addChild(sessionsCommand, "sessions", "s")
277+
.build();
278+
279+
event.register(plugin, structureBoxCommand, "structurebox", "sbox", "sb");
204280
}
205281

206282
@Listener
@@ -251,7 +327,7 @@ public boolean isFreeSpace(UUID playerID, String schematicName, Collection<Locat
251327
final HashMap<Location, Object> originalBlocks = new HashMap<>();
252328
Player p = Sponge.server().player(playerID).get();
253329
for (Location loc : locations){
254-
ServerLocation spongeLoc = ServerLocation.of((ServerWorld) ((SpongeWorld) loc.getExtent()).getWorld(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
330+
ServerLocation spongeLoc = SpongeAdapter.adapt(loc);
255331
originalBlocks.put(loc, spongeLoc.blockType());
256332
if (!Settings.CheckFreeSpace){
257333
continue;
@@ -296,7 +372,7 @@ public SpongeWorldEdit getWorldEditPlugin() {
296372

297373
public void clearInterior(Collection<Location> interior) {
298374
for (Location loc : interior){
299-
ServerLocation.of((ServerWorld) ((SpongeWorld) loc.getExtent()).getWorld(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()).setBlockType(BlockTypes.AIR.get(), BlockChangeFlags.NONE);
375+
SpongeAdapter.adapt(loc).setBlockType(BlockTypes.AIR.get(), BlockChangeFlags.NONE);
300376
}
301377
}
302378

@@ -381,6 +457,10 @@ public void loadLocales() throws IOException {
381457
}
382458

383459

460+
}
461+
462+
private void handleMetrics() {
463+
384464
}
385465

386466
public PluginContainer getPlugin() {
Lines changed: 32 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,55 @@
11
package io.github.eirikh1996.structureboxes.command;
22

33
import io.github.eirikh1996.structureboxes.StructureBoxes;
4+
import net.kyori.adventure.audience.Audience;
5+
import net.kyori.adventure.text.Component;
6+
import net.kyori.adventure.text.TextComponent;
47
import org.spongepowered.api.Sponge;
8+
import org.spongepowered.api.command.Command;
9+
import org.spongepowered.api.command.CommandCompletion;
510
import org.spongepowered.api.command.exception.CommandException;
611
import org.spongepowered.api.command.CommandResult;
7-
import org.spongepowered.api.command.CommandSource;
812
import org.spongepowered.api.command.parameter.CommandContext;
913
import org.spongepowered.api.command.CommandExecutor;
10-
import org.spongepowered.api.command.exception.CommandException;
11-
import org.spongepowered.api.command.parameter.CommandContext;
12-
import org.spongepowered.api.plugin.PluginContainer;
13-
import org.spongepowered.api.text.Text;
14+
import org.spongepowered.api.command.parameter.Parameter;
15+
import org.spongepowered.api.entity.living.player.server.ServerPlayer;
16+
import org.spongepowered.plugin.PluginContainer;
17+
18+
import java.util.ArrayList;
19+
import java.util.Collections;
20+
import java.util.List;
1421

1522
public class StructureBoxCommand implements CommandExecutor {
23+
private final PluginContainer plugin;
1624

17-
public static
18-
//Create command
19-
CommandSpec createCommand = CommandSpec.builder()
20-
.permission("structureboxes.create")
21-
.arguments(GenericArguments.string(Text.of()), GenericArguments.flags().flag("m").buildWith(GenericArguments.none()), GenericArguments.flags().flag("e").buildWith(GenericArguments.integer(Text.EMPTY)))
22-
.executor(new StructureBoxCreateCommand())
23-
.build();
25+
public void register(final PluginContainer container) {
2426

25-
//undo command
26-
CommandSpec undoCommand = CommandSpec.builder()
27-
.executor(new StructureBoxUndoCommand())
28-
.permission("structureboxes.undo")
29-
.build();
3027

3128

32-
//reload command
33-
CommandSpec reloadCommand = CommandSpec.builder()
34-
.executor(new StructureBoxReloadCommand())
35-
.permission("structureboxes.reload")
36-
.build();
29+
Command.builder().executor(this);
30+
}
3731

38-
//sessions command
39-
CommandSpec sessionsCommand = CommandSpec.builder()
40-
.executor(new StructureBoxSessionsCommand())
41-
.arguments(
42-
GenericArguments.optional(GenericArguments.string(Text.of("player|-a"))),
43-
GenericArguments.optional(GenericArguments.integer(Text.of("page"))))
44-
.build();
32+
public StructureBoxCommand(final PluginContainer plugin) {
33+
this.plugin = plugin;
4534

46-
CommandSpec structureBoxCommand = CommandSpec.builder()
47-
.executor(new StructureBoxCommand())
48-
.child(createCommand, "create", "cr", "c")
49-
.child(undoCommand, "undo", "u" , "ud")
50-
.child(reloadCommand, "reload", "r", "rl")
51-
.child(sessionsCommand, "sessions", "s")
52-
.build();
53-
Sponge.commandManager().register(plugin, structureBoxCommand, "structurebox", "sbox", "sb");
35+
}
5436

5537

5638
@Override
5739
public CommandResult execute(CommandContext args) throws CommandException {
40+
final Audience audience = args.cause().audience();
5841
PluginContainer plugin = StructureBoxes.getInstance().getPlugin();
59-
src.sendMessage(Text.of("§5 ==================[§6StructureBoxes§5]=================="));
60-
src.sendMessage(Text.of("§6 Author: " + String.join(",", plugin.getAuthors())));
61-
src.sendMessage(Text.of("§6 Version: v" + plugin.getVersion()));
62-
src.sendMessage(Text.of("§6 /sb create <schematic ID> [-m] - Creates new structure box"));
63-
src.sendMessage(Text.of("§6 If using FAWE, -m will move schematic to global directory"));
64-
src.sendMessage(Text.of("§6 /sb undo - Undoes the last undo session"));
65-
src.sendMessage(Text.of("§6 /sb reload - Reloads the plugin"));
66-
src.sendMessage(Text.of("§6 /sb sessions [player|-a|you] [page] - Shows active sessions"));
67-
src.sendMessage(Text.of("§5 ========================================================"));
68-
return CommandResult.empty();
42+
final List<String> contributors = new ArrayList<>();
43+
plugin.metadata().contributors().forEach((c) -> contributors.add(c.name()));
44+
audience.sendMessage(Component.text("§5 ==================[§6StructureBoxes§5]=================="));
45+
audience.sendMessage(Component.text("§6 Author: " + String.join(",", contributors)));
46+
audience.sendMessage(Component.text("§6 Version: v" + plugin.metadata().version()));
47+
audience.sendMessage(Component.text("§6 /sb create <schematic ID> [-m] - Creates new structure box"));
48+
audience.sendMessage(Component.text("§6 If using FAWE, -m will move schematic to global directory"));
49+
audience.sendMessage(Component.text("§6 /sb undo - Undoes the last undo session"));
50+
audience.sendMessage(Component.text("§6 /sb reload - Reloads the plugin"));
51+
audience.sendMessage(Component.text("§6 /sb sessions [player|-a|you] [page] - Shows active sessions"));
52+
audience.sendMessage(Component.text("§5 ========================================================"));
53+
return CommandResult.success();
6954
}
7055
}

StructureBoxes-Sponge/src/main/java/io/github/eirikh1996/structureboxes/command/StructureBoxCreateCommand.java

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,16 @@
66
import io.github.eirikh1996.structureboxes.WorldEditHandler;
77
import io.github.eirikh1996.structureboxes.localisation.I18nSupport;
88
import io.github.eirikh1996.structureboxes.settings.Settings;
9+
import net.kyori.adventure.text.Component;
910
import org.jetbrains.annotations.NotNull;
1011
import org.jetbrains.annotations.Nullable;
1112
import org.spongepowered.api.block.BlockType;
12-
import org.spongepowered.api.command.CommandException;
1313
import org.spongepowered.api.command.CommandResult;
14-
import org.spongepowered.api.command.CommandSource;
15-
import org.spongepowered.api.command.args.CommandContext;
16-
import org.spongepowered.api.command.spec.CommandExecutor;
17-
import org.spongepowered.api.data.key.Keys;
14+
import org.spongepowered.api.command.CommandExecutor;
15+
import org.spongepowered.api.command.parameter.CommandContext;
1816
import org.spongepowered.api.entity.living.player.Player;
1917
import org.spongepowered.api.item.inventory.ItemStack;
2018
import org.spongepowered.api.item.inventory.entity.PlayerInventory;
21-
import org.spongepowered.api.text.Text;
2219
import org.spongepowered.api.world.World;
2320

2421
import java.util.ArrayList;
@@ -28,45 +25,45 @@
2825

2926
public class StructureBoxCreateCommand implements CommandExecutor {
3027
@Override
31-
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
32-
if (!(src instanceof Player)) {
33-
src.sendMessage(Text.of(COMMAND_PREFIX + I18nSupport.getInternationalisedString("Command - Must be player")));
28+
public CommandResult execute(CommandContext args) {
29+
if (!(args.cause().root() instanceof Player)) {
30+
src.sendMessage(Component.text(COMMAND_PREFIX + I18nSupport.getInternationalisedString("Command - Must be player")));
3431
return CommandResult.empty();
3532
}
3633
if (!src.hasPermission("structureboxes.create")) {
37-
src.sendMessage(Text.of(COMMAND_PREFIX + I18nSupport.getInternationalisedString("Command - No permission")));
34+
src.sendMessage(Component.text(COMMAND_PREFIX + I18nSupport.getInternationalisedString("Command - No permission")));
3835
return CommandResult.empty();
3936
}
40-
String arg = args.<String>getOne(Text.of()).get();
37+
String arg = args.<String>getOne(Component.text()).get();
4138
final Player player = (Player) src;
4239
@NotNull final WorldEditHandler weHandler = StructureBoxes.getInstance().getWorldEditHandler();
4340
World world = player.getWorld();
4441
SpongeWorld spongeWorld = StructureBoxes.getInstance().getWorldEditPlugin().getWorld(world);
4542
@Nullable Clipboard clipboard = weHandler.loadClipboardFromSchematic(spongeWorld, arg);
4643
if (clipboard == null) {
47-
player.sendMessage(Text.of(COMMAND_PREFIX + I18nSupport.getInternationalisedString("Command - No schematic")));
44+
player.sendMessage(Component.text(COMMAND_PREFIX + I18nSupport.getInternationalisedString("Command - No schematic")));
4845
return CommandResult.empty();
4946
}
5047
if (Settings.MaxStructureSize > -1 && weHandler.getStructureSize(clipboard) > Settings.MaxStructureSize) {
51-
player.sendMessage(Text.of(COMMAND_PREFIX + I18nSupport.getInternationalisedString("Command - Structure too large")));
48+
player.sendMessage(Component.text(COMMAND_PREFIX + I18nSupport.getInternationalisedString("Command - Structure too large")));
5249
return CommandResult.empty();
5350
}
5451

5552
final ItemStack structureBox = ItemStack.builder().fromBlockState(((BlockType) Settings.StructureBoxItem).getDefaultState()).build();
56-
structureBox.offer(Keys.DISPLAY_NAME, Text.of(Settings.StructureBoxLore));
53+
structureBox.offer(Keys.DISPLAY_NAME, Component.text(Settings.StructureBoxLore));
5754
List<Text> lore = new ArrayList<>();
58-
lore.add(0, Text.of(Settings.StructureBoxPrefix + arg));
55+
lore.add(0, Component.text(Settings.StructureBoxPrefix + arg));
5956
for (int i = 0 ; i < Settings.StructureBoxInstruction.size() ; i++) {
60-
lore.add(Text.of(Settings.StructureBoxInstruction.get(i)));
57+
lore.add(Component.text(Settings.StructureBoxInstruction.get(i)));
6158
}
6259
structureBox.offer(Keys.ITEM_LORE, lore);
6360
PlayerInventory pInv = (PlayerInventory) player.getInventory();
6461
if (!pInv.getMain().canFit(structureBox) && !pInv.getHotbar().canFit(structureBox)) {
65-
player.sendMessage(Text.of(COMMAND_PREFIX + I18nSupport.getInternationalisedString("Command - Insufficient inventory space")));
62+
player.sendMessage(Component.text(COMMAND_PREFIX + I18nSupport.getInternationalisedString("Command - Insufficient inventory space")));
6663
return CommandResult.empty();
6764
}
6865
pInv.offer(structureBox);
69-
player.sendMessage(Text.of(COMMAND_PREFIX + I18nSupport.getInternationalisedString("Command - New structure box created")));
66+
player.sendMessage(Component.text(COMMAND_PREFIX + I18nSupport.getInternationalisedString("Command - New structure box created")));
7067
return CommandResult.success();
7168
}
7269
}

0 commit comments

Comments
 (0)