Skip to content

Commit

Permalink
Added signs removal to cancel transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
EtienneDx committed Apr 25, 2019
1 parent b1c5057 commit f71b496
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 5 deletions.
5 changes: 4 additions & 1 deletion plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ permissions:
children:
realestate.info: true
realestate.admin: true
realestate.destroysigns: true
realestate.claim.buy: true
realestate.claim.sell: true
realestate.claim.rent: true
Expand Down Expand Up @@ -77,4 +78,6 @@ permissions:
realestate.ignore.limit:
description: Allows the player to buy bigger claims than his available claim size
default: false

realestate.destroysigns:
description: Allows the player to destroy any sign representing a transaction
default: op
21 changes: 17 additions & 4 deletions src/me/EtienneDx/RealEstate/ClaimSell.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.block.Sign;

public class ClaimSell implements ConfigurationSerializable
public class ClaimSell implements ConfigurationSerializable, Transaction
{
public long claimId;
long claimId;
private UUID owner = null;
private double price;
private Sign sign = null;
Sign sign = null;

public ClaimSell(Map<String, Object> map)
{
Expand All @@ -34,7 +35,7 @@ public ClaimSell(Map<String, Object> map)
public ClaimSell(Claim claim, Player player, double price, Sign sign)
{
this.claimId = claim.getID();
this.owner = player.getUniqueId();
this.owner = player != null ? player.getUniqueId() : null;
this.price = price;
this.sign = sign;
}
Expand Down Expand Up @@ -64,4 +65,16 @@ public Map<String, Object> serialize()

return map;
}

@Override
public Block getHolder()
{
return (Block) sign;
}

@Override
public UUID getOwner()
{
return owner;
}
}
21 changes: 21 additions & 0 deletions src/me/EtienneDx/RealEstate/REListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.SignChangeEvent;
import org.bukkit.plugin.PluginManager;

Expand Down Expand Up @@ -95,4 +96,24 @@ else if(type.equals("claim") && !player.getName().equalsIgnoreCase(claim.getOwne
}
}
}

@EventHandler
public void onBreakBlock(BlockBreakEvent event)
{
if(event.getBlock() instanceof Sign)
{
Claim claim = GriefPrevention.instance.dataStore.getClaimAt(event.getBlock().getLocation(), false, null);
Transaction tr = RealEstate.transactionsStore.getTransaction(claim);
if(tr.getHolder() == event.getBlock() && !tr.getOwner().equals(event.getPlayer().getUniqueId()) &&
!RealEstate.perms.has(event.getPlayer(), "realestate.destroysigns"))
{
event.getPlayer().sendMessage(RealEstate.instance.dataStore.chatPrefix +
ChatColor.RED + "Only the author of the sell/rent sign is allowed to destroy it");
event.setCancelled(true);
return;
}
// the sign has been destroy, we can remove the transaction
RealEstate.transactionsStore.cancelTransaction(claim);
}
}
}
11 changes: 11 additions & 0 deletions src/me/EtienneDx/RealEstate/Transaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package me.EtienneDx.RealEstate;

import java.util.UUID;

import org.bukkit.block.Block;

public interface Transaction
{
public Block getHolder();
public UUID getOwner();
}
49 changes: 49 additions & 0 deletions src/me/EtienneDx/RealEstate/TransactionsStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,25 @@

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import org.bukkit.Bukkit;
import org.bukkit.block.Sign;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;

import me.ryanhamshire.GriefPrevention.Claim;
import net.md_5.bungee.api.ChatColor;

public class TransactionsStore
{
public final String dataFilePath = RealEstate.pluginDirPath + "transactions.data";
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();

public HashMap<String, ClaimSell> claimSell;

Expand Down Expand Up @@ -53,10 +60,52 @@ public void sell(Claim claim, Player player, double price, Sign sign)
claimSell.put(claim.getID().toString(), cs);
cs.updateSign();
saveData();

RealEstate.instance.addLogEntry("[" + this.dateFormat.format(this.date) + "] " + player.getName() +
" has made " + (claim.isAdminClaim() ? "an admin" : "a") + " " + (claim.parent == null ? "claim" : "subclaim") + " for sale at " +
"[" + player.getLocation().getWorld() + ", " +
"X: " + player.getLocation().getBlockX() + ", " +
"Y: " + player.getLocation().getBlockY() + ", " +
"Z: " + player.getLocation().getBlockZ() + "] " +
"Price: " + price + " " + RealEstate.econ.currencyNamePlural());

if(player != null)
{
player.sendMessage(RealEstate.instance.dataStore.chatPrefix + ChatColor.AQUA + "You have successfully created " +
(claim.isAdminClaim() ? "an admin" : "a") + " " + (claim.parent == null ? "claim" : "subclaim") + " sale for " +
ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural());
}
if(RealEstate.instance.dataStore.cfgBroadcastSell)
{
for(Player p : Bukkit.getServer().getOnlinePlayers())
{
if(p != player)
{
p.sendMessage(RealEstate.instance.dataStore.chatPrefix + ChatColor.DARK_GREEN + player.getDisplayName() +
ChatColor.AQUA + " has put " +
(claim.isAdminClaim() ? "an admin" : "a") + " " + (claim.parent == null ? "claim" : "subclaim") + " for sale for " +
ChatColor.GREEN + price + " " + RealEstate.econ.currencyNamePlural());
}
}
}
}

public boolean anyTransaction(Claim claim)
{
return claimSell.containsKey(claim.getID().toString());
}

public Transaction getTransaction(Claim claim)
{
if(claimSell.containsKey(claim.getID().toString()))
return claimSell.get(claim.getID().toString());
return null;
}

public void cancelTransaction(Claim claim)
{
if(claimSell.containsKey(claim.getID().toString()))
claimSell.remove(claim.getID().toString());
saveData();
}
}

0 comments on commit f71b496

Please sign in to comment.