Configured build to use VaultUnlocked.

This commit is contained in:
天クマ 2025-12-31 13:15:08 -03:00
commit a5dd9f5060
12 changed files with 405 additions and 61 deletions

31
LICENSE
View file

@ -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 This program is distributed in the hope that it will be useful,
of this software and associated documentation files (the "Software"), to deal but WITHOUT ANY WARRANTY; without even the implied warranty of
in the Software without restriction, including without limitation the rights MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell GNU General Public License for more details.
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.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.

View file

@ -3,22 +3,20 @@ plugins {
id("xyz.jpenilla.run-paper") version "2.3.1" id("xyz.jpenilla.run-paper") version "2.3.1"
} }
group = "com.example" group = "io.github.adrianvic.itemeconomy"
version = System.getenv("YOURPLUGIN_VERSION_NAME") ?: "unknown" version = System.getenv("ITEMECO_VERSION_NAME") ?: "unknown"
repositories { repositories {
mavenCentral() mavenCentral()
maven("https://repo.papermc.io/repository/maven-public/") maven("https://repo.papermc.io/repository/maven-public/")
maven("https://repo.codemc.io/repository/creatorfromhell/")
} }
/* ----------------------------------------- */ /* ----------------------------------------- */
/* SUPPORTED VERSIONS */ /* SUPPORTED VERSIONS */
/* ----------------------------------------- */ /* ----------------------------------------- */
val mcVersions = listOf( val mcVersions = listOf<String>()
"b1_7_3",
"r1_21"
)
/* ----------------------------------------- */ /* ----------------------------------------- */
/* CREATE SOURCE SET PER VERSION */ /* CREATE SOURCE SET PER VERSION */
@ -59,9 +57,10 @@ mcVersions.forEach { ver ->
/* ----------------------------------------- */ /* ----------------------------------------- */
dependencies { dependencies {
add("compileOnly", "io.papermc.paper:paper-api:1.21.10-R0.1-SNAPSHOT") add("compileOnly", "io.papermc.paper:paper-api:1.21.11-R0.1-SNAPSHOT")
add("r1_21CompileOnly", "io.papermc.paper:paper-api:1.21.10-R0.1-SNAPSHOT") add("compileOnly", "net.milkbowl.vault:VaultUnlockedAPI:2.16")
add("b1_7_3CompileOnly", files("libs/craftbukkit-1060.jar")) // 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 { manifest {
attributes( attributes(
"yourplugin-Impl-Version" to ver, "itemeco-Impl-Version" to ver,
"yourplugin-Environment" to (System.getenv("YOURPLUGIN_BUILD_CHANNEL") ?: "dev") "itemeco-Environment" to (System.getenv("ITEMECO_BUILD_CHANNEL") ?: "dev")
) )
} }

View file

@ -1 +1 @@
rootProject.name = 'yourplugin' rootProject.name = 'ItemEconomy'

View file

@ -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"

View file

@ -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.");
}
}

View 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");
}
}

View 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);
}
}
}

View file

@ -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);
}
}

View 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;
}
}

View file

@ -0,0 +1,4 @@
item: "diamond"
singular: "diamond"
plural: "diamonds"
format: "{}$"

View file

@ -0,0 +1,6 @@
name: ItemEconomy
main: io.github.adrianvic.itemeconomy.Main
version: 0.1.0
depend:
- Vault
api-version: '1.21'

View file

@ -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"