added /re seller command to change who is selling an admin claim

This commit is contained in:
EtienneDx 2019-07-31 17:26:15 +02:00
parent 96a50c1107
commit 2cff090309
6 changed files with 1292 additions and 1202 deletions

View File

@ -1,300 +1,349 @@
package me.EtienneDx.RealEstate; package me.EtienneDx.RealEstate;
import java.util.UUID; import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import com.earth2me.essentials.User; import com.earth2me.essentials.User;
import co.aikar.commands.BaseCommand; import co.aikar.commands.BaseCommand;
import co.aikar.commands.CommandHelp; import co.aikar.commands.CommandHelp;
import co.aikar.commands.annotation.CommandAlias; import co.aikar.commands.annotation.CommandAlias;
import co.aikar.commands.annotation.CommandCompletion; import co.aikar.commands.annotation.CommandCompletion;
import co.aikar.commands.annotation.CommandPermission; import co.aikar.commands.annotation.CommandPermission;
import co.aikar.commands.annotation.Conditions; import co.aikar.commands.annotation.Conditions;
import co.aikar.commands.annotation.Default; import co.aikar.commands.annotation.Default;
import co.aikar.commands.annotation.Description; import co.aikar.commands.annotation.Description;
import co.aikar.commands.annotation.HelpCommand; import co.aikar.commands.annotation.HelpCommand;
import co.aikar.commands.annotation.Optional; import co.aikar.commands.annotation.Optional;
import co.aikar.commands.annotation.Subcommand; import co.aikar.commands.annotation.Subcommand;
import me.EtienneDx.RealEstate.Transactions.BoughtTransaction; import me.EtienneDx.RealEstate.Transactions.BoughtTransaction;
import me.EtienneDx.RealEstate.Transactions.ClaimRent; import me.EtienneDx.RealEstate.Transactions.ClaimRent;
import me.EtienneDx.RealEstate.Transactions.ExitOffer; import me.EtienneDx.RealEstate.Transactions.ExitOffer;
import me.EtienneDx.RealEstate.Transactions.Transaction; import me.EtienneDx.RealEstate.Transactions.Transaction;
import me.ryanhamshire.GriefPrevention.Claim; import me.ryanhamshire.GriefPrevention.Claim;
import me.ryanhamshire.GriefPrevention.GriefPrevention; import me.ryanhamshire.GriefPrevention.GriefPrevention;
@CommandAlias("re|realestate") @CommandAlias("re|realestate")
public class RECommand extends BaseCommand public class RECommand extends BaseCommand
{ {
@Subcommand("info") @Subcommand("info")
@Description("Gives the player informations about the claim he is standing in") @Description("Gives the player informations about the claim he is standing in")
@CommandPermission("realestate.info") @CommandPermission("realestate.info")
public static void info(Player player) public static void info(Player player)
{ {
if(RealEstate.transactionsStore.anyTransaction( if(RealEstate.transactionsStore.anyTransaction(
GriefPrevention.instance.dataStore.getClaimAt(((Player)player).getLocation(), false, null))) GriefPrevention.instance.dataStore.getClaimAt(((Player)player).getLocation(), false, null)))
{ {
Transaction tr = RealEstate.transactionsStore.getTransaction( Transaction tr = RealEstate.transactionsStore.getTransaction(
GriefPrevention.instance.dataStore.getClaimAt(((Player)player).getLocation(), false, null)); GriefPrevention.instance.dataStore.getClaimAt(((Player)player).getLocation(), false, null));
tr.preview((Player)player); tr.preview((Player)player);
} }
else else
{ {
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "No transaction found at your location!"); player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "No transaction found at your location!");
} }
} }
@Subcommand("renewrent") @Subcommand("renewrent")
@Description("Allows the player renting a claim or subclaim to enable or disable the automatic renew of his rent") @Description("Allows the player renting a claim or subclaim to enable or disable the automatic renew of his rent")
@Conditions("partOfRent") @Conditions("partOfRent")
@CommandCompletion("enable|disable") @CommandCompletion("enable|disable")
public static void renewRent(Player player, @Optional String newStatus) public static void renewRent(Player player, @Optional String newStatus)
{ {
Location loc = player.getLocation(); Location loc = player.getLocation();
Claim claim = GriefPrevention.instance.dataStore.getClaimAt(loc, false, null); Claim claim = GriefPrevention.instance.dataStore.getClaimAt(loc, false, null);
ClaimRent cr = (ClaimRent)RealEstate.transactionsStore.getTransaction(claim); ClaimRent cr = (ClaimRent)RealEstate.transactionsStore.getTransaction(claim);
String claimType = claim.parent == null ? "claim" : "subclaim"; String claimType = claim.parent == null ? "claim" : "subclaim";
if(!RealEstate.instance.config.cfgEnableAutoRenew) if(!RealEstate.instance.config.cfgEnableAutoRenew)
{ {
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "Automatic renew is disabled!"); player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "Automatic renew is disabled!");
return; return;
} }
if(newStatus == null) if(newStatus == null)
{ {
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + "Automatic renew is currently " + player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + "Automatic renew is currently " +
ChatColor.GREEN + (cr.autoRenew ? "enabled" : "disabled") + ChatColor.AQUA + " for this " + claimType + "!"); ChatColor.GREEN + (cr.autoRenew ? "enabled" : "disabled") + ChatColor.AQUA + " for this " + claimType + "!");
} }
else if(!newStatus.equalsIgnoreCase("enable") && !newStatus.equalsIgnoreCase("disable")) else if(!newStatus.equalsIgnoreCase("enable") && !newStatus.equalsIgnoreCase("disable"))
{ {
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "Usage : /re renewrent [enable|disable]!"); player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "Usage : /re renewrent [enable|disable]!");
} }
else if(cr.buyer.equals(player.getUniqueId())) else if(cr.buyer.equals(player.getUniqueId()))
{ {
cr.autoRenew = newStatus.equalsIgnoreCase("enable"); cr.autoRenew = newStatus.equalsIgnoreCase("enable");
RealEstate.transactionsStore.saveData(); RealEstate.transactionsStore.saveData();
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + "Automatic renew is now " + player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + "Automatic renew is now " +
ChatColor.GREEN + (cr.autoRenew ? "enabled" : "disabled") + ChatColor.AQUA + " for this " + claimType + "!"); ChatColor.GREEN + (cr.autoRenew ? "enabled" : "disabled") + ChatColor.AQUA + " for this " + claimType + "!");
} }
else else
{ {
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "Only the buyer may change this setting!"); player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "Only the buyer may change this setting!");
} }
} }
@Subcommand("exitoffer") @Subcommand("seller")
@Conditions("partOfBoughtTransaction") @Description("Displays or changes the seller of a claim (admin only)")
public class ExitOfferCommand extends BaseCommand @Conditions("inPendingTransactionClaim")
{ public static void setSeller(Player player, @Optional String newSeller)
@Subcommand("info") {
@Default Location loc = player.getLocation();
@Description("View informations about the exit offer") Claim claim = GriefPrevention.instance.dataStore.getClaimAt(loc, false, null);
public void info(Player player) Transaction tr = RealEstate.transactionsStore.getTransaction(claim);
{ if(!claim.isAdminClaim())
BoughtTransaction bt = (BoughtTransaction)RealEstate.transactionsStore.getTransaction(player); {
if(bt.exitOffer == null) player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "This is not an admin claim");
{ }
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + "There is currently no exit offer for this claim!"); else if(newSeller == null)
} {
else if(bt.exitOffer.offerBy.equals(player.getUniqueId())) player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + "The seller of this claim is " +
{ ChatColor.GREEN + (tr.getOwner() == null ? "the server" : Bukkit.getPlayer(tr.getOwner()).getDisplayName()));
String msg = RealEstate.instance.config.chatPrefix + ChatColor.AQUA + "You offered to exit the contract for " + }
ChatColor.GREEN + bt.exitOffer.price + " " + RealEstate.econ.currencyNamePlural() + ChatColor.AQUA + else if(!RealEstate.perms.has(player, "realestate.admin"))
", but your offer hasn't been accepted or denied yet...\n"; {
msg += ChatColor.AQUA + "To cancel your offer, just type " + ChatColor.LIGHT_PURPLE + "/re exitoffer cancel"; player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "You don't have the permission to change the seller");
player.sendMessage(msg); }
} else if(newSeller.equalsIgnoreCase("server"))
else// it is the other person {
{ tr.setOwner(null);
String msg = RealEstate.instance.config.chatPrefix + ChatColor.GREEN + Bukkit.getOfflinePlayer(bt.exitOffer.offerBy).getName() + tr.update();
ChatColor.AQUA + " offered to exit the contract for " + player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + "Changed the seller to the server");
ChatColor.GREEN + bt.exitOffer.price + " " + RealEstate.econ.currencyNamePlural() + "\n"; }
msg += ChatColor.AQUA + "To accept the offer, just type " + ChatColor.LIGHT_PURPLE + "/re exitoffer accept\n"; else
msg += ChatColor.AQUA + "To refuse the offer, just type " + ChatColor.LIGHT_PURPLE + "/re exitoffer refuse\n"; {
player.sendMessage(msg); Player newOwner = Bukkit.getPlayer(newSeller);
} if(newOwner == null)
} {
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "Couldn't find this player (he may be offline)");
@Subcommand("create") }
@Description("Creates an offer to break an ongoing transaction") else if(!RealEstate.perms.has(newOwner, "realestate.admin"))
public void create(Player player, @Conditions("positiveDouble") Double price) {
{ player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED +
BoughtTransaction bt = (BoughtTransaction)RealEstate.transactionsStore.getTransaction(player); "This player doesn't have the right to lease/rent/sell admin claims");
if(bt.exitOffer != null) }
{ else
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + {
"There is already an exit proposition for this transaction!"); tr.setOwner(newOwner.getUniqueId());
return; tr.update();
} player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + "Changed the seller to " +
if(bt.buyer == null) ChatColor.GREEN + newOwner.getDisplayName());
{ }
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + }
"No one is engaged by this transaction yet!"); }
return;
} @Subcommand("exitoffer")
bt.exitOffer = new ExitOffer(player.getUniqueId(), price); @Conditions("partOfBoughtTransaction")
public class ExitOfferCommand extends BaseCommand
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + {
"The proposition has been successfully created!"); @Subcommand("info")
UUID other = player.getUniqueId().equals(bt.owner) ? bt.buyer : bt.owner; @Default
OfflinePlayer otherP = Bukkit.getOfflinePlayer(other); @Description("View informations about the exit offer")
Location loc = player.getLocation(); public void info(Player player)
String claimType = GriefPrevention.instance.dataStore.getClaimAt(loc, false, null).parent == null ? "claim" : "subclaim"; {
if(otherP.isOnline()) BoughtTransaction bt = (BoughtTransaction)RealEstate.transactionsStore.getTransaction(player);
{ if(bt.exitOffer == null)
((Player)otherP).sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.GREEN + player.getName() + {
ChatColor.AQUA + " has created an offer to exit the rent/lease contract for the " + claimType + " at " + player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + "There is currently no exit offer for this claim!");
ChatColor.BLUE + "[" + loc.getWorld().getName() + ", X: " + loc.getBlockX() + ", Y: " + loc.getBlockY() + ", Z: " }
+ loc.getBlockZ() + "]" + ChatColor.AQUA + " for " + ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural()); else if(bt.exitOffer.offerBy.equals(player.getUniqueId()))
} {
else if(RealEstate.instance.config.cfgMailOffline && RealEstate.ess != null) String msg = RealEstate.instance.config.chatPrefix + ChatColor.AQUA + "You offered to exit the contract for " +
{ ChatColor.GREEN + bt.exitOffer.price + " " + RealEstate.econ.currencyNamePlural() + ChatColor.AQUA +
User u = RealEstate.ess.getUser(other); ", but your offer hasn't been accepted or denied yet...\n";
u.addMail(RealEstate.instance.config.chatPrefix + ChatColor.GREEN + player.getName() + msg += ChatColor.AQUA + "To cancel your offer, just type " + ChatColor.LIGHT_PURPLE + "/re exitoffer cancel";
ChatColor.AQUA + " has created an offer to exit the rent/lease contract for the " + claimType + " at " + player.sendMessage(msg);
ChatColor.BLUE + "[" + loc.getWorld().getName() + ", X: " + loc.getBlockX() + ", Y: " + loc.getBlockY() + ", Z: " }
+ loc.getBlockZ() + "]" + ChatColor.AQUA + " for " + ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural()); else// it is the other person
} {
} String msg = RealEstate.instance.config.chatPrefix + ChatColor.GREEN + Bukkit.getOfflinePlayer(bt.exitOffer.offerBy).getName() +
ChatColor.AQUA + " offered to exit the contract for " +
@Subcommand("accept") ChatColor.GREEN + bt.exitOffer.price + " " + RealEstate.econ.currencyNamePlural() + "\n";
@Description("Accepts an offer to break an ongoing transaction") msg += ChatColor.AQUA + "To accept the offer, just type " + ChatColor.LIGHT_PURPLE + "/re exitoffer accept\n";
public void accept(Player player) msg += ChatColor.AQUA + "To refuse the offer, just type " + ChatColor.LIGHT_PURPLE + "/re exitoffer refuse\n";
{ player.sendMessage(msg);
Location loc = player.getLocation(); }
Claim claim = GriefPrevention.instance.dataStore.getClaimAt(loc, false, null); }
BoughtTransaction bt = (BoughtTransaction)RealEstate.transactionsStore.getTransaction(claim);
String claimType = claim.parent == null ? "claim" : "subclaim"; @Subcommand("create")
if(bt.exitOffer == null) @Description("Creates an offer to break an ongoing transaction")
{ public void create(Player player, @Conditions("positiveDouble") Double price)
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + {
"There has been no exit propositions for this transaction!"); BoughtTransaction bt = (BoughtTransaction)RealEstate.transactionsStore.getTransaction(player);
} if(bt.exitOffer != null)
else if(bt.exitOffer.offerBy.equals(player.getUniqueId())) {
{ player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED +
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "There is already an exit proposition for this transaction!");
"You can't accept or refuse an offer you made!"); return;
} }
else if(Utils.makePayment(player.getUniqueId(), bt.exitOffer.offerBy, bt.exitOffer.price, true, false)) if(bt.buyer == null)
{ {
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED +
"This exit offer has been accepted, the " + claimType + " is no longer rented or leased!"); "No one is engaged by this transaction yet!");
UUID other = player.getUniqueId().equals(bt.owner) ? bt.buyer : bt.owner; return;
OfflinePlayer otherP = Bukkit.getOfflinePlayer(other); }
if(otherP.isOnline()) bt.exitOffer = new ExitOffer(player.getUniqueId(), price);
{
((Player)otherP).sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.GREEN + player.getName() + player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.AQUA +
ChatColor.AQUA + " has accepted your offer to exit the rent/lease contract for the " + claimType + " at " + "The proposition has been successfully created!");
ChatColor.BLUE + "[" + loc.getWorld().getName() + ", X: " + loc.getBlockX() + ", Y: " + loc.getBlockY() + UUID other = player.getUniqueId().equals(bt.owner) ? bt.buyer : bt.owner;
", Z: " + loc.getBlockZ() + "]. It is no longer rented or leased."); OfflinePlayer otherP = Bukkit.getOfflinePlayer(other);
} Location loc = player.getLocation();
else if(RealEstate.instance.config.cfgMailOffline && RealEstate.ess != null) String claimType = GriefPrevention.instance.dataStore.getClaimAt(loc, false, null).parent == null ? "claim" : "subclaim";
{ if(otherP.isOnline())
User u = RealEstate.ess.getUser(other); {
u.addMail(RealEstate.instance.config.chatPrefix + ChatColor.GREEN + player.getName() + ((Player)otherP).sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.GREEN + player.getName() +
ChatColor.AQUA + " has accepted your offer to exit the rent/lease contract for the " + claimType + " at " + ChatColor.AQUA + " has created an offer to exit the rent/lease contract for the " + claimType + " at " +
ChatColor.BLUE + "[" + loc.getWorld().getName() + ", X: " + loc.getBlockX() + ", Y: " + loc.getBlockY() + ChatColor.BLUE + "[" + loc.getWorld().getName() + ", X: " + loc.getBlockX() + ", Y: " + loc.getBlockY() + ", Z: "
", Z: " + loc.getBlockZ() + "]. It is no longer rented or leased."); + loc.getBlockZ() + "]" + ChatColor.AQUA + " for " + ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural());
} }
bt.exitOffer = null; else if(RealEstate.instance.config.cfgMailOffline && RealEstate.ess != null)
claim.dropPermission(bt.buyer.toString()); {
GriefPrevention.instance.dataStore.saveClaim(claim); User u = RealEstate.ess.getUser(other);
bt.buyer = null; u.addMail(RealEstate.instance.config.chatPrefix + ChatColor.GREEN + player.getName() +
bt.update();// eventual cancel is contained in here ChatColor.AQUA + " has created an offer to exit the rent/lease contract for the " + claimType + " at " +
} ChatColor.BLUE + "[" + loc.getWorld().getName() + ", X: " + loc.getBlockX() + ", Y: " + loc.getBlockY() + ", Z: "
// the make payment takes care of sending error if need be + loc.getBlockZ() + "]" + ChatColor.AQUA + " for " + ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural());
} }
}
@Subcommand("refuse")
@Description("Refuses an offer to break an ongoing transaction") @Subcommand("accept")
public void refuse(Player player) @Description("Accepts an offer to break an ongoing transaction")
{ public void accept(Player player)
Location loc = player.getLocation(); {
Claim claim = GriefPrevention.instance.dataStore.getClaimAt(loc, false, null); Location loc = player.getLocation();
BoughtTransaction bt = (BoughtTransaction)RealEstate.transactionsStore.getTransaction(claim); Claim claim = GriefPrevention.instance.dataStore.getClaimAt(loc, false, null);
String claimType = claim.parent == null ? "claim" : "subclaim"; BoughtTransaction bt = (BoughtTransaction)RealEstate.transactionsStore.getTransaction(claim);
if(bt.exitOffer == null) String claimType = claim.parent == null ? "claim" : "subclaim";
{ if(bt.exitOffer == null)
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + {
"There has been no exit propositions for this transaction!"); player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED +
} "There has been no exit propositions for this transaction!");
else if(bt.exitOffer.offerBy.equals(player.getUniqueId())) }
{ else if(bt.exitOffer.offerBy.equals(player.getUniqueId()))
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + {
"You can't accept or refuse an offer you made!"); player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED +
} "You can't accept or refuse an offer you made!");
else }
{ else if(Utils.makePayment(player.getUniqueId(), bt.exitOffer.offerBy, bt.exitOffer.price, true, false))
bt.exitOffer = null; {
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.AQUA +
"This exit offer has been refused"); "This exit offer has been accepted, the " + claimType + " is no longer rented or leased!");
UUID other = player.getUniqueId().equals(bt.owner) ? bt.buyer : bt.owner; UUID other = player.getUniqueId().equals(bt.owner) ? bt.buyer : bt.owner;
OfflinePlayer otherP = Bukkit.getOfflinePlayer(other); OfflinePlayer otherP = Bukkit.getOfflinePlayer(other);
if(otherP.isOnline()) if(otherP.isOnline())
{ {
((Player)otherP).sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.GREEN + player.getName() + ((Player)otherP).sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.GREEN + player.getName() +
ChatColor.AQUA + " has refused your offer to exit the rent/lease contract for the " + claimType + " at " + ChatColor.AQUA + " has accepted your offer to exit the rent/lease contract for the " + claimType + " at " +
ChatColor.BLUE + "[" + loc.getWorld().getName() + ", X: " + loc.getBlockX() + ", Y: " + loc.getBlockY() + ChatColor.BLUE + "[" + loc.getWorld().getName() + ", X: " + loc.getBlockX() + ", Y: " + loc.getBlockY() +
", Z: " + loc.getBlockZ() + "]"); ", Z: " + loc.getBlockZ() + "]. It is no longer rented or leased.");
} }
else if(RealEstate.instance.config.cfgMailOffline && RealEstate.ess != null) else if(RealEstate.instance.config.cfgMailOffline && RealEstate.ess != null)
{ {
User u = RealEstate.ess.getUser(other); User u = RealEstate.ess.getUser(other);
u.addMail(RealEstate.instance.config.chatPrefix + ChatColor.GREEN + player.getName() + u.addMail(RealEstate.instance.config.chatPrefix + ChatColor.GREEN + player.getName() +
ChatColor.AQUA + " has refused your offer to exit the rent/lease contract for the " + claimType + " at " + ChatColor.AQUA + " has accepted your offer to exit the rent/lease contract for the " + claimType + " at " +
ChatColor.BLUE + "[" + loc.getWorld().getName() + ", X: " + loc.getBlockX() + ", Y: " + loc.getBlockY() + ChatColor.BLUE + "[" + loc.getWorld().getName() + ", X: " + loc.getBlockX() + ", Y: " + loc.getBlockY() +
", Z: " + loc.getBlockZ() + "]"); ", Z: " + loc.getBlockZ() + "]. It is no longer rented or leased.");
} }
} bt.exitOffer = null;
} claim.dropPermission(bt.buyer.toString());
GriefPrevention.instance.dataStore.saveClaim(claim);
@Subcommand("cancel") bt.buyer = null;
@Description("Cancels an offer to break an ongoing transaction") bt.update();// eventual cancel is contained in here
public void cancel(Player player) }
{ // the make payment takes care of sending error if need be
Location loc = player.getLocation(); }
Claim claim = GriefPrevention.instance.dataStore.getClaimAt(loc, false, null);
BoughtTransaction bt = (BoughtTransaction)RealEstate.transactionsStore.getTransaction(claim); @Subcommand("refuse")
String claimType = claim.parent == null ? "claim" : "subclaim"; @Description("Refuses an offer to break an ongoing transaction")
if(bt.exitOffer.offerBy.equals(player.getUniqueId())) public void refuse(Player player)
{ {
bt.exitOffer = null; Location loc = player.getLocation();
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + Claim claim = GriefPrevention.instance.dataStore.getClaimAt(loc, false, null);
"This exit offer has been cancelled"); BoughtTransaction bt = (BoughtTransaction)RealEstate.transactionsStore.getTransaction(claim);
UUID other = player.getUniqueId().equals(bt.owner) ? bt.buyer : bt.owner; String claimType = claim.parent == null ? "claim" : "subclaim";
OfflinePlayer otherP = Bukkit.getOfflinePlayer(other); if(bt.exitOffer == null)
if(otherP.isOnline()) {
{ player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED +
((Player)otherP).sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.GREEN + player.getName() + "There has been no exit propositions for this transaction!");
ChatColor.AQUA + " has cancelled his offer to exit the rent/lease contract for the " + claimType + " at " + }
ChatColor.BLUE + "[" + loc.getWorld().getName() + ", X: " + loc.getBlockX() + ", Y: " + loc.getBlockY() + ", Z: " else if(bt.exitOffer.offerBy.equals(player.getUniqueId()))
+ loc.getBlockZ() + "]"); {
} player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED +
else if(RealEstate.instance.config.cfgMailOffline && RealEstate.ess != null) "You can't accept or refuse an offer you made!");
{ }
User u = RealEstate.ess.getUser(other); else
u.addMail(RealEstate.instance.config.chatPrefix + ChatColor.GREEN + player.getName() + {
ChatColor.AQUA + " has cancelled his offer to exit the rent/lease contract for the " + claimType + " at " + bt.exitOffer = null;
ChatColor.BLUE + "[" + loc.getWorld().getName() + ", X: " + loc.getBlockX() + ", Y: " + loc.getBlockY() + ", Z: " player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.AQUA +
+ loc.getBlockZ() + "]"); "This exit offer has been refused");
} UUID other = player.getUniqueId().equals(bt.owner) ? bt.buyer : bt.owner;
} OfflinePlayer otherP = Bukkit.getOfflinePlayer(other);
else if(otherP.isOnline())
{ {
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + ((Player)otherP).sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.GREEN + player.getName() +
"Only the player who created this exit proposition may cancel it"); ChatColor.AQUA + " has refused your offer to exit the rent/lease contract for the " + claimType + " at " +
} ChatColor.BLUE + "[" + loc.getWorld().getName() + ", X: " + loc.getBlockX() + ", Y: " + loc.getBlockY() +
} ", Z: " + loc.getBlockZ() + "]");
} }
else if(RealEstate.instance.config.cfgMailOffline && RealEstate.ess != null)
@HelpCommand {
public static void onHelp(CommandSender sender, CommandHelp help) User u = RealEstate.ess.getUser(other);
{ u.addMail(RealEstate.instance.config.chatPrefix + ChatColor.GREEN + player.getName() +
help.showHelp(); ChatColor.AQUA + " has refused your offer to exit the rent/lease contract for the " + claimType + " at " +
} ChatColor.BLUE + "[" + loc.getWorld().getName() + ", X: " + loc.getBlockX() + ", Y: " + loc.getBlockY() +
} ", Z: " + loc.getBlockZ() + "]");
}
}
}
@Subcommand("cancel")
@Description("Cancels an offer to break an ongoing transaction")
public void cancel(Player player)
{
Location loc = player.getLocation();
Claim claim = GriefPrevention.instance.dataStore.getClaimAt(loc, false, null);
BoughtTransaction bt = (BoughtTransaction)RealEstate.transactionsStore.getTransaction(claim);
String claimType = claim.parent == null ? "claim" : "subclaim";
if(bt.exitOffer.offerBy.equals(player.getUniqueId()))
{
bt.exitOffer = null;
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.AQUA +
"This exit offer has been cancelled");
UUID other = player.getUniqueId().equals(bt.owner) ? bt.buyer : bt.owner;
OfflinePlayer otherP = Bukkit.getOfflinePlayer(other);
if(otherP.isOnline())
{
((Player)otherP).sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.GREEN + player.getName() +
ChatColor.AQUA + " has cancelled his offer to exit the rent/lease contract for the " + claimType + " at " +
ChatColor.BLUE + "[" + loc.getWorld().getName() + ", X: " + loc.getBlockX() + ", Y: " + loc.getBlockY() + ", Z: "
+ loc.getBlockZ() + "]");
}
else if(RealEstate.instance.config.cfgMailOffline && RealEstate.ess != null)
{
User u = RealEstate.ess.getUser(other);
u.addMail(RealEstate.instance.config.chatPrefix + ChatColor.GREEN + player.getName() +
ChatColor.AQUA + " has cancelled his offer to exit the rent/lease contract for the " + claimType + " at " +
ChatColor.BLUE + "[" + loc.getWorld().getName() + ", X: " + loc.getBlockX() + ", Y: " + loc.getBlockY() + ", Z: "
+ loc.getBlockZ() + "]");
}
}
else
{
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED +
"Only the player who created this exit proposition may cancel it");
}
}
}
@HelpCommand
public static void onHelp(CommandSender sender, CommandHelp help)
{
help.showHelp();
}
}

View File

@ -1,416 +1,423 @@
package me.EtienneDx.RealEstate; package me.EtienneDx.RealEstate;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.block.Sign; import org.bukkit.block.Sign;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.block.Action; import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.SignChangeEvent; import org.bukkit.event.block.SignChangeEvent;
import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
import me.EtienneDx.RealEstate.Transactions.Transaction; import me.EtienneDx.RealEstate.Transactions.Transaction;
import me.ryanhamshire.GriefPrevention.Claim; import me.ryanhamshire.GriefPrevention.Claim;
import me.ryanhamshire.GriefPrevention.GriefPrevention; import me.ryanhamshire.GriefPrevention.GriefPrevention;
public class REListener implements Listener public class REListener implements Listener
{ {
void registerEvents() void registerEvents()
{ {
PluginManager pm = RealEstate.instance.getServer().getPluginManager(); PluginManager pm = RealEstate.instance.getServer().getPluginManager();
pm.registerEvents(this, RealEstate.instance); pm.registerEvents(this, RealEstate.instance);
//RealEstate.instance.getCommand("re").setExecutor(this); //RealEstate.instance.getCommand("re").setExecutor(this);
} }
@EventHandler @EventHandler
public void onSignChange(SignChangeEvent event) public void onSignChange(SignChangeEvent event)
{ {
if(RealEstate.instance.config.cfgSellKeywords.contains(event.getLine(0).toLowerCase()) || if(RealEstate.instance.config.cfgSellKeywords.contains(event.getLine(0).toLowerCase()) ||
RealEstate.instance.config.cfgLeaseKeywords.contains(event.getLine(0).toLowerCase()) || RealEstate.instance.config.cfgLeaseKeywords.contains(event.getLine(0).toLowerCase()) ||
RealEstate.instance.config.cfgRentKeywords.contains(event.getLine(0).toLowerCase())) RealEstate.instance.config.cfgRentKeywords.contains(event.getLine(0).toLowerCase()))
{ {
Player player = event.getPlayer(); Player player = event.getPlayer();
Location loc = event.getBlock().getLocation(); Location loc = event.getBlock().getLocation();
Claim claim = GriefPrevention.instance.dataStore.getClaimAt(loc, false, null); Claim claim = GriefPrevention.instance.dataStore.getClaimAt(loc, false, null);
if(claim == null)// must have something to sell if(claim == null)// must have something to sell
{ {
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "The sign you placed is not inside a claim!"); player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "The sign you placed is not inside a claim!");
event.setCancelled(true); event.setCancelled(true);
event.getBlock().breakNaturally(); event.getBlock().breakNaturally();
return; return;
} }
if(RealEstate.transactionsStore.anyTransaction(claim)) if(RealEstate.transactionsStore.anyTransaction(claim))
{ {
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "This claim already has an ongoing transaction!"); player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "This claim already has an ongoing transaction!");
event.setCancelled(true); event.setCancelled(true);
event.getBlock().breakNaturally(); event.getBlock().breakNaturally();
return; return;
} }
if(RealEstate.transactionsStore.anyTransaction(claim.parent)) if(RealEstate.transactionsStore.anyTransaction(claim.parent))
{ {
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "The parent claim already has an ongoing transaction!"); player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "The parent claim already has an ongoing transaction!");
event.setCancelled(true); event.setCancelled(true);
event.getBlock().breakNaturally(); event.getBlock().breakNaturally();
return; return;
} }
for(Claim c : claim.children) for(Claim c : claim.children)
{ {
if(RealEstate.transactionsStore.anyTransaction(c)) if(RealEstate.transactionsStore.anyTransaction(c))
{ {
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED +
"A subclaim of this claim already has an ongoing transaction!"); "A subclaim of this claim already has an ongoing transaction!");
event.setCancelled(true); event.setCancelled(true);
event.getBlock().breakNaturally(); event.getBlock().breakNaturally();
return; return;
} }
} }
// empty is considered a wish to sell // empty is considered a wish to sell
if(RealEstate.instance.config.cfgSellKeywords.contains(event.getLine(0).toLowerCase())) if(RealEstate.instance.config.cfgSellKeywords.contains(event.getLine(0).toLowerCase()))
{ {
if(!RealEstate.instance.config.cfgEnableSell) if(!RealEstate.instance.config.cfgEnableSell)
{ {
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "Selling is disabled!"); player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "Selling is disabled!");
event.setCancelled(true); event.setCancelled(true);
event.getBlock().breakNaturally(); event.getBlock().breakNaturally();
return; return;
} }
String type = claim.parent == null ? "claim" : "subclaim"; String type = claim.parent == null ? "claim" : "subclaim";
if(!RealEstate.perms.has(player, "realestate." + type + ".sell")) if(!RealEstate.perms.has(player, "realestate." + type + ".sell"))
{ {
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "You don't have the permission to sell " + type + "s!"); player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "You don't have the permission to sell " + type + "s!");
event.setCancelled(true); event.setCancelled(true);
event.getBlock().breakNaturally(); event.getBlock().breakNaturally();
return; return;
} }
// check for a valid price // check for a valid price
double price; double price;
try try
{ {
price = getDouble(event, 1, RealEstate.instance.config.cfgPriceSellPerBlock * claim.getArea()); price = getDouble(event, 1, RealEstate.instance.config.cfgPriceSellPerBlock * claim.getArea());
} }
catch (NumberFormatException e) catch (NumberFormatException e)
{ {
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "The price you entered is not a valid number!"); player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "The price you entered is not a valid number!");
event.setCancelled(true); event.setCancelled(true);
event.getBlock().breakNaturally(); event.getBlock().breakNaturally();
return; return;
} }
if(price <= 0) if(price <= 0)
{ {
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "The price must be greater than 0!"); player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "The price must be greater than 0!");
event.setCancelled(true); event.setCancelled(true);
event.getBlock().breakNaturally(); event.getBlock().breakNaturally();
return; return;
} }
if(claim.isAdminClaim() && !RealEstate.perms.has(player, "realestate.admin"))// admin may sell admin claims if(claim.isAdminClaim() && !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!"); player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "You don't have the permission to sell admin claims!");
event.setCancelled(true); event.setCancelled(true);
event.getBlock().breakNaturally(); event.getBlock().breakNaturally();
return; return;
} }
else if(type.equals("claim") && !player.getUniqueId().equals(claim.ownerID))// only the owner may sell his claim else if(type.equals("claim") && !player.getUniqueId().equals(claim.ownerID))// only the owner may sell his claim
{ {
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "You can only sell claims you own!"); player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "You can only sell claims you own!");
event.setCancelled(true); event.setCancelled(true);
event.getBlock().breakNaturally(); event.getBlock().breakNaturally();
return; return;
} }
// we should be good to sell it now // we should be good to sell it now
event.setCancelled(true);// need to cancel the event, so we can update the sign elsewhere 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, player, price, event.getBlock().getLocation());
} }
else if(RealEstate.instance.config.cfgRentKeywords.contains(event.getLine(0).toLowerCase()))// we want to rent it else if(RealEstate.instance.config.cfgRentKeywords.contains(event.getLine(0).toLowerCase()))// we want to rent it
{ {
if(!RealEstate.instance.config.cfgEnableRent) if(!RealEstate.instance.config.cfgEnableRent)
{ {
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "Renting is disabled!"); player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "Renting is disabled!");
event.setCancelled(true); event.setCancelled(true);
event.getBlock().breakNaturally(); event.getBlock().breakNaturally();
return; return;
} }
String type = claim.parent == null ? "claim" : "subclaim"; String type = claim.parent == null ? "claim" : "subclaim";
if(!RealEstate.perms.has(player, "realestate." + type + ".rent")) if(!RealEstate.perms.has(player, "realestate." + type + ".rent"))
{ {
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "You don't have the permission to rent " + type + "s!"); player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "You don't have the permission to rent " + type + "s!");
event.setCancelled(true); event.setCancelled(true);
event.getBlock().breakNaturally(); event.getBlock().breakNaturally();
return; return;
} }
// check for a valid price // check for a valid price
double price; double price;
try try
{ {
price = getDouble(event, 1, RealEstate.instance.config.cfgPriceRentPerBlock * claim.getArea()); price = getDouble(event, 1, RealEstate.instance.config.cfgPriceRentPerBlock * claim.getArea());
} }
catch (NumberFormatException e) catch (NumberFormatException e)
{ {
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "The price you entered is not a valid number!"); player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "The price you entered is not a valid number!");
event.setCancelled(true); event.setCancelled(true);
event.getBlock().breakNaturally(); event.getBlock().breakNaturally();
return; return;
} }
if(price <= 0) if(price <= 0)
{ {
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "The price must be greater than 0!"); player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "The price must be greater than 0!");
event.setCancelled(true); event.setCancelled(true);
event.getBlock().breakNaturally(); event.getBlock().breakNaturally();
return; return;
} }
if(event.getLine(2).isEmpty()) if(event.getLine(2).isEmpty())
{ {
event.setLine(2, RealEstate.instance.config.cfgRentTime); event.setLine(2, RealEstate.instance.config.cfgRentTime);
} }
int duration = parseDuration(event.getLine(2)); int duration = parseDuration(event.getLine(2));
if(duration == 0) if(duration == 0)
{ {
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "Couldn't read the date!\n" + player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "Couldn't read the date!\n" +
"Date must be formatted as follow" + ChatColor.GREEN + "10 weeks" + ChatColor.RED + " or " + "Date must be formatted as follow : " + ChatColor.GREEN + "10 weeks" + ChatColor.RED + " or " +
ChatColor.GREEN + "3 days" + ChatColor.RED + " or " + ChatColor.GREEN + "1 week 3 days"); ChatColor.GREEN + "3 days" + ChatColor.RED + " or " + ChatColor.GREEN + "1 week 3 days");
event.setCancelled(true); event.setCancelled(true);
event.getBlock().breakNaturally(); event.getBlock().breakNaturally();
return; return;
} }
int rentPeriods = 1; int rentPeriods = 1;
if(RealEstate.instance.config.cfgEnableRentPeriod) if(RealEstate.instance.config.cfgEnableRentPeriod)
{ {
if(event.getLine(3).isEmpty()) if(event.getLine(3).isEmpty())
{ {
event.setLine(3, "1"); event.setLine(3, "1");
} }
try try
{ {
rentPeriods = Integer.parseInt(event.getLine(3)); rentPeriods = Integer.parseInt(event.getLine(3));
} }
catch (NumberFormatException e) catch (NumberFormatException e)
{ {
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED +
"The number of rent periods you entered is not a valid number!"); "The number of rent periods you entered is not a valid number!");
event.setCancelled(true); event.setCancelled(true);
event.getBlock().breakNaturally(); event.getBlock().breakNaturally();
return; return;
} }
if(rentPeriods <= 0) if(rentPeriods <= 0)
{ {
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED +
"The number of rent periods must be greater than 0!"); "The number of rent periods must be greater than 0!");
event.setCancelled(true); event.setCancelled(true);
event.getBlock().breakNaturally(); event.getBlock().breakNaturally();
return; return;
} }
} }
if(claim.isAdminClaim() && !RealEstate.perms.has(player, "realestate.admin"))// admin may rent admin claims if(claim.isAdminClaim() && !RealEstate.perms.has(player, "realestate.admin"))// admin may rent admin claims
{ {
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "You don't have the permission to rent admin claims!"); player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "You don't have the permission to rent admin claims!");
event.setCancelled(true); event.setCancelled(true);
event.getBlock().breakNaturally(); event.getBlock().breakNaturally();
return; return;
} }
else if(type.equals("claim") && !player.getUniqueId().equals(claim.ownerID))// only the owner may sell his claim else if(type.equals("claim") && !player.getUniqueId().equals(claim.ownerID))// only the owner may sell his claim
{ {
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "You can only rent claims you own!"); player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "You can only rent claims you own!");
event.setCancelled(true); event.setCancelled(true);
event.getBlock().breakNaturally(); event.getBlock().breakNaturally();
return; return;
} }
// all should be good, we can create the rent // all should be good, we can create the rent
event.setCancelled(true); event.setCancelled(true);
RealEstate.transactionsStore.rent(claim, player, price, event.getBlock().getLocation(), duration, rentPeriods); RealEstate.transactionsStore.rent(claim, player, price, event.getBlock().getLocation(), duration, rentPeriods);
} }
else if(RealEstate.instance.config.cfgLeaseKeywords.contains(event.getLine(0).toLowerCase()))// we want to rent it else if(RealEstate.instance.config.cfgLeaseKeywords.contains(event.getLine(0).toLowerCase()))// we want to rent it
{ {
if(!RealEstate.instance.config.cfgEnableLease) if(!RealEstate.instance.config.cfgEnableLease)
{ {
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "Leasing is disabled!"); player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "Leasing is disabled!");
event.setCancelled(true); event.setCancelled(true);
event.getBlock().breakNaturally(); event.getBlock().breakNaturally();
return; return;
} }
String type = claim.parent == null ? "claim" : "subclaim"; String type = claim.parent == null ? "claim" : "subclaim";
if(!RealEstate.perms.has(player, "realestate." + type + ".lease")) if(!RealEstate.perms.has(player, "realestate." + type + ".lease"))
{ {
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "You don't have the permission to lease " + type + "s!"); player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "You don't have the permission to lease " + type + "s!");
event.setCancelled(true); event.setCancelled(true);
event.getBlock().breakNaturally(); event.getBlock().breakNaturally();
return; return;
} }
// check for a valid price // check for a valid price
double price; double price;
try try
{ {
price = getDouble(event, 1, RealEstate.instance.config.cfgPriceLeasePerBlock * claim.getArea()); price = getDouble(event, 1, RealEstate.instance.config.cfgPriceLeasePerBlock * claim.getArea());
} }
catch (NumberFormatException e) catch (NumberFormatException e)
{ {
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "The price you entered is not a valid number!"); player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "The price you entered is not a valid number!");
event.setCancelled(true); event.setCancelled(true);
event.getBlock().breakNaturally(); event.getBlock().breakNaturally();
return; return;
} }
if(price <= 0) if(price <= 0)
{ {
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "The price must be greater than 0!"); player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "The price must be greater than 0!");
event.setCancelled(true); event.setCancelled(true);
event.getBlock().breakNaturally(); event.getBlock().breakNaturally();
return; return;
} }
if(event.getLine(2).isEmpty()) if(event.getLine(2).isEmpty())
{ {
event.setLine(2, "" + RealEstate.instance.config.cfgLeasePayments); event.setLine(2, "" + RealEstate.instance.config.cfgLeasePayments);
} }
int paymentsCount; int paymentsCount;
try try
{ {
paymentsCount = Integer.parseInt(event.getLine(2)); paymentsCount = Integer.parseInt(event.getLine(2));
} }
catch(Exception e) catch(Exception e)
{ {
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED +
"The number of payments you enterred is not a valid number!"); "The number of payments you enterred is not a valid number!");
event.setCancelled(true); event.setCancelled(true);
event.getBlock().breakNaturally(); event.getBlock().breakNaturally();
return; return;
} }
if(event.getLine(3).isEmpty()) if(event.getLine(3).isEmpty())
{ {
event.setLine(3, RealEstate.instance.config.cfgLeaseTime); event.setLine(3, RealEstate.instance.config.cfgLeaseTime);
} }
int frequency = parseDuration(event.getLine(3)); int frequency = parseDuration(event.getLine(3));
if(frequency == 0) if(frequency == 0)
{ {
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "Couldn't read the date!\n" + player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "Couldn't read the date!\n" +
"Date must be formatted as follow" + ChatColor.GREEN + "10 weeks" + ChatColor.RED + " or " + "Date must be formatted as follow" + ChatColor.GREEN + "10 weeks" + ChatColor.RED + " or " +
ChatColor.GREEN + "3 days" + ChatColor.RED + " or " + ChatColor.GREEN + "1 week 3 days"); ChatColor.GREEN + "3 days" + ChatColor.RED + " or " + ChatColor.GREEN + "1 week 3 days");
event.setCancelled(true); event.setCancelled(true);
event.getBlock().breakNaturally(); event.getBlock().breakNaturally();
return; return;
} }
if(claim.isAdminClaim() && !RealEstate.perms.has(player, "realestate.admin"))// admin may rent admin claims if(claim.isAdminClaim() && !RealEstate.perms.has(player, "realestate.admin"))// admin may rent admin claims
{ {
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "You don't have the permission to lease admin claims!"); player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "You don't have the permission to lease admin claims!");
event.setCancelled(true); event.setCancelled(true);
event.getBlock().breakNaturally(); event.getBlock().breakNaturally();
return; return;
} }
else if(type.equals("claim") && !player.getUniqueId().equals(claim.ownerID))// only the owner may sell his claim else if(type.equals("claim") && !player.getUniqueId().equals(claim.ownerID))// only the owner may sell his claim
{ {
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "You can only lease claims you own!"); player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "You can only lease claims you own!");
event.setCancelled(true); event.setCancelled(true);
event.getBlock().breakNaturally(); event.getBlock().breakNaturally();
return; return;
} }
// all should be good, we can create the rent // all should be good, we can create the rent
event.setCancelled(true); event.setCancelled(true);
RealEstate.transactionsStore.lease(claim, player, price, event.getBlock().getLocation(), frequency, paymentsCount); RealEstate.transactionsStore.lease(claim, player, price, event.getBlock().getLocation(), frequency, paymentsCount);
} }
} }
} }
private int parseDuration(String line) private int parseDuration(String line)
{ {
Pattern p = Pattern.compile("^(?:(?<weeks>\\d{1,2}) ?w(?:eeks?)?)? ?(?:(?<days>\\d{1,2}) ?d(?:ays?)?)?$", Pattern.CASE_INSENSITIVE); Pattern p = Pattern.compile("^(?:(?<weeks>\\d{1,2}) ?w(?:eeks?)?)? ?(?:(?<days>\\d{1,2}) ?d(?:ays?)?)?$", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(line); Matcher m = p.matcher(line);
if(!line.isEmpty() && m.matches()) if(!line.isEmpty() && m.matches())
{ {
int ret = 0; int ret = 0;
if(m.group("weeks") != null) if(m.group("weeks") != null)
ret += 7 * Integer.parseInt(m.group("weeks")); ret += 7 * Integer.parseInt(m.group("weeks"));
if(m.group("days") != null) if(m.group("days") != null)
ret += Integer.parseInt(m.group("days")); ret += Integer.parseInt(m.group("days"));
return ret; return ret;
} }
return 0; return 0;
} }
private double getDouble(SignChangeEvent event, int line, double defaultValue) throws NumberFormatException private double getDouble(SignChangeEvent event, int line, double defaultValue) throws NumberFormatException
{ {
if(event.getLine(line).isEmpty())// if no price precised, make it the default one if(event.getLine(line).isEmpty())// if no price precised, make it the default one
{ {
event.setLine(line, Double.toString(defaultValue)); event.setLine(line, Double.toString(defaultValue));
} }
return Double.parseDouble(event.getLine(line)); return Double.parseDouble(event.getLine(line));
} }
@EventHandler @EventHandler
public void onPlayerInteract(PlayerInteractEvent event) public void onPlayerInteract(PlayerInteractEvent event)
{ {
if(event.getAction().equals(Action.RIGHT_CLICK_BLOCK) && event.getHand().equals(EquipmentSlot.HAND) && if(event.getAction().equals(Action.RIGHT_CLICK_BLOCK) && event.getHand().equals(EquipmentSlot.HAND) &&
event.getClickedBlock().getState() instanceof Sign) event.getClickedBlock().getState() instanceof Sign)
{ {
Sign sign = (Sign)event.getClickedBlock().getState(); Sign sign = (Sign)event.getClickedBlock().getState();
// it is a real estate sign // it is a real estate sign
if(ChatColor.stripColor(sign.getLine(0)).equalsIgnoreCase(ChatColor.stripColor(RealEstate.instance.config.cfgSignsHeader))) if(ChatColor.stripColor(sign.getLine(0)).equalsIgnoreCase(ChatColor.stripColor(RealEstate.instance.config.cfgSignsHeader)))
{ {
Player player = event.getPlayer(); Player player = event.getPlayer();
Claim claim = GriefPrevention.instance.dataStore.getClaimAt(event.getClickedBlock().getLocation(), false, null); Claim claim = GriefPrevention.instance.dataStore.getClaimAt(event.getClickedBlock().getLocation(), false, null);
if(!RealEstate.transactionsStore.anyTransaction(claim)) if(!RealEstate.transactionsStore.anyTransaction(claim))
{ {
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED +
"This claim is no longer for rent or for sell, sorry..."); "This claim is no longer for rent, sell or lease, sorry...");
event.getClickedBlock().breakNaturally(); event.getClickedBlock().breakNaturally();
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
Transaction tr = RealEstate.transactionsStore.getTransaction(claim); Transaction tr = RealEstate.transactionsStore.getTransaction(claim);
if(player.isSneaking()) if(player.isSneaking())
tr.preview(player); tr.preview(player);
else else
tr.interact(player); tr.interact(player);
} }
} }
} }
@EventHandler @EventHandler
public void onBreakBlock(BlockBreakEvent event) public void onBreakBlock(BlockBreakEvent event)
{ {
if(event.getBlock().getState() instanceof Sign) if(event.getBlock().getState() instanceof Sign)
{ {
Claim claim = GriefPrevention.instance.dataStore.getClaimAt(event.getBlock().getLocation(), false, null); Claim claim = GriefPrevention.instance.dataStore.getClaimAt(event.getBlock().getLocation(), false, null);
if(claim != null) if(claim != null)
{ {
Transaction tr = RealEstate.transactionsStore.getTransaction(claim); Transaction tr = RealEstate.transactionsStore.getTransaction(claim);
if(tr != null && event.getBlock().equals(tr.getHolder())) if(tr != null && event.getBlock().equals(tr.getHolder()))
{ {
if(event.getPlayer() != null && !tr.getOwner().equals(event.getPlayer().getUniqueId()) && if(event.getPlayer() != null && tr.getOwner() != null && !event.getPlayer().getUniqueId().equals(tr.getOwner()) &&
!RealEstate.perms.has(event.getPlayer(), "realestate.destroysigns")) !RealEstate.perms.has(event.getPlayer(), "realestate.destroysigns"))
{ {
event.getPlayer().sendMessage(RealEstate.instance.config.chatPrefix + event.getPlayer().sendMessage(RealEstate.instance.config.chatPrefix +
ChatColor.RED + "Only the author of the sell/rent sign is allowed to destroy it"); ChatColor.RED + "Only the author of the sell/rent/lease sign is allowed to destroy it");
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
// the sign has been destroy, we can try to cancel the transaction else if(event.getPlayer() != null && tr.getOwner() == null && !RealEstate.perms.has(event.getPlayer(), "realestate.admin"))
if(!tr.tryCancelTransaction(event.getPlayer())) {
{ event.getPlayer().sendMessage(RealEstate.instance.config.chatPrefix +
event.setCancelled(true); ChatColor.RED + "Only an admin is allowed to destroy this sign");
} event.setCancelled(true);
} return;
} }
} // the sign has been destroy, we can try to cancel the transaction
} if(!tr.tryCancelTransaction(event.getPlayer()))
} {
event.setCancelled(true);
}
}
}
}
}
}

View File

@ -1,228 +1,248 @@
package me.EtienneDx.RealEstate; package me.EtienneDx.RealEstate;
import java.io.File; import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.bukkit.configuration.serialization.ConfigurationSerialization; import org.bukkit.configuration.serialization.ConfigurationSerialization;
import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import com.earth2me.essentials.Essentials; import com.earth2me.essentials.Essentials;
import co.aikar.commands.BukkitCommandManager; import co.aikar.commands.BukkitCommandManager;
import co.aikar.commands.ConditionFailedException; import co.aikar.commands.ConditionFailedException;
import me.EtienneDx.RealEstate.Transactions.BoughtTransaction; import me.EtienneDx.RealEstate.Transactions.BoughtTransaction;
import me.EtienneDx.RealEstate.Transactions.ClaimLease; import me.EtienneDx.RealEstate.Transactions.ClaimLease;
import me.EtienneDx.RealEstate.Transactions.ClaimRent; import me.EtienneDx.RealEstate.Transactions.ClaimRent;
import me.EtienneDx.RealEstate.Transactions.ClaimSell; import me.EtienneDx.RealEstate.Transactions.ClaimSell;
import me.EtienneDx.RealEstate.Transactions.ExitOffer; import me.EtienneDx.RealEstate.Transactions.ExitOffer;
import me.EtienneDx.RealEstate.Transactions.Transaction; import me.EtienneDx.RealEstate.Transactions.Transaction;
import me.EtienneDx.RealEstate.Transactions.TransactionsStore; import me.EtienneDx.RealEstate.Transactions.TransactionsStore;
import me.ryanhamshire.GriefPrevention.Claim; import me.ryanhamshire.GriefPrevention.Claim;
import me.ryanhamshire.GriefPrevention.GriefPrevention; import me.ryanhamshire.GriefPrevention.GriefPrevention;
import net.milkbowl.vault.economy.Economy; import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.permission.Permission; import net.milkbowl.vault.permission.Permission;
public class RealEstate extends JavaPlugin public class RealEstate extends JavaPlugin
{ {
public Logger log; public Logger log;
public Config config; public Config config;
BukkitCommandManager manager; BukkitCommandManager manager;
public final static String pluginDirPath = "plugins" + File.separator + "RealEstate" + File.separator; public final static String pluginDirPath = "plugins" + File.separator + "RealEstate" + File.separator;
public static boolean vaultPresent = false; public static boolean vaultPresent = false;
public static Economy econ = null; public static Economy econ = null;
public static Permission perms = null; public static Permission perms = null;
public static Essentials ess = null; public static Essentials ess = null;
public static RealEstate instance = null; public static RealEstate instance = null;
public static TransactionsStore transactionsStore = null; public static TransactionsStore transactionsStore = null;
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public void onEnable() public void onEnable()
{ {
RealEstate.instance = this; RealEstate.instance = this;
this.log = getLogger(); this.log = getLogger();
if (checkVault()) if (checkVault())
{ {
this.log.info("Vault has been detected and enabled."); this.log.info("Vault has been detected and enabled.");
if (setupEconomy()) if (setupEconomy())
{ {
this.log.info("Vault is using " + econ.getName() + " as the economy plugin."); this.log.info("Vault is using " + econ.getName() + " as the economy plugin.");
} }
else else
{ {
this.log.warning("No compatible economy plugin detected [Vault]."); this.log.warning("No compatible economy plugin detected [Vault].");
this.log.warning("Disabling plugin."); this.log.warning("Disabling plugin.");
getPluginLoader().disablePlugin(this); getPluginLoader().disablePlugin(this);
return; return;
} }
if (setupPermissions()) if (setupPermissions())
{ {
this.log.info("Vault is using " + perms.getName() + " for the permissions."); this.log.info("Vault is using " + perms.getName() + " for the permissions.");
} }
else else
{ {
this.log.warning("No compatible permissions plugin detected [Vault]."); this.log.warning("No compatible permissions plugin detected [Vault].");
this.log.warning("Disabling plugin."); this.log.warning("Disabling plugin.");
getPluginLoader().disablePlugin(this); getPluginLoader().disablePlugin(this);
return; return;
} }
} }
if((ess = (Essentials)getServer().getPluginManager().getPlugin("Essentials")) != null) if((ess = (Essentials)getServer().getPluginManager().getPlugin("Essentials")) != null)
{ {
this.log.info("Found Essentials, using version " + ess.getDescription().getVersion()); this.log.info("Found Essentials, using version " + ess.getDescription().getVersion());
} }
this.config = new Config(); this.config = new Config();
this.config.loadConfig();// loads config or default this.config.loadConfig();// loads config or default
this.config.saveConfig();// save eventual default this.config.saveConfig();// save eventual default
ConfigurationSerialization.registerClass(ClaimSell.class); ConfigurationSerialization.registerClass(ClaimSell.class);
ConfigurationSerialization.registerClass(ClaimRent.class); ConfigurationSerialization.registerClass(ClaimRent.class);
ConfigurationSerialization.registerClass(ClaimLease.class); ConfigurationSerialization.registerClass(ClaimLease.class);
ConfigurationSerialization.registerClass(ExitOffer.class); ConfigurationSerialization.registerClass(ExitOffer.class);
RealEstate.transactionsStore = new TransactionsStore(); RealEstate.transactionsStore = new TransactionsStore();
new REListener().registerEvents(); new REListener().registerEvents();
manager = new BukkitCommandManager(this); manager = new BukkitCommandManager(this);
manager.enableUnstableAPI("help"); manager.enableUnstableAPI("help");
registerConditions(); registerConditions();
manager.registerCommand(new RECommand()); manager.registerCommand(new RECommand());
GriefPrevention.realEstate = new GP_RealEstateHook(); GriefPrevention.realEstate = new GP_RealEstateHook();
} }
private void registerConditions() private void registerConditions()
{ {
manager.getCommandConditions().addCondition("inClaim", (context) -> { manager.getCommandConditions().addCondition("inClaim", (context) -> {
if(context.getIssuer().isPlayer() && if(context.getIssuer().isPlayer() &&
GriefPrevention.instance.dataStore.getClaimAt(context.getIssuer().getPlayer().getLocation(), false, null) != null) GriefPrevention.instance.dataStore.getClaimAt(context.getIssuer().getPlayer().getLocation(), false, null) != null)
{ {
return; return;
} }
throw new ConditionFailedException("You must stand inside of a claim to use this command!"); throw new ConditionFailedException("You must stand inside of a claim to use this command!");
}); });
manager.getCommandConditions().addCondition("inBoughtClaim", (context) -> { manager.getCommandConditions().addCondition("inPendingTransactionClaim", (context) -> {
if(!context.getIssuer().isPlayer()) if(!context.getIssuer().isPlayer())
{ {
throw new ConditionFailedException("Only Players can perform this command!"); throw new ConditionFailedException("Only Players can perform this command!");
} }
Claim c = GriefPrevention.instance.dataStore.getClaimAt(context.getIssuer().getPlayer().getLocation(), false, null); Claim c = GriefPrevention.instance.dataStore.getClaimAt(context.getIssuer().getPlayer().getLocation(), false, null);
if(c == null) if(c == null)
{ {
throw new ConditionFailedException("You must stand inside of a claim to use this command!"); throw new ConditionFailedException("You must stand inside of a claim to use this command!");
} }
Transaction tr = transactionsStore.getTransaction(c); Transaction tr = transactionsStore.getTransaction(c);
if(tr == null || !(tr instanceof BoughtTransaction)) if(tr == null)
{ {
throw new ConditionFailedException("This claim is neither to rent or to lease!"); throw new ConditionFailedException("This claim is neither to rent or to lease!");
} }
}); else if(tr instanceof BoughtTransaction && ((BoughtTransaction)tr).getBuyer() != null)
manager.getCommandConditions().addCondition("partOfBoughtTransaction", context -> { {
if(!context.getIssuer().isPlayer()) throw new ConditionFailedException("This claim already has a buyer!");
{ }
throw new ConditionFailedException("Only Players can perform this command!"); });
} manager.getCommandConditions().addCondition("inBoughtClaim", (context) -> {
Claim c = GriefPrevention.instance.dataStore.getClaimAt(context.getIssuer().getPlayer().getLocation(), false, null); if(!context.getIssuer().isPlayer())
if(c == null) {
{ throw new ConditionFailedException("Only Players can perform this command!");
throw new ConditionFailedException("You must stand inside of a claim to use this command!"); }
} Claim c = GriefPrevention.instance.dataStore.getClaimAt(context.getIssuer().getPlayer().getLocation(), false, null);
Transaction tr = transactionsStore.getTransaction(c); if(c == null)
if(tr == null) {
{ throw new ConditionFailedException("You must stand inside of a claim to use this command!");
throw new ConditionFailedException("This claim is neither to sell, rent or lease!"); }
} Transaction tr = transactionsStore.getTransaction(c);
if(!(tr instanceof BoughtTransaction)) if(tr == null || !(tr instanceof BoughtTransaction))
{ {
throw new ConditionFailedException("This command only applies to rented or leased claims!"); throw new ConditionFailedException("This claim is neither to rent or to lease!");
} }
if((((BoughtTransaction)tr).buyer != null&& ((BoughtTransaction)tr).buyer.equals(context.getIssuer().getPlayer().getUniqueId())) || });
tr.getOwner().equals(context.getIssuer().getPlayer().getUniqueId())) manager.getCommandConditions().addCondition("partOfBoughtTransaction", context -> {
{ if(!context.getIssuer().isPlayer())
return; {
} throw new ConditionFailedException("Only Players can perform this command!");
throw new ConditionFailedException("You are not part of this transaction!"); }
}); Claim c = GriefPrevention.instance.dataStore.getClaimAt(context.getIssuer().getPlayer().getLocation(), false, null);
manager.getCommandConditions().addCondition("partOfRent", context -> { if(c == null)
if(!context.getIssuer().isPlayer()) {
{ throw new ConditionFailedException("You must stand inside of a claim to use this command!");
throw new ConditionFailedException("Only Players can perform this command!"); }
} Transaction tr = transactionsStore.getTransaction(c);
Claim c = GriefPrevention.instance.dataStore.getClaimAt(context.getIssuer().getPlayer().getLocation(), false, null); if(tr == null)
if(c == null) {
{ throw new ConditionFailedException("This claim is neither to sell, rent or lease!");
throw new ConditionFailedException("You must stand inside of a claim to use this command!"); }
} if(!(tr instanceof BoughtTransaction))
Transaction tr = transactionsStore.getTransaction(c); {
if(tr == null) throw new ConditionFailedException("This command only applies to rented or leased claims!");
{ }
throw new ConditionFailedException("This claim is neither to sell, rent or lease!"); if((((BoughtTransaction)tr).buyer != null&& ((BoughtTransaction)tr).buyer.equals(context.getIssuer().getPlayer().getUniqueId())) ||
} tr.getOwner().equals(context.getIssuer().getPlayer().getUniqueId()))
if(!(tr instanceof ClaimRent)) {
{ return;
throw new ConditionFailedException("This command only applies to rented claims!"); }
} throw new ConditionFailedException("You are not part of this transaction!");
if((((ClaimRent)tr).buyer != null && ((ClaimRent)tr).buyer.equals(context.getIssuer().getPlayer().getUniqueId())) || });
tr.getOwner().equals(context.getIssuer().getPlayer().getUniqueId())) manager.getCommandConditions().addCondition("partOfRent", context -> {
{ if(!context.getIssuer().isPlayer())
return; {
} throw new ConditionFailedException("Only Players can perform this command!");
throw new ConditionFailedException("You are not part of this transaction!"); }
}); Claim c = GriefPrevention.instance.dataStore.getClaimAt(context.getIssuer().getPlayer().getLocation(), false, null);
manager.getCommandConditions().addCondition(Double.class, "positiveDouble", (c, exec, value) -> { if(c == null)
if(value > 0) return; {
throw new ConditionFailedException("The value must be greater than zero!"); throw new ConditionFailedException("You must stand inside of a claim to use this command!");
}); }
} Transaction tr = transactionsStore.getTransaction(c);
if(tr == null)
public void addLogEntry(String entry) {
{ throw new ConditionFailedException("This claim is neither to sell, rent or lease!");
try }
{ if(!(tr instanceof ClaimRent))
File logFile = new File(this.config.logFilePath); {
if (!logFile.exists()) { throw new ConditionFailedException("This command only applies to rented claims!");
logFile.createNewFile(); }
} if((((ClaimRent)tr).buyer != null && ((ClaimRent)tr).buyer.equals(context.getIssuer().getPlayer().getUniqueId())) ||
FileWriter fw = new FileWriter(logFile, true); tr.getOwner().equals(context.getIssuer().getPlayer().getUniqueId()))
PrintWriter pw = new PrintWriter(fw); {
return;
pw.println(entry); }
pw.flush(); throw new ConditionFailedException("You are not part of this transaction!");
pw.close(); });
} manager.getCommandConditions().addCondition(Double.class, "positiveDouble", (c, exec, value) -> {
catch (IOException e) if(value > 0) return;
{ throw new ConditionFailedException("The value must be greater than zero!");
e.printStackTrace(); });
} }
}
public void addLogEntry(String entry)
private boolean checkVault() {
{ try
vaultPresent = getServer().getPluginManager().getPlugin("Vault") != null; {
return vaultPresent; File logFile = new File(this.config.logFilePath);
} if (!logFile.exists()) {
logFile.createNewFile();
private boolean setupEconomy() }
{ FileWriter fw = new FileWriter(logFile, true);
RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class); PrintWriter pw = new PrintWriter(fw);
if (rsp == null) {
return false; pw.println(entry);
} pw.flush();
econ = (Economy)rsp.getProvider(); pw.close();
return econ != null; }
} catch (IOException e)
{
private boolean setupPermissions() e.printStackTrace();
{ }
RegisteredServiceProvider<Permission> rsp = getServer().getServicesManager().getRegistration(Permission.class); }
perms = (Permission)rsp.getProvider();
return perms != null; private boolean checkVault()
} {
} vaultPresent = getServer().getPluginManager().getPlugin("Vault") != null;
return vaultPresent;
}
private boolean setupEconomy()
{
RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
if (rsp == null) {
return false;
}
econ = (Economy)rsp.getProvider();
return econ != null;
}
private boolean setupPermissions()
{
RegisteredServiceProvider<Permission> rsp = getServer().getServicesManager().getRegistration(Permission.class);
perms = (Permission)rsp.getProvider();
return perms != null;
}
}

View File

@ -1,63 +1,68 @@
package me.EtienneDx.RealEstate.Transactions; package me.EtienneDx.RealEstate.Transactions;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.block.Sign; import org.bukkit.block.Sign;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import me.EtienneDx.RealEstate.RealEstate; import me.EtienneDx.RealEstate.RealEstate;
import me.ryanhamshire.GriefPrevention.Claim; import me.ryanhamshire.GriefPrevention.Claim;
public abstract class BoughtTransaction extends ClaimTransaction public abstract class BoughtTransaction extends ClaimTransaction
{ {
public UUID buyer = null; public UUID buyer = null;
public ExitOffer exitOffer = null; public ExitOffer exitOffer = null;
public boolean destroyedSign = false; public boolean destroyedSign = false;
public BoughtTransaction(Map<String, Object> map) public BoughtTransaction(Map<String, Object> map)
{ {
super(map); super(map);
if(map.get("buyer") != null) if(map.get("buyer") != null)
buyer = UUID.fromString((String)map.get("buyer")); buyer = UUID.fromString((String)map.get("buyer"));
if(map.get("exitOffer") != null) if(map.get("exitOffer") != null)
exitOffer = (ExitOffer) map.get("exitOffer"); exitOffer = (ExitOffer) map.get("exitOffer");
if(map.get("destroyedSign") != null)// may be the case on upgrading from 0.0.1-SNAPSHOT if(map.get("destroyedSign") != null)// may be the case on upgrading from 0.0.1-SNAPSHOT
destroyedSign = (boolean) map.get("destroyedSign"); destroyedSign = (boolean) map.get("destroyedSign");
} }
public BoughtTransaction(Claim claim, Player player, double price, Location sign) public BoughtTransaction(Claim claim, Player player, double price, Location sign)
{ {
super(claim, player, price, sign); super(claim, player, price, sign);
} }
@Override @Override
public Map<String, Object> serialize() public Map<String, Object> serialize()
{ {
Map<String, Object> map = super.serialize(); Map<String, Object> map = super.serialize();
if(buyer != null) if(buyer != null)
map.put("buyer", buyer.toString()); map.put("buyer", buyer.toString());
if(exitOffer != null) if(exitOffer != null)
map.put("exitOffer", exitOffer); map.put("exitOffer", exitOffer);
map.put("destroyedSign", destroyedSign); map.put("destroyedSign", destroyedSign);
return map; return map;
} }
public void destroySign() public void destroySign()
{ {
if((this instanceof ClaimRent &&RealEstate.instance.config.cfgDestroyRentSigns) || if((this instanceof ClaimRent &&RealEstate.instance.config.cfgDestroyRentSigns) ||
(this instanceof ClaimLease &&RealEstate.instance.config.cfgDestroyLeaseSigns)) (this instanceof ClaimLease &&RealEstate.instance.config.cfgDestroyLeaseSigns))
{ {
if(!destroyedSign && getHolder().getState() instanceof Sign) if(!destroyedSign && getHolder().getState() instanceof Sign)
getHolder().breakNaturally(); getHolder().breakNaturally();
destroyedSign = true; destroyedSign = true;
} }
} }
public UUID getBuyer() public UUID getBuyer()
{ {
return buyer; return buyer;
} }
}
public void setOwner(UUID newOwner)
{
this.owner = newOwner;
}
}

View File

@ -1,179 +1,187 @@
package me.EtienneDx.RealEstate.Transactions; package me.EtienneDx.RealEstate.Transactions;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import com.earth2me.essentials.User; import com.earth2me.essentials.User;
import me.EtienneDx.RealEstate.RealEstate; import me.EtienneDx.RealEstate.RealEstate;
import me.EtienneDx.RealEstate.Utils; import me.EtienneDx.RealEstate.Utils;
import me.ryanhamshire.GriefPrevention.Claim; import me.ryanhamshire.GriefPrevention.Claim;
import me.ryanhamshire.GriefPrevention.GriefPrevention; import me.ryanhamshire.GriefPrevention.GriefPrevention;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
import org.bukkit.Bukkit; import java.util.UUID;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer; import org.bukkit.Bukkit;
import org.bukkit.block.Sign; import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
public class ClaimSell extends ClaimTransaction import org.bukkit.block.Sign;
{
public ClaimSell(Claim claim, Player player, double price, Location sign) public class ClaimSell extends ClaimTransaction
{ {
super(claim, player, price, sign); public ClaimSell(Claim claim, Player player, double price, Location sign)
} {
super(claim, player, price, sign);
@Override }
public void update()
{ @Override
if(sign.getBlock().getState() instanceof Sign) public void update()
{ {
Sign s = (Sign) sign.getBlock().getState(); if(sign.getBlock().getState() instanceof Sign)
s.setLine(0, RealEstate.instance.config.cfgSignsHeader); {
s.setLine(1, ChatColor.DARK_GREEN + RealEstate.instance.config.cfgReplaceSell); Sign s = (Sign) sign.getBlock().getState();
s.setLine(2, owner != null ? Utils.getSignString(Bukkit.getOfflinePlayer(owner).getName()) : "SERVER"); s.setLine(0, RealEstate.instance.config.cfgSignsHeader);
if(RealEstate.instance.config.cfgUseCurrencySymbol) s.setLine(1, ChatColor.DARK_GREEN + RealEstate.instance.config.cfgReplaceSell);
{ s.setLine(2, owner != null ? Utils.getSignString(Bukkit.getOfflinePlayer(owner).getName()) : "SERVER");
s.setLine(3, RealEstate.instance.config.cfgCurrencySymbol + " " + price); if(RealEstate.instance.config.cfgUseCurrencySymbol)
} {
else s.setLine(3, RealEstate.instance.config.cfgCurrencySymbol + " " + price);
{ }
s.setLine(3, price + " " + RealEstate.econ.currencyNamePlural()); else
} {
s.update(true); s.setLine(3, price + " " + RealEstate.econ.currencyNamePlural());
} }
else s.update(true);
{ }
RealEstate.transactionsStore.cancelTransaction(this); else
} {
} RealEstate.transactionsStore.cancelTransaction(this);
}
@Override }
public boolean tryCancelTransaction(Player p)
{ @Override
RealEstate.transactionsStore.cancelTransaction(this); public boolean tryCancelTransaction(Player p)
return true; {
} RealEstate.transactionsStore.cancelTransaction(this);
return true;
@Override }
public void interact(Player player)
{ @Override
Claim claim = GriefPrevention.instance.dataStore.getClaimAt(sign, false, null);// getting by id creates errors for subclaims public void interact(Player player)
if(claim == null) {
{ Claim claim = GriefPrevention.instance.dataStore.getClaimAt(sign, false, null);// getting by id creates errors for subclaims
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "This claim does not exist!"); if(claim == null)
RealEstate.transactionsStore.cancelTransaction(claim); {
return; player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "This claim does not exist!");
} RealEstate.transactionsStore.cancelTransaction(claim);
String claimType = claim.parent == null ? "claim" : "subclaim"; return;
}
if (owner.equals(player.getUniqueId())) String claimType = claim.parent == null ? "claim" : "subclaim";
{
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "You already own this " + claimType + "!"); if (player.getUniqueId().equals(owner))
return; {
} player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "You already own this " + claimType + "!");
if(claim.parent == null && !owner.equals(claim.ownerID)) return;
{ }
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + Bukkit.getPlayer(owner).getDisplayName() + if(claim.parent == null && owner != null && !owner.equals(claim.ownerID))
" does not have the right to sell this " + claimType + "!"); {
RealEstate.transactionsStore.cancelTransaction(claim); player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + Bukkit.getPlayer(owner).getDisplayName() +
return; " does not have the right to sell this " + claimType + "!");
} RealEstate.transactionsStore.cancelTransaction(claim);
if(!player.hasPermission("realestate." + claimType + ".buy")) return;
{ }
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "You do not have the permission to purchase " + if(!player.hasPermission("realestate." + claimType + ".buy"))
claimType + "s!"); {
return; player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "You do not have the permission to purchase " +
} claimType + "s!");
// for real claims, you may need to have enough claim blocks in reserve to purchase it (if transferClaimBlocks is false) return;
if(claimType.equalsIgnoreCase("claim") && !RealEstate.instance.config.cfgTransferClaimBlocks && }
GriefPrevention.instance.dataStore.getPlayerData(player.getUniqueId()).getRemainingClaimBlocks() < claim.getArea()) // for real claims, you may need to have enough claim blocks in reserve to purchase it (if transferClaimBlocks is false)
{ if(claimType.equalsIgnoreCase("claim") && !RealEstate.instance.config.cfgTransferClaimBlocks &&
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + GriefPrevention.instance.dataStore.getPlayerData(player.getUniqueId()).getRemainingClaimBlocks() < claim.getArea())
"You don't have enough claim blocks to purchase this claim, you need to get " + ChatColor.DARK_GREEN + {
(claim.getArea() - GriefPrevention.instance.dataStore.getPlayerData(player.getUniqueId()).getRemainingClaimBlocks()) + player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED +
ChatColor.RED + " more blocks!"); "You don't have enough claim blocks to purchase this claim, you need to get " + ChatColor.DARK_GREEN +
return; (claim.getArea() - GriefPrevention.instance.dataStore.getPlayerData(player.getUniqueId()).getRemainingClaimBlocks()) +
} ChatColor.RED + " more blocks!");
// the player has the right to buy, let's make the payment return;
}
if(Utils.makePayment(owner, player.getUniqueId(), price, false, true))// if payment succeed // the player has the right to buy, let's make the payment
{
Utils.transferClaim(claim, player.getUniqueId(), owner); if(Utils.makePayment(owner, player.getUniqueId(), price, false, true))// if payment succeed
// normally, this is always the case, so it's not necessary, but until I proven my point, here {
if(claim.parent != null || claim.ownerID.equals(player.getUniqueId())) Utils.transferClaim(claim, player.getUniqueId(), owner);
{ // normally, this is always the case, so it's not necessary, but until I proven my point, here
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + "You have successfully purchased this " + claimType + if(claim.parent != null || claim.ownerID.equals(player.getUniqueId()))
" for " + ChatColor.GREEN + price + RealEstate.econ.currencyNamePlural()); {
RealEstate.instance.addLogEntry( player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + "You have successfully purchased this " + claimType +
"[" + RealEstate.transactionsStore.dateFormat.format(RealEstate.transactionsStore.date) + "] " + player.getName() + " for " + ChatColor.GREEN + price + RealEstate.econ.currencyNamePlural());
" has purchased a " + claimType + " at " + RealEstate.instance.addLogEntry(
"[" + player.getLocation().getWorld() + ", " + "[" + RealEstate.transactionsStore.dateFormat.format(RealEstate.transactionsStore.date) + "] " + player.getName() +
"X: " + player.getLocation().getBlockX() + ", " + " has purchased a " + claimType + " at " +
"Y: " + player.getLocation().getBlockY() + ", " + "[" + player.getLocation().getWorld() + ", " +
"Z: " + player.getLocation().getBlockZ() + "] " + "X: " + player.getLocation().getBlockX() + ", " +
"Price: " + price + " " + RealEstate.econ.currencyNamePlural()); "Y: " + player.getLocation().getBlockY() + ", " +
"Z: " + player.getLocation().getBlockZ() + "] " +
if(RealEstate.instance.config.cfgMessageOwner) "Price: " + price + " " + RealEstate.econ.currencyNamePlural());
{
OfflinePlayer oldOwner = Bukkit.getOfflinePlayer(owner); if(RealEstate.instance.config.cfgMessageOwner)
if(oldOwner.isOnline()) {
{ OfflinePlayer oldOwner = Bukkit.getOfflinePlayer(owner);
((Player) oldOwner).sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + player.getDisplayName() + if(oldOwner.isOnline())
" has purchased your " + claimType + " at " + ChatColor.BLUE + {
"[" + player.getLocation().getWorld().getName() + ", " + ((Player) oldOwner).sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + player.getDisplayName() +
"X: " + player.getLocation().getBlockX() + ", " + " has purchased your " + claimType + " at " + ChatColor.BLUE +
"Y: " + player.getLocation().getBlockY() + ", " + "[" + player.getLocation().getWorld().getName() + ", " +
"Z: " + player.getLocation().getBlockZ() + "] " + ChatColor.AQUA + "for " + ChatColor.GREEN + "X: " + player.getLocation().getBlockX() + ", " +
price + " " + RealEstate.econ.currencyNamePlural()); "Y: " + player.getLocation().getBlockY() + ", " +
} "Z: " + player.getLocation().getBlockZ() + "] " + ChatColor.AQUA + "for " + ChatColor.GREEN +
else if(RealEstate.instance.config.cfgMailOffline && RealEstate.ess != null) price + " " + RealEstate.econ.currencyNamePlural());
{ }
User u = RealEstate.ess.getUser(owner); else if(RealEstate.instance.config.cfgMailOffline && RealEstate.ess != null)
u.addMail(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + player.getDisplayName() + {
" has purchased your " + claimType + " at " + ChatColor.BLUE + User u = RealEstate.ess.getUser(owner);
"[" + player.getLocation().getWorld().getName() + ", " + u.addMail(RealEstate.instance.config.chatPrefix + ChatColor.AQUA + player.getDisplayName() +
"X: " + player.getLocation().getBlockX() + ", " + " has purchased your " + claimType + " at " + ChatColor.BLUE +
"Y: " + player.getLocation().getBlockY() + ", " + "[" + player.getLocation().getWorld().getName() + ", " +
"Z: " + player.getLocation().getBlockZ() + "] " + ChatColor.AQUA + "for " + ChatColor.GREEN + "X: " + player.getLocation().getBlockX() + ", " +
price + " " + RealEstate.econ.currencyNamePlural());; "Y: " + player.getLocation().getBlockY() + ", " +
} "Z: " + player.getLocation().getBlockZ() + "] " + ChatColor.AQUA + "for " + ChatColor.GREEN +
} price + " " + RealEstate.econ.currencyNamePlural());;
} }
else }
{ }
player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "Cannot purchase claim!"); else
return; {
} player.sendMessage(RealEstate.instance.config.chatPrefix + ChatColor.RED + "Cannot purchase claim!");
RealEstate.transactionsStore.cancelTransaction(claim); return;
} }
} RealEstate.transactionsStore.cancelTransaction(claim);
}
@Override }
public void preview(Player player)
{ @Override
Claim claim = GriefPrevention.instance.dataStore.getClaimAt(sign, false, null); public void preview(Player player)
String msg = ""; {
if(player.hasPermission("realestate.info")) Claim claim = GriefPrevention.instance.dataStore.getClaimAt(sign, false, null);
{ String msg = "";
String claimType = claim.parent == null ? "claim" : "subclaim"; if(player.hasPermission("realestate.info"))
msg = ChatColor.BLUE + "-----= " + ChatColor.WHITE + "[" + ChatColor.GOLD + "RealEstate Sale Info" + ChatColor.WHITE + "]" + {
ChatColor.BLUE + " =-----\n"; String claimType = claim.parent == null ? "claim" : "subclaim";
msg += ChatColor.AQUA + "This " + claimType + " is for sale for " + msg = ChatColor.BLUE + "-----= " + ChatColor.WHITE + "[" + ChatColor.GOLD + "RealEstate Sale Info" + ChatColor.WHITE + "]" +
ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural() + "\n"; ChatColor.BLUE + " =-----\n";
if(claimType.equalsIgnoreCase("claim")) msg += ChatColor.AQUA + "This " + claimType + " is for sale for " +
{ ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural() + "\n";
msg += ChatColor.AQUA + "The current owner is: " + ChatColor.GREEN + claim.getOwnerName(); if(claimType.equalsIgnoreCase("claim"))
} {
else msg += ChatColor.AQUA + "The current owner is: " + ChatColor.GREEN + claim.getOwnerName();
{ }
msg += ChatColor.AQUA + "The main claim owner is: " + ChatColor.GREEN + claim.getOwnerName() + "\n"; else
msg += ChatColor.LIGHT_PURPLE + "Note: " + ChatColor.AQUA + "You will only buy access to this subclaim!"; {
} msg += ChatColor.AQUA + "The main claim owner is: " + ChatColor.GREEN + claim.getOwnerName() + "\n";
} msg += ChatColor.LIGHT_PURPLE + "Note: " + ChatColor.AQUA + "You will only buy access to this subclaim!";
else }
{ }
msg = RealEstate.instance.config.chatPrefix + ChatColor.RED + "You don't have the permission to view real estate informations!"; else
} {
player.sendMessage(msg); msg = RealEstate.instance.config.chatPrefix + ChatColor.RED + "You don't have the permission to view real estate informations!";
} }
} player.sendMessage(msg);
}
@Override
public void setOwner(UUID newOwner)
{
this.owner = newOwner;
}
}

View File

@ -1,16 +1,17 @@
package me.EtienneDx.RealEstate.Transactions; package me.EtienneDx.RealEstate.Transactions;
import java.util.UUID; import java.util.UUID;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
public interface Transaction public interface Transaction
{ {
public Block getHolder(); public Block getHolder();
public UUID getOwner(); public UUID getOwner();
public void interact(Player player); public void setOwner(UUID newOwner);
public void preview(Player player); public void interact(Player player);
public void update(); public void preview(Player player);
public boolean tryCancelTransaction(Player p); public void update();
} public boolean tryCancelTransaction(Player p);
}