This commit is contained in:
yaohunya 2024-08-22 00:55:39 +08:00
commit 1b6efaf131
10 changed files with 446 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/.idea/
/out/

BIN
lib/AuItemStackLibrary.jar Normal file

Binary file not shown.

BIN
lib/DemonCSGO.jar Normal file

Binary file not shown.

View File

@ -0,0 +1,57 @@
package com.yaohun.crazyprizepool;
import com.yaohun.crazyprizepool.data.PoolData;
import com.yaohun.crazyprizepool.listener.OpenBoxEvent;
import com.yaohun.crazyprizepool.manager.PoolManager;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin {
private Main plugin;
private String prefix = "§7[§6四叶草§7] §f";
private PoolManager poolManager;
@Override
public void onEnable() {
plugin = this;
poolManager = new PoolManager(this);
getServer().getPluginManager().registerEvents(new OpenBoxEvent(this),this);
}
@Override
public void onDisable() {
poolManager.SavePlayerData();
}
@Override
public boolean onCommand(CommandSender sender, Command cmd, String Command, String[] args) {
if (Command.equalsIgnoreCase("crazypool") && sender.isOp()) {
if (args.length == 1) {
String fileName = args[0];
PoolData poolData = poolManager.getPoolData(fileName);
if (poolData == null) {
return true;
}
Player player = (Player) sender;
player.getInventory().addItem(poolData.getItem());
}
}
return true;
}
public PoolManager getPoolManager() {
return poolManager;
}
public Main getPlugin() {
return plugin;
}
public String getPrefix() {
return prefix;
}
}

View File

@ -0,0 +1,73 @@
package com.yaohun.crazyprizepool.data;
import com.yaohun.itemlibrary.api.ItemKuAPI;
import me.Demon.DemonPlugin.DemonAPI;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.ItemStack;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
public class ContentData {
private String poolKey;
private Map<String, ItemStack> stackMap = new HashMap<>();
private Map<String, Double> chanceMap = new HashMap<>();
public ContentData(String poolKey, ConfigurationSection section) {
ConfigurationSection section1 = section.getConfigurationSection(poolKey);
for (String contentKey : section1.getKeys(false)) {
double chance = section1.getDouble(contentKey + ".chance");
if(chance >= 0.1) {
chanceMap.put(contentKey, chance);
int itemAmount = section1.getInt(contentKey + ".amount");
String itemKey = section1.getString(contentKey + ".item");
if (itemKey.contains("ItemKu#")) {
String[] strings = itemKey.split("#");
String stackKey = strings[1];
ItemStack stack = ItemKuAPI.getItems(stackKey, itemAmount);
stackMap.put(contentKey, stack);
} else if (itemKey.contains("CSGO#")) {
String[] strings = itemKey.split("#");
String stackKey = strings[1];
ItemStack stack = me.Demon.DemonCSGO.Api.CratesAPI.getCratesItemStack(stackKey, itemAmount);
stackMap.put(contentKey, stack);
} else {
ItemStack stack = section1.getItemStack(contentKey + ".item");
stack.setAmount(itemAmount);
stackMap.put(contentKey, stack);
}
}
}
}
public ItemStack getCratesStack(String contentKey){
if(this.stackMap.get(contentKey) == null){
return null;
}
return this.stackMap.get(contentKey);
}
public String Start_CratesKey(){
Map<String, Double> chanceMap = new HashMap<>();
int a = 0;
for (String contentKey : this.chanceMap.keySet()){
double chance = this.chanceMap.get(contentKey);
chanceMap.put(contentKey,chance);
a += chance;
}
String CratesKey = null;
Random ran = new Random();
ran.setSeed(System.currentTimeMillis()+ DemonAPI.getRandomInt(80000,1000));
int b = ran.nextInt(a);
for (String rewardKey : this.chanceMap.keySet()){
if(chanceMap.get(rewardKey) > b){
CratesKey = rewardKey;
break;
}
b -= chanceMap.get(rewardKey);
}
return CratesKey;
}
}

View File

@ -0,0 +1,102 @@
package com.yaohun.crazyprizepool.data;
import com.yaohun.crazyprizepool.Main;
import de.tr7zw.itemnbtapi.NBTItem;
import me.Demon.DemonPlugin.DemonAPI;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.ItemStack;
import java.io.File;
import java.util.*;
public class PoolData {
private String name;
private ItemStack item;
private Map<Integer, Map<String, Double>> poolChanceMap;
private Map<String, ContentData> contentMap;
public PoolData(Main plugin,String fileName){
File file = new File(plugin.getDataFolder(),fileName+".yml");
FileConfiguration yml = YamlConfiguration.loadConfiguration(file);
this.name = yml.getString("PoolName",fileName);
this.item = yml.getItemStack("PoolItem");
Map<String, ContentData> contentDataMap = new HashMap<>();
ConfigurationSection section = yml.getConfigurationSection("PoolContent");
for (String poolKey : section.getKeys(false)) {
contentDataMap.put(poolKey,new ContentData(poolKey,section));
}
this.contentMap = contentDataMap;
ConfigurationSection section1 = yml.getConfigurationSection("FloatChance");
Map<Integer, Map<String, Double>> integerMapMap = new HashMap<>();
for (String needKey : section1.getKeys(false)) {
int needAmount = Integer.parseInt(needKey);
Map<String, Double> stringDoubleMap = new HashMap<>();
ConfigurationSection sectionChance = section1.getConfigurationSection(needKey);
for (String poolKey : sectionChance.getKeys(false)){
stringDoubleMap.put(poolKey,sectionChance.getDouble(poolKey));
}
integerMapMap.put(needAmount,stringDoubleMap);
}
this.poolChanceMap = integerMapMap;
}
public ItemStack getItem() {
NBTItem nbt = new NBTItem(item);
nbt.setString("poolItem",this.name);
return nbt.getItem();
}
// 执行抽奖获取抽奖池序号
public int getPoolAmount(int playerValue){
int poolAmount = 0;
// 假设玩家是5
// 5: XXXX
// 10: XXXX
// 20: XXXX
// 30: XXXX
// 遍历
Map<Integer, Map<String, Double>> chanceMap = this.poolChanceMap;
// 使用 TreeMap 来自动排序
Map<Integer, Map<String, Double>> sortedMap = new TreeMap<>(chanceMap);
for (Map.Entry<Integer, Map<String, Double>> entry : sortedMap.entrySet()) {
if(playerValue >= entry.getKey()){
poolAmount = entry.getKey();
}else{
break;
}
}
return poolAmount;
}
public String getPoolKey(int poolAmount){
Map<String,Double> dataMap = this.poolChanceMap.get(poolAmount);
Map<String, Double> chanceMap = new HashMap<>();
int a = 0;
for (String rewardKey : dataMap.keySet()) {
double chance = dataMap.get(rewardKey);
chanceMap.put(rewardKey,chance);
a += chance;
}
String poolKey = null;
Random ran = new Random();
ran.setSeed(System.currentTimeMillis()+DemonAPI.getRandomInt(80000,1000));
int b = ran.nextInt(a);
for (String key : dataMap.keySet()){
if(chanceMap.get(key) > b){
poolKey = key;
break;
}
b -= chanceMap.get(key);
}
return poolKey;
}
public ContentData getContentData(String poolKey){
if(this.contentMap.get(poolKey) == null){
return null;
}
return this.contentMap.get(poolKey);
}
}

View File

@ -0,0 +1,55 @@
package com.yaohun.crazyprizepool.listener;
import com.yaohun.crazyprizepool.Main;
import com.yaohun.crazyprizepool.data.ContentData;
import com.yaohun.crazyprizepool.data.PoolData;
import com.yaohun.crazyprizepool.manager.PoolManager;
import de.tr7zw.itemnbtapi.NBTItem;
import me.Demon.DemonPlugin.DemonAPI;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import java.util.UUID;
public class OpenBoxEvent implements Listener {
public Main plugin;
public String prefix;
public OpenBoxEvent(Main main){
plugin = main;
prefix = main.getPrefix();
}
@EventHandler
public void onUse(PlayerInteractEvent e){
Player p = e.getPlayer();
UUID uuid = p.getUniqueId();
ItemStack item = p.getInventory().getItemInMainHand();
if(!DemonAPI.itemIsNull(item)){
NBTItem nbt = new NBTItem(item);
if(nbt.hasKey("poolItem")){
String poolKey = nbt.getString("poolItem");
PoolData poolData = plugin.getPoolManager().getPoolData(poolKey);
if(poolData == null){return;}
PoolManager manager = plugin.getPoolManager();
int PlayerAmount = manager.getPlayerPoolAmount(uuid,poolKey);
// 获取玩家本次打开奖池的奖池概率
int PoolAmount = poolData.getPoolAmount(PlayerAmount);
// 获取抽中的是S级A级B级C级
String PoolKey = poolData.getPoolKey(PoolAmount);
ContentData contentData = poolData.getContentData(PoolKey);
if (contentData == null) {
return;
}
String rewardKey = contentData.Start_CratesKey();
ItemStack reward_item = contentData.getCratesStack(rewardKey);
p.getWorld().dropItem(p.getLocation(),reward_item);
// 为玩家增加打开奖池次数+1
plugin.getPoolManager().addPlayerPoolAmount(uuid,poolKey,1);
}
}
}
}

View File

@ -0,0 +1,122 @@
package com.yaohun.crazyprizepool.manager;
import com.yaohun.crazyprizepool.Main;
import com.yaohun.crazyprizepool.data.ContentData;
import com.yaohun.crazyprizepool.data.PoolData;
import me.Demon.DemonPlugin.DemonAPI;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public class PoolManager {
private Main plugin;
private Map<UUID, Map<String,Integer>> uuidDataMap = new HashMap<>();
private Map<String, PoolData> poolDataMap = new HashMap<>();
public PoolManager(Main main){
plugin = main;
FileConfiguration yml = plugin.getConfig();
ConfigurationSection section = yml.getConfigurationSection("PoolData");
if(section != null) {
for (String uuid : section.getKeys(false)){
Map<String,Integer> integerMap = new HashMap<>();
ConfigurationSection section1 = section.getConfigurationSection(uuid);
for (String poolKey : section1.getKeys(false)){
integerMap.put(poolKey,section1.getInt(poolKey));
}
UUID uuid1 = UUID.fromString(uuid);
uuidDataMap.put(uuid1,integerMap);
}
DemonAPI.sendConsoleMessage("§f[§a!§f] §f玩家数据 §8> §6"+uuidDataMap.size()+"");
}
File folder = new File("./plugins","CrazyPrizePool");
File[] files = folder.listFiles((dir, name) -> name.endsWith(".yml")); // 列出目录下所有以 .yml 结尾的文件
if (files != null) { // 确保目录存在且不是空的
for (File file : files) { // 遍历每个文件
String fileName = file.getName().replace(".yml","");
if(!fileName.contains("config")) {
poolDataMap.put(fileName,new PoolData(plugin,fileName));
}
}
DemonAPI.sendConsoleMessage("§f[§a!§f] §f奖池方案 §8> §6"+poolDataMap.size()+"");
}
}
public void SavePlayerData(){
FileConfiguration yml = plugin.getConfig();
for (UUID uuid : uuidDataMap.keySet()){
Map<String,Integer> integerMap = uuidDataMap.get(uuid);
for (String poolKey : integerMap.keySet()) {
yml.set("PoolData." + uuid+"."+poolKey,integerMap.get(poolKey));
}
}
plugin.saveConfig();
}
// 根据手持物品判断奖池
public PoolData getItemConvertPoolData(ItemStack stack){
if(DemonAPI.itemIsNull(stack) || DemonAPI.itemIsLore(stack)){
return null;
}
ItemMeta stackMeta = stack.getItemMeta();
for (String poolKey : poolDataMap.keySet()){
PoolData poolData = poolDataMap.get(poolKey);
ItemStack poolItem = poolData.getItem();
if(DemonAPI.itemIsNull(poolItem) || DemonAPI.itemIsLore(poolItem)){
continue;
}
ItemMeta meta = poolItem.getItemMeta();
if(meta.getDisplayName().contains(stackMeta.getDisplayName())){
if(meta.getLore().get(1).equalsIgnoreCase(stackMeta.getLore().get(1))){
return poolData;
}
}
}
return null;
}
// 根据Key获取奖池
public PoolData getPoolData(String poolKey){
if(this.poolDataMap.get(poolKey) == null){
return null;
}
return this.poolDataMap.get(poolKey);
}
// 获取玩家打开这个奖池累积次数
public int getPlayerPoolAmount(UUID uuid,String poolKey){
if(this.uuidDataMap.get(uuid) == null){
return 0;
}
if(this.uuidDataMap.get(uuid).get(poolKey) == null){
return 0;
}
return this.uuidDataMap.get(uuid).get(poolKey);
}
// 增加玩家打开这个奖池的累积次数
public void addPlayerPoolAmount(UUID uuid,String poolKey,int amount){
int nowAmount = getPlayerPoolAmount(uuid,poolKey);
int newAmount = nowAmount + amount;
setPlayerPoolAmount(uuid,poolKey,newAmount);
}
// 设置玩家打开这个奖池的累积次数
public void setPlayerPoolAmount(UUID uuid,String poolKey,int amount){
Map<UUID, Map<String,Integer>> uuidData = new HashMap<>();
if(this.uuidDataMap.get(uuid) != null){
uuidData = this.uuidDataMap;
}
if(uuidData.get(uuid).get(poolKey) == null){
Map<String,Integer> integerMap = new HashMap<>();
integerMap.put(poolKey,amount);
uuidData.put(uuid,integerMap);
}
this.uuidDataMap = uuidData;
}
}

View File

@ -0,0 +1,30 @@
PoolName: 四叶草奖池
PoolItem:
==: org.bukkit.inventory.ItemStack
type: DIAMOND
meta:
==: ItemMeta
meta-type: UNSPECIFIC
display-name: §b§l荣耀之石
lore:
- §c§l★§7获得它是一种荣耀
- §a§l✔§7从破产抽奖区获得
enchants:
PROTECTION_ENVIRONMENTAL: 1
ItemFlags:
- HIDE_ENCHANTS
# 当玩家累积开启这个达到不同次数后奖池的概率则会变化
FloatChance:
10:
S: 0.01
A: 1.00
B: 5.00
C: 10.00
D: 100.00
PoolContent:
S:
C9:
chance: 200.0
# ItemKu#<Key> 调用物品库物品
# CSGO#<Key> 调用CSGO物品
item: ItemKu#

View File

@ -0,0 +1,5 @@
name: CrazyPrizePool
main: com.yaohun.crazyprizepool.Main
version: 1.0
commands:
crazypool: