Configured build to use VaultUnlocked.
This commit is contained in:
parent
580c915af8
commit
a5dd9f5060
12 changed files with 405 additions and 61 deletions
17
src/main/java/io/github/adrianvic/itemeconomy/Config.java
Normal file
17
src/main/java/io/github/adrianvic/itemeconomy/Config.java
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
package io.github.adrianvic.itemeconomy;
|
||||
|
||||
import org.bukkit.Material;
|
||||
|
||||
public class Config {
|
||||
public static Material ITEM;
|
||||
public static String FORMAT;
|
||||
public static String PLURAL;
|
||||
public static String SINGULAR;
|
||||
|
||||
public static void loadConfig(UnrealConfig conf) {
|
||||
ITEM = Material.valueOf(((String)conf.get("item")).toUpperCase());
|
||||
FORMAT = (String)conf.get("format");
|
||||
PLURAL = (String)conf.get("plural");
|
||||
SINGULAR = (String)conf.get("singular");
|
||||
}
|
||||
}
|
||||
50
src/main/java/io/github/adrianvic/itemeconomy/Main.java
Normal file
50
src/main/java/io/github/adrianvic/itemeconomy/Main.java
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
package io.github.adrianvic.itemeconomy;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import net.milkbowl.vault.economy.Economy;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.plugin.ServicePriority;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
public class Main extends JavaPlugin {
|
||||
public void onEnable() {
|
||||
Config.loadConfig(new UnrealConfig(this, this.getDataFolder(), "config.yml"));
|
||||
Bukkit.getServicesManager().register(Economy.class, new VaultLayer(), this, ServicePriority.High);
|
||||
}
|
||||
|
||||
public void onDisable() {
|
||||
super.onDisable();
|
||||
}
|
||||
|
||||
public static List<ItemStack> getInventory(Player player) {
|
||||
return Arrays.stream(player.getInventory().getContents()).map((o) -> {
|
||||
return o == null ? new ItemStack(Material.AIR) : o;
|
||||
}).toList();
|
||||
}
|
||||
|
||||
public static boolean removeItems(Player player, Material type, int amount) {
|
||||
if (player.getInventory().all(type).values().stream().mapToInt(ItemStack::getAmount).sum() < amount) {
|
||||
return false;
|
||||
} else {
|
||||
player.getInventory().removeItem(new ItemStack[]{new ItemStack(type, amount)});
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public static void addItems(Player player, Material type, int amount) {
|
||||
HashMap<Integer, ItemStack> nope = player.getInventory().addItem(new ItemStack[]{new ItemStack(type, amount)});
|
||||
Iterator var4 = nope.values().iterator();
|
||||
|
||||
while(var4.hasNext()) {
|
||||
ItemStack v = (ItemStack)var4.next();
|
||||
player.getWorld().dropItemNaturally(player.getLocation(), v);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,92 @@
|
|||
package io.github.adrianvic.itemeconomy;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.CopyOption;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
|
||||
public class UnrealConfig extends HashMap<String, Object> {
|
||||
private static final Yaml yaml = new Yaml();
|
||||
private final File file;
|
||||
|
||||
public UnrealConfig(Object plugin, File dataDirectory, String filename) {
|
||||
this(plugin, dataDirectory.toPath(), filename, filename);
|
||||
}
|
||||
|
||||
public UnrealConfig(Object plugin, File dataDirectory, String filename, String default_filename) {
|
||||
this(plugin, dataDirectory.toPath(), filename, default_filename);
|
||||
}
|
||||
|
||||
public UnrealConfig(Object plugin, Path dataDirectory, String filename) {
|
||||
this(plugin, dataDirectory, filename, filename);
|
||||
}
|
||||
|
||||
public UnrealConfig(Object plugin, Path dataDirectory, String filename, String default_filename) {
|
||||
this.file = Paths.get(dataDirectory.toFile().getPath(), filename).toFile();
|
||||
if (!dataDirectory.toFile().exists()) {
|
||||
dataDirectory.toFile().mkdir();
|
||||
}
|
||||
|
||||
if (!this.file.exists()) {
|
||||
try {
|
||||
InputStream stream = plugin.getClass().getClassLoader().getResourceAsStream(default_filename);
|
||||
|
||||
try {
|
||||
assert stream != null;
|
||||
|
||||
Files.copy(stream, this.file.toPath(), new CopyOption[0]);
|
||||
} catch (Throwable var9) {
|
||||
if (stream != null) {
|
||||
try {
|
||||
stream.close();
|
||||
} catch (Throwable var8) {
|
||||
var9.addSuppressed(var8);
|
||||
}
|
||||
}
|
||||
|
||||
throw var9;
|
||||
}
|
||||
|
||||
if (stream != null) {
|
||||
stream.close();
|
||||
}
|
||||
} catch (IOException var10) {
|
||||
throw new RuntimeException(var10);
|
||||
}
|
||||
}
|
||||
|
||||
this.reload();
|
||||
}
|
||||
|
||||
public void reload() {
|
||||
try {
|
||||
this.clear();
|
||||
this.putAll((Map)yaml.load(new FileInputStream(this.file)));
|
||||
} catch (FileNotFoundException var2) {
|
||||
var2.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void save() {
|
||||
try {
|
||||
yaml.dump(this, new FileWriter(this.file));
|
||||
} catch (IOException var2) {
|
||||
var2.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public Map<String, Object> clone() {
|
||||
return new HashMap(this);
|
||||
}
|
||||
}
|
||||
213
src/main/java/io/github/adrianvic/itemeconomy/VaultLayer.java
Normal file
213
src/main/java/io/github/adrianvic/itemeconomy/VaultLayer.java
Normal file
|
|
@ -0,0 +1,213 @@
|
|||
package io.github.adrianvic.itemeconomy;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import net.milkbowl.vault.economy.Economy;
|
||||
import net.milkbowl.vault.economy.EconomyResponse;
|
||||
import net.milkbowl.vault.economy.EconomyResponse.ResponseType;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
public class VaultLayer implements Economy {
|
||||
public boolean isEnabled() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return "ItemEconomy";
|
||||
}
|
||||
|
||||
public boolean hasBankSupport() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public int fractionalDigits() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public String format(double amount) {
|
||||
return Config.FORMAT.replace("{}", String.valueOf(amount));
|
||||
}
|
||||
|
||||
public String currencyNamePlural() {
|
||||
return Config.PLURAL;
|
||||
}
|
||||
|
||||
public String currencyNameSingular() {
|
||||
return Config.SINGULAR;
|
||||
}
|
||||
|
||||
public boolean hasAccount(String playerName) {
|
||||
return Bukkit.getPlayer(playerName) != null;
|
||||
}
|
||||
|
||||
public double getBalance(String playerName) {
|
||||
Player player = Bukkit.getPlayer(playerName);
|
||||
return player != null ? (double)Main.getInventory(player).stream().filter(Objects::nonNull).filter((i) -> {
|
||||
return i.getType().equals(Config.ITEM);
|
||||
}).mapToInt(ItemStack::getAmount).sum() : 0.0D;
|
||||
}
|
||||
|
||||
public boolean has(String playerName, double amount) {
|
||||
return this.getBalance(playerName) >= amount;
|
||||
}
|
||||
|
||||
public EconomyResponse withdrawPlayer(String playerName, double amount) {
|
||||
if (amount == 0.0D) {
|
||||
return new EconomyResponse(amount, this.getBalance(playerName), ResponseType.SUCCESS, (String)null);
|
||||
} else if (amount < 0.0D) {
|
||||
return this.depositPlayer(playerName, -amount);
|
||||
} else if (!this.has(playerName, amount)) {
|
||||
return new EconomyResponse(amount, this.getBalance(playerName), ResponseType.FAILURE, "Недостаточно средств");
|
||||
} else {
|
||||
Player player;
|
||||
if ((player = Bukkit.getPlayer(playerName)) == null) {
|
||||
return new EconomyResponse(amount, this.getBalance(playerName), ResponseType.FAILURE, "Игрок офлайн");
|
||||
} else {
|
||||
return !Main.removeItems(player, Config.ITEM, (int)amount) ? new EconomyResponse(amount, this.getBalance(playerName), ResponseType.FAILURE, "Недостаточно средств") : new EconomyResponse(amount, this.getBalance(playerName), ResponseType.SUCCESS, (String)null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public EconomyResponse depositPlayer(String playerName, double amount) {
|
||||
if (amount == 0.0D) {
|
||||
return new EconomyResponse(amount, this.getBalance(playerName), ResponseType.SUCCESS, (String)null);
|
||||
} else if (amount < 0.0D) {
|
||||
return this.withdrawPlayer(playerName, -amount);
|
||||
} else {
|
||||
Player player;
|
||||
if ((player = Bukkit.getPlayer(playerName)) == null) {
|
||||
return new EconomyResponse(amount, this.getBalance(playerName), ResponseType.FAILURE, "Игрок офлайн");
|
||||
} else {
|
||||
Main.addItems(player, Config.ITEM, (int)amount);
|
||||
return new EconomyResponse(amount, this.getBalance(playerName), ResponseType.SUCCESS, (String)null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean hasAccount(OfflinePlayer player) {
|
||||
return this.hasAccount(player.getName());
|
||||
}
|
||||
|
||||
public boolean hasAccount(String playerName, String worldName) {
|
||||
return this.hasAccount(playerName);
|
||||
}
|
||||
|
||||
public boolean hasAccount(OfflinePlayer player, String worldName) {
|
||||
return this.hasAccount(player.getName());
|
||||
}
|
||||
|
||||
public double getBalance(OfflinePlayer player) {
|
||||
return this.getBalance(player.getName());
|
||||
}
|
||||
|
||||
public double getBalance(String playerName, String world) {
|
||||
return this.getBalance(playerName);
|
||||
}
|
||||
|
||||
public double getBalance(OfflinePlayer player, String world) {
|
||||
return this.getBalance(player.getName());
|
||||
}
|
||||
|
||||
public boolean has(OfflinePlayer player, double amount) {
|
||||
return this.has(player.getName(), amount);
|
||||
}
|
||||
|
||||
public boolean has(String playerName, String worldName, double amount) {
|
||||
return this.has(playerName, amount);
|
||||
}
|
||||
|
||||
public boolean has(OfflinePlayer player, String worldName, double amount) {
|
||||
return this.has(player.getName(), amount);
|
||||
}
|
||||
|
||||
public EconomyResponse withdrawPlayer(OfflinePlayer player, double amount) {
|
||||
return this.withdrawPlayer(player.getName(), amount);
|
||||
}
|
||||
|
||||
public EconomyResponse withdrawPlayer(String playerName, String worldName, double amount) {
|
||||
return this.withdrawPlayer(playerName, amount);
|
||||
}
|
||||
|
||||
public EconomyResponse withdrawPlayer(OfflinePlayer player, String worldName, double amount) {
|
||||
return this.withdrawPlayer(player.getName(), amount);
|
||||
}
|
||||
|
||||
public EconomyResponse depositPlayer(OfflinePlayer player, double amount) {
|
||||
return this.depositPlayer(player.getName(), amount);
|
||||
}
|
||||
|
||||
public EconomyResponse depositPlayer(String playerName, String worldName, double amount) {
|
||||
return this.depositPlayer(playerName, amount);
|
||||
}
|
||||
|
||||
public EconomyResponse depositPlayer(OfflinePlayer player, String worldName, double amount) {
|
||||
return this.depositPlayer(player.getName(), amount);
|
||||
}
|
||||
|
||||
public EconomyResponse createBank(String name, String player) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public EconomyResponse createBank(String name, OfflinePlayer player) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public EconomyResponse deleteBank(String name) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public EconomyResponse bankBalance(String name) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public EconomyResponse bankHas(String name, double amount) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public EconomyResponse bankWithdraw(String name, double amount) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public EconomyResponse bankDeposit(String name, double amount) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public EconomyResponse isBankOwner(String name, String playerName) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public EconomyResponse isBankOwner(String name, OfflinePlayer player) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public EconomyResponse isBankMember(String name, String playerName) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public EconomyResponse isBankMember(String name, OfflinePlayer player) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<String> getBanks() {
|
||||
return List.of();
|
||||
}
|
||||
|
||||
public boolean createPlayerAccount(String playerName) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean createPlayerAccount(OfflinePlayer player) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean createPlayerAccount(String playerName, String worldName) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean createPlayerAccount(OfflinePlayer player, String worldName) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue