Initial commit
This commit is contained in:
101
src/main/java/com/github/krzysiek944/mscrole/MSCROLE.java
Normal file
101
src/main/java/com/github/krzysiek944/mscrole/MSCROLE.java
Normal file
@@ -0,0 +1,101 @@
|
||||
package com.github.krzysiek944.mscrole;
|
||||
|
||||
import com.github.krzysiek944.mscrole.commands.MscroleAdminCommand;
|
||||
import com.github.krzysiek944.mscrole.commands.MscroleCommand;
|
||||
import com.github.krzysiek944.mscrole.listeners.GuiListener;
|
||||
import com.github.krzysiek944.mscrole.listeners.PlayerJoinListener;
|
||||
import com.github.krzysiek944.mscrole.services.CurrencyManager;
|
||||
import com.github.krzysiek944.mscrole.services.ShopManager;
|
||||
import com.github.krzysiek944.mscrole.utils.DataManager;
|
||||
import com.github.krzysiek944.mscrole.utils.ShopDataManager;
|
||||
import com.github.krzysiek944.mscrole.utils.MessageManager;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.bukkit.ChatColor;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
import java.io.File;
|
||||
|
||||
public class MSCROLE extends JavaPlugin {
|
||||
|
||||
private static MSCROLE instance;
|
||||
private CurrencyManager currencyManager;
|
||||
private DataManager dataManager;
|
||||
private ShopManager shopManager;
|
||||
private ShopDataManager shopDataManager;
|
||||
private MessageManager messageManager;
|
||||
private Logger logger = Logger.getLogger("Minecraft");
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
instance = this;
|
||||
saveDefaultConfig();
|
||||
|
||||
this.dataManager = new DataManager();
|
||||
this.dataManager.saveDefaultConfig();
|
||||
this.currencyManager = new CurrencyManager(dataManager);
|
||||
|
||||
this.shopDataManager = new ShopDataManager();
|
||||
this.shopDataManager.saveDefaultConfig();
|
||||
this.shopManager = new ShopManager(shopDataManager);
|
||||
|
||||
this.messageManager = new MessageManager(this);
|
||||
|
||||
getServer().getPluginManager().registerEvents(new PlayerJoinListener(this), this);
|
||||
getServer().getPluginManager().registerEvents(new GuiListener(), this);
|
||||
|
||||
getCommand("mscroll").setExecutor(new MscroleCommand());
|
||||
getCommand("mscrolladmin").setExecutor(new MscroleAdminCommand());
|
||||
|
||||
getLogger().info("Plugin MSCROLE został włączony!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
getLogger().info("Plugin MSCROLE został wyłączony!");
|
||||
}
|
||||
|
||||
public String getPrefix() {
|
||||
return ChatColor.translateAlternateColorCodes('&', getConfig().getString("prefix"));
|
||||
}
|
||||
|
||||
public static MSCROLE getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public CurrencyManager getCurrencyManager() {
|
||||
return currencyManager;
|
||||
}
|
||||
|
||||
public DataManager getDataManager() {
|
||||
return dataManager;
|
||||
}
|
||||
|
||||
public ShopManager getShopManager() {
|
||||
return shopManager;
|
||||
}
|
||||
|
||||
public ShopDataManager getShopDataManager() {
|
||||
return shopDataManager;
|
||||
}
|
||||
|
||||
public MessageManager getMessageManager() {
|
||||
return messageManager;
|
||||
}
|
||||
|
||||
public void logToFile(String message) {
|
||||
try {
|
||||
File dataFolder = getDataFolder();
|
||||
if (!dataFolder.exists()) {
|
||||
dataFolder.mkdir();
|
||||
}
|
||||
File logFile = new File(getDataFolder(), "logs/currency.log");
|
||||
if (!logFile.exists()) {
|
||||
logFile.getParentFile().mkdirs();
|
||||
logFile.createNewFile();
|
||||
}
|
||||
java.nio.file.Files.write(logFile.toPath(), (new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date()) + " " + message + "\n").getBytes(), java.nio.file.StandardOpenOption.APPEND);
|
||||
} catch (java.io.IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,222 @@
|
||||
package com.github.krzysiek944.mscrole.commands;
|
||||
|
||||
import com.github.krzysiek944.mscrole.MSCROLE;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class MscroleAdminCommand implements CommandExecutor {
|
||||
|
||||
private final MSCROLE plugin = MSCROLE.getInstance();
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
if (!sender.hasPermission(plugin.getConfig().getString("permissions.mscroleadmin"))) {
|
||||
sender.sendMessage(plugin.getPrefix() + plugin.getMessageManager().getMessage("no-permission"));
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args.length == 0) {
|
||||
sendHelp(sender);
|
||||
return true;
|
||||
}
|
||||
|
||||
String subCommand = args[0].toLowerCase();
|
||||
|
||||
switch (subCommand) {
|
||||
case "add":
|
||||
addItem(sender, args);
|
||||
break;
|
||||
case "remove":
|
||||
removeItem(sender, args);
|
||||
break;
|
||||
case "give":
|
||||
giveCurrency(sender, args);
|
||||
break;
|
||||
case "take":
|
||||
takeCurrency(sender, args);
|
||||
break;
|
||||
case "set":
|
||||
setCurrency(sender, args);
|
||||
break;
|
||||
case "list":
|
||||
listItems(sender);
|
||||
break;
|
||||
default:
|
||||
sender.sendMessage(plugin.getPrefix() + plugin.getMessageManager().getMessage("unknown-command"));
|
||||
break;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void sendHelp(CommandSender sender) {
|
||||
sender.sendMessage("");
|
||||
sender.sendMessage(plugin.getMessageManager().getMessage("admin-help-header"));
|
||||
sender.sendMessage(plugin.getMessageManager().getMessage("admin-help-add"));
|
||||
sender.sendMessage(plugin.getMessageManager().getMessage("admin-help-remove"));
|
||||
sender.sendMessage(plugin.getMessageManager().getMessage("admin-help-give"));
|
||||
sender.sendMessage(plugin.getMessageManager().getMessage("admin-help-take"));
|
||||
sender.sendMessage(plugin.getMessageManager().getMessage("admin-help-set"));
|
||||
sender.sendMessage(plugin.getMessageManager().getMessage("admin-help-list"));
|
||||
sender.sendMessage(plugin.getMessageManager().getMessage("admin-help-footer"));
|
||||
sender.sendMessage("");
|
||||
}
|
||||
|
||||
private void addItem(CommandSender sender, String[] args) {
|
||||
if (args.length < 5) {
|
||||
sender.sendMessage(plugin.getPrefix() + plugin.getMessageManager().getMessage("invalid-usage").replace("{usage}", "/mscrolladmin add <slot> <price> <item> <amount> <commands>"));
|
||||
return;
|
||||
}
|
||||
|
||||
int slot;
|
||||
int price;
|
||||
Material material;
|
||||
int amount = 1;
|
||||
List<String> commands = new ArrayList<>();
|
||||
|
||||
try {
|
||||
slot = Integer.parseInt(args[1]);
|
||||
price = Integer.parseInt(args[2]);
|
||||
material = Material.getMaterial(args[3].toUpperCase());
|
||||
amount = Integer.parseInt(args[4]);
|
||||
for (int i = 5; i < args.length; i++) {
|
||||
commands.add(args[i]);
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
sender.sendMessage(plugin.getPrefix() + plugin.getMessageManager().getMessage("invalid-amount"));
|
||||
return;
|
||||
}
|
||||
|
||||
if (material == null) {
|
||||
sender.sendMessage(plugin.getPrefix() + plugin.getMessageManager().getMessage("material-not-found").replace("{material}", args[3]));
|
||||
return;
|
||||
}
|
||||
|
||||
if (slot < 10 || slot > 43) {
|
||||
sender.sendMessage(plugin.getPrefix() + plugin.getMessageManager().getMessage("invalid-slot"));
|
||||
return;
|
||||
}
|
||||
|
||||
plugin.getShopManager().addItem(slot, price, commands, new ItemStack(material, amount));
|
||||
sender.sendMessage(plugin.getPrefix() + plugin.getMessageManager().getMessage("item-added").replace("{slot}", String.valueOf(slot)).replace("{price}", String.valueOf(price)));
|
||||
}
|
||||
|
||||
private void removeItem(CommandSender sender, String[] args) {
|
||||
if (args.length < 2) {
|
||||
sender.sendMessage(plugin.getPrefix() + plugin.getMessageManager().getMessage("invalid-usage").replace("{usage}", "/mscrolladmin remove <slot>"));
|
||||
return;
|
||||
}
|
||||
|
||||
int slot;
|
||||
try {
|
||||
slot = Integer.parseInt(args[1]);
|
||||
} catch (NumberFormatException e) {
|
||||
sender.sendMessage(plugin.getPrefix() + plugin.getMessageManager().getMessage("invalid-slot"));
|
||||
return;
|
||||
}
|
||||
|
||||
plugin.getShopManager().removeItem(slot);
|
||||
sender.sendMessage(plugin.getPrefix() + plugin.getMessageManager().getMessage("item-removed").replace("{slot}", String.valueOf(slot)));
|
||||
}
|
||||
|
||||
private void giveCurrency(CommandSender sender, String[] args) {
|
||||
if (args.length < 3) {
|
||||
sender.sendMessage(plugin.getPrefix() + plugin.getMessageManager().getMessage("invalid-usage").replace("{usage}", "/mscrolladmin give <player> <amount>"));
|
||||
return;
|
||||
}
|
||||
|
||||
Player target = Bukkit.getPlayer(args[1]);
|
||||
if (target == null) {
|
||||
sender.sendMessage(plugin.getPrefix() + plugin.getMessageManager().getMessage("player-not-found"));
|
||||
return;
|
||||
}
|
||||
|
||||
int amount;
|
||||
try {
|
||||
amount = Integer.parseInt(args[2]);
|
||||
} catch (NumberFormatException e) {
|
||||
sender.sendMessage(plugin.getPrefix() + plugin.getMessageManager().getMessage("invalid-amount"));
|
||||
return;
|
||||
}
|
||||
|
||||
plugin.getCurrencyManager().addBalance(target, amount);
|
||||
sender.sendMessage(plugin.getPrefix() + plugin.getMessageManager().getMessage("currency-given").replace("{amount}", String.valueOf(amount)).replace("{receiver}", target.getName()));
|
||||
target.sendMessage(plugin.getPrefix() + plugin.getMessageManager().getMessage("currency-received-from-admin").replace("{amount}", String.valueOf(amount)));
|
||||
}
|
||||
|
||||
private void takeCurrency(CommandSender sender, String[] args) {
|
||||
if (args.length < 3) {
|
||||
sender.sendMessage(plugin.getPrefix() + plugin.getMessageManager().getMessage("invalid-usage").replace("{usage}", "/mscrolladmin take <player> <amount>"));
|
||||
return;
|
||||
}
|
||||
|
||||
Player target = Bukkit.getPlayer(args[1]);
|
||||
if (target == null) {
|
||||
sender.sendMessage(plugin.getPrefix() + plugin.getMessageManager().getMessage("player-not-found"));
|
||||
return;
|
||||
}
|
||||
|
||||
int amount;
|
||||
try {
|
||||
amount = Integer.parseInt(args[2]);
|
||||
} catch (NumberFormatException e) {
|
||||
sender.sendMessage(plugin.getPrefix() + plugin.getMessageManager().getMessage("invalid-amount"));
|
||||
return;
|
||||
}
|
||||
|
||||
plugin.getCurrencyManager().subtractBalance(target, amount);
|
||||
sender.sendMessage(plugin.getPrefix() + plugin.getMessageManager().getMessage("currency-taken").replace("{amount}", String.valueOf(amount)).replace("{player}", target.getName()));
|
||||
target.sendMessage(plugin.getPrefix() + plugin.getMessageManager().getMessage("currency-taken-by-admin").replace("{amount}", String.valueOf(amount)));
|
||||
}
|
||||
|
||||
private void setCurrency(CommandSender sender, String[] args) {
|
||||
if (args.length < 3) {
|
||||
sender.sendMessage(plugin.getPrefix() + plugin.getMessageManager().getMessage("invalid-usage").replace("{usage}", "/mscrolladmin set <player> <amount>"));
|
||||
return;
|
||||
}
|
||||
|
||||
Player target = Bukkit.getPlayer(args[1]);
|
||||
if (target == null) {
|
||||
sender.sendMessage(plugin.getPrefix() + plugin.getMessageManager().getMessage("player-not-found"));
|
||||
return;
|
||||
}
|
||||
|
||||
int amount;
|
||||
try {
|
||||
amount = Integer.parseInt(args[2]);
|
||||
} catch (NumberFormatException e) {
|
||||
sender.sendMessage(plugin.getPrefix() + plugin.getMessageManager().getMessage("invalid-amount"));
|
||||
return;
|
||||
}
|
||||
|
||||
plugin.getCurrencyManager().setBalance(target, amount);
|
||||
sender.sendMessage(plugin.getPrefix() + plugin.getMessageManager().getMessage("balance-set").replace("{player}", target.getName()).replace("{amount}", String.valueOf(amount)));
|
||||
}
|
||||
|
||||
private void listItems(CommandSender sender) {
|
||||
sender.sendMessage("");
|
||||
sender.sendMessage(plugin.getMessageManager().getMessage("list-items-header"));
|
||||
ConfigurationSection itemsSection = plugin.getShopManager().getItems();
|
||||
if (itemsSection == null || itemsSection.getKeys(false).isEmpty()) {
|
||||
sender.sendMessage(plugin.getMessageManager().getMessage("shop-empty"));
|
||||
} else {
|
||||
for (String key : itemsSection.getKeys(false)) {
|
||||
int slot = Integer.parseInt(key);
|
||||
int price = itemsSection.getInt(key + ".price");
|
||||
ItemStack item = itemsSection.getItemStack(key + ".item");
|
||||
sender.sendMessage(plugin.getMessageManager().getMessage("list-items-format").replace("{slot}", String.valueOf(slot)).replace("{item}", item.getType().toString()).replace("{price}", String.valueOf(price)).replace("{currency}", plugin.getConfig().getString("currency-name")));
|
||||
}
|
||||
}
|
||||
sender.sendMessage(plugin.getMessageManager().getMessage("list-items-footer"));
|
||||
sender.sendMessage("");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
package com.github.krzysiek944.mscrole.commands;
|
||||
|
||||
import com.github.krzysiek944.mscrole.MSCROLE;
|
||||
import com.github.krzysiek944.mscrole.gui.AdminGUI;
|
||||
import com.github.krzysiek944.mscrole.gui.ShopGUI;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class MscroleCommand implements CommandExecutor {
|
||||
|
||||
private final MSCROLE plugin = MSCROLE.getInstance();
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
if (!(sender instanceof Player)) {
|
||||
sender.sendMessage(plugin.getMessageManager().getMessage("only-player"));
|
||||
return true;
|
||||
}
|
||||
|
||||
Player player = (Player) sender;
|
||||
|
||||
if (!player.hasPermission(plugin.getConfig().getString("permissions.mscrole"))) {
|
||||
player.sendMessage(plugin.getPrefix() + plugin.getMessageManager().getMessage("no-permission"));
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args.length == 0) {
|
||||
sendHelp(player);
|
||||
return true;
|
||||
}
|
||||
|
||||
String subCommand = args[0].toLowerCase();
|
||||
|
||||
switch (subCommand) {
|
||||
case "balans":
|
||||
case "balance":
|
||||
case "bal":
|
||||
showBalance(player);
|
||||
break;
|
||||
case "wyslij":
|
||||
case "send":
|
||||
case "pay":
|
||||
sendMoney(player, args);
|
||||
break;
|
||||
|
||||
case "sklep":
|
||||
case "shop":
|
||||
new ShopGUI().openShop(player);
|
||||
break;
|
||||
|
||||
case "admin":
|
||||
if (player.hasPermission(plugin.getConfig().getString("permissions.mscroleadmin"))) {
|
||||
new AdminGUI().openAdminPanel(player);
|
||||
} else {
|
||||
player.sendMessage(plugin.getPrefix() + plugin.getMessageManager().getMessage("no-permission"));
|
||||
}
|
||||
break;
|
||||
default:
|
||||
player.sendMessage(plugin.getPrefix() + plugin.getMessageManager().getMessage("unknown-command"));
|
||||
break;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void sendHelp(Player player) {
|
||||
player.sendMessage("");
|
||||
player.sendMessage(plugin.getMessageManager().getMessage("help-header"));
|
||||
player.sendMessage(plugin.getMessageManager().getMessage("help-balance"));
|
||||
player.sendMessage(plugin.getMessageManager().getMessage("help-send"));
|
||||
player.sendMessage(plugin.getMessageManager().getMessage("help-shop"));
|
||||
player.sendMessage(plugin.getMessageManager().getMessage("help-admin"));
|
||||
player.sendMessage(plugin.getMessageManager().getMessage("help-footer"));
|
||||
player.sendMessage("");
|
||||
}
|
||||
|
||||
private void showBalance(Player player) {
|
||||
int balance = plugin.getCurrencyManager().getBalance(player);
|
||||
player.sendMessage(plugin.getPrefix() + plugin.getMessageManager().getMessage("currency-check").replace("{amount}", String.valueOf(balance)));
|
||||
}
|
||||
|
||||
private void sendMoney(Player player, String[] args) {
|
||||
if (args.length < 3) {
|
||||
player.sendMessage(plugin.getPrefix() + plugin.getMessageManager().getMessage("invalid-usage").replace("{usage}", "/mscroll send <player> <amount>"));
|
||||
return;
|
||||
}
|
||||
|
||||
Player target = Bukkit.getPlayer(args[1]);
|
||||
if (target == null) {
|
||||
player.sendMessage(plugin.getPrefix() + plugin.getMessageManager().getMessage("player-not-found"));
|
||||
return;
|
||||
}
|
||||
|
||||
if (target == player) {
|
||||
player.sendMessage(plugin.getPrefix() + plugin.getMessageManager().getMessage("cannot-send-to-self"));
|
||||
return;
|
||||
}
|
||||
|
||||
int amount;
|
||||
try {
|
||||
amount = Integer.parseInt(args[2]);
|
||||
} catch (NumberFormatException e) {
|
||||
player.sendMessage(plugin.getPrefix() + plugin.getMessageManager().getMessage("invalid-amount"));
|
||||
return;
|
||||
}
|
||||
|
||||
if (amount <= 0) {
|
||||
player.sendMessage(plugin.getPrefix() + plugin.getMessageManager().getMessage("amount-must-be-positive"));
|
||||
return;
|
||||
}
|
||||
|
||||
int playerBalance = plugin.getCurrencyManager().getBalance(player);
|
||||
if (playerBalance < amount) {
|
||||
player.sendMessage(plugin.getPrefix() + plugin.getMessageManager().getMessage("not-enough-currency").replace("{amount}", String.valueOf(playerBalance)));
|
||||
return;
|
||||
}
|
||||
|
||||
plugin.getCurrencyManager().subtractBalance(player, amount);
|
||||
plugin.getCurrencyManager().addBalance(target, amount);
|
||||
|
||||
player.sendMessage(plugin.getPrefix() + plugin.getMessageManager().getMessage("currency-sent").replace("{amount}", String.valueOf(amount)).replace("{receiver}", target.getName()));
|
||||
target.sendMessage(plugin.getPrefix() + plugin.getMessageManager().getMessage("currency-received").replace("{amount}", String.valueOf(amount)).replace("{sender}", player.getName()));
|
||||
|
||||
plugin.logToFile("[Currency] " + player.getName() + " sent " + amount + " to " + target.getName());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package com.github.krzysiek944.mscrole.gui;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class AdminGUI {
|
||||
|
||||
public void openAdminPanel(Player player) {
|
||||
Inventory gui = Bukkit.createInventory(null, 27, "§c§lPanel Administratora");
|
||||
|
||||
// Frame
|
||||
ItemStack frame = new ItemStack(Material.RED_STAINED_GLASS_PANE);
|
||||
ItemMeta frameMeta = frame.getItemMeta();
|
||||
frameMeta.setDisplayName(" ");
|
||||
frame.setItemMeta(frameMeta);
|
||||
for (int i = 0; i < 27; i++) {
|
||||
if (i < 9 || i > 17) {
|
||||
gui.setItem(i, frame);
|
||||
}
|
||||
}
|
||||
|
||||
// Options
|
||||
ItemStack addItem = new ItemStack(Material.EMERALD);
|
||||
ItemMeta addItemMeta = addItem.getItemMeta();
|
||||
addItemMeta.setDisplayName("§a§lDodaj Przedmiot");
|
||||
List<String> addItemLore = new ArrayList<>();
|
||||
addItemLore.add("§7Kliknij aby dodać");
|
||||
addItemLore.add("§7przedmiot do sklepu");
|
||||
addItemMeta.setLore(addItemLore);
|
||||
addItem.setItemMeta(addItemMeta);
|
||||
gui.setItem(10, addItem);
|
||||
|
||||
ItemStack giveCurrency = new ItemStack(Material.DIAMOND);
|
||||
ItemMeta giveCurrencyMeta = giveCurrency.getItemMeta();
|
||||
giveCurrencyMeta.setDisplayName("§b§lDaj Walutę");
|
||||
List<String> giveCurrencyLore = new ArrayList<>();
|
||||
giveCurrencyLore.add("§7Kliknij aby dać");
|
||||
giveCurrencyLore.add("§7graczowi MSCROLE");
|
||||
giveCurrencyMeta.setLore(giveCurrencyLore);
|
||||
giveCurrency.setItemMeta(giveCurrencyMeta);
|
||||
gui.setItem(12, giveCurrency);
|
||||
|
||||
ItemStack takeCurrency = new ItemStack(Material.GOLD_INGOT);
|
||||
ItemMeta takeCurrencyMeta = takeCurrency.getItemMeta();
|
||||
takeCurrencyMeta.setDisplayName("§e§lZabierz Walutę");
|
||||
List<String> takeCurrencyLore = new ArrayList<>();
|
||||
takeCurrencyLore.add("§7Kliknij aby zabrać");
|
||||
takeCurrencyLore.add("§7graczowi MSCROLE");
|
||||
takeCurrencyMeta.setLore(takeCurrencyLore);
|
||||
takeCurrency.setItemMeta(takeCurrencyMeta);
|
||||
gui.setItem(14, takeCurrency);
|
||||
|
||||
ItemStack listItems = new ItemStack(Material.PAPER);
|
||||
ItemMeta listItemsMeta = listItems.getItemMeta();
|
||||
listItemsMeta.setDisplayName("§f§lLista Przedmiotów");
|
||||
List<String> listItemsLore = new ArrayList<>();
|
||||
listItemsLore.add("§7Kliknij aby zobaczyć");
|
||||
listItemsLore.add("§7wszystkie przedmioty w sklepie");
|
||||
listItemsMeta.setLore(listItemsLore);
|
||||
listItems.setItemMeta(listItemsMeta);
|
||||
gui.setItem(16, listItems);
|
||||
|
||||
ItemStack closeButton = new ItemStack(Material.BARRIER);
|
||||
ItemMeta closeMeta = closeButton.getItemMeta();
|
||||
closeMeta.setDisplayName("§c§lZamknij");
|
||||
closeMeta.setLore(new ArrayList<>());
|
||||
closeButton.setItemMeta(closeMeta);
|
||||
gui.setItem(22, closeButton);
|
||||
|
||||
player.openInventory(gui);
|
||||
}
|
||||
}
|
||||
133
src/main/java/com/github/krzysiek944/mscrole/gui/ShopGUI.java
Normal file
133
src/main/java/com/github/krzysiek944/mscrole/gui/ShopGUI.java
Normal file
@@ -0,0 +1,133 @@
|
||||
package com.github.krzysiek944.mscrole.gui;
|
||||
|
||||
import com.github.krzysiek944.mscrole.MSCROLE;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.inventory.meta.SkullMeta;
|
||||
import org.bukkit.ChatColor;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ShopGUI {
|
||||
|
||||
private final MSCROLE plugin = MSCROLE.getInstance();
|
||||
|
||||
public void openShop(Player player) {
|
||||
Inventory gui = Bukkit.createInventory(null, 54, "§6§lSklep MSCROLE");
|
||||
|
||||
// Decorative frame
|
||||
ItemStack frame = new ItemStack(Material.GRAY_STAINED_GLASS_PANE);
|
||||
ItemMeta frameMeta = frame.getItemMeta();
|
||||
frameMeta.setDisplayName(" ");
|
||||
frame.setItemMeta(frameMeta);
|
||||
|
||||
for (int i : new int[]{0, 1, 2, 3, 5, 6, 7, 8, 45, 46, 47, 48, 50, 51, 52, 53}) {
|
||||
gui.setItem(i, frame);
|
||||
}
|
||||
|
||||
// Balance info
|
||||
ItemStack balanceItem = new ItemStack(Material.PLAYER_HEAD);
|
||||
SkullMeta balanceMeta = (SkullMeta) balanceItem.getItemMeta();
|
||||
balanceMeta.setOwningPlayer(player);
|
||||
balanceMeta.setDisplayName("§e§lTwój Balans");
|
||||
List<String> balanceLore = new ArrayList<>();
|
||||
balanceLore.add("§7Posiadasz: §a" + (int) plugin.getCurrencyManager().getBalance(player) + " MSCROLE");
|
||||
balanceLore.add("");
|
||||
balanceLore.add("§7Kliknij przedmioty poniżej");
|
||||
balanceLore.add("§7aby je kupić!");
|
||||
balanceMeta.setLore(balanceLore);
|
||||
balanceItem.setItemMeta(balanceMeta);
|
||||
gui.setItem(4, balanceItem);
|
||||
|
||||
// Close button
|
||||
ItemStack closeButton = new ItemStack(Material.BARRIER);
|
||||
ItemMeta closeMeta = closeButton.getItemMeta();
|
||||
closeMeta.setDisplayName("§c§lZamknij");
|
||||
List<String> closeLore = new ArrayList<>();
|
||||
closeLore.add("§7Kliknij aby zamknąć sklep");
|
||||
closeMeta.setLore(closeLore);
|
||||
closeButton.setItemMeta(closeMeta);
|
||||
gui.setItem(49, closeButton);
|
||||
|
||||
// Load items
|
||||
ConfigurationSection itemsSection = plugin.getShopManager().getItems();
|
||||
if (itemsSection == null) {
|
||||
ItemStack empty = new ItemStack(Material.BARRIER);
|
||||
ItemMeta emptyMeta = empty.getItemMeta();
|
||||
emptyMeta.setDisplayName("§c§lSklep jest pusty!");
|
||||
List<String> emptyLore = new ArrayList<>();
|
||||
emptyLore.add("§7Administrator nie dodał");
|
||||
emptyLore.add("§7jeszcze żadnych przedmiotów!");
|
||||
emptyMeta.setLore(emptyLore);
|
||||
empty.setItemMeta(emptyMeta);
|
||||
gui.setItem(22, empty);
|
||||
} else {
|
||||
for (String key : itemsSection.getKeys(false)) {
|
||||
int slot = Integer.parseInt(key);
|
||||
ItemStack item = null;
|
||||
ConfigurationSection itemSection = itemsSection.getConfigurationSection(key + ".item");
|
||||
|
||||
if (itemSection != null && itemSection.isString("material")) {
|
||||
// New format
|
||||
String materialName = itemSection.getString("material").toUpperCase();
|
||||
Material material = Material.getMaterial(materialName);
|
||||
if (material == null) {
|
||||
plugin.getLogger().warning("Invalid material in shop.yml: " + materialName + " for slot " + key);
|
||||
continue;
|
||||
}
|
||||
int amount = itemSection.getInt("amount", 1);
|
||||
item = new ItemStack(material, amount);
|
||||
ItemMeta itemMeta = item.getItemMeta();
|
||||
if (itemMeta != null) {
|
||||
if (itemSection.isString("name")) {
|
||||
itemMeta.setDisplayName(ChatColor.translateAlternateColorCodes('&', itemSection.getString("name")));
|
||||
}
|
||||
if (itemSection.isList("lore")) {
|
||||
List<String> newLore = new ArrayList<>();
|
||||
for (String line : itemSection.getStringList("lore")) {
|
||||
newLore.add(ChatColor.translateAlternateColorCodes('&', line));
|
||||
}
|
||||
itemMeta.setLore(newLore);
|
||||
}
|
||||
item.setItemMeta(itemMeta);
|
||||
}
|
||||
} else {
|
||||
// Old format
|
||||
item = itemsSection.getItemStack(key + ".item");
|
||||
}
|
||||
|
||||
if (item == null) {
|
||||
plugin.getLogger().warning("Could not load item in shop.yml for slot " + key);
|
||||
continue;
|
||||
}
|
||||
|
||||
int price = itemsSection.getInt(key + ".price");
|
||||
|
||||
ItemMeta itemMeta = item.getItemMeta();
|
||||
List<String> lore;
|
||||
if (itemMeta.hasLore()) {
|
||||
lore = itemMeta.getLore();
|
||||
} else {
|
||||
lore = new ArrayList<>();
|
||||
}
|
||||
lore.add("");
|
||||
lore.add("§7Cena: §e" + price + " MSCROLE");
|
||||
lore.add("");
|
||||
lore.add("§eKliknij aby kupić!");
|
||||
itemMeta.setLore(lore);
|
||||
item.setItemMeta(itemMeta);
|
||||
|
||||
gui.setItem(slot, item);
|
||||
}
|
||||
}
|
||||
|
||||
player.openInventory(gui);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package com.github.krzysiek944.mscrole.listeners;
|
||||
|
||||
import com.github.krzysiek944.mscrole.MSCROLE;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.Sound;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class GuiListener implements Listener {
|
||||
|
||||
private final MSCROLE plugin = MSCROLE.getInstance();
|
||||
|
||||
@EventHandler
|
||||
public void onInventoryClick(InventoryClickEvent event) {
|
||||
if (event.getView().getTitle().equals("§6§lSklep MSCROLE")) {
|
||||
event.setCancelled(true);
|
||||
Player player = (Player) event.getWhoClicked();
|
||||
ItemStack clickedItem = event.getCurrentItem();
|
||||
|
||||
if (clickedItem == null || clickedItem.getType() == Material.AIR) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (clickedItem.getType() == Material.BARRIER) {
|
||||
player.closeInventory();
|
||||
return;
|
||||
}
|
||||
|
||||
int slot = event.getSlot();
|
||||
int price = plugin.getShopManager().getItems().getInt(slot + ".price");
|
||||
List<String> commands = plugin.getShopManager().getItems().getStringList(slot + ".commands");
|
||||
|
||||
if (price > 0) {
|
||||
int balance = plugin.getCurrencyManager().getBalance(player);
|
||||
if (balance >= price) {
|
||||
plugin.getCurrencyManager().subtractBalance(player, price);
|
||||
for (String command : commands) {
|
||||
plugin.getServer().dispatchCommand(plugin.getServer().getConsoleSender(), command.replace("%player%", player.getName()));
|
||||
}
|
||||
player.sendMessage(plugin.getPrefix() + plugin.getMessageManager().getMessage("item-purchased").replace("{price}", String.valueOf(price)).replace("{currency}", plugin.getConfig().getString("currency-name")));
|
||||
player.playSound(player.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 1, 1);
|
||||
} else {
|
||||
player.sendMessage(plugin.getPrefix() + plugin.getMessageManager().getMessage("not-enough-currency-shop").replace("{currency}", plugin.getConfig().getString("currency-name")).replace("{price}", String.valueOf(price)));
|
||||
player.playSound(player.getLocation(), Sound.ENTITY_VILLAGER_NO, 1, 1);
|
||||
}
|
||||
}
|
||||
} else if (event.getView().getTitle().equals("§c§lPanel Administratora")) {
|
||||
event.setCancelled(true);
|
||||
Player player = (Player) event.getWhoClicked();
|
||||
ItemStack clickedItem = event.getCurrentItem();
|
||||
|
||||
if (clickedItem == null || clickedItem.getType() == Material.AIR) {
|
||||
return;
|
||||
}
|
||||
|
||||
player.closeInventory();
|
||||
|
||||
switch (clickedItem.getType()) {
|
||||
case EMERALD:
|
||||
player.sendMessage("");
|
||||
player.sendMessage(plugin.getPrefix() + plugin.getMessageManager().getMessage("admin-add-item-help-header"));
|
||||
player.sendMessage(plugin.getMessageManager().getMessage("admin-add-item-help-command"));
|
||||
player.sendMessage("");
|
||||
player.sendMessage(plugin.getMessageManager().getMessage("admin-add-item-help-example-command"));
|
||||
player.sendMessage(plugin.getMessageManager().getMessage("admin-add-item-help-example-description"));
|
||||
player.sendMessage("");
|
||||
break;
|
||||
case DIAMOND:
|
||||
player.sendMessage("");
|
||||
player.sendMessage(plugin.getPrefix() + plugin.getMessageManager().getMessage("admin-give-currency-help-header"));
|
||||
player.sendMessage(plugin.getMessageManager().getMessage("admin-give-currency-help-command"));
|
||||
player.sendMessage("");
|
||||
break;
|
||||
case GOLD_INGOT:
|
||||
player.sendMessage("");
|
||||
player.sendMessage(plugin.getPrefix() + plugin.getMessageManager().getMessage("admin-take-currency-help-header"));
|
||||
player.sendMessage(plugin.getMessageManager().getMessage("admin-take-currency-help-command"));
|
||||
player.sendMessage("");
|
||||
break;
|
||||
case PAPER:
|
||||
player.performCommand("mscrolladmin list");
|
||||
break;
|
||||
case BARRIER:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.github.krzysiek944.mscrole.listeners;
|
||||
|
||||
import com.github.krzysiek944.mscrole.MSCROLE;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
|
||||
public class PlayerJoinListener implements Listener {
|
||||
|
||||
private final MSCROLE plugin;
|
||||
|
||||
public PlayerJoinListener(MSCROLE plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerJoin(PlayerJoinEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
if (!player.hasPlayedBefore()) {
|
||||
int startingBalance = plugin.getConfig().getInt("starting-balance", 1000);
|
||||
plugin.getCurrencyManager().setBalance(player, startingBalance);
|
||||
player.sendMessage(plugin.getPrefix() + plugin.getMessageManager().getMessage("player-join").replace("{player}", player.getName()).replace("{amount}", String.valueOf(startingBalance)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.github.krzysiek944.mscrole.services;
|
||||
|
||||
import com.github.krzysiek944.mscrole.MSCROLE;
|
||||
import com.github.krzysiek944.mscrole.utils.DataManager;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class CurrencyManager {
|
||||
|
||||
private final MSCROLE plugin = MSCROLE.getInstance();
|
||||
private final DataManager dataManager;
|
||||
|
||||
public CurrencyManager(DataManager dataManager) {
|
||||
this.dataManager = dataManager;
|
||||
}
|
||||
|
||||
public int getBalance(Player player) {
|
||||
return getBalance(player.getUniqueId());
|
||||
}
|
||||
|
||||
public int getBalance(UUID uuid) {
|
||||
return dataManager.getConfig().getInt("balances." + uuid.toString(), 0);
|
||||
}
|
||||
|
||||
public void setBalance(Player player, int amount) {
|
||||
setBalance(player.getUniqueId(), amount);
|
||||
}
|
||||
|
||||
public void setBalance(UUID uuid, int amount) {
|
||||
dataManager.getConfig().set("balances." + uuid.toString(), amount);
|
||||
dataManager.saveConfig();
|
||||
plugin.logToFile("[Currency] Set balance of " + uuid.toString() + " to " + amount);
|
||||
}
|
||||
|
||||
public void addBalance(Player player, int amount) {
|
||||
addBalance(player.getUniqueId(), amount);
|
||||
}
|
||||
|
||||
public void addBalance(UUID uuid, int amount) {
|
||||
setBalance(uuid, getBalance(uuid) + amount);
|
||||
plugin.logToFile("[Currency] Added " + amount + " to " + uuid.toString());
|
||||
}
|
||||
|
||||
public void subtractBalance(Player player, int amount) {
|
||||
subtractBalance(player.getUniqueId(), amount);
|
||||
}
|
||||
|
||||
public void subtractBalance(UUID uuid, int amount) {
|
||||
setBalance(uuid, getBalance(uuid) - amount);
|
||||
plugin.logToFile("[Currency] Subtracted " + amount + " from " + uuid.toString());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.github.krzysiek944.mscrole.services;
|
||||
|
||||
import com.github.krzysiek944.mscrole.MSCROLE;
|
||||
import com.github.krzysiek944.mscrole.utils.ShopDataManager;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ShopManager {
|
||||
|
||||
private final MSCROLE plugin = MSCROLE.getInstance();
|
||||
private final ShopDataManager shopDataManager;
|
||||
|
||||
public ShopManager(ShopDataManager shopDataManager) {
|
||||
this.shopDataManager = shopDataManager;
|
||||
}
|
||||
|
||||
public void addItem(int slot, int price, List<String> commands, ItemStack item) {
|
||||
String path = "items." + slot;
|
||||
shopDataManager.getConfig().set(path + ".price", price);
|
||||
shopDataManager.getConfig().set(path + ".commands", commands);
|
||||
shopDataManager.getConfig().set(path + ".item", item);
|
||||
shopDataManager.saveConfig();
|
||||
}
|
||||
|
||||
public void removeItem(int slot) {
|
||||
shopDataManager.getConfig().set("items." + slot, null);
|
||||
shopDataManager.saveConfig();
|
||||
}
|
||||
|
||||
public ConfigurationSection getItems() {
|
||||
return shopDataManager.getConfig().getConfigurationSection("items");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.github.krzysiek944.mscrole.utils;
|
||||
|
||||
import com.github.krzysiek944.mscrole.MSCROLE;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class DataManager {
|
||||
|
||||
private final MSCROLE plugin = MSCROLE.getInstance();
|
||||
private FileConfiguration dataConfig = null;
|
||||
private File configFile = null;
|
||||
|
||||
public void reloadConfig() {
|
||||
if (configFile == null) {
|
||||
configFile = new File(plugin.getDataFolder(), "data.yml");
|
||||
}
|
||||
dataConfig = YamlConfiguration.loadConfiguration(configFile);
|
||||
}
|
||||
|
||||
public FileConfiguration getConfig() {
|
||||
if (dataConfig == null) {
|
||||
reloadConfig();
|
||||
}
|
||||
return dataConfig;
|
||||
}
|
||||
|
||||
public void saveConfig() {
|
||||
if (dataConfig == null || configFile == null) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
getConfig().save(configFile);
|
||||
} catch (IOException e) {
|
||||
plugin.getLogger().severe("Could not save config to " + configFile);
|
||||
}
|
||||
}
|
||||
|
||||
public void saveDefaultConfig() {
|
||||
if (configFile == null) {
|
||||
configFile = new File(plugin.getDataFolder(), "data.yml");
|
||||
}
|
||||
if (!configFile.exists()) {
|
||||
plugin.saveResource("data.yml", false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.github.krzysiek944.mscrole.utils;
|
||||
|
||||
import com.github.krzysiek944.mscrole.MSCROLE;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class MessageManager {
|
||||
|
||||
private final MSCROLE plugin;
|
||||
private FileConfiguration messageConfig;
|
||||
|
||||
public MessageManager(MSCROLE plugin) {
|
||||
this.plugin = plugin;
|
||||
setupMessages();
|
||||
}
|
||||
|
||||
private void setupMessages() {
|
||||
File messageFile = new File(plugin.getDataFolder(), "message.yml");
|
||||
if (!messageFile.exists()) {
|
||||
plugin.saveResource("message.yml", false);
|
||||
}
|
||||
messageConfig = YamlConfiguration.loadConfiguration(messageFile);
|
||||
}
|
||||
|
||||
public String getMessage(String path) {
|
||||
return ChatColor.translateAlternateColorCodes('&', messageConfig.getString(path, "&cMessage not found: " + path));
|
||||
}
|
||||
|
||||
public void reloadMessages() {
|
||||
File messageFile = new File(plugin.getDataFolder(), "message.yml");
|
||||
messageConfig = YamlConfiguration.loadConfiguration(messageFile);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.github.krzysiek944.mscrole.utils;
|
||||
|
||||
import com.github.krzysiek944.mscrole.MSCROLE;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class ShopDataManager {
|
||||
|
||||
private final MSCROLE plugin = MSCROLE.getInstance();
|
||||
private FileConfiguration shopConfig = null;
|
||||
private File configFile = null;
|
||||
|
||||
public void reloadConfig() {
|
||||
if (configFile == null) {
|
||||
configFile = new File(plugin.getDataFolder(), "shop.yml");
|
||||
}
|
||||
shopConfig = YamlConfiguration.loadConfiguration(configFile);
|
||||
}
|
||||
|
||||
public FileConfiguration getConfig() {
|
||||
if (shopConfig == null) {
|
||||
reloadConfig();
|
||||
}
|
||||
return shopConfig;
|
||||
}
|
||||
|
||||
public void saveConfig() {
|
||||
if (shopConfig == null || configFile == null) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
getConfig().save(configFile);
|
||||
} catch (IOException e) {
|
||||
plugin.getLogger().severe("Could not save config to " + configFile);
|
||||
}
|
||||
}
|
||||
|
||||
public void saveDefaultConfig() {
|
||||
if (configFile == null) {
|
||||
configFile = new File(plugin.getDataFolder(), "shop.yml");
|
||||
}
|
||||
if (!configFile.exists()) {
|
||||
plugin.saveResource("shop.yml", false);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user