Command sets coordinates for the spawnflyarea

This commit is contained in:
Maximilian P. Käfer 2020-11-17 18:44:40 +01:00
parent d452f76007
commit 7fe63c08b5
2 changed files with 49 additions and 30 deletions

View File

@ -3,36 +3,40 @@ package dev.kske.spawnfly;
import org.bukkit.command.*;
public class SpawnCommand implements CommandExecutor {
SpawnFly spawnFly;
public SpawnCommand(SpawnFly spawnFly) { this.spawnFly = spawnFly;}
public SpawnCommand(SpawnFly spawnFly) {
this.spawnFly = spawnFly;
}
@Override
public boolean onCommand(CommandSender sender, org.bukkit.command.Command command, String label,
String[] args) {
if(command.getName().equals("setspawnflyarea")) {
if(args.length == 6) {
int[] cords = new int[6];
int cnt = 0;
if (command.getName().equals("setspawnflyarea")) {
if (args.length == 6) {
int[] cords = new int[6];
int cnt = 0;
for (int i = 0; i < args.length; i++) {
try {
int value = Integer.parseInt(args[i]);
cords[i] = value;
//sender.sendMessage("Argument " +(i + 1) + " is " + value);
// sender.sendMessage("Argument " +(i + 1) + " is " + value);
cnt++;
} catch (Exception e) {
//sender.sendMessage("Argument " + (i + 1) + " is not valid as a coordinate!");
// sender.sendMessage("Argument " + (i + 1) + " is not valid as a
// coordinate!");
}
}
if(cnt == 6) {
spawnFly.setCoordinates(cords[0], cords[1], cords[2], cords[3], cords[4], cords[5]);
}
else {
if (cnt == 6) {
spawnFly.setCoordinates(cords[0], cords[1], cords[2], cords[3], cords[4],
cords[5]);
} else {
sender.sendMessage("Please use valid coordinates!");
}
}
} else
sender.sendMessage("Please use 6 coordinates!");
}
return true;
}
}

View File

@ -9,7 +9,8 @@ import org.bukkit.event.entity.EntityToggleGlideEvent;
import org.bukkit.event.player.*;
public class SpawnListener implements Listener {
int x1, y1, z1, x2, y2, z2;
int x1 = 0, y1 = 0, z1 = 0, x2 = 0, y2 = 0, z2 = 0;
Map<String, Boolean> canceledEvents = new HashMap<>();
@ -18,7 +19,8 @@ public class SpawnListener implements Listener {
public void onMove(PlayerMoveEvent e) {
Player player = e.getPlayer();
Location location = player.getLocation();
if(isInSpawn(location) && player.getVelocity().getY() < 0 && !player.isOnGround() && !canceledEvents.get(player.getName()) && player.getFallDistance() > 1) {
if (isInSpawn(location) && player.getVelocity().getY() < 0 && !player.isOnGround()
&& !canceledEvents.get(player.getName()) && player.getFallDistance() > 1) {
player.setGliding(true);
canceledEvents.put(player.getName(), true);
}
@ -35,26 +37,39 @@ public class SpawnListener implements Listener {
}
private boolean isInSpawn(Location location) {
return (location.getX() <= -277 + 3 && location.getX() >= -302 - 3)
&& (location.getZ() <= -172 + 3 && location.getZ() >= -191 - 3);
if (x1 != 0 && y1 != 0 && z1 != 0 && x2 != 0 && y2 != 0 && z2 != 0) {
if (isInRange(x1, x2, (int) location.getX()) && isInRange(y1, y2, (int) location.getY())
&& isInRange(z1, z2, (int) location.getZ())) return true;
}
return false;
}
private boolean isInRange(int rangeEnd1, int rangeEnd2, int intToCheck) {
if (rangeEnd1 > rangeEnd2 && intToCheck <= rangeEnd1 && intToCheck >= rangeEnd2) {
return true;
} else if (rangeEnd1 < rangeEnd2 && intToCheck >= rangeEnd1 && intToCheck <= rangeEnd2) {
return true;
}
return false;
}
@EventHandler
public void cancelEvent(EntityToggleGlideEvent e) {
if(canceledEvents.get(e.getEntity().getName()) && e.getEntity().getVelocity().getY() != 0 && !e.getEntity().isOnGround())
e.setCancelled(true);
if (canceledEvents.get(e.getEntity().getName()) && e.getEntity().getVelocity().getY() != 0
&& !e.getEntity().isOnGround())
e.setCancelled(true);
else {
e.setCancelled(false);
canceledEvents.put(e.getEntity().getName(), false);
}
}
public void setCoordinates(int x1, int y1, int z1, int x2, int y2, int z2) {
this.x1 = x1;
this.y1 = y1;
this.z1 = z1;
this.x2 = x2;
this.y2 = y2;
this.z2 = z2;
this.x1 = x1;
this.y1 = y1;
this.z1 = z1;
this.x2 = x2;
this.y2 = y2;
this.z2 = z2;
}
}
}