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 all commits
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 @@ -38,6 +38,9 @@ public JVillageCMD(JVillage plugin) {
registerCommand(new JDemoteCommand(plugin), "demote");
registerCommand(new JPromoteCommand(plugin), "promote");
registerCommand(new JSetSpawnCommand(plugin), "setspawn");
registerCommand(new JWarpCommand(plugin), "warp");
registerCommand(new JSetWarpCommand(plugin), "setwarp");
registerCommand(new JDelWarpCommand(plugin), "delwarp");
registerCommand(new JRenameCommand(plugin), "rename");
registerCommand(new JListCommand(plugin), "list");
registerCommand(new JFlagCommand(plugin), "flag", "flags", "f");
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 @@ -60,13 +60,15 @@ public boolean onCommand(CommandSender commandSender, Command command, String s,

//If string doesn't only contain numbers and letters
if (!villageName.matches("[a-zA-Z0-9]+")) {
commandSender.sendMessage(language.getMessage("command_village_create_invalid_name"));
commandSender.sendMessage(language.getMessage("command_village_create_invalid_name")
.replace("%max%", settings.getConfigInteger("settings.town.max-name-length.value").toString()));
return true;
}

//If string is too long
if (villageName.length() > settings.getConfigInteger("settings.town.max-name-length.value")) {
commandSender.sendMessage(language.getMessage("command_village_create_invalid_name"));
commandSender.sendMessage(language.getMessage("command_village_create_invalid_name")
.replace("%max%", settings.getConfigInteger("settings.town.max-name-length.value").toString()));
return true;
}

Expand Down Expand Up @@ -127,7 +129,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
@@ -0,0 +1,68 @@
package com.johnymuffin.jvillage.beta.commands.village;

import com.johnymuffin.jvillage.beta.JVillage;
import com.johnymuffin.jvillage.beta.player.VPlayer;
import com.johnymuffin.jvillage.beta.models.Village;
import com.johnymuffin.jvillage.beta.commands.JVBaseCommand;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class JDelWarpCommand extends JVBaseCommand implements CommandExecutor {

public JDelWarpCommand(JVillage plugin) {
super(plugin);
}

@Override
public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) {
if (!isAuthorized(commandSender, "jvillage.player.delwarp")) {
commandSender.sendMessage(language.getMessage("no_permission"));
return true;
}

if (!(commandSender instanceof Player)) {
commandSender.sendMessage(language.getMessage("unavailable_to_console"));
return true;
}

if (strings.length < 1) {
commandSender.sendMessage(language.getMessage("command_village_delwarp_use"));
return true;
}

Player player = (Player) commandSender;
VPlayer vPlayer = plugin.getPlayerMap().getPlayer(player.getUniqueId());
Village village = vPlayer.getSelectedVillage();

if (village == null) {
commandSender.sendMessage(language.getMessage("no_village_selected"));
return true;
}

if (!village.isOwner(vPlayer.getUUID()) && !village.isAssistant(vPlayer.getUUID())) {
String message = language.getMessage("command_village_delwarp_no_permission");
message = message.replace("%village%", village.getTownName());
commandSender.sendMessage(message);
return true;
}

String warpName = strings[0];

if (!village.getWarps().containsKey(warpName)) {
commandSender.sendMessage(language.getMessage("command_village_delwarp_not_found")
.replace("%warp%", warpName)
.replace("%village%", village.getTownName()));
return true;
}

village.removeWarp(warpName);
village.broadcastToTown(language.getMessage("command_village_delwarp_del_broadcast")
.replace("%player%", player.getName())
.replace("%warp%", warpName)
);

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ public boolean onCommand(CommandSender commandSender, Command command, String s,
target.setSelectedVillage(null);
}
village.removeMember(uuid);
commandSender.sendMessage(language.getMessage("command_village_kick_success"));
commandSender.sendMessage(language.getMessage("command_village_kick_success")
.replace("%player%", target.getUsername())
.replace("%village%", village.getTownName()));

//Message the player if they are online
Player targetPlayer = getPlayerFromUUID(uuid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,14 @@ public boolean onCommand(CommandSender commandSender, Command command, String s,
String villageName = strings[0];

if (!villageName.matches("[a-zA-Z0-9]+")) {
commandSender.sendMessage(language.getMessage("command_village_rename_invalid_name"));
commandSender.sendMessage(language.getMessage("command_village_rename_invalid_name")
.replace("%max%", settings.getConfigInteger("settings.town.max-name-length.value").toString()));
return true;
}

if (villageName.length() > settings.getConfigInteger("settings.town.max-name-length.value")) {
commandSender.sendMessage(language.getMessage("command_village_rename_invalid_name"));
commandSender.sendMessage(language.getMessage("command_village_rename_invalid_name")
.replace("%max%", settings.getConfigInteger("settings.town.max-name-length.value").toString()));
return true;
}

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 All @@ -25,6 +25,11 @@ public boolean onCommand(CommandSender commandSender, Command command, String s,
return true;
}

if (!(commandSender instanceof Player)) {
commandSender.sendMessage(language.getMessage("unavailable_to_console"));
return true;
}

Player player = (Player) commandSender;
VPlayer vPlayer = plugin.getPlayerMap().getPlayer(player.getUniqueId());
Village village = vPlayer.getSelectedVillage();
Expand All @@ -49,9 +54,13 @@ 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());
village.broadcastToTown(language.getMessage("command_village_setspawn_set_broadcast")
.replace("%player%", player.getName())
.replace("%cords%", cords.toString())
);

return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package com.johnymuffin.jvillage.beta.commands.village;

import com.johnymuffin.beta.fundamentals.api.EconomyAPI;
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.VSpawnCords;
import com.johnymuffin.jvillage.beta.models.Village;
import com.johnymuffin.jvillage.beta.models.chunk.VChunk;
import com.johnymuffin.jvillage.beta.player.VPlayer;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class JSetWarpCommand extends JVBaseCommand implements CommandExecutor {

public JSetWarpCommand(JVillage plugin) {
super(plugin);
}

@Override
public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) {
if (!isAuthorized(commandSender, "jvillage.player.setwarp")) {
commandSender.sendMessage(language.getMessage("no_permission"));
return true;
}

if (!(commandSender instanceof Player)) {
commandSender.sendMessage(language.getMessage("unavailable_to_console"));
return true;
}

if (strings.length < 1) {
commandSender.sendMessage(language.getMessage("command_village_setwarp_use"));
return true;
}

Player player = (Player) commandSender;
VPlayer vPlayer = plugin.getPlayerMap().getPlayer(player.getUniqueId());
Village village = vPlayer.getSelectedVillage();

if (village == null) {
commandSender.sendMessage(language.getMessage("no_village_selected"));
return true;
}

if (!village.isOwner(vPlayer.getUUID()) && !village.isAssistant(vPlayer.getUUID())) {
String message = language.getMessage("command_village_setwarp_no_permission");
message = message.replace("%village%", village.getTownName());
commandSender.sendMessage(message);
return true;
}

String warpName = strings[0];

if (!warpName.matches("[a-zA-Z0-9]+")) {
commandSender.sendMessage(language.getMessage("command_village_setwarp_invalid_name")
.replace("%max%", settings.getConfigInteger("settings.warp.max-name-length.value").toString()));
return true;
}

if (warpName.length() > settings.getConfigInteger("settings.warp.max-name-length.value")) {
commandSender.sendMessage(language.getMessage("command_village_setwarp_invalid_name")
.replace("%max%", settings.getConfigInteger("settings.warp.max-name-length.value").toString()));
return true;
}

if (village.getWarps().containsKey(warpName)) {
commandSender.sendMessage(language.getMessage("command_village_setwarp_already_exists"));
return true;
}

VChunk vChunk = new VChunk(player.getLocation());
if (!village.getClaims().contains(vChunk)) {
String message = language.getMessage("command_village_setwarp_not_in_village");
message = message.replace("%village%", village.getTownName());
commandSender.sendMessage(message);
return true;
}

double creationCost = settings.getConfigDouble("settings.warp.price.amount");
if (creationCost > 0 && plugin.isFundamentalsEnabled()) {
EconomyAPI.EconomyResult result = FundamentalsAPI.getEconomy().subtractBalance(player.getUniqueId(), creationCost, player.getWorld().getName());
String message;
switch (result) {
case successful:
commandSender.sendMessage(language.getMessage("command_village_setwarp_payment")
.replace("%amount%", settings.getConfigDouble("settings.warp.price.amount").toString())
.replace("%warp%", warpName));
break;
case notEnoughFunds:
commandSender.sendMessage(language.getMessage("command_village_setwarp_insufficient_funds")
.replace("%cost%", String.valueOf(creationCost)));
return true;
default:
message = language.getMessage("unknown_economy_error");
commandSender.sendMessage(message);
return true;
}
}
VSpawnCords cords = new VSpawnCords(player.getLocation());
village.addWarp(warpName, cords);
village.broadcastToTown(language.getMessage("command_village_setwarp_set_broadcast")
.replace("%player%", player.getName())
.replace("%warp%", warpName));
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public boolean onCommand(CommandSender commandSender, Command command, String s,
commandSender.sendMessage(message);
return true;
} catch (Exception e) {
String message = language.getMessage("command_village_spawm_unsafe");
String message = language.getMessage("command_village_spawn_unsafe");
message = message.replace("%village%", village.getTownName());
commandSender.sendMessage(message);
return true;
Expand Down
Loading
Loading