Initial commit

This commit is contained in:
Scifi9902
2026-10-12 13:57:05 -03:00
commit 4bde3b176a
2576 changed files with 212659 additions and 0 deletions
+25
View File
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>ProjectCranked</artifactId>
<groupId>net.crankedpvp</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>XPShop</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>net.crankedpvp</groupId>
<artifactId>Utils</artifactId>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,41 @@
package net.crankedpvp.xpshop;
import lombok.Getter;
import me.mattstudios.mf.base.CommandManager;
import net.crankedpvp.xpshop.commands.XPShopCommand;
import net.crankedpvp.xpshop.language.LanguageHandler;
import net.crankedpvp.xpshop.struct.XPShopManager;
import net.crankedpvp.xpshop.utils.ConfigManager;
import org.bukkit.plugin.java.JavaPlugin;
@Getter
public class XPShopPlugin extends JavaPlugin {
@Getter
private static XPShopPlugin instance;
private CommandManager commandManager;
private XPShopManager xpShopManager;
private ConfigManager configManager;
private LanguageHandler languageHandler;
@Override
public void onEnable() {
instance = this;
this.languageHandler = new LanguageHandler(this);
this.languageHandler.load();
this.configManager = new ConfigManager(this);
configManager.setupFiles();
this.xpShopManager = new XPShopManager(this);
this.commandManager = new CommandManager(this, false);
this.commandManager.register(new XPShopCommand());
}
@Override
public void onDisable() {
instance = null;
}
}
@@ -0,0 +1,75 @@
package net.crankedpvp.xpshop.commands;
import me.mattstudios.mf.annotations.Command;
import me.mattstudios.mf.annotations.Default;
import me.mattstudios.mf.annotations.Permission;
import me.mattstudios.mf.annotations.SubCommand;
import me.mattstudios.mf.base.CommandBase;
import net.crankedpvp.xpshop.XPShopPlugin;
import net.crankedpvp.xpshop.inventory.XPShopInventory;
import net.crankedpvp.xpshop.language.Language;
import org.bukkit.Material;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
@Command("xpshop")
public class XPShopCommand extends CommandBase {
@Default
public void onDefault(CommandSender sender) {
XPShopInventory xpShopInventory = new XPShopInventory((Player) sender);
xpShopInventory.getXpShopGui().open((HumanEntity) sender);
}
@SubCommand("reload")
@Permission("cranked.admin")
public void onReload(CommandSender sender) {
Language.ADMIN_RELOADING.send(sender);
long then = System.currentTimeMillis();
XPShopPlugin.getInstance().getLanguageHandler().load();
XPShopPlugin.getInstance().getXpShopManager().reload(XPShopPlugin.getInstance());
XPShopPlugin.getInstance().getConfigManager().reloadFiles();
long diff = System.currentTimeMillis() - then;
Language.ADMIN_RELOADED.send(sender, str -> str.replace("%took%", String.valueOf(diff)));
}
@SubCommand("additem")
@Permission("cranked.admin")
public void onAddItem(CommandSender sender, Integer price) {
Player player = ((Player)sender);
ItemStack item = player.getItemInHand().clone();
if (item == null || player.getItemInHand().getType() == Material.AIR) {
Language.EMPTY_HAND.send(player);
return;
}
XPShopPlugin.getInstance().getXpShopManager().getShopItems().put(item, price);
XPShopPlugin.getInstance().getXpShopManager().addItemStack(item, price);
Language.ADDED_ITEM_SHOP.send(player);
}
@SubCommand("removeitem")
@Permission("cranked.admin")
public void onRemoveItem(CommandSender sender) {
Player player = ((Player)sender);
ItemStack item = player.getItemInHand().clone();
if (item == null || item.getType() == Material.AIR) {
Language.EMPTY_HAND.send(player);
return;
}
if (!XPShopPlugin.getInstance().getXpShopManager().getShopItems().containsKey(item)) {
Language.NOT_IN_SHOP.send(player);
return;
}
XPShopPlugin.getInstance().getXpShopManager().getShopItems().remove(item);
XPShopPlugin.getInstance().getXpShopManager().deleteItemStack(item);
Language.REMOVED_ITEM_SHOP.send(player);
}
}
@@ -0,0 +1,98 @@
package net.crankedpvp.xpshop.inventory;
import dev.triumphteam.gui.guis.GuiItem;
import dev.triumphteam.gui.guis.PaginatedGui;
import lombok.Getter;
import net.crankedpvp.crankedutils.utils.GuiUtil;
import net.crankedpvp.crankedutils.utils.NumberUtils;
import net.crankedpvp.crankedutils.utils.XPUtils;
import net.crankedpvp.crankedutils.utils.items.ItemBuilder;
import net.crankedpvp.xpshop.XPShopPlugin;
import net.crankedpvp.xpshop.language.Language;
import net.crankedpvp.xpshop.struct.XPShopManager;
import net.crankedpvp.xpshop.utils.ConfigManager;
import org.bukkit.DyeColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
@Getter
public class XPShopInventory {
private final Player player;
private final PaginatedGui xpShopGui;
private final XPShopManager xpShopManager = XPShopPlugin.getInstance().getXpShopManager();
private final ConfigManager configManager = XPShopPlugin.getInstance().getConfigManager();
public XPShopInventory(Player player) {
this.player = player;
this.xpShopGui = new PaginatedGui(6, 28, "XP Shop Menu");
this.xpShopGui.setDefaultClickAction(event -> event.setCancelled(true));
this.xpShopGui.getFiller().fillBorder(new GuiItem(createFillerItem()));
this.xpShopGui.setItem(6, 5, new GuiItem(createUserInfoItem()));
this.xpShopGui.updateItem(6, 5, new GuiItem(createUserInfoItem()));
this.xpShopGui.setItem(6, 1, new GuiItem(GuiUtil.createPreviousItem(), event -> {
this.xpShopGui.previous();
}));
this.xpShopGui.setItem(6, 9, new GuiItem(GuiUtil.createNextItem(), event -> {
this.xpShopGui.next();
}));
List<Map.Entry<ItemStack, Integer>> entries = new ArrayList<>(xpShopManager.getShopItems().entrySet());
entries.sort(Comparator.comparing((itemStackIntegerEntry -> itemStackIntegerEntry.getKey().getType())));
for (Map.Entry<ItemStack, Integer> entry : entries) {
float price = entry.getValue();
ItemStack item = entry.getKey();
this.xpShopGui.addItem(new GuiItem(createXpShopItem(item.clone(), price), event -> {
XPShopPlugin.getInstance().getXpShopManager().purchaseItem(player, item, price);
this.xpShopGui.updateItem(6, 5, new GuiItem(createUserInfoItem()));
}));
}
}
public ItemStack createXpShopItem(ItemStack itemStack, double price) {
double discountedPrice;
if (player.hasPermission("satan.discount")) {
int discountPercent = this.configManager.getAbstractConfig(ConfigManager.ConfigFile.SETTINGS).getConfig().getInt("satan-discount");
discountedPrice = price - (price * discountPercent / 100);
} else {
discountedPrice = 0;
}
String loreLine = discountedPrice == 0 ? Language.PRICE_LINE.getMessage(str -> str.replace("{price}", NumberUtils.formatMoney(price))) : Language.DISCOUNTED_PRICE_LINE.getMessage(str -> str.replace("{price}", NumberUtils.formatMoney(price)).replace("{discounted-price}", NumberUtils.formatMoney(discountedPrice)));
return new ItemBuilder(itemStack.clone())
.addLore("")
.addLore(loreLine)
.colorize()
.build();
}
public ItemStack createUserInfoItem() {
return new ItemBuilder(Material.EXP_BOTTLE)
.setName("&a&l"+this.player.getName() + "'s EXP Info")
.addLore("")
.addLore("&2&lPlayer Info")
.addLore("&a&l * &fExperience&7: &a" + NumberUtils.formatMoney(XPUtils.getTotalExperience(this.player)) + " &2EXP")
.colorize()
.build();
}
public ItemStack createFillerItem() {
return new ItemBuilder(new ItemStack(Material.STAINED_GLASS_PANE, 1, DyeColor.GRAY.getData())).setName("&7").colorize().build();
}
}
@@ -0,0 +1,108 @@
package net.crankedpvp.xpshop.language;
import com.google.common.collect.ImmutableList;
import net.crankedpvp.crankedutils.utils.CC;
import org.apache.commons.lang3.StringUtils;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;
public enum Language {
BALANCE,
HELP,
NO_PERMISSION,
ADMIN_RELOADING,
ADMIN_RELOADED,
NOT_ENOUGH_EXP,
SUCCESSFUL_ITEM_PURCHASE,
EMPTY_HAND,
NOT_IN_SHOP,
REMOVED_ITEM_SHOP,
ADDED_ITEM_SHOP,
PRICE_LINE,
DISCOUNTED_PRICE_LINE,
;
private String value;
private ImmutableList<String> valueList;
public String getValue() {
return this.value != null ? this.value : this.name();
}
public String getValue(UnaryOperator<String> function) {
return this.value != null ? function.apply(this.value) : this.name();
}
public String getPath() {
return this.name();
}
public List<String> getValuesAsList() {
return this.valueList;
}
public List<String> getValuesAsList(String oldString, String newString) {
List<String> list = new ArrayList<>(this.valueList);
list.replaceAll(s -> s.replace(oldString, newString));
return list;
}
void setValue(String value) {
this.value = CC.translate(value);
List<String> tempList = Arrays.stream(StringUtils.split(value, System.lineSeparator()))
.map(CC::translate)
.collect(Collectors.toList());
this.valueList = ImmutableList.copyOf(tempList);
}
void setValue(List<String> valueList) {
this.value = CC.translate(valueList.parallelStream().collect(Collectors.joining(System.lineSeparator())));
List<String> tempList = valueList.stream()
.map(CC::translate)
.collect(Collectors.toList());
this.valueList = ImmutableList.copyOf(tempList);
}
public void send(CommandSender sender, UnaryOperator<String> function) {
sender.sendMessage(CC.translate(this.getValue(function)));
}
public void send(CommandSender sender) {
sender.sendMessage(CC.translate(this.getValue()));
}
public void send(List<CommandSender> senders, UnaryOperator<String> function) {
String msg = CC.translate(this.getValue(function));
senders.forEach(sender -> sender.sendMessage(msg));
}
public void sendPlayers(List<Player> senders, UnaryOperator<String> function) {
String msg = CC.translate(this.getValue(function));
senders.forEach(sender -> sender.sendMessage(msg));
}
public void send(CommandSender ...senders) {
String msg = CC.translate(this.getValue());
Arrays.asList(senders).forEach(sender -> sender.sendMessage(msg));
}
public String getMessage() {
return CC.translate(this.getValue());
}
public String getMessage(UnaryOperator<String> function) {
return CC.translate(this.getValue(function));
}
}
@@ -0,0 +1,82 @@
package net.crankedpvp.xpshop.language;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public final class LanguageHandler {
private final File file;
private final FileConfiguration config;
private final String path;
private final JavaPlugin plugin;
public LanguageHandler(JavaPlugin plugin) {
this.plugin = plugin;
this.path = "language.yml";
file = new File(plugin.getDataFolder(), path);
config = new YamlConfiguration();
}
public void load() {
this.saveDefaultConfig();
this.loadConfigFromDisk();
this.copyDefaults();
this.save();
this.loadConfigFromDisk();
this.loadLangMessages();
}
private void loadLangMessages() {
for (Language entry : Language.values()) {
String path = entry.getPath();
if (config.isList(path)) {
entry.setValue(config.getStringList(path));
continue;
}
entry.setValue(config.getString(path));
}
}
private void loadConfigFromDisk() {
try {
config.load(file);
} catch (IOException | InvalidConfigurationException e) {
e.printStackTrace();
}
}
public void save() {
try {
config.save(file);
} catch (IOException e) {
plugin.getLogger().severe("Could not save config file for " + file.getPath() + "!");
e.printStackTrace();
}
}
private void copyDefaults() {
InputStream defConfig = plugin.getResource(this.path);
if (defConfig != null) {
config.setDefaults(YamlConfiguration.loadConfiguration(new InputStreamReader(defConfig)));
}
config.options().copyDefaults(true);
}
private void saveDefaultConfig() {
if (!file.exists()) {
file.getParentFile().mkdirs();
plugin.saveResource(file.getName(),false);
}
}
}
@@ -0,0 +1,117 @@
package net.crankedpvp.xpshop.struct;
import lombok.Getter;
import lombok.Setter;
import net.crankedpvp.crankedutils.utils.*;
import net.crankedpvp.xpshop.language.Language;
import net.crankedpvp.xpshop.utils.ConfigManager;
import net.crankedpvp.crankedutils.utils.serializer.item.ItemStackSerializer;
import net.crankedpvp.xpshop.XPShopPlugin;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import java.util.HashMap;
import java.util.Map;
@Getter
@Setter
public class XPShopManager {
private Map<ItemStack, Integer> shopItems;
private final ConfigManager configManager;
public XPShopManager(XPShopPlugin plugin) {
this.shopItems = new HashMap<>();
this.configManager = plugin.getConfigManager();
load(plugin);
}
public int getPurchaseAbleItemsAmount(long balance) {
int amount = 0;
for (Integer price : shopItems.values()) {
if (balance >= price) {
amount++;
}
}
return amount;
}
public void reload(XPShopPlugin plugin) {
this.shopItems.clear();
plugin.getConfigManager().getAbstractConfig(ConfigManager.ConfigFile.SHOP).reloadConfig();
load(plugin);
}
public void load(XPShopPlugin plugin) {
ConfigManager configmanager = plugin.getConfigManager();
FileConfiguration config = configmanager.getConfig(ConfigManager.ConfigFile.SHOP);
ConfigurationSection configurationSection = config.getConfigurationSection("shop.items");
for (String key : configurationSection.getKeys(false)) {
int price = configurationSection.getInt(key);
ItemStack item = ItemStackSerializer.deserialize(key);
this.shopItems.put(item, price);
}
}
public void save(XPShopPlugin plugin) {
ConfigManager configmanager = plugin.getConfigManager();
FileConfiguration config = configmanager.getConfig(ConfigManager.ConfigFile.SHOP);
ConfigurationSection configurationSection = config.getConfigurationSection("shop.items");
for (Map.Entry<ItemStack, Integer> entry : this.shopItems.entrySet()) {
double price = entry.getValue();
ItemStack item = entry.getKey();
configurationSection.set(ItemStackSerializer.serialize(item), price);
}
XPShopPlugin.getInstance().getConfigManager().getAbstractConfig(ConfigManager.ConfigFile.SHOP).saveConfig();
}
public void deleteItemStack(ItemStack item) {
ConfigManager configmanager = XPShopPlugin.getInstance().getConfigManager();
FileConfiguration config = configmanager.getConfig(ConfigManager.ConfigFile.SHOP);
ConfigurationSection configurationSection = config.getConfigurationSection("shop.items");
configurationSection.set(ItemStackSerializer.serialize(item), null);
XPShopPlugin.getInstance().getConfigManager().getAbstractConfig(ConfigManager.ConfigFile.SHOP).saveConfig();
}
public void addItemStack(ItemStack item, double price) {
ConfigManager configmanager = XPShopPlugin.getInstance().getConfigManager();
FileConfiguration config = configmanager.getConfig(ConfigManager.ConfigFile.SHOP);
ConfigurationSection configurationSection = config.getConfigurationSection("shop.items");
configurationSection.set(ItemStackSerializer.serialize(item), price);
XPShopPlugin.getInstance().getConfigManager().getAbstractConfig(ConfigManager.ConfigFile.SHOP).saveConfig();
}
public void purchaseItem(Player player, ItemStack item, float price) {
if (player.hasPermission("satan.discount")) {
int discountPercent = this.configManager.getAbstractConfig(ConfigManager.ConfigFile.SETTINGS).getConfig().getInt("satan-discount");
price = price - (price * discountPercent / 100);
}
if (canAfford(player, price)) {
removeExp(player, price);
String message = Language.SUCCESSFUL_ITEM_PURCHASE.getMessage().replace("%price%", NumberUtils.formatMoney(price));
message = message.replace("%itemName%", item.getItemMeta().getDisplayName() == null ? StringUtils.getProperCase(item.getType().name()) : item.getItemMeta().getDisplayName());
player.sendMessage(message);
PlayerUtils.giveOrDropProtectedItem(player, item, 60);
} else {
double missing = price - player.getExp();
String message = Language.NOT_ENOUGH_EXP.getMessage().replace("%missing%", NumberUtils.formatMoney(missing));
message = message.replace("%itemName%", item.getItemMeta().getDisplayName() == null ? StringUtils.getProperCase(item.getType().name()) : item.getItemMeta().getDisplayName());
player.sendMessage(message);
}
}
public boolean canAfford(Player player, float price) {
return XPUtils.getTotalExperience(player) >= price;
}
public void removeExp(Player player, float price) {
XPUtils.removeExperience(player, (int) price);
}
}
@@ -0,0 +1,52 @@
package net.crankedpvp.xpshop.utils;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import net.crankedpvp.crankedutils.utils.config.AbstractConfig;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.HashMap;
import java.util.Map;
public class ConfigManager {
@Getter
private final Map<ConfigFile, AbstractConfig> configs = new HashMap<>();
public ConfigManager(JavaPlugin plugin) {
for (ConfigFile configFile : ConfigFile.values()) {
AbstractConfig abstractConfig = new AbstractConfig(plugin, configFile.getFileName());
configs.put(configFile, abstractConfig);
}
}
public void setupFiles() {
configs.values().forEach(AbstractConfig::setupConfigFile);
}
public void reloadFiles() {
configs.values().forEach(AbstractConfig::reloadConfig);
}
public AbstractConfig getAbstractConfig(ConfigFile configFile) {
return configs.get(configFile);
}
public FileConfiguration getConfig(ConfigFile configFile) {
return getAbstractConfig(configFile).getConfig();
}
@Getter
@RequiredArgsConstructor
public enum ConfigFile {
SHOP("shop.yml"),
SETTINGS("settings.yml"),
;
private final String fileName;
}
}
+15
View File
@@ -0,0 +1,15 @@
HELP:
- "&a&lXPShop Help"
- "&7Add help message here bro, don't be lazy"
NO_PERMISSION: "&cYou do not have permission to do this"
BALANCE: "&a&lXPShop &8➥ &7You have &a%balance%&7 &2EXP&7."
ADMIN_RELOADING: "&a&lXPShop &8➥ &7Reloading configs now..."
ADMIN_RELOADED: "&a&lXPShop &8➥ &7Reloaded configs and other cool stuff. Took &d%took%ms&7."
SUCCESSFUL_ITEM_PURCHASE: "&a&lXPShop &8➥ &7You have successfuly purchased %itemName%&7 for &a%price%&7 &2EXP&7."
NOT_ENOUGH_EXP: "&a&lXPShop &8➥ &7You need &a%missing%&7 EXP to purchase %itemName%"
EMPTY_HAND: "&cPlease hold the item you wish to add/remove to the shop"
NOT_IN_SHOP: "&cThis item is not in the EXP shop!"
REMOVED_ITEM_SHOP: "&cRemoved the item in your hand from the EXP shop!"
ADDED_ITEM_SHOP: "&cAdded the item in your hand to the EXP shop!"
PRICE_LINE: "&a&l* &fPrice: &a{price} EXP"
DISCOUNTED_PRICE_LINE: "&a&l* &fPrice: &c&m{price}&a {discounted-price} EXP &7(Satan Discount)"
+7
View File
@@ -0,0 +1,7 @@
name: CrankedXPShop
main: net.crankedpvp.xpshop.XPShopPlugin
version: ${git.total.commit.count}.0
description: "§fCommit ID: §a${git.commit.id.abbrev}"
website: https://crankedpvp.net/
depend:
- CrankedUtils
+1
View File
@@ -0,0 +1 @@
satan-discount: 15
+2
View File
@@ -0,0 +1,2 @@
shop:
items: {}