Skip to content

Commit

Permalink
Finished all basic features, fixed bugs, ready for beta0.1 release
Browse files Browse the repository at this point in the history
  • Loading branch information
EtienneDx committed Apr 27, 2019
1 parent 114deef commit 8e04b56
Show file tree
Hide file tree
Showing 11 changed files with 785 additions and 95 deletions.
16 changes: 10 additions & 6 deletions plugin.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
name: RealEstate
main: me.EtienneDx.RealEstate.RealEstate
version: 0.1
version: beta0.1
authors: [EtienneDx]
depend: [GriefPrevention, Vault]

commands:
re:
description: Access to the GriefProtection_RealEstate informations.
usage: /<command> ?
description: Access to the claim transaction information.
usage: /<command>
aliases: [realestate]
permission: realestate.info
permission-message: You do not have access to that command!
Expand All @@ -28,6 +28,7 @@ permissions:
realestate.subclaim.sell: true
realestate.subclaim.rent: true
realestate.subclaim.lease: true
realestate.autorenew: true
realestate.claim.*:
description: Allows the player full access over claims
default: op
Expand Down Expand Up @@ -67,13 +68,16 @@ permissions:
default: true
realestate.subclaim.sell:
description: Allows the player to sell subclaims
default: true
default: false
realestate.subclaim.rent:
description: Allows the player to rent subclaims
default: true
realestate.subclaim.lease:
description: Allows the player to lease subclaims
default: true
default: false
realestate.destroysigns:
description: Allows the player to destroy any sign representing a transaction
default: op
default: op
realestate.autorenew:
description: Allows the player to enable automatic renew for his rented claims and subclaims
default: true
337 changes: 337 additions & 0 deletions src/me/EtienneDx/RealEstate/ClaimLease.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,337 @@
package me.EtienneDx.RealEstate;

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

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;

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 ClaimTransaction
{
LocalDateTime lastPayment = null;
int frequency;
int paymentsLeft;
UUID buyer = null;

public ClaimLease(Map<String, Object> 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");
if(map.get("buyer") != null)
buyer = UUID.fromString((String)map.get("buyer"));
}

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<String, Object> serialize() {
Map<String, Object> map = super.serialize();

if(lastPayment != null)
map.put("lastPayment", lastPayment.format(DateTimeFormatter.ISO_DATE_TIME));
map.put("frequency", frequency);
map.put("paymentsLeft", paymentsLeft);
if(buyer != null)
map.put("buyer", buyer.toString());

return map;
}

@Override
public void update()
{
if(buyer == null)// not yet leased
{
if(sign.getBlock().getState() instanceof Sign)
{
Sign s = (Sign)sign.getBlock().getState();
s.setLine(0, RealEstate.instance.dataStore.cfgSignsHeader);
s.setLine(1, ChatColor.DARK_GREEN + RealEstate.instance.dataStore.cfgReplaceLease);
//s.setLine(2, owner != null ? Bukkit.getOfflinePlayer(owner).getName() : "SERVER");
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.dataStore.cfgMessageBuyer)
{
((Player)buyerPlayer).sendMessage(RealEstate.instance.dataStore.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.dataStore.cfgMessageOwner)
{
((Player)seller).sendMessage(RealEstate.instance.dataStore.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.dataStore.cfgMessageBuyer)
{
((Player)buyerPlayer).sendMessage(RealEstate.instance.dataStore.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.dataStore.cfgMessageOwner)
{
((Player)seller).sendMessage(RealEstate.instance.dataStore.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.dataStore.cfgMessageBuyer)
{
((Player)buyerPlayer).sendMessage(RealEstate.instance.dataStore.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.dataStore.cfgMessageOwner)
{
((Player)seller).sendMessage(RealEstate.instance.dataStore.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.dataStore.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.dataStore.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.dataStore.chatPrefix + ChatColor.RED + "You already own this " + claimType + "!");
return;
}
if(claim.parent == null && !owner.equals(claim.ownerID))
{
player.sendMessage(RealEstate.instance.dataStore.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.dataStore.chatPrefix + ChatColor.RED + "You do not have the permission to lease " +
claimType + "s!");
return;
}
if(player.getUniqueId().equals(buyer))
{
player.sendMessage(RealEstate.instance.dataStore.chatPrefix + ChatColor.RED + "You are already leasing this " +
claimType + "!");
return;
}
if(buyer != null)
{
player.sendMessage(RealEstate.instance.dataStore.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);
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.dataStore.cfgMessageOwner && seller.isOnline())
{
((Player)seller).sendMessage(RealEstate.instance.dataStore.chatPrefix + ChatColor.GREEN + player.getName() + ChatColor.AQUA +
" has just paid for your lease for the " + claimType + " at " +
"[" + sign.getWorld().getName() + ", " +
"X: " + sign.getBlockX() + ", " +
"Y: " + sign.getBlockY() + ", " +
"Z: " + sign.getBlockZ() + "] " +
" for " + ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural() + ChatColor.AQUA + ", " +
ChatColor.GREEN + paymentsLeft + ChatColor.AQUA + " payments left");
}

player.sendMessage(RealEstate.instance.dataStore.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.dataStore.chatPrefix + ChatColor.RED + "You don't have the permission to view real estate informations!";
}
player.sendMessage(msg);
}

}
Loading

0 comments on commit 8e04b56

Please sign in to comment.