|
| 1 | +package com.stemcraft.commands; |
| 2 | + |
| 3 | +import com.stemcraft.*; |
| 4 | +import com.stemcraft.bridges.SMWorldEdit; |
| 5 | +import com.stemcraft.utils.SMUtilsMath; |
| 6 | +import net.md_5.bungee.api.ChatColor; |
| 7 | +import net.md_5.bungee.api.chat.BaseComponent; |
| 8 | +import net.md_5.bungee.api.chat.TextComponent; |
| 9 | +import org.bukkit.Location; |
| 10 | + |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.Collection; |
| 13 | +import java.util.List; |
| 14 | + |
| 15 | +public class SMCommandRegion extends SMCommand { |
| 16 | + |
| 17 | + public SMCommandRegion() { |
| 18 | + super("region"); |
| 19 | + description("Region management"); |
| 20 | + permission("stemcraft.command.region"); |
| 21 | + tabCompletion("create"); |
| 22 | + tabCompletion("delete", "{region}"); |
| 23 | + tabCompletion("list"); |
| 24 | + tabCompletion("set", "{region}", "teleport-enter"); |
| 25 | + register(); |
| 26 | + } |
| 27 | + |
| 28 | + @Override |
| 29 | + public String usage() { |
| 30 | + return "/region [create|delete|list|load|unload|teleport] [world]"; |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public void execute(SMCommandContext ctx) { |
| 35 | + if(ctx.args.wantsHelp()) { |
| 36 | + ctx.usage(); |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + String action = ctx.args.shift("create|delete|list|set"); |
| 41 | + String regionName = ""; |
| 42 | + SMRegion region = null; |
| 43 | + |
| 44 | + if(action == null) { |
| 45 | + ctx.usage(); |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + if(!action.equalsIgnoreCase("list")) { |
| 50 | + regionName = ctx.args.shift(); |
| 51 | + |
| 52 | + if (regionName.isEmpty()) { |
| 53 | + ctx.error("You must specify a region name"); |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + region = SMRegion.get(regionName); |
| 58 | + |
| 59 | + if(region == null && !action.equalsIgnoreCase("create")) { |
| 60 | + ctx.error("The region '{regionName}' does not exists", "regionName", regionName); |
| 61 | + return; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + if(action.equalsIgnoreCase("create")) { |
| 66 | + if(ctx.fromConsole()) { |
| 67 | + ctx.error("Command must be run by a player"); |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + List<Location> points = SMWorldEdit.getSelection(ctx.player); |
| 72 | + if(points == null) { |
| 73 | + ctx.error("You do not have a world edit selection"); |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + if(SMRegion.exists(regionName)) { |
| 78 | + ctx.error("A region with that name already exists"); |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + SMRegion.create(regionName, points); |
| 83 | + ctx.success("Region '{regionName}' created", "regionName", regionName); |
| 84 | + } else if(action.equalsIgnoreCase("delete")) { |
| 85 | + assert region != null; |
| 86 | + region.delete(); |
| 87 | + ctx.success("Region '{regionName}' deleted", "regionName", regionName); |
| 88 | + } else if(action.equalsIgnoreCase("list")) { |
| 89 | + Collection<String> worlds = SMWorld.list(); |
| 90 | + |
| 91 | + new SMPaginate(ctx.sender, 1) |
| 92 | + .count(worlds.size()) |
| 93 | + .command("world list") |
| 94 | + .title("Worlds") |
| 95 | + .none("No worlds where found") |
| 96 | + .showItems((start, max) -> { |
| 97 | + List<BaseComponent[]> rows = new ArrayList<>(); |
| 98 | + int end = Math.min(start + max, worlds.size()); |
| 99 | + |
| 100 | + for (String worldName : worlds.stream().skip(start).limit(end - start).toList()) { |
| 101 | + BaseComponent[] row = new BaseComponent[2]; |
| 102 | + row[0] = new TextComponent(ChatColor.GOLD + worldName + " "); |
| 103 | + |
| 104 | + SMWorld smWorld = new SMWorld(worldName); |
| 105 | + if (smWorld.isLoaded()) { |
| 106 | + row[1] = new TextComponent(ChatColor.GREEN + "Loaded"); |
| 107 | + } else { |
| 108 | + row[1] = new TextComponent(ChatColor.RED + "Not loaded"); |
| 109 | + } |
| 110 | + |
| 111 | + rows.add(row); |
| 112 | + } |
| 113 | + |
| 114 | + return rows; |
| 115 | + }); |
| 116 | + } else if(action.equalsIgnoreCase("set")) { |
| 117 | + String subAction = ctx.args.shift("teleport-enter|teleport-exit"); |
| 118 | + |
| 119 | + if(ctx.fromConsole()) { |
| 120 | + ctx.error("Command must be run by a player"); |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + if(subAction == null) { |
| 125 | + ctx.usage(); |
| 126 | + } else if(subAction.equalsIgnoreCase("teleport-enter")) { |
| 127 | + Location location = SMUtilsMath.round(ctx.player.getLocation()); |
| 128 | + region.setTeleportEnter(location); |
| 129 | + region.save(); |
| 130 | + |
| 131 | + ctx.success("The 'teleport-enter' flag has been set"); |
| 132 | + } |
| 133 | + } else { |
| 134 | + ctx.error("Unknown action: " + action); |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments