feat: 添加 vault 相关事件

This commit is contained in:
2024-11-05 13:27:13 +08:00
parent bcfb866da7
commit 840cc6d238
5 changed files with 164 additions and 11 deletions

View File

@@ -5,7 +5,7 @@ plugins {
} }
group 'cn.hamster3' group 'cn.hamster3'
version '2.2.5' version '2.2.6'
repositories { repositories {
maven { maven {

View File

@@ -0,0 +1,46 @@
package cn.hamster3.currency.event.vault;
import org.bukkit.Bukkit;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;
import java.util.UUID;
public class VaultEconomyGiveEvent extends Event {
private static final HandlerList handlerList = new HandlerList();
@NotNull
private final UUID uuid;
@NotNull
private final String currencyID;
private double amount;
public VaultEconomyGiveEvent(@NotNull UUID uuid, @NotNull String currencyID, double amount) {
super(!Bukkit.getServer().isPrimaryThread());
this.uuid = uuid;
this.currencyID = currencyID;
this.amount = amount;
}
public @NotNull UUID getUuid() {
return uuid;
}
public @NotNull String getCurrencyID() {
return currencyID;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
@Override
public HandlerList getHandlers() {
return handlerList;
}
}

View File

@@ -0,0 +1,46 @@
package cn.hamster3.currency.event.vault;
import org.bukkit.Bukkit;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;
import java.util.UUID;
public class VaultEconomySeeEvent extends Event {
private static final HandlerList handlerList = new HandlerList();
@NotNull
private final UUID uuid;
@NotNull
private final String currencyID;
private double result;
public VaultEconomySeeEvent(@NotNull UUID uuid, @NotNull String currencyID, double result) {
super(!Bukkit.getServer().isPrimaryThread());
this.uuid = uuid;
this.currencyID = currencyID;
this.result = result;
}
public @NotNull UUID getUuid() {
return uuid;
}
public @NotNull String getCurrencyID() {
return currencyID;
}
public double getResult() {
return result;
}
public void setResult(double result) {
this.result = result;
}
@Override
public HandlerList getHandlers() {
return handlerList;
}
}

View File

@@ -0,0 +1,46 @@
package cn.hamster3.currency.event.vault;
import org.bukkit.Bukkit;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;
import java.util.UUID;
public class VaultEconomyTakeEvent extends Event {
private static final HandlerList handlerList = new HandlerList();
@NotNull
private final UUID uuid;
@NotNull
private final String currencyID;
private double amount;
public VaultEconomyTakeEvent(@NotNull UUID uuid, @NotNull String currencyID, double amount) {
super(!Bukkit.getServer().isPrimaryThread());
this.uuid = uuid;
this.currencyID = currencyID;
this.amount = amount;
}
public @NotNull UUID getUuid() {
return uuid;
}
public @NotNull String getCurrencyID() {
return currencyID;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
@Override
public HandlerList getHandlers() {
return handlerList;
}
}

View File

@@ -5,20 +5,20 @@ import cn.hamster3.currency.core.IDataManager;
import cn.hamster3.currency.core.Message; import cn.hamster3.currency.core.Message;
import cn.hamster3.currency.data.CurrencyLog; import cn.hamster3.currency.data.CurrencyLog;
import cn.hamster3.currency.data.PlayerData; import cn.hamster3.currency.data.PlayerData;
import cn.hamster3.currency.event.vault.VaultEconomyGiveEvent;
import cn.hamster3.currency.event.vault.VaultEconomySeeEvent;
import cn.hamster3.currency.event.vault.VaultEconomyTakeEvent;
import net.milkbowl.vault.economy.AbstractEconomy; import net.milkbowl.vault.economy.AbstractEconomy;
import net.milkbowl.vault.economy.EconomyResponse; import net.milkbowl.vault.economy.EconomyResponse;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class VaultEconomyHook extends AbstractEconomy { public class VaultEconomyHook extends AbstractEconomy {
private static final EconomyResponse NOT_IMPLEMENTED_RESPONSE = private static final EconomyResponse NOT_IMPLEMENTED_RESPONSE = new EconomyResponse(
new EconomyResponse( 0, 0, EconomyResponse.ResponseType.NOT_IMPLEMENTED, "HamsterCurrency未实现该功能~"
0,
0,
EconomyResponse.ResponseType.NOT_IMPLEMENTED,
"HamsterCurrency未实现该功能~"
); );
private final IDataManager dataManager; private final IDataManager dataManager;
@@ -31,6 +31,12 @@ public class VaultEconomyHook extends AbstractEconomy {
return new EconomyResponse(amount, 0, EconomyResponse.ResponseType.FAILURE, "玩家账户不存在"); return new EconomyResponse(amount, 0, EconomyResponse.ResponseType.FAILURE, "玩家账户不存在");
} }
String type = FileManager.getVaultCurrencyType(); String type = FileManager.getVaultCurrencyType();
VaultEconomyGiveEvent event = new VaultEconomyGiveEvent(data.getUuid(), type, amount);
Bukkit.getPluginManager().callEvent(event);
amount = event.getAmount();
if (amount == 0) {
return new EconomyResponse(amount, data.getPlayerCurrency(type), EconomyResponse.ResponseType.SUCCESS, null);
}
double balance = data.getPlayerCurrency(type) + amount; double balance = data.getPlayerCurrency(type) + amount;
if (data.getPlayerCurrency(type) > 0 && balance < 0) { if (data.getPlayerCurrency(type) > 0 && balance < 0) {
return new EconomyResponse(amount, 0, EconomyResponse.ResponseType.FAILURE, "玩家金额超出上限"); return new EconomyResponse(amount, 0, EconomyResponse.ResponseType.FAILURE, "玩家金额超出上限");
@@ -38,7 +44,7 @@ public class VaultEconomyHook extends AbstractEconomy {
data.setPlayerCurrency(type, balance); data.setPlayerCurrency(type, balance);
dataManager.savePlayerData(data); dataManager.savePlayerData(data);
dataManager.insertLog(new CurrencyLog(data.getUuid(), data.getPlayerName(), type, "add", amount, balance)); dataManager.insertLog(new CurrencyLog(data.getUuid(), data.getPlayerName(), type, "add", amount, balance));
return new EconomyResponse(amount, data.getPlayerCurrency(type), EconomyResponse.ResponseType.SUCCESS, ""); return new EconomyResponse(amount, data.getPlayerCurrency(type), EconomyResponse.ResponseType.SUCCESS, null);
} }
protected EconomyResponse withdrawPlayer(PlayerData data, double amount) { protected EconomyResponse withdrawPlayer(PlayerData data, double amount) {
@@ -46,6 +52,12 @@ public class VaultEconomyHook extends AbstractEconomy {
return new EconomyResponse(amount, 0, EconomyResponse.ResponseType.FAILURE, "玩家账户不存在"); return new EconomyResponse(amount, 0, EconomyResponse.ResponseType.FAILURE, "玩家账户不存在");
} }
String type = FileManager.getVaultCurrencyType(); String type = FileManager.getVaultCurrencyType();
VaultEconomyTakeEvent event = new VaultEconomyTakeEvent(data.getUuid(), type, amount);
Bukkit.getPluginManager().callEvent(event);
amount = event.getAmount();
if (amount == 0) {
return new EconomyResponse(amount, data.getPlayerCurrency(type), EconomyResponse.ResponseType.SUCCESS, null);
}
if (data.getPlayerCurrency(type) < amount) { if (data.getPlayerCurrency(type) < amount) {
return new EconomyResponse(amount, data.getPlayerCurrency(type), EconomyResponse.ResponseType.FAILURE, "余额不足"); return new EconomyResponse(amount, data.getPlayerCurrency(type), EconomyResponse.ResponseType.FAILURE, "余额不足");
} }
@@ -53,7 +65,7 @@ public class VaultEconomyHook extends AbstractEconomy {
data.setPlayerCurrency(type, balance); data.setPlayerCurrency(type, balance);
dataManager.savePlayerData(data); dataManager.savePlayerData(data);
dataManager.insertLog(new CurrencyLog(data.getUuid(), data.getPlayerName(), type, "take", amount, balance)); dataManager.insertLog(new CurrencyLog(data.getUuid(), data.getPlayerName(), type, "take", amount, balance));
return new EconomyResponse(amount, data.getPlayerCurrency(type), EconomyResponse.ResponseType.SUCCESS, "扣款成功"); return new EconomyResponse(amount, data.getPlayerCurrency(type), EconomyResponse.ResponseType.SUCCESS, null);
} }
private boolean has(PlayerData data, double amount) { private boolean has(PlayerData data, double amount) {
@@ -67,7 +79,10 @@ public class VaultEconomyHook extends AbstractEconomy {
if (data == null) { if (data == null) {
return 0; return 0;
} }
return data.getPlayerCurrency(FileManager.getVaultCurrencyType()); double currency = data.getPlayerCurrency(FileManager.getVaultCurrencyType());
VaultEconomySeeEvent event = new VaultEconomySeeEvent(data.getUuid(), FileManager.getVaultCurrencyType(), currency);
Bukkit.getPluginManager().callEvent(event);
return event.getResult();
} }
@Override @Override