Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AccountBalanceChangeEvent #2

Open
wants to merge 14 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/main/java/org/gestern/gringotts/AccountChest.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ public Location chestLocation() {
}

public boolean matchesLocation(Location loc) {
return matchesLocation(loc, false);
}

public boolean matchesLocation(Location loc, boolean updateContainerLocations) {
if (updateContainerLocations) {
this.containerLocations.clear();
}

if (this.containerLocations.isEmpty()) {
InventoryHolder holder = chest();

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/gestern/gringotts/GringottsAccount.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.gestern.gringotts.api.TransactionResult;
import org.gestern.gringotts.currency.Denomination;
import org.gestern.gringotts.data.DAO;
import org.gestern.gringotts.event.AccountBalanceChangeEvent;

import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -204,6 +205,7 @@ public TransactionResult add(long amount) {
}

if (remaining == 0) {
Bukkit.getPluginManager().callEvent(new AccountBalanceChangeEvent(owner, this.getBalance()));
return TransactionResult.SUCCESS;
}

Expand Down Expand Up @@ -316,6 +318,7 @@ public TransactionResult remove(long amount) {
dao.storeCents(this, cents - remaining);
}

Bukkit.getPluginManager().callEvent(new AccountBalanceChangeEvent(owner, this.getBalance()));
return TransactionResult.SUCCESS;
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.gestern.gringotts.event;

import org.bukkit.Bukkit;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.gestern.gringotts.accountholder.AccountHolder;
import org.jetbrains.annotations.NotNull;

/**
* The type balance change event.
*/
public class AccountBalanceChangeEvent extends Event {
/**
* The constant handlers.
*/
public static final HandlerList handlers = new HandlerList();
/**
* The Holder.
*/
public final AccountHolder holder;
/**
* Account balance.
*/
public final long balance;

/**
* Instantiates a new Calculate start balance event.
*
* @param holder the holder
*/
public AccountBalanceChangeEvent(AccountHolder holder, long balance) {
super(!Bukkit.getServer().isPrimaryThread());

this.holder = holder;
this.balance = balance;
}

/**
* Gets handler list.
*
* @return the handler list
*/
@SuppressWarnings("unused")
public static HandlerList getHandlerList() {
return handlers;
}

@NotNull
@Override
public HandlerList getHandlers() {
return handlers;
}

public AccountHolder getHolder() {
return holder;
}

@Override
public String toString() {
return this.getClass().getSimpleName() + "{holder=" + this.holder.getId() + ",value=" + this.balance + "}";
}

}
111 changes: 84 additions & 27 deletions src/main/java/org/gestern/gringotts/event/AccountListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,24 @@

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Tag;
import org.bukkit.block.Block;
import org.bukkit.block.Sign;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockDispenseEvent;
import org.bukkit.event.block.SignChangeEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.InventoryMoveItemEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.scheduler.BukkitRunnable;
import org.gestern.gringotts.AccountChest;
import org.gestern.gringotts.Configuration;
import org.gestern.gringotts.Gringotts;
import org.gestern.gringotts.GringottsAccount;
import org.gestern.gringotts.Util;

import com.destroystokyo.paper.event.block.BlockDestroyEvent;
Expand Down Expand Up @@ -77,54 +80,88 @@ public void onSignChange(SignChangeEvent event) {
final VaultCreationEvent creation = new PlayerVaultCreationEvent(type, event);

Bukkit.getServer().getPluginManager().callEvent(creation);

if (creation.isValid()) {
new BukkitRunnable() {
@Override
public void run() {
AccountChest chest = getAccountChestFromSign(event.getBlock().getLocation());
if (chest.balance(true) > 0) {
Bukkit.getPluginManager().callEvent(new AccountBalanceChangeEvent(chest.account.owner, chest.account.getBalance()));
}
}
}.runTask(Gringotts.instance);
}
}
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onSignBreak(BlockDestroyEvent event) {
if (Tag.SIGNS.isTagged(event.getBlock().getType())) {
Gringotts.instance.getDao().deleteAccountChest(
event.getBlock().getWorld().getName(),
event.getBlock().getX(),
event.getBlock().getY(),
event.getBlock().getZ()
);
public void onBlockDestroy(BlockDestroyEvent event) {
onBlockBreak(event.getBlock());
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBlockBreak(BlockBreakEvent event) {
onBlockBreak(event.getBlock());
}

private void onBlockBreak(Block block) {
if (Tag.SIGNS.isTagged(block.getType())) {
AccountChest chest = getAccountChestFromSign(block.getLocation());
if (chest == null) return;

if (Gringotts.instance.getDao().deleteAccountChest(chest)) {
GringottsAccount account = chest.getAccount();
Bukkit.getPluginManager().callEvent(new AccountBalanceChangeEvent(account.owner, account.getBalance()));
}
} else if (Util.isValidContainer(block.getType())) {
AccountChest chest = getAccountChestFromContainer(block.getLocation(), true);
if (chest == null) return;
GringottsAccount account = chest.getAccount();
Bukkit.getPluginManager().callEvent(new AccountBalanceChangeEvent(account.owner, account.getBalance()));
}
}


@EventHandler(priority = EventPriority.MONITOR)
public void onInventoryClose(InventoryCloseEvent event) {
if (!Util.isValidInventory(event.getInventory().getType())) return;

AccountChest chest = getAccountChestFromHolder(event.getInventory());
AccountChest chest = getAccountChestFromContainer(event.getInventory().getLocation());
if (chest == null) return;

chest.setCachedBalance(chest.balance(true));
Gringotts.instance.getDao().updateChestBalance(chest, chest.getCachedBalance());
if (Gringotts.instance.getDao().updateChestBalance(chest, chest.getCachedBalance())) {
Bukkit.getPluginManager().callEvent(new AccountBalanceChangeEvent(chest.account.owner, chest.account.getBalance()));
}
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onInventoryMoveItem(InventoryMoveItemEvent event) {
if (Util.isValidInventory(event.getSource().getType())) {
AccountChest chest = getAccountChestFromHolder(event.getSource());
AccountChest chest = getAccountChestFromContainer(event.getSource().getLocation());
if (chest != null) {
new BukkitRunnable() {
@Override
public void run() {
chest.setCachedBalance(chest.balance(true));
Gringotts.instance.getDao().updateChestBalance(chest, chest.getCachedBalance());
if (Gringotts.instance.getDao().updateChestBalance(chest, chest.getCachedBalance())) {
Bukkit.getPluginManager().callEvent(new AccountBalanceChangeEvent(chest.account.owner, chest.account.getBalance()));
}
}
}.runTask(Gringotts.instance);
}
}
if (event.getDestination() != null && Util.isValidInventory(event.getDestination().getType())) {
AccountChest chest = getAccountChestFromHolder(event.getDestination());
AccountChest chest = getAccountChestFromContainer(event.getDestination().getLocation());
if (chest != null) {
new BukkitRunnable() {
@Override
public void run() {
chest.setCachedBalance(chest.balance(true));
Gringotts.instance.getDao().updateChestBalance(chest, chest.getCachedBalance());
if (Gringotts.instance.getDao().updateChestBalance(chest, chest.getCachedBalance())) {
Bukkit.getPluginManager().callEvent(new AccountBalanceChangeEvent(chest.account.owner, chest.account.getBalance()));
}
}
}.runTask(Gringotts.instance);
}
Expand All @@ -133,17 +170,17 @@ public void run() {

@EventHandler
public void onDispenseEvent(BlockDispenseEvent event) {
if (event.getBlock().getState() instanceof InventoryHolder) {
AccountChest chest = getAccountChestFromHolder(((InventoryHolder) event.getBlock().getState()).getInventory());
if (chest != null) {
new BukkitRunnable() {
@Override
public void run() {
chest.setCachedBalance(chest.balance(true));
Gringotts.instance.getDao().updateChestBalance(chest, chest.getCachedBalance());
AccountChest chest = getAccountChestFromContainer(event.getBlock().getLocation());
if (chest != null) {
new BukkitRunnable() {
@Override
public void run() {
chest.setCachedBalance(chest.balance(true));
if (Gringotts.instance.getDao().updateChestBalance(chest, chest.getCachedBalance())) {
Bukkit.getPluginManager().callEvent(new AccountBalanceChangeEvent(chest.account.owner, chest.account.getBalance()));
}
}.runTask(Gringotts.instance);
}
}
}.runTask(Gringotts.instance);
}
}

Expand All @@ -163,14 +200,34 @@ public void onSignEdit(PlayerOpenSignEvent event) {
* @param holder
* @return the {@link AccountChest} or null if none was found
*/
private AccountChest getAccountChestFromHolder(Inventory holder) {
private AccountChest getAccountChestFromContainer(Location location) {
return getAccountChestFromContainer(location, false);
}

private AccountChest getAccountChestFromContainer(Location location, boolean updateContainerLocations) {
for (AccountChest chest : Gringotts.instance.getDao().retrieveChests()) {
if (!chest.isChestLoaded()) continue; // For a chest to be open or interacted with, it needs to be loaded

if (chest.matchesLocation(holder.getLocation())) {
if (chest.matchesLocation(location, updateContainerLocations)) {
return chest;
}
}
return null;
}

/**
* Get the AccountChest from its sign's location
* @param location
* @return the {@link AccountChest} or null if none was found
*/
private AccountChest getAccountChestFromSign(Location location) {
for (AccountChest chest : Gringotts.instance.getDao().retrieveChests()) {
if (!chest.isChestLoaded()) continue; // For a chest to be open or interacted with, it needs to be loaded

if (chest.sign.getLocation().equals(location)) {
return chest;
}
}
return null;
}
}