Skip to content

Commit a3d1937

Browse files
committed
updates
1 parent 7462346 commit a3d1937

File tree

4 files changed

+111
-2
lines changed

4 files changed

+111
-2
lines changed

src/main/java/com/stemcraft/SMRegion.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,22 @@ public static boolean exists(String name) {
135135
return regionList.containsKey(name.toLowerCase());
136136
}
137137

138+
/**
139+
* Get a region name list
140+
* @return The region names
141+
*/
142+
public static List<String> getRegionNames() {
143+
return new ArrayList<>(regionList.keySet());
144+
}
145+
146+
/**
147+
* Get a region list
148+
* @return The region list
149+
*/
150+
public static List<SMRegion> getRegions() {
151+
return new ArrayList<>(regionList.values());
152+
}
153+
138154
/**
139155
* Get all the regions filtered by World (null for all)
140156
* @param world Filter by world

src/main/java/com/stemcraft/bridges/SMWorldEdit.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
package com.stemcraft.bridges;
22

3+
import com.sk89q.worldedit.LocalSession;
34
import com.sk89q.worldedit.bukkit.BukkitAdapter;
45
import com.sk89q.worldedit.bukkit.WorldEditPlugin;
6+
import com.sk89q.worldedit.math.BlockVector3;
57
import com.sk89q.worldedit.regions.Polygonal2DRegion;
68
import com.sk89q.worldedit.regions.Region;
9+
import com.sk89q.worldedit.regions.RegionSelector;
10+
import com.sk89q.worldedit.regions.selector.CuboidRegionSelector;
11+
import com.sk89q.worldedit.regions.selector.Polygonal2DRegionSelector;
712
import com.stemcraft.STEMCraft;
813
import org.bukkit.Location;
914
import org.bukkit.entity.Player;
1015
import org.bukkit.plugin.Plugin;
1116

1217
import java.util.ArrayList;
1318
import java.util.List;
19+
import java.util.Objects;
1420

1521
public class SMWorldEdit {
1622
private static final boolean initialized = false;
@@ -71,4 +77,56 @@ public static List<Location> getSelection(Player player) {
7177

7278
return points;
7379
}
80+
81+
/**
82+
* Set a players world edit selection
83+
* @param player The player to set
84+
* @param points The selection points
85+
*/
86+
public static void setSelection(Player player, List<Location> points) {
87+
WorldEditPlugin worldEdit = getBase();
88+
if (worldEdit == null || points.size() < 2) {
89+
return;
90+
}
91+
92+
com.sk89q.worldedit.world.World world = BukkitAdapter.adapt(Objects.requireNonNull(points.get(0).getWorld()));
93+
LocalSession session = worldEdit.getSession(player);
94+
RegionSelector selector;
95+
96+
if (points.size() == 2) {
97+
// Cuboid selection
98+
selector = new CuboidRegionSelector(world);
99+
BlockVector3 pos1 = toBlockVector3(points.get(0));
100+
BlockVector3 pos2 = toBlockVector3(points.get(1));
101+
102+
try {
103+
selector.selectPrimary(pos1, null);
104+
selector.selectSecondary(pos2, null);
105+
} catch (Exception e) {
106+
STEMCraft.error(e);
107+
}
108+
} else {
109+
// Polygon selection
110+
selector = new Polygonal2DRegionSelector(world);
111+
for (Location point : points) {
112+
BlockVector3 pos = toBlockVector3(point);
113+
try {
114+
selector.selectPrimary(pos, null);
115+
} catch (Exception e) {
116+
STEMCraft.error(e);
117+
}
118+
}
119+
}
120+
121+
session.setRegionSelector(world, selector);
122+
}
123+
124+
/**
125+
* Convert Location to BlockVector3
126+
* @param location The location to convert
127+
* @return The resulting BlockVector3
128+
*/
129+
private static BlockVector3 toBlockVector3(Location location) {
130+
return BlockVector3.at(location.getX(), location.getY(), location.getZ());
131+
}
74132
}

src/main/java/com/stemcraft/commands/SMCommandRegion.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,19 @@ public class SMCommandRegion extends SMCommand {
1717
public SMCommandRegion() {
1818
super("region");
1919
description("Region management");
20+
alias("rg");
2021
permission("stemcraft.command.region");
2122
tabCompletion("create");
2223
tabCompletion("delete", "{region}");
24+
tabCompletion("select", "{region}");
2325
tabCompletion("list");
2426
tabCompletion("set", "{region}", "teleport-enter");
2527
register();
2628
}
2729

2830
@Override
2931
public String usage() {
30-
return "/region [create|delete|list|load|unload|teleport] [world]";
32+
return "/region [create|select|delete|list|load|unload|teleport] [region]";
3133
}
3234

3335
@Override
@@ -37,7 +39,7 @@ public void execute(SMCommandContext ctx) {
3739
return;
3840
}
3941

40-
String action = ctx.args.shift("create|delete|list|set");
42+
String action = ctx.args.shift("create|delete|list|set|select");
4143
String regionName = "";
4244
SMRegion region = null;
4345

@@ -85,6 +87,16 @@ public void execute(SMCommandContext ctx) {
8587
assert region != null;
8688
region.delete();
8789
ctx.success("Region '{regionName}' deleted", "regionName", regionName);
90+
} else if(action.equalsIgnoreCase("select")) {
91+
assert region != null;
92+
93+
if(ctx.fromConsole()) {
94+
ctx.error("This command requires to be run by a player");
95+
return;
96+
}
97+
98+
SMWorldEdit.setSelection(ctx.player, region.getPoints());
99+
ctx.success("Region '{regionName}' selected", "regionName", regionName);
88100
} else if(action.equalsIgnoreCase("list")) {
89101
Collection<String> worlds = SMWorld.list();
90102

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.stemcraft.tabcompletions;
2+
3+
import com.stemcraft.SMRegion;
4+
import com.stemcraft.SMTabCompletion;
5+
6+
import java.util.List;
7+
8+
public class SMTabCompletionRegion extends SMTabCompletion<SMRegion> {
9+
10+
public SMTabCompletionRegion() {
11+
super("region");
12+
}
13+
14+
@Override
15+
public List<String> completions(String arg) {
16+
return SMRegion.getRegionNames();
17+
}
18+
19+
@Override
20+
public SMRegion resolve(String arg) {
21+
return SMRegion.get(arg);
22+
}
23+
}

0 commit comments

Comments
 (0)