Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Village spawn yaw + Village warps #5

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/main/java/com/johnymuffin/jvillage/beta/JVUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
Expand Down Expand Up @@ -72,6 +73,23 @@ public static double round(double value, int places) {
return bd.doubleValue();
}

public static int closestYaw(double yaw){
if(yaw < 0) yaw = 360 + yaw;
Set<Integer> yaws = new HashSet<>(Arrays.asList(0, 90, 180, 270, 360));
int closest = -1;
double lowestDiff = Integer.MAX_VALUE;

for(Integer entry : yaws){
double diff = Math.abs(yaw - entry);
if(diff < lowestDiff){
closest = entry;
lowestDiff = diff;
}
}
if(closest == 360) return 0;
return closest;
}

public static String formatVillageList(Village[] villages) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < villages.length; i++) {
Expand Down
15 changes: 8 additions & 7 deletions src/main/java/com/johnymuffin/jvillage/beta/JVillage.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.johnymuffin.jvillage.beta.maps.JPlayerMap;
import com.johnymuffin.jvillage.beta.maps.JVillageMap;
import com.johnymuffin.jvillage.beta.models.VCords;
import com.johnymuffin.jvillage.beta.models.VSpawnCords;
import com.johnymuffin.jvillage.beta.models.Village;
import com.johnymuffin.jvillage.beta.models.chunk.VChunk;
import com.johnymuffin.jvillage.beta.models.chunk.VClaim;
Expand Down Expand Up @@ -584,13 +585,13 @@ public boolean townyImport() {
}

//Misc
VCords townSpawn = null;
VSpawnCords townSpawn = null;
try {
townSpawn = new VCords(town.getSpawn().getBlockX(), town.getSpawn().getBlockY(), town.getSpawn().getBlockZ(), town.getSpawn().getWorld().getName());
townSpawn = new VSpawnCords(town.getSpawn().getBlockX(), town.getSpawn().getBlockY(), town.getSpawn().getBlockZ(), 0, town.getSpawn().getWorld().getName());
} catch (Exception exception) {
log.warning("[" + pluginName + "] Could not find town spawn for town " + newTownName + ". World spawn will be used instead.");
logger(Level.WARNING, "Could not find town spawn for town " + newTownName + ". The town spawn will be set to the world spawn.");
townSpawn = new VCords(Bukkit.getWorlds().get(0).getSpawnLocation().getBlockX(), Bukkit.getWorlds().get(0).getSpawnLocation().getBlockY(), Bukkit.getWorlds().get(0).getSpawnLocation().getBlockZ(), Bukkit.getWorlds().get(0).getName());
townSpawn = new VSpawnCords(Bukkit.getWorlds().get(0).getSpawnLocation().getBlockX(), Bukkit.getWorlds().get(0).getSpawnLocation().getBlockY(), Bukkit.getWorlds().get(0).getSpawnLocation().getBlockZ(), 0, Bukkit.getWorlds().get(0).getName());
}

//First chunk from townSpawn
Expand Down Expand Up @@ -801,15 +802,15 @@ public boolean factionsImport() {

//Faction Spawn
Location factionSpawn = faction.getHome();
VCords villageSpawn = null;
VSpawnCords villageSpawn = null;
VChunk spawnChunk = null;
if(factionSpawn == null) {
log.warning("[" + pluginName + "] Faction " + newVillageName + " has no spawn. The world spawn will be used instead.");
spawnChunk = new VChunk(Bukkit.getWorlds().get(0).getSpawnLocation());
villageSpawn = new VCords(Bukkit.getWorlds().get(0).getSpawnLocation());
villageSpawn = new VSpawnCords(Bukkit.getWorlds().get(0).getSpawnLocation());
} else {
spawnChunk = new VChunk(factionSpawn);
villageSpawn = new VCords(factionSpawn);
villageSpawn = new VSpawnCords(factionSpawn);
}


Expand Down Expand Up @@ -902,7 +903,7 @@ public JPlayerData getPlayerData() {
return this.playerData;
}

public Village generateNewVillage(String townName, UUID owner, VChunk vChunk, VCords townSpawn) {
public Village generateNewVillage(String townName, UUID owner, VChunk vChunk, VSpawnCords townSpawn) {
if (!villageNameAvailable(townName)) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.johnymuffin.beta.fundamentals.api.FundamentalsAPI;
import com.johnymuffin.jvillage.beta.JVillage;
import com.johnymuffin.jvillage.beta.commands.JVBaseCommand;
import com.johnymuffin.jvillage.beta.models.VCords;
import com.johnymuffin.jvillage.beta.models.VSpawnCords;
import com.johnymuffin.jvillage.beta.models.Village;
import com.johnymuffin.jvillage.beta.models.chunk.ChunkClaimSettings;
import com.johnymuffin.jvillage.beta.models.chunk.VChunk;
Expand Down Expand Up @@ -127,7 +127,7 @@ public boolean onCommand(CommandSender commandSender, Command command, String s,
}
}

Village newVillage = new Village(plugin, villageName, UUID.randomUUID(), player.getUniqueId(), vChunk, new VCords(player.getLocation()));
Village newVillage = new Village(plugin, villageName, UUID.randomUUID(), player.getUniqueId(), vChunk, new VSpawnCords(player.getLocation()));
plugin.getVillageMap().addVillageToMap(newVillage);

//Metadata for first chunk
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.johnymuffin.jvillage.beta.JVillage;
import com.johnymuffin.jvillage.beta.commands.JVBaseCommand;
import com.johnymuffin.jvillage.beta.models.VCords;
import com.johnymuffin.jvillage.beta.models.VSpawnCords;
import com.johnymuffin.jvillage.beta.models.Village;
import com.johnymuffin.jvillage.beta.models.chunk.VChunk;
import com.johnymuffin.jvillage.beta.player.VPlayer;
Expand Down Expand Up @@ -49,7 +49,7 @@ public boolean onCommand(CommandSender commandSender, Command command, String s,
return true;
}

VCords cords = new VCords(player.getLocation());
VSpawnCords cords = new VSpawnCords(player.getLocation());
village.setTownSpawn(cords);
village.broadcastToTown(player.getDisplayName() + " has set the spawn point to " + cords.toString());
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.johnymuffin.jvillage.beta.models;

import com.johnymuffin.jvillage.beta.JVUtility;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.json.simple.JSONObject;

public class VSpawnCords extends VCords{

private int yaw;

public VSpawnCords(int x, int y, int z, int yaw, String worldName){
super(x, y, z, worldName);
this.yaw = yaw;
}

public VSpawnCords(Location location){
this(location.getBlockX(), location.getBlockY(), location.getBlockZ(), JVUtility.closestYaw(location.getYaw()), location.getWorld().getName());
}

public VSpawnCords(JSONObject jsonObject){
super(Long.valueOf(String.valueOf(jsonObject.get("x"))).intValue(),
Long.valueOf(String.valueOf(jsonObject.get("y"))).intValue(),
Long.valueOf(String.valueOf(jsonObject.get("z"))).intValue(),
(String) jsonObject.get("world"));
if(jsonObject.get("yaw") == null){
yaw = 0;
}else{
yaw = Long.valueOf(String.valueOf(jsonObject.get("yaw"))).intValue();
}
}

@Override
public JSONObject getJsonObject(){
JSONObject jsonObject = new JSONObject();
jsonObject.put("x", super.getX());
jsonObject.put("y", super.getY());
jsonObject.put("z", super.getZ());
jsonObject.put("yaw", yaw);
jsonObject.put("world", super.getWorldName());
return jsonObject;
}

public int getYaw(){
return yaw;
}

@Override
public Location getLocation() {
return new Location(Bukkit.getWorld(super.getWorldName()), super.getX(), super.getY(), super.getZ(), yaw, 0);
}
}
10 changes: 5 additions & 5 deletions src/main/java/com/johnymuffin/jvillage/beta/models/Village.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class Village implements ClaimManager {
private final ArrayList<UUID> members = new ArrayList<UUID>();
private final ArrayList<UUID> assistants = new ArrayList<UUID>();
private UUID owner;
private VCords townSpawn;
private VSpawnCords townSpawn;

private boolean modified = false;

Expand All @@ -42,7 +42,7 @@ private void initializeFlags() {
}
}

public Village(JVillage plugin, String townName, UUID townUUID, UUID owner, VChunk vChunk, VCords townSpawn) {
public Village(JVillage plugin, String townName, UUID townUUID, UUID owner, VChunk vChunk, VSpawnCords townSpawn) {
this.plugin = plugin;
this.townName = townName;
this.townUUID = townUUID;
Expand All @@ -60,7 +60,7 @@ public Village(JVillage plugin, UUID uuid, JSONObject object) {
this.townName = String.valueOf(object.get("name"));
this.townUUID = uuid; // Ignore UUID in JSON file and use the one from the file name
this.owner = UUID.fromString(String.valueOf(object.get("owner")));
this.townSpawn = new VCords((JSONObject) object.get("townSpawn"));
this.townSpawn = new VSpawnCords((JSONObject) object.get("townSpawn"));
JSONArray members = (JSONArray) object.get("members");
for (Object member : members) {
this.members.add(UUID.fromString(String.valueOf(member)));
Expand Down Expand Up @@ -372,11 +372,11 @@ public void setOwner(UUID uuid) {
// return claims;
// }

public VCords getTownSpawn() {
public VSpawnCords getTownSpawn() {
return townSpawn;
}

public void setTownSpawn(VCords cords) {
public void setTownSpawn(VSpawnCords cords) {
modified = true; // Indicate that the village has been modified and needs to be saved
townSpawn = cords;
}
Expand Down