diff --git a/LICENSE b/LICENSE index 2f52f53..8804b94 100644 --- a/LICENSE +++ b/LICENSE @@ -1,22 +1,15 @@ -MIT License + ItemEconomy + Copyright (C) 2025 tenkuma -Copyright (c) 2025 Adrian Victor de Abreu Alves + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + You should have received a copy of the GNU General Public License + along with this program. If not, see . \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index 83ae306..640baeb 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -3,22 +3,20 @@ plugins { id("xyz.jpenilla.run-paper") version "2.3.1" } -group = "com.example" -version = System.getenv("YOURPLUGIN_VERSION_NAME") ?: "unknown" +group = "io.github.adrianvic.itemeconomy" +version = System.getenv("ITEMECO_VERSION_NAME") ?: "unknown" repositories { mavenCentral() maven("https://repo.papermc.io/repository/maven-public/") + maven("https://repo.codemc.io/repository/creatorfromhell/") } /* ----------------------------------------- */ /* SUPPORTED VERSIONS */ /* ----------------------------------------- */ -val mcVersions = listOf( - "b1_7_3", - "r1_21" -) +val mcVersions = listOf() /* ----------------------------------------- */ /* CREATE SOURCE SET PER VERSION */ @@ -59,9 +57,10 @@ mcVersions.forEach { ver -> /* ----------------------------------------- */ dependencies { - add("compileOnly", "io.papermc.paper:paper-api:1.21.10-R0.1-SNAPSHOT") - add("r1_21CompileOnly", "io.papermc.paper:paper-api:1.21.10-R0.1-SNAPSHOT") - add("b1_7_3CompileOnly", files("libs/craftbukkit-1060.jar")) + add("compileOnly", "io.papermc.paper:paper-api:1.21.11-R0.1-SNAPSHOT") + add("compileOnly", "net.milkbowl.vault:VaultUnlockedAPI:2.16") +// add("r1_21CompileOnly", "io.papermc.paper:paper-api:1.21.10-R0.1-SNAPSHOT") +// add("b1_7_3CompileOnly", files("libs/craftbukkit-1060.jar")) } /* ----------------------------------------- */ @@ -77,8 +76,8 @@ mcVersions.forEach { ver -> manifest { attributes( - "yourplugin-Impl-Version" to ver, - "yourplugin-Environment" to (System.getenv("YOURPLUGIN_BUILD_CHANNEL") ?: "dev") + "itemeco-Impl-Version" to ver, + "itemeco-Environment" to (System.getenv("ITEMECO_BUILD_CHANNEL") ?: "dev") ) } diff --git a/settings.gradle b/settings.gradle index 589c4e2..077147b 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1 +1 @@ -rootProject.name = 'yourplugin' +rootProject.name = 'ItemEconomy' diff --git a/src/b1_7_3/resources/plugin.yml b/src/b1_7_3/resources/plugin.yml deleted file mode 100644 index 88b1ecd..0000000 --- a/src/b1_7_3/resources/plugin.yml +++ /dev/null @@ -1,7 +0,0 @@ -name: "Your Plugin" -version: ${version} -main: com.example.yourplugin -api-version: '1.21' -author: 'Your Name' -website: "https://example.com" -description: "Put a description here" diff --git a/src/main/java/com/example/yourplugin/Main.java b/src/main/java/com/example/yourplugin/Main.java deleted file mode 100644 index 116844a..0000000 --- a/src/main/java/com/example/yourplugin/Main.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.example.yourplugin; - -import org.bukkit.plugin.java.JavaPlugin; - -public class Main extends JavaPlugin { - @Override - public void onEnable() { - getLogger().info("Example plugin is enabled."); - } - - @Override - public void onDisable() { - getLogger().info("Example plugin is disabled."); - } - -} diff --git a/src/main/java/io/github/adrianvic/itemeconomy/Config.java b/src/main/java/io/github/adrianvic/itemeconomy/Config.java new file mode 100644 index 0000000..2f2bb4f --- /dev/null +++ b/src/main/java/io/github/adrianvic/itemeconomy/Config.java @@ -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"); + } +} diff --git a/src/main/java/io/github/adrianvic/itemeconomy/Main.java b/src/main/java/io/github/adrianvic/itemeconomy/Main.java new file mode 100644 index 0000000..63b19f9 --- /dev/null +++ b/src/main/java/io/github/adrianvic/itemeconomy/Main.java @@ -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 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 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); + } + + } +} diff --git a/src/main/java/io/github/adrianvic/itemeconomy/UnrealConfig.java b/src/main/java/io/github/adrianvic/itemeconomy/UnrealConfig.java new file mode 100644 index 0000000..89cf663 --- /dev/null +++ b/src/main/java/io/github/adrianvic/itemeconomy/UnrealConfig.java @@ -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 { + 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 clone() { + return new HashMap(this); + } +} diff --git a/src/main/java/io/github/adrianvic/itemeconomy/VaultLayer.java b/src/main/java/io/github/adrianvic/itemeconomy/VaultLayer.java new file mode 100644 index 0000000..cc1260d --- /dev/null +++ b/src/main/java/io/github/adrianvic/itemeconomy/VaultLayer.java @@ -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 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; + } +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml new file mode 100644 index 0000000..f4834d2 --- /dev/null +++ b/src/main/resources/config.yml @@ -0,0 +1,4 @@ +item: "diamond" +singular: "diamond" +plural: "diamonds" +format: "{}$" \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..dbeb4ba --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,6 @@ +name: ItemEconomy +main: io.github.adrianvic.itemeconomy.Main +version: 0.1.0 +depend: +- Vault +api-version: '1.21' diff --git a/src/r1_21/resources/plugin.yml b/src/r1_21/resources/plugin.yml deleted file mode 100644 index 88b1ecd..0000000 --- a/src/r1_21/resources/plugin.yml +++ /dev/null @@ -1,7 +0,0 @@ -name: "Your Plugin" -version: ${version} -main: com.example.yourplugin -api-version: '1.21' -author: 'Your Name' -website: "https://example.com" -description: "Put a description here"