diff --git a/src/me/EtienneDx/RealEstate/GP_RealEstateHook.java b/src/me/EtienneDx/RealEstate/GP_RealEstateHook.java index 874af49..51bb06a 100644 --- a/src/me/EtienneDx/RealEstate/GP_RealEstateHook.java +++ b/src/me/EtienneDx/RealEstate/GP_RealEstateHook.java @@ -1,78 +1,78 @@ -package me.EtienneDx.RealEstate; - -import org.bukkit.Material; -import org.bukkit.entity.Player; - -import me.EtienneDx.RealEstate.Transactions.BoughtTransaction; -import me.EtienneDx.RealEstate.Transactions.Transaction; -import me.ryanhamshire.GriefPrevention.Claim; -import me.ryanhamshire.GriefPrevention.IRealEstate; - -public class GP_RealEstateHook implements IRealEstate -{ - @Override - public String allowEdit(Claim claim, Player player) - { - Transaction b = RealEstate.transactionsStore.getTransaction(claim); - if(b != null && b.getOwner().equals(player.getUniqueId()) && b instanceof BoughtTransaction) - { - if(((BoughtTransaction)b).getBuyer() != null) - return "This claim is currently involved in a transaction, you can't edit it!"; - } - return null; - } - - @Override - public String allowBuild(Claim claim, Player player, Material material) - { - Transaction b = RealEstate.transactionsStore.getTransaction(claim); - if(b != null && b.getOwner().equals(player.getUniqueId()) && b instanceof BoughtTransaction) - { - if(((BoughtTransaction)b).getBuyer() != null) - return "This claim is currently involved in a transaction, you can't build on it!"; - } - return null; - } - - @Override - public String allowAccess(Claim claim, Player player) - { - Transaction b = RealEstate.transactionsStore.getTransaction(claim); - if(b != null && b.getOwner().equals(player.getUniqueId()) && b instanceof BoughtTransaction) - { - if(((BoughtTransaction)b).getBuyer() != null) - return "This claim is currently involved in a transaction, you can't access it!"; - } - return null; - } - - @Override - public String allowContainers(Claim claim, Player player) - { - Transaction b = RealEstate.transactionsStore.getTransaction(claim); - if(b != null && b.getOwner().equals(player.getUniqueId()) && b instanceof BoughtTransaction) - { - if(((BoughtTransaction)b).getBuyer() != null) - return "This claim is currently involved in a transaction, you can't access it's containers!"; - } - return null; - } - - @Override - public String allowGrantPermission(Claim claim, Player player) - { - Transaction b = RealEstate.transactionsStore.getTransaction(claim); - if(b != null && b instanceof BoughtTransaction) - { - if(((BoughtTransaction)b).getBuyer() != null) - return "This claim is currently involved in a transaction, you can't change any permission!"; - } - return null; - } - - @Override - public boolean anyTransaction(Claim claim) - { - return RealEstate.transactionsStore.anyTransaction(claim); - } -} +package me.EtienneDx.RealEstate; + +import org.bukkit.Material; +import org.bukkit.entity.Player; + +import me.EtienneDx.RealEstate.Transactions.BoughtTransaction; +import me.EtienneDx.RealEstate.Transactions.Transaction; +import me.ryanhamshire.GriefPrevention.Claim; +import me.ryanhamshire.GriefPrevention.IRealEstate; + +public class GP_RealEstateHook implements IRealEstate +{ + @Override + public String allowEdit(Claim claim, Player player) + { + Transaction b = RealEstate.transactionsStore.getTransaction(claim); + if(b != null && player.getUniqueId().equals(b.getOwner()) && b instanceof BoughtTransaction) + { + if(((BoughtTransaction)b).getBuyer() != null) + return "This claim is currently involved in a transaction, you can't edit it!"; + } + return null; + } + + @Override + public String allowBuild(Claim claim, Player player, Material material) + { + Transaction b = RealEstate.transactionsStore.getTransaction(claim); + if(b != null && player.getUniqueId().equals(b.getOwner()) && b instanceof BoughtTransaction)// ?? + { + if(((BoughtTransaction)b).getBuyer() != null) + return "This claim is currently involved in a transaction, you can't build on it!"; + } + return null; + } + + @Override + public String allowAccess(Claim claim, Player player) + { + Transaction b = RealEstate.transactionsStore.getTransaction(claim); + if(b != null && player.getUniqueId().equals(b.getOwner()) && b instanceof BoughtTransaction) + { + if(((BoughtTransaction)b).getBuyer() != null) + return "This claim is currently involved in a transaction, you can't access it!"; + } + return null; + } + + @Override + public String allowContainers(Claim claim, Player player) + { + Transaction b = RealEstate.transactionsStore.getTransaction(claim); + if(b != null && player.getUniqueId().equals(b.getOwner()) && b instanceof BoughtTransaction) + { + if(((BoughtTransaction)b).getBuyer() != null) + return "This claim is currently involved in a transaction, you can't access it's containers!"; + } + return null; + } + + @Override + public String allowGrantPermission(Claim claim, Player player) + { + Transaction b = RealEstate.transactionsStore.getTransaction(claim); + if(b != null && b instanceof BoughtTransaction) + { + if(((BoughtTransaction)b).getBuyer() != null) + return "This claim is currently involved in a transaction, you can't change any permission!"; + } + return null; + } + + @Override + public boolean anyTransaction(Claim claim) + { + return RealEstate.transactionsStore.anyTransaction(claim); + } +} diff --git a/src/me/EtienneDx/RealEstate/RECommand.java b/src/me/EtienneDx/RealEstate/RECommand.java index b66a485..40dbc24 100644 --- a/src/me/EtienneDx/RealEstate/RECommand.java +++ b/src/me/EtienneDx/RealEstate/RECommand.java @@ -87,7 +87,7 @@ public class RECommand extends BaseCommand } } - @Subcommand("seller") + /*@Subcommand("seller") @Description("Displays or changes the seller of a claim (admin only)") @Conditions("inPendingTransactionClaim") public static void setSeller(Player player, @Optional String newSeller) @@ -112,6 +112,7 @@ public class RECommand extends BaseCommand { tr.setOwner(null); tr.update(); + RealEstate.transactionsStore.saveData(); player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + "Changed the seller to the server"); } else @@ -130,11 +131,12 @@ public class RECommand extends BaseCommand { tr.setOwner(newOwner.getUniqueId()); tr.update(); + RealEstate.transactionsStore.saveData(); player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + "Changed the seller to " + ChatColor.GREEN + newOwner.getDisplayName()); } } - } + }*/ @Subcommand("exitoffer") @Conditions("partOfBoughtTransaction") diff --git a/src/me/EtienneDx/RealEstate/REListener.java b/src/me/EtienneDx/RealEstate/REListener.java index 8384864..7314056 100644 --- a/src/me/EtienneDx/RealEstate/REListener.java +++ b/src/me/EtienneDx/RealEstate/REListener.java @@ -114,13 +114,16 @@ public class REListener implements Listener event.getBlock().breakNaturally(); return; } - - if(claim.isAdminClaim() && !RealEstate.perms.has(player, "realestate.admin"))// admin may sell admin claims + + if(claim.isAdminClaim()) { - player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "You don't have the permission to sell admin claims!"); - event.setCancelled(true); - event.getBlock().breakNaturally(); - return; + if(!RealEstate.perms.has(player, "realestate.admin"))// admin may sell admin claims + { + player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "You don't have the permission to sell admin claims!"); + event.setCancelled(true); + event.getBlock().breakNaturally(); + return; + } } else if(type.equals("claim") && !player.getUniqueId().equals(claim.ownerID))// only the owner may sell his claim { @@ -132,7 +135,7 @@ public class REListener implements Listener // we should be good to sell it now event.setCancelled(true);// need to cancel the event, so we can update the sign elsewhere - RealEstate.transactionsStore.sell(claim, player, price, event.getBlock().getLocation()); + RealEstate.transactionsStore.sell(claim, claim.isAdminClaim() ? null : player, price, event.getBlock().getLocation()); } else if(RealEstate.instance.config.cfgRentKeywords.contains(event.getLine(0).toLowerCase()))// we want to rent it { @@ -216,12 +219,15 @@ public class REListener implements Listener } } - if(claim.isAdminClaim() && !RealEstate.perms.has(player, "realestate.admin"))// admin may rent admin claims + if(claim.isAdminClaim()) { - player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "You don't have the permission to rent admin claims!"); - event.setCancelled(true); - event.getBlock().breakNaturally(); - return; + if(!RealEstate.perms.has(player, "realestate.admin"))// admin may sell admin claims + { + player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "You don't have the permission to rent admin claims!"); + event.setCancelled(true); + event.getBlock().breakNaturally(); + return; + } } else if(type.equals("claim") && !player.getUniqueId().equals(claim.ownerID))// only the owner may sell his claim { @@ -307,12 +313,15 @@ public class REListener implements Listener return; } - if(claim.isAdminClaim() && !RealEstate.perms.has(player, "realestate.admin"))// admin may rent admin claims + if(claim.isAdminClaim()) { - player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "You don't have the permission to lease admin claims!"); - event.setCancelled(true); - event.getBlock().breakNaturally(); - return; + if(!RealEstate.perms.has(player, "realestate.admin"))// admin may sell admin claims + { + player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "You don't have the permission to lease admin claims!"); + event.setCancelled(true); + event.getBlock().breakNaturally(); + return; + } } else if(type.equals("claim") && !player.getUniqueId().equals(claim.ownerID))// only the owner may sell his claim { diff --git a/src/me/EtienneDx/RealEstate/Transactions/ClaimLease.java b/src/me/EtienneDx/RealEstate/Transactions/ClaimLease.java index 84fa249..27265a1 100644 --- a/src/me/EtienneDx/RealEstate/Transactions/ClaimLease.java +++ b/src/me/EtienneDx/RealEstate/Transactions/ClaimLease.java @@ -1,410 +1,413 @@ -package me.EtienneDx.RealEstate.Transactions; - -import java.time.Duration; -import java.time.LocalDate; -import java.time.LocalDateTime; -import java.time.LocalTime; -import java.time.Period; -import java.time.format.DateTimeFormatter; -import java.util.Map; -import org.bukkit.Bukkit; -import org.bukkit.Location; -import org.bukkit.OfflinePlayer; -import org.bukkit.block.Sign; -import org.bukkit.entity.Player; - -import com.earth2me.essentials.User; - -import me.EtienneDx.RealEstate.RealEstate; -import me.EtienneDx.RealEstate.Utils; -import me.ryanhamshire.GriefPrevention.Claim; -import me.ryanhamshire.GriefPrevention.ClaimPermission; -import me.ryanhamshire.GriefPrevention.GriefPrevention; -import net.md_5.bungee.api.ChatColor; - -public class ClaimLease extends BoughtTransaction -{ - public LocalDateTime lastPayment = null; - public int frequency; - public int paymentsLeft; - - public ClaimLease(Map map) - { - super(map); - if(map.get("lastPayment") != null) - lastPayment = LocalDateTime.parse((String) map.get("lastPayment"), DateTimeFormatter.ISO_DATE_TIME); - frequency = (int)map.get("frequency"); - paymentsLeft = (int)map.get("paymentsLeft"); - } - - public ClaimLease(Claim claim, Player player, double price, Location sign, int frequency, int paymentsLeft) - { - super(claim, player, price, sign); - this.frequency = frequency; - this.paymentsLeft = paymentsLeft; - } - - @Override - public Map serialize() { - Map map = super.serialize(); - - if(lastPayment != null) - map.put("lastPayment", lastPayment.format(DateTimeFormatter.ISO_DATE_TIME)); - map.put("frequency", frequency); - map.put("paymentsLeft", paymentsLeft); - - return map; - } - - @Override - public void update() - { - if(buyer == null)// not yet leased - { - if(destroyedSign) - { - RealEstate.transactionsStore.cancelTransaction(this); - } - else if(sign.getBlock().getState() instanceof Sign) - { - Sign s = (Sign)sign.getBlock().getState(); - s.setLine(0, RealEstate.instance.config.cfgSignsHeader); - s.setLine(1, ChatColor.DARK_GREEN + RealEstate.instance.config.cfgReplaceLease); - //s.setLine(2, owner != null ? Bukkit.getOfflinePlayer(owner).getName() : "SERVER"); - //s.setLine(2, paymentsLeft + "x " + price + " " + RealEstate.econ.currencyNamePlural()); - if(RealEstate.instance.config.cfgUseCurrencySymbol) - { - s.setLine(2, paymentsLeft + "x " + RealEstate.instance.config.cfgCurrencySymbol + " " + price); - } - else - { - s.setLine(2, paymentsLeft + "x " + price + " " + RealEstate.econ.currencyNamePlural()); - } - s.setLine(3, Utils.getTime(frequency, null, false)); - s.update(true); - } - } - else - { - int days = Period.between(lastPayment.toLocalDate(), LocalDate.now()).getDays(); - Duration hours = Duration.between(lastPayment.toLocalTime(), LocalTime.now()); - if(hours.isNegative() && !hours.isZero()) - { - hours = hours.plusHours(24); - days--; - } - if(days >= frequency)// we exceeded the time limit! - { - payLease(); - } - } - } - - private void payLease() - { - if(buyer == null) return; - - OfflinePlayer buyerPlayer = Bukkit.getOfflinePlayer(buyer); - OfflinePlayer seller = Bukkit.getOfflinePlayer(owner); - - String claimType = GriefPrevention.instance.dataStore.getClaimAt(sign, false, null).parent == null ? "claim" : "subclaim"; - - if(Utils.makePayment(owner, buyer, price, false, false)) - { - lastPayment = LocalDateTime.now(); - paymentsLeft--; - if(paymentsLeft > 0) - { - if(buyerPlayer.isOnline() && RealEstate.instance.config.cfgMessageBuyer) - { - ((Player)buyerPlayer).sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + - "Paid lease for the " + claimType + " at " + ChatColor.BLUE + "[" + sign.getWorld().getName() + ", X: " + sign.getBlockX() + - ", Y: " + sign.getBlockY() + ", Z: " + sign.getBlockZ() + "]" + - ChatColor.AQUA + " for the price of " + ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural() + - ChatColor.AQUA + ", " + ChatColor.GREEN + paymentsLeft + ChatColor.AQUA + " payments left"); - } - else if(RealEstate.instance.config.cfgMailOffline && RealEstate.ess != null) - { - User u = RealEstate.ess.getUser(this.buyer); - u.addMail(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + - "Paid lease for the " + claimType + " at " + ChatColor.BLUE + "[" + sign.getWorld().getName() + ", X: " + sign.getBlockX() + - ", Y: " + sign.getBlockY() + ", Z: " + sign.getBlockZ() + "]" + - ChatColor.AQUA + " for the price of " + ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural() + - ChatColor.AQUA + ", " + ChatColor.GREEN + paymentsLeft + ChatColor.AQUA + " payments left"); - } - - if(seller.isOnline() && RealEstate.instance.config.cfgMessageOwner) - { - ((Player)seller).sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.GREEN + buyerPlayer.getName() + - ChatColor.AQUA + " has paid lease for the " + claimType + " at " + ChatColor.BLUE + "[" + - sign.getWorld().getName() + ", X: " + sign.getBlockX() + ", Y: " + - sign.getBlockY() + ", Z: " + sign.getBlockZ() + "]" + - ChatColor.AQUA + " at the price of " + ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural() + - ChatColor.AQUA + ", " + ChatColor.GREEN + paymentsLeft + ChatColor.AQUA + " payments left"); - } - else if(RealEstate.instance.config.cfgMailOffline && RealEstate.ess != null) - { - User u = RealEstate.ess.getUser(this.owner); - u.addMail(RealEstate.instance.config.chatPrefix + ChatColor.GREEN + buyerPlayer.getName() + - ChatColor.AQUA + " has paid lease for the " + claimType + " at " + ChatColor.BLUE + "[" + - sign.getWorld().getName() + ", X: " + sign.getBlockX() + ", Y: " + - sign.getBlockY() + ", Z: " + sign.getBlockZ() + "]" + - ChatColor.AQUA + " at the price of " + ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural() + - ChatColor.AQUA + ", " + ChatColor.GREEN + paymentsLeft + ChatColor.AQUA + " payments left"); - } - } - else - { - if(buyerPlayer.isOnline() && RealEstate.instance.config.cfgMessageBuyer) - { - ((Player)buyerPlayer).sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + - "Paid final lease for the " + claimType + " at " + ChatColor.BLUE + "[" + sign.getWorld().getName() + ", X: " + sign.getBlockX() + - ", Y: " + sign.getBlockY() + ", Z: " + sign.getBlockZ() + "]" + - ChatColor.AQUA + " for the price of " + ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural() + - ChatColor.AQUA + ", the " + claimType + " is now yours"); - } - else if(RealEstate.instance.config.cfgMailOffline && RealEstate.ess != null) - { - User u = RealEstate.ess.getUser(this.buyer); - u.addMail(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + - "Paid final lease for the " + claimType + " at " + ChatColor.BLUE + "[" + sign.getWorld().getName() + ", X: " + sign.getBlockX() + - ", Y: " + sign.getBlockY() + ", Z: " + sign.getBlockZ() + "]" + - ChatColor.AQUA + " for the price of " + ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural() + - ChatColor.AQUA + ", the " + claimType + " is now yours"); - } - - if(seller.isOnline() && RealEstate.instance.config.cfgMessageOwner) - { - ((Player)seller).sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.GREEN + buyerPlayer.getName() + - ChatColor.AQUA + " has paid lease for the " + claimType + " at " + ChatColor.BLUE + "[" + - sign.getWorld().getName() + ", X: " + sign.getBlockX() + ", Y: " + - sign.getBlockY() + ", Z: " + sign.getBlockZ() + "]" + - ChatColor.AQUA + "at the price of " + ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural() + - ChatColor.AQUA + ", the " + claimType + " is now his property"); - } - else if(RealEstate.instance.config.cfgMailOffline && RealEstate.ess != null) - { - User u = RealEstate.ess.getUser(this.owner); - u.addMail(RealEstate.instance.config.chatPrefix + ChatColor.GREEN + buyerPlayer.getName() + - ChatColor.AQUA + " has paid lease for the " + claimType + " at " + ChatColor.BLUE + "[" + - sign.getWorld().getName() + ", X: " + sign.getBlockX() + ", Y: " + - sign.getBlockY() + ", Z: " + sign.getBlockZ() + "]" + - ChatColor.AQUA + "at the price of " + ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural() + - ChatColor.AQUA + ", the " + claimType + " is now his property"); - } - Claim claim = GriefPrevention.instance.dataStore.getClaimAt(sign, false, null); - - Utils.transferClaim(claim, buyer, owner); - RealEstate.transactionsStore.cancelTransaction(this);// the transaction is finished - } - } - else - { - if(buyerPlayer.isOnline() && RealEstate.instance.config.cfgMessageBuyer) - { - ((Player)buyerPlayer).sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + - "Couldn't pay the lease for the " + claimType + " at " + ChatColor.BLUE + "[" + sign.getWorld().getName() + ", X: " + - sign.getBlockX() + ", Y: " + - sign.getBlockY() + ", Z: " + sign.getBlockZ() + "]" + ChatColor.RED + ", the transaction has been cancelled."); - } - else if(RealEstate.instance.config.cfgMailOffline && RealEstate.ess != null) - { - User u = RealEstate.ess.getUser(this.buyer); - u.addMail(RealEstate.instance.config.chatPrefix + ChatColor.RED + - "Couldn't pay the lease for the " + claimType + " at " + ChatColor.BLUE + "[" + sign.getWorld().getName() + ", X: " + - sign.getBlockX() + ", Y: " + - sign.getBlockY() + ", Z: " + sign.getBlockZ() + "]" + ChatColor.RED + ", the transaction has been cancelled."); - } - if(seller.isOnline() && RealEstate.instance.config.cfgMessageOwner) - { - ((Player)seller).sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.GREEN + buyerPlayer.getName() + - ChatColor.AQUA + " couldn't pay lease for the " + claimType + " at " + ChatColor.BLUE + "[" + - sign.getWorld().getName() + ", X: " + sign.getBlockX() + ", Y: " + - sign.getBlockY() + ", Z: " + sign.getBlockZ() + "]" + - ChatColor.AQUA + ", the transaction has been cancelled"); - } - else if(RealEstate.instance.config.cfgMailOffline && RealEstate.ess != null) - { - User u = RealEstate.ess.getUser(this.owner); - u.addMail(RealEstate.instance.config.chatPrefix + ChatColor.GREEN + buyerPlayer.getName() + - ChatColor.AQUA + " couldn't pay lease for the " + claimType + " at " + ChatColor.BLUE + "[" + - sign.getWorld().getName() + ", X: " + sign.getBlockX() + ", Y: " + - sign.getBlockY() + ", Z: " + sign.getBlockZ() + "]" + - ChatColor.AQUA + ", the transaction has been cancelled"); - } - RealEstate.transactionsStore.cancelTransaction(this); - } - // no need to re update, since there's no sign - RealEstate.transactionsStore.saveData(); - } - - @Override - public boolean tryCancelTransaction(Player p) - { - if(buyer != null) - { - Claim claim = GriefPrevention.instance.dataStore.getClaimAt(sign, false, null); - if(p != null) - p.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "This " + (claim.parent == null ? "claim" : "subclaim") + - " is currently rented, you can't cancel the transaction!"); - return false; - } - else - { - RealEstate.transactionsStore.cancelTransaction(this); - return true; - } - } - - @Override - public void interact(Player player) - { - Claim claim = GriefPrevention.instance.dataStore.getClaimAt(sign, false, null);// getting by id creates errors for subclaims - if(claim == null) - { - player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "This claim does not exist!"); - RealEstate.transactionsStore.cancelTransaction(claim); - return; - } - String claimType = claim.parent == null ? "claim" : "subclaim"; - - if (owner.equals(player.getUniqueId())) - { - player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "You already own this " + claimType + "!"); - return; - } - if(claim.parent == null && !owner.equals(claim.ownerID)) - { - player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + Bukkit.getPlayer(owner).getDisplayName() + - " does not have the right to put this " + claimType + " for lease!"); - RealEstate.transactionsStore.cancelTransaction(claim); - return; - } - if(!player.hasPermission("realestate." + claimType + ".lease")) - { - player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "You do not have the permission to lease " + - claimType + "s!"); - return; - } - if(player.getUniqueId().equals(buyer)) - { - player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "You are already leasing this " + - claimType + "!"); - return; - } - if(buyer != null) - { - player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "Someone already leases this " + - claimType + "!"); - return; - } - - if(Utils.makePayment(owner, player.getUniqueId(), price, false, true))// if payment succeed - { - buyer = player.getUniqueId(); - lastPayment = LocalDateTime.now(); - paymentsLeft--; - claim.setPermission(buyer.toString(), ClaimPermission.Build); - GriefPrevention.instance.dataStore.saveClaim(claim); - getHolder().breakNaturally();// leases don't have signs indicating the remaining time - update(); - RealEstate.transactionsStore.saveData(); - - RealEstate.instance.addLogEntry( - "[" + RealEstate.transactionsStore.dateFormat.format(RealEstate.transactionsStore.date) + "] " + player.getName() + - " has started leasing a " + claimType + " at " + - "[" + player.getLocation().getWorld() + ", " + - "X: " + player.getLocation().getBlockX() + ", " + - "Y: " + player.getLocation().getBlockY() + ", " + - "Z: " + player.getLocation().getBlockZ() + "] " + - "Price: " + price + " " + RealEstate.econ.currencyNamePlural()); - - OfflinePlayer seller = Bukkit.getOfflinePlayer(owner); - if(RealEstate.instance.config.cfgMessageOwner && seller.isOnline()) - { - ((Player)seller).sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.GREEN + player.getName() + ChatColor.AQUA + - " has just paid for your lease for the " + claimType + " at " + - ChatColor.BLUE + "[" + sign.getWorld().getName() + ", X: " + sign.getBlockX() + ", Y: " + sign.getBlockY() + ", Z: " - + sign.getBlockZ() + "]" + ChatColor.AQUA + - " for " + ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural() + ChatColor.AQUA + ", " + - ChatColor.GREEN + paymentsLeft + ChatColor.AQUA + " payments left"); - } - else if(RealEstate.instance.config.cfgMailOffline && RealEstate.ess != null) - { - User u = RealEstate.ess.getUser(this.owner); - u.addMail(RealEstate.instance.config.chatPrefix + ChatColor.GREEN + player.getName() + ChatColor.AQUA + - " has just paid for your lease for the " + claimType + " at " + - ChatColor.BLUE + "[" + sign.getWorld().getName() + ", X: " + sign.getBlockX() + ", Y: " + sign.getBlockY() + ", Z: " - + sign.getBlockZ() + "]" + ChatColor.AQUA + - " for " + ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural() + ChatColor.AQUA + ", " + - ChatColor.GREEN + paymentsLeft + ChatColor.AQUA + " payments left"); - } - - player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + "You have successfully paid lease for this " + claimType + - " for " + ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural() + ChatColor.AQUA + ", " + - ChatColor.GREEN + paymentsLeft + ChatColor.AQUA + " payments left"); - } - } - - @Override - public void preview(Player player) - { - Claim claim = GriefPrevention.instance.dataStore.getClaimAt(sign, false, null); - String msg = ""; - if(player.hasPermission("realestate.info")) - { - String claimType = claim.parent == null ? "claim" : "subclaim"; - msg = ChatColor.BLUE + "-----= " + ChatColor.WHITE + "[" + ChatColor.GOLD + "RealEstate Rent Info" + ChatColor.WHITE + "]" + - ChatColor.BLUE + " =-----\n"; - if(buyer == null) - { - msg += ChatColor.AQUA + "This " + claimType + " is for lease for " + - ChatColor.GREEN + paymentsLeft + ChatColor.AQUA + " payments of " + - ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural() + ChatColor.AQUA + " each.\nPayments are due every " + - ChatColor.GREEN + Utils.getTime(frequency, null, true) + "\n"; - - if(claimType.equalsIgnoreCase("claim")) - { - msg += ChatColor.AQUA + "The current owner is: " + ChatColor.GREEN + claim.getOwnerName(); - } - else - { - msg += ChatColor.AQUA + "The main claim owner is: " + ChatColor.GREEN + claim.getOwnerName() + "\n"; - msg += ChatColor.LIGHT_PURPLE + "Note: " + ChatColor.AQUA + "You will only get access to this subclaim!"; - } - } - else - { - int days = Period.between(lastPayment.toLocalDate(), LocalDate.now()).getDays(); - Duration hours = Duration.between(lastPayment.toLocalTime(), LocalTime.now()); - if(hours.isNegative() && !hours.isZero()) - { - hours = hours.plusHours(24); - days--; - } - int daysLeft = frequency - days - 1;// we need to remove the current day - Duration timeRemaining = Duration.ofHours(24).minus(hours); - - msg += ChatColor.AQUA + "This " + claimType + " is currently leased by " + - ChatColor.GREEN + Bukkit.getOfflinePlayer(buyer).getName() + ChatColor.AQUA + " for " + - ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural() + ChatColor.AQUA + ". There is " + - ChatColor.GREEN + paymentsLeft + ChatColor.AQUA + " payments left. Next payment is in " + - ChatColor.GREEN + Utils.getTime(daysLeft, timeRemaining, true) + ChatColor.AQUA + ".\n"; - if(claimType.equalsIgnoreCase("claim")) - { - msg += ChatColor.AQUA + "The current owner is: " + ChatColor.GREEN + claim.getOwnerName(); - } - else - { - msg += ChatColor.AQUA + "The main claim owner is: " + ChatColor.GREEN + claim.getOwnerName(); - } - } - } - else - { - msg = RealEstate.instance.config.chatPrefix + ChatColor.RED + "You don't have the permission to view real estate informations!"; - } - player.sendMessage(msg); - } - -} +package me.EtienneDx.RealEstate.Transactions; + +import java.time.Duration; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.Period; +import java.time.format.DateTimeFormatter; +import java.util.Map; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.OfflinePlayer; +import org.bukkit.block.Sign; +import org.bukkit.entity.Player; + +import com.earth2me.essentials.User; + +import me.EtienneDx.RealEstate.RealEstate; +import me.EtienneDx.RealEstate.Utils; +import me.ryanhamshire.GriefPrevention.Claim; +import me.ryanhamshire.GriefPrevention.ClaimPermission; +import me.ryanhamshire.GriefPrevention.GriefPrevention; +import net.md_5.bungee.api.ChatColor; + +public class ClaimLease extends BoughtTransaction +{ + public LocalDateTime lastPayment = null; + public int frequency; + public int paymentsLeft; + + public ClaimLease(Map map) + { + super(map); + if(map.get("lastPayment") != null) + lastPayment = LocalDateTime.parse((String) map.get("lastPayment"), DateTimeFormatter.ISO_DATE_TIME); + frequency = (int)map.get("frequency"); + paymentsLeft = (int)map.get("paymentsLeft"); + } + + public ClaimLease(Claim claim, Player player, double price, Location sign, int frequency, int paymentsLeft) + { + super(claim, player, price, sign); + this.frequency = frequency; + this.paymentsLeft = paymentsLeft; + } + + @Override + public Map serialize() { + Map map = super.serialize(); + + if(lastPayment != null) + map.put("lastPayment", lastPayment.format(DateTimeFormatter.ISO_DATE_TIME)); + map.put("frequency", frequency); + map.put("paymentsLeft", paymentsLeft); + + return map; + } + + @Override + public void update() + { + if(buyer == null)// not yet leased + { + if(destroyedSign) + { + RealEstate.transactionsStore.cancelTransaction(this); + } + else if(sign.getBlock().getState() instanceof Sign) + { + Sign s = (Sign)sign.getBlock().getState(); + s.setLine(0, RealEstate.instance.config.cfgSignsHeader); + s.setLine(1, ChatColor.DARK_GREEN + RealEstate.instance.config.cfgReplaceLease); + //s.setLine(2, owner != null ? Bukkit.getOfflinePlayer(owner).getName() : "SERVER"); + //s.setLine(2, paymentsLeft + "x " + price + " " + RealEstate.econ.currencyNamePlural()); + if(RealEstate.instance.config.cfgUseCurrencySymbol) + { + s.setLine(2, paymentsLeft + "x " + RealEstate.instance.config.cfgCurrencySymbol + " " + price); + } + else + { + s.setLine(2, paymentsLeft + "x " + price + " " + RealEstate.econ.currencyNamePlural()); + } + s.setLine(3, Utils.getTime(frequency, null, false)); + s.update(true); + } + } + else + { + int days = Period.between(lastPayment.toLocalDate(), LocalDate.now()).getDays(); + Duration hours = Duration.between(lastPayment.toLocalTime(), LocalTime.now()); + if(hours.isNegative() && !hours.isZero()) + { + hours = hours.plusHours(24); + days--; + } + if(days >= frequency)// we exceeded the time limit! + { + payLease(); + } + } + } + + private void payLease() + { + if(buyer == null) return; + + OfflinePlayer buyerPlayer = Bukkit.getOfflinePlayer(buyer); + OfflinePlayer seller = owner == null ? null : Bukkit.getOfflinePlayer(owner); + + String claimType = GriefPrevention.instance.dataStore.getClaimAt(sign, false, null).parent == null ? "claim" : "subclaim"; + + if(Utils.makePayment(owner, buyer, price, false, false)) + { + lastPayment = LocalDateTime.now(); + paymentsLeft--; + if(paymentsLeft > 0) + { + if(buyerPlayer.isOnline() && RealEstate.instance.config.cfgMessageBuyer) + { + ((Player)buyerPlayer).sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + + "Paid lease for the " + claimType + " at " + ChatColor.BLUE + "[" + sign.getWorld().getName() + ", X: " + sign.getBlockX() + + ", Y: " + sign.getBlockY() + ", Z: " + sign.getBlockZ() + "]" + + ChatColor.AQUA + " for the price of " + ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural() + + ChatColor.AQUA + ", " + ChatColor.GREEN + paymentsLeft + ChatColor.AQUA + " payments left"); + } + else if(RealEstate.instance.config.cfgMailOffline && RealEstate.ess != null) + { + User u = RealEstate.ess.getUser(this.buyer); + u.addMail(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + + "Paid lease for the " + claimType + " at " + ChatColor.BLUE + "[" + sign.getWorld().getName() + ", X: " + sign.getBlockX() + + ", Y: " + sign.getBlockY() + ", Z: " + sign.getBlockZ() + "]" + + ChatColor.AQUA + " for the price of " + ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural() + + ChatColor.AQUA + ", " + ChatColor.GREEN + paymentsLeft + ChatColor.AQUA + " payments left"); + } + + if(owner != null) + { + if(seller.isOnline() && RealEstate.instance.config.cfgMessageOwner) + { + ((Player)seller).sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.GREEN + buyerPlayer.getName() + + ChatColor.AQUA + " has paid lease for the " + claimType + " at " + ChatColor.BLUE + "[" + + sign.getWorld().getName() + ", X: " + sign.getBlockX() + ", Y: " + + sign.getBlockY() + ", Z: " + sign.getBlockZ() + "]" + + ChatColor.AQUA + " at the price of " + ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural() + + ChatColor.AQUA + ", " + ChatColor.GREEN + paymentsLeft + ChatColor.AQUA + " payments left"); + } + else if(RealEstate.instance.config.cfgMailOffline && RealEstate.ess != null) + { + User u = RealEstate.ess.getUser(this.owner); + u.addMail(RealEstate.instance.config.chatPrefix + ChatColor.GREEN + buyerPlayer.getName() + + ChatColor.AQUA + " has paid lease for the " + claimType + " at " + ChatColor.BLUE + "[" + + sign.getWorld().getName() + ", X: " + sign.getBlockX() + ", Y: " + + sign.getBlockY() + ", Z: " + sign.getBlockZ() + "]" + + ChatColor.AQUA + " at the price of " + ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural() + + ChatColor.AQUA + ", " + ChatColor.GREEN + paymentsLeft + ChatColor.AQUA + " payments left"); + } + } + } + else + { + if(buyerPlayer.isOnline() && RealEstate.instance.config.cfgMessageBuyer) + { + ((Player)buyerPlayer).sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + + "Paid final lease for the " + claimType + " at " + ChatColor.BLUE + "[" + sign.getWorld().getName() + ", X: " + sign.getBlockX() + + ", Y: " + sign.getBlockY() + ", Z: " + sign.getBlockZ() + "]" + + ChatColor.AQUA + " for the price of " + ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural() + + ChatColor.AQUA + ", the " + claimType + " is now yours"); + } + else if(RealEstate.instance.config.cfgMailOffline && RealEstate.ess != null) + { + User u = RealEstate.ess.getUser(this.buyer); + u.addMail(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + + "Paid final lease for the " + claimType + " at " + ChatColor.BLUE + "[" + sign.getWorld().getName() + ", X: " + sign.getBlockX() + + ", Y: " + sign.getBlockY() + ", Z: " + sign.getBlockZ() + "]" + + ChatColor.AQUA + " for the price of " + ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural() + + ChatColor.AQUA + ", the " + claimType + " is now yours"); + } + + if(seller.isOnline() && RealEstate.instance.config.cfgMessageOwner) + { + ((Player)seller).sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.GREEN + buyerPlayer.getName() + + ChatColor.AQUA + " has paid lease for the " + claimType + " at " + ChatColor.BLUE + "[" + + sign.getWorld().getName() + ", X: " + sign.getBlockX() + ", Y: " + + sign.getBlockY() + ", Z: " + sign.getBlockZ() + "]" + + ChatColor.AQUA + "at the price of " + ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural() + + ChatColor.AQUA + ", the " + claimType + " is now his property"); + } + else if(RealEstate.instance.config.cfgMailOffline && RealEstate.ess != null) + { + User u = RealEstate.ess.getUser(this.owner); + u.addMail(RealEstate.instance.config.chatPrefix + ChatColor.GREEN + buyerPlayer.getName() + + ChatColor.AQUA + " has paid lease for the " + claimType + " at " + ChatColor.BLUE + "[" + + sign.getWorld().getName() + ", X: " + sign.getBlockX() + ", Y: " + + sign.getBlockY() + ", Z: " + sign.getBlockZ() + "]" + + ChatColor.AQUA + "at the price of " + ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural() + + ChatColor.AQUA + ", the " + claimType + " is now his property"); + } + Claim claim = GriefPrevention.instance.dataStore.getClaimAt(sign, false, null); + + Utils.transferClaim(claim, buyer, owner); + RealEstate.transactionsStore.cancelTransaction(this);// the transaction is finished + } + } + else + { + if(buyerPlayer.isOnline() && RealEstate.instance.config.cfgMessageBuyer) + { + ((Player)buyerPlayer).sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + + "Couldn't pay the lease for the " + claimType + " at " + ChatColor.BLUE + "[" + sign.getWorld().getName() + ", X: " + + sign.getBlockX() + ", Y: " + + sign.getBlockY() + ", Z: " + sign.getBlockZ() + "]" + ChatColor.RED + ", the transaction has been cancelled."); + } + else if(RealEstate.instance.config.cfgMailOffline && RealEstate.ess != null) + { + User u = RealEstate.ess.getUser(this.buyer); + u.addMail(RealEstate.instance.config.chatPrefix + ChatColor.RED + + "Couldn't pay the lease for the " + claimType + " at " + ChatColor.BLUE + "[" + sign.getWorld().getName() + ", X: " + + sign.getBlockX() + ", Y: " + + sign.getBlockY() + ", Z: " + sign.getBlockZ() + "]" + ChatColor.RED + ", the transaction has been cancelled."); + } + if(seller.isOnline() && RealEstate.instance.config.cfgMessageOwner) + { + ((Player)seller).sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.GREEN + buyerPlayer.getName() + + ChatColor.AQUA + " couldn't pay lease for the " + claimType + " at " + ChatColor.BLUE + "[" + + sign.getWorld().getName() + ", X: " + sign.getBlockX() + ", Y: " + + sign.getBlockY() + ", Z: " + sign.getBlockZ() + "]" + + ChatColor.AQUA + ", the transaction has been cancelled"); + } + else if(RealEstate.instance.config.cfgMailOffline && RealEstate.ess != null) + { + User u = RealEstate.ess.getUser(this.owner); + u.addMail(RealEstate.instance.config.chatPrefix + ChatColor.GREEN + buyerPlayer.getName() + + ChatColor.AQUA + " couldn't pay lease for the " + claimType + " at " + ChatColor.BLUE + "[" + + sign.getWorld().getName() + ", X: " + sign.getBlockX() + ", Y: " + + sign.getBlockY() + ", Z: " + sign.getBlockZ() + "]" + + ChatColor.AQUA + ", the transaction has been cancelled"); + } + RealEstate.transactionsStore.cancelTransaction(this); + } + // no need to re update, since there's no sign + RealEstate.transactionsStore.saveData(); + } + + @Override + public boolean tryCancelTransaction(Player p) + { + if(buyer != null) + { + Claim claim = GriefPrevention.instance.dataStore.getClaimAt(sign, false, null); + if(p != null) + p.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "This " + (claim.parent == null ? "claim" : "subclaim") + + " is currently rented, you can't cancel the transaction!"); + return false; + } + else + { + RealEstate.transactionsStore.cancelTransaction(this); + return true; + } + } + + @Override + public void interact(Player player) + { + Claim claim = GriefPrevention.instance.dataStore.getClaimAt(sign, false, null);// getting by id creates errors for subclaims + if(claim == null) + { + player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "This claim does not exist!"); + RealEstate.transactionsStore.cancelTransaction(claim); + return; + } + String claimType = claim.parent == null ? "claim" : "subclaim"; + + if (owner.equals(player.getUniqueId())) + { + player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "You already own this " + claimType + "!"); + return; + } + if(claim.parent == null && !owner.equals(claim.ownerID)) + { + player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + Bukkit.getPlayer(owner).getDisplayName() + + " does not have the right to put this " + claimType + " for lease!"); + RealEstate.transactionsStore.cancelTransaction(claim); + return; + } + if(!player.hasPermission("realestate." + claimType + ".lease")) + { + player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "You do not have the permission to lease " + + claimType + "s!"); + return; + } + if(player.getUniqueId().equals(buyer)) + { + player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "You are already leasing this " + + claimType + "!"); + return; + } + if(buyer != null) + { + player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "Someone already leases this " + + claimType + "!"); + return; + } + + if(Utils.makePayment(owner, player.getUniqueId(), price, false, true))// if payment succeed + { + buyer = player.getUniqueId(); + lastPayment = LocalDateTime.now(); + paymentsLeft--; + claim.setPermission(buyer.toString(), ClaimPermission.Build); + GriefPrevention.instance.dataStore.saveClaim(claim); + getHolder().breakNaturally();// leases don't have signs indicating the remaining time + update(); + RealEstate.transactionsStore.saveData(); + + RealEstate.instance.addLogEntry( + "[" + RealEstate.transactionsStore.dateFormat.format(RealEstate.transactionsStore.date) + "] " + player.getName() + + " has started leasing a " + claimType + " at " + + "[" + player.getLocation().getWorld() + ", " + + "X: " + player.getLocation().getBlockX() + ", " + + "Y: " + player.getLocation().getBlockY() + ", " + + "Z: " + player.getLocation().getBlockZ() + "] " + + "Price: " + price + " " + RealEstate.econ.currencyNamePlural()); + + OfflinePlayer seller = Bukkit.getOfflinePlayer(owner); + if(RealEstate.instance.config.cfgMessageOwner && seller.isOnline()) + { + ((Player)seller).sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.GREEN + player.getName() + ChatColor.AQUA + + " has just paid for your lease for the " + claimType + " at " + + ChatColor.BLUE + "[" + sign.getWorld().getName() + ", X: " + sign.getBlockX() + ", Y: " + sign.getBlockY() + ", Z: " + + sign.getBlockZ() + "]" + ChatColor.AQUA + + " for " + ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural() + ChatColor.AQUA + ", " + + ChatColor.GREEN + paymentsLeft + ChatColor.AQUA + " payments left"); + } + else if(RealEstate.instance.config.cfgMailOffline && RealEstate.ess != null) + { + User u = RealEstate.ess.getUser(this.owner); + u.addMail(RealEstate.instance.config.chatPrefix + ChatColor.GREEN + player.getName() + ChatColor.AQUA + + " has just paid for your lease for the " + claimType + " at " + + ChatColor.BLUE + "[" + sign.getWorld().getName() + ", X: " + sign.getBlockX() + ", Y: " + sign.getBlockY() + ", Z: " + + sign.getBlockZ() + "]" + ChatColor.AQUA + + " for " + ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural() + ChatColor.AQUA + ", " + + ChatColor.GREEN + paymentsLeft + ChatColor.AQUA + " payments left"); + } + + player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + "You have successfully paid lease for this " + claimType + + " for " + ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural() + ChatColor.AQUA + ", " + + ChatColor.GREEN + paymentsLeft + ChatColor.AQUA + " payments left"); + } + } + + @Override + public void preview(Player player) + { + Claim claim = GriefPrevention.instance.dataStore.getClaimAt(sign, false, null); + String msg = ""; + if(player.hasPermission("realestate.info")) + { + String claimType = claim.parent == null ? "claim" : "subclaim"; + msg = ChatColor.BLUE + "-----= " + ChatColor.WHITE + "[" + ChatColor.GOLD + "RealEstate Rent Info" + ChatColor.WHITE + "]" + + ChatColor.BLUE + " =-----\n"; + if(buyer == null) + { + msg += ChatColor.AQUA + "This " + claimType + " is for lease for " + + ChatColor.GREEN + paymentsLeft + ChatColor.AQUA + " payments of " + + ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural() + ChatColor.AQUA + " each.\nPayments are due every " + + ChatColor.GREEN + Utils.getTime(frequency, null, true) + "\n"; + + if(claimType.equalsIgnoreCase("claim")) + { + msg += ChatColor.AQUA + "The current owner is: " + ChatColor.GREEN + claim.getOwnerName(); + } + else + { + msg += ChatColor.AQUA + "The main claim owner is: " + ChatColor.GREEN + claim.getOwnerName() + "\n"; + msg += ChatColor.LIGHT_PURPLE + "Note: " + ChatColor.AQUA + "You will only get access to this subclaim!"; + } + } + else + { + int days = Period.between(lastPayment.toLocalDate(), LocalDate.now()).getDays(); + Duration hours = Duration.between(lastPayment.toLocalTime(), LocalTime.now()); + if(hours.isNegative() && !hours.isZero()) + { + hours = hours.plusHours(24); + days--; + } + int daysLeft = frequency - days - 1;// we need to remove the current day + Duration timeRemaining = Duration.ofHours(24).minus(hours); + + msg += ChatColor.AQUA + "This " + claimType + " is currently leased by " + + ChatColor.GREEN + Bukkit.getOfflinePlayer(buyer).getName() + ChatColor.AQUA + " for " + + ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural() + ChatColor.AQUA + ". There is " + + ChatColor.GREEN + paymentsLeft + ChatColor.AQUA + " payments left. Next payment is in " + + ChatColor.GREEN + Utils.getTime(daysLeft, timeRemaining, true) + ChatColor.AQUA + ".\n"; + if(claimType.equalsIgnoreCase("claim")) + { + msg += ChatColor.AQUA + "The current owner is: " + ChatColor.GREEN + claim.getOwnerName(); + } + else + { + msg += ChatColor.AQUA + "The main claim owner is: " + ChatColor.GREEN + claim.getOwnerName(); + } + } + } + else + { + msg = RealEstate.instance.config.chatPrefix + ChatColor.RED + "You don't have the permission to view real estate informations!"; + } + player.sendMessage(msg); + } + +} diff --git a/src/me/EtienneDx/RealEstate/Transactions/ClaimRent.java b/src/me/EtienneDx/RealEstate/Transactions/ClaimRent.java index f7ae7ad..5ab838c 100644 --- a/src/me/EtienneDx/RealEstate/Transactions/ClaimRent.java +++ b/src/me/EtienneDx/RealEstate/Transactions/ClaimRent.java @@ -1,390 +1,392 @@ -package me.EtienneDx.RealEstate.Transactions; - -import java.time.Duration; -import java.time.LocalDate; -import java.time.LocalDateTime; -import java.time.LocalTime; -import java.time.Period; -import java.time.format.DateTimeFormatter; -import java.util.Map; -import org.bukkit.Bukkit; -import org.bukkit.Location; -import org.bukkit.OfflinePlayer; -import org.bukkit.block.Sign; -import org.bukkit.entity.Player; - -import com.earth2me.essentials.User; - -import me.EtienneDx.RealEstate.RealEstate; -import me.EtienneDx.RealEstate.Utils; -import me.ryanhamshire.GriefPrevention.Claim; -import me.ryanhamshire.GriefPrevention.ClaimPermission; -import me.ryanhamshire.GriefPrevention.GriefPrevention; -import net.md_5.bungee.api.ChatColor; - -public class ClaimRent extends BoughtTransaction -{ - LocalDateTime startDate = null; - int duration; - public boolean autoRenew = false; - public int periodCount = 0; - public int maxPeriod; - - public ClaimRent(Map map) - { - super(map); - if(map.get("startDate") != null) - startDate = LocalDateTime.parse((String) map.get("startDate"), DateTimeFormatter.ISO_DATE_TIME); - duration = (int)map.get("duration"); - autoRenew = (boolean) map.get("autoRenew"); - periodCount = (int) map.get("periodCount"); - maxPeriod = (int) map.get("maxPeriod"); - } - - public ClaimRent(Claim claim, Player player, double price, Location sign, int duration, int rentPeriods) - { - super(claim, player, price, sign); - this.duration = duration; - this.maxPeriod = RealEstate.instance.config.cfgEnableRentPeriod ? rentPeriods : 1; - } - - @Override - public Map serialize() { - Map map = super.serialize(); - - if(startDate != null) - map.put("startDate", startDate.format(DateTimeFormatter.ISO_DATE_TIME)); - map.put("duration", duration); - map.put("autoRenew", autoRenew); - map.put("periodCount", periodCount); - map.put("maxPeriod", maxPeriod); - - return map; - } - - @Override - public void update() - { - if(buyer == null) - { - if(destroyedSign) - { - RealEstate.transactionsStore.cancelTransaction(this); - } - else if(sign.getBlock().getState() instanceof Sign) - { - Sign s = (Sign) sign.getBlock().getState(); - s.setLine(0, RealEstate.instance.config.cfgSignsHeader); - s.setLine(1, ChatColor.DARK_GREEN + RealEstate.instance.config.cfgReplaceRent); - //s.setLine(2, owner != null ? Bukkit.getOfflinePlayer(owner).getName() : "SERVER"); - if(RealEstate.instance.config.cfgUseCurrencySymbol) - { - s.setLine(2, RealEstate.instance.config.cfgCurrencySymbol + " " + price); - } - else - { - s.setLine(2, price + " " + RealEstate.econ.currencyNamePlural()); - } - s.setLine(3, (maxPeriod > 1 ? maxPeriod + "x " : "") + Utils.getTime(duration, null, false)); - s.update(true); - } - } - else - { - // we want to know how much time has gone by since startDate - int days = Period.between(startDate.toLocalDate(), LocalDate.now()).getDays(); - Duration hours = Duration.between(startDate.toLocalTime(), LocalTime.now()); - if(hours.isNegative() && !hours.isZero()) - { - hours = hours.plusHours(24); - days--; - } - if(days >= duration)// we exceeded the time limit! - { - payRent(); - } - else if(sign.getBlock().getState() instanceof Sign) - { - Sign s = (Sign) sign.getBlock().getState(); - s.setLine(0, RealEstate.instance.config.cfgSignsHeader); - s.setLine(1, Utils.getSignString("Rented by " + Bukkit.getOfflinePlayer(buyer).getName())); - s.setLine(2, "Time remaining : "); - - int daysLeft = duration - days - 1;// we need to remove the current day - Duration timeRemaining = Duration.ofHours(24).minus(hours); - - s.setLine(3, Utils.getTime(daysLeft, timeRemaining, false)); - s.update(true); - } - } - - } - - private void unRent(boolean msgBuyer) - { - Claim claim = GriefPrevention.instance.dataStore.getClaimAt(sign, false, null); - claim.dropPermission(buyer.toString()); - GriefPrevention.instance.dataStore.saveClaim(claim); - if(msgBuyer && Bukkit.getOfflinePlayer(buyer).isOnline() && RealEstate.instance.config.cfgMessageBuyer) - { - Bukkit.getPlayer(buyer).sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + - "The rent for the " + (claim.parent == null ? "claim" : "subclaim") + " at " + ChatColor.BLUE + "[" + - sign.getWorld().getName() + ", X: " + sign.getBlockX() + ", Y: " + - sign.getBlockY() + ", Z: " + sign.getBlockZ() + "]" + ChatColor.AQUA + " is now over, your access has been revoked."); - } - buyer = null; - RealEstate.transactionsStore.saveData(); - update(); - } - - private void payRent() - { - if(buyer == null) return; - - OfflinePlayer buyerPlayer = Bukkit.getOfflinePlayer(this.buyer); - OfflinePlayer seller = Bukkit.getOfflinePlayer(owner); - - String claimType = GriefPrevention.instance.dataStore.getClaimAt(sign, false, null).parent == null ? "claim" : "subclaim"; - - if((autoRenew || periodCount < maxPeriod) && Utils.makePayment(owner, this.buyer, price, false, false)) - { - periodCount = (periodCount + 1) % maxPeriod; - startDate = LocalDateTime.now(); - if(buyerPlayer.isOnline() && RealEstate.instance.config.cfgMessageBuyer) - { - ((Player)buyerPlayer).sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + - "Paid rent for the " + claimType + " at " + ChatColor.BLUE + "[" + sign.getWorld().getName() + ", X: " + sign.getBlockX() + - ", Y: " + sign.getBlockY() + ", Z: " + sign.getBlockZ() + "]" + - ChatColor.AQUA + "for the price of " + ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural()); - } - else if(RealEstate.instance.config.cfgMailOffline && RealEstate.ess != null) - { - User u = RealEstate.ess.getUser(this.buyer); - u.addMail(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + - "Paid rent for the " + claimType + " at " + ChatColor.BLUE + "[" + sign.getWorld().getName() + ", X: " + sign.getBlockX() + - ", Y: " + sign.getBlockY() + ", Z: " + sign.getBlockZ() + "]" + - ChatColor.AQUA + "for the price of " + ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural()); - } - - if(seller.isOnline() && RealEstate.instance.config.cfgMessageOwner) - { - ((Player)seller).sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + buyerPlayer.getName() + - " has paid rent for the " + claimType + " at " + ChatColor.BLUE + "[" + - sign.getWorld().getName() + ", X: " + sign.getBlockX() + ", Y: " + - sign.getBlockY() + ", Z: " + sign.getBlockZ() + "]" + - ChatColor.AQUA + "at the price of " + ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural()); - } - else if(RealEstate.instance.config.cfgMailOffline && RealEstate.ess != null) - { - User u = RealEstate.ess.getUser(this.owner); - u.addMail(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + buyerPlayer.getName() + - " has paid rent for the " + claimType + " at " + ChatColor.BLUE + "[" + - sign.getWorld().getName() + ", X: " + sign.getBlockX() + ", Y: " + - sign.getBlockY() + ", Z: " + sign.getBlockZ() + "]" + - ChatColor.AQUA + "at the price of " + ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural()); - } - - - } - else if (autoRenew) - { - if(buyerPlayer.isOnline() && RealEstate.instance.config.cfgMessageBuyer) - { - ((Player)buyerPlayer).sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + - "Couldn't pay the rent for the " + claimType + " at " + ChatColor.BLUE + "[" + sign.getWorld().getName() + ", X: " + - sign.getBlockX() + ", Y: " + - sign.getBlockY() + ", Z: " + sign.getBlockZ() + "]" + ChatColor.RED + ", your access has been revoked."); - } - else if(RealEstate.instance.config.cfgMailOffline && RealEstate.ess != null) - { - User u = RealEstate.ess.getUser(this.buyer); - u.addMail(RealEstate.instance.config.chatPrefix + ChatColor.RED + - "Couldn't pay the rent for the " + claimType + " at " + ChatColor.BLUE + "[" + sign.getWorld().getName() + ", X: " + - sign.getBlockX() + ", Y: " + - sign.getBlockY() + ", Z: " + sign.getBlockZ() + "]" + ChatColor.RED + ", your access has been revoked."); - } - unRent(false); - return; - } - else - { - unRent(true); - return; - } - update(); - RealEstate.transactionsStore.saveData(); - } - - @Override - public boolean tryCancelTransaction(Player p) - { - if(buyer != null) - { - Claim claim = GriefPrevention.instance.dataStore.getClaimAt(sign, false, null); - if(p != null) - p.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "This " + (claim.parent == null ? "claim" : "subclaim") + - " is currently rented, you can't cancel the transaction!"); - return false; - } - else - { - RealEstate.transactionsStore.cancelTransaction(this); - return true; - } - } - - @Override - public void interact(Player player) - { - Claim claim = GriefPrevention.instance.dataStore.getClaimAt(sign, false, null);// getting by id creates errors for subclaims - if(claim == null) - { - player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "This claim does not exist!"); - RealEstate.transactionsStore.cancelTransaction(claim); - return; - } - String claimType = claim.parent == null ? "claim" : "subclaim"; - - if (owner.equals(player.getUniqueId())) - { - player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "You already own this " + claimType + "!"); - return; - } - if(claim.parent == null && !owner.equals(claim.ownerID)) - { - player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + Bukkit.getPlayer(owner).getDisplayName() + - " does not have the right to rent this " + claimType + "!"); - RealEstate.transactionsStore.cancelTransaction(claim); - return; - } - if(!player.hasPermission("realestate." + claimType + ".rent")) - { - player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "You do not have the permission to rent " + - claimType + "s!"); - return; - } - if(player.getUniqueId().equals(buyer)) - { - player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "You are already renting this " + - claimType + "!"); - return; - } - if(buyer != null) - { - player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "Someone already rents this " + - claimType + "!"); - return; - } - - if(Utils.makePayment(owner, player.getUniqueId(), price, false, true))// if payment succeed - { - buyer = player.getUniqueId(); - startDate = LocalDateTime.now(); - autoRenew = false; - claim.setPermission(buyer.toString(), ClaimPermission.Build); - GriefPrevention.instance.dataStore.saveClaim(claim); - update(); - RealEstate.transactionsStore.saveData(); - - RealEstate.instance.addLogEntry( - "[" + RealEstate.transactionsStore.dateFormat.format(RealEstate.transactionsStore.date) + "] " + player.getName() + - " has rented a " + claimType + " at " + - "[" + player.getLocation().getWorld() + ", " + - "X: " + player.getLocation().getBlockX() + ", " + - "Y: " + player.getLocation().getBlockY() + ", " + - "Z: " + player.getLocation().getBlockZ() + "] " + - "Price: " + price + " " + RealEstate.econ.currencyNamePlural()); - - OfflinePlayer seller = Bukkit.getOfflinePlayer(owner); - if(RealEstate.instance.config.cfgMessageOwner && seller.isOnline()) - { - ((Player)seller).sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.GREEN + player.getName() + ChatColor.AQUA + - " has just rented your " + claimType + " at " + - ChatColor.BLUE + "[" + sign.getWorld().getName() + ", X: " + sign.getBlockX() + ", Y: " + sign.getBlockY() + ", Z: " - + sign.getBlockZ() + "]" + ChatColor.AQUA + - " for " + ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural()); - } - else if(RealEstate.instance.config.cfgMailOffline && RealEstate.ess != null) - { - User u = RealEstate.ess.getUser(this.owner); - u.addMail(RealEstate.instance.config.chatPrefix + ChatColor.GREEN + player.getName() + ChatColor.AQUA + - " has just rented your " + claimType + " at " + - ChatColor.BLUE + "[" + sign.getWorld().getName() + ", X: " + sign.getBlockX() + ", Y: " + sign.getBlockY() + ", Z: " - + sign.getBlockZ() + "]" + ChatColor.AQUA + - " for " + ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural()); - } - - player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + "You have successfully rented this " + claimType + - " for " + ChatColor.GREEN + price + RealEstate.econ.currencyNamePlural()); - - destroySign(); - } - } - - @Override - public void preview(Player player) - { - Claim claim = GriefPrevention.instance.dataStore.getClaimAt(sign, false, null); - String msg = ""; - if(player.hasPermission("realestate.info")) - { - String claimType = claim.parent == null ? "claim" : "subclaim"; - msg = ChatColor.BLUE + "-----= " + ChatColor.WHITE + "[" + ChatColor.GOLD + "RealEstate Rent Info" + ChatColor.WHITE + "]" + - ChatColor.BLUE + " =-----\n"; - if(buyer == null) - { - msg += ChatColor.AQUA + "This " + claimType + " is for rent for " + - ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural() + ChatColor.AQUA + " for " + - (maxPeriod > 1 ? "" + ChatColor.GREEN + maxPeriod + ChatColor.AQUA + " periods of " : "") + - ChatColor.GREEN + Utils.getTime(duration, null, true) + "\n"; - - if(claimType.equalsIgnoreCase("claim")) - { - msg += ChatColor.AQUA + "The current owner is: " + ChatColor.GREEN + claim.getOwnerName(); - } - else - { - msg += ChatColor.AQUA + "The main claim owner is: " + ChatColor.GREEN + claim.getOwnerName() + "\n"; - msg += ChatColor.LIGHT_PURPLE + "Note: " + ChatColor.AQUA + "You will only rent access to this subclaim!"; - } - } - else - { - int days = Period.between(startDate.toLocalDate(), LocalDate.now()).getDays(); - Duration hours = Duration.between(startDate.toLocalTime(), LocalTime.now()); - if(hours.isNegative() && !hours.isZero()) - { - hours = hours.plusHours(24); - days--; - } - int daysLeft = duration - days - 1;// we need to remove the current day - Duration timeRemaining = Duration.ofHours(24).minus(hours); - - msg += ChatColor.AQUA + "This " + claimType + " is currently rented by " + - ChatColor.GREEN + Bukkit.getOfflinePlayer(buyer).getName() + ChatColor.AQUA + " for " + - ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural() + ChatColor.AQUA + " for " + - (maxPeriod - periodCount > 1 ? "" + ChatColor.GREEN + (maxPeriod - periodCount) + ChatColor.AQUA + " periods of " + - ChatColor.GREEN + Utils.getTime(duration, null, false) + ChatColor.AQUA + ". The current period will end in " : "another ") + - ChatColor.GREEN + Utils.getTime(daysLeft, timeRemaining, true) + "\n"; - if((owner.equals(player.getUniqueId()) || buyer.equals(player.getUniqueId())) && RealEstate.instance.config.cfgEnableAutoRenew) - { - msg += ChatColor.AQUA + "Automatic renew is currently " + ChatColor.LIGHT_PURPLE + (autoRenew ? "enabled" : "disabled") + "\n"; - } - if(claimType.equalsIgnoreCase("claim")) - { - msg += ChatColor.AQUA + "The current owner is: " + ChatColor.GREEN + claim.getOwnerName(); - } - else - { - msg += ChatColor.AQUA + "The main claim owner is: " + ChatColor.GREEN + claim.getOwnerName(); - } - } - } - else - { - msg = RealEstate.instance.config.chatPrefix + ChatColor.RED + "You don't have the permission to view real estate informations!"; - } - player.sendMessage(msg); - } - -} +package me.EtienneDx.RealEstate.Transactions; + +import java.time.Duration; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.Period; +import java.time.format.DateTimeFormatter; +import java.util.Map; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.OfflinePlayer; +import org.bukkit.block.Sign; +import org.bukkit.entity.Player; + +import com.earth2me.essentials.User; + +import me.EtienneDx.RealEstate.RealEstate; +import me.EtienneDx.RealEstate.Utils; +import me.ryanhamshire.GriefPrevention.Claim; +import me.ryanhamshire.GriefPrevention.ClaimPermission; +import me.ryanhamshire.GriefPrevention.GriefPrevention; +import net.md_5.bungee.api.ChatColor; + +public class ClaimRent extends BoughtTransaction +{ + LocalDateTime startDate = null; + int duration; + public boolean autoRenew = false; + public int periodCount = 0; + public int maxPeriod; + + public ClaimRent(Map map) + { + super(map); + if(map.get("startDate") != null) + startDate = LocalDateTime.parse((String) map.get("startDate"), DateTimeFormatter.ISO_DATE_TIME); + duration = (int)map.get("duration"); + autoRenew = (boolean) map.get("autoRenew"); + periodCount = (int) map.get("periodCount"); + maxPeriod = (int) map.get("maxPeriod"); + } + + public ClaimRent(Claim claim, Player player, double price, Location sign, int duration, int rentPeriods) + { + super(claim, player, price, sign); + this.duration = duration; + this.maxPeriod = RealEstate.instance.config.cfgEnableRentPeriod ? rentPeriods : 1; + } + + @Override + public Map serialize() { + Map map = super.serialize(); + + if(startDate != null) + map.put("startDate", startDate.format(DateTimeFormatter.ISO_DATE_TIME)); + map.put("duration", duration); + map.put("autoRenew", autoRenew); + map.put("periodCount", periodCount); + map.put("maxPeriod", maxPeriod); + + return map; + } + + @Override + public void update() + { + if(buyer == null) + { + if(destroyedSign) + { + RealEstate.transactionsStore.cancelTransaction(this); + } + else if(sign.getBlock().getState() instanceof Sign) + { + Sign s = (Sign) sign.getBlock().getState(); + s.setLine(0, RealEstate.instance.config.cfgSignsHeader); + s.setLine(1, ChatColor.DARK_GREEN + RealEstate.instance.config.cfgReplaceRent); + //s.setLine(2, owner != null ? Bukkit.getOfflinePlayer(owner).getName() : "SERVER"); + if(RealEstate.instance.config.cfgUseCurrencySymbol) + { + s.setLine(2, RealEstate.instance.config.cfgCurrencySymbol + " " + price); + } + else + { + s.setLine(2, price + " " + RealEstate.econ.currencyNamePlural()); + } + s.setLine(3, (maxPeriod > 1 ? maxPeriod + "x " : "") + Utils.getTime(duration, null, false)); + s.update(true); + } + } + else + { + // we want to know how much time has gone by since startDate + int days = Period.between(startDate.toLocalDate(), LocalDate.now()).getDays(); + Duration hours = Duration.between(startDate.toLocalTime(), LocalTime.now()); + if(hours.isNegative() && !hours.isZero()) + { + hours = hours.plusHours(24); + days--; + } + if(days >= duration)// we exceeded the time limit! + { + payRent(); + } + else if(sign.getBlock().getState() instanceof Sign) + { + Sign s = (Sign) sign.getBlock().getState(); + s.setLine(0, RealEstate.instance.config.cfgSignsHeader); + s.setLine(1, Utils.getSignString("Rented by " + Bukkit.getOfflinePlayer(buyer).getName())); + s.setLine(2, "Time remaining : "); + + int daysLeft = duration - days - 1;// we need to remove the current day + Duration timeRemaining = Duration.ofHours(24).minus(hours); + + s.setLine(3, Utils.getTime(daysLeft, timeRemaining, false)); + s.update(true); + } + } + + } + + private void unRent(boolean msgBuyer) + { + Claim claim = GriefPrevention.instance.dataStore.getClaimAt(sign, false, null); + claim.dropPermission(buyer.toString()); + GriefPrevention.instance.dataStore.saveClaim(claim); + if(msgBuyer && Bukkit.getOfflinePlayer(buyer).isOnline() && RealEstate.instance.config.cfgMessageBuyer) + { + Bukkit.getPlayer(buyer).sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + + "The rent for the " + (claim.parent == null ? "claim" : "subclaim") + " at " + ChatColor.BLUE + "[" + + sign.getWorld().getName() + ", X: " + sign.getBlockX() + ", Y: " + + sign.getBlockY() + ", Z: " + sign.getBlockZ() + "]" + ChatColor.AQUA + " is now over, your access has been revoked."); + } + buyer = null; + RealEstate.transactionsStore.saveData(); + update(); + } + + private void payRent() + { + if(buyer == null) return; + + OfflinePlayer buyerPlayer = Bukkit.getOfflinePlayer(this.buyer); + OfflinePlayer seller = owner == null ? null : Bukkit.getOfflinePlayer(owner); + + String claimType = GriefPrevention.instance.dataStore.getClaimAt(sign, false, null).parent == null ? "claim" : "subclaim"; + + if((autoRenew || periodCount < maxPeriod) && Utils.makePayment(owner, this.buyer, price, false, false)) + { + periodCount = (periodCount + 1) % maxPeriod; + startDate = LocalDateTime.now(); + if(buyerPlayer.isOnline() && RealEstate.instance.config.cfgMessageBuyer) + { + ((Player)buyerPlayer).sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + + "Paid rent for the " + claimType + " at " + ChatColor.BLUE + "[" + sign.getWorld().getName() + ", X: " + sign.getBlockX() + + ", Y: " + sign.getBlockY() + ", Z: " + sign.getBlockZ() + "]" + + ChatColor.AQUA + "for the price of " + ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural()); + } + else if(RealEstate.instance.config.cfgMailOffline && RealEstate.ess != null) + { + User u = RealEstate.ess.getUser(this.buyer); + u.addMail(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + + "Paid rent for the " + claimType + " at " + ChatColor.BLUE + "[" + sign.getWorld().getName() + ", X: " + sign.getBlockX() + + ", Y: " + sign.getBlockY() + ", Z: " + sign.getBlockZ() + "]" + + ChatColor.AQUA + "for the price of " + ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural()); + } + + if(seller != null) + { + if(seller.isOnline() && RealEstate.instance.config.cfgMessageOwner) + { + ((Player)seller).sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + buyerPlayer.getName() + + " has paid rent for the " + claimType + " at " + ChatColor.BLUE + "[" + + sign.getWorld().getName() + ", X: " + sign.getBlockX() + ", Y: " + + sign.getBlockY() + ", Z: " + sign.getBlockZ() + "]" + + ChatColor.AQUA + "at the price of " + ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural()); + } + else if(RealEstate.instance.config.cfgMailOffline && RealEstate.ess != null) + { + User u = RealEstate.ess.getUser(this.owner); + u.addMail(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + buyerPlayer.getName() + + " has paid rent for the " + claimType + " at " + ChatColor.BLUE + "[" + + sign.getWorld().getName() + ", X: " + sign.getBlockX() + ", Y: " + + sign.getBlockY() + ", Z: " + sign.getBlockZ() + "]" + + ChatColor.AQUA + "at the price of " + ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural()); + } + } + + } + else if (autoRenew) + { + if(buyerPlayer.isOnline() && RealEstate.instance.config.cfgMessageBuyer) + { + ((Player)buyerPlayer).sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + + "Couldn't pay the rent for the " + claimType + " at " + ChatColor.BLUE + "[" + sign.getWorld().getName() + ", X: " + + sign.getBlockX() + ", Y: " + + sign.getBlockY() + ", Z: " + sign.getBlockZ() + "]" + ChatColor.RED + ", your access has been revoked."); + } + else if(RealEstate.instance.config.cfgMailOffline && RealEstate.ess != null) + { + User u = RealEstate.ess.getUser(this.buyer); + u.addMail(RealEstate.instance.config.chatPrefix + ChatColor.RED + + "Couldn't pay the rent for the " + claimType + " at " + ChatColor.BLUE + "[" + sign.getWorld().getName() + ", X: " + + sign.getBlockX() + ", Y: " + + sign.getBlockY() + ", Z: " + sign.getBlockZ() + "]" + ChatColor.RED + ", your access has been revoked."); + } + unRent(false); + return; + } + else + { + unRent(true); + return; + } + update(); + RealEstate.transactionsStore.saveData(); + } + + @Override + public boolean tryCancelTransaction(Player p) + { + if(buyer != null) + { + Claim claim = GriefPrevention.instance.dataStore.getClaimAt(sign, false, null); + if(p != null) + p.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "This " + (claim.parent == null ? "claim" : "subclaim") + + " is currently rented, you can't cancel the transaction!"); + return false; + } + else + { + RealEstate.transactionsStore.cancelTransaction(this); + return true; + } + } + + @Override + public void interact(Player player) + { + Claim claim = GriefPrevention.instance.dataStore.getClaimAt(sign, false, null);// getting by id creates errors for subclaims + if(claim == null) + { + player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "This claim does not exist!"); + RealEstate.transactionsStore.cancelTransaction(claim); + return; + } + String claimType = claim.parent == null ? "claim" : "subclaim"; + + if (owner.equals(player.getUniqueId())) + { + player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "You already own this " + claimType + "!"); + return; + } + if(claim.parent == null && !owner.equals(claim.ownerID)) + { + player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + Bukkit.getPlayer(owner).getDisplayName() + + " does not have the right to rent this " + claimType + "!"); + RealEstate.transactionsStore.cancelTransaction(claim); + return; + } + if(!player.hasPermission("realestate." + claimType + ".rent")) + { + player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "You do not have the permission to rent " + + claimType + "s!"); + return; + } + if(player.getUniqueId().equals(buyer)) + { + player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "You are already renting this " + + claimType + "!"); + return; + } + if(buyer != null) + { + player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "Someone already rents this " + + claimType + "!"); + return; + } + + if(Utils.makePayment(owner, player.getUniqueId(), price, false, true))// if payment succeed + { + buyer = player.getUniqueId(); + startDate = LocalDateTime.now(); + autoRenew = false; + claim.setPermission(buyer.toString(), ClaimPermission.Build); + GriefPrevention.instance.dataStore.saveClaim(claim); + update(); + RealEstate.transactionsStore.saveData(); + + RealEstate.instance.addLogEntry( + "[" + RealEstate.transactionsStore.dateFormat.format(RealEstate.transactionsStore.date) + "] " + player.getName() + + " has rented a " + claimType + " at " + + "[" + player.getLocation().getWorld() + ", " + + "X: " + player.getLocation().getBlockX() + ", " + + "Y: " + player.getLocation().getBlockY() + ", " + + "Z: " + player.getLocation().getBlockZ() + "] " + + "Price: " + price + " " + RealEstate.econ.currencyNamePlural()); + + OfflinePlayer seller = Bukkit.getOfflinePlayer(owner); + if(RealEstate.instance.config.cfgMessageOwner && seller.isOnline()) + { + ((Player)seller).sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.GREEN + player.getName() + ChatColor.AQUA + + " has just rented your " + claimType + " at " + + ChatColor.BLUE + "[" + sign.getWorld().getName() + ", X: " + sign.getBlockX() + ", Y: " + sign.getBlockY() + ", Z: " + + sign.getBlockZ() + "]" + ChatColor.AQUA + + " for " + ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural()); + } + else if(RealEstate.instance.config.cfgMailOffline && RealEstate.ess != null) + { + User u = RealEstate.ess.getUser(this.owner); + u.addMail(RealEstate.instance.config.chatPrefix + ChatColor.GREEN + player.getName() + ChatColor.AQUA + + " has just rented your " + claimType + " at " + + ChatColor.BLUE + "[" + sign.getWorld().getName() + ", X: " + sign.getBlockX() + ", Y: " + sign.getBlockY() + ", Z: " + + sign.getBlockZ() + "]" + ChatColor.AQUA + + " for " + ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural()); + } + + player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + "You have successfully rented this " + claimType + + " for " + ChatColor.GREEN + price + RealEstate.econ.currencyNamePlural()); + + destroySign(); + } + } + + @Override + public void preview(Player player) + { + Claim claim = GriefPrevention.instance.dataStore.getClaimAt(sign, false, null); + String msg = ""; + if(player.hasPermission("realestate.info")) + { + String claimType = claim.parent == null ? "claim" : "subclaim"; + msg = ChatColor.BLUE + "-----= " + ChatColor.WHITE + "[" + ChatColor.GOLD + "RealEstate Rent Info" + ChatColor.WHITE + "]" + + ChatColor.BLUE + " =-----\n"; + if(buyer == null) + { + msg += ChatColor.AQUA + "This " + claimType + " is for rent for " + + ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural() + ChatColor.AQUA + " for " + + (maxPeriod > 1 ? "" + ChatColor.GREEN + maxPeriod + ChatColor.AQUA + " periods of " : "") + + ChatColor.GREEN + Utils.getTime(duration, null, true) + "\n"; + + if(claimType.equalsIgnoreCase("claim")) + { + msg += ChatColor.AQUA + "The current owner is: " + ChatColor.GREEN + claim.getOwnerName(); + } + else + { + msg += ChatColor.AQUA + "The main claim owner is: " + ChatColor.GREEN + claim.getOwnerName() + "\n"; + msg += ChatColor.LIGHT_PURPLE + "Note: " + ChatColor.AQUA + "You will only rent access to this subclaim!"; + } + } + else + { + int days = Period.between(startDate.toLocalDate(), LocalDate.now()).getDays(); + Duration hours = Duration.between(startDate.toLocalTime(), LocalTime.now()); + if(hours.isNegative() && !hours.isZero()) + { + hours = hours.plusHours(24); + days--; + } + int daysLeft = duration - days - 1;// we need to remove the current day + Duration timeRemaining = Duration.ofHours(24).minus(hours); + + msg += ChatColor.AQUA + "This " + claimType + " is currently rented by " + + ChatColor.GREEN + Bukkit.getOfflinePlayer(buyer).getName() + ChatColor.AQUA + " for " + + ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural() + ChatColor.AQUA + " for " + + (maxPeriod - periodCount > 1 ? "" + ChatColor.GREEN + (maxPeriod - periodCount) + ChatColor.AQUA + " periods of " + + ChatColor.GREEN + Utils.getTime(duration, null, false) + ChatColor.AQUA + ". The current period will end in " : "another ") + + ChatColor.GREEN + Utils.getTime(daysLeft, timeRemaining, true) + "\n"; + if((owner.equals(player.getUniqueId()) || buyer.equals(player.getUniqueId())) && RealEstate.instance.config.cfgEnableAutoRenew) + { + msg += ChatColor.AQUA + "Automatic renew is currently " + ChatColor.LIGHT_PURPLE + (autoRenew ? "enabled" : "disabled") + "\n"; + } + if(claimType.equalsIgnoreCase("claim")) + { + msg += ChatColor.AQUA + "The current owner is: " + ChatColor.GREEN + claim.getOwnerName(); + } + else + { + msg += ChatColor.AQUA + "The main claim owner is: " + ChatColor.GREEN + claim.getOwnerName(); + } + } + } + else + { + msg = RealEstate.instance.config.chatPrefix + ChatColor.RED + "You don't have the permission to view real estate informations!"; + } + player.sendMessage(msg); + } + +} diff --git a/src/me/EtienneDx/RealEstate/Transactions/ClaimSell.java b/src/me/EtienneDx/RealEstate/Transactions/ClaimSell.java index def3459..a378a8d 100644 --- a/src/me/EtienneDx/RealEstate/Transactions/ClaimSell.java +++ b/src/me/EtienneDx/RealEstate/Transactions/ClaimSell.java @@ -115,7 +115,7 @@ public class ClaimSell extends ClaimTransaction "Z: " + player.getLocation().getBlockZ() + "] " + "Price: " + price + " " + RealEstate.econ.currencyNamePlural()); - if(RealEstate.instance.config.cfgMessageOwner) + if(RealEstate.instance.config.cfgMessageOwner && owner != null) { OfflinePlayer oldOwner = Bukkit.getOfflinePlayer(owner); if(oldOwner.isOnline()) diff --git a/src/me/EtienneDx/RealEstate/Transactions/TransactionsStore.java b/src/me/EtienneDx/RealEstate/Transactions/TransactionsStore.java index 64f9af7..20f6ba8 100644 --- a/src/me/EtienneDx/RealEstate/Transactions/TransactionsStore.java +++ b/src/me/EtienneDx/RealEstate/Transactions/TransactionsStore.java @@ -1,273 +1,294 @@ -package me.EtienneDx.RealEstate.Transactions; - -import java.io.File; -import java.io.IOException; -import java.text.DateFormat; -import java.text.SimpleDateFormat; -import java.util.Collection; -import java.util.Date; -import java.util.HashMap; - -import org.bukkit.Bukkit; -import org.bukkit.Location; -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.entity.Player; -import org.bukkit.scheduler.BukkitRunnable; - -import me.EtienneDx.RealEstate.RealEstate; -import me.ryanhamshire.GriefPrevention.Claim; -import me.ryanhamshire.GriefPrevention.GriefPrevention; -import net.md_5.bungee.api.ChatColor; - -public class TransactionsStore -{ - public final String dataFilePath = RealEstate.pluginDirPath + "transactions.data"; - DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); - Date date = new Date(); - - public HashMap claimSell; - public HashMap claimRent; - public HashMap claimLease; - - public TransactionsStore() - { - loadData(); - new BukkitRunnable() - { - - @Override - public void run() - { - Collection col = claimRent.values();// need intermediate since some may get removed in the process - for(ClaimRent cr : col) - { - cr.update(); - } - Collection co = claimLease.values();// need intermediate since some may get removed in the process - for(ClaimLease cl : co) - { - cl.update(); - } - } - }.runTaskTimer(RealEstate.instance, 0, 1200L);// run every 60 seconds - } - - public void loadData() - { - claimSell = new HashMap<>(); - claimRent = new HashMap<>(); - claimLease = new HashMap<>(); - - FileConfiguration config = YamlConfiguration.loadConfiguration(new File(this.dataFilePath)); - for(String key : config.getKeys(true)) - { - if(key.startsWith("Sell.")) - { - ClaimSell cs = (ClaimSell)config.get(key); - claimSell.put(key.substring(5), cs); - } - else if(key.startsWith("Rent.")) - { - ClaimRent cr = (ClaimRent)config.get(key); - claimRent.put(key.substring(5), cr); - } - else if(key.startsWith("Lease.")) - { - ClaimLease cl = (ClaimLease)config.get(key); - claimLease.put(key.substring(6), cl); - } - } - } - - public void saveData() - { - YamlConfiguration config = new YamlConfiguration(); - for (ClaimSell cs : claimSell.values()) - config.set("Sell." + cs.claimId, cs); - for (ClaimRent cr : claimRent.values()) - config.set("Rent." + cr.claimId, cr); - for (ClaimLease cl : claimLease.values()) - config.set("Lease." + cl.claimId, cl); - try - { - config.save(new File(this.dataFilePath)); - } - catch (IOException e) - { - RealEstate.instance.log.info("Unable to write to the data file at \"" + this.dataFilePath + "\""); - } - } - - public boolean anyTransaction(Claim claim) - { - return claim != null && - (claimSell.containsKey(claim.getID().toString()) || - claimRent.containsKey(claim.getID().toString()) || - claimLease.containsKey(claim.getID().toString())); - } - - public Transaction getTransaction(Claim claim) - { - if(claimSell.containsKey(claim.getID().toString())) - return claimSell.get(claim.getID().toString()); - if(claimRent.containsKey(claim.getID().toString())) - return claimRent.get(claim.getID().toString()); - if(claimLease.containsKey(claim.getID().toString())) - return claimLease.get(claim.getID().toString()); - return null; - } - - public void cancelTransaction(Claim claim) - { - if(anyTransaction(claim)) - { - Transaction tr = getTransaction(claim); - cancelTransaction(tr); - } - saveData(); - } - - public void cancelTransaction(Transaction tr) - { - if(tr.getHolder() != null) - tr.getHolder().breakNaturally(); - if(tr instanceof ClaimSell) - { - claimSell.remove(String.valueOf(((ClaimSell) tr).claimId)); - } - if(tr instanceof ClaimRent) - { - claimRent.remove(String.valueOf(((ClaimRent) tr).claimId)); - } - if(tr instanceof ClaimLease) - { - claimLease.remove(String.valueOf(((ClaimLease) tr).claimId)); - } - saveData(); - } - - public boolean canCancelTransaction(Transaction tr) - { - return tr instanceof ClaimSell || (tr instanceof ClaimRent && ((ClaimRent)tr).buyer == null) || - (tr instanceof ClaimLease && ((ClaimLease)tr).buyer == null); - } - - public void sell(Claim claim, Player player, double price, Location sign) - { - ClaimSell cs = new ClaimSell(claim, player, price, sign); - claimSell.put(claim.getID().toString(), cs); - cs.update(); - saveData(); - - RealEstate.instance.addLogEntry("[" + this.dateFormat.format(this.date) + "] " + player.getName() + - " has made " + (claim.isAdminClaim() ? "an admin" : "a") + " " + (claim.parent == null ? "claim" : "subclaim") + " for sale at " + - "[" + player.getLocation().getWorld() + ", " + - "X: " + player.getLocation().getBlockX() + ", " + - "Y: " + player.getLocation().getBlockY() + ", " + - "Z: " + player.getLocation().getBlockZ() + "] " + - "Price: " + price + " " + RealEstate.econ.currencyNamePlural()); - - if(player != null) - { - player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + "You have successfully created " + - (claim.isAdminClaim() ? "an admin" : "a") + " " + (claim.parent == null ? "claim" : "subclaim") + " sale for " + - ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural()); - } - if(RealEstate.instance.config.cfgBroadcastSell) - { - for(Player p : Bukkit.getServer().getOnlinePlayers()) - { - if(p != player) - { - p.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.DARK_GREEN + player.getDisplayName() + - ChatColor.AQUA + " has put " + - (claim.isAdminClaim() ? "an admin" : "a") + " " + (claim.parent == null ? "claim" : "subclaim") + " for sale for " + - ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural()); - } - } - } - } - - public void rent(Claim claim, Player player, double price, Location sign, int duration, int rentPeriods) - { - ClaimRent cr = new ClaimRent(claim, player, price, sign, duration, rentPeriods); - claimRent.put(claim.getID().toString(), cr); - cr.update(); - saveData(); - - RealEstate.instance.addLogEntry("[" + this.dateFormat.format(this.date) + "] " + player.getName() + - " has made " + (claim.isAdminClaim() ? "an admin" : "a") + " " + (claim.parent == null ? "claim" : "subclaim") + " for rent at " + - "[" + player.getLocation().getWorld() + ", " + - "X: " + player.getLocation().getBlockX() + ", " + - "Y: " + player.getLocation().getBlockY() + ", " + - "Z: " + player.getLocation().getBlockZ() + "] " + - "Price: " + price + " " + RealEstate.econ.currencyNamePlural()); - - if(player != null) - { - player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + "You have successfully put " + - (claim.isAdminClaim() ? "an admin" : "a") + " " + (claim.parent == null ? "claim" : "subclaim") + " for rent for " + - ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural()); - } - if(RealEstate.instance.config.cfgBroadcastSell) - { - for(Player p : Bukkit.getServer().getOnlinePlayers()) - { - if(p != player) - { - p.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.DARK_GREEN + player.getDisplayName() + - ChatColor.AQUA + " has put " + - (claim.isAdminClaim() ? "an admin" : "a") + " " + (claim.parent == null ? "claim" : "subclaim") + " for rent for " + - ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural()); - } - } - } - } - - public void lease(Claim claim, Player player, double price, Location sign, int frequency, int paymentsCount) - { - ClaimLease cl = new ClaimLease(claim, player, price, sign, frequency, paymentsCount); - claimLease.put(claim.getID().toString(), cl); - cl.update(); - saveData(); - - RealEstate.instance.addLogEntry("[" + this.dateFormat.format(this.date) + "] " + player.getName() + - " has made " + (claim.isAdminClaim() ? "an admin" : "a") + " " + (claim.parent == null ? "claim" : "subclaim") + " for lease at " + - "[" + player.getLocation().getWorld() + ", " + - "X: " + player.getLocation().getBlockX() + ", " + - "Y: " + player.getLocation().getBlockY() + ", " + - "Z: " + player.getLocation().getBlockZ() + "] " + - "Payments Count : " + paymentsCount + " " + - "Price: " + price + " " + RealEstate.econ.currencyNamePlural()); - - if(player != null) - { - player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + "You have successfully put " + - (claim.isAdminClaim() ? "an admin" : "a") + " " + (claim.parent == null ? "claim" : "subclaim") + " for lease for " + - ChatColor.GREEN + paymentsCount + ChatColor.AQUA + " payments of " + - ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural()); - } - if(RealEstate.instance.config.cfgBroadcastSell) - { - for(Player p : Bukkit.getServer().getOnlinePlayers()) - { - if(p != player) - { - p.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.DARK_GREEN + player.getDisplayName() + - ChatColor.AQUA + " has put " + - (claim.isAdminClaim() ? "an admin" : "a") + " " + (claim.parent == null ? "claim" : "subclaim") + " for lease for " + - ChatColor.GREEN + paymentsCount + ChatColor.AQUA + " payments of " + - ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural()); - } - } - } - } - - public Transaction getTransaction(Player player) - { - if(player == null) return null; - Claim c = GriefPrevention.instance.dataStore.getClaimAt(player.getLocation(), false, null); - return getTransaction(c); - } -} +package me.EtienneDx.RealEstate.Transactions; + +import java.io.File; +import java.io.IOException; +import java.nio.file.FileSystem; +import java.nio.file.FileSystems; +import java.nio.file.Files; +import java.nio.file.Path; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Collection; +import java.util.Date; +import java.util.HashMap; + +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.entity.Player; +import org.bukkit.scheduler.BukkitRunnable; +import org.bukkit.util.FileUtil; + +import me.EtienneDx.RealEstate.RealEstate; +import me.ryanhamshire.GriefPrevention.Claim; +import me.ryanhamshire.GriefPrevention.GriefPrevention; +import net.md_5.bungee.api.ChatColor; + +public class TransactionsStore +{ + public final String dataFilePath = RealEstate.pluginDirPath + "transactions.data"; + DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); + Date date = new Date(); + + public HashMap claimSell; + public HashMap claimRent; + public HashMap claimLease; + + public TransactionsStore() + { + loadData(); + new BukkitRunnable() + { + + @Override + public void run() + { + Collection col = claimRent.values();// need intermediate since some may get removed in the process + for(ClaimRent cr : col) + { + cr.update(); + } + Collection co = claimLease.values();// need intermediate since some may get removed in the process + for(ClaimLease cl : co) + { + cl.update(); + } + } + }.runTaskTimer(RealEstate.instance, 0, 1200L);// run every 60 seconds + } + + public void loadData() + { + claimSell = new HashMap<>(); + claimRent = new HashMap<>(); + claimLease = new HashMap<>(); + + File file = new File(this.dataFilePath); + + FileConfiguration config = YamlConfiguration.loadConfiguration(file); + try { + RealEstate.instance.addLogEntry(new String(Files.readAllBytes(FileSystems.getDefault().getPath(this.dataFilePath)))); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + ConfigurationSection sell = config.getConfigurationSection("Sell"); + ConfigurationSection rent = config.getConfigurationSection("Rent"); + ConfigurationSection lease = config.getConfigurationSection("Lease"); + if(sell != null) + { + RealEstate.instance.addLogEntry(sell.toString()); + RealEstate.instance.addLogEntry(sell.getKeys(false).size() + ""); + for(String key : sell.getKeys(false)) + { + ClaimSell cs = (ClaimSell)sell.get(key); + claimSell.put(key, cs); + } + } + if(rent != null) + for(String key : rent.getKeys(false)) + { + ClaimRent cr = (ClaimRent)rent.get(key); + claimRent.put(key, cr); + } + if(lease != null) + for(String key : lease.getKeys(false)) + { + ClaimLease cl = (ClaimLease)lease.get(key); + claimLease.put(key, cl); + } + } + + public void saveData() + { + YamlConfiguration config = new YamlConfiguration(); + for (ClaimSell cs : claimSell.values()) + config.set("Sell." + cs.claimId, cs); + for (ClaimRent cr : claimRent.values()) + config.set("Rent." + cr.claimId, cr); + for (ClaimLease cl : claimLease.values()) + config.set("Lease." + cl.claimId, cl); + try + { + config.save(new File(this.dataFilePath)); + } + catch (IOException e) + { + RealEstate.instance.log.info("Unable to write to the data file at \"" + this.dataFilePath + "\""); + } + } + + public boolean anyTransaction(Claim claim) + { + return claim != null && + (claimSell.containsKey(claim.getID().toString()) || + claimRent.containsKey(claim.getID().toString()) || + claimLease.containsKey(claim.getID().toString())); + } + + public Transaction getTransaction(Claim claim) + { + if(claimSell.containsKey(claim.getID().toString())) + return claimSell.get(claim.getID().toString()); + if(claimRent.containsKey(claim.getID().toString())) + return claimRent.get(claim.getID().toString()); + if(claimLease.containsKey(claim.getID().toString())) + return claimLease.get(claim.getID().toString()); + return null; + } + + public void cancelTransaction(Claim claim) + { + if(anyTransaction(claim)) + { + Transaction tr = getTransaction(claim); + cancelTransaction(tr); + } + saveData(); + } + + public void cancelTransaction(Transaction tr) + { + if(tr.getHolder() != null) + tr.getHolder().breakNaturally(); + if(tr instanceof ClaimSell) + { + claimSell.remove(String.valueOf(((ClaimSell) tr).claimId)); + } + if(tr instanceof ClaimRent) + { + claimRent.remove(String.valueOf(((ClaimRent) tr).claimId)); + } + if(tr instanceof ClaimLease) + { + claimLease.remove(String.valueOf(((ClaimLease) tr).claimId)); + } + saveData(); + } + + public boolean canCancelTransaction(Transaction tr) + { + return tr instanceof ClaimSell || (tr instanceof ClaimRent && ((ClaimRent)tr).buyer == null) || + (tr instanceof ClaimLease && ((ClaimLease)tr).buyer == null); + } + + public void sell(Claim claim, Player player, double price, Location sign) + { + ClaimSell cs = new ClaimSell(claim, player, price, sign); + claimSell.put(claim.getID().toString(), cs); + cs.update(); + saveData(); + + RealEstate.instance.addLogEntry("[" + this.dateFormat.format(this.date) + "] " + (player == null ? "The Server" : player.getName()) + + " has made " + (claim.isAdminClaim() ? "an admin" : "a") + " " + (claim.parent == null ? "claim" : "subclaim") + " for sale at " + + "[" + claim.getGreaterBoundaryCorner().getWorld() + ", " + + "X: " + claim.getGreaterBoundaryCorner().getBlockX() + ", " + + "Y: " + claim.getGreaterBoundaryCorner().getBlockY() + ", " + + "Z: " + claim.getGreaterBoundaryCorner().getBlockZ() + "] " + + "Price: " + price + " " + RealEstate.econ.currencyNamePlural()); + + if(player != null) + { + player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + "You have successfully created " + + (claim.isAdminClaim() ? "an admin" : "a") + " " + (claim.parent == null ? "claim" : "subclaim") + " sale for " + + ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural()); + } + if(RealEstate.instance.config.cfgBroadcastSell) + { + for(Player p : Bukkit.getServer().getOnlinePlayers()) + { + if(p != player) + { + p.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.DARK_GREEN + (player == null ? "The Server" : player.getDisplayName()) + + ChatColor.AQUA + " has put " + + (claim.isAdminClaim() ? "an admin" : "a") + " " + (claim.parent == null ? "claim" : "subclaim") + " for sale for " + + ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural()); + } + } + } + } + + public void rent(Claim claim, Player player, double price, Location sign, int duration, int rentPeriods) + { + ClaimRent cr = new ClaimRent(claim, player, price, sign, duration, rentPeriods); + claimRent.put(claim.getID().toString(), cr); + cr.update(); + saveData(); + + RealEstate.instance.addLogEntry("[" + this.dateFormat.format(this.date) + "] " + (player == null ? "The Server" : player.getName()) + + " has made " + (claim.isAdminClaim() ? "an admin" : "a") + " " + (claim.parent == null ? "claim" : "subclaim") + " for rent at " + + "[" + claim.getLesserBoundaryCorner().getWorld() + ", " + + "X: " + claim.getLesserBoundaryCorner().getBlockX() + ", " + + "Y: " + claim.getLesserBoundaryCorner().getBlockY() + ", " + + "Z: " + claim.getLesserBoundaryCorner().getBlockZ() + "] " + + "Price: " + price + " " + RealEstate.econ.currencyNamePlural()); + + if(player != null) + { + player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + "You have successfully put " + + (claim.isAdminClaim() ? "an admin" : "a") + " " + (claim.parent == null ? "claim" : "subclaim") + " for rent for " + + ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural()); + } + if(RealEstate.instance.config.cfgBroadcastSell) + { + for(Player p : Bukkit.getServer().getOnlinePlayers()) + { + if(p != player) + { + p.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.DARK_GREEN + (player == null ? "The Server" : player.getDisplayName()) + + ChatColor.AQUA + " has put " + + (claim.isAdminClaim() ? "an admin" : "a") + " " + (claim.parent == null ? "claim" : "subclaim") + " for rent for " + + ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural()); + } + } + } + } + + public void lease(Claim claim, Player player, double price, Location sign, int frequency, int paymentsCount) + { + ClaimLease cl = new ClaimLease(claim, player, price, sign, frequency, paymentsCount); + claimLease.put(claim.getID().toString(), cl); + cl.update(); + saveData(); + + RealEstate.instance.addLogEntry("[" + this.dateFormat.format(this.date) + "] " + (player == null ? "The Server" : player.getName()) + + " has made " + (claim.isAdminClaim() ? "an admin" : "a") + " " + (claim.parent == null ? "claim" : "subclaim") + " for lease at " + + "[" + claim.getLesserBoundaryCorner().getWorld() + ", " + + "X: " + claim.getLesserBoundaryCorner().getBlockX() + ", " + + "Y: " + claim.getLesserBoundaryCorner().getBlockY() + ", " + + "Z: " + claim.getLesserBoundaryCorner().getBlockZ() + "] " + + "Payments Count : " + paymentsCount + " " + + "Price: " + price + " " + RealEstate.econ.currencyNamePlural()); + + if(player != null) + { + player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + "You have successfully put " + + (claim.isAdminClaim() ? "an admin" : "a") + " " + (claim.parent == null ? "claim" : "subclaim") + " for lease for " + + ChatColor.GREEN + paymentsCount + ChatColor.AQUA + " payments of " + + ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural()); + } + if(RealEstate.instance.config.cfgBroadcastSell) + { + for(Player p : Bukkit.getServer().getOnlinePlayers()) + { + if(p != player) + { + p.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.DARK_GREEN + (player == null ? "The Server" : player.getDisplayName()) + + ChatColor.AQUA + " has put " + + (claim.isAdminClaim() ? "an admin" : "a") + " " + (claim.parent == null ? "claim" : "subclaim") + " for lease for " + + ChatColor.GREEN + paymentsCount + ChatColor.AQUA + " payments of " + + ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural()); + } + } + } + } + + public Transaction getTransaction(Player player) + { + if(player == null) return null; + Claim c = GriefPrevention.instance.dataStore.getClaimAt(player.getLocation(), false, null); + return getTransaction(c); + } +} diff --git a/src/me/EtienneDx/RealEstate/Utils.java b/src/me/EtienneDx/RealEstate/Utils.java index 6dd9a04..9e1e94f 100644 --- a/src/me/EtienneDx/RealEstate/Utils.java +++ b/src/me/EtienneDx/RealEstate/Utils.java @@ -1,154 +1,157 @@ -package me.EtienneDx.RealEstate; - -import java.time.Duration; -import java.util.UUID; - -import org.bukkit.Bukkit; -import org.bukkit.OfflinePlayer; -import org.bukkit.entity.Player; - -import me.ryanhamshire.GriefPrevention.Claim; -import me.ryanhamshire.GriefPrevention.ClaimPermission; -import me.ryanhamshire.GriefPrevention.GriefPrevention; -import me.ryanhamshire.GriefPrevention.PlayerData; -import net.md_5.bungee.api.ChatColor; -import net.milkbowl.vault.economy.EconomyResponse; - -public class Utils -{ - public static boolean makePayment(UUID receiver, UUID giver, double amount, boolean msgSeller, boolean msgBuyer) - { - // seller might be null if it is the server - OfflinePlayer s = receiver != null ? Bukkit.getOfflinePlayer(receiver) : null, b = Bukkit.getOfflinePlayer(giver); - if(!RealEstate.econ.has(b, amount)) - { - if(b.isOnline() && msgBuyer) - { - ((Player)b).sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + - "You don't have enough money to make this transaction!"); - } - if(s != null && s.isOnline() && msgSeller) - { - ((Player)s).sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + - b.getName() + " doesn't have enough money to make this transaction!"); - } - return false; - } - EconomyResponse resp = RealEstate.econ.withdrawPlayer(b, amount); - if(!resp.transactionSuccess()) - { - if(b.isOnline() && msgBuyer) - { - ((Player)b).sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + - "Could not withdraw the money!"); - } - if(s != null && s.isOnline() && msgSeller) - { - ((Player)s).sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + - "Could not withdraw the money!"); - } - return false; - } - if(s != null) - { - resp = RealEstate.econ.depositPlayer(s, amount); - if(!resp.transactionSuccess()) - { - if(b.isOnline() && msgBuyer) - { - ((Player)b).sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + - "Could not deposit to " + s.getName() + ", refunding Player!"); - } - if(s != null && s.isOnline() && msgSeller) - { - ((Player)s).sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + - "Could not deposit to you, refunding" + b.getName() + "!"); - } - RealEstate.econ.depositPlayer(b, amount); - return false; - } - } - - return true; - } - - public static String getTime(int days, Duration hours, boolean details) - { - String time = ""; - if(days >= 7) - { - time += (days / 7) + " week" + (days >= 14 ? "s" : ""); - } - if(days % 7 > 0) - { - time += (time.isEmpty() ? "" : " ") + (days % 7) + " day" + (days % 7 > 1 ? "s" : ""); - } - if((details || days < 7) && hours != null && hours.toHours() > 0) - { - time += (time.isEmpty() ? "" : " ") + hours.toHours() + " hour" + (hours.toHours() > 1 ? "s" : ""); - } - if((details || days == 0) && hours != null && (time.isEmpty() || hours.toMinutes() % 60 > 0)) - { - time += (time.isEmpty() ? "" : " ") + (hours.toMinutes() % 60) + " min" + (hours.toMinutes() % 60 > 1 ? "s" : ""); - } - - return time; - } - - public static void transferClaim(Claim claim, UUID buyer, UUID seller) - { - // blocks transfer : - // if transfert is true, the seller will lose the blocks he had - // and the buyer will get them - // (that means the buyer will keep the same amount of remaining blocks after the transaction) - if(claim.parent == null && RealEstate.instance.config.cfgTransferClaimBlocks) - { - PlayerData buyerData = GriefPrevention.instance.dataStore.getPlayerData(buyer); - PlayerData sellerData = GriefPrevention.instance.dataStore.getPlayerData(seller); - - // the seller has to provide the blocks - sellerData.setBonusClaimBlocks(sellerData.getBonusClaimBlocks() - claim.getArea()); - if (sellerData.getBonusClaimBlocks() < 0)// can't have negative bonus claim blocks, so if need be, we take into the accrued - { - sellerData.setAccruedClaimBlocks(sellerData.getAccruedClaimBlocks() + sellerData.getBonusClaimBlocks()); - sellerData.setBonusClaimBlocks(0); - } - - // the buyer receive them - buyerData.setBonusClaimBlocks(buyerData.getBonusClaimBlocks() + claim.getArea()); - } - - // start to change owner - if(claim.parent == null) - for(Claim child : claim.children) - { - child.clearPermissions(); - child.managers.clear(); - } - claim.clearPermissions(); - - try - { - if(claim.parent == null) - GriefPrevention.instance.dataStore.changeClaimOwner(claim, buyer); - else - { - claim.setPermission(buyer.toString(), ClaimPermission.Build); - } - } - catch (Exception e)// error occurs when trying to change subclaim owner - { - e.printStackTrace(); - return; - } - GriefPrevention.instance.dataStore.saveClaim(claim); - - } - - public static String getSignString(String str) - { - if(str.length() > 16) - str = str.substring(0, 16); - return str; - } -} +package me.EtienneDx.RealEstate; + +import java.time.Duration; +import java.util.UUID; + +import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; +import org.bukkit.entity.Player; + +import me.ryanhamshire.GriefPrevention.Claim; +import me.ryanhamshire.GriefPrevention.ClaimPermission; +import me.ryanhamshire.GriefPrevention.GriefPrevention; +import me.ryanhamshire.GriefPrevention.PlayerData; +import net.md_5.bungee.api.ChatColor; +import net.milkbowl.vault.economy.EconomyResponse; + +public class Utils +{ + public static boolean makePayment(UUID receiver, UUID giver, double amount, boolean msgSeller, boolean msgBuyer) + { + // seller might be null if it is the server + OfflinePlayer s = receiver != null ? Bukkit.getOfflinePlayer(receiver) : null, b = Bukkit.getOfflinePlayer(giver); + if(!RealEstate.econ.has(b, amount)) + { + if(b.isOnline() && msgBuyer) + { + ((Player)b).sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + + "You don't have enough money to make this transaction!"); + } + if(s != null && s.isOnline() && msgSeller) + { + ((Player)s).sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + + b.getName() + " doesn't have enough money to make this transaction!"); + } + return false; + } + EconomyResponse resp = RealEstate.econ.withdrawPlayer(b, amount); + if(!resp.transactionSuccess()) + { + if(b.isOnline() && msgBuyer) + { + ((Player)b).sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + + "Could not withdraw the money!"); + } + if(s != null && s.isOnline() && msgSeller) + { + ((Player)s).sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + + "Could not withdraw the money!"); + } + return false; + } + if(s != null) + { + resp = RealEstate.econ.depositPlayer(s, amount); + if(!resp.transactionSuccess()) + { + if(b.isOnline() && msgBuyer) + { + ((Player)b).sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + + "Could not deposit to " + s.getName() + ", refunding Player!"); + } + if(s != null && s.isOnline() && msgSeller) + { + ((Player)s).sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + + "Could not deposit to you, refunding" + b.getName() + "!"); + } + RealEstate.econ.depositPlayer(b, amount); + return false; + } + } + + return true; + } + + public static String getTime(int days, Duration hours, boolean details) + { + String time = ""; + if(days >= 7) + { + time += (days / 7) + " week" + (days >= 14 ? "s" : ""); + } + if(days % 7 > 0) + { + time += (time.isEmpty() ? "" : " ") + (days % 7) + " day" + (days % 7 > 1 ? "s" : ""); + } + if((details || days < 7) && hours != null && hours.toHours() > 0) + { + time += (time.isEmpty() ? "" : " ") + hours.toHours() + " hour" + (hours.toHours() > 1 ? "s" : ""); + } + if((details || days == 0) && hours != null && (time.isEmpty() || hours.toMinutes() % 60 > 0)) + { + time += (time.isEmpty() ? "" : " ") + (hours.toMinutes() % 60) + " min" + (hours.toMinutes() % 60 > 1 ? "s" : ""); + } + + return time; + } + + public static void transferClaim(Claim claim, UUID buyer, UUID seller) + { + // blocks transfer : + // if transfert is true, the seller will lose the blocks he had + // and the buyer will get them + // (that means the buyer will keep the same amount of remaining blocks after the transaction) + if(claim.parent == null && RealEstate.instance.config.cfgTransferClaimBlocks) + { + PlayerData buyerData = GriefPrevention.instance.dataStore.getPlayerData(buyer); + if(seller != null) + { + PlayerData sellerData = GriefPrevention.instance.dataStore.getPlayerData(seller); + + // the seller has to provide the blocks + sellerData.setBonusClaimBlocks(sellerData.getBonusClaimBlocks() - claim.getArea()); + if (sellerData.getBonusClaimBlocks() < 0)// can't have negative bonus claim blocks, so if need be, we take into the accrued + { + sellerData.setAccruedClaimBlocks(sellerData.getAccruedClaimBlocks() + sellerData.getBonusClaimBlocks()); + sellerData.setBonusClaimBlocks(0); + } + } + + // the buyer receive them + buyerData.setBonusClaimBlocks(buyerData.getBonusClaimBlocks() + claim.getArea()); + } + + // start to change owner + if(claim.parent == null) + for(Claim child : claim.children) + { + child.clearPermissions(); + child.managers.clear(); + } + claim.clearPermissions(); + + try + { + if(claim.parent == null) + GriefPrevention.instance.dataStore.changeClaimOwner(claim, buyer); + else + { + claim.setPermission(buyer.toString(), ClaimPermission.Build); + } + } + catch (Exception e)// error occurs when trying to change subclaim owner + { + e.printStackTrace(); + return; + } + GriefPrevention.instance.dataStore.saveClaim(claim); + + } + + public static String getSignString(String str) + { + if(str.length() > 16) + str = str.substring(0, 16); + return str; + } +}