# Solved Constructors

Discussion in 'Plugin Development' started by TerroDoor, Nov 18, 2019.

Not open for further replies.
1. Offline

### TerroDoor

Hi,

I’ve got 2 locations and I’ve created a 10x10 area of blocks using a method,

I’m trying to figure out how to check if the player is inside this area using another method, eg: playerIsInCube();

If I use a constructor instead to create my Area of blocks will it be called instantly when my sever starts/reloaded?

All help is appreciated

Sent from my iPhone using Tapatalk

#1
2. Online

### timtower Moderator Moderator

@TerroDoor Depends on where you put it.
Put it in the onEnable and you are good.

#2
3. Offline

### TerroDoor

I need to access the newly made location inside my other method and I’m having trouble figuring out how.

I wanna be able to check if a player is inside/between two locations to cancel damage etc

Sent from my iPhone using Tapatalk

#3
4. Online

### timtower Moderator Moderator

@TerroDoor Why not just check the exact values?

#4
5. Offline

### TerroDoor

Instead of creating an area just check if player is between two locations?

Sent from my iPhone using Tapatalk

#5
6. Online

Yes

#6
7. Offline

### TerroDoor

@timtower okay so here's what ive got, it seems to be working but im confusing myself with the Math side of things(im terrible with math)

Here's my setup method for my chosen area, I've used to set locations(pos1, pos2) and given the loc's an X,Y,Z value
it's creating the area, but only glass. im trying to make a hollow outline in pink around the borders with glass inside it.

Code:
```public void setupSpawn() {

double minx = Math.min(pos1.getX(), pos2.getX());
double maxx = Math.max(pos1.getX(), pos2.getX());
double minz = Math.min(pos1.getZ(), pos2.getZ());
double maxz = Math.max(pos1.getZ(), pos2.getZ());
double miny = Math.min(pos1.getY(), pos2.getY());
double maxy = Math.max(pos1.getY(), pos2.getY());

for (double x = minx; x <= maxx; x++) {

for (double y = miny; y <= maxy; y++) {

for (double z = minz; z <= maxz; z++) {

Location loc = new Location(w, x, y, z);
if (x == maxx || y == maxy || z == maxz) {

loc.getBlock().setType(Material.GLASS);
} else {
loc.getBlock().setType(Material.PINK_WOOL);

```
I've also created a Boolean method to return true if the player is inside the area of the two locations, if the player move's outside of the location, I want to build a wall of solid glass so that player cant return back inside(if you see what I mean) is this partly right?

Code:
``` public boolean inSpawn(Player p) {

double maxx = Math.max(pos1.getX(), pos2.getX());
double maxz = Math.max(pos1.getZ(), pos2.getZ());
double maxy = Math.max(pos1.getY(), pos2.getY());

double px = p.getLocation().getX();
double py = p.getLocation().getY();
double pz = p.getLocation().getZ();

if (px >= maxx || py >= maxy || pz >= maxz) {
return false;
} else {
return true;

```

#7
8. Online

### timtower Moderator Moderator

@TerroDoor Why not just make the terrain around spawn 3 blocks lower than the spawn platform itself?
Because what you are doing now is also blocking other players from exiting.

#8
9. Offline

### TerroDoor

I made it a 5 block drop on the y axis for pos2, so now when they leave the area I’m trying to send the player a message “protection is lost” , but in the player move event it’s spamming me.. how can I fix or go around this?

Sent from my iPhone using Tapatalk

#9
10. Online

### timtower Moderator Moderator

List of UUID's with players that left spawn, if the UUID is in that list then don't send the message.
If the player is in spawn (regardless of how) remove the UUID from the list.

#10
11. Offline

### TerroDoor

Where would I add the players into the list?

Sent from my iPhone using Tapatalk

#11
12. Online

### timtower Moderator Moderator

The first time that they are not in spawn anymore, at the same moment you can send your message.

#12
13. Offline

### TerroDoor

Code:
``` public boolean isInSpawn(Player p) {

ArrayList<UUID> inspawn = new ArrayList<UUID>();

double minx = Math.min(pos1.getBlockX(), pos2.getBlockX());
double maxx = Math.max(pos1.getBlockX(), pos2.getBlockX());
double minz = Math.min(pos1.getBlockZ(), pos2.getBlockZ());
double maxz = Math.max(pos1.getBlockZ(), pos2.getBlockZ());
double miny = Math.min(pos1.getBlockY(), pos2.getBlockY());
double maxy = Math.max(pos1.getBlockY(), pos2.getBlockY());
double px = p.getLocation().getX();
double py = p.getLocation().getY();
double pz = p.getLocation().getZ();
if (px >= maxx || px >= minx || py >= miny || py >= maxy || pz >= minz || pz >= maxz) {

inspawn.remove(p.getUniqueId());
p.sendMessage("protection lost");
return false;

} else {

return true;
}

```
@timtower thanks heaps for your help so far, I cant seem to make it work! any tips?

#13
14. Online

### timtower Moderator Moderator

@TerroDoor Move the inspawn variable to the class, not within the method.

#14
15. Offline

### TerroDoor

@timtower still no luck even with my List in the class , is my If statement correct I feel like ive done improper math to calculate it

#15
16. Online

### timtower Moderator Moderator

@TerroDoor Your checks are wrong, you are checking if px>=maxx AND your are checking for px>=minx

#16
17. Offline

### TerroDoor

Not with my pc but I think I understand

If (px >= maxx && px>= minx) || and so on?

Sent from my iPhone using Tapatalk

#17
18. Online

### timtower Moderator Moderator

@TerroDoor No, then px should be over both.
px>=minX && px<=maxx

#18
19. Offline

### TerroDoor

I still can’t get it to work D: do I do the same as you mentioned above for the Y and X? It won’t send a message or anything and there’s no error’s. Is the Boolean method being called without having to call it in another method?

Edit: used playermoveevent to call the method(worked but spams me) is this a smart approach?

Sent from my iPhone using Tapatalk

Last edited: Nov 19, 2019
#19
20. Online

#20
21. Offline

### TerroDoor

@timtower incoming..

Code:
``` World w = Bukkit.getWorld("world");
Location pos1 = new Location(w, 100, 100, 100);
Location pos2 = new Location(w, 120, 90, 120);

ArrayList<Player> inspawn = new ArrayList<Player>();

public boolean isInSpawn(Player p) {

double minx = Math.min(pos1.getBlockX(), pos2.getBlockX());
double maxx = Math.max(pos1.getBlockX(), pos2.getBlockX());
double minz = Math.min(pos1.getBlockZ(), pos2.getBlockZ());
double maxz = Math.max(pos1.getBlockZ(), pos2.getBlockZ());
double miny = Math.min(pos1.getBlockY(), pos2.getBlockY());
double maxy = Math.max(pos1.getBlockY(), pos2.getBlockY());

double x = p.getLocation().getBlockX();
double y = p.getLocation().getBlockY();
double z = p.getLocation().getBlockZ();

if (x > minx && x < maxx
&& y < miny && y > maxy
&& z < minz && z > maxz) {

p.sendMessage("in area");
return true;

} else {

p.sendMessage("out of area");
return false;
}
}
public void setupSpawn() {

double minx = Math.min(pos1.getBlockX(), pos2.getBlockX());
double maxx = Math.max(pos1.getBlockX(), pos2.getBlockX());
double minz = Math.min(pos1.getBlockZ(), pos2.getBlockZ());
double maxz = Math.max(pos1.getBlockZ(), pos2.getBlockZ());
double miny = Math.min(pos1.getBlockY(), pos2.getBlockY());
double maxy = Math.max(pos1.getBlockY(), pos2.getBlockY());
for (double x = minx; x <= maxx; x++) {
for (double y = miny; y <= maxy; y++) {
for (double z = minz; z <= maxz; z++) {
Location loc = new Location(w, x, y, z);
if (x == minx || x == maxx || z == minz || z == maxz || y == miny || y == maxy) {

loc.getBlock().setType(Material.PINK_WOOL);
} else {
loc.getBlock().setType(Material.AIR);
}
}
}
}
return;
}
@EventHandler
public void onMove(PlayerMoveEvent e) {

Player p = (Player)e.getPlayer();

}

```

#21
22. Online

### timtower Moderator Moderator

@TerroDoor Don't store Player objects.
You aren't adding the player to the list, or removing from.

#22
23. Offline

### TerroDoor

How do I initialise a player object in my inSpawn method?

I tried earlier but had no success, do I add them in the move event or my Boolean method

Sent from my iPhone using Tapatalk

#23
24. Online

### timtower Moderator Moderator

@TerroDoor Why would you need to initialize a player object?

#24
25. Offline

### TerroDoor

For the player location to check their XYZ

Sent from my iPhone using Tapatalk

#25
26. Online

### timtower Moderator Moderator

isInSpawn(Player p) {
You have a player object already...

#26
27. Offline

### TerroDoor

What do you mean by “don’t store Player Objects” I’m unsure as to what’s wrong with my code, the p.sendMessage won’t fire

Sent from my iPhone using Tapatalk

#27
28. Online

### timtower Moderator Moderator

Store UUID's, not the Player objects. Because if you store them incorrectly then you will get memory leaks.
And I never see the isInSpawn check being called in your posted code.

#28
29. Offline

### TerroDoor

After a break and revision I’ve gotten it to work, and it’s working well. I add and remove the array from the player in the move event but I still get spammed, how can I stop this? Thanks for your amount of help so far

I also went with Vectors for my pos1,2 as i can use the “isInAABB” method

Sent from my iPhone using Tapatalk

#29
30. Online

#30