Skip to content

Commit

Permalink
fixes for admin claims renting and leasing
Browse files Browse the repository at this point in the history
  • Loading branch information
EtienneDx committed Aug 27, 2019
1 parent f25693c commit 44f64dd
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public Map<String, Object> serialize()

public void destroySign()
{
if((this instanceof ClaimRent &&RealEstate.instance.config.cfgDestroyRentSigns) ||
(this instanceof ClaimLease &&RealEstate.instance.config.cfgDestroyLeaseSigns))
if((this instanceof ClaimRent && RealEstate.instance.config.cfgDestroyRentSigns) ||
(this instanceof ClaimLease && RealEstate.instance.config.cfgDestroyLeaseSigns))
{
if(!destroyedSign && getHolder().getState() instanceof Sign)
getHolder().breakNaturally();
Expand Down
57 changes: 31 additions & 26 deletions src/me/EtienneDx/RealEstate/Transactions/ClaimLease.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,11 @@ public Map<String, Object> serialize() {
}

@Override
public void update()
public boolean update()
{
if(buyer == null)// not yet leased
{
if(destroyedSign)
{
RealEstate.transactionsStore.cancelTransaction(this);
}
else if(sign.getBlock().getState() instanceof Sign)
if(sign.getBlock().getState() instanceof Sign)
{
Sign s = (Sign)sign.getBlock().getState();
s.setLine(0, RealEstate.instance.config.cfgSignsHeader);
Expand All @@ -83,6 +79,11 @@ else if(sign.getBlock().getState() instanceof Sign)
s.setLine(3, Utils.getTime(frequency, null, false));
s.update(true);
}
else
{
return true;
}

}
else
{
Expand All @@ -98,6 +99,7 @@ else if(sign.getBlock().getState() instanceof Sign)
payLease();
}
}
return false;
}

private void payLease()
Expand Down Expand Up @@ -271,12 +273,12 @@ public void interact(Player player)
}
String claimType = claim.parent == null ? "claim" : "subclaim";

if (owner.equals(player.getUniqueId()))
if (owner != null && 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))
if(claim.parent == null && owner != 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!");
Expand Down Expand Up @@ -323,26 +325,29 @@ public void interact(Player player)
"Z: " + player.getLocation().getBlockZ() + "] " +
"Price: " + price + " " + RealEstate.econ.currencyNamePlural());

OfflinePlayer seller = Bukkit.getOfflinePlayer(owner);
if(RealEstate.instance.config.cfgMessageOwner && seller.isOnline())
if(owner != null)
{
((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");
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");
}
}
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 + ", " +
Expand Down
55 changes: 30 additions & 25 deletions src/me/EtienneDx/RealEstate/Transactions/ClaimRent.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,11 @@ public Map<String, Object> serialize() {
}

@Override
public void update()
public boolean update()
{
if(buyer == null)
{
if(destroyedSign)
{
RealEstate.transactionsStore.cancelTransaction(this);
}
else if(sign.getBlock().getState() instanceof Sign)
if(sign.getBlock().getState() instanceof Sign)
{
Sign s = (Sign) sign.getBlock().getState();
s.setLine(0, RealEstate.instance.config.cfgSignsHeader);
Expand All @@ -88,6 +84,10 @@ else if(sign.getBlock().getState() instanceof Sign)
s.setLine(3, (maxPeriod > 1 ? maxPeriod + "x " : "") + Utils.getTime(duration, null, false));
s.update(true);
}
else
{
return true;
}
}
else
{
Expand Down Expand Up @@ -117,6 +117,7 @@ else if(sign.getBlock().getState() instanceof Sign)
s.update(true);
}
}
return false;

}

Expand Down Expand Up @@ -247,12 +248,12 @@ public void interact(Player player)
}
String claimType = claim.parent == null ? "claim" : "subclaim";

if (owner.equals(player.getUniqueId()))
if (owner != null && 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))
if(claim.parent == null && owner != 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 + "!");
Expand Down Expand Up @@ -298,24 +299,28 @@ public void interact(Player player)
"Z: " + player.getLocation().getBlockZ() + "] " +
"Price: " + price + " " + RealEstate.econ.currencyNamePlural());

OfflinePlayer seller = Bukkit.getOfflinePlayer(owner);
if(RealEstate.instance.config.cfgMessageOwner && seller.isOnline())
if(owner != null)
{
((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());
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());
}
}
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());
Expand Down Expand Up @@ -369,7 +374,7 @@ public void preview(Player player)
(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)
if((owner != null && 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";
}
Expand Down
3 changes: 2 additions & 1 deletion src/me/EtienneDx/RealEstate/Transactions/ClaimSell.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public ClaimSell(Map<String, Object> map)
}

@Override
public void update()
public boolean update()
{
if(sign.getBlock().getState() instanceof Sign)
{
Expand All @@ -53,6 +53,7 @@ public void update()
{
RealEstate.transactionsStore.cancelTransaction(this);
}
return false;
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/me/EtienneDx/RealEstate/Transactions/Transaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public interface Transaction
public void setOwner(UUID newOwner);
public void interact(Player player);
public void preview(Player player);
public void update();
public boolean update();
public boolean tryCancelTransaction(Player p);
public void msgInfo(Player player);
}
34 changes: 22 additions & 12 deletions src/me/EtienneDx/RealEstate/Transactions/TransactionsStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

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 java.util.Iterator;

import org.bukkit.Bukkit;
import org.bukkit.Location;
Expand All @@ -19,8 +17,6 @@
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;
Expand All @@ -45,16 +41,30 @@ public TransactionsStore()
@Override
public void run()
{
Collection<ClaimRent> col = claimRent.values();// need intermediate since some may get removed in the process
for(ClaimRent cr : col)
//Collection<ClaimRent> col = claimRent.values();// need intermediate since some may get removed in the process
Iterator<ClaimRent> ite = claimRent.values().iterator();
int i = 0;
while(ite.hasNext())
{
cr.update();
if(ite.next().update())
ite.remove();
}
Collection<ClaimLease> co = claimLease.values();// need intermediate since some may get removed in the process
/*for(ClaimRent cr : col)
{
cr.update();
}*/
/*Collection<ClaimLease> co = claimLease.values();// need intermediate since some may get removed in the process
for(ClaimLease cl : co)
{
cl.update();
}*/
Iterator<ClaimLease> it = claimLease.values().iterator();
while(it.hasNext())
{
if(it.next().update())
it.remove();
}
saveData();
}
}.runTaskTimer(RealEstate.instance, 0, 1200L);// run every 60 seconds
}
Expand Down Expand Up @@ -176,7 +186,7 @@ public boolean canCancelTransaction(Transaction tr)

public void sell(Claim claim, Player player, double price, Location sign)
{
ClaimSell cs = new ClaimSell(claim, player, price, sign);
ClaimSell cs = new ClaimSell(claim, claim.isAdminClaim() ? null : player, price, sign);
claimSell.put(claim.getID().toString(), cs);
cs.update();
saveData();
Expand Down Expand Up @@ -212,7 +222,7 @@ public void sell(Claim claim, Player player, double price, Location sign)

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 cr = new ClaimRent(claim, claim.isAdminClaim() ? null : player, price, sign, duration, rentPeriods);
claimRent.put(claim.getID().toString(), cr);
cr.update();
saveData();
Expand Down Expand Up @@ -248,7 +258,7 @@ public void rent(Claim claim, Player player, double price, Location sign, int du

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 cl = new ClaimLease(claim, claim.isAdminClaim() ? null : player, price, sign, frequency, paymentsCount);
claimLease.put(claim.getID().toString(), cl);
cl.update();
saveData();
Expand Down

0 comments on commit 44f64dd

Please sign in to comment.