Skip to content

Commit

Permalink
Created the exitoffer command
Browse files Browse the repository at this point in the history
  • Loading branch information
EtienneDx committed Apr 28, 2019
1 parent 8e04b56 commit 9578ebb
Show file tree
Hide file tree
Showing 9 changed files with 280 additions and 52 deletions.
46 changes: 46 additions & 0 deletions src/me/EtienneDx/RealEstate/BoughtTransaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package me.EtienneDx.RealEstate;

import java.util.Map;
import java.util.UUID;

import org.bukkit.Location;
import org.bukkit.entity.Player;

import me.ryanhamshire.GriefPrevention.Claim;

public abstract class BoughtTransaction extends ClaimTransaction
{
UUID buyer = null;
ExitOffer exitOffer = null;

public BoughtTransaction(Map<String, Object> map)
{
super(map);
if(map.get("buyer") != null)
buyer = UUID.fromString((String)map.get("buyer"));
if(map.get("exitOffer") != null)
exitOffer = (ExitOffer) map.get("exitOffer");
}

public BoughtTransaction(Claim claim, Player player, double price, Location sign)
{
super(claim, player, price, sign);
}

@Override
public Map<String, Object> serialize()
{
Map<String, Object> map = super.serialize();
if(buyer != null)
map.put("buyer", buyer.toString());
if(exitOffer != null)
map.put("exitOffer", exitOffer);

return map;
}

public UUID getBuyer()
{
return buyer;
}
}
13 changes: 5 additions & 8 deletions src/me/EtienneDx/RealEstate/ClaimLease.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
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;
Expand All @@ -20,12 +18,11 @@
import me.ryanhamshire.GriefPrevention.GriefPrevention;
import net.md_5.bungee.api.ChatColor;

public class ClaimLease extends ClaimTransaction
public class ClaimLease extends BoughtTransaction
{
LocalDateTime lastPayment = null;
int frequency;
int paymentsLeft;
UUID buyer = null;

public ClaimLease(Map<String, Object> map)
{
Expand All @@ -34,8 +31,6 @@ public ClaimLease(Map<String, Object> map)
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)
Expand All @@ -53,8 +48,6 @@ public Map<String, Object> serialize() {
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;
}
Expand All @@ -74,6 +67,10 @@ public void update()
s.setLine(3, Utils.getTime(frequency, null, false));
s.update(true);
}
else
{
RealEstate.transactionsStore.cancelTransaction(this);
}
}
else
{
Expand Down
62 changes: 28 additions & 34 deletions src/me/EtienneDx/RealEstate/ClaimRent.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
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;
Expand All @@ -20,11 +18,10 @@
import me.ryanhamshire.GriefPrevention.GriefPrevention;
import net.md_5.bungee.api.ChatColor;

public class ClaimRent extends ClaimTransaction
public class ClaimRent extends BoughtTransaction
{
LocalDateTime startDate = null;
int duration;
UUID rentedBy = null;
boolean autoRenew = false;

public ClaimRent(Map<String, Object> map)
Expand All @@ -33,8 +30,6 @@ public ClaimRent(Map<String, Object> map)
if(map.get("startDate") != null)
startDate = LocalDateTime.parse((String) map.get("startDate"), DateTimeFormatter.ISO_DATE_TIME);
duration = (int)map.get("duration");
if(map.get("rentedBy") != null)
rentedBy = UUID.fromString((String)map.get("rentedBy"));
autoRenew = (boolean) map.get("autoRenew");
}

Expand All @@ -51,8 +46,6 @@ public Map<String, Object> serialize() {
if(startDate != null)
map.put("startDate", startDate.format(DateTimeFormatter.ISO_DATE_TIME));
map.put("duration", duration);
if(rentedBy != null)
map.put("rentedBy", rentedBy.toString());
map.put("autoRenew", autoRenew);

return map;
Expand All @@ -64,7 +57,7 @@ public void update()
if(sign.getBlock().getState() instanceof Sign)
{
Sign s = (Sign) sign.getBlock().getState();
if(rentedBy == null)
if(buyer == null)
{
s.setLine(0, RealEstate.instance.dataStore.cfgSignsHeader);
s.setLine(1, ChatColor.DARK_GREEN + RealEstate.instance.dataStore.cfgReplaceRent);
Expand Down Expand Up @@ -94,7 +87,7 @@ public void update()
else
{
s.setLine(0, RealEstate.instance.dataStore.cfgSignsHeader);
s.setLine(1, ("Rented by " + Bukkit.getOfflinePlayer(rentedBy).getName()).substring(0, 16));
s.setLine(1, ("Rented by " + Bukkit.getOfflinePlayer(buyer).getName()).substring(0, 16));
s.setLine(2, "Time remaining : ");

int daysLeft = duration - days - 1;// we need to remove the current day
Expand All @@ -106,7 +99,7 @@ public void update()

}
}
else
else if(buyer == null)// if no one is renting it, we can delete it (no sign indicating it's rentable)
{
RealEstate.transactionsStore.cancelTransaction(this);
}
Expand All @@ -115,42 +108,42 @@ public void update()
private void unRent(boolean msgBuyer)
{
Claim claim = GriefPrevention.instance.dataStore.getClaimAt(sign, false, null);
claim.dropPermission(rentedBy.toString());
if(msgBuyer && Bukkit.getOfflinePlayer(rentedBy).isOnline() && RealEstate.instance.dataStore.cfgMessageBuyer)
claim.dropPermission(buyer.toString());
if(msgBuyer && Bukkit.getOfflinePlayer(buyer).isOnline() && RealEstate.instance.dataStore.cfgMessageBuyer)
{
Bukkit.getPlayer(rentedBy).sendMessage(RealEstate.instance.dataStore.chatPrefix + ChatColor.AQUA +
Bukkit.getPlayer(buyer).sendMessage(RealEstate.instance.dataStore.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.");
}
rentedBy = null;
buyer = null;
RealEstate.transactionsStore.saveData();
update();
}

private void payRent()
{
if(rentedBy == null) return;
if(buyer == null) return;

OfflinePlayer buyer = Bukkit.getOfflinePlayer(rentedBy);
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(Utils.makePayment(owner, rentedBy, price, false, false))
if(Utils.makePayment(owner, this.buyer, price, false, false))
{
startDate = LocalDateTime.now();
if(buyer.isOnline() && RealEstate.instance.dataStore.cfgMessageBuyer)
if(buyerPlayer.isOnline() && RealEstate.instance.dataStore.cfgMessageBuyer)
{
((Player)buyer).sendMessage(RealEstate.instance.dataStore.chatPrefix + ChatColor.AQUA +
((Player)buyerPlayer).sendMessage(RealEstate.instance.dataStore.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.dataStore.cfgMessageOwner)
{
((Player)seller).sendMessage(RealEstate.instance.dataStore.chatPrefix + ChatColor.AQUA + buyer.getName() +
((Player)seller).sendMessage(RealEstate.instance.dataStore.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() + "]" +
Expand All @@ -159,9 +152,9 @@ private void payRent()
}
else
{
if(buyer.isOnline() && RealEstate.instance.dataStore.cfgMessageBuyer)
if(buyerPlayer.isOnline() && RealEstate.instance.dataStore.cfgMessageBuyer)
{
((Player)buyer).sendMessage(RealEstate.instance.dataStore.chatPrefix + ChatColor.RED +
((Player)buyerPlayer).sendMessage(RealEstate.instance.dataStore.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.");
Expand All @@ -175,7 +168,7 @@ private void payRent()
@Override
public boolean tryCancelTransaction(Player p)
{
if(rentedBy != null)
if(buyer != null)
{
Claim claim = GriefPrevention.instance.dataStore.getClaimAt(sign, false, null);
if(p != null)
Expand Down Expand Up @@ -220,13 +213,13 @@ public void interact(Player player)
claimType + "s!");
return;
}
if(player.getUniqueId().equals(rentedBy))
if(player.getUniqueId().equals(buyer))
{
player.sendMessage(RealEstate.instance.dataStore.chatPrefix + ChatColor.RED + "You are already renting this " +
claimType + "!");
return;
}
if(rentedBy != null)
if(buyer != null)
{
player.sendMessage(RealEstate.instance.dataStore.chatPrefix + ChatColor.RED + "Someone already rents this " +
claimType + "!");
Expand All @@ -235,10 +228,10 @@ public void interact(Player player)

if(Utils.makePayment(owner, player.getUniqueId(), price, false, true))// if payment succeed
{
rentedBy = player.getUniqueId();
buyer = player.getUniqueId();
startDate = LocalDateTime.now();
autoRenew = false;
claim.setPermission(rentedBy.toString(), ClaimPermission.Build);
claim.setPermission(buyer.toString(), ClaimPermission.Build);
update();
RealEstate.transactionsStore.saveData();

Expand Down Expand Up @@ -278,15 +271,12 @@ public void preview(Player player)
String claimType = claim.parent == null ? "claim" : "subclaim";
msg = ChatColor.BLUE + "-----= " + ChatColor.WHITE + "[" + ChatColor.GOLD + "RealEstate Rent Info" + ChatColor.WHITE + "]" +
ChatColor.BLUE + " =-----\n";
if(rentedBy == null)
if(buyer == null)
{
msg += ChatColor.AQUA + "This " + claimType + " is for rent for " +
ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural() + ChatColor.AQUA + " for a duration of " +
ChatColor.GREEN + Utils.getTime(duration, null, true) + "\n";
if(rentedBy.equals(player.getUniqueId()) && RealEstate.instance.dataStore.cfgEnableAutoRenew)
{
msg += ChatColor.AQUA + "Automatic renew is currently " + ChatColor.LIGHT_PURPLE + (autoRenew ? "enable" : "disable") + "\n";
}

if(claimType.equalsIgnoreCase("claim"))
{
msg += ChatColor.AQUA + "The current owner is: " + ChatColor.GREEN + claim.getOwnerName();
Expand All @@ -310,9 +300,13 @@ public void preview(Player player)
Duration timeRemaining = Duration.ofHours(24).minus(hours);

msg += ChatColor.AQUA + "This " + claimType + " is currently rented by " +
ChatColor.GREEN + Bukkit.getOfflinePlayer(rentedBy).getName() + ChatColor.AQUA + " for " +
ChatColor.GREEN + Bukkit.getOfflinePlayer(buyer).getName() + ChatColor.AQUA + " for " +
ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural() + ChatColor.AQUA + " for another " +
ChatColor.GREEN + Utils.getTime(daysLeft, timeRemaining, true) + "\n";
if((owner.equals(player.getUniqueId()) || buyer.equals(player.getUniqueId())) && RealEstate.instance.dataStore.cfgEnableAutoRenew)
{
msg += ChatColor.AQUA + "Automatic renew is currently " + ChatColor.LIGHT_PURPLE + (autoRenew ? "enable" : "disable") + "\n";
}
if(claimType.equalsIgnoreCase("claim"))
{
msg += ChatColor.AQUA + "The current owner is: " + ChatColor.GREEN + claim.getOwnerName();
Expand Down
2 changes: 1 addition & 1 deletion src/me/EtienneDx/RealEstate/DataStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void loadConfig(YamlConfiguration config)
this.cfgSignsHeader = config.getString("RealEstate.Keywords.SignsHeader", ChatColor.GOLD + "[RealEstate]");
//this.cfgSigns = getConfigList(config, "RealEstate.Keywords.Signs", Arrays.asList("[re]", "[realestate]"));

this.cfgSellKeywords = getConfigList(config, "RealEstate.Keywords.Sell", Arrays.asList("[sell]", "[sellclaim]", "[sc]", "[re]", "[realestate]"));
this.cfgSellKeywords = getConfigList(config, "RealEstate.Keywords.Sell", Arrays.asList("[sell]", "[sell claim]", "[sc]", "[re]", "[realestate]"));
this.cfgRentKeywords = getConfigList(config, "RealEstate.Keywords.Rent", Arrays.asList("[rent]", "[rent claim]", "[rc]"));
this.cfgLeaseKeywords = getConfigList(config, "RealEstate.Keywords.Lease", Arrays.asList("[lease]", "[lease claim]", "[lc]"));

Expand Down
34 changes: 34 additions & 0 deletions src/me/EtienneDx/RealEstate/ExitOffer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package me.EtienneDx.RealEstate;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.bukkit.configuration.serialization.ConfigurationSerializable;

public class ExitOffer implements ConfigurationSerializable
{
UUID offerBy;
double price;

public ExitOffer(UUID offerBy, double price)
{
this.offerBy = offerBy;
this.price = price;
}

public ExitOffer(Map<String, Object> map)
{
offerBy = UUID.fromString((String)map.get("offerBy"));
price = (double)map.get("price");
}

@Override
public Map<String, Object> serialize()
{
HashMap<String, Object> map = new HashMap<>();
map.put("offerBy", offerBy.toString());
map.put("price", price);
return map;
}
}
Loading

0 comments on commit 9578ebb

Please sign in to comment.