Skip to content

Commit

Permalink
所持金保存
Browse files Browse the repository at this point in the history
  • Loading branch information
Quarri6343 committed Aug 13, 2023
1 parent 3ba0cb7 commit 8eeed0e
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 30 deletions.
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id 'java'
id "com.github.johnrengelman.shadow" version "8.1.1"
id "io.freefair.lombok" version "6.6.1"
}

group = 'net.riblab'
Expand Down Expand Up @@ -32,6 +33,8 @@ dependencies {
compileOnly "com.github.MilkBowl:VaultAPI:1.7"
implementation 'dev.jorel:commandapi-bukkit-shade:9.0.3'
implementation 'de.tr7zw:item-nbt-api-plugin:2.11.3'
implementation 'com.github.Exlll.ConfigLib:configlib-yaml:v4.2.0'
compileOnly "org.projectlombok:lombok:1.18.24"
}

def targetJavaVersion = 17
Expand Down Expand Up @@ -62,6 +65,7 @@ processResources {
shadowJar {
relocate 'de.tr7zw.nbtapi', 'net.riblab.tradecore.nbtapi'
relocate 'dev.jorel.commandapi', 'net.riblab.tradecore.commandapi'
relocate 'de.exlll.configlib', 'net.riblab.tradecore.configlib'
}

//Disable jar and replace with shadowJar
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/net/riblab/tradecore/ConfigManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package net.riblab.tradecore;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import de.exlll.configlib.Comment;
import de.exlll.configlib.Configuration;
import de.exlll.configlib.YamlConfigurations;
import lombok.Getter;

public class ConfigManager {

@Configuration
public static class CurrencyData {
@Comment({"所持金"})
public Map<UUID,Double> playerBank = new HashMap<>();
@Comment({"所持投票チケット数"})
public Map<UUID,Integer> playerTickets = new HashMap<>();
}

@Getter
private CurrencyData currencyData;
private static final Path currencyConfigFile = new File(TradeCore.getInstance().getDataFolder(), "currency.yml").toPath();

public void save(){
YamlConfigurations.save(currencyConfigFile, CurrencyData.class, currencyData);
}

public void load(){
// Load a new instance from the configuration file
currencyData = YamlConfigurations.update(currencyConfigFile, CurrencyData.class);
}
}
48 changes: 24 additions & 24 deletions src/main/java/net/riblab/tradecore/EconomyImplementer.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
import java.util.UUID;

public class EconomyImplementer implements Economy {

private TradeCore plugin = TradeCore.getInstance();
private final ConfigManager.CurrencyData data = TradeCore.getInstance().getConfigManager().getCurrencyData();

@Override
public boolean isEnabled() {
Expand Down Expand Up @@ -57,7 +56,8 @@ public boolean hasAccount(String s) {

@Override
public boolean hasAccount(OfflinePlayer offlinePlayer) {
return plugin.playerBank.get(offlinePlayer) != null;
UUID uuid = offlinePlayer.getUniqueId();
return data.playerBank.get(uuid) != null;
}

@Override
Expand All @@ -74,26 +74,26 @@ public boolean hasAccount(OfflinePlayer offlinePlayer, String s) {
public double getBalance(String s) {
Player player = Bukkit.getPlayer(s);
UUID uuid = player.getUniqueId();
return plugin.playerBank.get(uuid);
return data.playerBank.get(uuid);
}

@Override
public double getBalance(OfflinePlayer offlinePlayer) {
UUID uuid = offlinePlayer.getUniqueId();
return plugin.playerBank.get(uuid);
return data.playerBank.get(uuid);
}

@Override
public double getBalance(String s, String s1) {
Player player = Bukkit.getPlayer(s);
UUID uuid = player.getUniqueId();
return plugin.playerBank.get(uuid);
return data.playerBank.get(uuid);
}

@Override
public double getBalance(OfflinePlayer offlinePlayer, String s) {
UUID uuid = offlinePlayer.getUniqueId();
return plugin.playerBank.get(uuid);
return data.playerBank.get(uuid);
}

@Override
Expand All @@ -120,67 +120,67 @@ public boolean has(OfflinePlayer offlinePlayer, String s, double v) {
public EconomyResponse withdrawPlayer(String s, double v) {
Player player = Bukkit.getPlayer(s);
UUID uuid = player.getUniqueId();
double oldBalance = plugin.playerBank.get(uuid);
plugin.playerBank.put(uuid, oldBalance - v);
double oldBalance = data.playerBank.get(uuid);
data.playerBank.put(uuid, oldBalance - v);
return null;
}

@Override
public EconomyResponse withdrawPlayer(OfflinePlayer offlinePlayer, double v) {
UUID uuid = offlinePlayer.getUniqueId();
double oldBalance = plugin.playerBank.get(uuid);
plugin.playerBank.put(uuid, oldBalance - v);
double oldBalance = data.playerBank.get(uuid);
data.playerBank.put(uuid, oldBalance - v);
return null;
}

@Override
public EconomyResponse withdrawPlayer(String s, String s1, double v) {
Player player = Bukkit.getPlayer(s);
UUID uuid = player.getUniqueId();
double oldBalance = plugin.playerBank.get(uuid);
plugin.playerBank.put(uuid, oldBalance - v);
double oldBalance = data.playerBank.get(uuid);
data.playerBank.put(uuid, oldBalance - v);
return null;
}

@Override
public EconomyResponse withdrawPlayer(OfflinePlayer offlinePlayer, String s, double v) {
UUID uuid = offlinePlayer.getUniqueId();
double oldBalance = plugin.playerBank.get(uuid);
plugin.playerBank.put(uuid, oldBalance - v);
double oldBalance = data.playerBank.get(uuid);
data.playerBank.put(uuid, oldBalance - v);
return null;
}

@Override
public EconomyResponse depositPlayer(String s, double v) {
Player player = Bukkit.getPlayer(s);
UUID uuid = player.getUniqueId();
double oldBalance = plugin.playerBank.get(uuid);
plugin.playerBank.put(uuid, oldBalance + v);
double oldBalance = data.playerBank.get(uuid);
data.playerBank.put(uuid, oldBalance + v);
return null;
}

@Override
public EconomyResponse depositPlayer(OfflinePlayer offlinePlayer, double v) {
UUID uuid = offlinePlayer.getUniqueId();
double oldBalance = plugin.playerBank.get(uuid);
plugin.playerBank.put(uuid, oldBalance + v);
double oldBalance = data.playerBank.get(uuid);
data.playerBank.put(uuid, oldBalance + v);
return null;
}

@Override
public EconomyResponse depositPlayer(String s, String s1, double v) {
Player player = Bukkit.getPlayer(s);
UUID uuid = player.getUniqueId();
double oldBalance = plugin.playerBank.get(uuid);
plugin.playerBank.put(uuid, oldBalance + v);
double oldBalance = data.playerBank.get(uuid);
data.playerBank.put(uuid, oldBalance + v);
return null;
}

@Override
public EconomyResponse depositPlayer(OfflinePlayer offlinePlayer, String s, double v) {
UUID uuid = offlinePlayer.getUniqueId();
double oldBalance = plugin.playerBank.get(uuid);
plugin.playerBank.put(uuid, oldBalance + v);
double oldBalance = data.playerBank.get(uuid);
data.playerBank.put(uuid, oldBalance + v);
return null;
}

Expand Down Expand Up @@ -255,7 +255,7 @@ public boolean createPlayerAccount(OfflinePlayer offlinePlayer) {
return false;

UUID uuid = offlinePlayer.getUniqueId();
plugin.playerBank.put(offlinePlayer.getUniqueId(), 0d);
data.playerBank.put(offlinePlayer.getUniqueId(), 0d);
return true;
}

Expand Down
23 changes: 18 additions & 5 deletions src/main/java/net/riblab/tradecore/TradeCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import dev.jorel.commandapi.CommandPermission;
import dev.jorel.commandapi.arguments.DoubleArgument;
import dev.jorel.commandapi.arguments.PlayerArgument;
import lombok.Getter;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import org.bukkit.Bukkit;
Expand All @@ -23,11 +24,11 @@
public final class TradeCore extends JavaPlugin implements Listener {

private static TradeCore instance;
public EconomyImplementer economy;
@Getter
private EconomyImplementer economy;
private VaultHook vaultHook;

public final HashMap<UUID,Double> playerBank = new HashMap<>();

@Getter
private ConfigManager configManager;
public TradeCore() {
instance = this;
}
Expand Down Expand Up @@ -56,6 +57,9 @@ public void onLoad(){

@Override
public void onEnable() {
configManager = new ConfigManager();
configManager.load();

economy = new EconomyImplementer();
vaultHook = new VaultHook();
vaultHook.hook();
Expand All @@ -80,18 +84,27 @@ public void run() {
Component text = Component.text("");
text = text.append(Component.text(negativeSpace + negativeSpace + negativeSpace + negativeSpace + TCResourcePackData.IconsFont.COIN.get_char()).font(TCResourcePackData.iconsFontName));
text = text.append(Component.text(" " + balance).font(TCResourcePackData.yPlus12FontName));
text = text.append(Component.text(" " + TCResourcePackData.IconsFont.VOTE_TICKET.get_char()).font(TCResourcePackData.iconsFontName));
text = text.append(Component.text(" " + TCResourcePackData.IconsFont.VOTE_TICKET.get_char()).font(TCResourcePackData.iconsFontName));
text = text.append(Component.text(" 0").font(TCResourcePackData.yPlus12FontName));
player.sendActionBar(text);
});
}
}.runTaskTimer(this, 0, 20);

//定期的にコンフィグを保存
new BukkitRunnable(){
@Override
public void run() {
configManager.save();
}
}.runTaskTimer(this, 0, 3600);
}

@Override
public void onDisable() {
vaultHook.unhook();
CommandAPI.onDisable();
configManager.save();
}

@EventHandler
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/riblab/tradecore/VaultHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class VaultHook {
private Economy provider;

public void hook() {
provider = plugin.economy;
provider = plugin.getEconomy();
Bukkit.getServicesManager().register(Economy.class, this.provider, this.plugin, ServicePriority.Normal);
Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "VaultAPI hooked into " + ChatColor.AQUA + plugin.getName());
}
Expand Down

0 comments on commit 8eeed0e

Please sign in to comment.