OllieJW 2021-07-14 11:38:11 +01:00 committed by GitHub
parent 5c42384d91
commit a559b0d606
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 348 additions and 110 deletions

View File

@ -1,44 +1,33 @@
package com.olliejw.oremarket.Chat;
import com.olliejw.oremarket.OreMarket;
import com.olliejw.oremarket.Utils.PlaceHolders;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import java.text.DecimalFormat;
import java.text.Format;
import java.util.Objects;
public class ValueUpdates {
PlaceHolders plh = new PlaceHolders();
public void announceValue() {
Bukkit.getScheduler().scheduleSyncRepeatingTask(OreMarket.main(), new Runnable() {
@Override
public void run() {
for (String oreItem : Objects.requireNonNull(OreMarket.main().getGuiConfig().getConfigurationSection("items")).getKeys(false)) {
ConfigurationSection keySection = Objects.requireNonNull(OreMarket.main().getGuiConfig().getConfigurationSection("items")).getConfigurationSection(oreItem);
Bukkit.getScheduler().scheduleSyncRepeatingTask(OreMarket.main(), () -> {
for (String key : Objects.requireNonNull(OreMarket.main().getGuiConfig().getConfigurationSection("items")).getKeys(false)) {
ConfigurationSection keySection = Objects.requireNonNull(OreMarket.main().getGuiConfig().getConfigurationSection("items")).getConfigurationSection(key);
String message = OreMarket.main().getConfig().getString("valueupdates.format");
assert keySection != null;
String name = Objects.requireNonNull(keySection.getString("name"));
int stock = keySection.getInt("stock");
double value = keySection.getDouble("value");
double cost = keySection.getDouble("cost");
double difference = (value-cost);
double change = (value/cost);
double percent = (change*100);
assert keySection != null;
assert message != null;
final Format DECIMAL_FORMAT = new DecimalFormat("#0.0#");
String format = ChatColor.translateAlternateColorCodes('&', Objects.requireNonNull(OreMarket.main().getConfig().getString("valueupdates.format"))
.replace("[name]", name)
.replace("[stock]", String.valueOf(stock))
.replace("[value]", DECIMAL_FORMAT.format(value))
.replace("[cost]", DECIMAL_FORMAT.format(cost))
.replace("[change]", DECIMAL_FORMAT.format(difference))
.replace("[percent]", DECIMAL_FORMAT.format(percent))
);
Bukkit.broadcastMessage(format);
for (Player player: Bukkit.getOnlinePlayers()) {
if (keySection.getBoolean("hide")) {
return;
}
player.sendMessage(plh.format(message, player, keySection));
}
}
}, 0L, (OreMarket.main().getConfig().getInt("valueupdates.time")* 20L*60));
}
}

View File

@ -0,0 +1,19 @@
package com.olliejw.oremarket.Commands;
import com.olliejw.oremarket.Events.MarketCrash;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
public class CrashMarket implements CommandExecutor {
MarketCrash mkCrash = new MarketCrash();
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (command.getName().equalsIgnoreCase("om-crash")) {
if (sender.hasPermission("oremarket.crashmarket")) {
mkCrash.forceCrash();
}
}
return true;
}
}

View File

@ -0,0 +1,20 @@
package com.olliejw.oremarket.Commands;
import com.olliejw.oremarket.Utils.AddItem;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class OpenMarket implements CommandExecutor {
AddItem addItem = new AddItem();
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (command.getName().equalsIgnoreCase("openmarket")) {
if (sender.hasPermission("oremarket.open")) {
addItem.createGUI((Player) sender);
}
}
return true;
}
}

View File

@ -1,6 +1,8 @@
package com.olliejw.oremarket.Commands;
import com.olliejw.oremarket.Chat.ValueUpdates;
import com.olliejw.oremarket.OreMarket;
import com.olliejw.oremarket.Utils.AddItem;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
@ -10,8 +12,13 @@ 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");
if (sender.hasPermission("oremarket.reload")) {
OreMarket.main().reloadConfig();
OreMarket.main().reloadGuiConfig();
sender.sendMessage(ChatColor.GREEN + "Reloaded successfully. Some options may require a restart to take place.");
} else {
sender.sendMessage(ChatColor.RED + "You do not have permission to do this!");
}
return true;
}
}

View File

@ -0,0 +1,24 @@
package com.olliejw.oremarket.Commands;
import com.olliejw.oremarket.Utils.Stats;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
public class StatsCommands implements CommandExecutor {
Stats stats = new Stats();
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (command.getName().equalsIgnoreCase("om-stats")) {
if (sender.hasPermission("oremarket.stats")) {
sender.sendMessage("Total Items: " + stats.totalItems());
sender.sendMessage("Total Value: " + stats.totalValues());
sender.sendMessage("https://bstats.org/plugin/bukkit/OreMarket/10961");
}
}
return true;
}
}

View File

@ -0,0 +1,36 @@
package com.olliejw.oremarket.Events;
import com.olliejw.oremarket.OreMarket;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Sound;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import java.util.Objects;
public class MarketCrash implements Listener {
public void forceCrash() {
String notification = OreMarket.main().getConfig().getString("marketcrash.message");
for (Player player: Bukkit.getOnlinePlayers()) {
assert notification != null;
player.sendMessage(ChatColor.translateAlternateColorCodes('&', notification).replace("[amount]", Objects.requireNonNull(OreMarket.main().getConfig().getString("marketcrash.amount"))));
player.playSound(player.getLocation(), Sound.ENTITY_ITEM_BREAK, 10.0F, 1);
}
for (String key : Objects.requireNonNull(OreMarket.main().getGuiConfig().getConfigurationSection("items")).getKeys(false)) {
ConfigurationSection keySection = Objects.requireNonNull(OreMarket.main().getGuiConfig().getConfigurationSection("items")).getConfigurationSection(key);
assert keySection != null;
double value = keySection.getDouble("value");
double amount = OreMarket.main().getConfig().getDouble("marketcrash.amount");
keySection.set("value", (value*(1-(amount/100))));
OreMarket.main().saveGuiConfig();
}
}
public void startCrash() {
if (OreMarket.main().getConfig().getBoolean("marketcrash.enabled")) {
Bukkit.getScheduler().scheduleSyncRepeatingTask(OreMarket.main(), this::forceCrash, (OreMarket.main().getConfig().getInt("marketcrash.time") * 20L * 60 * 60), (OreMarket.main().getConfig().getInt("marketcrash.time") * 20L * 60 * 60));
}
}
}

View File

@ -1,7 +1,7 @@
package com.olliejw.oremarket.Listeners;
import com.olliejw.oremarket.OreMarket;
import org.bukkit.Bukkit;
import com.olliejw.oremarket.Utils.Stats;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
@ -69,6 +69,8 @@ public class InventoryEvents implements Listener {
double value = OreMarket.main().getGuiConfig().getDouble("items." + event.getSlot() + ".value"); // Ore Value
int stock = OreMarket.main().getGuiConfig().getInt("items." + event.getSlot() + ".stock"); // Ore stock
String itemConfig = OreMarket.main().getGuiConfig().getString("items." + event.getSlot() + ".item"); // Config location of item
boolean close = OreMarket.main().getGuiConfig().getBoolean("items." + event.getSlot() + ".close");
assert itemConfig != null;
ItemStack clickedItem = new ItemStack(Objects.requireNonNull(Material.matchMaterial(itemConfig))); // Item that user clicked
@ -90,6 +92,9 @@ public class InventoryEvents implements Listener {
}
}
}
if (close) {
player.closeInventory();
}
}
}

View File

@ -1,23 +1,35 @@
package com.olliejw.oremarket;
import com.olliejw.oremarket.Chat.ValueUpdates;
import com.olliejw.oremarket.Commands.CrashMarket;
import com.olliejw.oremarket.Commands.OpenMarket;
import com.olliejw.oremarket.Commands.Reload;
import com.olliejw.oremarket.Commands.StatsCommands;
import com.olliejw.oremarket.Events.MarketCrash;
import com.olliejw.oremarket.Listeners.InventoryEvents;
import com.olliejw.oremarket.Utils.AddItem;
import com.olliejw.oremarket.Utils.Stats;
import com.olliejw.oremarket.Utils.UpdateChecker;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.io.IOException;
import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.logging.Logger;
public final class OreMarket extends JavaPlugin {
ValueUpdates valueUpdates = new ValueUpdates();
MarketCrash mkCrash = new MarketCrash();
Stats stats = new Stats();
private static OreMarket instance;
private static final Logger log = Logger.getLogger("Minecraft");
private static Economy econ = null;
@ -29,20 +41,49 @@ public final class OreMarket extends JavaPlugin {
instance = this;
saveDefaultConfig();
createGuiConfig();
Logger logger = this.getLogger();
// Spigot and bStats
new UpdateChecker(this, 91015).getVersion(version -> {
if (this.getDescription().getVersion().equalsIgnoreCase(version)) {
logger.info("You are up to date!");
} else {
logger.info("New updates available!");
for (Player player : Bukkit.getOnlinePlayers()) {
if (player.hasPermission("oremarket.reload")) { player.sendMessage(ChatColor.GREEN + "New OreMarket version available!"); }
}
}
});
final Metrics metrics = new Metrics(this, 10961);
metrics.addCustomChart(new Metrics.SimplePie("", () -> ""));
metrics.addCustomChart(new Metrics.SingleLineChart("total_values", new Callable<Integer>() {
@Override
public Integer call() {
return stats.totalValues();
}
}));
metrics.addCustomChart(new Metrics.SingleLineChart("total_items", new Callable<Integer>() {
@Override
public Integer call() {
return stats.totalItems();
}
}));
// Commands and Events
this.getServer().getPluginManager().registerEvents(new InventoryEvents(), this);
Objects.requireNonNull(this.getCommand("openmarket")).setExecutor(new AddItem());
Objects.requireNonNull(this.getCommand("openmarket")).setExecutor(new OpenMarket());
Objects.requireNonNull(this.getCommand("om-reload")).setExecutor(new Reload());
Objects.requireNonNull(this.getCommand("om-stats")).setExecutor(new StatsCommands());
Objects.requireNonNull(this.getCommand("om-crash")).setExecutor(new CrashMarket());
valueUpdates.announceValue();
mkCrash.startCrash();
stats.reloadStats();
// Economy
if (!setupEconomy() ) {
log.severe(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName()));
getServer().getPluginManager().disablePlugin(this);
}
valueUpdates.announceValue();
}
public FileConfiguration getGuiConfig() {
@ -71,6 +112,13 @@ public final class OreMarket extends JavaPlugin {
e.printStackTrace();
}
}
public void reloadGuiConfig() {
try {
guiConfig.load(guiFile);
} catch (IOException | InvalidConfigurationException e) {
e.printStackTrace();
}
}
public static OreMarket main(){
return instance;

View File

@ -4,9 +4,6 @@ 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;
@ -14,67 +11,42 @@ import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.text.DecimalFormat;
import java.text.Format;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class AddItem implements Listener, CommandExecutor {
public class AddItem implements Listener {
String title = OreMarket.main().getGuiConfig().getString("gui.title");
int rows = OreMarket.main().getGuiConfig().getInt("gui.rows");
Inventory inv = Bukkit.createInventory(null, rows*9, ChatColor.translateAlternateColorCodes('&', title));
PlaceHolders plh = new PlaceHolders();
public void createGUI (Player player) {
for (String key : Objects.requireNonNull(OreMarket.main().getGuiConfig().getConfigurationSection("items")).getKeys(false)) {
ConfigurationSection keySection = Objects.requireNonNull(OreMarket.main().getGuiConfig().getConfigurationSection("items")).getConfigurationSection(key);
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (command.getName().equalsIgnoreCase("openmarket")) {
Player player = (Player) sender;
assert keySection != null;
for (String key : Objects.requireNonNull(OreMarket.main().getGuiConfig().getConfigurationSection("items")).getKeys(false)) {
ConfigurationSection keySection = Objects.requireNonNull(OreMarket.main().getGuiConfig().getConfigurationSection("items")).getConfigurationSection(key);
assert keySection != null;
int stock = keySection.getInt("stock");
final Format DECIMAL_FORMAT = new DecimalFormat("#0.0#");
double value = keySection.getDouble("value");
double cost = keySection.getDouble("cost");
double change = (value/cost);
double percent = (change*100);
ItemStack item = new ItemStack(Objects.requireNonNull(Material.matchMaterial(Objects.requireNonNull(keySection.getString("item")))));
ItemMeta meta = item.getItemMeta();
assert meta != null;
meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', Objects.requireNonNull(keySection.getString("name")))
.replace("[stock]", DECIMAL_FORMAT.format(stock))
.replace("[percent]", DECIMAL_FORMAT.format(percent))
.replace("[value]", DECIMAL_FORMAT.format(value))
.replace("[cost]", DECIMAL_FORMAT.format(cost))
);
List<String> lore = new ArrayList<>();
for (String loreItem : Objects.requireNonNull(keySection.getStringList("lore"))) {
String string = ChatColor.translateAlternateColorCodes('&', loreItem)
.replace("[stock]", DECIMAL_FORMAT.format(stock))
.replace("[percent]", DECIMAL_FORMAT.format(percent))
.replace("[value]", DECIMAL_FORMAT.format(value))
.replace("[cost]", DECIMAL_FORMAT.format(cost))
;
lore.add(string);
}
meta.setLore(lore);
item.setItemMeta(meta);
inv.setItem(Integer.parseInt(key), item);
player.openInventory(inv);
ItemStack item = new ItemStack(Objects.requireNonNull(Material.matchMaterial(Objects.requireNonNull(keySection.getString("item")))));
ItemMeta meta = item.getItemMeta();
String name = keySection.getString("name");
assert meta != null;
assert name != null;
meta.setDisplayName(plh.format(name, player, keySection));
List<String> lore = new ArrayList<>();
for (String loreItem : Objects.requireNonNull(keySection.getStringList("lore"))) {
String string = plh.format(loreItem, player, keySection);
lore.add(string);
}
meta.setLore(lore);
item.setItemMeta(meta);
inv.setItem(Integer.parseInt(key), item);
player.openInventory(inv);
}
return true;
}
}

View File

@ -0,0 +1,38 @@
package com.olliejw.oremarket.Utils;
import com.olliejw.oremarket.OreMarket;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import java.text.DecimalFormat;
import java.text.Format;
public class PlaceHolders {
public String format (String string, HumanEntity player, ConfigurationSection configurationSection) {
Player playerObj = (Player) player;
String name = configurationSection.getString("name");
final Format DECIMAL_FORMAT = new DecimalFormat("#0.0#");
int stock = configurationSection.getInt("stock");
double value = configurationSection.getDouble("value");
double cost = configurationSection.getDouble("cost");
double difference = (value-cost);
double change = (value/cost);
double percent = ((change*100)-100);
double balance = OreMarket.getEconomy().getBalance(playerObj);
assert name != null;
return ChatColor.translateAlternateColorCodes('&', string
.replace("[name]", name)
.replace("[stock]", String.valueOf(stock))
.replace("[value]", DECIMAL_FORMAT.format(value))
.replace("[cost]", DECIMAL_FORMAT.format(cost))
.replace("[change]", DECIMAL_FORMAT.format(difference))
.replace("[percent]", DECIMAL_FORMAT.format(percent))
.replace("[balance]", DECIMAL_FORMAT.format(balance))
);
}
}

View File

@ -0,0 +1,36 @@
package com.olliejw.oremarket.Utils;
import com.olliejw.oremarket.OreMarket;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
import java.util.Objects;
public class Stats {
public int totalItems () {
int items = 0;
for (String key : Objects.requireNonNull(OreMarket.main().getGuiConfig().getConfigurationSection("items")).getKeys(false)) {
items++;
}
return items;
}
public int totalValues() {
int values = 0;
for (String key : Objects.requireNonNull(OreMarket.main().getGuiConfig().getConfigurationSection("items")).getKeys(false)) {
ConfigurationSection keySection = Objects.requireNonNull(OreMarket.main().getGuiConfig().getConfigurationSection("items")).getConfigurationSection(key);
assert keySection != null;
values = (values + keySection.getInt("value"));
}
return values;
}
public void reloadStats() {
Bukkit.getScheduler().scheduleSyncRepeatingTask(OreMarket.main(), () -> {
totalValues();
totalItems();
}, 0L, 10*20);
}
}

View File

@ -0,0 +1,33 @@
package com.olliejw.oremarket.Utils;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.Consumer;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Scanner;
public class UpdateChecker {
private final JavaPlugin plugin;
private final int resourceId;
public UpdateChecker(JavaPlugin plugin, int resourceId) {
this.plugin = plugin;
this.resourceId = resourceId;
}
public void getVersion(final Consumer<String> consumer) {
Bukkit.getScheduler().runTaskAsynchronously(this.plugin, () -> {
try (InputStream inputStream = new URL("https://api.spigotmc.org/legacy/update.php?resource=" + this.resourceId).openStream(); Scanner scanner = new Scanner(inputStream)) {
if (scanner.hasNext()) {
consumer.accept(scanner.next());
}
} catch (IOException exception) {
this.plugin.getLogger().info("Cannot look for updates: " + exception.getMessage());
}
});
}
}

View File

@ -10,7 +10,7 @@ valueupdates:
# The below will look like: (These values are just examples)
# "Diamond is now valued at $500 ($250 / 200% change from the original $250). 100 left in stock!"
format: >
[name] is now valued at $[value] ($[change] / [percent]% change from the original [cost]). [stock] left in stock!
[name] is now valued at $[value] ($[change] / [percent]% change from the original $[cost]). [stock] left in stock!
# Tax (%) is added to every purchase and sale
tax: 10
@ -20,3 +20,13 @@ tax: 10
# Default: 0.01 (RECOMMENDED)
multiplier: 0.01
# Market Crash is an event that completely kills the values of ores just like real life.
# As this is experimental I recommend that you do not enable this with a server full of people
marketcrash:
enabled: false
time: 8 # Hours
amount: 80 # Amount (%) the values of ores will decrease
message: >
&4&lMARKET CRASH! ORE VALUES DECREASED BY UP TO [amount]%

View File

@ -1,27 +1,7 @@
# Example Item
#0:
# # Item to be shown in the GUI and given/taken
# item: DIAMOND
# # Item name (Buying this ore will not give the user an ore with this name!)
# name: '&bDiamond Ore'
# # Lore (Infinite lines). Find the placeholders here -> https://github.com/OllieJW/Ore-Market/wiki/Placeholders
# lore:
# - '&aValue: $[value]'
# - '&aOriginal: $[cost]'
# - '&aStock: [stock]'
# - '&aChange: [percent]%'
# - '&7Right-Click to buy'
# - '&7Left-Click to sell'
# # Starting cost of ore
# cost: 1000
# # Current cost of ore (Changing this will edit the GUI value)
# value: 1000
# # Stock of the ore (Set to -1 for infinite)
# stock: 100
# Customise the GUI here! View the GitHub wiki to utilise all of OreMarkets features
gui:
title: '&6OreMarket'
title: '&4OreMarket'
rows: 3
items:
@ -64,4 +44,19 @@ items:
- '&7Left-Click to sell'
cost: 250
value: 250
stock: 100
stock: 100
5:
item: BARRIER
name: '&cClose'
lore:
- '&cClose GUI'
close: true
hide: true
6:
item: PAPER
name: '&aBalance'
lore:
- '&a$[balance]'
hide: true

View File

@ -12,4 +12,10 @@ commands:
usage: /<command>
om-reload:
description: Reloads OreMarket
usage: /<command>
om-stats:
description: Shows OreMarket stats
usage: /<command>
om-crash:
description: Crashes Market
usage: /<command>