Skip to content

Commit 061f7cb

Browse files
committed
added region selector
1 parent 42029c8 commit 061f7cb

File tree

6 files changed

+247
-11
lines changed

6 files changed

+247
-11
lines changed

pom.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>me.kodysimpson</groupId>
88
<artifactId>SimpAPI</artifactId>
9-
<version>4.5.1</version>
9+
<version>4.5.2</version>
1010
<packaging>jar</packaging>
1111

1212
<name>SimpAPI</name>
@@ -100,6 +100,12 @@
100100
<version>2.12.4</version>
101101
<scope>compile</scope>
102102
</dependency>
103+
<dependency>
104+
<groupId>org.projectlombok</groupId>
105+
<artifactId>lombok</artifactId>
106+
<version>1.18.32</version>
107+
<scope>provided</scope>
108+
</dependency>
103109
</dependencies>
104110

105111
<!-- <reporting>-->

src/main/java/me/kodysimpson/simpapi/menu/Menu.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import org.bukkit.inventory.InventoryHolder;
1212
import org.bukkit.inventory.ItemStack;
1313
import org.bukkit.inventory.meta.ItemMeta;
14-
import org.bukkit.plugin.Plugin;
1514
import org.jetbrains.annotations.NotNull;
1615

1716
import java.util.Arrays;
@@ -27,13 +26,11 @@ public abstract class Menu implements InventoryHolder {
2726
protected Player p;
2827
protected Inventory inventory;
2928
protected ItemStack FILLER_GLASS = makeItem(Material.GRAY_STAINED_GLASS_PANE, " ");
30-
protected Plugin plugin;
3129

3230
//Constructor for Menu. Pass in a PlayerMenuUtility so that
3331
// we have information on who's menu this is and
3432
// what info is to be transferred
35-
public Menu(Plugin plugin, PlayerMenuUtility playerMenuUtility) {
36-
this.plugin = plugin;
33+
public Menu(PlayerMenuUtility playerMenuUtility) {
3734
this.playerMenuUtility = playerMenuUtility;
3835
this.p = playerMenuUtility.getOwner();
3936
}
@@ -68,7 +65,7 @@ public void open() {
6865
}
6966

7067
public void back() throws MenuManagerException, MenuManagerNotSetupException {
71-
MenuManager.openMenu(playerMenuUtility.lastMenu().getClass(), plugin, playerMenuUtility.getOwner());
68+
MenuManager.openMenu(playerMenuUtility.lastMenu().getClass(), playerMenuUtility.getOwner());
7269
}
7370

7471
protected void reloadItems() {
@@ -80,7 +77,7 @@ protected void reloadItems() {
8077

8178
protected void reload() throws MenuManagerException, MenuManagerNotSetupException {
8279
p.closeInventory();
83-
MenuManager.openMenu(this.getClass(), plugin, p);
80+
MenuManager.openMenu(this.getClass(), p);
8481
}
8582

8683
//Overridden method from the InventoryHolder interface
@@ -90,7 +87,7 @@ protected void reload() throws MenuManagerException, MenuManagerNotSetupExceptio
9087
}
9188

9289
/**
93-
* This will fill all of the empty slots with "filler glass"
90+
* This will fill all the empty slots with "filler glass"
9491
*/
9592
//Helpful utility method to fill all remaining slots with "filler glass"
9693
public void setFillerGlass() {

src/main/java/me/kodysimpson/simpapi/menu/MenuManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ public static void setup(Server server, Plugin plugin) {
5151
* @param player The player to open the menu for
5252
* @throws MenuManagerNotSetupException Thrown if the setup() method has not been called and used properly
5353
*/
54-
public static void openMenu(Class<? extends Menu> menuClass, Plugin plugin, Player player) throws MenuManagerException, MenuManagerNotSetupException {
54+
public static void openMenu(Class<? extends Menu> menuClass, Player player) throws MenuManagerException, MenuManagerNotSetupException {
5555
try {
56-
menuClass.getConstructor(Plugin.class, PlayerMenuUtility.class).newInstance(plugin, getPlayerMenuUtility(player)).open();
56+
menuClass.getConstructor(Plugin.class, PlayerMenuUtility.class).newInstance(getPlayerMenuUtility(player)).open();
5757
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
5858
throw new MenuManagerException();
5959
}

src/main/java/me/kodysimpson/simpapi/menu/PaginatedMenu.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public abstract class PaginatedMenu extends Menu {
2424
protected int index = 0;
2525

2626
public PaginatedMenu(Plugin plugin, PlayerMenuUtility playerMenuUtility) {
27-
super(plugin, playerMenuUtility);
27+
super(playerMenuUtility);
2828
}
2929

3030
/**
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package me.kodysimpson.simpapi.region;
2+
3+
import lombok.Data;
4+
import org.bukkit.Location;
5+
import org.bukkit.World;
6+
import org.bukkit.block.Block;
7+
import org.bukkit.entity.Entity;
8+
import org.bukkit.entity.Player;
9+
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
13+
@Data
14+
public class Region {
15+
16+
private Location corner1;
17+
private Location corner2;
18+
19+
public Region() {
20+
this.corner1 = null;
21+
this.corner2 = null;
22+
}
23+
24+
public boolean isSet(){
25+
return corner1 != null && corner2 != null;
26+
}
27+
28+
public World getWorld(){
29+
return corner1.getWorld();
30+
}
31+
32+
public boolean isIn(final Location loc) {
33+
34+
double xMin = Math.min(corner1.getX(), corner2.getX());
35+
double xMax = Math.max(corner1.getX(), corner2.getX());
36+
double yMin = Math.min(corner1.getY(), corner2.getY());
37+
double yMax = Math.max(corner1.getY(), corner2.getY());
38+
double zMin = Math.min(corner1.getZ(), corner2.getZ());
39+
double zMax = Math.max(corner1.getZ(), corner2.getZ());
40+
41+
//if the location is within the region in the x, y, and z axis
42+
return loc.getBlockX() >= xMin && loc.getBlockX() <= xMax && loc
43+
.getBlockY() >= yMin && loc.getBlockY() <= yMax && loc
44+
.getBlockZ() >= zMin && loc.getBlockZ() <= zMax;
45+
}
46+
47+
public int getTotalBlockSize() {
48+
return (int) (this.getHeight() * this.getXWidth() * this.getZWidth());
49+
}
50+
51+
public double getHeight() {
52+
double yMin = Math.min(corner1.getY(), corner2.getY());
53+
double yMax = Math.max(corner1.getY(), corner2.getY());
54+
return yMax - yMin + 1;
55+
}
56+
57+
public double getXWidth() {
58+
double xMin = Math.min(corner1.getX(), corner2.getX());
59+
double xMax = Math.max(corner1.getX(), corner2.getX());
60+
return xMax - xMin + 1;
61+
}
62+
63+
public double getZWidth() {
64+
double zMin = Math.min(corner1.getZ(), corner2.getZ());
65+
double zMax = Math.max(corner1.getZ(), corner2.getZ());
66+
return zMax - zMin + 1;
67+
}
68+
69+
public List<Block> blockList(World world) {
70+
71+
double xMin = Math.min(corner1.getX(), corner2.getX());
72+
double xMax = Math.max(corner1.getX(), corner2.getX());
73+
double yMin = Math.min(corner1.getY(), corner2.getY());
74+
double yMax = Math.max(corner1.getY(), corner2.getY());
75+
double zMin = Math.min(corner1.getZ(), corner2.getZ());
76+
double zMax = Math.max(corner1.getZ(), corner2.getZ());
77+
78+
final ArrayList<Block> bL = new ArrayList<>(this.getTotalBlockSize());
79+
for(double x = xMin; x <= xMax; ++x) {
80+
for(double y = yMin; y <= yMax; ++y) {
81+
for(double z = zMin; z <= zMax; ++z) {
82+
final Block b = world.getBlockAt((int )x, (int) y, (int) z);
83+
bL.add(b);
84+
}
85+
}
86+
}
87+
return bL;
88+
}
89+
90+
public boolean isPlayerIn(Player player) {
91+
return this.isIn(player.getLocation());
92+
}
93+
94+
public List<Entity> getEntities(){
95+
List<Entity> entities = new ArrayList<>();
96+
for (Block block : blockList(corner1.getWorld())) {
97+
entities.addAll(block.getWorld().getNearbyEntities(block.getLocation(), 1, 1, 1));
98+
}
99+
return entities;
100+
}
101+
102+
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package me.kodysimpson.simpapi.region;
2+
3+
import org.bukkit.Bukkit;
4+
import org.bukkit.ChatColor;
5+
import org.bukkit.Location;
6+
import org.bukkit.World;
7+
import org.bukkit.entity.Entity;
8+
import org.bukkit.entity.EntityType;
9+
import org.bukkit.entity.Slime;
10+
import org.bukkit.scoreboard.Scoreboard;
11+
import org.bukkit.scoreboard.Team;
12+
13+
import static org.bukkit.Bukkit.getServer;
14+
15+
//Created by milo
16+
public class RegionSelector {
17+
18+
public static void killSelectorsWithTag(String inputTag) {
19+
for (World world : getServer().getWorlds()) {
20+
for (Entity entity : world.getEntities()) {
21+
for (String tag : entity.getScoreboardTags()) {
22+
if (tag.equals("regionselector-" + inputTag)) {
23+
entity.remove();
24+
break;
25+
}
26+
}
27+
}
28+
}
29+
}
30+
31+
public void killAllSelectors() {
32+
for (World world : getServer().getWorlds()) {
33+
for (Entity entity : world.getEntities()) {
34+
for (String tag : entity.getScoreboardTags()) {
35+
if (tag.startsWith("regionselector-")) {
36+
entity.remove();
37+
break;
38+
}
39+
}
40+
}
41+
}
42+
}
43+
44+
public void removeTempTeams() {
45+
Scoreboard board = Bukkit.getScoreboardManager().getMainScoreboard();
46+
for (Team team : board.getTeams()) {
47+
if (team.getName().startsWith("regionselector+")) {
48+
team.unregister();
49+
}
50+
}
51+
}
52+
53+
public static void drawSelector(Region region, String id, ChatColor glowColor) {
54+
55+
Location loc1 = region.getCorner1();
56+
Location loc2 = region.getCorner2();
57+
World world = region.getWorld();
58+
if (loc1.getWorld() == null || !loc1.getWorld().equals(loc2.getWorld())) {
59+
return;
60+
}
61+
int minx = Math.min(loc1.getBlockX(), loc2.getBlockX());
62+
int miny = Math.min(loc1.getBlockY(), loc2.getBlockY());
63+
int minz = Math.min(loc1.getBlockZ(), loc2.getBlockZ());
64+
int maxx = Math.max(loc1.getBlockX(), loc2.getBlockX());
65+
int maxy = Math.max(loc1.getBlockY(), loc2.getBlockY());
66+
int maxz = Math.max(loc1.getBlockZ(), loc2.getBlockZ());
67+
68+
Scoreboard board = Bukkit.getScoreboardManager().getMainScoreboard();
69+
70+
int highestNumber = 0;
71+
72+
for (Team team : board.getTeams()) {
73+
if (team.getName().startsWith("regionselector+")) {
74+
String number = team.getName().substring(13);
75+
try {
76+
int tempNumber = Integer.parseInt(number);
77+
if (tempNumber >= highestNumber) {
78+
highestNumber = tempNumber;
79+
}
80+
} catch (NumberFormatException e) {
81+
e.printStackTrace();
82+
}
83+
}
84+
}
85+
86+
Team selectorTeam = board.registerNewTeam("regionselector+" + (highestNumber + 1));
87+
if (glowColor != null) {
88+
selectorTeam.setColor(glowColor);
89+
}
90+
selectorTeam.setOption(Team.Option.COLLISION_RULE, Team.OptionStatus.NEVER);
91+
92+
for (double x = minx; x <= maxx; x++) {
93+
makeSlime(id, world, selectorTeam, x, miny, minz);
94+
makeSlime(id, world, selectorTeam, x, maxy, minz);
95+
makeSlime(id, world, selectorTeam, x, miny, maxz);
96+
makeSlime(id, world, selectorTeam, x, maxy, maxz);
97+
}
98+
99+
for (double y = miny; y <= maxy; y++) {
100+
makeSlime(id, world, selectorTeam, minx, y, minz);
101+
makeSlime(id, world, selectorTeam, maxx, y, minz);
102+
makeSlime(id, world, selectorTeam, minx, y, maxz);
103+
makeSlime(id, world, selectorTeam, maxx, y, maxz);
104+
}
105+
106+
for (double z = minz; z <= maxz; z++) {
107+
makeSlime(id, world, selectorTeam, minx, miny, z);
108+
makeSlime(id, world, selectorTeam, maxx, miny, z);
109+
makeSlime(id, world, selectorTeam, minx, maxy, z);
110+
makeSlime(id, world, selectorTeam, maxx, maxy, z);
111+
}
112+
113+
}
114+
115+
private static void makeSlime(String id, World world, Team selectorTeam, double x, double y, double z) {
116+
Slime slime = (Slime) world.spawnEntity(new Location(world, x + 0.5, y, z + 0.5), EntityType.SLIME);
117+
slime.setSize(2);
118+
slime.setAI(false);
119+
slime.setGravity(false);
120+
slime.setCollidable(false);
121+
slime.setSilent(true);
122+
slime.setCanPickupItems(false);
123+
slime.setGlowing(true);
124+
slime.setInvulnerable(true);
125+
slime.setInvisible(true);
126+
slime.addScoreboardTag("regionselector-" + id);
127+
slime.setPersistent(true);
128+
selectorTeam.addEntry(String.valueOf(slime.getUniqueId()));
129+
}
130+
131+
}

0 commit comments

Comments
 (0)