测试版

This commit is contained in:
tianyu 2024-07-24 07:00:58 +08:00
commit 1d6b7d242e
9 changed files with 626 additions and 0 deletions

41
.gitignore vendored Normal file
View File

@ -0,0 +1,41 @@
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
/out/
/libs/
/.idea/

37
pom.xml Normal file
View File

@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>DemonShop</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<repositories>
<repository>
<id>public-rpg</id>
<url>https://repo.aurora-pixels.com/repository/public-rpg/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.12.2</version>
</dependency>
<dependency>
<groupId>me.Demon.DemonPlugin</groupId>
<artifactId>DemonAPI</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,34 @@
package me.Demon.DemonShop.FileConfig;
import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File;
import java.io.IOException;
public class PlanData {
public static File file;
public PlanData(String name){
File plans = new File("./plugins/DemonShop","商店配置");
plans.mkdir();
file = new File(plans,name+".yml");
try {
file.createNewFile();
}catch (IOException e){
e.printStackTrace();
}
}
public File getFile(){
return file;
}
public void save(){
try {
YamlConfiguration.loadConfiguration(file).save(file);
}catch (IOException e){
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,141 @@
package me.Demon.DemonShop;
import me.Demon.DemonPlugin.DemonAPI;
import me.Demon.DemonShop.gui.CreateGui;
import me.Demon.DemonShop.gui.ShopGui;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
public class Main extends JavaPlugin implements Listener {
public static Main plugin;
public static String prefix = "§7[§6商店§7] §f";
public static List<String> file_Name = new ArrayList<>();
public static HashMap<Player, ItemStack> hashItems = new HashMap<>();
public static HashMap<Player, Integer> hashPrice = new HashMap<>();
public static HashMap<Player, Integer> hashAmount = new HashMap<>();
@Override
public void onEnable() {
plugin = this;
saveDefaultConfig();
Bukkit.getConsoleSender().sendMessage("§e[DemonShop] §r正在启动插件...");
getServer().getPluginManager().registerEvents(this, this);
Bukkit.getServer().getPluginManager().registerEvents(new CreateGui(), plugin);
Bukkit.getServer().getPluginManager().registerEvents(new ShopGui(), plugin);
loadFile_NameData();
}
public void loadFile_NameData() {
File plans = new File("./plugins/DemonShop", "商店配置");
File[] files = plans.listFiles();
File[] arrayOfFile1;
int j = (Objects.requireNonNull(arrayOfFile1 = files)).length;
for (int i = 0; i < j; i++) {
File s = arrayOfFile1[i];
file_Name.add(s.getName().replace(".yml", ""));
}
}
public void onDisable() {
Bukkit.getConsoleSender().sendMessage("§e[DemonShop] §r正在关闭插件...");
}
public boolean onCommand(CommandSender sender, Command cmd, String Command, String[] args) {
if (Command.equalsIgnoreCase("dshop")) {
if (args.length == 3 && args[0].equalsIgnoreCase("open")) {
String key = args[1];
if (!file_Name.contains(key)) {
sender.sendMessage("该商店不存在,请联系[客服]进行修复.");
return true;
}
File file = new File("plugins/DemonShop/商店配置", key + ".yml");
YamlConfiguration yml = YamlConfiguration.loadConfiguration(file);
Player p = Bukkit.getPlayer(args[2]);
if (p != null) {
ShopGui.OpenGui(p, key, yml);
} else {
sender.sendMessage("目标玩家不在线.");
}
}
if (args.length == 2 && args[0].equalsIgnoreCase("open")) {
String key = args[1];
if (!file_Name.contains(key)) {
sender.sendMessage("该商店不存在,请联系[客服]进行修复.");
return true;
}
File file = new File("plugins/DemonShop/商店配置", key + ".yml");
YamlConfiguration yml = YamlConfiguration.loadConfiguration(file);
ShopGui.OpenGui((Player) sender, key, yml);
}
}
if (Command.equalsIgnoreCase("dshops") && sender.isOp()) {
if (args.length == 0) {
sender.sendMessage("/dshops [代号] [价格] [数量]");
}
if (args.length == 1 && args[0].equalsIgnoreCase("reload")) {
file_Name.clear();
loadFile_NameData();
sender.sendMessage(prefix + "重载完成.");
}
if (args.length == 3) {
Player p = (Player) sender;
String key = args[0];
File file = getFilePath(key);
if (file == null) {
sender.sendMessage("请检查该文件配置是否存在.");
return true;
}
YamlConfiguration yml = YamlConfiguration.loadConfiguration(file);
ItemStack item = p.getInventory().getItemInHand();
if (!DemonAPI.itemIsNull(item) && !DemonAPI.itemIsLore(item)) {
int price = Integer.parseInt(args[1]);
int amount = Integer.parseInt(args[2]);
hashItems.put(p, item);
hashPrice.put(p, price);
hashAmount.put(p, amount);
CreateGui.OpenGui(p, key, yml);
} else {
sender.sendMessage(prefix + "手持物品出现错误,请检查后再次使用.");
}
}
}
return false;
}
public static File getFilePath(String name) {
File plans = new File("./plugins/DemonShop", "商店配置");
File[] files = plans.listFiles();
File[] arrayOfFile1;
int j = (Objects.requireNonNull(arrayOfFile1 = files)).length;
for (int i = 0; i < j; i++) {
File s = arrayOfFile1[i];
if (s.getName().equals(name + ".yml"))
return s;
}
return null;
}
public static void saveFileConfig(YamlConfiguration yml, File file) {
try {
yml.save(file);
} catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,65 @@
package me.Demon.DemonShop.gui;
import me.Demon.DemonShop.Main;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.YamlConfiguration;
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.Inventory;
import org.bukkit.inventory.ItemStack;
import java.io.File;
public class CreateGui implements Listener {
public static void OpenGui(Player p, String key, YamlConfiguration yml) {
Inventory inv = Bukkit.createInventory(null, 54, "§1§2§3§r正在设置商店: " + key);
if (yml.getString("DataShop") != null) {
for (String keySlow : yml.getConfigurationSection("DataShop").getKeys(false)) {
int slot = Integer.parseInt(keySlow);
ItemStack item = yml.getItemStack("DataShop." + keySlow + ".items").clone();
inv.setItem(slot, item);
}
}
p.openInventory(inv);
}
@EventHandler
public void onclick(InventoryClickEvent e) {
Player p = (Player) e.getWhoClicked();
if (e.getView().getTitle().contains("§1§2§3§r正在设置商店: ")) {
e.setCancelled(true);
String key = e.getView().getTitle().replace("§1§2§3§r正在设置商店: ", "");
if (e.getRawSlot() >= 0 && e.getRawSlot() <= 54) {
File file = Main.getFilePath(key);
if (file != null) {
YamlConfiguration yml = YamlConfiguration.loadConfiguration(file);
if (Main.hashPrice.get(p) == null) {
p.closeInventory();
return;
}
int price = Main.hashPrice.get(p);
int amount = Main.hashAmount.get(p);
ItemStack item = Main.hashItems.get(p);
item.setAmount(1);
yml.set("DataShop." + e.getRawSlot() + ".VaultType", "Points");
yml.set("DataShop." + e.getRawSlot() + ".price", price);
yml.set("DataShop." + e.getRawSlot() + ".amount", amount);
yml.set("DataShop." + e.getRawSlot() + ".items", item);
Main.saveFileConfig(yml, file);
RemoveHash();
p.sendMessage("设置成功 " + key);
OpenGui(p, key, yml);
}
}
}
}
public static void RemoveHash() {
Main.hashPrice.clear();
Main.hashAmount.clear();
Main.hashItems.clear();
}
}

View File

@ -0,0 +1,259 @@
package me.Demon.DemonShop.gui;
import me.Demon.DemonCoins.util.CoinsAPI;
import me.Demon.DemonPlugin.DemonAPI;
import me.Demon.DemonShop.Main;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.configuration.file.YamlConfiguration;
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.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class ShopGui implements Listener {
public static String buyItem_Lore = "§a§l✔§7SHIFT+左键点击购买§s§h§o§p";
@EventHandler
public void onclick(InventoryClickEvent e) {
Player p = (Player) e.getWhoClicked();
if (e.getView().getTitle().contains("§d§s§h§o§p§r")) {
e.setCancelled(true);
String key = e.getView().getTitle().replace("§d§s§h§o§p§r", "");
if (e.getRawSlot() >= 0 && e.getRawSlot() < e.getInventory().getSize()) {
ItemStack click_item = e.getCurrentItem();
if (DemonAPI.itemIsNull(click_item)) {
return;
}
if (DemonAPI.itemIsLore(click_item)) {
return;
}
p.playSound(p.getLocation(), Sound.BLOCK_LEVER_CLICK, 1, 2);
int keySlot = e.getRawSlot();
if (!Main.file_Name.contains(key)) {
return;
}
File file = new File("plugins/DemonShop/商店配置", key + ".yml");
YamlConfiguration yml = YamlConfiguration.loadConfiguration(file);
if (click_item.getType() == Material.BOOK) {
ItemMeta meta = click_item.getItemMeta();
if (meta.getDisplayName().contains("§a返回菜单")) {
String cmd = yml.getString("BackMenu.cmd");
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), cmd.replace("%p%", p.getName()));
return;
}
}
String str = "DataShop." + keySlot + ".";
String VaultType = yml.getString(str + "VaultType");
int price = yml.getInt(str + "price");
int amount = yml.getInt(str + "amount");
ItemStack item = yml.getItemStack(str + "items").clone();
item.setAmount(amount);
boolean limit_butt = yml.getBoolean(str + "limit_butt");
//获取商品剩余数量
int limit_amount = yml.getInt("DataShop." + keySlot + ".limit_amount");
if (limit_butt && e.getClick() == ClickType.SHIFT_RIGHT && p.isOp()) {
int limit_amount_max = yml.getInt("DataShop." + keySlot + ".limit_amount_max");
yml.set("DataShop." + keySlot + ".limit_amount", limit_amount_max);
Main.saveFileConfig(yml, file);
p.sendMessage(Main.prefix + "成功为商品补货!");
OpenGui(p, key, yml);
return;
}
if (e.getClick() == ClickType.SHIFT_LEFT) {
//检测该商品是否是限量购买
if (limit_butt) {
//获取商品剩余数量是否大于1
if (limit_amount <= 0) {
p.sendMessage(Main.prefix + "该商品已被其他玩家抢购一空...");
return;
}
}
if (VaultType.equalsIgnoreCase("points")) {
if (DemonAPI.hasPoints(p, price)) {
DemonAPI.takePoints(p, price);
if (yml.getString(str + "Command") != null) {
String commands = yml.getString(str + "Command").replace("%p%", p.getName());
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), commands);
} else {
p.getInventory().addItem(item);
}
p.sendMessage(Main.prefix + "你花费了§d" + price + "点券§f购买了 " + item.getItemMeta().getDisplayName() + "§7§lx§f§l" + amount);
CheckLimitItems(p, limit_butt, keySlot, yml, file, limit_amount, key);
if (price >= 100) {
if (amount == 1) {
Bukkit.broadcastMessage("§7[§6战争领域§7] §f玩家§6" + p.getName() + "§f在" + e.getView().getTitle() + "中购买了 " + item.getItemMeta().getDisplayName());
} else {
Bukkit.broadcastMessage("§7[§6战争领域§7] §f玩家§6" + p.getName() + "§f在" + e.getView().getTitle() + "中购买了 " + item.getItemMeta().getDisplayName() + "§7§lx§f§l" + amount);
}
}
} else {
p.sendMessage(Main.prefix + "你需要§d" + price + "§f点券才能操作.");
}
} else if (VaultType.equalsIgnoreCase("money")) {
if (DemonAPI.hasMoney(p, price)) {
DemonAPI.takeMoney(p, price);
if (yml.getString(str + "Command") != null) {
String commands = yml.getString(str + "Command").replace("%p%", p.getName());
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), commands);
} else {
p.getInventory().addItem(item);
}
p.sendMessage(Main.prefix + "你花费了§6" + price + "金币§f购买了 " + item.getItemMeta().getDisplayName() + "§7§lx§f§l" + amount);
CheckLimitItems(p, limit_butt, keySlot, yml, file, limit_amount, key);
} else {
p.sendMessage(Main.prefix + "你需要§6$" + price + "§f金币才能操作.");
}
} else if (VaultType.equalsIgnoreCase("coins")) {
if (CoinsAPI.getCoinsAmount(p.getName()) >= price) {
CoinsAPI.removeCoins(p.getName(), price);
if (yml.getString(str + "Command") != null) {
String commands = yml.getString(str + "Command").replace("%p%", p.getName());
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), commands);
} else {
p.getInventory().addItem(item);
}
p.sendMessage(Main.prefix + "你花费了§b" + price + "软妹币§f购买了 " + item.getItemMeta().getDisplayName() + "§7§lx§f§l" + amount);
CheckLimitItems(p, limit_butt, keySlot, yml, file, limit_amount, key);
if (price >= 10) {
if (amount == 1) {
Bukkit.broadcastMessage("§7[§6战争领域§7] §f玩家§6" + p.getName() + "§f在" + e.getView().getTitle() + "中购买了 " + item.getItemMeta().getDisplayName());
} else {
Bukkit.broadcastMessage("§7[§6战争领域§7] §f玩家§6" + p.getName() + "§f在" + e.getView().getTitle() + "中购买了 " + item.getItemMeta().getDisplayName() + "§7§lx§f§l" + amount);
}
}
} else {
p.sendMessage(Main.prefix + "你需要§b" + price + "§f软妹币才能操作.");
}
}
}
}
}
}
public static void takeShopAmount(int KeySlot, YamlConfiguration yml, File file, int limit_amount) {
yml.set("DataShop." + KeySlot + ".limit_amount", (limit_amount - 1));
Main.saveFileConfig(yml, file);
}
public static void CheckLimitItems(Player p, boolean limit_butt, int keySlot, YamlConfiguration yml, File file, int limit_amount, String key) {
if (limit_butt) {
takeShopAmount(keySlot, yml, file, limit_amount);
OpenGui(p, key, yml);
}
}
public static ItemStack getGlass_Pane(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 void OpenGui(Player p, String key, YamlConfiguration yml) {
int Guisize = yml.getInt("Guisize");
Inventory inv = Bukkit.createInventory(null, Guisize, "§d§s§h§o§p§r" + key);
if (yml.getString("DataShop") != null) {
String Type = yml.getString("Type");
if (!Type.equalsIgnoreCase("money")) {
for (int i = 0; i < inv.getSize(); i++) {
int date = (int) (Math.random() * 6.0D);
inv.setItem(i, getGlass_Pane("§r", date));
}
}
if (yml.getString("BackMenu") != null) {
int slot = yml.getInt("BackMenu.slot");
inv.setItem(slot, BackMenu());
}
for (String keySlow : yml.getConfigurationSection("DataShop").getKeys(false)) {
int slot = Integer.parseInt(keySlow);
ItemStack item = yml.getItemStack("DataShop." + keySlow + ".items").clone();
int amount = yml.getInt("DataShop." + keySlow + ".amount");
int price = yml.getInt("DataShop." + keySlow + ".price");
boolean limit_butt = yml.getBoolean("DataShop." + keySlow + ".limit_butt");
String VaultType = yml.getString("DataShop." + keySlow + ".VaultType");
if (limit_butt) {
int limit_amount = yml.getInt("DataShop." + keySlow + ".limit_amount");
int limit_amount_max = yml.getInt("DataShop." + keySlow + ".limit_amount_max");
inv.setItem(slot, ShopItems_limit(item, amount, price, limit_amount, limit_amount_max, VaultType));
} else {
inv.setItem(slot, ShopItems_Nolimit(item, amount, price, VaultType));
}
}
}
p.openInventory(inv);
p.playSound(p.getLocation(), Sound.BLOCK_CHEST_OPEN, 1, 1);
}
public static ItemStack ShopItems_limit(ItemStack items, int amount, int price, int limit_amount, int limit_amount_max, String VaultType) {
ItemStack item = items.clone();
item.setAmount(amount);
item.setDurability(items.getDurability());
ItemMeta meta = item.getItemMeta();
List<String> lore = new ArrayList<>();
if (VaultType.equalsIgnoreCase("points")) {
lore.add("§7§l★§7单买价格: §d" + price + "点券");
} else if (VaultType.equalsIgnoreCase("money")) {
lore.add("§7§l★§7单买价格: §6" + price + "金币");
} else if (VaultType.equalsIgnoreCase("coins")) {
lore.add("§7§l★§7单买价格: §b" + price + "软妹币");
}
lore.add("§7§l★§7限量出售: §b" + limit_amount + " §7/ §b" + limit_amount_max);
lore.add(buyItem_Lore);
lore.add("§7-------------------------§b§u§y§r");
lore.addAll(items.getItemMeta().getLore());
meta.setLore(lore);
meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
meta.addItemFlags(ItemFlag.HIDE_UNBREAKABLE);
item.setItemMeta(meta);
return item;
}
public static ItemStack ShopItems_Nolimit(ItemStack items, int amount, int price, String VaultType) {
ItemStack item = items.clone();
item.setAmount(amount);
item.setDurability(items.getDurability());
ItemMeta meta = item.getItemMeta();
List<String> lore = new ArrayList<>();
if (VaultType.equalsIgnoreCase("points")) {
lore.add("§7§l★§7单买价格: §d" + price + "点券");
} else if (VaultType.equalsIgnoreCase("money")) {
lore.add("§7§l★§7单买价格: §6" + price + "金币");
} else if (VaultType.equalsIgnoreCase("coins")) {
lore.add("§7§l★§7单买价格: §b" + price + "软妹币");
}
lore.add(buyItem_Lore);
lore.add("§7-------------------------§b§u§y§r");
lore.addAll(items.getItemMeta().getLore());
meta.setLore(lore);
meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
meta.addItemFlags(ItemFlag.HIDE_UNBREAKABLE);
item.setItemMeta(meta);
return item;
}
public static ItemStack BackMenu() {
ItemStack item = new ItemStack(Material.BOOK);
ItemMeta meta = item.getItemMeta();
meta.setDisplayName("§a返回菜单");
List<String> lore = new ArrayList<>();
lore.add("§7§l★§7返回商城菜单");
meta.setLore(lore);
item.setItemMeta(meta);
return item;
}
}

View File

View File

@ -0,0 +1,6 @@
name: DemonShop
main: me.Demon.DemonShop.Main
version: 1.0.1
commands:
dshops:
dshop:

View File

@ -0,0 +1,43 @@
BackMenu:
slot: 1
cmd: 'cc open menu'
VaultType: Points
Guisize: 27
DataShop:
'0':
limit_butt: true
limit_amount: 100
limit_amount_max: 100
price: 5
amount: 1
items:
==: org.bukkit.inventory.ItemStack
type: PAPER
meta:
==: ItemMeta
meta-type: UNSPECIFIC
display-name: §6§l大斗魂奖章
lore:
- §a§l★§7在§e魂师竞技场§7中兑换获得
- §7在大陆中用处十分广泛
- §r
- '§7使用方法:'
- §7用于打造各种装备道具
internal: H4sIAAAAAAAAAONiYOBgYCkuSa1g4E5OzMzJTMyPNzQwZAAAMqktbxgAAAA=
'2':
price: 8
amount: 1
items:
==: org.bukkit.inventory.ItemStack
type: PAPER
meta:
==: ItemMeta
meta-type: UNSPECIFIC
display-name: §6§l大斗魂奖章
lore:
- §a§l★§7在§e魂师竞技场§7中兑换获得
- §7在大陆中用处十分广泛
- §r
- '§7使用方法:'
- §7用于打造各种装备道具
internal: H4sIAAAAAAAAAONiYOBgYCkuSa1g4E5OzMzJTMyPNzQwZAAAMqktbxgAAAA=