diff --git a/TODO.md b/TODO.md index 8bf7801..4d61850 100644 --- a/TODO.md +++ b/TODO.md @@ -26,13 +26,13 @@ | MET-022 | Planning | (Optional) Add new features, quality-of-life improvements, or CI configuration after the modernization and documentation tasks are complete. | | GIT-009 | Planning | i have three reserve plots and one just got overwritten when a new player joined. | | GIT-008 | Ready | Implement multiple plot sizes by granting multiple plots around each other. -| GIT-007 | Ready | Allow buying and selling of homes via an ecnoomy plugin. | +| GIT-007 | Planning | Allow buying and selling of homes via an ecnoomy plugin. | | GIT-006 | Ready | Add levels support. | | GIT-005 | Planning | Add the possibility to create multiple plot homes in shapes. | -| GIT-004 | Ready | Add a command to request home allocation. | +| GIT-004 | Planning | Add a command to request home allocation. | | GIT-003 | Ready | Add support for multiple homes. | -| GIT-002 | Ready | Add a command to teleport to a reservation you are part of. | -| GIT-001 | Ready | Add a command to move to a different home. | +| GIT-002 | Planning | Add a command to teleport to a reservation you are part of. | +| GIT-001 | Planning | Add a command to move to a different home. | # GIT-001 - Add a command to move to a different home. **Status:** Ready diff --git a/build.gradle b/build.gradle index 8344484..29c7cfc 100644 --- a/build.gradle +++ b/build.gradle @@ -1,4 +1,5 @@ plugins { + id 'com.diffplug.spotless' version '8.10.3' id 'java-library' id 'maven-publish' id 'org.danilopianini.git-sensitive-semantic-versioning' version '7.0.24' @@ -87,4 +88,15 @@ tasks.named('wrapper') { distributionPath = '.gradle/wrapper/dists' archiveBase = Wrapper.PathBase.PROJECT archivePath = '.gradle/wrapper/dists' +} + +spotless { + java { + googleJavaFormat('1.36.1') + importOrder 'java', 'javax', 'com', 'net', 'org', '' + licenseHeaderFile 'config/license-header.txt' + target 'src/**/*.java' + removeUnusedImports() + toggleOffOn() + } } \ No newline at end of file diff --git a/config/license-header.txt b/config/license-header.txt new file mode 100644 index 0000000..10969f3 --- /dev/null +++ b/config/license-header.txt @@ -0,0 +1,17 @@ +/* +This file is part of Metropolis. + +Metropolis is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +Metropolis is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with Metropolis. If not, see . +*/ + diff --git a/src/integrationTest/java/com/majinnaibu/bukkitplugins/metropolis/MetropolisServerIntegrationTest.java b/src/integrationTest/java/com/majinnaibu/bukkitplugins/metropolis/MetropolisServerIntegrationTest.java index aabfad5..8e2a15d 100644 --- a/src/integrationTest/java/com/majinnaibu/bukkitplugins/metropolis/MetropolisServerIntegrationTest.java +++ b/src/integrationTest/java/com/majinnaibu/bukkitplugins/metropolis/MetropolisServerIntegrationTest.java @@ -1,3 +1,20 @@ +/* +This file is part of Metropolis. + +Metropolis is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +Metropolis is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with Metropolis. If not, see . +*/ + package com.majinnaibu.bukkitplugins.metropolis; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -15,110 +32,114 @@ import org.bukkit.configuration.file.YamlConfiguration; import org.junit.jupiter.api.Test; class MetropolisServerIntegrationTest { - @Test - void runningPaperLoadsMetropolis() throws IOException { - String installedPlugins; - try (RconClient rcon = connectToConfiguredServer()) { - installedPlugins = rcon.execute("plugins"); - } - assertTrue(installedPlugins.contains("Metropolis"), installedPlugins); - } + @Test + void runningPaperLoadsMetropolis() throws IOException { + String installedPlugins; + try (RconClient rcon = connectToConfiguredServer()) { + installedPlugins = rcon.execute("plugins"); + } + assertTrue(installedPlugins.contains("Metropolis"), installedPlugins); + } - @Test - void plotReservationIsPersistedByTheLoadedWorldGuardPlugin() throws IOException { - String regionId = "metropolis-it-" + UUID.randomUUID().toString().substring(0, 8); - try (RconClient rcon = connectToConfiguredServer()) { - String response = rcon.execute("metropolis-plot-reserve " + regionId + " 100 60 100 105 70 105"); - assertTrue(response.contains("Reserved plot region " + regionId), response); - } + @Test + void plotReservationIsPersistedByTheLoadedWorldGuardPlugin() throws IOException { + String regionId = "metropolis-it-" + UUID.randomUUID().toString().substring(0, 8); + try (RconClient rcon = connectToConfiguredServer()) { + String response = + rcon.execute("metropolis-plot-reserve " + regionId + " 100 60 100 105 70 105"); + assertTrue(response.contains("Reserved plot region " + regionId), response); + } - Path serverDirectory = Path.of(requiredEnvironment("METROPOLIS_SERVER_DIR")); - String worldName = System.getenv().getOrDefault("METROPOLIS_WORLD", "world"); - Path regionsFile = serverDirectory.resolve("plugins/WorldGuard/worlds") - .resolve(worldName) - .resolve("regions.yml"); - YamlConfiguration regions = YamlConfiguration.loadConfiguration(regionsFile.toFile()); + Path serverDirectory = Path.of(requiredEnvironment("METROPOLIS_SERVER_DIR")); + String worldName = System.getenv().getOrDefault("METROPOLIS_WORLD", "world"); + Path regionsFile = + serverDirectory + .resolve("plugins/WorldGuard/worlds") + .resolve(worldName) + .resolve("regions.yml"); + YamlConfiguration regions = YamlConfiguration.loadConfiguration(regionsFile.toFile()); - assertTrue(regions.contains("regions." + regionId), "WorldGuard did not persist region " + regionId); - } + assertTrue( + regions.contains("regions." + regionId), "WorldGuard did not persist region " + regionId); + } - private static RconClient connectToConfiguredServer() throws IOException { - String host = requiredEnvironment("METROPOLIS_RCON_HOST"); - int port = Integer.parseInt(requiredEnvironment("METROPOLIS_RCON_PORT")); - String password = requiredEnvironment("METROPOLIS_RCON_PASSWORD"); - return new RconClient(host, port, password); - } + private static RconClient connectToConfiguredServer() throws IOException { + String host = requiredEnvironment("METROPOLIS_RCON_HOST"); + int port = Integer.parseInt(requiredEnvironment("METROPOLIS_RCON_PORT")); + String password = requiredEnvironment("METROPOLIS_RCON_PASSWORD"); + return new RconClient(host, port, password); + } - private static String requiredEnvironment(String name) { - String value = System.getenv(name); - if (value == null || value.isBlank()) { - throw new IllegalStateException("Set " + name + " to run integrationTest."); - } - return value; - } + private static String requiredEnvironment(String name) { + String value = System.getenv(name); + if (value == null || value.isBlank()) { + throw new IllegalStateException("Set " + name + " to run integrationTest."); + } + return value; + } - private static final class RconClient implements AutoCloseable { - private final Socket socket = new Socket(); - private final DataInputStream input; - private final DataOutputStream output; - private int requestId = 1; + private static final class RconClient implements AutoCloseable { + private final Socket socket = new Socket(); + private final DataInputStream input; + private final DataOutputStream output; + private int requestId = 1; - private RconClient(String host, int port, String password) throws IOException { - socket.connect(new InetSocketAddress(host, port), 5000); - socket.setSoTimeout(5000); - input = new DataInputStream(socket.getInputStream()); - output = new DataOutputStream(socket.getOutputStream()); - int authId = requestId++; - Packet response = send(authId, 3, password); - if (response.id != authId) { - throw new IOException("RCON authentication failed."); - } - } + private RconClient(String host, int port, String password) throws IOException { + socket.connect(new InetSocketAddress(host, port), 5000); + socket.setSoTimeout(5000); + input = new DataInputStream(socket.getInputStream()); + output = new DataOutputStream(socket.getOutputStream()); + int authId = requestId++; + Packet response = send(authId, 3, password); + if (response.id != authId) { + throw new IOException("RCON authentication failed."); + } + } - private String execute(String command) throws IOException { - return send(requestId++, 2, command).payload; - } + private String execute(String command) throws IOException { + return send(requestId++, 2, command).payload; + } - private Packet send(int id, int type, String payload) throws IOException { - byte[] data = payload.getBytes(StandardCharsets.UTF_8); - writeLittleEndianInt(data.length + 10); - writeLittleEndianInt(id); - writeLittleEndianInt(type); - output.write(data); - output.writeByte(0); - output.writeByte(0); - output.flush(); + private Packet send(int id, int type, String payload) throws IOException { + byte[] data = payload.getBytes(StandardCharsets.UTF_8); + writeLittleEndianInt(data.length + 10); + writeLittleEndianInt(id); + writeLittleEndianInt(type); + output.write(data); + output.writeByte(0); + output.writeByte(0); + output.flush(); - int length = readLittleEndianInt(); - byte[] packet = input.readNBytes(length); - if (packet.length != length || length < 10) { - throw new IOException("Received an invalid RCON packet."); - } - int responseId = littleEndianInt(packet, 0); - String response = new String(packet, 8, length - 10, StandardCharsets.UTF_8); - return new Packet(responseId, response); - } + int length = readLittleEndianInt(); + byte[] packet = input.readNBytes(length); + if (packet.length != length || length < 10) { + throw new IOException("Received an invalid RCON packet."); + } + int responseId = littleEndianInt(packet, 0); + String response = new String(packet, 8, length - 10, StandardCharsets.UTF_8); + return new Packet(responseId, response); + } - private void writeLittleEndianInt(int value) throws IOException { - output.writeInt(Integer.reverseBytes(value)); - } + private void writeLittleEndianInt(int value) throws IOException { + output.writeInt(Integer.reverseBytes(value)); + } - private int readLittleEndianInt() throws IOException { - return Integer.reverseBytes(input.readInt()); - } + private int readLittleEndianInt() throws IOException { + return Integer.reverseBytes(input.readInt()); + } - private static int littleEndianInt(byte[] bytes, int offset) { - return (bytes[offset] & 0xff) - | ((bytes[offset + 1] & 0xff) << 8) - | ((bytes[offset + 2] & 0xff) << 16) - | (bytes[offset + 3] << 24); - } + private static int littleEndianInt(byte[] bytes, int offset) { + return (bytes[offset] & 0xff) + | ((bytes[offset + 1] & 0xff) << 8) + | ((bytes[offset + 2] & 0xff) << 16) + | (bytes[offset + 3] << 24); + } - @Override - public void close() throws IOException { - socket.close(); - } - } + @Override + public void close() throws IOException { + socket.close(); + } + } - private record Packet(int id, String payload) {} -} \ No newline at end of file + private record Packet(int id, String payload) {} +} diff --git a/src/main/java/com/majinnaibu/bukkitplugins/metropolis/Cuboid.java b/src/main/java/com/majinnaibu/bukkitplugins/metropolis/Cuboid.java index ec107bb..8438f85 100644 --- a/src/main/java/com/majinnaibu/bukkitplugins/metropolis/Cuboid.java +++ b/src/main/java/com/majinnaibu/bukkitplugins/metropolis/Cuboid.java @@ -1,220 +1,265 @@ +/* +This file is part of Metropolis. + +Metropolis is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +Metropolis is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with Metropolis. If not, see . +*/ + package com.majinnaibu.bukkitplugins.metropolis; - -import org.bukkit.Location; - import com.sk89q.worldedit.math.BlockVector3; import com.sk89q.worldedit.regions.Region; +import org.bukkit.Location; + public class Cuboid implements Comparable { - private int id; - - public int minX; - public int minY; - public int minZ; - - public int maxX; - public int maxY; - public int maxZ; - - public int getId(){ - return id; - } - - public void setId(int id){ - this.id = id; - } - - public Cuboid(int minX, int minY, int minZ, int maxX, int maxY, int maxZ){ - this.minX = minX; - this.minY = minY; - this.minZ = minZ; - this.maxX = maxX; - this.maxY = maxY; - this.maxZ = maxZ; - } - - public Cuboid(BlockVector3 min, BlockVector3 max) { - this.minX = min.x(); - this.minY = min.y(); - this.minZ = min.z(); - this.maxX = max.x(); - this.maxY = max.y(); - this.maxZ = max.z(); - } + private int id; - public Cuboid() { - this.minX = 0; - this.minY = 0; - this.minZ = 0; - this.maxX = 0; - this.maxY = 0; - this.maxZ = 0; - } + public int minX; + public int minY; + public int minZ; - public Cuboid(Region selection) { - this.minX = selection.getMinimumPoint().x(); - this.minY = selection.getMinimumPoint().y(); - this.minZ = selection.getMinimumPoint().z(); - this.maxX = selection.getMaximumPoint().x(); - this.maxY = selection.getMaximumPoint().y(); - this.maxZ = selection.getMaximumPoint().z(); - } + public int maxX; + public int maxY; + public int maxZ; - public BlockVector3 getMin(){ - return BlockVector3.at(minX, minY, minZ); - } - - public BlockVector3 getMax(){ - return BlockVector3.at(maxX, maxY, maxZ); - } + public int getId() { + return id; + } - @Override - public int compareTo(Cuboid o) { - BlockVector3 min = getMin(); - BlockVector3 otherMin = o.getMin(); - - if(min.x() < otherMin.x()){ - return -1; - }else if(min.x() > otherMin.x()){ - return 1; - }else if(min.z() < otherMin.z()){ - return -1; - }else if(min.z() > otherMin.z()){ - return 1; - }else if(min.y() < otherMin.y()){ - return -1; - }else if(min.y() > otherMin.y()){ - return 1; - }else{ - return 0; - } - } + public void setId(int id) { + this.id = id; + } - public static int compareBlockVectors(BlockVector3 v1, BlockVector3 v2){ - if(v1 == null){ - if(v2 == null){ - return 0; - }else{ - return -1; - } - }else if(v2 == null){ - return 1; - } - if(v1.x() < v2.x()){ - return -1; - }else if(v1.x() > v2.x()){ - return 1; - }else if(v1.z() < v2.z()){ - return -1; - }else if(v1.z() > v2.z()){ - return 1; - }else if(v1.y() < v2.y()){ - return -1; - }else if(v1.y() > v2.y()){ - return 1; - }else{ - return 0; - } - } - - public static boolean isBlockLessThan(BlockVector3 v1, BlockVector3 v2) { - return compareBlockVectors(v1, v2) < 0; - } - - public Cuboid inset(int amount){ - return inset(amount, amount, amount); - } - - public Cuboid inset(int x, int z){ - return inset(x, 0, z); - } - - public Cuboid inset(int x, int y, int z){ - return new Cuboid(this.minX + x, this.minY, this.minZ + z, this.maxX - x, this.maxY, this.maxZ - z); - } + public Cuboid(int minX, int minY, int minZ, int maxX, int maxY, int maxZ) { + this.minX = minX; + this.minY = minY; + this.minZ = minZ; + this.maxX = maxX; + this.maxY = maxY; + this.maxZ = maxZ; + } - public int getVolume() { - return (this.maxX - this.minX) * (this.maxY - this.minY) * (this.maxZ - this.minZ); - } - public int getMinX(){return minX;} - public void setMinX(int minX){this.minX = minX;} - public int getMinY(){return minY;} - public void setMinY(int minY){this.minY = minY;} - public int getMinZ(){return minZ;} - public void setMinZ(int minZ){this.minZ = minZ;} - public int getMaxX(){return maxX;} - public void setMaxX(int maxX){this.maxX = maxX;} - public int getMaxY(){return maxY;} - public void setMaxY(int maxY){this.maxY = maxY;} - public int getMaxZ(){return maxZ;} - public void setMaxZ(int maxZ){this.maxZ = maxZ;} + public Cuboid(BlockVector3 min, BlockVector3 max) { + this.minX = min.x(); + this.minY = min.y(); + this.minZ = min.z(); + this.maxX = max.x(); + this.maxY = max.y(); + this.maxZ = max.z(); + } - public Cuboid outset(int amount) { - return outset(amount, amount, amount); - } + public Cuboid() { + this.minX = 0; + this.minY = 0; + this.minZ = 0; + this.maxX = 0; + this.maxY = 0; + this.maxZ = 0; + } - public Cuboid outset(int x, int z){ - return outset(x, 0, z); - } - - public Cuboid outset(int x, int y, int z) { - return new Cuboid(this.minX - x, this.minY - y, this.minZ - z, this.maxX + x, this.maxY + y, this.maxZ + z); - } - - public int getCenterX(){ - return (this.minX + this.maxX) /2; - } - - public int getCenterY(){ - return (this.minY + this.maxY)/2; - } - - public int getCenterZ(){ - return (this.minZ + this.maxZ)/2; - } + public Cuboid(Region selection) { + this.minX = selection.getMinimumPoint().x(); + this.minY = selection.getMinimumPoint().y(); + this.minZ = selection.getMinimumPoint().z(); + this.maxX = selection.getMaximumPoint().x(); + this.maxY = selection.getMaximumPoint().y(); + this.maxZ = selection.getMaximumPoint().z(); + } - public boolean intersects(Cuboid other) { - if (this.maxX >= other.minX && this.minX <= other.maxX) - { - if (this.maxZ >= other.minZ && this.minZ <= other.maxZ) - { - if (this.maxY >= other.minY && this.minY <= other.maxY) - { - return true; - } - else - { - return false; - } - } - else - { - return false; - } + public BlockVector3 getMin() { + return BlockVector3.at(minX, minY, minZ); + } + + public BlockVector3 getMax() { + return BlockVector3.at(maxX, maxY, maxZ); + } + + @Override + public int compareTo(Cuboid o) { + BlockVector3 min = getMin(); + BlockVector3 otherMin = o.getMin(); + + if (min.x() < otherMin.x()) { + return -1; + } else if (min.x() > otherMin.x()) { + return 1; + } else if (min.z() < otherMin.z()) { + return -1; + } else if (min.z() > otherMin.z()) { + return 1; + } else if (min.y() < otherMin.y()) { + return -1; + } else if (min.y() > otherMin.y()) { + return 1; + } else { + return 0; + } + } + + public static int compareBlockVectors(BlockVector3 v1, BlockVector3 v2) { + if (v1 == null) { + if (v2 == null) { + return 0; + } else { + return -1; + } + } else if (v2 == null) { + return 1; + } + if (v1.x() < v2.x()) { + return -1; + } else if (v1.x() > v2.x()) { + return 1; + } else if (v1.z() < v2.z()) { + return -1; + } else if (v1.z() > v2.z()) { + return 1; + } else if (v1.y() < v2.y()) { + return -1; + } else if (v1.y() > v2.y()) { + return 1; + } else { + return 0; + } + } + + public static boolean isBlockLessThan(BlockVector3 v1, BlockVector3 v2) { + return compareBlockVectors(v1, v2) < 0; + } + + public Cuboid inset(int amount) { + return inset(amount, amount, amount); + } + + public Cuboid inset(int x, int z) { + return inset(x, 0, z); + } + + public Cuboid inset(int x, int y, int z) { + return new Cuboid( + this.minX + x, this.minY, this.minZ + z, this.maxX - x, this.maxY, this.maxZ - z); + } + + public int getVolume() { + return (this.maxX - this.minX) * (this.maxY - this.minY) * (this.maxZ - this.minZ); + } + + public int getMinX() { + return minX; + } + + public void setMinX(int minX) { + this.minX = minX; + } + + public int getMinY() { + return minY; + } + + public void setMinY(int minY) { + this.minY = minY; + } + + public int getMinZ() { + return minZ; + } + + public void setMinZ(int minZ) { + this.minZ = minZ; + } + + public int getMaxX() { + return maxX; + } + + public void setMaxX(int maxX) { + this.maxX = maxX; + } + + public int getMaxY() { + return maxY; + } + + public void setMaxY(int maxY) { + this.maxY = maxY; + } + + public int getMaxZ() { + return maxZ; + } + + public void setMaxZ(int maxZ) { + this.maxZ = maxZ; + } + + public Cuboid outset(int amount) { + return outset(amount, amount, amount); + } + + public Cuboid outset(int x, int z) { + return outset(x, 0, z); + } + + public Cuboid outset(int x, int y, int z) { + return new Cuboid( + this.minX - x, this.minY - y, this.minZ - z, this.maxX + x, this.maxY + y, this.maxZ + z); + } + + public int getCenterX() { + return (this.minX + this.maxX) / 2; + } + + public int getCenterY() { + return (this.minY + this.maxY) / 2; + } + + public int getCenterZ() { + return (this.minZ + this.maxZ) / 2; + } + + public boolean intersects(Cuboid other) { + if (this.maxX >= other.minX && this.minX <= other.maxX) { + if (this.maxZ >= other.minZ && this.minZ <= other.maxZ) { + if (this.maxY >= other.minY && this.minY <= other.maxY) { + return true; + } else { + return false; } - else - { - return false; - } - } + } else { + return false; + } + } else { + return false; + } + } - public boolean contains(Location bedSpawn) { - if(this.minX > bedSpawn.getBlockX() || this.maxX < bedSpawn.getBlockX()){ - return false; - }else if(this.minZ > bedSpawn.getBlockZ() || this.maxZ < bedSpawn.getBlockZ()){ - return false; - }else if(this.minY > bedSpawn.getBlockY() || this.maxY < bedSpawn.getBlockY()){ - return false; - } - - return true; - } + public boolean contains(Location bedSpawn) { + if (this.minX > bedSpawn.getBlockX() || this.maxX < bedSpawn.getBlockX()) { + return false; + } else if (this.minZ > bedSpawn.getBlockZ() || this.maxZ < bedSpawn.getBlockZ()) { + return false; + } else if (this.minY > bedSpawn.getBlockY() || this.maxY < bedSpawn.getBlockY()) { + return false; + } - @Override - public String toString() { - return String.format("{Cuboid minX=%d, minY=%d, minZ=%d, maxX=%d, maxY=%d, maxZ=%d}", minX, minY, minZ, maxX, maxY, maxZ); - } - - + return true; + } + + @Override + public String toString() { + return String.format( + "{Cuboid minX=%d, minY=%d, minZ=%d, maxX=%d, maxY=%d, maxZ=%d}", + minX, minY, minZ, maxX, maxY, maxZ); + } } diff --git a/src/main/java/com/majinnaibu/bukkitplugins/metropolis/CurrentHomesStore.java b/src/main/java/com/majinnaibu/bukkitplugins/metropolis/CurrentHomesStore.java index 58ad13f..0caf3de 100644 --- a/src/main/java/com/majinnaibu/bukkitplugins/metropolis/CurrentHomesStore.java +++ b/src/main/java/com/majinnaibu/bukkitplugins/metropolis/CurrentHomesStore.java @@ -1,3 +1,20 @@ +/* +This file is part of Metropolis. + +Metropolis is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +Metropolis is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with Metropolis. If not, see . +*/ + package com.majinnaibu.bukkitplugins.metropolis; import java.io.File; @@ -11,40 +28,40 @@ import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.YamlConfiguration; final class CurrentHomesStore { - private CurrentHomesStore() {} + private CurrentHomesStore() {} - static Map load(File file, Function legacyOwnerResolver) - throws IOException, InvalidConfigurationException { - Map homesByOwner = new HashMap<>(); - if (!file.exists()) { - return homesByOwner; - } + static Map load(File file, Function legacyOwnerResolver) + throws IOException, InvalidConfigurationException { + Map homesByOwner = new HashMap<>(); + if (!file.exists()) { + return homesByOwner; + } - YamlConfiguration yaml = new YamlConfiguration(); - yaml.load(file); - for (String ownerKey : yaml.getKeys(false)) { - UUID ownerId; - try { - ownerId = UUID.fromString(ownerKey); - } catch (IllegalArgumentException ex) { - ownerId = legacyOwnerResolver.apply(ownerKey); - } - int homeNumber = yaml.getInt(ownerKey, 0); - if (ownerId != null && homeNumber > 0) { - homesByOwner.put(ownerId, homeNumber); - } - } - return homesByOwner; - } + YamlConfiguration yaml = new YamlConfiguration(); + yaml.load(file); + for (String ownerKey : yaml.getKeys(false)) { + UUID ownerId; + try { + ownerId = UUID.fromString(ownerKey); + } catch (IllegalArgumentException ex) { + ownerId = legacyOwnerResolver.apply(ownerKey); + } + int homeNumber = yaml.getInt(ownerKey, 0); + if (ownerId != null && homeNumber > 0) { + homesByOwner.put(ownerId, homeNumber); + } + } + return homesByOwner; + } - static void save(File file, Map homesByOwner) throws IOException { - File parent = file.getParentFile(); - if (parent != null) { - parent.mkdirs(); - } + static void save(File file, Map homesByOwner) throws IOException { + File parent = file.getParentFile(); + if (parent != null) { + parent.mkdirs(); + } - YamlConfiguration yaml = new YamlConfiguration(); - homesByOwner.forEach((ownerId, homeNumber) -> yaml.set(ownerId.toString(), homeNumber)); - yaml.save(file); - } -} \ No newline at end of file + YamlConfiguration yaml = new YamlConfiguration(); + homesByOwner.forEach((ownerId, homeNumber) -> yaml.set(ownerId.toString(), homeNumber)); + yaml.save(file); + } +} diff --git a/src/main/java/com/majinnaibu/bukkitplugins/metropolis/MetropolisPlugin.java b/src/main/java/com/majinnaibu/bukkitplugins/metropolis/MetropolisPlugin.java index 9f08531..3358a3d 100644 --- a/src/main/java/com/majinnaibu/bukkitplugins/metropolis/MetropolisPlugin.java +++ b/src/main/java/com/majinnaibu/bukkitplugins/metropolis/MetropolisPlugin.java @@ -1,1154 +1,1240 @@ -package com.majinnaibu.bukkitplugins.metropolis; - -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Set; -import java.util.UUID; - -import org.bukkit.Location; -import org.bukkit.Material; -import org.bukkit.OfflinePlayer; -import org.bukkit.Server; -import org.bukkit.World; -import org.bukkit.block.Block; -import org.bukkit.block.Sign; -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.command.PluginCommand; -import org.bukkit.configuration.Configuration; -import org.bukkit.entity.Player; -import org.bukkit.plugin.Plugin; -import org.bukkit.plugin.PluginDescriptionFile; -import org.bukkit.plugin.PluginManager; -import org.bukkit.plugin.java.JavaPlugin; - -import com.majinnaibu.bukkitplugins.metropolis.commands.MetropolisCommand; -import com.majinnaibu.bukkitplugins.metropolis.commands.MetropolisDebugGenerateTestHomesCommand; -import com.majinnaibu.bukkitplugins.metropolis.commands.MetropolisFlagResetCommand; -import com.majinnaibu.bukkitplugins.metropolis.commands.MetropolisHomeEvictCommand; -import com.majinnaibu.bukkitplugins.metropolis.commands.MetropolisHomeAcquire; -import com.majinnaibu.bukkitplugins.metropolis.commands.MetropolisHomeGenerateCommand; -import com.majinnaibu.bukkitplugins.metropolis.commands.MetropolisHomeGoCommand; -import com.majinnaibu.bukkitplugins.metropolis.commands.MetropolisHomeListCommand; -import com.majinnaibu.bukkitplugins.metropolis.commands.MetropolisHomeMoveCommand; -import com.majinnaibu.bukkitplugins.metropolis.commands.MetropolisPlotGoCommand; -import com.majinnaibu.bukkitplugins.metropolis.commands.MetropolisPlotReserveCommand; -import com.majinnaibu.bukkitplugins.metropolis.eventlisteners.PlayerJoinListener; -import com.sk89q.worldedit.bukkit.BukkitAdapter; -import com.sk89q.worldedit.math.BlockVector3; -import com.sk89q.worldedit.bukkit.WorldEditPlugin; -import com.sk89q.worldguard.WorldGuard; -import com.sk89q.worldguard.bukkit.WorldGuardPlugin; -import com.sk89q.worldguard.domains.DefaultDomain; -import com.sk89q.worldguard.protection.flags.Flags; -import com.sk89q.worldguard.protection.flags.StateFlag; -import com.sk89q.worldguard.protection.managers.RegionManager; -import com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion; -import com.sk89q.worldguard.protection.regions.ProtectedRegion; - -public class MetropolisPlugin extends JavaPlugin { - public static final boolean DEBUG = false; - private static final int version = 1; - - public static final int ROAD_NORTH=1; - public static final int ROAD_SOUTH=2; - public static final int ROAD_EAST=4; - public static final int ROAD_WEST=8; - - public PluginDescriptionFile pdf = null; - public WorldGuardPlugin worldGuard = null; - public WorldEditPlugin worldEdit = null; - public World world = null; - public RegionManager regionManager = null; - - private List _occupiedPlots; - private HashMap> _ownedPlots; - private HashMap _userOverrides; - private HashMap _currentHomes; - - private PlayerJoinListener _playerJoinListener = null; - - int size = 1; - - private int plotSizeX = 24; - //private int plotSizeY = 256; - private int plotSizeZ = 24; - private int gridSizeX = 28; - private int gridSizeY = 256; - private int gridSizeZ = 28; - private int roadWidth = 4; - private int roadLevel = 62; - private int spaceAboveRoad = 2; - private Material roadMaterial = Material.COBBLESTONE; - private boolean generateRoadSupports = true; - private Material roadSupportMaterial = Material.STONE; - private String worldName = "world"; - private boolean generateFloor = false; - private Material floorMaterial = Material.GRASS_BLOCK; - private int spaceAboveFloor = 2; - private boolean generateSign = false; - private boolean generateSpawn = true; - private boolean setWorldSpawn = true; - private Material spawnFloorMaterial = Material.COBBLESTONE; - private boolean generateFloorSupports = false; - private Material floorSupportMaterial = Material.STONE; - private boolean generateWall = false; - private Material wallMaterial = Material.GLASS; - private int wallHeight = 128; - int _maxPlots = 1; - int _plotMultiplier = 1; - - private Cuboid _spawnCuboid = null; - private Cuboid _cityCuboid = null; - private ProtectedRegion _spawnRegion = null; - private ProtectedRegion _cityRegion = null; - - - @Override - public void onDisable() { - getLogger().info(String.format("%s disabled", pdf.getFullName())); - } - - @Override - public void onEnable() { - pdf = getDescription(); - - _ownedPlots = new HashMap>(); - _userOverrides = new HashMap(); - _currentHomes = new HashMap(); - loadCurrentHomes(); - - if(DEBUG){getLogger().info("Checking config");} - Configuration config = getConfig(); - if(!config.contains("version")){ - //new - if(DEBUG){getLogger().info("No config exists. Assuming new installation.");} - }else{ - int configVersion = safeGetIntFromConfig(config, "version"); - if(configVersion < version){ - if(DEBUG){getLogger().info(String.format("Updating config from version v%s to v%s.", configVersion, version));} - if(configVersion != version){ - //upgrade config - config.set("version", version); - } - saveConfig(); - if(DEBUG){getLogger().info("Config updated");} - } - } - - config.set("version", version); - saveConfig(); - - config.options().copyDefaults(true); - - if(DEBUG){getLogger().info("Reading configuration from file.");} - plotSizeX = safeGetIntFromConfig(config, "plot.sizeX"); - plotSizeZ = safeGetIntFromConfig(config, "plot.sizeZ"); - generateFloor = safeGetBooleanFromConfig(config, "plot.floor.generate"); - floorMaterial = safeGetMaterialFromConfig(config, "plot.floor.material"); - spaceAboveFloor = safeGetIntFromConfig(config, "plot.floor.clearSpaceAbove"); - generateFloorSupports = safeGetBooleanFromConfig(config, "plot.floor.supports.generate"); - floorSupportMaterial = safeGetMaterialFromConfig(config, "plot.floor.supports.material"); - generateSign = safeGetBooleanFromConfig(config, "plot.sign.generate"); - roadWidth = safeGetIntFromConfig(config, "road.width"); - spaceAboveRoad = safeGetIntFromConfig(config, "road.clearSpaceAbove"); - roadLevel = safeGetIntFromConfig(config, "road.level"); - roadMaterial = safeGetMaterialFromConfig(config, "road.material"); - generateRoadSupports = safeGetBooleanFromConfig(config, "road.supports.generate"); - roadSupportMaterial = safeGetMaterialFromConfig(config, "road.supports.material"); - generateSpawn = safeGetBooleanFromConfig(config, "spawn.generate"); - setWorldSpawn = safeGetBooleanFromConfig(config, "spawn.setAsWorldSpawn"); - spawnFloorMaterial = safeGetMaterialFromConfig(config, "spawn.material"); - generateWall = safeGetBooleanFromConfig(config, "wall.generate"); - wallMaterial = safeGetMaterialFromConfig(config, "wall.material"); - wallHeight = safeGetIntFromConfig(config, "wall.height"); - worldName = safeGetStringFromConfig(config, "worldname"); - _maxPlots = safeGetIntFromConfig(config, "plot.multiplier"); - _plotMultiplier = safeGetIntFromConfig(config, "plot.maxPerPlayer"); - - buildUserOverrides(); - - saveConfig(); - if(DEBUG){getLogger().info("Done reading config.");} - - getLogger().info(String.format("Metropolis: world name is %s", worldName)); - - Server server = getServer(); - if(server == null){ - throw new RuntimeException("getServer() is null"); - } - PluginManager pluginManager = server.getPluginManager(); - if(pluginManager == null){ - throw new RuntimeException("server.getPluginManager() is null"); - } - - Plugin plugin = pluginManager.getPlugin("WorldGuard"); - if(plugin == null || !(plugin instanceof WorldGuardPlugin)){ - throw new RuntimeException("WorldGuard must be loaded first"); - } - - worldGuard = (WorldGuardPlugin) plugin; - - plugin = pluginManager.getPlugin("WorldEdit"); - if(plugin == null || !(plugin instanceof WorldEditPlugin)){ - throw new RuntimeException("WorldEdit must be loaded first"); - } - worldEdit = (WorldEditPlugin) plugin; - - world = server.getWorld(worldName); - if(world == null){ - throw new RuntimeException(String.format("The world %s does not exist", worldName)); - } - - gridSizeX = plotSizeX + roadWidth; - gridSizeY = world.getMaxHeight(); - gridSizeZ = plotSizeZ + roadWidth; - - regionManager = WorldGuard.getInstance().getPlatform().getRegionContainer().get(BukkitAdapter.adapt(world)); - if(regionManager == null){ - throw new RuntimeException("WorldGuard regions don't seem to be enabled."); - } - - _cityRegion = regionManager.getRegion("City"); - if(_cityRegion == null){ - _cityRegion = new ProtectedCuboidRegion("City", getPlotMin(0, 0, 1), this.getPlotMax(0, 0, 1)); - _cityRegion.setPriority(0); - _cityRegion.setFlag(Flags.PVP, StateFlag.State.DENY); - _cityRegion.setFlag(Flags.MOB_DAMAGE, StateFlag.State.DENY); - _cityRegion.setFlag(Flags.MOB_SPAWNING, StateFlag.State.DENY); - _cityRegion.setFlag(Flags.CREEPER_EXPLOSION, StateFlag.State.DENY); - _cityRegion.setFlag(Flags.ENDER_BUILD, StateFlag.State.DENY); - _cityRegion.setFlag(Flags.GHAST_FIREBALL, StateFlag.State.DENY); - _cityRegion.setFlag(Flags.TNT, StateFlag.State.DENY); - _cityRegion.setFlag(Flags.LAVA_FLOW, StateFlag.State.DENY); - _cityRegion.setFlag(Flags.SNOW_FALL, StateFlag.State.DENY); - regionManager.addRegion(_cityRegion); - } - - _cityCuboid = new Cuboid(_cityRegion.getMinimumPoint(), _cityRegion.getMaximumPoint()); - - _spawnRegion = regionManager.getRegion("Spawn"); - if(_spawnRegion == null){ - _spawnRegion = new ProtectedCuboidRegion("Spawn", getPlotMin(0, 0, 1), getPlotMax(0, 0, 1)); - _spawnRegion.setPriority(1); - _spawnRegion.setFlag(Flags.PVP, StateFlag.State.DENY); - _spawnRegion.setFlag(Flags.MOB_DAMAGE, StateFlag.State.DENY); - _spawnRegion.setFlag(Flags.MOB_SPAWNING, StateFlag.State.DENY); - _spawnRegion.setFlag(Flags.CREEPER_EXPLOSION, StateFlag.State.DENY); - _spawnRegion.setFlag(Flags.ENDER_BUILD, StateFlag.State.DENY); - _spawnRegion.setFlag(Flags.GHAST_FIREBALL, StateFlag.State.DENY); - _spawnRegion.setFlag(Flags.TNT, StateFlag.State.DENY); - _spawnRegion.setFlag(Flags.LAVA_FLOW, StateFlag.State.DENY); - _spawnRegion.setFlag(Flags.SNOW_FALL, StateFlag.State.DENY); - regionManager.addRegion(_spawnRegion); - - _spawnCuboid = new Cuboid(_spawnRegion.getMinimumPoint(), _spawnRegion.getMaximumPoint()); - - setupSpawn(); - }else{ - _spawnCuboid = new Cuboid(_spawnRegion.getMinimumPoint(), _spawnRegion.getMaximumPoint()); - } - - _spawnCuboid = new Cuboid(_spawnRegion.getMinimumPoint(), _spawnRegion.getMaximumPoint()); - - if(DEBUG){ - /* - getLogger().info("Metropolis: first 25 plots"); - - int n = 5; - - for (int ix=-n; ix<=n; ix++){ - for (int iz=-n; iz<=n; iz++){ - getLogger().info(getCuboid(iz, ix).toString()); - } - } - */ - - getLogger().info(String.format("roadWidth = %d", roadWidth)); - } - - _occupiedPlots = new ArrayList(); - fillOccupiedPlots(); - resizeCityRegion(); - - _playerJoinListener = new PlayerJoinListener(this); - getServer().getPluginManager().registerEvents(_playerJoinListener, this); - - getLogger().info(String.format("%s enabled", pdf.getFullName())); - - RegisterCommandHandler("metropolis", new MetropolisCommand(this)); - - RegisterCommandHandler("metropolis-debug-generatetesthomes", new MetropolisDebugGenerateTestHomesCommand(this)); - - RegisterCommandHandler("metropolis-flag-reset", new MetropolisFlagResetCommand(this)); - - RegisterCommandHandler("metropolis-home-evict", new MetropolisHomeEvictCommand(this)); - RegisterCommandHandler("metropolis-home-acquire", new MetropolisHomeAcquire(this)); - RegisterCommandHandler("metropolis-home-generate", new MetropolisHomeGenerateCommand(this)); - RegisterCommandHandler("metropolis-home-go", new MetropolisHomeGoCommand(this)); - RegisterCommandHandler("metropolis-home-list", new MetropolisHomeListCommand(this)); - RegisterCommandHandler("metropolis-home-move", new MetropolisHomeMoveCommand(this)); - - RegisterCommandHandler("metropolis-plot-go", new MetropolisPlotGoCommand(this)); - RegisterCommandHandler("metropolis-plot-reserve", new MetropolisPlotReserveCommand(this)); - } - - private void loadCurrentHomes() { - File homesFile = new File(getDataFolder(), "currentHomes.yml"); - try { - _currentHomes.putAll(CurrentHomesStore.load( - homesFile, - ownerName -> getServer().getOfflinePlayer(ownerName).getUniqueId())); - saveCurrentHomes(); - } catch (IOException | org.bukkit.configuration.InvalidConfigurationException e) { - getLogger().log(java.util.logging.Level.SEVERE, "Unable to load currentHomes.yml", e); - } - } - - private void buildUserOverrides() { - if(getConfig().isList("userOverrides")){ - List list = getConfig().getList("userOverrides"); - - for(Object o2 : list){ - if(o2 instanceof HashMap){ - HashMapmap = (HashMap)o2; - String username = ""; - if(map.containsKey("username")){ - Object o3 = map.get("username"); - if(o3 instanceof String){ - username = (String)o3; - } - } - - int plotMultiplier = _plotMultiplier; - if(map.containsKey("plotMultiplier")){ - Object o3 = map.get("plotMultiplier"); - if(o3 instanceof Integer){ - plotMultiplier = (Integer)o3; - } - } - - int maxPlots = _maxPlots; - if(map.containsKey("maxPlots")){ - Object o3 = map.get("maxPlots"); - if(o3 instanceof Integer){ - maxPlots = (Integer)o3; - } - } - - UUID playerId = getServer().getOfflinePlayer(username).getUniqueId(); - UserOverride override = new UserOverride(username, plotMultiplier, maxPlots); - _userOverrides.put(playerId, override); - } - } - } - } - - private Cuboid getCuboid(int row, int col) { - //This is only used for debug info - BlockVector3 min = getPlotMin(row, col, 1); - BlockVector3 max = getPlotMax(row, col, 1); - return new Cuboid(min, max); - } - - private void RegisterCommandHandler(String commandName, CommandExecutor executor){ - PluginCommand command = getCommand(commandName); - if(command == null){ - throw new RuntimeException(String.format("The command %s does not appear to exist", commandName)); - }else{ - command.setExecutor(executor); - } - } - - private String safeGetStringFromConfig(Configuration config, String name) { - if(config.isString(name)){ - return config.getString(name); - }else{ - throwInvalidConfigException(); - return null; - } - } - - private boolean safeGetBooleanFromConfig(Configuration config, String name) { - if(config.isBoolean(name)){ - return config.getBoolean(name); - }else{ - throwInvalidConfigException(); - return false; - } - } - - private int safeGetIntFromConfig(Configuration config, String name) { - if(config.isInt(name)){ - return config.getInt(name); - }else{ - throwInvalidConfigException(); - return 0; - } - } - - private Material safeGetMaterialFromConfig(Configuration config, String name){ - Material material = null; - if(config.isInt(name)){ - material = switch (config.getInt(name)) { - case 1 -> Material.STONE; - case 2 -> Material.GRASS_BLOCK; - case 4 -> Material.COBBLESTONE; - case 7 -> Material.BEDROCK; - default -> null; - }; - }else if(config.isString(name)){ - material = Material.matchMaterial(config.getString(name)); - } - if (material == null) { - getLogger().severe("Invalid material configured at " + name); - throwInvalidConfigException(); - } - return material; - } - - private void throwInvalidConfigException() { - getLogger().info("Metropolis: ERROR config file is invalid. Please correct Metropolis/config.yml and restart the server."); - throw new RuntimeException("Config file is invalid."); - } - - private void setupSpawn() { - getLogger().info("Metropolis: Spawn Cuboid is " + _spawnCuboid.toString()); - - if(generateSpawn){ - int x= 0; - int y=roadLevel; - int z=0; - - //floor - for(x=_spawnCuboid.getMinX(); x<= _spawnCuboid.getMaxX(); x++){ - for(z=_spawnCuboid.getMinZ(); z<=_spawnCuboid.getMaxZ(); z++){ - for(y=roadLevel+1; y plots = _ownedPlots.get(playerId); - plots.add(plot); - }else{ - List plots = new ArrayList(); - plots.add(plot); - _ownedPlots.put(playerId, plots); - } - } - - @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - return super.onCommand(sender, command, label, args); - } - - public PlayerHome getPlayerHome(Player player) { - PlayerHome home = null; - - int homeNumber = _currentHomes.getOrDefault(player.getUniqueId(), 1); - String regionName = String.format("h_%d_%s", homeNumber, player.getUniqueId()); - ProtectedRegion homeRegion = regionManager.getRegion(regionName); - - if(homeRegion == null){ - PlayerHome existingHome = getOwnedHome(player.getUniqueId(), homeNumber); - if (existingHome != null) { - existingHome.setPlayerName(player.getName()); - return existingHome; - } - if(DEBUG){ - getLogger().info(String.format("Creating home for player %s", player.getName())); - } - home = generateHome(player); - }else{ - home = new PlayerHome(homeRegion); - home.setPlayerName(player.getName()); - } - - return home; - } - - private void generateFloor(Cuboid plotCuboid){ - int x=0; - int y=roadLevel; - int z=0; - - for(x = plotCuboid.minX; x <= plotCuboid.maxX; x++){ - for(z=plotCuboid.minZ; z<=plotCuboid.maxZ; z++){ - setFloor(x, y, z); - - clearSpaceAbove(x, y, z); - } - } - } - - private void clearSpaceAbove(int x, int y, int z) { - Block block = null; - - for(int i=0; i0){ - int x=0; - int y= roadLevel; - int z=0; - - if(plotCuboid == null){ - if(DEBUG){ - getLogger().warning("plotCuboid is null"); - } - return; - } - - int roadWidth1 = roadWidth / 2; - int roadWidth2 = roadWidth - roadWidth1; - - //North West Corner - if((roadMask & (ROAD_NORTH | ROAD_WEST)) != 0){ - for(x=plotCuboid.minX - roadWidth1; x= min; col--){ - if(!areBlocksOccupied(row, col, plotMultiplier)){ - if(DEBUG){getLogger().info(String.format("row: %d, col: %d", row, col));} - return new Cuboid(getPlotMin(row, col, plotMultiplier), getPlotMax(row, col, plotMultiplier)); - } - } - - //Left - col = min; - for(row = max; row > min; row--){ - if(!areBlocksOccupied(row, col, plotMultiplier)){ - if(row != 0 || col != 0){ - if(DEBUG){getLogger().info(String.format("row: %d, col: %d", row, col));} - return new Cuboid(getPlotMin(row, col, plotMultiplier), getPlotMax(row, col, plotMultiplier)); - } - } - } - - ring++; - min = -ring; - max = ring - (plotMultiplier-1); - } - - if(DEBUG){getLogger().info(String.format("row: %d, col: %d", row, col));} - return new Cuboid(getPlotMin(row, col, plotMultiplier), getPlotMax(row, col, plotMultiplier)); - } - - private void resizeCityRegion() { - size=calculateCitySize(); - ProtectedRegion cityRegion = regionManager.getRegion("City"); - if(cityRegion instanceof ProtectedCuboidRegion){ - ProtectedCuboidRegion region = (ProtectedCuboidRegion)cityRegion; - - BlockVector3 min; - BlockVector3 max; - - min = getPlotMin(-size/2, -size/2, 1); - max = getPlotMax(size/2, size/2, 1); - - ProtectedCuboidRegion resizedRegion = new ProtectedCuboidRegion(region.getId(), min, max); - resizedRegion.copyFrom(region); - regionManager.removeRegion(region.getId()); - regionManager.addRegion(resizedRegion); - _cityRegion = resizedRegion; - _cityCuboid = new Cuboid(min, max); - saveRegions(); - } - } - - private int calculateCitySize() { - int iSize = 3; - - for(Plot home: _occupiedPlots){ - int plotCol=Math.abs(getPlotXFromMin(home.getCuboid())); - int plotRow=Math.abs(getPlotZFromMin(home.getCuboid())); - if(DEBUG){getLogger().info(String.format("col: %d, row: %d, iSize: %d", plotCol, plotRow, iSize));} - iSize = Math.max(Math.max(plotRow*2+1, plotCol*2+1), iSize); - } - - if(DEBUG){getLogger().info(String.format("City size is %d", iSize));} - return iSize; - } - - public BlockVector3 getPlotMin(int row, int col, int plotMultiplier){ - BlockVector3 gridMin = getGridMin(row, col, plotMultiplier); - - BlockVector3 bv = BlockVector3.at(gridMin.x() + roadWidth/2, gridMin.y(), gridMin.z() + roadWidth/2); - getLogger().info(String.format("getPlotMin (%d, %d, %d)", bv.x(), bv.y(), bv.z())); - return bv; - } - - public BlockVector3 getPlotMax(int row, int col, int plotMultiplier){ - BlockVector3 gridMax = getGridMax(row, col, plotMultiplier); - - BlockVector3 bv = BlockVector3.at(gridMax.x() - (roadWidth - roadWidth/2), gridMax.y(), gridMax.z() - (roadWidth-roadWidth/2)); - getLogger().info(String.format("getPlotMax (%d, %d, %d)", bv.x(), bv.y(), bv.z())); - return bv; - } - - public BlockVector3 getGridMin(int row, int col, int plotMultiplier){ - int level = 0; - - BlockVector3 bv = BlockVector3.at(col * gridSizeX, level * gridSizeY, row * gridSizeZ); - getLogger().info(String.format("getGridMin (%d, %d, %d)", bv.x(), bv.y(), bv.z())); - return bv; - } - - public BlockVector3 getGridMax(int row, int col, int plotMultiplier){ - int level = 0; - - BlockVector3 bv = BlockVector3.at((col+plotMultiplier) * gridSizeX*plotMultiplier-1, (level+1/*plotMultiplier*/) * gridSizeY-1, (row+plotMultiplier) * gridSizeZ-1); - getLogger().info(String.format("getGridMax (%d, %d, %d)", bv.x(), bv.y(), bv.z())); - return bv; - } - - private int getPlotXFromMin(Cuboid cuboid) { - return (cuboid.minX - roadWidth/2)/gridSizeX; - } - - private int getPlotZFromMin(Cuboid cuboid) { - return (cuboid.minZ - roadWidth/2)/gridSizeZ; - } - - private void setHomeOccupied(UUID ownerId, String ownerName, int homeNumber, BlockVector3 minimumPoint, BlockVector3 maximumPoint) { - PlayerHome home = new PlayerHome(ownerId, ownerName, homeNumber, minimumPoint, maximumPoint); - if(!_occupiedPlots.contains(home)){ - _occupiedPlots.add(home); - addOwnedPlot(ownerId, home); - } - } - - public PlayerHome generateHome(String playerName) { - return generateHome(getServer().getOfflinePlayer(playerName)); - } - - public PlayerHome generateHome(OfflinePlayer player) { - UUID playerId = player.getUniqueId(); - String playerName = player.getName() == null ? playerId.toString() : player.getName(); - int homeNumber = _currentHomes.getOrDefault(playerId, 1); - int multiplier = getPlotMultiplier(playerId); - - if(DEBUG){getLogger().info(String.format("Generating home for %s", playerName));} - Cuboid homeCuboid = null; - ProtectedRegion phomeRegion = null; - String regionName = getHomeRegionName(playerId, homeNumber); - phomeRegion = regionManager.getRegion(regionName); - if(phomeRegion != null){ - return PlayerHome.get(phomeRegion); - } - PlayerHome existingHome = getOwnedHome(playerId, homeNumber); - if (existingHome != null) { - return existingHome; - } - - homeCuboid = findNextUnownedHomeRegion(multiplier); - - getLogger().info("Metropolis Generating home in " + homeCuboid.toString()); - - ProtectedCuboidRegion newHomeRegion = new ProtectedCuboidRegion(regionName, homeCuboid.getMin(), homeCuboid.getMax()); - newHomeRegion.setFlag(Flags.PVP, StateFlag.State.DENY); - newHomeRegion.setFlag(Flags.MOB_DAMAGE, StateFlag.State.DENY); - newHomeRegion.setFlag(Flags.MOB_SPAWNING, StateFlag.State.DENY); - newHomeRegion.setFlag(Flags.CREEPER_EXPLOSION, StateFlag.State.DENY); - newHomeRegion.setFlag(Flags.ENDER_BUILD, StateFlag.State.DENY); - newHomeRegion.setFlag(Flags.GHAST_FIREBALL, StateFlag.State.DENY); - newHomeRegion.setFlag(Flags.TNT, StateFlag.State.DENY); - - DefaultDomain d = newHomeRegion.getOwners(); - d.addPlayer(playerId); - newHomeRegion.setPriority(1); - - regionManager.addRegion(newHomeRegion); - try { - regionManager.save(); - } catch (Exception e) { - getLogger().info("Metropolis: ERROR Problem saving region"); - e.printStackTrace(); - } - - try { - regionManager.save(); - } catch (Exception e) { - getLogger().info("Metropolis: ERROR Problem saving region"); - e.printStackTrace(); - } - getLogger().info(String.format( - "New home region (%d, %d, %d) (%d, %d, %d)", - newHomeRegion.getMinimumPoint().x(), - newHomeRegion.getMinimumPoint().y(), - newHomeRegion.getMinimumPoint().z(), - newHomeRegion.getMaximumPoint().x(), - newHomeRegion.getMaximumPoint().y(), - newHomeRegion.getMaximumPoint().z() - )); - - setHomeOccupied(playerId, playerName, homeNumber, newHomeRegion.getMinimumPoint(), newHomeRegion.getMaximumPoint()); - _currentHomes.putIfAbsent(playerId, homeNumber); - saveCurrentHomes(); - - createRoads(homeCuboid); - - if(generateFloor){ - generateFloor(homeCuboid); - } - - if(DEBUG){getLogger().info(String.format("generateSign: %s", String.valueOf(generateSign)));} - if(generateSign){ - generateSign(homeCuboid, playerName); - } - - if(DEBUG){getLogger().info(String.format("Done generating home for %s", playerName));} - - PlayerHome home = new PlayerHome(newHomeRegion); - home.setPlayerName(playerName); - return home; - } - - private void generateSign(Cuboid plotCuboid, String playerName) { - Block signBlock = world.getBlockAt(plotCuboid.getCenterX(), roadLevel+1, plotCuboid.getCenterZ()); - signBlock.setType(Material.OAK_SIGN); - Sign sign = (Sign)signBlock.getState(); - sign.setLine(0, "Home of"); - - sign.setLine(1, playerName.substring(0, Math.min(15, playerName.length()))); - if(playerName.length() > 15){ - sign.setLine(2, playerName.substring(16, Math.min(30, playerName.length()))); - if(playerName.length() > 45){ - sign.setLine(3, playerName.substring(31, Math.min(45, playerName.length()))); - } - } - - sign.update(true); - } - - public List getCityBlocks() { - return Collections.unmodifiableList(_occupiedPlots); - } - - public World getWorld(){ - return world; - } - - public void reserveCuboid(String regionName, Cuboid cuboid) { - ProtectedCuboidRegion reservedRegion = new ProtectedCuboidRegion(regionName, cuboid.getMin(), cuboid.getMax()); - reservedRegion.setFlag(Flags.PVP, StateFlag.State.DENY); - reservedRegion.setFlag(Flags.MOB_DAMAGE, StateFlag.State.DENY); - reservedRegion.setFlag(Flags.MOB_SPAWNING, StateFlag.State.DENY); - reservedRegion.setFlag(Flags.CREEPER_EXPLOSION, StateFlag.State.DENY); - reservedRegion.setFlag(Flags.ENDER_BUILD, StateFlag.State.DENY); - reservedRegion.setFlag(Flags.GHAST_FIREBALL, StateFlag.State.DENY); - reservedRegion.setFlag(Flags.TNT, StateFlag.State.DENY); - reservedRegion.setFlag(Flags.LAVA_FLOW, StateFlag.State.DENY); - reservedRegion.setFlag(Flags.SNOW_FALL, StateFlag.State.DENY); - regionManager.addRegion(reservedRegion); - - _occupiedPlots.add(Plot.get(reservedRegion)); - saveRegions(); - } - - public Cuboid getCityCuboid(){ - return _cityCuboid; - } - - public boolean getGenerateWall(){ - return generateWall; - } - - public Material getWallMaterial(){ - return wallMaterial; - } - public int getWallheight(){ - return wallHeight; - } - - public ProtectedRegion getRegion(String regionName) { - if(regionManager == null){ - return null; - } - - return regionManager.getRegion(regionName); - } - - public void removeRegion(String regionId) { - if(regionManager == null){ - return; - } - - try{ - regionManager.removeRegion(regionId); - }catch(Exception ex){ - getLogger().info(String.format("[ERROR] Metropolis: Unable to remove region {%s}.", regionId)); - return; - } - } - - public void saveRegions() { - try { - regionManager.save(); - } catch (Exception ex) { - getLogger().info(String.format("[SEVERE] Metropolis: Unable to save WorldGuard regions.")); - return; - } - } - - public int getNumPlots(UUID playerId) { - if(_ownedPlots.containsKey(playerId)){ - List plots = _ownedPlots.get(playerId); - if(plots == null){ - return 0; - }else{ - return plots.size(); - } - }else{ - return 0; - } - } - - public int getMaxPlots(UUID playerId) { - if(_userOverrides.containsKey(playerId)){ - return _userOverrides.get(playerId).getMaxPlots(); - }else{ - return _maxPlots; - } - } - - public void assignPlot(OfflinePlayer player) { - generateHome(player); - } - - private int getPlotMultiplier(UUID playerId) { - if(_userOverrides.containsKey(playerId)){ - return _userOverrides.get(playerId).getPlotMultiplier(); - }else{ - return _plotMultiplier; - } - } - - public Plot getPlot(String string) { - /** - * string is the name of the region to get a plot for - * - * loop through all regions and find one with the specified name return null if there is none - */ - for(Plot plot : _occupiedPlots){ - if(plot.getRegionName().equals(string)){ - return plot; - } - } - - return null; - } - - public Player getPlayer(String name) { - Player player = getServer().getPlayerExact(name); - if (player != null) { - return player; - } - return PlayerLookup.findOnline(name, getServer().getOnlinePlayers()); - } - - public OfflinePlayer getOfflinePlayer(String name){ - Player onlinePlayer = getPlayer(name); - if (onlinePlayer != null) { - return onlinePlayer; - } - for (OfflinePlayer offlinePlayer : getServer().getOfflinePlayers()) { - if (name.equalsIgnoreCase(offlinePlayer.getName())) { - return offlinePlayer; - } - } - return null; - } - - public String teleportPlayerToPlot(Player player, Plot plot) { - Location loc = plot.getViableSpawnLocation(world); - - if(loc != null){ - player.teleport(loc); - } - - return null; - } - - public boolean homeExists(UUID playerId, int homeNumber) { - for(Plot plot: _occupiedPlots){ - if(plot instanceof PlayerHome home && home.getPlayerId().equals(playerId) && home.getNumber() == homeNumber){ - return true; - } - } - - return false; - } - - public void setHome(UUID playerId, int newHomeNumber) { - _currentHomes.put(playerId, newHomeNumber); - saveCurrentHomes(); - } - - public String getHomeRegionName(UUID playerId, int homeNumber) { - PlayerHome existingHome = getOwnedHome(playerId, homeNumber); - return existingHome == null - ? String.format("h_%d_%s", homeNumber, playerId) - : existingHome.getRegionName(); - } - - public String getCurrentHomeRegionName(UUID playerId) { - return getHomeRegionName(playerId, _currentHomes.getOrDefault(playerId, 1)); - } - - private PlayerHome getOwnedHome(UUID playerId, int homeNumber) { - List plots = _ownedPlots.get(playerId); - if (plots == null) { - return null; - } - for (Plot plot : plots) { - if (plot instanceof PlayerHome home && home.getNumber() == homeNumber) { - return home; - } - } - return null; - } - - private void saveCurrentHomes() { - File homesFile = new File(getDataFolder(), "currentHomes.yml"); - try { - CurrentHomesStore.save(homesFile, _currentHomes); - } catch (IOException e) { - getLogger().log(java.util.logging.Level.SEVERE, "Unable to save currentHomes.yml", e); - } - } -} +/* +This file is part of Metropolis. + +Metropolis is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +Metropolis is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with Metropolis. If not, see . +*/ + +package com.majinnaibu.bukkitplugins.metropolis; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.UUID; + +import com.majinnaibu.bukkitplugins.metropolis.commands.MetropolisCommand; +import com.majinnaibu.bukkitplugins.metropolis.commands.MetropolisDebugGenerateTestHomesCommand; +import com.majinnaibu.bukkitplugins.metropolis.commands.MetropolisFlagResetCommand; +import com.majinnaibu.bukkitplugins.metropolis.commands.MetropolisHomeAcquire; +import com.majinnaibu.bukkitplugins.metropolis.commands.MetropolisHomeEvictCommand; +import com.majinnaibu.bukkitplugins.metropolis.commands.MetropolisHomeGenerateCommand; +import com.majinnaibu.bukkitplugins.metropolis.commands.MetropolisHomeGoCommand; +import com.majinnaibu.bukkitplugins.metropolis.commands.MetropolisHomeListCommand; +import com.majinnaibu.bukkitplugins.metropolis.commands.MetropolisHomeMoveCommand; +import com.majinnaibu.bukkitplugins.metropolis.commands.MetropolisPlotGoCommand; +import com.majinnaibu.bukkitplugins.metropolis.commands.MetropolisPlotReserveCommand; +import com.majinnaibu.bukkitplugins.metropolis.eventlisteners.PlayerJoinListener; +import com.sk89q.worldedit.bukkit.BukkitAdapter; +import com.sk89q.worldedit.bukkit.WorldEditPlugin; +import com.sk89q.worldedit.math.BlockVector3; +import com.sk89q.worldguard.WorldGuard; +import com.sk89q.worldguard.bukkit.WorldGuardPlugin; +import com.sk89q.worldguard.domains.DefaultDomain; +import com.sk89q.worldguard.protection.flags.Flags; +import com.sk89q.worldguard.protection.flags.StateFlag; +import com.sk89q.worldguard.protection.managers.RegionManager; +import com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion; +import com.sk89q.worldguard.protection.regions.ProtectedRegion; + +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.OfflinePlayer; +import org.bukkit.Server; +import org.bukkit.World; +import org.bukkit.block.Block; +import org.bukkit.block.Sign; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.command.PluginCommand; +import org.bukkit.configuration.Configuration; +import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; +import org.bukkit.plugin.PluginDescriptionFile; +import org.bukkit.plugin.PluginManager; +import org.bukkit.plugin.java.JavaPlugin; + +public class MetropolisPlugin extends JavaPlugin { + public static final boolean DEBUG = false; + private static final int version = 1; + + public static final int ROAD_NORTH = 1; + public static final int ROAD_SOUTH = 2; + public static final int ROAD_EAST = 4; + public static final int ROAD_WEST = 8; + + public PluginDescriptionFile pdf = null; + public WorldGuardPlugin worldGuard = null; + public WorldEditPlugin worldEdit = null; + public World world = null; + public RegionManager regionManager = null; + + private List _occupiedPlots; + private HashMap> _ownedPlots; + private HashMap _userOverrides; + private HashMap _currentHomes; + + private PlayerJoinListener _playerJoinListener = null; + + int size = 1; + + private int plotSizeX = 24; + // private int plotSizeY = 256; + private int plotSizeZ = 24; + private int gridSizeX = 28; + private int gridSizeY = 256; + private int gridSizeZ = 28; + private int roadWidth = 4; + private int roadLevel = 62; + private int spaceAboveRoad = 2; + private Material roadMaterial = Material.COBBLESTONE; + private boolean generateRoadSupports = true; + private Material roadSupportMaterial = Material.STONE; + private String worldName = "world"; + private boolean generateFloor = false; + private Material floorMaterial = Material.GRASS_BLOCK; + private int spaceAboveFloor = 2; + private boolean generateSign = false; + private boolean generateSpawn = true; + private boolean setWorldSpawn = true; + private Material spawnFloorMaterial = Material.COBBLESTONE; + private boolean generateFloorSupports = false; + private Material floorSupportMaterial = Material.STONE; + private boolean generateWall = false; + private Material wallMaterial = Material.GLASS; + private int wallHeight = 128; + int _maxPlots = 1; + int _plotMultiplier = 1; + + private Cuboid _spawnCuboid = null; + private Cuboid _cityCuboid = null; + private ProtectedRegion _spawnRegion = null; + private ProtectedRegion _cityRegion = null; + + @Override + public void onDisable() { + getLogger().info(String.format("%s disabled", pdf.getFullName())); + } + + @Override + public void onEnable() { + pdf = getDescription(); + + _ownedPlots = new HashMap>(); + _userOverrides = new HashMap(); + _currentHomes = new HashMap(); + loadCurrentHomes(); + + if (DEBUG) { + getLogger().info("Checking config"); + } + Configuration config = getConfig(); + if (!config.contains("version")) { + // new + if (DEBUG) { + getLogger().info("No config exists. Assuming new installation."); + } + } else { + int configVersion = safeGetIntFromConfig(config, "version"); + if (configVersion < version) { + if (DEBUG) { + getLogger() + .info( + String.format( + "Updating config from version v%s to v%s.", configVersion, version)); + } + if (configVersion != version) { + // upgrade config + config.set("version", version); + } + saveConfig(); + if (DEBUG) { + getLogger().info("Config updated"); + } + } + } + + config.set("version", version); + saveConfig(); + + config.options().copyDefaults(true); + + if (DEBUG) { + getLogger().info("Reading configuration from file."); + } + plotSizeX = safeGetIntFromConfig(config, "plot.sizeX"); + plotSizeZ = safeGetIntFromConfig(config, "plot.sizeZ"); + generateFloor = safeGetBooleanFromConfig(config, "plot.floor.generate"); + floorMaterial = safeGetMaterialFromConfig(config, "plot.floor.material"); + spaceAboveFloor = safeGetIntFromConfig(config, "plot.floor.clearSpaceAbove"); + generateFloorSupports = safeGetBooleanFromConfig(config, "plot.floor.supports.generate"); + floorSupportMaterial = safeGetMaterialFromConfig(config, "plot.floor.supports.material"); + generateSign = safeGetBooleanFromConfig(config, "plot.sign.generate"); + roadWidth = safeGetIntFromConfig(config, "road.width"); + spaceAboveRoad = safeGetIntFromConfig(config, "road.clearSpaceAbove"); + roadLevel = safeGetIntFromConfig(config, "road.level"); + roadMaterial = safeGetMaterialFromConfig(config, "road.material"); + generateRoadSupports = safeGetBooleanFromConfig(config, "road.supports.generate"); + roadSupportMaterial = safeGetMaterialFromConfig(config, "road.supports.material"); + generateSpawn = safeGetBooleanFromConfig(config, "spawn.generate"); + setWorldSpawn = safeGetBooleanFromConfig(config, "spawn.setAsWorldSpawn"); + spawnFloorMaterial = safeGetMaterialFromConfig(config, "spawn.material"); + generateWall = safeGetBooleanFromConfig(config, "wall.generate"); + wallMaterial = safeGetMaterialFromConfig(config, "wall.material"); + wallHeight = safeGetIntFromConfig(config, "wall.height"); + worldName = safeGetStringFromConfig(config, "worldname"); + _maxPlots = safeGetIntFromConfig(config, "plot.multiplier"); + _plotMultiplier = safeGetIntFromConfig(config, "plot.maxPerPlayer"); + + buildUserOverrides(); + + saveConfig(); + if (DEBUG) { + getLogger().info("Done reading config."); + } + + getLogger().info(String.format("Metropolis: world name is %s", worldName)); + + Server server = getServer(); + if (server == null) { + throw new RuntimeException("getServer() is null"); + } + PluginManager pluginManager = server.getPluginManager(); + if (pluginManager == null) { + throw new RuntimeException("server.getPluginManager() is null"); + } + + Plugin plugin = pluginManager.getPlugin("WorldGuard"); + if (plugin == null || !(plugin instanceof WorldGuardPlugin)) { + throw new RuntimeException("WorldGuard must be loaded first"); + } + + worldGuard = (WorldGuardPlugin) plugin; + + plugin = pluginManager.getPlugin("WorldEdit"); + if (plugin == null || !(plugin instanceof WorldEditPlugin)) { + throw new RuntimeException("WorldEdit must be loaded first"); + } + worldEdit = (WorldEditPlugin) plugin; + + world = server.getWorld(worldName); + if (world == null) { + throw new RuntimeException(String.format("The world %s does not exist", worldName)); + } + + gridSizeX = plotSizeX + roadWidth; + gridSizeY = world.getMaxHeight(); + gridSizeZ = plotSizeZ + roadWidth; + + regionManager = + WorldGuard.getInstance().getPlatform().getRegionContainer().get(BukkitAdapter.adapt(world)); + if (regionManager == null) { + throw new RuntimeException("WorldGuard regions don't seem to be enabled."); + } + + _cityRegion = regionManager.getRegion("City"); + if (_cityRegion == null) { + _cityRegion = + new ProtectedCuboidRegion("City", getPlotMin(0, 0, 1), this.getPlotMax(0, 0, 1)); + _cityRegion.setPriority(0); + _cityRegion.setFlag(Flags.PVP, StateFlag.State.DENY); + _cityRegion.setFlag(Flags.MOB_DAMAGE, StateFlag.State.DENY); + _cityRegion.setFlag(Flags.MOB_SPAWNING, StateFlag.State.DENY); + _cityRegion.setFlag(Flags.CREEPER_EXPLOSION, StateFlag.State.DENY); + _cityRegion.setFlag(Flags.ENDER_BUILD, StateFlag.State.DENY); + _cityRegion.setFlag(Flags.GHAST_FIREBALL, StateFlag.State.DENY); + _cityRegion.setFlag(Flags.TNT, StateFlag.State.DENY); + _cityRegion.setFlag(Flags.LAVA_FLOW, StateFlag.State.DENY); + _cityRegion.setFlag(Flags.SNOW_FALL, StateFlag.State.DENY); + regionManager.addRegion(_cityRegion); + } + + _cityCuboid = new Cuboid(_cityRegion.getMinimumPoint(), _cityRegion.getMaximumPoint()); + + _spawnRegion = regionManager.getRegion("Spawn"); + if (_spawnRegion == null) { + _spawnRegion = new ProtectedCuboidRegion("Spawn", getPlotMin(0, 0, 1), getPlotMax(0, 0, 1)); + _spawnRegion.setPriority(1); + _spawnRegion.setFlag(Flags.PVP, StateFlag.State.DENY); + _spawnRegion.setFlag(Flags.MOB_DAMAGE, StateFlag.State.DENY); + _spawnRegion.setFlag(Flags.MOB_SPAWNING, StateFlag.State.DENY); + _spawnRegion.setFlag(Flags.CREEPER_EXPLOSION, StateFlag.State.DENY); + _spawnRegion.setFlag(Flags.ENDER_BUILD, StateFlag.State.DENY); + _spawnRegion.setFlag(Flags.GHAST_FIREBALL, StateFlag.State.DENY); + _spawnRegion.setFlag(Flags.TNT, StateFlag.State.DENY); + _spawnRegion.setFlag(Flags.LAVA_FLOW, StateFlag.State.DENY); + _spawnRegion.setFlag(Flags.SNOW_FALL, StateFlag.State.DENY); + regionManager.addRegion(_spawnRegion); + + _spawnCuboid = new Cuboid(_spawnRegion.getMinimumPoint(), _spawnRegion.getMaximumPoint()); + + setupSpawn(); + } else { + _spawnCuboid = new Cuboid(_spawnRegion.getMinimumPoint(), _spawnRegion.getMaximumPoint()); + } + + _spawnCuboid = new Cuboid(_spawnRegion.getMinimumPoint(), _spawnRegion.getMaximumPoint()); + + if (DEBUG) { + /* + getLogger().info("Metropolis: first 25 plots"); + + int n = 5; + + for (int ix=-n; ix<=n; ix++){ + for (int iz=-n; iz<=n; iz++){ + getLogger().info(getCuboid(iz, ix).toString()); + } + } + */ + + getLogger().info(String.format("roadWidth = %d", roadWidth)); + } + + _occupiedPlots = new ArrayList(); + fillOccupiedPlots(); + resizeCityRegion(); + + _playerJoinListener = new PlayerJoinListener(this); + getServer().getPluginManager().registerEvents(_playerJoinListener, this); + + getLogger().info(String.format("%s enabled", pdf.getFullName())); + + RegisterCommandHandler("metropolis", new MetropolisCommand(this)); + + RegisterCommandHandler( + "metropolis-debug-generatetesthomes", new MetropolisDebugGenerateTestHomesCommand(this)); + + RegisterCommandHandler("metropolis-flag-reset", new MetropolisFlagResetCommand(this)); + + RegisterCommandHandler("metropolis-home-evict", new MetropolisHomeEvictCommand(this)); + RegisterCommandHandler("metropolis-home-acquire", new MetropolisHomeAcquire(this)); + RegisterCommandHandler("metropolis-home-generate", new MetropolisHomeGenerateCommand(this)); + RegisterCommandHandler("metropolis-home-go", new MetropolisHomeGoCommand(this)); + RegisterCommandHandler("metropolis-home-list", new MetropolisHomeListCommand(this)); + RegisterCommandHandler("metropolis-home-move", new MetropolisHomeMoveCommand(this)); + + RegisterCommandHandler("metropolis-plot-go", new MetropolisPlotGoCommand(this)); + RegisterCommandHandler("metropolis-plot-reserve", new MetropolisPlotReserveCommand(this)); + } + + private void loadCurrentHomes() { + File homesFile = new File(getDataFolder(), "currentHomes.yml"); + try { + _currentHomes.putAll( + CurrentHomesStore.load( + homesFile, ownerName -> getServer().getOfflinePlayer(ownerName).getUniqueId())); + saveCurrentHomes(); + } catch (IOException | org.bukkit.configuration.InvalidConfigurationException e) { + getLogger().log(java.util.logging.Level.SEVERE, "Unable to load currentHomes.yml", e); + } + } + + private void buildUserOverrides() { + if (getConfig().isList("userOverrides")) { + List list = getConfig().getList("userOverrides"); + + for (Object o2 : list) { + if (o2 instanceof HashMap) { + HashMap map = (HashMap) o2; + String username = ""; + if (map.containsKey("username")) { + Object o3 = map.get("username"); + if (o3 instanceof String) { + username = (String) o3; + } + } + + int plotMultiplier = _plotMultiplier; + if (map.containsKey("plotMultiplier")) { + Object o3 = map.get("plotMultiplier"); + if (o3 instanceof Integer) { + plotMultiplier = (Integer) o3; + } + } + + int maxPlots = _maxPlots; + if (map.containsKey("maxPlots")) { + Object o3 = map.get("maxPlots"); + if (o3 instanceof Integer) { + maxPlots = (Integer) o3; + } + } + + UUID playerId = getServer().getOfflinePlayer(username).getUniqueId(); + UserOverride override = new UserOverride(username, plotMultiplier, maxPlots); + _userOverrides.put(playerId, override); + } + } + } + } + + private Cuboid getCuboid(int row, int col) { + // This is only used for debug info + BlockVector3 min = getPlotMin(row, col, 1); + BlockVector3 max = getPlotMax(row, col, 1); + return new Cuboid(min, max); + } + + private void RegisterCommandHandler(String commandName, CommandExecutor executor) { + PluginCommand command = getCommand(commandName); + if (command == null) { + throw new RuntimeException( + String.format("The command %s does not appear to exist", commandName)); + } else { + command.setExecutor(executor); + } + } + + private String safeGetStringFromConfig(Configuration config, String name) { + if (config.isString(name)) { + return config.getString(name); + } else { + throwInvalidConfigException(); + return null; + } + } + + private boolean safeGetBooleanFromConfig(Configuration config, String name) { + if (config.isBoolean(name)) { + return config.getBoolean(name); + } else { + throwInvalidConfigException(); + return false; + } + } + + private int safeGetIntFromConfig(Configuration config, String name) { + if (config.isInt(name)) { + return config.getInt(name); + } else { + throwInvalidConfigException(); + return 0; + } + } + + private Material safeGetMaterialFromConfig(Configuration config, String name) { + Material material = null; + if (config.isInt(name)) { + material = + switch (config.getInt(name)) { + case 1 -> Material.STONE; + case 2 -> Material.GRASS_BLOCK; + case 4 -> Material.COBBLESTONE; + case 7 -> Material.BEDROCK; + default -> null; + }; + } else if (config.isString(name)) { + material = Material.matchMaterial(config.getString(name)); + } + if (material == null) { + getLogger().severe("Invalid material configured at " + name); + throwInvalidConfigException(); + } + return material; + } + + private void throwInvalidConfigException() { + getLogger() + .info( + "Metropolis: ERROR config file is invalid. Please correct Metropolis/config.yml and restart the server."); + throw new RuntimeException("Config file is invalid."); + } + + private void setupSpawn() { + getLogger().info("Metropolis: Spawn Cuboid is " + _spawnCuboid.toString()); + + if (generateSpawn) { + int x = 0; + int y = roadLevel; + int z = 0; + + // floor + for (x = _spawnCuboid.getMinX(); x <= _spawnCuboid.getMaxX(); x++) { + for (z = _spawnCuboid.getMinZ(); z <= _spawnCuboid.getMaxZ(); z++) { + for (y = roadLevel + 1; y < world.getMaxHeight(); y++) { + Block block = world.getBlockAt(x, y, z); + block.setType(Material.AIR); + } + + y = roadLevel; + Block block = world.getBlockAt(x, y, z); + block.setType(spawnFloorMaterial); + } + } + + // roads + createRoads(_spawnCuboid); + } + + if (setWorldSpawn) { + world.setSpawnLocation(_spawnCuboid.getCenterX(), roadLevel + 1, _spawnCuboid.getCenterZ()); + } + } + + private void fillOccupiedPlots() { + _occupiedPlots.clear(); + _ownedPlots.clear(); + + for (ProtectedRegion region : regionManager.getRegions().values()) { + if (region instanceof ProtectedCuboidRegion) { + ProtectedCuboidRegion cuboidRegion = (ProtectedCuboidRegion) region; + if (cuboidRegion.getId().startsWith("h_")) { + PlayerHome home = PlayerHome.get(region); + if (!_currentHomes.containsKey(home.getPlayerId())) { + _currentHomes.put(home.getPlayerId(), home.getNumber()); + } + _occupiedPlots.add(home); + addOwnedPlot(home.getPlayerId(), home); + } else if (cuboidRegion.getId().startsWith("r_")) { + _occupiedPlots.add(Plot.get(cuboidRegion)); + } + } + } + + size = calculateCitySize(); + } + + private void addOwnedPlot(UUID playerId, Plot plot) { + if (_ownedPlots.containsKey(playerId)) { + List plots = _ownedPlots.get(playerId); + plots.add(plot); + } else { + List plots = new ArrayList(); + plots.add(plot); + _ownedPlots.put(playerId, plots); + } + } + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + return super.onCommand(sender, command, label, args); + } + + public PlayerHome getPlayerHome(Player player) { + PlayerHome home = null; + + int homeNumber = _currentHomes.getOrDefault(player.getUniqueId(), 1); + String regionName = String.format("h_%d_%s", homeNumber, player.getUniqueId()); + ProtectedRegion homeRegion = regionManager.getRegion(regionName); + + if (homeRegion == null) { + PlayerHome existingHome = getOwnedHome(player.getUniqueId(), homeNumber); + if (existingHome != null) { + existingHome.setPlayerName(player.getName()); + return existingHome; + } + if (DEBUG) { + getLogger().info(String.format("Creating home for player %s", player.getName())); + } + home = generateHome(player); + } else { + home = new PlayerHome(homeRegion); + home.setPlayerName(player.getName()); + } + + return home; + } + + private void generateFloor(Cuboid plotCuboid) { + int x = 0; + int y = roadLevel; + int z = 0; + + for (x = plotCuboid.minX; x <= plotCuboid.maxX; x++) { + for (z = plotCuboid.minZ; z <= plotCuboid.maxZ; z++) { + setFloor(x, y, z); + + clearSpaceAbove(x, y, z); + } + } + } + + private void clearSpaceAbove(int x, int y, int z) { + Block block = null; + + for (int i = 0; i < spaceAboveFloor; i++) { + block = world.getBlockAt(x, y + 1 + i, z); + block.setType(Material.AIR); + } + } + + private void setFloor(int x, int y, int z) { + // if(DEBUG){getLogger().info(String.format("setting road at (%d, %d, %d)", x, y, z));} + + Block block = world.getBlockAt(x, y, z); + + // Set the floor block + block.setType(floorMaterial); + + // Set the support + if (generateFloorSupports && isPhysicsMaterial(block.getType())) { + Block blockUnder = world.getBlockAt(x, y - 1, z); + if (!isSolidMaterial(blockUnder.getType())) { + blockUnder.setType(floorSupportMaterial); + } + } + } + + private void createRoads(Cuboid plotCuboid, int roadMask) { + if (roadWidth > 0) { + int x = 0; + int y = roadLevel; + int z = 0; + + if (plotCuboid == null) { + if (DEBUG) { + getLogger().warning("plotCuboid is null"); + } + return; + } + + int roadWidth1 = roadWidth / 2; + int roadWidth2 = roadWidth - roadWidth1; + + // North West Corner + if ((roadMask & (ROAD_NORTH | ROAD_WEST)) != 0) { + for (x = plotCuboid.minX - roadWidth1; x < plotCuboid.minX; x++) { + for (z = plotCuboid.minZ - roadWidth1; z < plotCuboid.minZ; z++) { + setRoad(x, y, z); + } + } + } + + // North Strip + if ((roadMask & ROAD_NORTH) != 0) { + for (x = plotCuboid.minX; x <= plotCuboid.maxX; x++) { + for (z = plotCuboid.minZ - roadWidth1; z < plotCuboid.minZ; z++) { + setRoad(x, y, z); + } + } + } + + // North East Corner + if ((roadMask & (ROAD_NORTH | ROAD_EAST)) != 0) { + for (x = plotCuboid.maxX + 1; x <= plotCuboid.maxX + roadWidth2; x++) { + for (z = plotCuboid.minZ - roadWidth1; z < plotCuboid.minZ; z++) { + setRoad(x, y, z); + } + } + } + + // East Strip + if ((roadMask & ROAD_EAST) != 0) { + for (x = plotCuboid.maxX + 1; x <= plotCuboid.maxX + roadWidth2; x++) { + for (z = plotCuboid.minZ; z <= plotCuboid.maxZ; z++) { + setRoad(x, y, z); + } + } + } + + // South East Corner + if ((roadMask & (ROAD_SOUTH | ROAD_EAST)) != 0) { + for (x = plotCuboid.maxX + 1; x <= plotCuboid.maxX + roadWidth2; x++) { + for (z = plotCuboid.maxZ + 1; z <= plotCuboid.maxZ + roadWidth2; z++) { + setRoad(x, y, z); + } + } + } + + // South Strip + if ((roadMask & ROAD_SOUTH) != 0) { + for (x = plotCuboid.minX; x <= plotCuboid.maxX; x++) { + for (z = plotCuboid.maxZ + 1; z <= plotCuboid.maxZ + roadWidth2; z++) { + setRoad(x, y, z); + } + } + } + + // South West Corner + if ((roadMask & (ROAD_SOUTH | ROAD_WEST)) != 0) { + for (x = plotCuboid.minX - roadWidth1; x < plotCuboid.minX; x++) { + for (z = plotCuboid.maxZ + 1; z <= plotCuboid.maxZ + roadWidth2; z++) { + setRoad(x, y, z); + } + } + } + + // West Strip + if ((roadMask & ROAD_WEST) != 0) { + for (x = plotCuboid.minX - roadWidth1; x < plotCuboid.minX; x++) { + for (z = plotCuboid.minZ; z <= plotCuboid.maxZ; z++) { + setRoad(x, y, z); + } + } + } + } + } + + private void createRoads(Cuboid plotCuboid) { + createRoads(plotCuboid, ROAD_NORTH | ROAD_SOUTH | ROAD_EAST | ROAD_WEST); + } + + private void setRoad(int x, int y, int z) { + // if(DEBUG){getLogger().info(String.format("setting road at (%d, %d, %d)", x, y, z));} + + Block block = world.getBlockAt(x, y, z); + // Set the road block + block.setType(roadMaterial); + + // Set the support + if (generateRoadSupports && isPhysicsMaterial(block.getType())) { + Block blockUnder = world.getBlockAt(x, y - 1, z); + if (!isSolidMaterial(blockUnder.getType())) { + blockUnder.setType(roadSupportMaterial); + } + } + + // Clear the space above + for (int y1 = 0; y1 < spaceAboveRoad; y1++) { + block = world.getBlockAt(x, y + y1 + 1, z); + block.setType(Material.AIR); + } + } + + private boolean isSolidMaterial(Material material) { + return material.isBlock() + && material != Material.AIR + && material != Material.WATER + && material != Material.LAVA + && material != Material.TORCH + && material != Material.REDSTONE_TORCH; + } + + private boolean isPhysicsMaterial(Material material) { + return material == Material.GRAVEL || material == Material.SAND; + } + + public boolean isBlockOccupied(int row, int col) { + Cuboid cuboid = new Cuboid(getGridMin(row, col, 1), getGridMax(row, col, 1)); + for (Plot plot : _occupiedPlots) { + if (plot.getCuboid().intersects(cuboid)) { + return true; + } + } + + if (cuboid.intersects(_spawnCuboid)) { + return true; + } + + return false; + } + + private boolean areBlocksOccupied(int row, int col, int i) { + for (int ix = col; ix < col + i; ix++) { + for (int iy = row; iy < row + i; iy++) { + if (isBlockOccupied(iy, ix)) { + return true; + } + } + } + + return false; + } + + private Cuboid findNextUnownedHomeRegion(int plotMultiplier) { + int row = 0; + int col = 0; + int ring = 0; + int min = -ring; + int max = ring - (plotMultiplier - 1); + boolean done = false; + + while (!done) { + row = min; + col = min; + + // Top + for (col = min; col <= max; col++) { + if (!areBlocksOccupied(row, col, plotMultiplier)) { + if (DEBUG) { + getLogger().info(String.format("row: %d, col: %d", row, col)); + } + return new Cuboid( + getPlotMin(row, col, plotMultiplier), getPlotMax(row, col, plotMultiplier)); + } + } + + // Right side + col = max; + for (row = min + 1; row < max; row++) { + if (!areBlocksOccupied(row, col, plotMultiplier)) { + if (DEBUG) { + getLogger().info(String.format("row: %d, col: %d", row, col)); + } + return new Cuboid( + getPlotMin(row, col, plotMultiplier), getPlotMax(row, col, plotMultiplier)); + } + } + + // Bottom + row = max; + for (col = max; col >= min; col--) { + if (!areBlocksOccupied(row, col, plotMultiplier)) { + if (DEBUG) { + getLogger().info(String.format("row: %d, col: %d", row, col)); + } + return new Cuboid( + getPlotMin(row, col, plotMultiplier), getPlotMax(row, col, plotMultiplier)); + } + } + + // Left + col = min; + for (row = max; row > min; row--) { + if (!areBlocksOccupied(row, col, plotMultiplier)) { + if (row != 0 || col != 0) { + if (DEBUG) { + getLogger().info(String.format("row: %d, col: %d", row, col)); + } + return new Cuboid( + getPlotMin(row, col, plotMultiplier), getPlotMax(row, col, plotMultiplier)); + } + } + } + + ring++; + min = -ring; + max = ring - (plotMultiplier - 1); + } + + if (DEBUG) { + getLogger().info(String.format("row: %d, col: %d", row, col)); + } + return new Cuboid(getPlotMin(row, col, plotMultiplier), getPlotMax(row, col, plotMultiplier)); + } + + private void resizeCityRegion() { + size = calculateCitySize(); + ProtectedRegion cityRegion = regionManager.getRegion("City"); + if (cityRegion instanceof ProtectedCuboidRegion) { + ProtectedCuboidRegion region = (ProtectedCuboidRegion) cityRegion; + + BlockVector3 min; + BlockVector3 max; + + min = getPlotMin(-size / 2, -size / 2, 1); + max = getPlotMax(size / 2, size / 2, 1); + + ProtectedCuboidRegion resizedRegion = new ProtectedCuboidRegion(region.getId(), min, max); + resizedRegion.copyFrom(region); + regionManager.removeRegion(region.getId()); + regionManager.addRegion(resizedRegion); + _cityRegion = resizedRegion; + _cityCuboid = new Cuboid(min, max); + saveRegions(); + } + } + + private int calculateCitySize() { + int iSize = 3; + + for (Plot home : _occupiedPlots) { + int plotCol = Math.abs(getPlotXFromMin(home.getCuboid())); + int plotRow = Math.abs(getPlotZFromMin(home.getCuboid())); + if (DEBUG) { + getLogger().info(String.format("col: %d, row: %d, iSize: %d", plotCol, plotRow, iSize)); + } + iSize = Math.max(Math.max(plotRow * 2 + 1, plotCol * 2 + 1), iSize); + } + + if (DEBUG) { + getLogger().info(String.format("City size is %d", iSize)); + } + return iSize; + } + + public BlockVector3 getPlotMin(int row, int col, int plotMultiplier) { + BlockVector3 gridMin = getGridMin(row, col, plotMultiplier); + + BlockVector3 bv = + BlockVector3.at(gridMin.x() + roadWidth / 2, gridMin.y(), gridMin.z() + roadWidth / 2); + getLogger().info(String.format("getPlotMin (%d, %d, %d)", bv.x(), bv.y(), bv.z())); + return bv; + } + + public BlockVector3 getPlotMax(int row, int col, int plotMultiplier) { + BlockVector3 gridMax = getGridMax(row, col, plotMultiplier); + + BlockVector3 bv = + BlockVector3.at( + gridMax.x() - (roadWidth - roadWidth / 2), + gridMax.y(), + gridMax.z() - (roadWidth - roadWidth / 2)); + getLogger().info(String.format("getPlotMax (%d, %d, %d)", bv.x(), bv.y(), bv.z())); + return bv; + } + + public BlockVector3 getGridMin(int row, int col, int plotMultiplier) { + int level = 0; + + BlockVector3 bv = BlockVector3.at(col * gridSizeX, level * gridSizeY, row * gridSizeZ); + getLogger().info(String.format("getGridMin (%d, %d, %d)", bv.x(), bv.y(), bv.z())); + return bv; + } + + public BlockVector3 getGridMax(int row, int col, int plotMultiplier) { + int level = 0; + + BlockVector3 bv = + BlockVector3.at( + (col + plotMultiplier) * gridSizeX * plotMultiplier - 1, + (level + 1 /*plotMultiplier*/) * gridSizeY - 1, + (row + plotMultiplier) * gridSizeZ - 1); + getLogger().info(String.format("getGridMax (%d, %d, %d)", bv.x(), bv.y(), bv.z())); + return bv; + } + + private int getPlotXFromMin(Cuboid cuboid) { + return (cuboid.minX - roadWidth / 2) / gridSizeX; + } + + private int getPlotZFromMin(Cuboid cuboid) { + return (cuboid.minZ - roadWidth / 2) / gridSizeZ; + } + + private void setHomeOccupied( + UUID ownerId, + String ownerName, + int homeNumber, + BlockVector3 minimumPoint, + BlockVector3 maximumPoint) { + PlayerHome home = new PlayerHome(ownerId, ownerName, homeNumber, minimumPoint, maximumPoint); + if (!_occupiedPlots.contains(home)) { + _occupiedPlots.add(home); + addOwnedPlot(ownerId, home); + } + } + + public PlayerHome generateHome(String playerName) { + return generateHome(getServer().getOfflinePlayer(playerName)); + } + + public PlayerHome generateHome(OfflinePlayer player) { + UUID playerId = player.getUniqueId(); + String playerName = player.getName() == null ? playerId.toString() : player.getName(); + int homeNumber = _currentHomes.getOrDefault(playerId, 1); + int multiplier = getPlotMultiplier(playerId); + + if (DEBUG) { + getLogger().info(String.format("Generating home for %s", playerName)); + } + Cuboid homeCuboid = null; + ProtectedRegion phomeRegion = null; + String regionName = getHomeRegionName(playerId, homeNumber); + phomeRegion = regionManager.getRegion(regionName); + if (phomeRegion != null) { + return PlayerHome.get(phomeRegion); + } + PlayerHome existingHome = getOwnedHome(playerId, homeNumber); + if (existingHome != null) { + return existingHome; + } + + homeCuboid = findNextUnownedHomeRegion(multiplier); + + getLogger().info("Metropolis Generating home in " + homeCuboid.toString()); + + ProtectedCuboidRegion newHomeRegion = + new ProtectedCuboidRegion(regionName, homeCuboid.getMin(), homeCuboid.getMax()); + newHomeRegion.setFlag(Flags.PVP, StateFlag.State.DENY); + newHomeRegion.setFlag(Flags.MOB_DAMAGE, StateFlag.State.DENY); + newHomeRegion.setFlag(Flags.MOB_SPAWNING, StateFlag.State.DENY); + newHomeRegion.setFlag(Flags.CREEPER_EXPLOSION, StateFlag.State.DENY); + newHomeRegion.setFlag(Flags.ENDER_BUILD, StateFlag.State.DENY); + newHomeRegion.setFlag(Flags.GHAST_FIREBALL, StateFlag.State.DENY); + newHomeRegion.setFlag(Flags.TNT, StateFlag.State.DENY); + + DefaultDomain d = newHomeRegion.getOwners(); + d.addPlayer(playerId); + newHomeRegion.setPriority(1); + + regionManager.addRegion(newHomeRegion); + try { + regionManager.save(); + } catch (Exception e) { + getLogger().info("Metropolis: ERROR Problem saving region"); + e.printStackTrace(); + } + + try { + regionManager.save(); + } catch (Exception e) { + getLogger().info("Metropolis: ERROR Problem saving region"); + e.printStackTrace(); + } + getLogger() + .info( + String.format( + "New home region (%d, %d, %d) (%d, %d, %d)", + newHomeRegion.getMinimumPoint().x(), + newHomeRegion.getMinimumPoint().y(), + newHomeRegion.getMinimumPoint().z(), + newHomeRegion.getMaximumPoint().x(), + newHomeRegion.getMaximumPoint().y(), + newHomeRegion.getMaximumPoint().z())); + + setHomeOccupied( + playerId, + playerName, + homeNumber, + newHomeRegion.getMinimumPoint(), + newHomeRegion.getMaximumPoint()); + _currentHomes.putIfAbsent(playerId, homeNumber); + saveCurrentHomes(); + + createRoads(homeCuboid); + + if (generateFloor) { + generateFloor(homeCuboid); + } + + if (DEBUG) { + getLogger().info(String.format("generateSign: %s", String.valueOf(generateSign))); + } + if (generateSign) { + generateSign(homeCuboid, playerName); + } + + if (DEBUG) { + getLogger().info(String.format("Done generating home for %s", playerName)); + } + + PlayerHome home = new PlayerHome(newHomeRegion); + home.setPlayerName(playerName); + return home; + } + + private void generateSign(Cuboid plotCuboid, String playerName) { + Block signBlock = + world.getBlockAt(plotCuboid.getCenterX(), roadLevel + 1, plotCuboid.getCenterZ()); + signBlock.setType(Material.OAK_SIGN); + Sign sign = (Sign) signBlock.getState(); + sign.setLine(0, "Home of"); + + sign.setLine(1, playerName.substring(0, Math.min(15, playerName.length()))); + if (playerName.length() > 15) { + sign.setLine(2, playerName.substring(16, Math.min(30, playerName.length()))); + if (playerName.length() > 45) { + sign.setLine(3, playerName.substring(31, Math.min(45, playerName.length()))); + } + } + + sign.update(true); + } + + public List getCityBlocks() { + return Collections.unmodifiableList(_occupiedPlots); + } + + public World getWorld() { + return world; + } + + public void reserveCuboid(String regionName, Cuboid cuboid) { + ProtectedCuboidRegion reservedRegion = + new ProtectedCuboidRegion(regionName, cuboid.getMin(), cuboid.getMax()); + reservedRegion.setFlag(Flags.PVP, StateFlag.State.DENY); + reservedRegion.setFlag(Flags.MOB_DAMAGE, StateFlag.State.DENY); + reservedRegion.setFlag(Flags.MOB_SPAWNING, StateFlag.State.DENY); + reservedRegion.setFlag(Flags.CREEPER_EXPLOSION, StateFlag.State.DENY); + reservedRegion.setFlag(Flags.ENDER_BUILD, StateFlag.State.DENY); + reservedRegion.setFlag(Flags.GHAST_FIREBALL, StateFlag.State.DENY); + reservedRegion.setFlag(Flags.TNT, StateFlag.State.DENY); + reservedRegion.setFlag(Flags.LAVA_FLOW, StateFlag.State.DENY); + reservedRegion.setFlag(Flags.SNOW_FALL, StateFlag.State.DENY); + regionManager.addRegion(reservedRegion); + + _occupiedPlots.add(Plot.get(reservedRegion)); + saveRegions(); + } + + public Cuboid getCityCuboid() { + return _cityCuboid; + } + + public boolean getGenerateWall() { + return generateWall; + } + + public Material getWallMaterial() { + return wallMaterial; + } + + public int getWallheight() { + return wallHeight; + } + + public ProtectedRegion getRegion(String regionName) { + if (regionManager == null) { + return null; + } + + return regionManager.getRegion(regionName); + } + + public void removeRegion(String regionId) { + if (regionManager == null) { + return; + } + + try { + regionManager.removeRegion(regionId); + } catch (Exception ex) { + getLogger() + .info(String.format("[ERROR] Metropolis: Unable to remove region {%s}.", regionId)); + return; + } + } + + public void saveRegions() { + try { + regionManager.save(); + } catch (Exception ex) { + getLogger().info(String.format("[SEVERE] Metropolis: Unable to save WorldGuard regions.")); + return; + } + } + + public int getNumPlots(UUID playerId) { + if (_ownedPlots.containsKey(playerId)) { + List plots = _ownedPlots.get(playerId); + if (plots == null) { + return 0; + } else { + return plots.size(); + } + } else { + return 0; + } + } + + public int getMaxPlots(UUID playerId) { + if (_userOverrides.containsKey(playerId)) { + return _userOverrides.get(playerId).getMaxPlots(); + } else { + return _maxPlots; + } + } + + public void assignPlot(OfflinePlayer player) { + generateHome(player); + } + + private int getPlotMultiplier(UUID playerId) { + if (_userOverrides.containsKey(playerId)) { + return _userOverrides.get(playerId).getPlotMultiplier(); + } else { + return _plotMultiplier; + } + } + + public Plot getPlot(String string) { + /** + * string is the name of the region to get a plot for + * + *

loop through all regions and find one with the specified name return null if there is none + */ + for (Plot plot : _occupiedPlots) { + if (plot.getRegionName().equals(string)) { + return plot; + } + } + + return null; + } + + public Player getPlayer(String name) { + Player player = getServer().getPlayerExact(name); + if (player != null) { + return player; + } + return PlayerLookup.findOnline(name, getServer().getOnlinePlayers()); + } + + public OfflinePlayer getOfflinePlayer(String name) { + Player onlinePlayer = getPlayer(name); + if (onlinePlayer != null) { + return onlinePlayer; + } + for (OfflinePlayer offlinePlayer : getServer().getOfflinePlayers()) { + if (name.equalsIgnoreCase(offlinePlayer.getName())) { + return offlinePlayer; + } + } + return null; + } + + public String teleportPlayerToPlot(Player player, Plot plot) { + Location loc = plot.getViableSpawnLocation(world); + + if (loc != null) { + player.teleport(loc); + } + + return null; + } + + public boolean homeExists(UUID playerId, int homeNumber) { + for (Plot plot : _occupiedPlots) { + if (plot instanceof PlayerHome home + && home.getPlayerId().equals(playerId) + && home.getNumber() == homeNumber) { + return true; + } + } + + return false; + } + + public void setHome(UUID playerId, int newHomeNumber) { + _currentHomes.put(playerId, newHomeNumber); + saveCurrentHomes(); + } + + public String getHomeRegionName(UUID playerId, int homeNumber) { + PlayerHome existingHome = getOwnedHome(playerId, homeNumber); + return existingHome == null + ? String.format("h_%d_%s", homeNumber, playerId) + : existingHome.getRegionName(); + } + + public String getCurrentHomeRegionName(UUID playerId) { + return getHomeRegionName(playerId, _currentHomes.getOrDefault(playerId, 1)); + } + + private PlayerHome getOwnedHome(UUID playerId, int homeNumber) { + List plots = _ownedPlots.get(playerId); + if (plots == null) { + return null; + } + for (Plot plot : plots) { + if (plot instanceof PlayerHome home && home.getNumber() == homeNumber) { + return home; + } + } + return null; + } + + private void saveCurrentHomes() { + File homesFile = new File(getDataFolder(), "currentHomes.yml"); + try { + CurrentHomesStore.save(homesFile, _currentHomes); + } catch (IOException e) { + getLogger().log(java.util.logging.Level.SEVERE, "Unable to save currentHomes.yml", e); + } + } +} diff --git a/src/main/java/com/majinnaibu/bukkitplugins/metropolis/PlayerHome.java b/src/main/java/com/majinnaibu/bukkitplugins/metropolis/PlayerHome.java index 105c5ef..467e205 100644 --- a/src/main/java/com/majinnaibu/bukkitplugins/metropolis/PlayerHome.java +++ b/src/main/java/com/majinnaibu/bukkitplugins/metropolis/PlayerHome.java @@ -1,107 +1,144 @@ -package com.majinnaibu.bukkitplugins.metropolis; - -import java.util.Set; -import java.util.UUID; - -import org.bukkit.Bukkit; -import org.bukkit.OfflinePlayer; - -import com.sk89q.worldedit.math.BlockVector3; -import com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion; -import com.sk89q.worldguard.protection.regions.ProtectedRegion; - -public class PlayerHome extends Plot{ - private UUID playerId; - private String playerName; - public UUID getPlayerId(){return this.playerId;} - public String getPlayerName(){return this.playerName;} - public void setPlayerName(String playerName){this.playerName = playerName;} - - private int number; - - public PlayerHome(UUID ownerId, String ownerName, int homeNumber, BlockVector3 min, BlockVector3 max) { - super(String.format("h_%d_%s", homeNumber, ownerId), min, max); - this.playerId = ownerId; - this.playerName = ownerName; - this.number = homeNumber; - } - - public PlayerHome() { - this.playerId = null; - this.playerName = ""; - } - - public PlayerHome(ProtectedRegion homeRegion){ - String regionName = homeRegion.getId(); - if (!regionName.startsWith("h_")) { - throw new IllegalArgumentException("Not a Metropolis home region: " + regionName); - } - String regionOwner = regionName.substring(2); - int separator = regionOwner.indexOf('_'); - if (separator > 0) { - try { - this.number = Integer.parseInt(regionOwner.substring(0, separator)); - regionOwner = regionOwner.substring(separator + 1); - } catch (NumberFormatException ex) { - this.number = 1; - } - } else { - this.number = 1; - } - try { - this.playerId = UUID.fromString(regionOwner); - OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(playerId); - this.playerName = offlinePlayer.getName() == null ? regionOwner : offlinePlayer.getName(); - } catch (IllegalArgumentException ex) { - Set ownerIds = homeRegion.getOwners().getUniqueIds(); - this.playerId = ownerIds.isEmpty() - ? Bukkit.getOfflinePlayer(regionOwner).getUniqueId() - : ownerIds.iterator().next(); - this.playerName = regionOwner; - } - setCuboid(new Cuboid(homeRegion.getMinimumPoint(), homeRegion.getMaximumPoint())); - } - - @Override - public boolean equals(Object other) { - if(!(other instanceof PlayerHome)){ - return super.equals(other); - } - - PlayerHome otherPlayerHome = (PlayerHome)other; - - if(!java.util.Objects.equals(this.playerId, otherPlayerHome.playerId)){ - return false; - } - - if(!getCuboid().equals(otherPlayerHome.getCuboid())){ - return false; - } - - return true; - } - - public String toFriendlyString() { - StringBuilder sb = new StringBuilder(); - - sb.append(String.format("Metropolis Home {Owner: %s min: (%d, %d, %d) max: (%d, %d, %d)}", getPlayerName(), getCuboid().getMinX(), getCuboid().getMinY(), getCuboid().getMinZ(), getCuboid().getMaxX(), getCuboid().getMaxY(), getCuboid().getMaxZ())); - - return sb.toString(); - } - - public static PlayerHome get(ProtectedRegion homeRegion){ - if(homeRegion instanceof ProtectedCuboidRegion){ - return new PlayerHome((ProtectedCuboidRegion) homeRegion); - }else{ - return null; - } - } - - public Integer getNumber() { - return number; - } - - public void setNumber(int number){ - this.number = number; - } -} +/* +This file is part of Metropolis. + +Metropolis is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +Metropolis is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with Metropolis. If not, see . +*/ + +package com.majinnaibu.bukkitplugins.metropolis; + +import java.util.Set; +import java.util.UUID; + +import com.sk89q.worldedit.math.BlockVector3; +import com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion; +import com.sk89q.worldguard.protection.regions.ProtectedRegion; + +import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; + +public class PlayerHome extends Plot { + private UUID playerId; + private String playerName; + + public UUID getPlayerId() { + return this.playerId; + } + + public String getPlayerName() { + return this.playerName; + } + + public void setPlayerName(String playerName) { + this.playerName = playerName; + } + + private int number; + + public PlayerHome( + UUID ownerId, String ownerName, int homeNumber, BlockVector3 min, BlockVector3 max) { + super(String.format("h_%d_%s", homeNumber, ownerId), min, max); + this.playerId = ownerId; + this.playerName = ownerName; + this.number = homeNumber; + } + + public PlayerHome() { + this.playerId = null; + this.playerName = ""; + } + + public PlayerHome(ProtectedRegion homeRegion) { + String regionName = homeRegion.getId(); + if (!regionName.startsWith("h_")) { + throw new IllegalArgumentException("Not a Metropolis home region: " + regionName); + } + String regionOwner = regionName.substring(2); + int separator = regionOwner.indexOf('_'); + if (separator > 0) { + try { + this.number = Integer.parseInt(regionOwner.substring(0, separator)); + regionOwner = regionOwner.substring(separator + 1); + } catch (NumberFormatException ex) { + this.number = 1; + } + } else { + this.number = 1; + } + try { + this.playerId = UUID.fromString(regionOwner); + OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(playerId); + this.playerName = offlinePlayer.getName() == null ? regionOwner : offlinePlayer.getName(); + } catch (IllegalArgumentException ex) { + Set ownerIds = homeRegion.getOwners().getUniqueIds(); + this.playerId = + ownerIds.isEmpty() + ? Bukkit.getOfflinePlayer(regionOwner).getUniqueId() + : ownerIds.iterator().next(); + this.playerName = regionOwner; + } + setCuboid(new Cuboid(homeRegion.getMinimumPoint(), homeRegion.getMaximumPoint())); + } + + @Override + public boolean equals(Object other) { + if (!(other instanceof PlayerHome)) { + return super.equals(other); + } + + PlayerHome otherPlayerHome = (PlayerHome) other; + + if (!java.util.Objects.equals(this.playerId, otherPlayerHome.playerId)) { + return false; + } + + if (!getCuboid().equals(otherPlayerHome.getCuboid())) { + return false; + } + + return true; + } + + public String toFriendlyString() { + StringBuilder sb = new StringBuilder(); + + sb.append( + String.format( + "Metropolis Home {Owner: %s min: (%d, %d, %d) max: (%d, %d, %d)}", + getPlayerName(), + getCuboid().getMinX(), + getCuboid().getMinY(), + getCuboid().getMinZ(), + getCuboid().getMaxX(), + getCuboid().getMaxY(), + getCuboid().getMaxZ())); + + return sb.toString(); + } + + public static PlayerHome get(ProtectedRegion homeRegion) { + if (homeRegion instanceof ProtectedCuboidRegion) { + return new PlayerHome((ProtectedCuboidRegion) homeRegion); + } else { + return null; + } + } + + public Integer getNumber() { + return number; + } + + public void setNumber(int number) { + this.number = number; + } +} diff --git a/src/main/java/com/majinnaibu/bukkitplugins/metropolis/PlayerLookup.java b/src/main/java/com/majinnaibu/bukkitplugins/metropolis/PlayerLookup.java index 140c7d9..f655ab2 100644 --- a/src/main/java/com/majinnaibu/bukkitplugins/metropolis/PlayerLookup.java +++ b/src/main/java/com/majinnaibu/bukkitplugins/metropolis/PlayerLookup.java @@ -1,19 +1,36 @@ +/* +This file is part of Metropolis. + +Metropolis is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +Metropolis is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with Metropolis. If not, see . +*/ + package com.majinnaibu.bukkitplugins.metropolis; import org.bukkit.entity.Player; final class PlayerLookup { - private PlayerLookup() {} + private PlayerLookup() {} - static Player findOnline(String requestedName, Iterable onlinePlayers) { - if (requestedName == null) { - return null; - } - for (Player player : onlinePlayers) { - if (requestedName.equalsIgnoreCase(player.getName())) { - return player; - } - } - return null; - } -} \ No newline at end of file + static Player findOnline(String requestedName, Iterable onlinePlayers) { + if (requestedName == null) { + return null; + } + for (Player player : onlinePlayers) { + if (requestedName.equalsIgnoreCase(player.getName())) { + return player; + } + } + return null; + } +} diff --git a/src/main/java/com/majinnaibu/bukkitplugins/metropolis/Plot.java b/src/main/java/com/majinnaibu/bukkitplugins/metropolis/Plot.java index c3d94f1..22eba96 100644 --- a/src/main/java/com/majinnaibu/bukkitplugins/metropolis/Plot.java +++ b/src/main/java/com/majinnaibu/bukkitplugins/metropolis/Plot.java @@ -1,119 +1,164 @@ -package com.majinnaibu.bukkitplugins.metropolis; - -import org.bukkit.Location; -import org.bukkit.Material; -import org.bukkit.World; -import org.bukkit.block.Block; - -import com.sk89q.worldedit.math.BlockVector3; -import com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion; -import com.sk89q.worldguard.protection.regions.ProtectedRegion; - -public class Plot implements Comparable{ - private int _id; - public int getId(){return _id;} - public void setId(int id){_id = id;} - - private Cuboid _cuboid; - public Cuboid getCuboid(){return _cuboid;} - public void setCuboid(Cuboid cuboid){_cuboid = cuboid;} - - private String _regionName; - public String getRegionName(){return _regionName;} - public void setRegionName(String regionName){_regionName = regionName;} - - public Plot(String regionName, BlockVector3 min, BlockVector3 max){ - _cuboid = new Cuboid(min, max); - _regionName = regionName; - } - - public Plot(ProtectedCuboidRegion cuboid){ - _cuboid = new Cuboid(cuboid.getMinimumPoint(), cuboid.getMaximumPoint()); - _regionName = cuboid.getId(); - } - - public Plot(){ - _cuboid = new Cuboid(); - _regionName = ""; - } - - public BlockVector3 getPlotMin(int roadWidth) { - return BlockVector3.at(_cuboid.minX - roadWidth/2, _cuboid.minY, _cuboid.minZ - roadWidth/2); - } - - public int getRow(int roadWidth, int plotSizeZ){ - BlockVector3 min = getPlotMin(roadWidth); - return min.z() / plotSizeZ; - } - - public int getCol(int roadWidth, int plotSizeX){ - return getPlotMin(roadWidth).x() / plotSizeX; - } - - @Override - public boolean equals(Object other) { - if(!(other instanceof Plot)){ - return super.equals(other); - } - - Plot otherPlayerHome = (Plot)other; - - if(!getCuboid().equals(otherPlayerHome.getCuboid())){ - return false; - } - - return true; - } - - @Override - public int compareTo(Plot another) { - return getCuboid().compareTo(another.getCuboid()); - } - - @Override - public String toString(){ - StringBuilder sb = new StringBuilder(); - - sb.append(String.format("{PlayerHome }")); - - return sb.toString(); - } - - public static Plot get(ProtectedRegion region){ - if(region instanceof ProtectedCuboidRegion){ - return new Plot((ProtectedCuboidRegion) region); - }else{ - return null; - } - } - - public String toFriendlyString() { - StringBuilder sb = new StringBuilder(); - - sb.append(String.format("Metropolis Reserved Plot {min: (%d, %d, %d) max: (%d, %d, %d)}", getCuboid().getMinX(), getCuboid().getMinY(), getCuboid().getMinZ(), getCuboid().getMaxX(), getCuboid().getMaxY(), getCuboid().getMaxZ())); - - return sb.toString(); - } - - public boolean contains(Location bedSpawn) { - return _cuboid.contains(bedSpawn); - } - - public Location getViableSpawnLocation(World world) { - Cuboid cuboid = getCuboid(); - for(int y=cuboid.maxY-1; y>= cuboid.minY; y--){ - for(int x=cuboid.minX; x<= cuboid.maxX; x++){ - for(int z=cuboid.minZ; z<= cuboid.maxZ; z++){ - Block block = world.getBlockAt(x, y, z); - Block blockAbove = world.getBlockAt(x, y+1, z); - Block blockUnder = world.getBlockAt(x, y-1, z); - if(block.getType() == Material.AIR & blockAbove.getType() == Material.AIR && blockUnder.getType() != Material.AIR){ - return new Location(world, x, y, z); - } - } - } - } - - return null; - } -} +/* +This file is part of Metropolis. + +Metropolis is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +Metropolis is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with Metropolis. If not, see . +*/ + +package com.majinnaibu.bukkitplugins.metropolis; + +import com.sk89q.worldedit.math.BlockVector3; +import com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion; +import com.sk89q.worldguard.protection.regions.ProtectedRegion; + +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.World; +import org.bukkit.block.Block; + +public class Plot implements Comparable { + private int _id; + + public int getId() { + return _id; + } + + public void setId(int id) { + _id = id; + } + + private Cuboid _cuboid; + + public Cuboid getCuboid() { + return _cuboid; + } + + public void setCuboid(Cuboid cuboid) { + _cuboid = cuboid; + } + + private String _regionName; + + public String getRegionName() { + return _regionName; + } + + public void setRegionName(String regionName) { + _regionName = regionName; + } + + public Plot(String regionName, BlockVector3 min, BlockVector3 max) { + _cuboid = new Cuboid(min, max); + _regionName = regionName; + } + + public Plot(ProtectedCuboidRegion cuboid) { + _cuboid = new Cuboid(cuboid.getMinimumPoint(), cuboid.getMaximumPoint()); + _regionName = cuboid.getId(); + } + + public Plot() { + _cuboid = new Cuboid(); + _regionName = ""; + } + + public BlockVector3 getPlotMin(int roadWidth) { + return BlockVector3.at( + _cuboid.minX - roadWidth / 2, _cuboid.minY, _cuboid.minZ - roadWidth / 2); + } + + public int getRow(int roadWidth, int plotSizeZ) { + BlockVector3 min = getPlotMin(roadWidth); + return min.z() / plotSizeZ; + } + + public int getCol(int roadWidth, int plotSizeX) { + return getPlotMin(roadWidth).x() / plotSizeX; + } + + @Override + public boolean equals(Object other) { + if (!(other instanceof Plot)) { + return super.equals(other); + } + + Plot otherPlayerHome = (Plot) other; + + if (!getCuboid().equals(otherPlayerHome.getCuboid())) { + return false; + } + + return true; + } + + @Override + public int compareTo(Plot another) { + return getCuboid().compareTo(another.getCuboid()); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + + sb.append(String.format("{PlayerHome }")); + + return sb.toString(); + } + + public static Plot get(ProtectedRegion region) { + if (region instanceof ProtectedCuboidRegion) { + return new Plot((ProtectedCuboidRegion) region); + } else { + return null; + } + } + + public String toFriendlyString() { + StringBuilder sb = new StringBuilder(); + + sb.append( + String.format( + "Metropolis Reserved Plot {min: (%d, %d, %d) max: (%d, %d, %d)}", + getCuboid().getMinX(), + getCuboid().getMinY(), + getCuboid().getMinZ(), + getCuboid().getMaxX(), + getCuboid().getMaxY(), + getCuboid().getMaxZ())); + + return sb.toString(); + } + + public boolean contains(Location bedSpawn) { + return _cuboid.contains(bedSpawn); + } + + public Location getViableSpawnLocation(World world) { + Cuboid cuboid = getCuboid(); + for (int y = cuboid.maxY - 1; y >= cuboid.minY; y--) { + for (int x = cuboid.minX; x <= cuboid.maxX; x++) { + for (int z = cuboid.minZ; z <= cuboid.maxZ; z++) { + Block block = world.getBlockAt(x, y, z); + Block blockAbove = world.getBlockAt(x, y + 1, z); + Block blockUnder = world.getBlockAt(x, y - 1, z); + if (block.getType() == Material.AIR & blockAbove.getType() == Material.AIR + && blockUnder.getType() != Material.AIR) { + return new Location(world, x, y, z); + } + } + } + } + + return null; + } +} diff --git a/src/main/java/com/majinnaibu/bukkitplugins/metropolis/UserOverride.java b/src/main/java/com/majinnaibu/bukkitplugins/metropolis/UserOverride.java index 0d15199..5056310 100644 --- a/src/main/java/com/majinnaibu/bukkitplugins/metropolis/UserOverride.java +++ b/src/main/java/com/majinnaibu/bukkitplugins/metropolis/UserOverride.java @@ -1,17 +1,42 @@ -package com.majinnaibu.bukkitplugins.metropolis; - -public class UserOverride { - private String _username; - private int _plotMultiplier; - private int _maxPlots; - - public String getUsername(){return _username;} - public int getPlotMultiplier(){return _plotMultiplier;} - public int getMaxPlots(){return _maxPlots;} - - public UserOverride(String username, int plotMultiplier, int maxPlots){ - _username = username; - _plotMultiplier = plotMultiplier; - _maxPlots = maxPlots; - } -} +/* +This file is part of Metropolis. + +Metropolis is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +Metropolis is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with Metropolis. If not, see . +*/ + +package com.majinnaibu.bukkitplugins.metropolis; + +public class UserOverride { + private String _username; + private int _plotMultiplier; + private int _maxPlots; + + public String getUsername() { + return _username; + } + + public int getPlotMultiplier() { + return _plotMultiplier; + } + + public int getMaxPlots() { + return _maxPlots; + } + + public UserOverride(String username, int plotMultiplier, int maxPlots) { + _username = username; + _plotMultiplier = plotMultiplier; + _maxPlots = maxPlots; + } +} diff --git a/src/main/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisCommand.java b/src/main/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisCommand.java index 4fba698..853033a 100644 --- a/src/main/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisCommand.java +++ b/src/main/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisCommand.java @@ -1,24 +1,40 @@ -package com.majinnaibu.bukkitplugins.metropolis.commands; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; - -import com.majinnaibu.bukkitplugins.metropolis.MetropolisPlugin; - -public class MetropolisCommand implements CommandExecutor { - private MetropolisPlugin _plugin; - - public MetropolisCommand(MetropolisPlugin plugin){ - _plugin = plugin; - } - - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - - sender.sendMessage("Metropolis: version "+ _plugin.pdf.getVersion()); - - return true; - } - -} +/* +This file is part of Metropolis. + +Metropolis is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +Metropolis is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with Metropolis. If not, see . +*/ + +package com.majinnaibu.bukkitplugins.metropolis.commands; + +import com.majinnaibu.bukkitplugins.metropolis.MetropolisPlugin; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; + +public class MetropolisCommand implements CommandExecutor { + private MetropolisPlugin _plugin; + + public MetropolisCommand(MetropolisPlugin plugin) { + _plugin = plugin; + } + + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + + sender.sendMessage("Metropolis: version " + _plugin.pdf.getVersion()); + + return true; + } +} diff --git a/src/main/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisDebugGenerateTestHomesCommand.java b/src/main/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisDebugGenerateTestHomesCommand.java index 01e6d12..d89c90f 100644 --- a/src/main/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisDebugGenerateTestHomesCommand.java +++ b/src/main/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisDebugGenerateTestHomesCommand.java @@ -1,32 +1,47 @@ -package com.majinnaibu.bukkitplugins.metropolis.commands; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; - -import com.majinnaibu.bukkitplugins.metropolis.MetropolisPlugin; - -public class MetropolisDebugGenerateTestHomesCommand implements CommandExecutor { - - private MetropolisPlugin _plugin; - - public MetropolisDebugGenerateTestHomesCommand(MetropolisPlugin plugin) { - _plugin = plugin; - - } - - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - try{ - int numHomes = Integer.parseInt(args[0]); - for(int i=1; i<= numHomes; i++){ - _plugin.generateHome(String.format("test%d", i)); - } - - return true; - }catch(NumberFormatException ex){ - return false; - } - } - -} +/* +This file is part of Metropolis. + +Metropolis is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +Metropolis is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with Metropolis. If not, see . +*/ + +package com.majinnaibu.bukkitplugins.metropolis.commands; + +import com.majinnaibu.bukkitplugins.metropolis.MetropolisPlugin; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; + +public class MetropolisDebugGenerateTestHomesCommand implements CommandExecutor { + + private MetropolisPlugin _plugin; + + public MetropolisDebugGenerateTestHomesCommand(MetropolisPlugin plugin) { + _plugin = plugin; + } + + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + try { + int numHomes = Integer.parseInt(args[0]); + for (int i = 1; i <= numHomes; i++) { + _plugin.generateHome(String.format("test%d", i)); + } + + return true; + } catch (NumberFormatException ex) { + return false; + } + } +} diff --git a/src/main/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisFlagResetCommand.java b/src/main/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisFlagResetCommand.java index e41fe9c..2676dae 100644 --- a/src/main/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisFlagResetCommand.java +++ b/src/main/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisFlagResetCommand.java @@ -1,53 +1,65 @@ -package com.majinnaibu.bukkitplugins.metropolis.commands; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; - -import com.majinnaibu.bukkitplugins.metropolis.MetropolisPlugin; -import com.sk89q.worldguard.protection.flags.Flags; -import com.sk89q.worldguard.protection.flags.StateFlag; -import com.sk89q.worldguard.protection.regions.ProtectedRegion; - -public class MetropolisFlagResetCommand implements CommandExecutor { - private MetropolisPlugin _plugin; - - public MetropolisFlagResetCommand(MetropolisPlugin plugin){ - _plugin = plugin; - } - - @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - ProtectedRegion cityRegion = _plugin.regionManager.getRegion("City"); - cityRegion.setFlag(Flags.PVP, StateFlag.State.DENY); - cityRegion.setFlag(Flags.MOB_DAMAGE, StateFlag.State.DENY); - cityRegion.setFlag(Flags.MOB_SPAWNING, StateFlag.State.DENY); - cityRegion.setFlag(Flags.CREEPER_EXPLOSION, StateFlag.State.DENY); - cityRegion.setFlag(Flags.ENDER_BUILD, StateFlag.State.DENY); - cityRegion.setFlag(Flags.GHAST_FIREBALL, StateFlag.State.DENY); - cityRegion.setFlag(Flags.TNT, StateFlag.State.DENY); - cityRegion.setFlag(Flags.LAVA_FLOW, StateFlag.State.DENY); - cityRegion.setFlag(Flags.SNOW_FALL, StateFlag.State.DENY); - - for(ProtectedRegion homeRegion: _plugin.regionManager.getRegions().values()){ - if(homeRegion.getId().startsWith("h_")){ - homeRegion.setFlag(Flags.PVP, StateFlag.State.DENY); - homeRegion.setFlag(Flags.MOB_DAMAGE, StateFlag.State.DENY); - homeRegion.setFlag(Flags.MOB_SPAWNING, StateFlag.State.DENY); - homeRegion.setFlag(Flags.CREEPER_EXPLOSION, StateFlag.State.DENY); - homeRegion.setFlag(Flags.ENDER_BUILD, StateFlag.State.DENY); - homeRegion.setFlag(Flags.GHAST_FIREBALL, StateFlag.State.DENY); - homeRegion.setFlag(Flags.TNT, StateFlag.State.DENY); - - } - } - - sender.sendMessage("Metropolis: flags have been reset"); - - return true; - - - - } - -} +/* +This file is part of Metropolis. + +Metropolis is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +Metropolis is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with Metropolis. If not, see . +*/ + +package com.majinnaibu.bukkitplugins.metropolis.commands; + +import com.majinnaibu.bukkitplugins.metropolis.MetropolisPlugin; +import com.sk89q.worldguard.protection.flags.Flags; +import com.sk89q.worldguard.protection.flags.StateFlag; +import com.sk89q.worldguard.protection.regions.ProtectedRegion; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; + +public class MetropolisFlagResetCommand implements CommandExecutor { + private MetropolisPlugin _plugin; + + public MetropolisFlagResetCommand(MetropolisPlugin plugin) { + _plugin = plugin; + } + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + ProtectedRegion cityRegion = _plugin.regionManager.getRegion("City"); + cityRegion.setFlag(Flags.PVP, StateFlag.State.DENY); + cityRegion.setFlag(Flags.MOB_DAMAGE, StateFlag.State.DENY); + cityRegion.setFlag(Flags.MOB_SPAWNING, StateFlag.State.DENY); + cityRegion.setFlag(Flags.CREEPER_EXPLOSION, StateFlag.State.DENY); + cityRegion.setFlag(Flags.ENDER_BUILD, StateFlag.State.DENY); + cityRegion.setFlag(Flags.GHAST_FIREBALL, StateFlag.State.DENY); + cityRegion.setFlag(Flags.TNT, StateFlag.State.DENY); + cityRegion.setFlag(Flags.LAVA_FLOW, StateFlag.State.DENY); + cityRegion.setFlag(Flags.SNOW_FALL, StateFlag.State.DENY); + + for (ProtectedRegion homeRegion : _plugin.regionManager.getRegions().values()) { + if (homeRegion.getId().startsWith("h_")) { + homeRegion.setFlag(Flags.PVP, StateFlag.State.DENY); + homeRegion.setFlag(Flags.MOB_DAMAGE, StateFlag.State.DENY); + homeRegion.setFlag(Flags.MOB_SPAWNING, StateFlag.State.DENY); + homeRegion.setFlag(Flags.CREEPER_EXPLOSION, StateFlag.State.DENY); + homeRegion.setFlag(Flags.ENDER_BUILD, StateFlag.State.DENY); + homeRegion.setFlag(Flags.GHAST_FIREBALL, StateFlag.State.DENY); + homeRegion.setFlag(Flags.TNT, StateFlag.State.DENY); + } + } + + sender.sendMessage("Metropolis: flags have been reset"); + + return true; + } +} diff --git a/src/main/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisHomeAcquire.java b/src/main/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisHomeAcquire.java index 716fe97..6f0d08b 100644 --- a/src/main/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisHomeAcquire.java +++ b/src/main/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisHomeAcquire.java @@ -1,35 +1,51 @@ +/* +This file is part of Metropolis. + +Metropolis is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +Metropolis is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with Metropolis. If not, see . +*/ + package com.majinnaibu.bukkitplugins.metropolis.commands; +import com.majinnaibu.bukkitplugins.metropolis.MetropolisPlugin; + import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -import com.majinnaibu.bukkitplugins.metropolis.MetropolisPlugin; - public class MetropolisHomeAcquire implements CommandExecutor { - MetropolisPlugin _plugin; - - public MetropolisHomeAcquire(MetropolisPlugin plugin){ - _plugin = plugin; - } + MetropolisPlugin _plugin; - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if(!(sender instanceof Player)){ - sender.sendMessage("This command can only be used by players"); - return false; - } - - Player player = (Player) sender; - - if(_plugin.getNumPlots(player.getUniqueId()) >= _plugin.getMaxPlots(player.getUniqueId())){ - sender.sendMessage("You cannot have any more plots"); - return false; - } - - _plugin.assignPlot(player); - return true; - } + public MetropolisHomeAcquire(MetropolisPlugin plugin) { + _plugin = plugin; + } + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if (!(sender instanceof Player)) { + sender.sendMessage("This command can only be used by players"); + return false; + } + + Player player = (Player) sender; + + if (_plugin.getNumPlots(player.getUniqueId()) >= _plugin.getMaxPlots(player.getUniqueId())) { + sender.sendMessage("You cannot have any more plots"); + return false; + } + + _plugin.assignPlot(player); + return true; + } } diff --git a/src/main/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisHomeEvictCommand.java b/src/main/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisHomeEvictCommand.java index 6f2b35b..45be49e 100644 --- a/src/main/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisHomeEvictCommand.java +++ b/src/main/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisHomeEvictCommand.java @@ -1,60 +1,79 @@ +/* +This file is part of Metropolis. + +Metropolis is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +Metropolis is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with Metropolis. If not, see . +*/ + package com.majinnaibu.bukkitplugins.metropolis.commands; +import com.majinnaibu.bukkitplugins.metropolis.MetropolisPlugin; +import com.sk89q.worldguard.protection.regions.ProtectedRegion; + import org.bukkit.OfflinePlayer; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; -import com.majinnaibu.bukkitplugins.metropolis.MetropolisPlugin; -import com.sk89q.worldguard.protection.regions.ProtectedRegion; - public class MetropolisHomeEvictCommand implements CommandExecutor { - MetropolisPlugin _plugin = null; + MetropolisPlugin _plugin = null; - public MetropolisHomeEvictCommand(MetropolisPlugin plugin){ - _plugin = plugin; - } - - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - //get the player and region - String playerName = ""; - - if(args.length == 0){ - return false; - } - - if(args.length >= 1){ - playerName = args[0]; - } - - OfflinePlayer player = _plugin.getOfflinePlayer(playerName); - if(player == null){ - sender.sendMessage(String.format("The requested player {%s}does not appear to exist.", playerName)); - return false; - } - - ProtectedRegion region = _plugin.getRegion(_plugin.getCurrentHomeRegionName(player.getUniqueId())); - if(region == null){ - sender.sendMessage(String.format("The player {%s} has no home to be evicted from.")); - return false; - } - - //remove the player as owner and/or member of the region - region.getMembers().removePlayer(player.getUniqueId()); - region.getOwners().removePlayer(player.getUniqueId()); - - //if the region has no owners delete the region - if(region.getMembers().size() == 0 && region.getOwners().size() == 0){ - _plugin.removeRegion(region.getId()); - } - - _plugin.saveRegions(); - - //?optionally regen the region - //_plugin.worldEdit.getCommand("regen").execute(_plugin.getServer().getConsoleSender(), "regen", new String[]{}); - - return true; - } + public MetropolisHomeEvictCommand(MetropolisPlugin plugin) { + _plugin = plugin; + } + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + // get the player and region + String playerName = ""; + + if (args.length == 0) { + return false; + } + + if (args.length >= 1) { + playerName = args[0]; + } + + OfflinePlayer player = _plugin.getOfflinePlayer(playerName); + if (player == null) { + sender.sendMessage( + String.format("The requested player {%s}does not appear to exist.", playerName)); + return false; + } + + ProtectedRegion region = + _plugin.getRegion(_plugin.getCurrentHomeRegionName(player.getUniqueId())); + if (region == null) { + sender.sendMessage(String.format("The player {%s} has no home to be evicted from.")); + return false; + } + + // remove the player as owner and/or member of the region + region.getMembers().removePlayer(player.getUniqueId()); + region.getOwners().removePlayer(player.getUniqueId()); + + // if the region has no owners delete the region + if (region.getMembers().size() == 0 && region.getOwners().size() == 0) { + _plugin.removeRegion(region.getId()); + } + + _plugin.saveRegions(); + + // ?optionally regen the region + // _plugin.worldEdit.getCommand("regen").execute(_plugin.getServer().getConsoleSender(), + // "regen", new String[]{}); + + return true; + } } diff --git a/src/main/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisHomeGenerateCommand.java b/src/main/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisHomeGenerateCommand.java index 892601a..dc748a8 100644 --- a/src/main/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisHomeGenerateCommand.java +++ b/src/main/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisHomeGenerateCommand.java @@ -1,36 +1,52 @@ -package com.majinnaibu.bukkitplugins.metropolis.commands; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.OfflinePlayer; - -import com.majinnaibu.bukkitplugins.metropolis.MetropolisPlugin; - -public class MetropolisHomeGenerateCommand implements CommandExecutor { - - private MetropolisPlugin _plugin; - - public MetropolisHomeGenerateCommand(MetropolisPlugin plugin){ - _plugin = plugin; - } - - @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - if(args.length < 1){ - return false; - } - - OfflinePlayer player = _plugin.getOfflinePlayer(args[0]); - if (player == null) { - sender.sendMessage("Unable to find player " + args[0]); - return false; - } - _plugin.generateHome(player); - - sender.sendMessage("[Metropolis] Home generated for " + args[0]); - - return true; - } - -} +/* +This file is part of Metropolis. + +Metropolis is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +Metropolis is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with Metropolis. If not, see . +*/ + +package com.majinnaibu.bukkitplugins.metropolis.commands; + +import com.majinnaibu.bukkitplugins.metropolis.MetropolisPlugin; + +import org.bukkit.OfflinePlayer; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; + +public class MetropolisHomeGenerateCommand implements CommandExecutor { + + private MetropolisPlugin _plugin; + + public MetropolisHomeGenerateCommand(MetropolisPlugin plugin) { + _plugin = plugin; + } + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if (args.length < 1) { + return false; + } + + OfflinePlayer player = _plugin.getOfflinePlayer(args[0]); + if (player == null) { + sender.sendMessage("Unable to find player " + args[0]); + return false; + } + _plugin.generateHome(player); + + sender.sendMessage("[Metropolis] Home generated for " + args[0]); + + return true; + } +} diff --git a/src/main/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisHomeGoCommand.java b/src/main/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisHomeGoCommand.java index 44a32e2..6a53f68 100644 --- a/src/main/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisHomeGoCommand.java +++ b/src/main/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisHomeGoCommand.java @@ -1,76 +1,94 @@ +/* +This file is part of Metropolis. + +Metropolis is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +Metropolis is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with Metropolis. If not, see . +*/ + package com.majinnaibu.bukkitplugins.metropolis.commands; +import com.majinnaibu.bukkitplugins.metropolis.MetropolisPlugin; +import com.majinnaibu.bukkitplugins.metropolis.PlayerHome; + import org.bukkit.Location; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -import com.majinnaibu.bukkitplugins.metropolis.MetropolisPlugin; -import com.majinnaibu.bukkitplugins.metropolis.PlayerHome; - public class MetropolisHomeGoCommand implements CommandExecutor { - MetropolisPlugin _plugin = null; - - public MetropolisHomeGoCommand(MetropolisPlugin plugin){ - _plugin = plugin; - } - - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - Player player = null; - if(sender instanceof Player){ - player = (Player)sender; + MetropolisPlugin _plugin = null; - if(args.length >= 1){ - if(player.hasPermission("")){ - player = _plugin.getPlayer(args[0]); - - if(player == null){ - sender.sendMessage(String.format("Unable to find player %s", args[0])); - return false; - } - }else{ - sender.sendMessage("Permission denied"); - return false; - } - } - }else{ - if(args.length >= 1){ - player = _plugin.getPlayer(args[0]); + public MetropolisHomeGoCommand(MetropolisPlugin plugin) { + _plugin = plugin; + } - if(player == null){ - sender.sendMessage(String.format("Unable to find player %s", args[0])); - return false; - } - }else{ - sender.sendMessage("You must be a player"); - return false; - } - } - - PlayerHome home = _plugin.getPlayerHome(player); - - Location loc = null; - Location bedSpawn = player.getBedSpawnLocation(); - - if(home.contains(bedSpawn)){ - loc = bedSpawn; - }else { - loc = home.getViableSpawnLocation(_plugin.getWorld()); - } - - if(loc != null){ - player.teleport(loc); - }else{ - if(sender == player){ - sender.sendMessage("There is no valid spawn location in your home"); - }else{ - sender.sendMessage(String.format("There is no valid spawn location in %s's home", player.getName())); - } - return false; - } - - return true; - } + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + Player player = null; + if (sender instanceof Player) { + player = (Player) sender; + + if (args.length >= 1) { + if (player.hasPermission("")) { + player = _plugin.getPlayer(args[0]); + + if (player == null) { + sender.sendMessage(String.format("Unable to find player %s", args[0])); + return false; + } + } else { + sender.sendMessage("Permission denied"); + return false; + } + } + } else { + if (args.length >= 1) { + player = _plugin.getPlayer(args[0]); + + if (player == null) { + sender.sendMessage(String.format("Unable to find player %s", args[0])); + return false; + } + } else { + sender.sendMessage("You must be a player"); + return false; + } + } + + PlayerHome home = _plugin.getPlayerHome(player); + + Location loc = null; + Location bedSpawn = player.getBedSpawnLocation(); + + if (home.contains(bedSpawn)) { + loc = bedSpawn; + } else { + loc = home.getViableSpawnLocation(_plugin.getWorld()); + } + + if (loc != null) { + player.teleport(loc); + } else { + if (sender == player) { + sender.sendMessage("There is no valid spawn location in your home"); + } else { + sender.sendMessage( + String.format("There is no valid spawn location in %s's home", player.getName())); + } + return false; + } + + return true; + } } diff --git a/src/main/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisHomeListCommand.java b/src/main/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisHomeListCommand.java index a7f3335..694ee6f 100644 --- a/src/main/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisHomeListCommand.java +++ b/src/main/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisHomeListCommand.java @@ -1,25 +1,41 @@ -package com.majinnaibu.bukkitplugins.metropolis.commands; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; - -import com.majinnaibu.bukkitplugins.metropolis.MetropolisPlugin; -import com.majinnaibu.bukkitplugins.metropolis.Plot; - -public class MetropolisHomeListCommand implements CommandExecutor { - private MetropolisPlugin _plugin; - - public MetropolisHomeListCommand(MetropolisPlugin plugin){ - _plugin = plugin; - } - - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - for(Plot cityBlock : _plugin.getCityBlocks()){ - sender.sendMessage(String.format("%s", cityBlock.toFriendlyString())); - } - return true; - } - -} +/* +This file is part of Metropolis. + +Metropolis is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +Metropolis is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with Metropolis. If not, see . +*/ + +package com.majinnaibu.bukkitplugins.metropolis.commands; + +import com.majinnaibu.bukkitplugins.metropolis.MetropolisPlugin; +import com.majinnaibu.bukkitplugins.metropolis.Plot; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; + +public class MetropolisHomeListCommand implements CommandExecutor { + private MetropolisPlugin _plugin; + + public MetropolisHomeListCommand(MetropolisPlugin plugin) { + _plugin = plugin; + } + + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + for (Plot cityBlock : _plugin.getCityBlocks()) { + sender.sendMessage(String.format("%s", cityBlock.toFriendlyString())); + } + return true; + } +} diff --git a/src/main/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisHomeMoveCommand.java b/src/main/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisHomeMoveCommand.java index 67edbd8..4593be1 100644 --- a/src/main/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisHomeMoveCommand.java +++ b/src/main/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisHomeMoveCommand.java @@ -1,53 +1,69 @@ +/* +This file is part of Metropolis. + +Metropolis is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +Metropolis is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with Metropolis. If not, see . +*/ + package com.majinnaibu.bukkitplugins.metropolis.commands; +import com.majinnaibu.bukkitplugins.metropolis.MetropolisPlugin; + import org.bukkit.OfflinePlayer; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; -import com.majinnaibu.bukkitplugins.metropolis.MetropolisPlugin; - public class MetropolisHomeMoveCommand implements CommandExecutor { - MetropolisPlugin _plugin; + MetropolisPlugin _plugin; - public MetropolisHomeMoveCommand(MetropolisPlugin plugin) { - _plugin = plugin; - } + public MetropolisHomeMoveCommand(MetropolisPlugin plugin) { + _plugin = plugin; + } - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - OfflinePlayer player = null; - int newHomeNumber = 0; - - if(sender instanceof OfflinePlayer){ - player = (OfflinePlayer)sender; - } - - if(args.length == 1){ - try{ - newHomeNumber = Integer.parseInt(args[0]); - }catch(NumberFormatException ex){ - return false; - } - }else if(args.length >= 2){ - try{ - newHomeNumber = Integer.parseInt(args[0]); - }catch(NumberFormatException ex){ - return false; - } - - player = _plugin.getOfflinePlayer(args[1]); - }else{ - return false; - } - - if(player == null || !_plugin.homeExists(player.getUniqueId(), newHomeNumber)){ - return false; - } - - _plugin.setHome(player.getUniqueId(), newHomeNumber); - - return true; - } + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + OfflinePlayer player = null; + int newHomeNumber = 0; + if (sender instanceof OfflinePlayer) { + player = (OfflinePlayer) sender; + } + + if (args.length == 1) { + try { + newHomeNumber = Integer.parseInt(args[0]); + } catch (NumberFormatException ex) { + return false; + } + } else if (args.length >= 2) { + try { + newHomeNumber = Integer.parseInt(args[0]); + } catch (NumberFormatException ex) { + return false; + } + + player = _plugin.getOfflinePlayer(args[1]); + } else { + return false; + } + + if (player == null || !_plugin.homeExists(player.getUniqueId(), newHomeNumber)) { + return false; + } + + _plugin.setHome(player.getUniqueId(), newHomeNumber); + + return true; + } } diff --git a/src/main/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisPlotGoCommand.java b/src/main/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisPlotGoCommand.java index f689117..dd2d615 100644 --- a/src/main/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisPlotGoCommand.java +++ b/src/main/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisPlotGoCommand.java @@ -1,49 +1,65 @@ +/* +This file is part of Metropolis. + +Metropolis is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +Metropolis is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with Metropolis. If not, see . +*/ + package com.majinnaibu.bukkitplugins.metropolis.commands; +import com.majinnaibu.bukkitplugins.metropolis.MetropolisPlugin; +import com.majinnaibu.bukkitplugins.metropolis.Plot; + import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -import com.majinnaibu.bukkitplugins.metropolis.MetropolisPlugin; -import com.majinnaibu.bukkitplugins.metropolis.Plot; - public class MetropolisPlotGoCommand implements CommandExecutor { - MetropolisPlugin _plugin; + MetropolisPlugin _plugin; - public MetropolisPlotGoCommand(MetropolisPlugin plugin) { - _plugin = plugin; - } + public MetropolisPlotGoCommand(MetropolisPlugin plugin) { + _plugin = plugin; + } - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - Player player = null; - Plot plot = null; - - if(sender instanceof Player){ - player = (Player) sender; - } - - if(args.length == 1 && player != null){ - plot = _plugin.getPlot(args[0]); - }else if(args.length >= 2){ - player = _plugin.getPlayer(args[1]); - plot = _plugin.getPlot(args[0]); - }else{ - return false; - } - - if(plot == null || player == null){ - return false; - } - - String errorMessage = _plugin.teleportPlayerToPlot(player, plot); - if(errorMessage != null){ - sender.sendMessage(errorMessage); - return false; - } - - return true; - } + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + Player player = null; + Plot plot = null; + if (sender instanceof Player) { + player = (Player) sender; + } + + if (args.length == 1 && player != null) { + plot = _plugin.getPlot(args[0]); + } else if (args.length >= 2) { + player = _plugin.getPlayer(args[1]); + plot = _plugin.getPlot(args[0]); + } else { + return false; + } + + if (plot == null || player == null) { + return false; + } + + String errorMessage = _plugin.teleportPlayerToPlot(player, plot); + if (errorMessage != null) { + sender.sendMessage(errorMessage); + return false; + } + + return true; + } } diff --git a/src/main/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisPlotReserveCommand.java b/src/main/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisPlotReserveCommand.java index d02f929..b7ff159 100644 --- a/src/main/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisPlotReserveCommand.java +++ b/src/main/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisPlotReserveCommand.java @@ -1,76 +1,95 @@ -package com.majinnaibu.bukkitplugins.metropolis.commands; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -import com.majinnaibu.bukkitplugins.metropolis.Cuboid; -import com.majinnaibu.bukkitplugins.metropolis.MetropolisPlugin; -import com.sk89q.worldedit.IncompleteRegionException; -import com.sk89q.worldedit.bukkit.BukkitAdapter; -import com.sk89q.worldedit.regions.Region; - -public class MetropolisPlotReserveCommand implements CommandExecutor { - MetropolisPlugin _plugin; - - public MetropolisPlotReserveCommand(MetropolisPlugin metropolisPlugin) { - _plugin = metropolisPlugin; - } - - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - int minX = 0; - int minY = 0; - int minZ = 0; - int maxX = 0; - int maxY = 0; - int maxZ = 0; - Cuboid cuboid = new Cuboid(); - - if(sender instanceof Player && args.length == 1){ - try { - Region selection = _plugin.worldEdit.getSession((Player) sender) - .getSelection(BukkitAdapter.adapt(_plugin.getWorld())); - cuboid = new Cuboid(selection); - } catch (IncompleteRegionException ex) { - sender.sendMessage("Select a WorldEdit region first."); - return false; - } - }else if(args.length == 7){ - try{ - minX = Integer.parseInt(args[1]); - minY = Integer.parseInt(args[2]); - minZ = Integer.parseInt(args[3]); - maxX = Integer.parseInt(args[4]); - maxY = Integer.parseInt(args[5]); - maxZ = Integer.parseInt(args[6]); - }catch(NumberFormatException ex){ - return false; - } - cuboid = new Cuboid(minX, minY, minZ, maxX, maxY, maxZ); - }else if(args.length == 5){ - try{ - minX = Integer.parseInt(args[1]); - minY = 0; - minZ = Integer.parseInt(args[2]); - maxX = Integer.parseInt(args[3]); - maxY = _plugin.getWorld().getMaxHeight(); - maxZ = Integer.parseInt(args[4]); - }catch(NumberFormatException ex){ - return false; - } - cuboid = new Cuboid(minX, minY, minZ, maxX, maxY, maxZ); - }else{ - return false; - } - - String regionName = args[0]; - - _plugin.reserveCuboid(regionName, cuboid); - sender.sendMessage("Reserved plot region " + regionName + "."); - - return true; - } - -} +/* +This file is part of Metropolis. + +Metropolis is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +Metropolis is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with Metropolis. If not, see . +*/ + +package com.majinnaibu.bukkitplugins.metropolis.commands; + +import com.majinnaibu.bukkitplugins.metropolis.Cuboid; +import com.majinnaibu.bukkitplugins.metropolis.MetropolisPlugin; +import com.sk89q.worldedit.IncompleteRegionException; +import com.sk89q.worldedit.bukkit.BukkitAdapter; +import com.sk89q.worldedit.regions.Region; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class MetropolisPlotReserveCommand implements CommandExecutor { + MetropolisPlugin _plugin; + + public MetropolisPlotReserveCommand(MetropolisPlugin metropolisPlugin) { + _plugin = metropolisPlugin; + } + + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + int minX = 0; + int minY = 0; + int minZ = 0; + int maxX = 0; + int maxY = 0; + int maxZ = 0; + Cuboid cuboid = new Cuboid(); + + if (sender instanceof Player && args.length == 1) { + try { + Region selection = + _plugin + .worldEdit + .getSession((Player) sender) + .getSelection(BukkitAdapter.adapt(_plugin.getWorld())); + cuboid = new Cuboid(selection); + } catch (IncompleteRegionException ex) { + sender.sendMessage("Select a WorldEdit region first."); + return false; + } + } else if (args.length == 7) { + try { + minX = Integer.parseInt(args[1]); + minY = Integer.parseInt(args[2]); + minZ = Integer.parseInt(args[3]); + maxX = Integer.parseInt(args[4]); + maxY = Integer.parseInt(args[5]); + maxZ = Integer.parseInt(args[6]); + } catch (NumberFormatException ex) { + return false; + } + cuboid = new Cuboid(minX, minY, minZ, maxX, maxY, maxZ); + } else if (args.length == 5) { + try { + minX = Integer.parseInt(args[1]); + minY = 0; + minZ = Integer.parseInt(args[2]); + maxX = Integer.parseInt(args[3]); + maxY = _plugin.getWorld().getMaxHeight(); + maxZ = Integer.parseInt(args[4]); + } catch (NumberFormatException ex) { + return false; + } + cuboid = new Cuboid(minX, minY, minZ, maxX, maxY, maxZ); + } else { + return false; + } + + String regionName = args[0]; + + _plugin.reserveCuboid(regionName, cuboid); + sender.sendMessage("Reserved plot region " + regionName + "."); + + return true; + } +} diff --git a/src/main/java/com/majinnaibu/bukkitplugins/metropolis/eventlisteners/DebugListener.java b/src/main/java/com/majinnaibu/bukkitplugins/metropolis/eventlisteners/DebugListener.java index a221cfb..0d2ad4b 100644 --- a/src/main/java/com/majinnaibu/bukkitplugins/metropolis/eventlisteners/DebugListener.java +++ b/src/main/java/com/majinnaibu/bukkitplugins/metropolis/eventlisteners/DebugListener.java @@ -1,21 +1,38 @@ -package com.majinnaibu.bukkitplugins.metropolis.eventlisteners; - -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.Listener; -import org.bukkit.event.player.PlayerInteractEvent; - -import com.majinnaibu.bukkitplugins.metropolis.MetropolisPlugin; - -public class DebugListener implements Listener { - MetropolisPlugin _plugin; - - public DebugListener(MetropolisPlugin plugin){ - _plugin = plugin; - } - - @EventHandler(priority=EventPriority.MONITOR) - public void onEvent(PlayerInteractEvent event){ - //event. - } -} +/* +This file is part of Metropolis. + +Metropolis is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +Metropolis is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with Metropolis. If not, see . +*/ + +package com.majinnaibu.bukkitplugins.metropolis.eventlisteners; + +import com.majinnaibu.bukkitplugins.metropolis.MetropolisPlugin; + +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerInteractEvent; + +public class DebugListener implements Listener { + MetropolisPlugin _plugin; + + public DebugListener(MetropolisPlugin plugin) { + _plugin = plugin; + } + + @EventHandler(priority = EventPriority.MONITOR) + public void onEvent(PlayerInteractEvent event) { + // event. + } +} diff --git a/src/main/java/com/majinnaibu/bukkitplugins/metropolis/eventlisteners/PlayerJoinListener.java b/src/main/java/com/majinnaibu/bukkitplugins/metropolis/eventlisteners/PlayerJoinListener.java index 2ecbd23..7b7cab7 100644 --- a/src/main/java/com/majinnaibu/bukkitplugins/metropolis/eventlisteners/PlayerJoinListener.java +++ b/src/main/java/com/majinnaibu/bukkitplugins/metropolis/eventlisteners/PlayerJoinListener.java @@ -1,39 +1,73 @@ -package com.majinnaibu.bukkitplugins.metropolis.eventlisteners; - -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.Listener; -import org.bukkit.event.player.PlayerJoinEvent; - -import com.majinnaibu.bukkitplugins.metropolis.Cuboid; -import com.majinnaibu.bukkitplugins.metropolis.MetropolisPlugin; -import com.majinnaibu.bukkitplugins.metropolis.PlayerHome; - -public class PlayerJoinListener implements Listener { - private MetropolisPlugin _plugin = null; - - public PlayerJoinListener(MetropolisPlugin plugin){ - _plugin = plugin; - } - - @EventHandler(priority=EventPriority.MONITOR) - public void onPlayerJoin(PlayerJoinEvent event){ - Player player = event.getPlayer(); - if(player == null){ - return; - } - - PlayerHome home = _plugin.getPlayerHome(player); - if(home==null){_plugin.getLogger().info("home is null");} - else if(home.getCuboid() == null){_plugin.getLogger().info("home.getCuboid() is null");} - else if(home.getCuboid().getVolume() == 0){_plugin.getLogger().info("home.getCuboid().getVolume() is 0");} - - if(home == null || home.getCuboid() == null || home.getCuboid().getVolume() == 0){ - _plugin.getLogger().info(String.format("Metropolis: Unable to get or create home for player %s", player.getName())); - }else{ - Cuboid cuboid = home.getCuboid(); - player.sendMessage(String.format("Metropolis: Welcome %s your home is between (%d, %d, %d) and (%d, %d, %d)", player.getName(), cuboid.getMinX(), cuboid.getMinY(), cuboid.getMinZ(), cuboid.getMaxX(), cuboid.getMaxY(), cuboid.getMaxZ())); - } - } -} +/* +This file is part of Metropolis. + +Metropolis is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +Metropolis is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with Metropolis. If not, see . +*/ + +package com.majinnaibu.bukkitplugins.metropolis.eventlisteners; + +import com.majinnaibu.bukkitplugins.metropolis.Cuboid; +import com.majinnaibu.bukkitplugins.metropolis.MetropolisPlugin; +import com.majinnaibu.bukkitplugins.metropolis.PlayerHome; + +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerJoinEvent; + +public class PlayerJoinListener implements Listener { + private MetropolisPlugin _plugin = null; + + public PlayerJoinListener(MetropolisPlugin plugin) { + _plugin = plugin; + } + + @EventHandler(priority = EventPriority.MONITOR) + public void onPlayerJoin(PlayerJoinEvent event) { + Player player = event.getPlayer(); + if (player == null) { + return; + } + + PlayerHome home = _plugin.getPlayerHome(player); + if (home == null) { + _plugin.getLogger().info("home is null"); + } else if (home.getCuboid() == null) { + _plugin.getLogger().info("home.getCuboid() is null"); + } else if (home.getCuboid().getVolume() == 0) { + _plugin.getLogger().info("home.getCuboid().getVolume() is 0"); + } + + if (home == null || home.getCuboid() == null || home.getCuboid().getVolume() == 0) { + _plugin + .getLogger() + .info( + String.format( + "Metropolis: Unable to get or create home for player %s", player.getName())); + } else { + Cuboid cuboid = home.getCuboid(); + player.sendMessage( + String.format( + "Metropolis: Welcome %s your home is between (%d, %d, %d) and (%d, %d, %d)", + player.getName(), + cuboid.getMinX(), + cuboid.getMinY(), + cuboid.getMinZ(), + cuboid.getMaxX(), + cuboid.getMaxY(), + cuboid.getMaxZ())); + } + } +} diff --git a/src/main/java/com/majinnaibu/bukkitplugins/metropolis/util/Material.java b/src/main/java/com/majinnaibu/bukkitplugins/metropolis/util/Material.java index 1608911..e4d7b7c 100644 --- a/src/main/java/com/majinnaibu/bukkitplugins/metropolis/util/Material.java +++ b/src/main/java/com/majinnaibu/bukkitplugins/metropolis/util/Material.java @@ -1,5 +1,22 @@ -package com.majinnaibu.bukkitplugins.metropolis.util; - -public class Material { - public static final int signTypeId = 63; -} +/* +This file is part of Metropolis. + +Metropolis is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +Metropolis is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with Metropolis. If not, see . +*/ + +package com.majinnaibu.bukkitplugins.metropolis.util; + +public class Material { + public static final int signTypeId = 63; +} diff --git a/src/test/java/com/majinnaibu/bukkitplugins/metropolis/CuboidTest.java b/src/test/java/com/majinnaibu/bukkitplugins/metropolis/CuboidTest.java index 76d1da0..02eb696 100644 --- a/src/test/java/com/majinnaibu/bukkitplugins/metropolis/CuboidTest.java +++ b/src/test/java/com/majinnaibu/bukkitplugins/metropolis/CuboidTest.java @@ -1,53 +1,70 @@ +/* +This file is part of Metropolis. + +Metropolis is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +Metropolis is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with Metropolis. If not, see . +*/ + package com.majinnaibu.bukkitplugins.metropolis; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; +import com.sk89q.worldedit.math.BlockVector3; + import org.bukkit.Location; import org.junit.jupiter.api.Test; -import com.sk89q.worldedit.math.BlockVector3; - class CuboidTest { - @Test - void convertsWorldEditBoundsToCuboidCoordinates() { - Cuboid cuboid = new Cuboid(BlockVector3.at(1, 2, 3), BlockVector3.at(4, 5, 6)); + @Test + void convertsWorldEditBoundsToCuboidCoordinates() { + Cuboid cuboid = new Cuboid(BlockVector3.at(1, 2, 3), BlockVector3.at(4, 5, 6)); - assertEquals(1, cuboid.getMinX()); - assertEquals(2, cuboid.getMinY()); - assertEquals(3, cuboid.getMinZ()); - assertEquals(4, cuboid.getMaxX()); - assertEquals(5, cuboid.getMaxY()); - assertEquals(6, cuboid.getMaxZ()); - } + assertEquals(1, cuboid.getMinX()); + assertEquals(2, cuboid.getMinY()); + assertEquals(3, cuboid.getMinZ()); + assertEquals(4, cuboid.getMaxX()); + assertEquals(5, cuboid.getMaxY()); + assertEquals(6, cuboid.getMaxZ()); + } - @Test - void comparesVectorsByXThenZThenYAndHandlesNulls() { - assertEquals(-1, Cuboid.compareBlockVectors(null, BlockVector3.ZERO)); - assertEquals(1, Cuboid.compareBlockVectors(BlockVector3.ZERO, null)); - assertEquals(0, Cuboid.compareBlockVectors(null, null)); - assertTrue(Cuboid.compareBlockVectors(BlockVector3.at(1, 0, 0), BlockVector3.at(2, 0, 0)) < 0); - assertTrue(Cuboid.compareBlockVectors(BlockVector3.at(1, 0, 1), BlockVector3.at(1, 0, 2)) < 0); - assertTrue(Cuboid.compareBlockVectors(BlockVector3.at(1, 1, 2), BlockVector3.at(1, 2, 2)) < 0); - } + @Test + void comparesVectorsByXThenZThenYAndHandlesNulls() { + assertEquals(-1, Cuboid.compareBlockVectors(null, BlockVector3.ZERO)); + assertEquals(1, Cuboid.compareBlockVectors(BlockVector3.ZERO, null)); + assertEquals(0, Cuboid.compareBlockVectors(null, null)); + assertTrue(Cuboid.compareBlockVectors(BlockVector3.at(1, 0, 0), BlockVector3.at(2, 0, 0)) < 0); + assertTrue(Cuboid.compareBlockVectors(BlockVector3.at(1, 0, 1), BlockVector3.at(1, 0, 2)) < 0); + assertTrue(Cuboid.compareBlockVectors(BlockVector3.at(1, 1, 2), BlockVector3.at(1, 2, 2)) < 0); + } - @Test - void treatsTouchingBoundsAsIntersecting() { - Cuboid first = new Cuboid(0, 0, 0, 10, 10, 10); - Cuboid touching = new Cuboid(10, 10, 10, 20, 20, 20); - Cuboid separate = new Cuboid(11, 0, 0, 20, 10, 10); + @Test + void treatsTouchingBoundsAsIntersecting() { + Cuboid first = new Cuboid(0, 0, 0, 10, 10, 10); + Cuboid touching = new Cuboid(10, 10, 10, 20, 20, 20); + Cuboid separate = new Cuboid(11, 0, 0, 20, 10, 10); - assertTrue(first.intersects(touching)); - assertFalse(first.intersects(separate)); - } + assertTrue(first.intersects(touching)); + assertFalse(first.intersects(separate)); + } - @Test - void containsPointsOnBoundsButRejectsPointsOutside() { - Cuboid cuboid = new Cuboid(1, 2, 3, 4, 5, 6); + @Test + void containsPointsOnBoundsButRejectsPointsOutside() { + Cuboid cuboid = new Cuboid(1, 2, 3, 4, 5, 6); - assertTrue(cuboid.contains(new Location(null, 1, 2, 3))); - assertTrue(cuboid.contains(new Location(null, 4, 5, 6))); - assertFalse(cuboid.contains(new Location(null, 5, 5, 6))); - } -} \ No newline at end of file + assertTrue(cuboid.contains(new Location(null, 1, 2, 3))); + assertTrue(cuboid.contains(new Location(null, 4, 5, 6))); + assertFalse(cuboid.contains(new Location(null, 5, 5, 6))); + } +} diff --git a/src/test/java/com/majinnaibu/bukkitplugins/metropolis/CurrentHomesStoreTest.java b/src/test/java/com/majinnaibu/bukkitplugins/metropolis/CurrentHomesStoreTest.java index 001ac5c..f1780ac 100644 --- a/src/test/java/com/majinnaibu/bukkitplugins/metropolis/CurrentHomesStoreTest.java +++ b/src/test/java/com/majinnaibu/bukkitplugins/metropolis/CurrentHomesStoreTest.java @@ -1,3 +1,20 @@ +/* +This file is part of Metropolis. + +Metropolis is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +Metropolis is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with Metropolis. If not, see . +*/ + package com.majinnaibu.bukkitplugins.metropolis; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -11,31 +28,34 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; class CurrentHomesStoreTest { - @TempDir - File temporaryDirectory; + @TempDir File temporaryDirectory; - @Test - void roundTripsHomesByUuid() throws Exception { - File file = new File(temporaryDirectory, "currentHomes.yml"); - UUID playerId = UUID.fromString("123e4567-e89b-12d3-a456-426614174000"); - Map expected = Map.of(playerId, 3); + @Test + void roundTripsHomesByUuid() throws Exception { + File file = new File(temporaryDirectory, "currentHomes.yml"); + UUID playerId = UUID.fromString("123e4567-e89b-12d3-a456-426614174000"); + Map expected = Map.of(playerId, 3); - CurrentHomesStore.save(file, expected); + CurrentHomesStore.save(file, expected); - assertEquals(expected, CurrentHomesStore.load(file, ignored -> null)); - } + assertEquals(expected, CurrentHomesStore.load(file, ignored -> null)); + } - @Test - void resolvesLegacyPlayerNameKeys() throws Exception { - File file = new File(temporaryDirectory, "currentHomes.yml"); - UUID playerId = UUID.fromString("123e4567-e89b-12d3-a456-426614174000"); - YamlConfiguration yaml = new YamlConfiguration(); - yaml.set("OldPlayerName", 2); - yaml.save(file); + @Test + void resolvesLegacyPlayerNameKeys() throws Exception { + File file = new File(temporaryDirectory, "currentHomes.yml"); + UUID playerId = UUID.fromString("123e4567-e89b-12d3-a456-426614174000"); + YamlConfiguration yaml = new YamlConfiguration(); + yaml.set("OldPlayerName", 2); + yaml.save(file); - assertEquals(Map.of(playerId, 2), CurrentHomesStore.load(file, name -> { - assertEquals("OldPlayerName", name); - return playerId; - })); - } -} \ No newline at end of file + assertEquals( + Map.of(playerId, 2), + CurrentHomesStore.load( + file, + name -> { + assertEquals("OldPlayerName", name); + return playerId; + })); + } +} diff --git a/src/test/java/com/majinnaibu/bukkitplugins/metropolis/PlayerHomeTest.java b/src/test/java/com/majinnaibu/bukkitplugins/metropolis/PlayerHomeTest.java index bf63338..eafd7ab 100644 --- a/src/test/java/com/majinnaibu/bukkitplugins/metropolis/PlayerHomeTest.java +++ b/src/test/java/com/majinnaibu/bukkitplugins/metropolis/PlayerHomeTest.java @@ -1,27 +1,41 @@ +/* +This file is part of Metropolis. + +Metropolis is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +Metropolis is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with Metropolis. If not, see . +*/ + package com.majinnaibu.bukkitplugins.metropolis; import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.UUID; -import org.junit.jupiter.api.Test; - import com.sk89q.worldedit.math.BlockVector3; -class PlayerHomeTest { - @Test - void buildsUuidBasedRegionIdentityAndPreservesDisplayName() { - UUID playerId = UUID.fromString("123e4567-e89b-12d3-a456-426614174000"); - PlayerHome home = new PlayerHome( - playerId, - "PlayerName", - 2, - BlockVector3.at(1, 2, 3), - BlockVector3.at(4, 5, 6)); +import org.junit.jupiter.api.Test; - assertEquals(playerId, home.getPlayerId()); - assertEquals("PlayerName", home.getPlayerName()); - assertEquals(2, home.getNumber()); - assertEquals("h_2_" + playerId, home.getRegionName()); - } -} \ No newline at end of file +class PlayerHomeTest { + @Test + void buildsUuidBasedRegionIdentityAndPreservesDisplayName() { + UUID playerId = UUID.fromString("123e4567-e89b-12d3-a456-426614174000"); + PlayerHome home = + new PlayerHome( + playerId, "PlayerName", 2, BlockVector3.at(1, 2, 3), BlockVector3.at(4, 5, 6)); + + assertEquals(playerId, home.getPlayerId()); + assertEquals("PlayerName", home.getPlayerName()); + assertEquals(2, home.getNumber()); + assertEquals("h_2_" + playerId, home.getRegionName()); + } +} diff --git a/src/test/java/com/majinnaibu/bukkitplugins/metropolis/PlayerLookupTest.java b/src/test/java/com/majinnaibu/bukkitplugins/metropolis/PlayerLookupTest.java index 7964faf..78ca431 100644 --- a/src/test/java/com/majinnaibu/bukkitplugins/metropolis/PlayerLookupTest.java +++ b/src/test/java/com/majinnaibu/bukkitplugins/metropolis/PlayerLookupTest.java @@ -1,3 +1,20 @@ +/* +This file is part of Metropolis. + +Metropolis is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +Metropolis is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with Metropolis. If not, see . +*/ + package com.majinnaibu.bukkitplugins.metropolis; import static org.junit.jupiter.api.Assertions.assertNull; @@ -11,12 +28,12 @@ import org.bukkit.entity.Player; import org.junit.jupiter.api.Test; class PlayerLookupTest { - @Test - void matchesOnlinePlayerNamesCaseInsensitivelyButNotByPrefix() { - Player player = mock(Player.class); - when(player.getName()).thenReturn("PlayerName"); + @Test + void matchesOnlinePlayerNamesCaseInsensitivelyButNotByPrefix() { + Player player = mock(Player.class); + when(player.getName()).thenReturn("PlayerName"); - assertSame(player, PlayerLookup.findOnline("playername", List.of(player))); - assertNull(PlayerLookup.findOnline("Player", List.of(player))); - } -} \ No newline at end of file + assertSame(player, PlayerLookup.findOnline("playername", List.of(player))); + assertNull(PlayerLookup.findOnline("Player", List.of(player))); + } +} diff --git a/src/test/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisHomeAcquireTest.java b/src/test/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisHomeAcquireTest.java index 778a5dc..d953ede 100644 --- a/src/test/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisHomeAcquireTest.java +++ b/src/test/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisHomeAcquireTest.java @@ -1,7 +1,24 @@ +/* +This file is part of Metropolis. + +Metropolis is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +Metropolis is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with Metropolis. If not, see . +*/ + package com.majinnaibu.bukkitplugins.metropolis.commands; -import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; @@ -9,40 +26,40 @@ import static org.mockito.Mockito.when; import java.util.UUID; +import com.majinnaibu.bukkitplugins.metropolis.MetropolisPlugin; + import org.bukkit.command.Command; import org.bukkit.entity.Player; import org.junit.jupiter.api.Test; -import com.majinnaibu.bukkitplugins.metropolis.MetropolisPlugin; - class MetropolisHomeAcquireTest { - @Test - void assignsAPlotWhenThePlayerIsBelowTheirLimit() { - MetropolisPlugin plugin = mock(MetropolisPlugin.class); - Player player = mock(Player.class); - UUID playerId = UUID.fromString("123e4567-e89b-12d3-a456-426614174000"); - when(player.getUniqueId()).thenReturn(playerId); - when(plugin.getNumPlots(playerId)).thenReturn(0); - when(plugin.getMaxPlots(playerId)).thenReturn(1); - MetropolisHomeAcquire executor = new MetropolisHomeAcquire(plugin); + @Test + void assignsAPlotWhenThePlayerIsBelowTheirLimit() { + MetropolisPlugin plugin = mock(MetropolisPlugin.class); + Player player = mock(Player.class); + UUID playerId = UUID.fromString("123e4567-e89b-12d3-a456-426614174000"); + when(player.getUniqueId()).thenReturn(playerId); + when(plugin.getNumPlots(playerId)).thenReturn(0); + when(plugin.getMaxPlots(playerId)).thenReturn(1); + MetropolisHomeAcquire executor = new MetropolisHomeAcquire(plugin); - assertTrue(executor.onCommand(player, mock(Command.class), "acquire", new String[0])); + assertTrue(executor.onCommand(player, mock(Command.class), "acquire", new String[0])); - verify(plugin).assignPlot(player); - } + verify(plugin).assignPlot(player); + } - @Test - void doesNotAssignAPlotAfterThePlayerReachesTheirLimit() { - MetropolisPlugin plugin = mock(MetropolisPlugin.class); - Player player = mock(Player.class); - UUID playerId = UUID.fromString("123e4567-e89b-12d3-a456-426614174000"); - when(player.getUniqueId()).thenReturn(playerId); - when(plugin.getNumPlots(playerId)).thenReturn(1); - when(plugin.getMaxPlots(playerId)).thenReturn(1); - MetropolisHomeAcquire executor = new MetropolisHomeAcquire(plugin); + @Test + void doesNotAssignAPlotAfterThePlayerReachesTheirLimit() { + MetropolisPlugin plugin = mock(MetropolisPlugin.class); + Player player = mock(Player.class); + UUID playerId = UUID.fromString("123e4567-e89b-12d3-a456-426614174000"); + when(player.getUniqueId()).thenReturn(playerId); + when(plugin.getNumPlots(playerId)).thenReturn(1); + when(plugin.getMaxPlots(playerId)).thenReturn(1); + MetropolisHomeAcquire executor = new MetropolisHomeAcquire(plugin); - assertFalse(executor.onCommand(player, mock(Command.class), "acquire", new String[0])); + assertFalse(executor.onCommand(player, mock(Command.class), "acquire", new String[0])); - verify(plugin, never()).assignPlot(player); - } -} \ No newline at end of file + verify(plugin, never()).assignPlot(player); + } +} diff --git a/src/test/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisPlotReserveCommandTest.java b/src/test/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisPlotReserveCommandTest.java index 15ed429..779d95c 100644 --- a/src/test/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisPlotReserveCommandTest.java +++ b/src/test/java/com/majinnaibu/bukkitplugins/metropolis/commands/MetropolisPlotReserveCommandTest.java @@ -1,3 +1,20 @@ +/* +This file is part of Metropolis. + +Metropolis is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +Metropolis is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with Metropolis. If not, see . +*/ + package com.majinnaibu.bukkitplugins.metropolis.commands; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -8,42 +25,46 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoInteractions; +import com.majinnaibu.bukkitplugins.metropolis.Cuboid; +import com.majinnaibu.bukkitplugins.metropolis.MetropolisPlugin; + import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.junit.jupiter.api.Test; import org.mockito.ArgumentCaptor; -import com.majinnaibu.bukkitplugins.metropolis.Cuboid; -import com.majinnaibu.bukkitplugins.metropolis.MetropolisPlugin; - class MetropolisPlotReserveCommandTest { - @Test - void reservesCoordinatesFromAllSixArguments() { - MetropolisPlugin plugin = mock(MetropolisPlugin.class); - CommandSender sender = mock(CommandSender.class); - Command command = mock(Command.class); - MetropolisPlotReserveCommand executor = new MetropolisPlotReserveCommand(plugin); + @Test + void reservesCoordinatesFromAllSixArguments() { + MetropolisPlugin plugin = mock(MetropolisPlugin.class); + CommandSender sender = mock(CommandSender.class); + Command command = mock(Command.class); + MetropolisPlotReserveCommand executor = new MetropolisPlotReserveCommand(plugin); - assertTrue(executor.onCommand(sender, command, "reserve", new String[] {"north", "1", "2", "3", "4", "5", "6"})); + assertTrue( + executor.onCommand( + sender, command, "reserve", new String[] {"north", "1", "2", "3", "4", "5", "6"})); - ArgumentCaptor cuboid = ArgumentCaptor.forClass(Cuboid.class); - verify(plugin).reserveCuboid(eq("north"), cuboid.capture()); - assertEquals(1, cuboid.getValue().getMinX()); - assertEquals(2, cuboid.getValue().getMinY()); - assertEquals(3, cuboid.getValue().getMinZ()); - assertEquals(4, cuboid.getValue().getMaxX()); - assertEquals(5, cuboid.getValue().getMaxY()); - assertEquals(6, cuboid.getValue().getMaxZ()); - } + ArgumentCaptor cuboid = ArgumentCaptor.forClass(Cuboid.class); + verify(plugin).reserveCuboid(eq("north"), cuboid.capture()); + assertEquals(1, cuboid.getValue().getMinX()); + assertEquals(2, cuboid.getValue().getMinY()); + assertEquals(3, cuboid.getValue().getMinZ()); + assertEquals(4, cuboid.getValue().getMaxX()); + assertEquals(5, cuboid.getValue().getMaxY()); + assertEquals(6, cuboid.getValue().getMaxZ()); + } - @Test - void rejectsMalformedCoordinateArgumentsWithoutCallingPlugin() { - MetropolisPlugin plugin = mock(MetropolisPlugin.class); - CommandSender sender = mock(CommandSender.class); - Command command = mock(Command.class); - MetropolisPlotReserveCommand executor = new MetropolisPlotReserveCommand(plugin); + @Test + void rejectsMalformedCoordinateArgumentsWithoutCallingPlugin() { + MetropolisPlugin plugin = mock(MetropolisPlugin.class); + CommandSender sender = mock(CommandSender.class); + Command command = mock(Command.class); + MetropolisPlotReserveCommand executor = new MetropolisPlotReserveCommand(plugin); - assertFalse(executor.onCommand(sender, command, "reserve", new String[] {"north", "1", "two", "3", "4", "5", "6"})); - verifyNoInteractions(plugin); - } -} \ No newline at end of file + assertFalse( + executor.onCommand( + sender, command, "reserve", new String[] {"north", "1", "two", "3", "4", "5", "6"})); + verifyNoInteractions(plugin); + } +} diff --git a/src/test/java/com/majinnaibu/bukkitplugins/metropolis/eventlisteners/PlayerJoinListenerTest.java b/src/test/java/com/majinnaibu/bukkitplugins/metropolis/eventlisteners/PlayerJoinListenerTest.java index b202091..d2dc893 100644 --- a/src/test/java/com/majinnaibu/bukkitplugins/metropolis/eventlisteners/PlayerJoinListenerTest.java +++ b/src/test/java/com/majinnaibu/bukkitplugins/metropolis/eventlisteners/PlayerJoinListenerTest.java @@ -1,3 +1,20 @@ +/* +This file is part of Metropolis. + +Metropolis is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +Metropolis is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with Metropolis. If not, see . +*/ + package com.majinnaibu.bukkitplugins.metropolis.eventlisteners; import static org.mockito.ArgumentMatchers.contains; @@ -7,31 +24,33 @@ import static org.mockito.Mockito.when; import java.util.UUID; -import org.bukkit.entity.Player; -import org.bukkit.event.player.PlayerJoinEvent; -import org.junit.jupiter.api.Test; - import com.majinnaibu.bukkitplugins.metropolis.MetropolisPlugin; import com.majinnaibu.bukkitplugins.metropolis.PlayerHome; import com.sk89q.worldedit.math.BlockVector3; +import org.bukkit.entity.Player; +import org.bukkit.event.player.PlayerJoinEvent; +import org.junit.jupiter.api.Test; + class PlayerJoinListenerTest { - @Test - void tellsJoiningPlayerTheirHomeBounds() { - MetropolisPlugin plugin = mock(MetropolisPlugin.class); - Player player = mock(Player.class); - PlayerJoinEvent event = mock(PlayerJoinEvent.class); - when(event.getPlayer()).thenReturn(player); - when(player.getName()).thenReturn("PlayerName"); - when(plugin.getPlayerHome(player)).thenReturn(new PlayerHome( - UUID.fromString("123e4567-e89b-12d3-a456-426614174000"), - "PlayerName", - 1, - BlockVector3.at(1, 2, 3), - BlockVector3.at(4, 5, 6))); + @Test + void tellsJoiningPlayerTheirHomeBounds() { + MetropolisPlugin plugin = mock(MetropolisPlugin.class); + Player player = mock(Player.class); + PlayerJoinEvent event = mock(PlayerJoinEvent.class); + when(event.getPlayer()).thenReturn(player); + when(player.getName()).thenReturn("PlayerName"); + when(plugin.getPlayerHome(player)) + .thenReturn( + new PlayerHome( + UUID.fromString("123e4567-e89b-12d3-a456-426614174000"), + "PlayerName", + 1, + BlockVector3.at(1, 2, 3), + BlockVector3.at(4, 5, 6))); - new PlayerJoinListener(plugin).onPlayerJoin(event); + new PlayerJoinListener(plugin).onPlayerJoin(event); - verify(player).sendMessage(contains("home is between (1, 2, 3) and (4, 5, 6)")); - } -} \ No newline at end of file + verify(player).sendMessage(contains("home is between (1, 2, 3) and (4, 5, 6)")); + } +}