From 85d7cf3a6d3b574f39f24a1d5f5fb1992d021145 Mon Sep 17 00:00:00 2001 From: OllieJW <71931263+OllieJW@users.noreply.github.com> Date: Sat, 10 Jul 2021 16:17:50 +0100 Subject: [PATCH] Add files via upload --- .../olliejw/oremarket/Commands/Reload.java | 17 ++++ .../java/com/olliejw/oremarket/OreMarket.java | 56 ++++++++++++ .../com/olliejw/oremarket/Utils/Events.java | 86 +++++++++++++++++++ .../olliejw/oremarket/Utils/MarketGUI.java | 42 +++++++++ src/main/resources/config.yml | 29 +++++++ src/main/resources/plugin.yml | 15 ++++ 6 files changed, 245 insertions(+) create mode 100644 src/main/java/com/olliejw/oremarket/Commands/Reload.java create mode 100644 src/main/java/com/olliejw/oremarket/OreMarket.java create mode 100644 src/main/java/com/olliejw/oremarket/Utils/Events.java create mode 100644 src/main/java/com/olliejw/oremarket/Utils/MarketGUI.java create mode 100644 src/main/resources/config.yml create mode 100644 src/main/resources/plugin.yml diff --git a/src/main/java/com/olliejw/oremarket/Commands/Reload.java b/src/main/java/com/olliejw/oremarket/Commands/Reload.java new file mode 100644 index 0000000..ef85c98 --- /dev/null +++ b/src/main/java/com/olliejw/oremarket/Commands/Reload.java @@ -0,0 +1,17 @@ +package com.olliejw.oremarket.Commands; + +import com.olliejw.oremarket.OreMarket; +import org.bukkit.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; + +public class Reload implements CommandExecutor { + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + OreMarket.main().reloadConfig(); + sender.sendMessage(ChatColor.GREEN + "Reloaded successfully"); + return true; + } +} diff --git a/src/main/java/com/olliejw/oremarket/OreMarket.java b/src/main/java/com/olliejw/oremarket/OreMarket.java new file mode 100644 index 0000000..8d1ac6a --- /dev/null +++ b/src/main/java/com/olliejw/oremarket/OreMarket.java @@ -0,0 +1,56 @@ +package com.olliejw.oremarket; + +import com.olliejw.oremarket.Commands.Reload; +import com.olliejw.oremarket.Utils.Events; +import com.olliejw.oremarket.Utils.MarketGUI; +import net.milkbowl.vault.economy.Economy; +import org.bukkit.Material; +import org.bukkit.inventory.ItemStack; +import org.bukkit.plugin.RegisteredServiceProvider; +import org.bukkit.plugin.java.JavaPlugin; + +import java.util.logging.Logger; + +public final class OreMarket extends JavaPlugin { + + private static OreMarket instance; + private static final Logger log = Logger.getLogger("Minecraft"); + private static Economy econ = null; + + public void onEnable() { + instance = this; + saveDefaultConfig(); + + + this.getServer().getPluginManager().registerEvents(new Events(), this); + this.getCommand("openmarket").setExecutor(new MarketGUI()); + this.getCommand("om-reload").setExecutor(new Reload()); + + if (!setupEconomy() ) { + log.severe(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName())); + getServer().getPluginManager().disablePlugin(this); + return; + } + } + + public static OreMarket main(){ + return instance; + } + + private boolean setupEconomy() { + if (getServer().getPluginManager().getPlugin("Vault") == null) { + return false; + } + RegisteredServiceProvider rsp = getServer().getServicesManager().getRegistration(Economy.class); + if (rsp == null) { + return false; + } + econ = rsp.getProvider(); + return econ != null; + } + + public static Economy getEconomy() { + return econ; + } +} + diff --git a/src/main/java/com/olliejw/oremarket/Utils/Events.java b/src/main/java/com/olliejw/oremarket/Utils/Events.java new file mode 100644 index 0000000..7b6bd1e --- /dev/null +++ b/src/main/java/com/olliejw/oremarket/Utils/Events.java @@ -0,0 +1,86 @@ +package com.olliejw.oremarket.Utils; + +import com.olliejw.oremarket.OreMarket; +import net.milkbowl.vault.economy.EconomyResponse; +import org.bukkit.ChatColor; +import org.bukkit.Material; +import org.bukkit.OfflinePlayer; +import org.bukkit.entity.HumanEntity; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.inventory.ClickType; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.inventory.InventoryDragEvent; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryView; +import org.bukkit.inventory.ItemStack; + +public class Events implements Listener { + + public void debug (String Message, HumanEntity Player) { + Player.sendMessage(ChatColor.translateAlternateColorCodes('&', Message)); + } + + public void addMoney (double Money, HumanEntity Player) { + EconomyResponse r = OreMarket.getEconomy().depositPlayer((OfflinePlayer) Player, Money); + if(r.transactionSuccess()) { + Player.sendMessage(String.format("You were given %s and now have %s", OreMarket.getEconomy().format(r.amount), OreMarket.getEconomy().format(r.balance))); + } else { + Player.sendMessage(String.format("An error occured: %s", r.errorMessage)); + } + } + public void takeMoney (double Money, HumanEntity Player) { + EconomyResponse r = OreMarket.getEconomy().withdrawPlayer((OfflinePlayer) Player, Money); + if(r.transactionSuccess()) { + Player.sendMessage(String.format("You paid %s and now have %s", OreMarket.getEconomy().format(r.amount), OreMarket.getEconomy().format(r.balance))); + } else { + Player.sendMessage(String.format("An error occured: %s", r.errorMessage)); + } + } + public double balance (HumanEntity Player) { + double bal = OreMarket.getEconomy().getBalance((OfflinePlayer) Player); + return bal; + } + + @EventHandler + public void clickEvent (InventoryClickEvent event) { + Inventory playerInventory = event.getWhoClicked().getInventory(); // Player's inventory + InventoryView playerView = event.getView(); // Player's inventory view + HumanEntity player = event.getWhoClicked(); // Player that clicked + + if (event.getCurrentItem() == null) { // Removes the giant error when clicking an empty slot + return; + } // Null check. Prevents errors + + if (playerView.getTitle().equals("OreMarket Coming soon!")) { event.setCancelled(true); // I know. Its a bad way of checking. + Double value = OreMarket.main().getConfig().getDouble("items." + event.getSlot() + ".value"); // Ore Value + String itemConfig = OreMarket.main().getConfig().getString("items." + event.getSlot() + ".item"); // Config location of item + ItemStack clickedItem = new ItemStack(Material.matchMaterial(itemConfig)); // Item that user clicked + ItemStack clicked = event.getCurrentItem(); // The exact item the player clicked + Material clickedType = event.getCurrentItem().getType(); // Clicked item as material ("Material.DIAMOND" rather than a long list of json crap) + + if (event.getClick() == ClickType.LEFT) { // Sell Mode + if (playerInventory.containsAtLeast(clickedItem, 1)) { // Do they have this ore? + playerInventory.removeItem(clickedItem); + addMoney(value, player); + } + } + if (event.getClick() == ClickType.RIGHT) { // Buy Mode + if (balance(player) > value) { // Do they have enough money + playerInventory.addItem(clickedItem); + takeMoney(value, player); + } else { + debug("&4You do not have enough money!", player); + } + } + } + } + + @EventHandler + public void moveEvent (InventoryDragEvent event) { + InventoryView playerView = event.getView(); // Player's inventory view + if (playerView.getTitle().equals("OreMarket Coming soon!")) { // Using our GUI + event.setCancelled(true); + } + } +} diff --git a/src/main/java/com/olliejw/oremarket/Utils/MarketGUI.java b/src/main/java/com/olliejw/oremarket/Utils/MarketGUI.java new file mode 100644 index 0000000..92cb9a2 --- /dev/null +++ b/src/main/java/com/olliejw/oremarket/Utils/MarketGUI.java @@ -0,0 +1,42 @@ +package com.olliejw.oremarket.Utils; + +import com.olliejw.oremarket.OreMarket; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +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.event.Listener; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; + +public class MarketGUI implements Listener, CommandExecutor { + + Inventory inv = Bukkit.createInventory(null, 27, "OreMarket Coming soon!"); + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if (command.getName().equalsIgnoreCase("openmarket")) { + Player player = (Player) sender; + + for (String key : OreMarket.main().getConfig().getConfigurationSection("items").getKeys(false)) { + ConfigurationSection keySection = OreMarket.main().getConfig().getConfigurationSection("items").getConfigurationSection(key); + assert keySection != null; + + ItemStack item = new ItemStack(Material.matchMaterial(keySection.getString("item"))); + ItemMeta meta = item.getItemMeta(); + meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', keySection.getString("name"))); + item.setItemMeta(meta); + + inv.setItem(Integer.parseInt(key), item); + player.openInventory(inv); + } + } + return true; + } +} + diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml new file mode 100644 index 0000000..9ab1e58 --- /dev/null +++ b/src/main/resources/config.yml @@ -0,0 +1,29 @@ +# Cost = Initial cost of ore. (0% change) +# Value = Current cost of ore. (With % change) +# When adding a new ore make the cost and value to same + +items: + 0: + item: DIAMOND + name: '&bDiamond Ore' + lore: + - '&a$1000' + cost: 1000 + value: 1000 + + 1: + item: GOLD_INGOT + name: '&6Gold Ingot' + lore: + - '&a$500' + cost: 500 + value: 500 + + 2: + item: IRON_INGOT + name: '&7Iron Ore' + lore: + - '&a$250' + cost: 250 + value: 250 + diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..53e1eb6 --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,15 @@ +name: OreMarket +version: ${project.version} +main: com.olliejw.oremarket.OreMarket +api-version: 1.16 +depend: [ Vault ] +authors: [ OllieJW ] +description: Realistic stock market for ores + +commands: + openmarket: + description: Opens OreMarket GUI + usage: / + om-reload: + description: Reloads OreMarket + usage: / \ No newline at end of file