Compare commits
5 Commits
3426fa2be8
...
3.1.3-SNAP
Author | SHA1 | Date | |
---|---|---|---|
fc07d5e03a | |||
268eaf5e7a | |||
4228930ba9 | |||
d5f3a24eae | |||
15cd8cc692 |
29
.gitea/workflows/main.yaml
Normal file
29
.gitea/workflows/main.yaml
Normal file
@@ -0,0 +1,29 @@
|
||||
name: Publish Project
|
||||
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- "*"
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
- name: Setup Java
|
||||
uses: actions/setup-java@v4
|
||||
with:
|
||||
java-version: 21
|
||||
distribution: temurin
|
||||
cache: gradle
|
||||
cache-dependency-path: gradle/wrapper/gradle-wrapper.properties
|
||||
- name: Build Project
|
||||
env:
|
||||
ORG_GRADLE_PROJECT_MAVEN_AIRGAME_USERNAME: ${{ secrets.MAVEN_AIRGAME_USERNAME }}
|
||||
ORG_GRADLE_PROJECT_MAVEN_AIRGAME_PASSWORD: ${{ secrets.MAVEN_AIRGAME_PASSWORD }}
|
||||
run: chmod +x gradlew && ./gradlew build publish --no-daemon
|
||||
- name: Publish to Release
|
||||
uses: softprops/action-gh-release@v2
|
||||
with:
|
||||
files: build/*.jar
|
@@ -6,7 +6,7 @@ plugins {
|
||||
}
|
||||
|
||||
group = "cn.hamster3.mc.plugin"
|
||||
version = "3.1.2"
|
||||
version = "3.1.3-SNAPSHOT"
|
||||
|
||||
repositories {
|
||||
maven("https://maven.airgame.net/maven-public/")
|
||||
@@ -56,11 +56,10 @@ publishing {
|
||||
}
|
||||
repositories {
|
||||
maven {
|
||||
url = uri("https://maven.airgame.net/public/")
|
||||
|
||||
url = uri("https://maven.airgame.net/public")
|
||||
credentials {
|
||||
username = rootProject.properties.getOrDefault("maven_username", "").toString()
|
||||
password = rootProject.properties.getOrDefault("maven_password", "").toString()
|
||||
username = findProperty("MAVEN_AIRGAME_USERNAME")?.toString() ?: ""
|
||||
password = findProperty("MAVEN_AIRGAME_PASSWORD")?.toString() ?: ""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package cn.hamster3.mc.plugin.currency;
|
||||
|
||||
import cn.hamster3.mc.plugin.ball.common.api.BallAPI;
|
||||
import cn.hamster3.mc.plugin.currency.api.CurrencyAPI;
|
||||
import cn.hamster3.mc.plugin.currency.command.ParentCurrencyCommand;
|
||||
import cn.hamster3.mc.plugin.currency.core.CurrencyConfigManager;
|
||||
import cn.hamster3.mc.plugin.currency.core.CurrencyDataManager;
|
||||
@@ -9,25 +10,22 @@ import cn.hamster3.mc.plugin.currency.data.CurrencyType;
|
||||
import cn.hamster3.mc.plugin.currency.hook.PlaceholderHook;
|
||||
import cn.hamster3.mc.plugin.currency.hook.VaultEconomyHook;
|
||||
import cn.hamster3.mc.plugin.currency.listener.BallListener;
|
||||
import cn.hamster3.mc.plugin.currency.listener.CurrencyListener;
|
||||
import lombok.Getter;
|
||||
import net.milkbowl.vault.economy.Economy;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.ServicePriority;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@SuppressWarnings("CallToPrintStackTrace")
|
||||
public class CurrencyPlugin extends JavaPlugin {
|
||||
public static final String BALL_CHANNEL = "HamsterCurrency";
|
||||
@Getter
|
||||
private static CurrencyPlugin instance;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public static void sync(@NotNull Runnable runnable) {
|
||||
Bukkit.getScheduler().runTask(instance, runnable);
|
||||
}
|
||||
@@ -36,6 +34,10 @@ public class CurrencyPlugin extends JavaPlugin {
|
||||
Bukkit.getScheduler().runTaskAsynchronously(instance, runnable);
|
||||
}
|
||||
|
||||
public static void error(@NotNull Throwable e) {
|
||||
instance.getLogger().log(Level.SEVERE, "", e);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoad() {
|
||||
instance = this;
|
||||
@@ -50,7 +52,7 @@ public class CurrencyPlugin extends JavaPlugin {
|
||||
CurrencyDataManager.init();
|
||||
logger.info("已初始化 CurrencyDataManager");
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
error(e);
|
||||
}
|
||||
if (Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) {
|
||||
logger.info("检测到 PlaceholderAPI 已安装");
|
||||
@@ -71,37 +73,22 @@ public class CurrencyPlugin extends JavaPlugin {
|
||||
logger.info("仓鼠经济正在启动");
|
||||
BallAPI.getInstance().getEventBus().register(BallListener.INSTANCE);
|
||||
logger.info("已注册 BallListener");
|
||||
Bukkit.getPluginManager().registerEvents(CurrencyListener.INSTANCE, this);
|
||||
logger.info("已注册 CurrencyListener");
|
||||
ParentCurrencyCommand.INSTANCE.register();
|
||||
if (CurrencyConfigManager.isEnableVault()) {
|
||||
if (Bukkit.getPluginManager().isPluginEnabled("Vault")) {
|
||||
if (CurrencyConfigManager.isEnableVault() && Bukkit.getPluginManager().isPluginEnabled("Vault")) {
|
||||
logger.info("检测到 Vault 已安装");
|
||||
Bukkit.getServicesManager().register(Economy.class, VaultEconomyHook.INSTANCE, this, ServicePriority.Normal);
|
||||
CurrencyType type = CurrencyDataManager.getVaultCurrencyType();
|
||||
CurrencyType type = CurrencyAPI.getVaultCurrencyType();
|
||||
if (type != null) {
|
||||
VaultEconomyHook economyHook = new VaultEconomyHook(type);
|
||||
Bukkit.getServicesManager().register(Economy.class, economyHook, this, ServicePriority.Normal);
|
||||
logger.info(String.format("已挂载货币类型 %s(%s) 至 Vault 系统", type.getId(), type.getName()));
|
||||
} else {
|
||||
logger.warning("已挂接 Vault 系统,但未找到 Vault 货币类型,请检查配置文件或创建货币类型: " + CurrencyConfigManager.getVaultCurrencyID());
|
||||
}
|
||||
} else {
|
||||
logger.info("未找到 Vault 插件! 取消注册 Vault 经济系统");
|
||||
}
|
||||
} else {
|
||||
logger.info("不进行 Vault 经济系统注册");
|
||||
}
|
||||
long time = System.currentTimeMillis() - start;
|
||||
logger.info("仓鼠经济启动完成,总计耗时 " + time + " ms");
|
||||
|
||||
async(() -> {
|
||||
for (Player player : Bukkit.getOnlinePlayers()) {
|
||||
try {
|
||||
CurrencyDataManager.loadPlayerData(player.getUniqueId());
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -114,5 +101,4 @@ public class CurrencyPlugin extends JavaPlugin {
|
||||
long time = System.currentTimeMillis() - start;
|
||||
logger.info("仓鼠经济已关闭,总计耗时 " + time + " ms");
|
||||
}
|
||||
|
||||
}
|
@@ -1,6 +1,7 @@
|
||||
package cn.hamster3.mc.plugin.currency.api;
|
||||
|
||||
import cn.hamster3.mc.plugin.currency.async.CompletableTask;
|
||||
import cn.hamster3.mc.plugin.currency.core.CurrencyConfigManager;
|
||||
import cn.hamster3.mc.plugin.currency.core.CurrencyDataManager;
|
||||
import cn.hamster3.mc.plugin.currency.data.CurrencyType;
|
||||
import cn.hamster3.mc.plugin.currency.event.CurrencyGiveEvent;
|
||||
@@ -31,7 +32,7 @@ public final class CurrencyAPI {
|
||||
}
|
||||
CurrencyGiveEvent event = new CurrencyGiveEvent(uuid, type, amount);
|
||||
Bukkit.getPluginManager().callEvent(event);
|
||||
CurrencyDataManager.addPlayerCurrency(event.getUuid(), event.getType().getId(), event.getAmount(), true);
|
||||
CurrencyDataManager.givePlayerCurrency(event.getUuid(), event.getType(), event.getAmount());
|
||||
});
|
||||
}
|
||||
|
||||
@@ -47,7 +48,7 @@ public final class CurrencyAPI {
|
||||
}
|
||||
CurrencyTakeEvent event = new CurrencyTakeEvent(uuid, type, amount);
|
||||
Bukkit.getPluginManager().callEvent(event);
|
||||
CurrencyDataManager.takePlayerCurrency(event.getUuid(), event.getType().getId(), event.getAmount(), true);
|
||||
CurrencyDataManager.takePlayerCurrency(event.getUuid(), event.getType(), event.getAmount());
|
||||
});
|
||||
}
|
||||
|
||||
@@ -60,7 +61,7 @@ public final class CurrencyAPI {
|
||||
}
|
||||
CurrencySetEvent event = new CurrencySetEvent(uuid, type, amount);
|
||||
Bukkit.getPluginManager().callEvent(event);
|
||||
CurrencyDataManager.setPlayerCurrency(event.getUuid(), event.getType().getId(), event.getAmount(), true);
|
||||
CurrencyDataManager.setPlayerCurrency(event.getUuid(), event.getType(), event.getAmount());
|
||||
});
|
||||
}
|
||||
|
||||
@@ -70,8 +71,8 @@ public final class CurrencyAPI {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static CompletableTask<Double> getPlayerCurrency(@NotNull UUID uuid, @NotNull String currencyID) {
|
||||
return CompletableTask.supplyAsync(() -> CurrencyDataManager.getPlayerCurrency(uuid, currencyID));
|
||||
public static CompletableTask<Double> getPlayerCurrency(@NotNull UUID uuid, @NotNull CurrencyType type) {
|
||||
return CompletableTask.supplyAsync(() -> CurrencyDataManager.getPlayerCurrency(uuid, type));
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@@ -81,7 +82,7 @@ public final class CurrencyAPI {
|
||||
|
||||
@Nullable
|
||||
public static CurrencyType getVaultCurrencyType() {
|
||||
return CurrencyDataManager.getVaultCurrencyType();
|
||||
return getCurrencyType(CurrencyConfigManager.getVaultCurrencyID());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package cn.hamster3.mc.plugin.currency.async;
|
||||
|
||||
import cn.hamster3.mc.plugin.core.common.api.CoreAPI;
|
||||
import cn.hamster3.mc.plugin.currency.CurrencyPlugin;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -8,7 +9,6 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
@SuppressWarnings("CallToPrintStackTrace")
|
||||
public class CompletableTask<T> {
|
||||
@NotNull
|
||||
private final List<Consumer<T>> onSuccess;
|
||||
@@ -16,11 +16,13 @@ public class CompletableTask<T> {
|
||||
private final List<Consumer<Throwable>> onFailed;
|
||||
@NotNull
|
||||
private volatile State state;
|
||||
@Nullable
|
||||
private T value;
|
||||
@Nullable
|
||||
private Throwable throwable;
|
||||
|
||||
public CompletableTask() {
|
||||
state = State.WAITING;
|
||||
state = State.RUNNING;
|
||||
onSuccess = new ArrayList<>();
|
||||
onFailed = new ArrayList<>();
|
||||
}
|
||||
@@ -31,7 +33,7 @@ public class CompletableTask<T> {
|
||||
CoreAPI.getInstance().getExecutorService().submit(() -> {
|
||||
try {
|
||||
runTask.run();
|
||||
task.success(null);
|
||||
task.completed(null);
|
||||
} catch (Exception e) {
|
||||
task.failed(e);
|
||||
}
|
||||
@@ -45,7 +47,7 @@ public class CompletableTask<T> {
|
||||
CoreAPI.getInstance().getExecutorService().submit(() -> {
|
||||
try {
|
||||
T call = supplyTask.call();
|
||||
task.success(call);
|
||||
task.completed(call);
|
||||
} catch (Exception e) {
|
||||
task.failed(e);
|
||||
}
|
||||
@@ -53,12 +55,12 @@ public class CompletableTask<T> {
|
||||
return task;
|
||||
}
|
||||
|
||||
public void success(@Nullable T value) {
|
||||
if (state != State.WAITING) {
|
||||
public void completed(@Nullable T value) {
|
||||
if (state != State.RUNNING) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
this.value = value;
|
||||
state = State.SUCCESS;
|
||||
state = State.COMPLETED;
|
||||
synchronized (this) {
|
||||
notifyAll();
|
||||
}
|
||||
@@ -66,13 +68,13 @@ public class CompletableTask<T> {
|
||||
try {
|
||||
success.accept(value);
|
||||
} catch (Throwable e) {
|
||||
e.printStackTrace();
|
||||
CurrencyPlugin.error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void failed(@Nullable Throwable throwable) {
|
||||
if (state != State.WAITING) {
|
||||
if (state != State.RUNNING) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
this.value = null;
|
||||
@@ -85,18 +87,18 @@ public class CompletableTask<T> {
|
||||
try {
|
||||
consumer.accept(throwable);
|
||||
} catch (Throwable e) {
|
||||
e.printStackTrace();
|
||||
CurrencyPlugin.error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public CompletableTask<T> onSuccess(@NotNull Consumer<T> consumer) {
|
||||
if (state == State.SUCCESS) {
|
||||
if (state == State.COMPLETED) {
|
||||
try {
|
||||
consumer.accept(value);
|
||||
} catch (Throwable e) {
|
||||
e.printStackTrace();
|
||||
CurrencyPlugin.error(e);
|
||||
}
|
||||
} else {
|
||||
onSuccess.add(consumer);
|
||||
@@ -110,7 +112,7 @@ public class CompletableTask<T> {
|
||||
try {
|
||||
consumer.accept(throwable);
|
||||
} catch (Throwable e) {
|
||||
e.printStackTrace();
|
||||
CurrencyPlugin.error(e);
|
||||
}
|
||||
} else {
|
||||
onFailed.add(consumer);
|
||||
@@ -119,7 +121,7 @@ public class CompletableTask<T> {
|
||||
}
|
||||
|
||||
public void join() throws InterruptedException {
|
||||
if (state == State.WAITING) {
|
||||
if (state == State.RUNNING) {
|
||||
synchronized (this) {
|
||||
wait();
|
||||
}
|
||||
@@ -140,11 +142,13 @@ public class CompletableTask<T> {
|
||||
public T get() throws Throwable {
|
||||
join();
|
||||
switch (state) {
|
||||
case SUCCESS:
|
||||
case COMPLETED:
|
||||
return value;
|
||||
case FAILED:
|
||||
if (throwable != null) {
|
||||
throw throwable;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -156,7 +160,7 @@ public class CompletableTask<T> {
|
||||
}
|
||||
}
|
||||
|
||||
public T getOrDefault(T defaultValue) {
|
||||
public T orElse(T defaultValue) {
|
||||
T value = getSilent();
|
||||
if (value == null) {
|
||||
return defaultValue;
|
||||
@@ -165,8 +169,8 @@ public class CompletableTask<T> {
|
||||
}
|
||||
|
||||
public enum State {
|
||||
WAITING,
|
||||
SUCCESS,
|
||||
RUNNING,
|
||||
COMPLETED,
|
||||
FAILED
|
||||
}
|
||||
}
|
||||
|
@@ -15,7 +15,6 @@ import java.sql.SQLException;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@SuppressWarnings("CallToPrintStackTrace")
|
||||
public class CurrencyCreateCommand extends ChildCommand {
|
||||
public static final CurrencyCreateCommand INSTANCE = new CurrencyCreateCommand();
|
||||
|
||||
@@ -29,7 +28,7 @@ public class CurrencyCreateCommand extends ChildCommand {
|
||||
|
||||
@Override
|
||||
public @NotNull String getUsage() {
|
||||
return "create <货币ID> <货币名称> <支持转账(t|f)>";
|
||||
return "create <货币ID>";
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -44,8 +43,8 @@ public class CurrencyCreateCommand extends ChildCommand {
|
||||
|
||||
@Override
|
||||
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
|
||||
if (args.length < 3) {
|
||||
ParentCurrencyCommand.INSTANCE.sendHelp(sender);
|
||||
if (args.length < 1) {
|
||||
sender.sendMessage(ParentCurrencyCommand.INSTANCE.getUsage() + " " + getUsage());
|
||||
return true;
|
||||
}
|
||||
if (CurrencyAPI.getCurrencyType(args[0]) != null) {
|
||||
@@ -53,40 +52,18 @@ public class CurrencyCreateCommand extends ChildCommand {
|
||||
return true;
|
||||
}
|
||||
String id = args[0];
|
||||
if (!id.matches("[a-zA-Z0-9-_]{0,36}")) {
|
||||
if (!id.matches("[a-zA-Z0-9-_]{1,36}")) {
|
||||
CurrencyMessage.CURRENCY_TYPE_ID_ERROR.show(sender);
|
||||
return true;
|
||||
}
|
||||
String name = args[1];
|
||||
if (name.length() > 36) {
|
||||
CurrencyMessage.CURRENCY_TYPE_NAME_TOO_LONG.show(sender);
|
||||
return true;
|
||||
}
|
||||
boolean transferable;
|
||||
switch (args[2]) {
|
||||
case "t":
|
||||
case "true": {
|
||||
transferable = true;
|
||||
break;
|
||||
}
|
||||
case "f":
|
||||
case "false": {
|
||||
transferable = false;
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
CurrencyMessage.CURRENCY_TYPE_TRANSFERABLE_INPUT_ERROR.show(sender);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
CurrencyPlugin.async(() -> {
|
||||
try {
|
||||
CurrencyType type = new CurrencyType(id, name, transferable);
|
||||
CurrencyType type = new CurrencyType(id);
|
||||
CurrencyDataManager.updateCurrencyType(type, true);
|
||||
CurrencyMessage.CURRENCY_CREATE_SUCCESS.show(sender);
|
||||
} catch (SQLException e) {
|
||||
CurrencyMessage.CURRENCY_CREATE_ERROR.show(sender);
|
||||
e.printStackTrace();
|
||||
CurrencyPlugin.error(e);
|
||||
}
|
||||
});
|
||||
return true;
|
||||
|
@@ -15,7 +15,6 @@ import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@SuppressWarnings("CallToPrintStackTrace")
|
||||
public class CurrencyDeleteCommand extends ChildCommand {
|
||||
public static final CurrencyDeleteCommand INSTANCE = new CurrencyDeleteCommand();
|
||||
|
||||
@@ -45,7 +44,7 @@ public class CurrencyDeleteCommand extends ChildCommand {
|
||||
@Override
|
||||
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
|
||||
if (args.length < 1) {
|
||||
ParentCurrencyCommand.INSTANCE.sendHelp(sender);
|
||||
sender.sendMessage(ParentCurrencyCommand.INSTANCE.getUsage() + " " + getUsage());
|
||||
return true;
|
||||
}
|
||||
CurrencyType type = CurrencyDataManager.getCurrencyType(args[0]);
|
||||
@@ -59,7 +58,7 @@ public class CurrencyDeleteCommand extends ChildCommand {
|
||||
CurrencyMessage.CURRENCY_DELETE_SUCCESS.show(sender);
|
||||
} catch (SQLException e) {
|
||||
CurrencyMessage.CURRENCY_DELETE_ERROR.show(sender);
|
||||
e.printStackTrace();
|
||||
CurrencyPlugin.error(e);
|
||||
}
|
||||
});
|
||||
return true;
|
||||
|
@@ -45,7 +45,7 @@ public class CurrencyGiveCommand extends ChildCommand {
|
||||
@Override
|
||||
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
|
||||
if (args.length < 3) {
|
||||
ParentCurrencyCommand.INSTANCE.sendHelp(sender);
|
||||
sender.sendMessage(ParentCurrencyCommand.INSTANCE.getUsage() + " " + getUsage());
|
||||
return true;
|
||||
}
|
||||
BallPlayerInfo playerInfo = BallAPI.getInstance().getPlayerInfo(args[0]);
|
||||
@@ -60,8 +60,8 @@ public class CurrencyGiveCommand extends ChildCommand {
|
||||
}
|
||||
double amount = Double.parseDouble(args[2]);
|
||||
CurrencyAPI.givePlayerCurrency(playerInfo.getUuid(), type.getId(), amount)
|
||||
.onFailed(throwable -> CurrencyMessage.ERROR_EXCEPTION_ADMIN.show(sender))
|
||||
.onSuccess(unused -> CurrencyMessage.CURRENCY_GIVE_SUCCESS.show(sender));
|
||||
.onSuccess(unused -> CurrencyMessage.CURRENCY_GIVE_SUCCESS.show(sender))
|
||||
.onFailed(throwable -> CurrencyMessage.ERROR_EXCEPTION_ADMIN.show(sender));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@@ -1,139 +0,0 @@
|
||||
package cn.hamster3.mc.plugin.currency.command;
|
||||
|
||||
import cn.hamster3.mc.plugin.ball.common.api.BallAPI;
|
||||
import cn.hamster3.mc.plugin.ball.common.entity.BallPlayerInfo;
|
||||
import cn.hamster3.mc.plugin.core.bukkit.command.ChildCommand;
|
||||
import cn.hamster3.mc.plugin.core.bukkit.constant.CoreMessage;
|
||||
import cn.hamster3.mc.plugin.core.common.data.DisplayMessage;
|
||||
import cn.hamster3.mc.plugin.currency.api.CurrencyAPI;
|
||||
import cn.hamster3.mc.plugin.currency.async.CompletableTask;
|
||||
import cn.hamster3.mc.plugin.currency.core.CurrencyDataManager;
|
||||
import cn.hamster3.mc.plugin.currency.core.CurrencyMessage;
|
||||
import cn.hamster3.mc.plugin.currency.data.CurrencyType;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@SuppressWarnings("CallToPrintStackTrace")
|
||||
public class CurrencyPayCommand extends ChildCommand {
|
||||
public static final CurrencyPayCommand INSTANCE = new CurrencyPayCommand();
|
||||
|
||||
private CurrencyPayCommand() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull String getName() {
|
||||
return "pay";
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull String getUsage() {
|
||||
return "pay <玩家> <货币ID> <数额>";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(@NotNull CommandSender sender) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull String getDescription() {
|
||||
return "向其他玩家转账";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
|
||||
if (!(sender instanceof Player)) {
|
||||
CoreMessage.COMMAND_MUST_USED_BY_PLAYER.show(sender);
|
||||
return true;
|
||||
}
|
||||
Player player = (Player) sender;
|
||||
if (args.length < 3) {
|
||||
ParentCurrencyCommand.INSTANCE.sendHelp(sender);
|
||||
return true;
|
||||
}
|
||||
BallPlayerInfo targetInfo = BallAPI.getInstance().getPlayerInfo(args[0]);
|
||||
if (targetInfo == null) {
|
||||
CurrencyMessage.PLAYER_NOT_FOUND.show(sender);
|
||||
return true;
|
||||
}
|
||||
CurrencyType type = CurrencyDataManager.getCurrencyType(args[1]);
|
||||
if (type == null) {
|
||||
CurrencyMessage.CURRENCY_TYPE_NOT_EXIST.show(sender);
|
||||
return true;
|
||||
}
|
||||
double payAmount;
|
||||
try {
|
||||
payAmount = Double.parseDouble(args[2]);
|
||||
} catch (NumberFormatException e) {
|
||||
CurrencyMessage.CURRENCY_AMOUNT_NUMBER_INPUT_ERROR.show(sender);
|
||||
return true;
|
||||
}
|
||||
if (payAmount <= 0) {
|
||||
CurrencyMessage.CURRENCY_AMOUNT_NUMBER_INPUT_ERROR.show(sender);
|
||||
return true;
|
||||
}
|
||||
if (!type.isTransferable()) {
|
||||
CurrencyMessage.PAY_CURRENCY_NOT_TRANSFERABLE.show(sender);
|
||||
return true;
|
||||
}
|
||||
CompletableTask.supplyAsync(() -> {
|
||||
double hasAmount = CurrencyAPI.getPlayerCurrency(player.getUniqueId(), type.getId()).getOrDefault(0D);
|
||||
if (hasAmount < payAmount) {
|
||||
CurrencyMessage.PAT_BALANCE_NOT_ENOUGH.show(sender,
|
||||
"%has%", String.format("%.2f", hasAmount),
|
||||
"%need%", String.format("%.2f", payAmount)
|
||||
);
|
||||
return false;
|
||||
}
|
||||
CurrencyDataManager.takePlayerCurrency(player.getUniqueId(), type.getId(), payAmount, true);
|
||||
CurrencyDataManager.addPlayerCurrency(targetInfo.getUuid(), type.getId(), payAmount, true);
|
||||
return true;
|
||||
}).onSuccess(success -> {
|
||||
if (!success) {
|
||||
return;
|
||||
}
|
||||
CurrencyMessage.PAY_SUCCESS.show(sender,
|
||||
"%type_id%", type.getId(),
|
||||
"%type_name%", type.getName(),
|
||||
"%from_player_name%", player.getName(),
|
||||
"%to_player_name%", targetInfo.getName(),
|
||||
"%amount%", String.valueOf(payAmount));
|
||||
DisplayMessage message = CurrencyMessage.PAY_RECEIVED.getMessage().copy()
|
||||
.replace("%type_id%", type.getId())
|
||||
.replace("%type_name%", type.getName())
|
||||
.replace("%from_player_name%", player.getName())
|
||||
.replace("%to_player_name%", targetInfo.getName())
|
||||
.replace("%amount%", String.valueOf(payAmount));
|
||||
BallAPI.getInstance().sendMessageToPlayer(targetInfo.getUuid(), message, true);
|
||||
}).onFailed(throwable -> {
|
||||
CurrencyMessage.ERROR_EXCEPTION.show(player);
|
||||
throwable.printStackTrace();
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
|
||||
switch (args.length) {
|
||||
case 1: {
|
||||
return null;
|
||||
}
|
||||
case 2: {
|
||||
return CurrencyDataManager.getCurrencyTypes().stream()
|
||||
.map(CurrencyType::getId)
|
||||
.filter(o -> o.startsWith(args[1]))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
default:
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,105 +0,0 @@
|
||||
package cn.hamster3.mc.plugin.currency.command;
|
||||
|
||||
import cn.hamster3.mc.plugin.core.bukkit.command.ChildCommand;
|
||||
import cn.hamster3.mc.plugin.currency.CurrencyPlugin;
|
||||
import cn.hamster3.mc.plugin.currency.core.CurrencyDataManager;
|
||||
import cn.hamster3.mc.plugin.currency.core.CurrencyMessage;
|
||||
import cn.hamster3.mc.plugin.currency.data.CurrencyType;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@SuppressWarnings("CallToPrintStackTrace")
|
||||
public class CurrencyResetCommand extends ChildCommand {
|
||||
public static final CurrencyResetCommand INSTANCE = new CurrencyResetCommand();
|
||||
|
||||
private CurrencyResetCommand() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull String getName() {
|
||||
return "reset";
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull String getUsage() {
|
||||
return "reset <货币ID> <货币名称> <支持转账(t|f)>";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(@NotNull CommandSender sender) {
|
||||
return sender.hasPermission("hamster.currency.admin");
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull String getDescription() {
|
||||
return "修改货币类型";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
|
||||
if (args.length < 4) {
|
||||
ParentCurrencyCommand.INSTANCE.sendHelp(sender);
|
||||
return true;
|
||||
}
|
||||
if (CurrencyDataManager.getCurrencyType(args[0]) == null) {
|
||||
CurrencyMessage.CURRENCY_TYPE_NOT_EXIST.show(sender);
|
||||
return true;
|
||||
}
|
||||
String id = args[0];
|
||||
if (!id.matches("[a-zA-Z0-9-_]{0,36}")) {
|
||||
CurrencyMessage.CURRENCY_TYPE_ID_ERROR.show(sender);
|
||||
return true;
|
||||
}
|
||||
String name = args[1];
|
||||
if (name.length() > 36) {
|
||||
CurrencyMessage.CURRENCY_TYPE_NAME_TOO_LONG.show(sender);
|
||||
return true;
|
||||
}
|
||||
boolean transferable;
|
||||
switch (args[2]) {
|
||||
case "t":
|
||||
case "true": {
|
||||
transferable = true;
|
||||
break;
|
||||
}
|
||||
case "f":
|
||||
case "false": {
|
||||
transferable = false;
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
CurrencyMessage.CURRENCY_TYPE_TRANSFERABLE_INPUT_ERROR.show(sender);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
CurrencyPlugin.async(() -> {
|
||||
try {
|
||||
CurrencyDataManager.updateCurrencyType(new CurrencyType(id, name, transferable), true);
|
||||
CurrencyMessage.CURRENCY_RESET_SUCCESS.show(sender);
|
||||
} catch (SQLException e) {
|
||||
CurrencyMessage.CURRENCY_RESET_ERROR.show(sender);
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
|
||||
if (args.length == 1) {
|
||||
return CurrencyDataManager.getCurrencyTypes().stream()
|
||||
.map(CurrencyType::getName)
|
||||
.filter(o -> o.startsWith(args[0]))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
@@ -3,6 +3,7 @@ package cn.hamster3.mc.plugin.currency.command;
|
||||
import cn.hamster3.mc.plugin.ball.common.api.BallAPI;
|
||||
import cn.hamster3.mc.plugin.ball.common.entity.BallPlayerInfo;
|
||||
import cn.hamster3.mc.plugin.core.bukkit.command.ChildCommand;
|
||||
import cn.hamster3.mc.plugin.currency.CurrencyPlugin;
|
||||
import cn.hamster3.mc.plugin.currency.api.CurrencyAPI;
|
||||
import cn.hamster3.mc.plugin.currency.core.CurrencyDataManager;
|
||||
import cn.hamster3.mc.plugin.currency.core.CurrencyMessage;
|
||||
@@ -17,7 +18,6 @@ import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@SuppressWarnings("CallToPrintStackTrace")
|
||||
public class CurrencySeeCommand extends ChildCommand {
|
||||
public static final CurrencySeeCommand INSTANCE = new CurrencySeeCommand();
|
||||
|
||||
@@ -47,7 +47,7 @@ public class CurrencySeeCommand extends ChildCommand {
|
||||
@Override
|
||||
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
|
||||
if (args.length < 1) {
|
||||
ParentCurrencyCommand.INSTANCE.sendHelp(sender);
|
||||
sender.sendMessage(ParentCurrencyCommand.INSTANCE.getUsage() + " " + getUsage());
|
||||
return true;
|
||||
}
|
||||
BallPlayerInfo playerInfo = BallAPI.getInstance().getPlayerInfo(args[0]);
|
||||
@@ -67,18 +67,19 @@ public class CurrencySeeCommand extends ChildCommand {
|
||||
types = CurrencyDataManager.getCurrencyTypes();
|
||||
}
|
||||
CurrencyAPI.getPlayerCurrency(playerInfo.getUuid())
|
||||
.onFailed(throwable -> {
|
||||
CurrencyMessage.ERROR_EXCEPTION_ADMIN.show(sender);
|
||||
CurrencyPlugin.error(throwable);
|
||||
})
|
||||
.onSuccess(map -> {
|
||||
for (CurrencyType type : types) {
|
||||
CurrencyMessage.CURRENCY_SEE_RESULT.show(sender,
|
||||
"%type_id%", type.getId(),
|
||||
"%type_name%", type.getName(),
|
||||
"%amount%", String.valueOf(map.getOrDefault(type.getId(), 0D)),
|
||||
"%amount%", String.valueOf(map.getOrDefault(type.getId(), type.getDefaultAmount())),
|
||||
"%player_name%", playerInfo.getName()
|
||||
);
|
||||
}
|
||||
}).onFailed(throwable -> {
|
||||
CurrencyMessage.ERROR_EXCEPTION_ADMIN.show(sender);
|
||||
throwable.printStackTrace();
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
@@ -45,7 +45,7 @@ public class CurrencySetCommand extends ChildCommand {
|
||||
@Override
|
||||
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
|
||||
if (args.length < 3) {
|
||||
ParentCurrencyCommand.INSTANCE.sendHelp(sender);
|
||||
sender.sendMessage(ParentCurrencyCommand.INSTANCE.getUsage() + " " + getUsage());
|
||||
return true;
|
||||
}
|
||||
BallPlayerInfo playerInfo = BallAPI.getInstance().getPlayerInfo(args[0]);
|
||||
@@ -60,8 +60,8 @@ public class CurrencySetCommand extends ChildCommand {
|
||||
}
|
||||
double amount = Double.parseDouble(args[2]);
|
||||
CurrencyAPI.setPlayerCurrency(playerInfo.getUuid(), type.getId(), amount)
|
||||
.onFailed(throwable -> CurrencyMessage.ERROR_EXCEPTION_ADMIN.show(sender))
|
||||
.onSuccess(unused -> CurrencyMessage.CURRENCY_SET_SUCCESS.show(sender));
|
||||
.onSuccess(unused -> CurrencyMessage.CURRENCY_SET_SUCCESS.show(sender))
|
||||
.onFailed(throwable -> CurrencyMessage.ERROR_EXCEPTION_ADMIN.show(sender));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@@ -45,7 +45,7 @@ public class CurrencyTakeCommand extends ChildCommand {
|
||||
@Override
|
||||
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
|
||||
if (args.length < 3) {
|
||||
ParentCurrencyCommand.INSTANCE.sendHelp(sender);
|
||||
sender.sendMessage(ParentCurrencyCommand.INSTANCE.getUsage() + " " + getUsage());
|
||||
return true;
|
||||
}
|
||||
BallPlayerInfo playerInfo = BallAPI.getInstance().getPlayerInfo(args[0]);
|
||||
@@ -60,8 +60,8 @@ public class CurrencyTakeCommand extends ChildCommand {
|
||||
}
|
||||
double amount = Double.parseDouble(args[2]);
|
||||
CurrencyAPI.takePlayerCurrency(playerInfo.getUuid(), type.getId(), amount)
|
||||
.onFailed(throwable -> CurrencyMessage.ERROR_EXCEPTION_ADMIN.show(sender))
|
||||
.onSuccess(unused -> CurrencyMessage.CURRENCY_TAKE_SUCCESS.show(sender));
|
||||
.onSuccess(unused -> CurrencyMessage.CURRENCY_TAKE_SUCCESS.show(sender))
|
||||
.onFailed(throwable -> CurrencyMessage.ERROR_EXCEPTION_ADMIN.show(sender));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@@ -11,13 +11,11 @@ public class ParentCurrencyCommand extends ParentCommand {
|
||||
|
||||
private ParentCurrencyCommand() {
|
||||
addChildCommand(CurrencyCreateCommand.INSTANCE);
|
||||
addChildCommand(CurrencyResetCommand.INSTANCE);
|
||||
addChildCommand(CurrencyDeleteCommand.INSTANCE);
|
||||
addChildCommand(CurrencyGiveCommand.INSTANCE);
|
||||
addChildCommand(CurrencyTakeCommand.INSTANCE);
|
||||
addChildCommand(CurrencySetCommand.INSTANCE);
|
||||
addChildCommand(CurrencySeeCommand.INSTANCE);
|
||||
addChildCommand(CurrencyPayCommand.INSTANCE);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -32,6 +30,6 @@ public class ParentCurrencyCommand extends ParentCommand {
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(@NotNull CommandSender sender) {
|
||||
return true;
|
||||
return sender.hasPermission("hamster.currency.admin");
|
||||
}
|
||||
}
|
||||
|
@@ -4,19 +4,18 @@ import cn.hamster3.mc.plugin.ball.common.api.BallAPI;
|
||||
import cn.hamster3.mc.plugin.core.common.api.CoreAPI;
|
||||
import cn.hamster3.mc.plugin.currency.CurrencyPlugin;
|
||||
import cn.hamster3.mc.plugin.currency.data.CurrencyType;
|
||||
import com.google.gson.JsonObject;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public final class CurrencyDataManager {
|
||||
private static final Map<String, CurrencyType> CURRENCY_TYPES = new ConcurrentHashMap<>();
|
||||
private static final Map<UUID, Map<String, Double>> PLAYER_DATA = new ConcurrentHashMap<>();
|
||||
|
||||
private CurrencyDataManager() {
|
||||
}
|
||||
@@ -25,23 +24,30 @@ public final class CurrencyDataManager {
|
||||
try (Connection connection = CoreAPI.getInstance().getConnection()) {
|
||||
try (Statement statement = connection.createStatement()) {
|
||||
statement.execute("CREATE TABLE IF NOT EXISTS hamster_currency_type(" +
|
||||
"`id` VARCHAR(36) PRIMARY KEY," +
|
||||
"`name` VARCHAR(36) NOT NULL," +
|
||||
"`transferable` BOOLEAN NOT NULL" +
|
||||
"id VARCHAR(36) PRIMARY KEY," +
|
||||
"data MEDIUMTEXT NOT NULL" +
|
||||
") CHARSET utf8mb4;");
|
||||
statement.execute("CREATE TABLE IF NOT EXISTS hamster_currency_data(" +
|
||||
"`uuid` CHAR(36) NOT NULL," +
|
||||
"`currency` VARCHAR(36) NOT NULL," +
|
||||
"`amount` DOUBLE NOT NULL," +
|
||||
"PRIMARY KEY `player_currency` (`uuid`, `currency`)" +
|
||||
"uuid CHAR(36) NOT NULL," +
|
||||
"currency VARCHAR(36) NOT NULL," +
|
||||
"amount DOUBLE NOT NULL," +
|
||||
"PRIMARY KEY `player_currency` (`uuid`, `currency`)," +
|
||||
"INDEX idx_currency(currency)" +
|
||||
") CHARSET utf8mb4;");
|
||||
statement.execute("CREATE TABLE IF NOT EXISTS hamster_currency_logs(" +
|
||||
"uuid CHAR(36) NOT NULL," +
|
||||
"currency VARCHAR(36) NOT NULL," +
|
||||
"action ENUM('MODIFY', 'SET')," +
|
||||
"amount DOUBLE NOT NULL," +
|
||||
"balance DOUBLE NOT NULL," +
|
||||
"time DATETIME NOT NULL," +
|
||||
"INDEX idx_uuid(uuid)," +
|
||||
"INDEX idx_uuid_currency(uuid, currency)" +
|
||||
") CHARSET utf8mb4;");
|
||||
try (ResultSet set = statement.executeQuery("SELECT * FROM hamster_currency_type;")) {
|
||||
while (set.next()) {
|
||||
CurrencyType type = new CurrencyType(
|
||||
set.getString("id"),
|
||||
set.getString("name"),
|
||||
set.getBoolean("transferable")
|
||||
);
|
||||
String dataString = set.getString("data");
|
||||
CurrencyType type = CoreAPI.getInstance().getGson().fromJson(dataString, CurrencyType.class);
|
||||
CURRENCY_TYPES.put(type.getId(), type);
|
||||
}
|
||||
}
|
||||
@@ -49,14 +55,14 @@ public final class CurrencyDataManager {
|
||||
}
|
||||
}
|
||||
|
||||
public static Map<String, Double> loadPlayerData(@NotNull UUID uuid) throws SQLException {
|
||||
public static Map<String, Double> getPlayerCurrency(@NotNull UUID uuid) throws SQLException {
|
||||
try (Connection connection = CoreAPI.getInstance().getConnection()) {
|
||||
try (PreparedStatement statement = connection.prepareStatement(
|
||||
"SELECT currency, amount FROM hamster_currency_data WHERE `uuid`=?;"
|
||||
)) {
|
||||
statement.setString(1, uuid.toString());
|
||||
try (ResultSet set = statement.executeQuery()) {
|
||||
Map<String, Double> playerData = new ConcurrentHashMap<>();
|
||||
Map<String, Double> playerData = new HashMap<>();
|
||||
while (set.next()) {
|
||||
playerData.put(
|
||||
set.getString("currency"),
|
||||
@@ -69,67 +75,82 @@ public final class CurrencyDataManager {
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Map<String, Double> getPlayerCurrency(@NotNull UUID uuid) throws SQLException {
|
||||
Map<String, Double> map = PLAYER_DATA.get(uuid);
|
||||
if (map == null) {
|
||||
map = loadPlayerData(uuid);
|
||||
PLAYER_DATA.put(uuid, map);
|
||||
public static double getPlayerCurrency(@NotNull UUID uuid, @NotNull CurrencyType type) throws SQLException {
|
||||
try (Connection connection = CoreAPI.getInstance().getConnection()) {
|
||||
return getPlayerCurrency(connection, uuid, type);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
public static double getPlayerCurrency(@NotNull UUID uuid, @NotNull String typeID) throws SQLException {
|
||||
return getPlayerCurrency(uuid).getOrDefault(typeID, 0d);
|
||||
private static double getPlayerCurrency(@NotNull Connection connection, @NotNull UUID uuid, @NotNull CurrencyType type) throws SQLException {
|
||||
try (PreparedStatement statement = connection.prepareStatement(
|
||||
"SELECT currency, amount FROM hamster_currency_data WHERE `uuid`=?;"
|
||||
)) {
|
||||
statement.setString(1, uuid.toString());
|
||||
try (ResultSet set = statement.executeQuery()) {
|
||||
if (set.next()) {
|
||||
return set.getDouble("amount");
|
||||
}
|
||||
return type.getDefaultAmount();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void addPlayerCurrency(@NotNull UUID uuid, @NotNull String typeID, double amount, boolean broadcast) throws SQLException {
|
||||
Map<String, Double> currency = getPlayerCurrency(uuid);
|
||||
currency.put(typeID, currency.getOrDefault(typeID, 0D) + amount);
|
||||
if (broadcast) {
|
||||
public static void givePlayerCurrency(@NotNull UUID uuid, @NotNull CurrencyType type, double amount) throws SQLException {
|
||||
String typeID = type.getId();
|
||||
try (Connection connection = CoreAPI.getInstance().getConnection()) {
|
||||
try (PreparedStatement statement = connection.prepareStatement(
|
||||
"INSERT INTO hamster_currency_data VALUES(?, ?, ?) ON DUPLICATE KEY UPDATE amount=amount+?;"
|
||||
)) {
|
||||
statement.setString(1, uuid.toString());
|
||||
statement.setString(2, typeID);
|
||||
statement.setDouble(3, amount);
|
||||
statement.setDouble(3, type.getDefaultAmount() + amount);
|
||||
statement.setDouble(4, amount);
|
||||
statement.executeUpdate();
|
||||
}
|
||||
if (type.isEnableLog()) {
|
||||
double currency = getPlayerCurrency(connection, uuid, type);
|
||||
try (PreparedStatement statement = connection.prepareStatement(
|
||||
"INSERT INTO hamster_currency_logs VALUES(?, ?, ?, ?, ?, NOW());"
|
||||
)) {
|
||||
statement.setString(1, uuid.toString());
|
||||
statement.setString(2, type.getId());
|
||||
statement.setString(3, "MODIFY");
|
||||
statement.setDouble(4, amount);
|
||||
statement.setDouble(5, currency);
|
||||
statement.executeUpdate();
|
||||
}
|
||||
}
|
||||
JsonObject object = new JsonObject();
|
||||
object.addProperty("uuid", uuid.toString());
|
||||
object.addProperty("typeID", typeID);
|
||||
object.addProperty("amount", amount);
|
||||
BallAPI.getInstance().sendBallMessage(CurrencyPlugin.BALL_CHANNEL, "addPlayerCurrency", object);
|
||||
}
|
||||
}
|
||||
|
||||
public static void takePlayerCurrency(@NotNull UUID uuid, @NotNull String typeID, double amount, boolean broadcast) throws SQLException {
|
||||
addPlayerCurrency(uuid, typeID, -amount, broadcast);
|
||||
public static void takePlayerCurrency(@NotNull UUID uuid, @NotNull CurrencyType type, double amount) throws SQLException {
|
||||
givePlayerCurrency(uuid, type, -amount);
|
||||
}
|
||||
|
||||
public static void setPlayerCurrency(@NotNull UUID uuid, @NotNull String typeID, double amount, boolean broadcast) throws SQLException {
|
||||
Map<String, Double> currency = getPlayerCurrency(uuid);
|
||||
currency.put(typeID, amount);
|
||||
if (broadcast) {
|
||||
public static void setPlayerCurrency(@NotNull UUID uuid, @NotNull CurrencyType type, double amount) throws SQLException {
|
||||
try (Connection connection = CoreAPI.getInstance().getConnection()) {
|
||||
try (PreparedStatement statement = connection.prepareStatement(
|
||||
"INSERT INTO hamster_currency_data VALUES(?, ?, ?) ON DUPLICATE KEY UPDATE amount=?;"
|
||||
)) {
|
||||
statement.setString(1, uuid.toString());
|
||||
statement.setString(2, typeID);
|
||||
statement.setString(2, type.getId());
|
||||
statement.setDouble(3, amount);
|
||||
statement.setDouble(4, amount);
|
||||
statement.executeUpdate();
|
||||
}
|
||||
if (type.isEnableLog()) {
|
||||
double currency = getPlayerCurrency(connection, uuid, type);
|
||||
try (PreparedStatement statement = connection.prepareStatement(
|
||||
"INSERT INTO hamster_currency_logs VALUES(?, ?, ?, ?, ?, NOW());"
|
||||
)) {
|
||||
statement.setString(1, uuid.toString());
|
||||
statement.setString(2, type.getId());
|
||||
statement.setString(3, "SET");
|
||||
statement.setDouble(4, amount);
|
||||
statement.setDouble(5, currency);
|
||||
statement.executeUpdate();
|
||||
}
|
||||
}
|
||||
JsonObject object = new JsonObject();
|
||||
object.addProperty("uuid", uuid.toString());
|
||||
object.addProperty("typeID", typeID);
|
||||
object.addProperty("amount", amount);
|
||||
BallAPI.getInstance().sendBallMessage(CurrencyPlugin.BALL_CHANNEL, "setPlayerCurrency", object);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -138,11 +159,10 @@ public final class CurrencyDataManager {
|
||||
if (broadcast) {
|
||||
try (Connection connection = CoreAPI.getInstance().getConnection()) {
|
||||
try (PreparedStatement statement = connection.prepareStatement(
|
||||
"REPLACE INTO hamster_currency_type VALUES(?, ?, ?);"
|
||||
"REPLACE INTO hamster_currency_type VALUES(?, ?);"
|
||||
)) {
|
||||
statement.setString(1, type.getId());
|
||||
statement.setString(2, type.getName());
|
||||
statement.setBoolean(3, type.isTransferable());
|
||||
statement.setString(2, CoreAPI.getInstance().getGson().toJson(type));
|
||||
statement.executeUpdate();
|
||||
}
|
||||
BallAPI.getInstance().sendBallMessage(CurrencyPlugin.BALL_CHANNEL, "updateCurrencyType", type);
|
||||
@@ -174,9 +194,4 @@ public final class CurrencyDataManager {
|
||||
public static CurrencyType getCurrencyType(@NotNull String id) {
|
||||
return CURRENCY_TYPES.get(id);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static CurrencyType getVaultCurrencyType() {
|
||||
return getCurrencyType(CurrencyConfigManager.getVaultCurrencyID());
|
||||
}
|
||||
}
|
||||
|
@@ -2,6 +2,7 @@ package cn.hamster3.mc.plugin.currency.data;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
@@ -9,13 +10,27 @@ public class CurrencyType {
|
||||
/**
|
||||
* 货币 ID
|
||||
*/
|
||||
@NotNull
|
||||
private String id;
|
||||
/**
|
||||
* 显示名称
|
||||
*/
|
||||
@NotNull
|
||||
private String name;
|
||||
/**
|
||||
* 是否允许转账
|
||||
* 默认余额
|
||||
*/
|
||||
private boolean transferable;
|
||||
private double defaultAmount;
|
||||
|
||||
/**
|
||||
* 是否启用日志
|
||||
*/
|
||||
private boolean enableLog;
|
||||
|
||||
public CurrencyType(@NotNull String id) {
|
||||
this.id = id;
|
||||
name = id;
|
||||
defaultAmount = 0;
|
||||
enableLog = false;
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package cn.hamster3.mc.plugin.currency.hook;
|
||||
|
||||
import cn.hamster3.mc.plugin.currency.api.CurrencyAPI;
|
||||
import cn.hamster3.mc.plugin.currency.data.CurrencyType;
|
||||
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
@@ -32,7 +33,11 @@ public final class PlaceholderHook extends PlaceholderExpansion {
|
||||
|
||||
@Override
|
||||
public String onRequest(OfflinePlayer player, @NotNull String params) {
|
||||
return String.format("%.2f", CurrencyAPI.getPlayerCurrency(player.getUniqueId(), params).getOrDefault(0D));
|
||||
CurrencyType type = CurrencyAPI.getCurrencyType(params);
|
||||
if (type == null) {
|
||||
return "0";
|
||||
}
|
||||
return String.format("%.2f", CurrencyAPI.getPlayerCurrency(player.getUniqueId(), type).orElse(0D));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -1,24 +1,27 @@
|
||||
package cn.hamster3.mc.plugin.currency.hook;
|
||||
|
||||
import cn.hamster3.mc.plugin.ball.common.api.BallAPI;
|
||||
import cn.hamster3.mc.plugin.currency.api.CurrencyAPI;
|
||||
import cn.hamster3.mc.plugin.currency.CurrencyPlugin;
|
||||
import cn.hamster3.mc.plugin.currency.core.CurrencyConfigManager;
|
||||
import cn.hamster3.mc.plugin.currency.core.CurrencyDataManager;
|
||||
import cn.hamster3.mc.plugin.currency.data.CurrencyType;
|
||||
import net.milkbowl.vault.economy.AbstractEconomy;
|
||||
import net.milkbowl.vault.economy.EconomyResponse;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public class VaultEconomyHook extends AbstractEconomy {
|
||||
public static final VaultEconomyHook INSTANCE = new VaultEconomyHook();
|
||||
private static final EconomyResponse NOT_IMPLEMENTED = new EconomyResponse(0, 0, EconomyResponse.ResponseType.NOT_IMPLEMENTED, "HamsterCurrency 未实现该功能~");
|
||||
private static final EconomyResponse CURRENCY_TYPE_NOT_FOUND = new EconomyResponse(0, 0, EconomyResponse.ResponseType.NOT_IMPLEMENTED, "Vault 货币类型设置出错");
|
||||
private static final EconomyResponse NOT_IMPLEMENTED = new EconomyResponse(0, 0, EconomyResponse.ResponseType.NOT_IMPLEMENTED, "not implemented");
|
||||
@NotNull
|
||||
private final CurrencyType type;
|
||||
|
||||
private VaultEconomyHook() {
|
||||
public VaultEconomyHook(@NotNull CurrencyType type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -51,23 +54,24 @@ public class VaultEconomyHook extends AbstractEconomy {
|
||||
return CurrencyConfigManager.getVaultNameSingular();
|
||||
}
|
||||
|
||||
public double getBalance(UUID uuid) {
|
||||
try {
|
||||
return CurrencyDataManager.getPlayerCurrency(uuid, type);
|
||||
} catch (SQLException e) {
|
||||
CurrencyPlugin.error(e);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getBalance(String playerName) {
|
||||
UUID uuid = BallAPI.getInstance().getPlayerInfo(playerName).getUuid();
|
||||
CurrencyType type = CurrencyDataManager.getVaultCurrencyType();
|
||||
if (type == null) {
|
||||
return 0D;
|
||||
}
|
||||
return CurrencyAPI.getPlayerCurrency(uuid, type.getId()).getSilent();
|
||||
return getBalance(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getBalance(OfflinePlayer player) {
|
||||
CurrencyType type = CurrencyDataManager.getVaultCurrencyType();
|
||||
if (type == null) {
|
||||
return 0D;
|
||||
}
|
||||
return CurrencyAPI.getPlayerCurrency(player.getUniqueId(), type.getId()).getSilent();
|
||||
return getBalance(player.getUniqueId());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -100,36 +104,27 @@ public class VaultEconomyHook extends AbstractEconomy {
|
||||
return getBalance(player) >= amount;
|
||||
}
|
||||
|
||||
public EconomyResponse withdrawPlayer(UUID uuid, double amount) {
|
||||
try {
|
||||
CurrencyDataManager.takePlayerCurrency(uuid, type, amount);
|
||||
double result = CurrencyDataManager.getPlayerCurrency(uuid, type);
|
||||
return new EconomyResponse(amount, result, EconomyResponse.ResponseType.SUCCESS, "");
|
||||
} catch (SQLException e) {
|
||||
CurrencyPlugin.error(e);
|
||||
return new EconomyResponse(amount, -1, EconomyResponse.ResponseType.FAILURE, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public EconomyResponse withdrawPlayer(String playerName, double amount) {
|
||||
CurrencyType type = CurrencyDataManager.getVaultCurrencyType();
|
||||
if (type == null) {
|
||||
return CURRENCY_TYPE_NOT_FOUND;
|
||||
}
|
||||
UUID uuid = BallAPI.getInstance().getPlayerInfo(playerName).getUuid();
|
||||
double balance = CurrencyAPI.getPlayerCurrency(uuid, type.getId()).getSilent();
|
||||
if (balance < amount) {
|
||||
return new EconomyResponse(amount, balance, EconomyResponse.ResponseType.FAILURE, "余额不足");
|
||||
}
|
||||
double result = balance - amount;
|
||||
CurrencyAPI.setPlayerCurrency(uuid, type.getId(), result).joinSilent();
|
||||
return new EconomyResponse(amount, result, EconomyResponse.ResponseType.SUCCESS, "扣款成功");
|
||||
return withdrawPlayer(uuid, amount);
|
||||
}
|
||||
|
||||
@Override
|
||||
public EconomyResponse withdrawPlayer(OfflinePlayer player, double amount) {
|
||||
CurrencyType type = CurrencyDataManager.getVaultCurrencyType();
|
||||
if (type == null) {
|
||||
return CURRENCY_TYPE_NOT_FOUND;
|
||||
}
|
||||
UUID uuid = player.getUniqueId();
|
||||
double balance = CurrencyAPI.getPlayerCurrency(uuid, type.getId()).getSilent();
|
||||
if (balance < amount) {
|
||||
return new EconomyResponse(amount, balance, EconomyResponse.ResponseType.FAILURE, "余额不足");
|
||||
}
|
||||
double result = balance - amount;
|
||||
CurrencyAPI.setPlayerCurrency(uuid, type.getId(), result).joinSilent();
|
||||
return new EconomyResponse(amount, result, EconomyResponse.ResponseType.SUCCESS, "扣款成功");
|
||||
return withdrawPlayer(uuid, amount);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -142,30 +137,27 @@ public class VaultEconomyHook extends AbstractEconomy {
|
||||
return withdrawPlayer(player, amount);
|
||||
}
|
||||
|
||||
public EconomyResponse depositPlayer(UUID uuid, double amount) {
|
||||
try {
|
||||
CurrencyDataManager.givePlayerCurrency(uuid, type, amount);
|
||||
double result = CurrencyDataManager.getPlayerCurrency(uuid, type);
|
||||
return new EconomyResponse(amount, result, EconomyResponse.ResponseType.SUCCESS, "");
|
||||
} catch (SQLException e) {
|
||||
CurrencyPlugin.error(e);
|
||||
return new EconomyResponse(amount, -1, EconomyResponse.ResponseType.FAILURE, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public EconomyResponse depositPlayer(String playerName, double amount) {
|
||||
CurrencyType type = CurrencyDataManager.getVaultCurrencyType();
|
||||
if (type == null) {
|
||||
return CURRENCY_TYPE_NOT_FOUND;
|
||||
}
|
||||
UUID uuid = BallAPI.getInstance().getPlayerInfo(playerName).getUuid();
|
||||
double balance = CurrencyAPI.getPlayerCurrency(uuid, type.getId()).getSilent();
|
||||
double result = balance + amount;
|
||||
CurrencyAPI.setPlayerCurrency(uuid, type.getId(), result).joinSilent();
|
||||
return new EconomyResponse(amount, result, EconomyResponse.ResponseType.SUCCESS, "");
|
||||
return depositPlayer(uuid, amount);
|
||||
}
|
||||
|
||||
@Override
|
||||
public EconomyResponse depositPlayer(OfflinePlayer player, double amount) {
|
||||
CurrencyType type = CurrencyDataManager.getVaultCurrencyType();
|
||||
if (type == null) {
|
||||
return CURRENCY_TYPE_NOT_FOUND;
|
||||
}
|
||||
UUID uuid = player.getUniqueId();
|
||||
double balance = CurrencyAPI.getPlayerCurrency(uuid, type.getId()).getSilent();
|
||||
double result = balance + amount;
|
||||
CurrencyAPI.setPlayerCurrency(uuid, type.getId(), result).joinSilent();
|
||||
return new EconomyResponse(amount, result, EconomyResponse.ResponseType.SUCCESS, "");
|
||||
return depositPlayer(uuid, amount);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -5,12 +5,9 @@ import cn.hamster3.mc.plugin.currency.CurrencyPlugin;
|
||||
import cn.hamster3.mc.plugin.currency.core.CurrencyDataManager;
|
||||
import cn.hamster3.mc.plugin.currency.data.CurrencyType;
|
||||
import com.google.common.eventbus.Subscribe;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.UUID;
|
||||
|
||||
@SuppressWarnings("CallToPrintStackTrace")
|
||||
public class BallListener {
|
||||
public static final BallListener INSTANCE = new BallListener();
|
||||
|
||||
@@ -23,36 +20,12 @@ public class BallListener {
|
||||
return;
|
||||
}
|
||||
switch (event.getAction()) {
|
||||
case "addPlayerCurrency": {
|
||||
JsonObject object = event.getContentAsJsonObject();
|
||||
UUID uuid = UUID.fromString(object.get("uuid").getAsString());
|
||||
String typeID = object.get("typeID").getAsString();
|
||||
double amount = object.get("amount").getAsDouble();
|
||||
try {
|
||||
CurrencyDataManager.addPlayerCurrency(uuid, typeID, amount, false);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "setPlayerCurrency": {
|
||||
JsonObject object = event.getContentAsJsonObject();
|
||||
UUID uuid = UUID.fromString(object.get("uuid").getAsString());
|
||||
String typeID = object.get("typeID").getAsString();
|
||||
double amount = object.get("amount").getAsDouble();
|
||||
try {
|
||||
CurrencyDataManager.setPlayerCurrency(uuid, typeID, amount, false);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "updateCurrencyType": {
|
||||
CurrencyType type = event.getContentAs(CurrencyType.class);
|
||||
try {
|
||||
CurrencyDataManager.updateCurrencyType(type, false);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
CurrencyPlugin.error(e);
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -60,7 +33,7 @@ public class BallListener {
|
||||
try {
|
||||
CurrencyDataManager.deleteCurrencyType(event.getContentAsString(), false);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
CurrencyPlugin.error(e);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
@@ -1,33 +0,0 @@
|
||||
package cn.hamster3.mc.plugin.currency.listener;
|
||||
|
||||
import cn.hamster3.mc.plugin.currency.CurrencyPlugin;
|
||||
import cn.hamster3.mc.plugin.currency.core.CurrencyDataManager;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerLoginEvent;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.UUID;
|
||||
|
||||
@SuppressWarnings("CallToPrintStackTrace")
|
||||
public class CurrencyListener implements Listener {
|
||||
public static final CurrencyListener INSTANCE = new CurrencyListener();
|
||||
|
||||
private CurrencyListener() {
|
||||
}
|
||||
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onPlayerLogin(PlayerLoginEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
UUID uuid = player.getUniqueId();
|
||||
CurrencyPlugin.async(() -> {
|
||||
try {
|
||||
CurrencyDataManager.loadPlayerData(uuid);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user