From cee388aa7d39657fd5d25cdbea75d65a661ad425 Mon Sep 17 00:00:00 2001 From: tianyu <32282861@qq.com> Date: Thu, 25 Jul 2024 05:56:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 38 ++++ pom.xml | 42 ++++ .../me/Demon/DemonCropBuyShop/CropData.java | 103 +++++++++ .../Demon/DemonCropBuyShop/CropShopAPI.java | 205 ++++++++++++++++++ .../Event/MarktStatsEvent.java | 46 ++++ .../Event/PlayerCropBuyEvent.java | 57 +++++ .../java/me/Demon/DemonCropBuyShop/Main.java | 82 +++++++ .../Demon/DemonCropBuyShop/Placeholders.java | 61 ++++++ .../DemonCropBuyShop/TimeCheckRunnable.java | 23 ++ .../DemonCropBuyShop/listener/BuyGui.java | 96 ++++++++ .../listener/GuiListener.java | 127 +++++++++++ .../DemonCropBuyShop/listener/MarkStats.java | 64 ++++++ .../listener/PlayerCropBuy.java | 54 +++++ src/main/resources/config.yml | 68 ++++++ src/main/resources/plugin.yml | 5 + 15 files changed, 1071 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/me/Demon/DemonCropBuyShop/CropData.java create mode 100644 src/main/java/me/Demon/DemonCropBuyShop/CropShopAPI.java create mode 100644 src/main/java/me/Demon/DemonCropBuyShop/Event/MarktStatsEvent.java create mode 100644 src/main/java/me/Demon/DemonCropBuyShop/Event/PlayerCropBuyEvent.java create mode 100644 src/main/java/me/Demon/DemonCropBuyShop/Main.java create mode 100644 src/main/java/me/Demon/DemonCropBuyShop/Placeholders.java create mode 100644 src/main/java/me/Demon/DemonCropBuyShop/TimeCheckRunnable.java create mode 100644 src/main/java/me/Demon/DemonCropBuyShop/listener/BuyGui.java create mode 100644 src/main/java/me/Demon/DemonCropBuyShop/listener/GuiListener.java create mode 100644 src/main/java/me/Demon/DemonCropBuyShop/listener/MarkStats.java create mode 100644 src/main/java/me/Demon/DemonCropBuyShop/listener/PlayerCropBuy.java create mode 100644 src/main/resources/config.yml create mode 100644 src/main/resources/plugin.yml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..b18b765 --- /dev/null +++ b/pom.xml @@ -0,0 +1,42 @@ + + + 4.0.0 + + org.example + DemonCropBuyShop + 1.0-SNAPSHOT + + + 8 + 8 + UTF-8 + + + + + public-rpg + https://repo.aurora-pixels.com/repository/public-rpg/ + + + + + + org.spigotmc + spigot-api + 1.12.2 + + + me.clip.placeholderapi + PlaceholderAPI + 2.9.2 + + + me.Demon.DemonPlugin + DemonAPI + 1.2.0 + + + + \ No newline at end of file diff --git a/src/main/java/me/Demon/DemonCropBuyShop/CropData.java b/src/main/java/me/Demon/DemonCropBuyShop/CropData.java new file mode 100644 index 0000000..f940c5e --- /dev/null +++ b/src/main/java/me/Demon/DemonCropBuyShop/CropData.java @@ -0,0 +1,103 @@ +package me.Demon.DemonCropBuyShop; + +import org.bukkit.Material; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.inventory.ItemStack; + +import java.text.DecimalFormat; +import java.util.ArrayList; +import java.util.List; + +public class CropData { + + private String cropType; + + private double dmoney; + private double money; + + private double last; + + private List log; + + public CropData(String cropType) { + this.cropType = cropType; + FileConfiguration yml = Main.plugin.getConfig(); + this.dmoney = yml.getDouble(cropType + ".default"); + this.money = yml.getDouble(cropType + ".money"); + this.last = yml.getDouble(cropType + ".last"); + this.log = yml.getStringList(cropType + ".log"); + } + + public void SaveData() { + FileConfiguration yml = Main.plugin.getConfig(); + yml.set(cropType + ".default", this.dmoney); + yml.set(cropType + ".money", this.money); + yml.set(cropType + ".last", this.last); + yml.set(cropType + ".log", this.log); + Main.plugin.saveConfig(); + } + + public String getCropType() { + return cropType; + } + + public ItemStack getItem() { + return new ItemStack(Material.valueOf(this.getCropType())); + } + + public double getDmoney() { + return dmoney; + } + + public double getMoney() { + return money; + } + + public void setMoney(double money) { + this.money = money; + } + + public double getLast() { + return last; + } + + public void setLast(double last) { + this.last = last; + } + + public List getLog() { + return log; + } + + public void setLog(List log) { + this.log = log; + } + + public void setCropMarketLog(String lore) { + List list = new ArrayList<>(); + list.add(lore); + List jiu_log = getLog(); + if (jiu_log.size() >= 4) { + for (int i = 0; i < 4; i++) { + list.add(jiu_log.get(i)); + } + } else { + list.addAll(getLog()); + } + setLog(list); + } + + public String getCurrenBFB() { + double change = getCurrenAndPrevious(); + if (change >= 0) { + return "§a§l↑↑ §a" + new DecimalFormat("###.##").format(change) + "%"; + } else { + return "§c§l↓↓ §c" + new DecimalFormat("###.##").format(change) + "%"; + } + } + + public double getCurrenAndPrevious() { + double change = getMoney() - getLast(); + return (change / getLast()) * 100; + } +} diff --git a/src/main/java/me/Demon/DemonCropBuyShop/CropShopAPI.java b/src/main/java/me/Demon/DemonCropBuyShop/CropShopAPI.java new file mode 100644 index 0000000..a2898ca --- /dev/null +++ b/src/main/java/me/Demon/DemonCropBuyShop/CropShopAPI.java @@ -0,0 +1,205 @@ +package me.Demon.DemonCropBuyShop; + +import me.Demon.DemonCropBuyShop.Event.MarktStatsEvent; +import me.Demon.DemonCropBuyShop.Event.PlayerCropBuyEvent; +import me.Demon.DemonPlugin.DemonAPI; +import org.bukkit.Bukkit; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.entity.Player; + +import java.text.DecimalFormat; +import java.util.Random; + +public class CropShopAPI { + + public static int getCropBuyAmount(String CorpType){ + FileConfiguration yml = Main.plugin.getConfig(); + return yml.getInt("BuyData."+CorpType); + } + + public static void addCropBuyAmount(String CorpType,int amount){ + FileConfiguration yml = Main.plugin.getConfig(); + int a = getCropBuyAmount(CorpType) + amount; + yml.set("BuyData."+CorpType,a); + Main.plugin.saveConfig(); + } + + public static void setCropBuyAmount(String CorpType,int amount){ + FileConfiguration yml = Main.plugin.getConfig(); + yml.set("BuyData."+CorpType,amount); + Main.plugin.saveConfig(); + } + + public static void SellCropsItemsEvant(Player p, String CropType, int sell_groupAmount){ + CropData dataAPI = Main.dataAPI.get(CropType); + int price = (int) dataAPI.getMoney(); + if(sell_groupAmount >= 2){ + price = price * sell_groupAmount; + } + double tax = 0.05; + if(p.hasPermission("vip2.use")){ + tax = 0.02; + }else if(p.hasPermission("vip1.use")){ + tax = 0.03; + } + price = (int) (price - (price * tax)); + double taxMoney = price * tax; + DemonAPI.giveMoney(p, price); + p.sendMessage(Main.prefix + "你获得了: §f$" + new DecimalFormat(",###.##").format(price) + "金币 §6(纳税: " + new DecimalFormat("#.##").format(taxMoney) + "金币)"); + int sellAmount = sell_groupAmount * 64; + if(CropType.equalsIgnoreCase("EGG")){ + sellAmount = sell_groupAmount * 16; + } + PlayerCropBuyEvent event = new PlayerCropBuyEvent(p, dataAPI.getCropType(), sellAmount,price,(int) taxMoney); + Bukkit.getPluginManager().callEvent(event); + } + + public static void Start_Updata_Event(String CropType,int type){ + // 获取CropData数据 + CropData dataAPI = Main.dataAPI.get(CropType); + // 刷新现在价格 + double current_price = dataAPI.getMoney(); + // type == 0 系统随机 + // type == 1 上涨 + // type == 2 下跌 + if (type == 0) { + if (current_price >= (dataAPI.getDmoney() * 1.1)) { + // zhenfu_1 = 越大上涨概率低 + // zhenfu = 越小概率越高 + System_Updata_Price(dataAPI, 40, 70, 30); + } else if (current_price >= (dataAPI.getDmoney() * 0.8)) { + System_Updata_Price(dataAPI, 70, 40, 40); + } else { + System_Updata_Price(dataAPI, 70, 40, 40); + } + } else if (type == 1) { + if (Main.getRandomInt(100, 1) >= 70) { + Admin_Up_Price(dataAPI, 1); + } else { + Admin_Up_Price(dataAPI, 2); + } + } else if (type == 2) { + if (Main.getRandomInt(100, 1) >= 40) { + Admin_Down_Price(dataAPI, 1); + } else { + Admin_Down_Price(dataAPI, 2); + } + } + } + + public static void System_Updata_Price(CropData dataAPI,int zhenfu,int zhenfu_1,int die){ + // type = 1[涨] 2[跌] + int mk_stats = 0; + String new_s = ""; + // 刷新现在价格 + double server_current = dataAPI.getMoney(); + int gailv = Main.getRandomInt(100,1); + if(gailv >= zhenfu){ + if(Main.getRandomInt(100,1) >= zhenfu_1){ + double Vmin = server_current * 0.02; + double Vmax = server_current * 0.04; + double Vprice = new Random().nextDouble()*(Vmax-Vmin)+Vmin; + double price = server_current + Vprice; + new_s = new DecimalFormat("###.##").format(price); + mk_stats = 1; + Bukkit.getConsoleSender().sendMessage("§6[现货交易所SY] §f农产品: "+dataAPI.getCropType()+" 状态: 震浮-涨 "+server_current+" ---> "+new_s+" "+getCurrenBFB(price,dataAPI.getMoney())); + } else { + double Vmin = server_current * 0.03; + double Vmax = server_current * 0.05; + double Vprice = new Random().nextDouble()*(Vmax-Vmin)+Vmin; + double price = server_current - Vprice; + new_s = new DecimalFormat("###.##").format(price); + mk_stats = 2; + Bukkit.getConsoleSender().sendMessage("§6[现货交易所SY] §f农产品: "+dataAPI.getCropType()+" 状态: 震浮-跌 "+server_current+" ---> "+new_s+" "+getCurrenBFB(price,dataAPI.getMoney())); + } + }else if(gailv >= die){ + // 扣除 + double Vmin = server_current * 0.01; + double Vmax = server_current * 0.03; + double Vprice = new Random().nextDouble()*(Vmax-Vmin)+Vmin; + double price = server_current - Vprice; + new_s = new DecimalFormat("###.##").format(price); + mk_stats = 1; + Bukkit.getConsoleSender().sendMessage("§6[现货交易所SY] §f农产品: "+dataAPI.getCropType()+" 状态: 跌 "+server_current+" ---> "+new_s+" "+getCurrenBFB(price,dataAPI.getMoney())); + }else { + double Vmin = server_current * 0.01; + double Vmax = server_current * 0.03; + double Vprice = new Random().nextDouble()*(Vmax-Vmin)+Vmin; + double price = server_current + Vprice; + new_s = new DecimalFormat("###.##").format(price); + mk_stats = 2; + Bukkit.getConsoleSender().sendMessage("§6[现货交易所SY] §f农产品: "+dataAPI.getCropType()+" 状态: 涨 "+server_current+" ---> "+new_s+" "+getCurrenBFB(price,dataAPI.getMoney())); + } + double xian_jia_money = Double.parseDouble(new_s); + MarktStatsEvent event = new MarktStatsEvent(dataAPI, mk_stats,xian_jia_money); + Bukkit.getPluginManager().callEvent(event); + } + + public static void Admin_Up_Price(CropData dataAPI,int type){ + // type = 1[涨] 2[跌] + int mk_stats = 1; + String new_s = ""; + // 刷新现在价格 + double server_current = dataAPI.getMoney(); + if(type == 1){ + double Vmin = server_current * 0.02; + double Vmax = server_current * 0.04; + double Vprice = new Random().nextDouble()*(Vmax-Vmin)+Vmin; + double price = server_current + Vprice; + new_s = new DecimalFormat("###.##").format(price); + Bukkit.getConsoleSender().sendMessage("§6[现货交易所] §f农产品: "+dataAPI.getCropType()+" 状态: 震浮-涨 "+server_current+" ---> "+new_s+" "+getCurrenBFB(price,dataAPI.getMoney())); + }else{ + double Vmin = server_current * 0.01; + double Vmax = server_current * 0.03; + double Vprice = new Random().nextDouble()*(Vmax-Vmin)+Vmin; + double price = server_current + Vprice; + new_s = new DecimalFormat("###.##").format(price); + Bukkit.getConsoleSender().sendMessage("§6[现货交易所] §f农产品: "+dataAPI.getCropType()+" 状态: 涨 "+server_current+" ---> "+new_s+" "+getCurrenBFB(price,dataAPI.getMoney())); + } + double xian_jia_money = Double.parseDouble(new_s); + MarktStatsEvent event = new MarktStatsEvent(dataAPI, mk_stats,xian_jia_money); + Bukkit.getPluginManager().callEvent(event); + } + + + public static void Admin_Down_Price(CropData dataAPI,int type){ + // type = 1[涨] 2[跌] + int mk_stats = 2; + String new_s = ""; + // 刷新现在价格 + double server_current = dataAPI.getMoney(); + if (type == 1) { + double Vmin = server_current * 0.03; + double Vmax = server_current * 0.05; + double Vprice = new Random().nextDouble()*(Vmax-Vmin)+Vmin; + double price = server_current - Vprice; + new_s = new DecimalFormat("###.##").format(price); + Bukkit.getConsoleSender().sendMessage("§6[现货交易所] §f农产品: "+dataAPI.getCropType()+" 状态: 震浮-跌 "+server_current+" ---> "+new_s+" "+getCurrenBFB(price,dataAPI.getMoney())); + } else { + double Vmin = server_current * 0.01; + double Vmax = server_current * 0.03; + double Vprice = new Random().nextDouble()*(Vmax-Vmin)+Vmin; + double price = server_current - Vprice; + new_s = new DecimalFormat("###.##").format(price); + Bukkit.getConsoleSender().sendMessage("§6[现货交易所] §f农产品: "+dataAPI.getCropType()+" 状态: 跌 "+server_current+" ---> "+new_s+" "+getCurrenBFB(price,dataAPI.getMoney())); + } + double xian_jia_money = Double.parseDouble(new_s); + MarktStatsEvent event = new MarktStatsEvent(dataAPI, mk_stats,xian_jia_money); + Bukkit.getPluginManager().callEvent(event); + } + + public static String getCurrenBFB(double xianjia,double last){ + double change = getCAP(xianjia,last); + if(change >= 0) { + return new DecimalFormat("###.##").format(change) + "%"; + }else{ + return "§c"+new DecimalFormat("###.##").format(change)+ "%"; + } + } + + public static double getCAP(double xianjia,double last){ + double change = xianjia - last; + return (change / last) * 100; + } + +} diff --git a/src/main/java/me/Demon/DemonCropBuyShop/Event/MarktStatsEvent.java b/src/main/java/me/Demon/DemonCropBuyShop/Event/MarktStatsEvent.java new file mode 100644 index 0000000..9f207af --- /dev/null +++ b/src/main/java/me/Demon/DemonCropBuyShop/Event/MarktStatsEvent.java @@ -0,0 +1,46 @@ +package me.Demon.DemonCropBuyShop.Event; + +import me.Demon.DemonCropBuyShop.CropData; +import org.bukkit.entity.Player; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +public class MarktStatsEvent extends Event { + + private static HandlerList handlers = new HandlerList(); + private CropData dataAPI; + private int type; + private double money; + + @Override + public HandlerList getHandlers(){ + return handlers; + } + + public static HandlerList getHandlerList(){ + return handlers; + } + + public MarktStatsEvent(CropData dataAPI, int type, double money){ + this.dataAPI = dataAPI; + this.type = type; + this.money = money; + } + + public CropData getDataAPI() { + return dataAPI; + } + + public int getType() { + return type; + } + + public double getMoney() { + return money; + } + + @Override + public String getEventName() { + return super.getEventName(); + } +} diff --git a/src/main/java/me/Demon/DemonCropBuyShop/Event/PlayerCropBuyEvent.java b/src/main/java/me/Demon/DemonCropBuyShop/Event/PlayerCropBuyEvent.java new file mode 100644 index 0000000..d1c69b3 --- /dev/null +++ b/src/main/java/me/Demon/DemonCropBuyShop/Event/PlayerCropBuyEvent.java @@ -0,0 +1,57 @@ +package me.Demon.DemonCropBuyShop.Event; + +import org.bukkit.entity.Player; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +public class PlayerCropBuyEvent extends Event { + + private static HandlerList handlers = new HandlerList(); + private Player player; + private String Croptype; + private int Buyamount; + private int BuyMoney; + private int BuyTax; + + @Override + public HandlerList getHandlers(){ + return handlers; + } + + public static HandlerList getHandlerList(){ + return handlers; + } + + public PlayerCropBuyEvent(Player player, String Croptype, int Buyamount, int BuyMoney, int BuyTax){ + this.player = player; + this.Croptype = Croptype; + this.Buyamount = Buyamount; + this.BuyMoney = BuyMoney; + this.BuyTax = BuyTax; + } + + public Player getPlayer(){ + return player; + } + + public String getCroptype() { + return Croptype; + } + + public int getBuyamount() { + return Buyamount; + } + + public int getBuyMoney() { + return BuyMoney; + } + + public int getBuyTax() { + return BuyTax; + } + + @Override + public String getEventName() { + return super.getEventName(); + } +} diff --git a/src/main/java/me/Demon/DemonCropBuyShop/Main.java b/src/main/java/me/Demon/DemonCropBuyShop/Main.java new file mode 100644 index 0000000..916d804 --- /dev/null +++ b/src/main/java/me/Demon/DemonCropBuyShop/Main.java @@ -0,0 +1,82 @@ +package me.Demon.DemonCropBuyShop; + +import com.sun.scenario.effect.Crop; +import me.Demon.DemonCropBuyShop.listener.BuyGui; +import me.Demon.DemonCropBuyShop.listener.GuiListener; +import me.Demon.DemonCropBuyShop.listener.MarkStats; +import me.Demon.DemonCropBuyShop.listener.PlayerCropBuy; +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.plugin.java.JavaPlugin; + +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Random; + +public class Main extends JavaPlugin { + public static Main plugin; + public static int TodayBank = 0; + public static String prefix = "§7[§6战争领域§7] §f"; + public static LinkedHashMap dataAPI = new LinkedHashMap<>(); + + @Override + public void onEnable() { + plugin = this; + saveDefaultConfig(); + Bukkit.getServer().getPluginManager().registerEvents(new PlayerCropBuy(), plugin); + Bukkit.getServer().getPluginManager().registerEvents(new GuiListener(), plugin); + Bukkit.getServer().getPluginManager().registerEvents(new MarkStats(), plugin); + Bukkit.getConsoleSender().sendMessage("§e[农贸商店] §r正在启动插件..."); + for (String cropType : getConfig().getKeys(false)){ + if(getConfig().getString(cropType+".log") != null){ + dataAPI.put(cropType,new CropData(cropType)); + } + } + Bukkit.getConsoleSender().sendMessage("§e[农贸商店] §r载入农贸: "+dataAPI.size()+"个"); + long time = 20L * 60 * 30; + new TimeCheckRunnable().runTaskTimer(this, time, time); + if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) { + new Placeholders(this).register(); + } + } + + @Override + public void onDisable() { + for (String cropType : dataAPI.keySet()){ + Main.dataAPI.get(cropType).SaveData(); + } + Bukkit.getConsoleSender().sendMessage("§e[农贸商店] §r正在关闭插件..."); + } + + public boolean onCommand(CommandSender sender, Command cmd, String Command, String[] args) { + if (Command.equalsIgnoreCase("cropshop")) { + if(args.length == 1 && args[0].equalsIgnoreCase("open")){ + new BuyGui((Player) sender); + } + + if(args.length == 1 && args[0].equalsIgnoreCase("start") && sender.isOp()){ + for (String corpType : Main.dataAPI.keySet()) { + CropShopAPI.Start_Updata_Event(corpType,0); + } + sender.sendMessage("[日志-农贸商店] 倒计时结束随机市场调整."); + } + + if(args.length == 1 && args[0].equalsIgnoreCase("resetprice") && sender.isOp()){ + for (String corpType : Main.dataAPI.keySet()) { + CropShopAPI.Start_Updata_Event(corpType,0); + } + sender.sendMessage("[日志-农贸商店] 倒计时结束随机市场调整."); + } + + } + return false; + } + public static boolean itemIsNull(ItemStack item){return item == null || item.getType() == Material.AIR;} + public static boolean itemIsLore(ItemStack item){return item.getItemMeta().getLore() == null;} + public static int getRandomInt(int max,int min){return new Random().nextInt(max) + min;} +} + diff --git a/src/main/java/me/Demon/DemonCropBuyShop/Placeholders.java b/src/main/java/me/Demon/DemonCropBuyShop/Placeholders.java new file mode 100644 index 0000000..8835cf9 --- /dev/null +++ b/src/main/java/me/Demon/DemonCropBuyShop/Placeholders.java @@ -0,0 +1,61 @@ +package me.Demon.DemonCropBuyShop; + +import me.clip.placeholderapi.expansion.PlaceholderExpansion; +import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; + +public class Placeholders extends PlaceholderExpansion { + + private Plugin plugin; + public Placeholders(Plugin plugin) { + this.plugin = plugin; + } + @Override + public boolean persist() { + return true; + } + + @Override + public boolean canRegister() { + return true; + } + + @Override + public String getAuthor() { + return plugin.getDescription().getAuthors().toString(); + } + + @Override + public String getIdentifier() { + return "dcropshop"; + } + + + // %dcropshop_bfb_ndsad% + // %dcropshop_jg_ndsad% + @Override + public String getVersion() { + return plugin.getDescription().getVersion(); + } + + public String onPlaceholderRequest(Player p, String indentifier) {if (p == null) { return ""; } + String[] sp = indentifier.split("_"); + if (sp.length == 2) { + CropData dataAPI = Main.dataAPI.get(sp[1]); + if (sp[0].equalsIgnoreCase("jg")) { + return "" + dataAPI.getMoney(); + } else { + return "" + dataAPI.getCurrenBFB(); + } + }else if (sp.length == 3) { + String cropType = sp[1]+"_"+sp[2]; + CropData dataAPI = Main.dataAPI.get(cropType); + if (sp[0].equalsIgnoreCase("jg")) { + return "" + dataAPI.getMoney(); + } else { + return "" + dataAPI.getCurrenBFB(); + } + } + return "-999"; + } +} diff --git a/src/main/java/me/Demon/DemonCropBuyShop/TimeCheckRunnable.java b/src/main/java/me/Demon/DemonCropBuyShop/TimeCheckRunnable.java new file mode 100644 index 0000000..002d3a3 --- /dev/null +++ b/src/main/java/me/Demon/DemonCropBuyShop/TimeCheckRunnable.java @@ -0,0 +1,23 @@ +package me.Demon.DemonCropBuyShop; + +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.scheduler.BukkitRunnable; + +import java.util.HashMap; + +public class TimeCheckRunnable extends BukkitRunnable { + + HashMap onlineSeconds; + + public TimeCheckRunnable() { + this.onlineSeconds = new HashMap<>(); + } + + public void run() { + for (String corpType : Main.dataAPI.keySet()) { + CropShopAPI.Start_Updata_Event(corpType,0); + } + Bukkit.getConsoleSender().sendMessage("[日志-农贸商店] 倒计时结束随机市场调整."); + } +} diff --git a/src/main/java/me/Demon/DemonCropBuyShop/listener/BuyGui.java b/src/main/java/me/Demon/DemonCropBuyShop/listener/BuyGui.java new file mode 100644 index 0000000..5b8eb34 --- /dev/null +++ b/src/main/java/me/Demon/DemonCropBuyShop/listener/BuyGui.java @@ -0,0 +1,96 @@ +package me.Demon.DemonCropBuyShop.listener; + +import me.Demon.DemonCropBuyShop.CropData; +import me.Demon.DemonCropBuyShop.Main; +import me.Demon.DemonPlugin.DemonAPI; +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.entity.Player; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryHolder; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; + +import java.text.DecimalFormat; +import java.util.ArrayList; +import java.util.List; + +public class BuyGui implements InventoryHolder { + + private Inventory inventory; + + private Player player; + + public BuyGui(Player p){ + this.player = p; + inventory = Bukkit.createInventory(this,36,"战争领域 - 农贸市场"); + int slot = 9; + for (String itemType : Main.dataAPI.keySet()){ + CropData dataAPI = Main.dataAPI.get(itemType); + Material material = Material.valueOf(itemType); + ItemStack item = new ItemStack(material,1); + inventory.setItem(slot,shopitem(item,dataAPI)); + slot++; + } + for (int i = 27;i lore = new ArrayList<>(); + lore.add("§a§l★§7单位: §2组"); + // 计算现价和上一次变动价格的涨幅和跌幅 + double change = dataAPI.getCurrenAndPrevious(); + String show = new DecimalFormat("###.##").format(change); + + String m = new DecimalFormat("###.##").format(dataAPI.getMoney()); + if(change >= 0) { + lore.add("§d§l★§7现价: §6" + m+"元 §a(+"+show+"%)"); + }else{ + lore.add("§d§l★§7现价: §6" + m+"元 §c("+show+"%)"); + } + lore.add("§b§l★§7历史行情:"); + lore.addAll(dataAPI.getLog()); + lore.add("§8#祝老板成为农贸大亨!"); + lore.add(" "); + if (item.getType() == Material.EGG) { + lore.add("§a§l★§7左键点击 §f| §7出售16个"); + lore.add("§a§l★§7右键点击 §f| §7出售27组"); + }else{ + lore.add("§a§l★§7左键点击 §f| §7出售64个"); + lore.add("§a§l★§7右键点击 §f| §7出售27组"); + } + meta.setLore(lore); + item.setItemMeta(meta); + return item; + } + + public static ItemStack glass(String name,int date){ + ItemStack item = new ItemStack(160,1,(short) date); + ItemMeta meta = item.getItemMeta(); + meta.setDisplayName(name); + item.setItemMeta(meta); + return item; + } + public static ItemStack BackBook(String name){ + ItemStack item = new ItemStack(Material.BOOK); + ItemMeta meta = item.getItemMeta(); + meta.setDisplayName(name); + item.setItemMeta(meta); + return item; + } + + @Override + public Inventory getInventory() { + return null; + } +} diff --git a/src/main/java/me/Demon/DemonCropBuyShop/listener/GuiListener.java b/src/main/java/me/Demon/DemonCropBuyShop/listener/GuiListener.java new file mode 100644 index 0000000..f27bf9a --- /dev/null +++ b/src/main/java/me/Demon/DemonCropBuyShop/listener/GuiListener.java @@ -0,0 +1,127 @@ +package me.Demon.DemonCropBuyShop.listener; + +import me.Demon.DemonCropBuyShop.CropData; +import me.Demon.DemonCropBuyShop.Event.PlayerCropBuyEvent; +import me.Demon.DemonCropBuyShop.Main; +import me.Demon.DemonPlugin.DemonAPI; +import me.skymc.taboomenu.TabooMenuAPI; +import org.bukkit.Bukkit; +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.ClickType; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; + +import java.text.DecimalFormat; + +public class GuiListener implements Listener { + + @EventHandler + public void onclick(InventoryClickEvent e){ + Player p = (Player) e.getWhoClicked(); + if(e.getInventory().getHolder() != null && e.getInventory().getHolder() instanceof BuyGui){ + e.setCancelled(true); + int rawSlot = e.getRawSlot(); + if(rawSlot <= 0 || rawSlot >= e.getInventory().getSize()){ + return; + } + if(rawSlot == 31){ + TabooMenuAPI.openMenu(p,"商城菜单",true); + return; + } + if(Main.itemIsNull(e.getCurrentItem()) && Main.itemIsLore(e.getCurrentItem())){ + return; + } + String CorpType = e.getCurrentItem().getType().name(); + if (e.getClick() == ClickType.LEFT) { + if(e.getCurrentItem().getType() == Material.EGG) { + SellItemsEvant(p,CorpType, 16, 1); + } else { + SellItemsEvant(p, CorpType, 64, 1); + } + } + if (e.getClick() == ClickType.RIGHT) { + if(e.getCurrentItem().getType() == Material.EGG) { + SellItemsEvant(p, CorpType, 16*27, 2); + }else{ + SellItemsEvant(p, CorpType, 1728, 2); + } + } + } + } + + public static void SellItemsEvant(Player p, String CropType, int sellAmount, int type){ + if(Main.TodayBank >= 30000000){ + p.sendMessage(Main.prefix+"农贸市场每日仅回收3000万金币的农作物."); + p.playSound(p.getLocation(), Sound.ENTITY_VILLAGER_NO, 1, 2); + return; + } + CropData dataAPI = Main.dataAPI.get(CropType); + //检测玩家是否拥有这么多的物品出售 + ItemStack item = dataAPI.getItem(); + int PlayerAmount = getInvAmount(p,item); + if(PlayerAmount < sellAmount){ + p.sendMessage(Main.prefix+"你没有足够的物品."); + p.playSound(p.getLocation(), Sound.ENTITY_VILLAGER_NO, 1, 2); + return; + } + SellItemEvent(p.getInventory(),PlayerAmount,item,sellAmount); + int price = (int) dataAPI.getMoney(); + if(type == 2){ + price = price * 27; + } + double tax = 0.05; + if(p.hasPermission("vip2.use")){ + tax = 0.02; + }else if(p.hasPermission("vip1.use")){ + tax = 0.03; + } + price = (int) (price - (price * tax)); + double taxMoney = price * tax; + DemonAPI.giveMoney(p, price); + Main.TodayBank = Main.TodayBank + price; + p.sendMessage(Main.prefix + "你获得了: §f$" + new DecimalFormat(",###.##").format(price) + "金币 §6(纳税: " + new DecimalFormat("#.##").format(taxMoney) + "金币)"); + p.updateInventory(); + p.playSound(p.getLocation(), Sound.BLOCK_COMPARATOR_CLICK, 1, 2); + PlayerCropBuyEvent event = new PlayerCropBuyEvent(p, dataAPI.getCropType(), sellAmount,price,(int) taxMoney); + Bukkit.getPluginManager().callEvent(event); + new BuyGui(p); + } + + public static int getInvAmount(Player p, ItemStack item){ + int amount = 0; + Inventory inv = p.getInventory(); + Material material = item.getType(); + for (int i = 0;i < 36;i++){ + ItemStack items = inv.getItem(i); + if(!Main.itemIsNull(items) && Main.itemIsLore(items)){ + Material inv_type = items.getType(); + if(inv_type == material){ + amount = amount + items.getAmount(); + } + } + } + return amount; + } + public static void SellItemEvent(Inventory inv,int playerAmount,ItemStack item,int sellAmount){ + int last = playerAmount - sellAmount; + for (int i = 0;i < 36;i++){ + ItemStack items = inv.getItem(i); + if(!Main.itemIsNull(items) && Main.itemIsLore(items)){ + Material inv_type = items.getType(); + if(inv_type == item.getType()){ + inv.setItem(i, new ItemStack(Material.AIR)); + } + } + } + if(last >= 1) { + ItemStack newItem = new ItemStack(item.getType()); + newItem.setAmount(last); + inv.addItem(newItem); + } + } +} diff --git a/src/main/java/me/Demon/DemonCropBuyShop/listener/MarkStats.java b/src/main/java/me/Demon/DemonCropBuyShop/listener/MarkStats.java new file mode 100644 index 0000000..2773fc0 --- /dev/null +++ b/src/main/java/me/Demon/DemonCropBuyShop/listener/MarkStats.java @@ -0,0 +1,64 @@ +package me.Demon.DemonCropBuyShop.listener; + +import me.Demon.DemonCropBuyShop.CropData; +import me.Demon.DemonCropBuyShop.Event.MarktStatsEvent; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; + +import java.text.DecimalFormat; + +public class MarkStats implements Listener { + + @EventHandler + public void markStats(MarktStatsEvent e){ + CropData dataAPI = e.getDataAPI(); + // 刷新现在价格 + double current_price = dataAPI.getMoney(); + // 判断本次事件是涨还是跌 + int mk_stats = e.getType(); + boolean butt = false; + if(mk_stats == 1){ + if(!isMoneyUpLimit(dataAPI.getDmoney(),current_price)){ + butt = true; + } + } else { + if(!isMoneyDownLimit(dataAPI.getDmoney(),current_price)){ + butt = true; + } + } + if(butt){ + // 首先记录当前价格 + double change = dataAPI.getCurrenAndPrevious(); + String show = new DecimalFormat("###.##").format(change); + if (change >= 0) { + String str_lore = " §f- §6" + dataAPI.getMoney() + " §a§l↑↑ §a" + show + "%"; + dataAPI.setCropMarketLog(str_lore); + } else { + String str_lore = " §f- §6" + dataAPI.getMoney() + " §c§l↓↓ §c" + show + "%"; + dataAPI.setCropMarketLog(str_lore); + } + // 设置上次价格 + dataAPI.setLast(current_price); + // 设置当前价格 + dataAPI.setMoney(e.getMoney()); + } + } + + + public static boolean isMoneyUpLimit(double Dmoney,double price){ + // Dmoney = 10000 * 1.3 = 13000; + // Bukkit.getConsoleSender().sendMessage("price = "+price+" default = "+Dmoney); + if(price >= (Dmoney * 1.5)){ + return true; + } + return false; + } + public static boolean isMoneyDownLimit(double Dmoney,double price){ + // Dmoney = 10000 * 1.3 = 13000; + // Bukkit.getConsoleSender().sendMessage("price = "+price+" default = "+Dmoney); + if(price <= (Dmoney * 0.3)){ + return true; + } + return false; + } +} diff --git a/src/main/java/me/Demon/DemonCropBuyShop/listener/PlayerCropBuy.java b/src/main/java/me/Demon/DemonCropBuyShop/listener/PlayerCropBuy.java new file mode 100644 index 0000000..3fef70f --- /dev/null +++ b/src/main/java/me/Demon/DemonCropBuyShop/listener/PlayerCropBuy.java @@ -0,0 +1,54 @@ +package me.Demon.DemonCropBuyShop.listener; + +import me.Demon.DemonCropBuyShop.CropShopAPI; +import me.Demon.DemonCropBuyShop.Event.PlayerCropBuyEvent; +import me.Demon.DemonCropBuyShop.Main; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; + +public class PlayerCropBuy implements Listener { + + @EventHandler + public void buyCrop(PlayerCropBuyEvent e){ + Player p = e.getPlayer(); + // 判断农作物出售数量是否大于27组~ + String cropType = e.getCroptype(); + CropShopAPI.addCropBuyAmount(cropType,e.getBuyamount()); + int sellAmount = CropShopAPI.getCropBuyAmount(cropType); + // 若大于27组则执行市场变动 + if(sellAmount >= (getCropLimit(cropType) * 2)){ + for (String crop : Main.dataAPI.keySet()){ + if(crop.equalsIgnoreCase(cropType)){ + CropShopAPI.Start_Updata_Event(cropType,2); + CropShopAPI.setCropBuyAmount(cropType,0); + } else { + CropShopAPI.Start_Updata_Event(crop,1); + } + } + } + } + + public static int getCropLimit(String Crop){ + if(Crop.equalsIgnoreCase("WHEAT")){ + return 27 * 64; + }else if(Crop.equalsIgnoreCase("POTATO_ITEM")){ + return 48 * 64; + }else if(Crop.equalsIgnoreCase("CARROT_ITEM")){ + return 48 * 64; + }else if(Crop.equalsIgnoreCase("MELON")){ + return 342 * 64; + }else if(Crop.equalsIgnoreCase("PUMPKIN")){ + return 57 * 64; + }else if(Crop.equalsIgnoreCase("CACTUS")){ + return 40 * 64; + }else if(Crop.equalsIgnoreCase("SUGAR_CANE")){ + return 162 * 64; + }else if(Crop.equalsIgnoreCase("NETHER_STALK")){ + return 57 * 64; + }else if(Crop.equalsIgnoreCase("EGG")){ + return 70 * 16; + } + return 27 * 64; + } +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml new file mode 100644 index 0000000..9ef6b73 --- /dev/null +++ b/src/main/resources/config.yml @@ -0,0 +1,68 @@ +WHEAT: + default: 2280.0 + money: 2280.37 + last: 2280.64 + log: + - ' §f- §63209.64 §c§l↓↓ §c-1.36%' + - ' §f- §63253.77 §a§l↑↑ §a3.11%' + - ' §f- §63155.66 §c§l↓↓ §c-3%' + - ' §f- §63253.16 §a§l↑↑ §a1.32%' + - ' §f- §63210.76 §c§l↓↓ §c-4.08%' +POTATO_ITEM: + default: 1280.0 + money: 1280.37 + last: 1280.64 + log: + - ' §f- §61280.64 §c§l↓↓ §c-1.36%' +CARROT_ITEM: + default: 1280.0 + money: 1280.37 + last: 1280.64 + log: + - ' §f- §61280.64 §c§l↓↓ §c-1.36%' +MELON: + default: 180.0 + money: 180.37 + last: 180.64 + log: + - ' §f- §6180.64 §c§l↓↓ §c-1.36%' +PUMPKIN: + default: 1080.0 + money: 1080.37 + last: 1080.64 + log: + - ' §f- §61080.64 §c§l↓↓ §c-1.36%' +CACTUS: + default: 1080.0 + money: 1080.37 + last: 1080.64 + log: + - ' §f- §61080.64 §c§l↓↓ §c-1.36%' +SUGAR_CANE: + default: 380.0 + money: 380.37 + last: 380.64 + log: + - ' §f- §6380.64 §c§l↓↓ §c-1.36%' +NETHER_STALK: + default: 1080.0 + money: 1080.37 + last: 1080.64 + log: + - ' §f- §6380.64 §c§l↓↓ §c-1.36%' +EGG: + default: 880.0 + money: 880.37 + last: 880.64 + log: + - ' §f- §6380.64 §c§l↓↓ §c-1.36%' +BuyData: + WHEAT: 1 + POTATO_ITEM: 1 + CARROT_ITEM: 1 + MELON: 1 + PUMPKIN: 1 + CACTUS: 1 + SUGAR_CANE: 1 + NETHER_STALK: 1 + EGG: 1 diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..1de179e --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,5 @@ +name: DemonCropBuyShop +main: me.Demon.DemonCropBuyShop.Main +version: 1.0.2 +commands: + cropshop: \ No newline at end of file