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

View File

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