From 3dbdbd25ecea188e65c2b21128cb308634db1ec5 Mon Sep 17 00:00:00 2001 From: YuTian <2953516620@qq.com> Date: Mon, 12 Aug 2024 17:40:52 +0800 Subject: [PATCH] 1.0 --- .gitignore | 3 + pom.xml | 91 ++++++ .../com/io/yutian/gemsystem/GemSystem.java | 56 ++++ .../io/yutian/gemsystem/GemSystemSetting.java | 61 ++++ .../yutian/gemsystem/data/GemCraftData.java | 27 ++ .../com/io/yutian/gemsystem/gui/GemGui.java | 276 ++++++++++++++++++ .../io/yutian/gemsystem/gui/GemResultGui.java | 82 ++++++ .../io/yutian/gemsystem/gui/GuiHolder.java | 14 + .../gemsystem/listener/GuiListener.java | 33 +++ .../gemsystem/util/PlayerInventoryUtil.java | 41 +++ .../io/yutian/gemsystem/util/RandomUtil.java | 59 ++++ src/main/resources/config.yml | 14 + src/main/resources/plugin.yml | 5 + 13 files changed, 762 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/com/io/yutian/gemsystem/GemSystem.java create mode 100644 src/main/java/com/io/yutian/gemsystem/GemSystemSetting.java create mode 100644 src/main/java/com/io/yutian/gemsystem/data/GemCraftData.java create mode 100644 src/main/java/com/io/yutian/gemsystem/gui/GemGui.java create mode 100644 src/main/java/com/io/yutian/gemsystem/gui/GemResultGui.java create mode 100644 src/main/java/com/io/yutian/gemsystem/gui/GuiHolder.java create mode 100644 src/main/java/com/io/yutian/gemsystem/listener/GuiListener.java create mode 100644 src/main/java/com/io/yutian/gemsystem/util/PlayerInventoryUtil.java create mode 100644 src/main/java/com/io/yutian/gemsystem/util/RandomUtil.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..53554ea --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/.idea/ +/GemSystem.iml +/target/ diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..bc5c1bc --- /dev/null +++ b/pom.xml @@ -0,0 +1,91 @@ + + + 4.0.0 + + com.io.yutian + GemSystem + 1.0-SNAPSHOT + jar + + GemSystem + + + 1.8 + UTF-8 + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + ${java.version} + ${java.version} + + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.4 + + + package + + shade + + + + + + + + src/main/resources + true + + + + + + + papermc-repo + https://repo.papermc.io/repository/maven-public/ + + + sonatype + https://oss.sonatype.org/content/groups/public/ + + + public-rpg + https://repo.aurora-pixels.com/repository/public-rpg/ + + + + + + com.destroystokyo.paper + paper-api + 1.12.2-R0.1-SNAPSHOT + provided + + + com.yaohun.itemku + AuItemStackLibrary + 1.12.2 + + + me.Demon.DemonPlugin + DemonAPI + 1.2.0 + + + com.yaohun.nbtapi + NBT-API + 1.12.2 + + + + diff --git a/src/main/java/com/io/yutian/gemsystem/GemSystem.java b/src/main/java/com/io/yutian/gemsystem/GemSystem.java new file mode 100644 index 0000000..7878849 --- /dev/null +++ b/src/main/java/com/io/yutian/gemsystem/GemSystem.java @@ -0,0 +1,56 @@ +package com.io.yutian.gemsystem; + +import com.io.yutian.gemsystem.gui.GemGui; +import com.io.yutian.gemsystem.gui.GuiHolder; +import com.io.yutian.gemsystem.listener.GuiListener; +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.inventory.InventoryHolder; +import org.bukkit.plugin.java.JavaPlugin; + +public final class GemSystem extends JavaPlugin { + + private static GemSystem instance; + + @Override + public void onEnable() { + instance = this; + Bukkit.getPluginManager().registerEvents(new GuiListener(), this); + + GemSystemSetting.reload(); + } + + @Override + public void onDisable() { + for (Player player : Bukkit.getOnlinePlayers()) { + if (player.getOpenInventory() != null) { + InventoryHolder inventoryHolder = player.getOpenInventory().getTopInventory().getHolder(); + if (inventoryHolder instanceof GuiHolder) { + player.closeInventory(); + } + } + } + } + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if (args.length == 1 && args[0].equalsIgnoreCase("reload") && sender.isOp()) { + GemSystemSetting.reload(); + sender.sendMessage("§8§l[§a§l!§8§l] §7重载成功"); + return true; + } + if (!(sender instanceof Player)) { + return true; + } + Player player = (Player) sender; + new GemGui(player).open(); + return true; + } + + public static GemSystem inst() { + return instance; + } + +} diff --git a/src/main/java/com/io/yutian/gemsystem/GemSystemSetting.java b/src/main/java/com/io/yutian/gemsystem/GemSystemSetting.java new file mode 100644 index 0000000..3dae624 --- /dev/null +++ b/src/main/java/com/io/yutian/gemsystem/GemSystemSetting.java @@ -0,0 +1,61 @@ +package com.io.yutian.gemsystem; + +import com.io.yutian.gemsystem.data.GemCraftData; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.FileConfiguration; + +import java.util.HashMap; +import java.util.Map; + +public class GemSystemSetting { + + private static Map> gemCraftDatas = new HashMap<>(); + + public static void reload() { + gemCraftDatas.clear(); + GemSystem.inst().saveDefaultConfig(); + GemSystem.inst().reloadConfig(); + FileConfiguration config = GemSystem.inst().getConfig(); + ConfigurationSection section = config.getConfigurationSection("gems"); + for (String key : section.getKeys(false)) { + ConfigurationSection craftSection = section.getConfigurationSection(key); + Map craftDatas = new HashMap<>(); + for (String craftKey : craftSection.getKeys(false)) { + int level = Integer.parseInt(craftKey); + double chance = craftSection.getDouble(craftKey+".chance"); + String result = craftSection.getString(craftKey+".result"); + String failResult = craftSection.getString(craftKey+".failResult"); + craftDatas.put(level, new GemCraftData(chance, result, failResult)); + } + gemCraftDatas.put(key, craftDatas); + } + } + + public static Map getGemCraftDatas(String gemType) { + return gemCraftDatas.get(gemType); + } + + public static boolean hasGemCraftData(String gemType) { + return gemCraftDatas.containsKey(gemType); + } + + public static boolean hasGemCraftData(String gemType, int level) { + if (!hasGemCraftData(gemType)) { + return false; + } + Map craftDatas = gemCraftDatas.get(gemType); + return craftDatas.containsKey(level); + } + + public static GemCraftData getGemCraftData(String gemType, int level) { + if (!hasGemCraftData(gemType)) { + return null; + } + Map craftDatas = gemCraftDatas.get(gemType); + if (!craftDatas.containsKey(level)) { + return null; + } + return craftDatas.get(level); + } + +} diff --git a/src/main/java/com/io/yutian/gemsystem/data/GemCraftData.java b/src/main/java/com/io/yutian/gemsystem/data/GemCraftData.java new file mode 100644 index 0000000..df51685 --- /dev/null +++ b/src/main/java/com/io/yutian/gemsystem/data/GemCraftData.java @@ -0,0 +1,27 @@ +package com.io.yutian.gemsystem.data; + +public class GemCraftData { + + private double chance; + private String result; + private String failResult; + + public GemCraftData(double chance, String result, String failResult) { + this.chance = chance; + this.result = result; + this.failResult = failResult; + } + + public double getChance() { + return chance; + } + + public String getResult() { + return result; + } + + public String getFailResult() { + return failResult; + } + +} diff --git a/src/main/java/com/io/yutian/gemsystem/gui/GemGui.java b/src/main/java/com/io/yutian/gemsystem/gui/GemGui.java new file mode 100644 index 0000000..a03e107 --- /dev/null +++ b/src/main/java/com/io/yutian/gemsystem/gui/GemGui.java @@ -0,0 +1,276 @@ +package com.io.yutian.gemsystem.gui; + +import com.io.yutian.gemsystem.GemSystemSetting; +import com.io.yutian.gemsystem.data.GemCraftData; +import com.io.yutian.gemsystem.util.PlayerInventoryUtil; +import com.io.yutian.gemsystem.util.RandomUtil; +import com.yaohun.itemlibrary.api.ItemKuAPI; +import de.tr7zw.itemnbtapi.NBTItem; +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.Sound; +import org.bukkit.entity.Player; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.inventory.InventoryCloseEvent; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryHolder; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Objects; + +public class GemGui implements InventoryHolder, GuiHolder { + + private static int[] SLOT_ARRAY_1 = {0,1,2,3,4,5,6,7,8,9,11,13,15,17,18,19,20,21,22,23,24,25,26}; + private static List SLOT_ARRAY_2 = Arrays.asList(10,12,14); + private static int SLOT_1 = 16; + + private static ItemStack ITEM_1; + private static ItemStack ITEM_2; + private static ItemStack ITEM_3; + private static ItemStack ITEM_4; + private static ItemStack ITEM_5; + private static ItemStack ITEM_6; + + private GemInfo cacheGemInfo; + private List gemCache = new ArrayList<>(); + private GemCraftData cacheGemCraftData; + + private Inventory inventory; + private Player player; + + public GemGui(Player player) { + this.inventory = Bukkit.createInventory(this, 27, "宝石合成"); + this.player = player; + for (int slot : SLOT_ARRAY_1) { + inventory.setItem(slot, ITEM_1); + } + for (int slot : SLOT_ARRAY_2) { + inventory.setItem(slot, ITEM_2); + } + inventory.setItem(SLOT_1, ITEM_5); + } + + @Override + public void open() { + player.openInventory(inventory); + } + + @Override + public void onClick(InventoryClickEvent event) { + Inventory clickedInventory = event.getClickedInventory(); + int slot = event.getRawSlot(); + event.setCancelled(true); + if (slot < 27) { + if (SLOT_ARRAY_2.contains(slot)) { + int index = getGemSlotIndex(slot); + if (index != -1 && gemCache.size() > index) { + removeGem(index); + } + } else if (slot == 16) { + if (canCraft()) { + craft(); + } + } + } else { + ItemStack clickedItem = event.getCurrentItem(); + if (clickedItem == null || clickedItem.getType() == Material.AIR) { + return; + } + GemInfo gemInfo = getGemInfo(clickedItem); + if (gemInfo == null) { + player.sendMessage("§8§l[§e§l!§8§l] §7该物品不是宝石"); + return; + } + System.out.println(gemInfo); + if (!GemSystemSetting.hasGemCraftData(gemInfo.getType(), gemInfo.getLevel())) { + player.sendMessage("§8§l[§e§l!§8§l] §7该物品无法合成"); + return; + } + cacheGemCraftData = GemSystemSetting.getGemCraftData(gemInfo.getType(), gemInfo.getLevel()); + if (cacheGemInfo != null && !cacheGemInfo.equals(gemInfo)) { + player.sendMessage("§8§l[§e§l!§8§l] §7该宝石类型或等级不符"); + return; + } + addGem(clickedItem, clickedInventory, event.getSlot()); + } + } + + @Override + public void onClose(InventoryCloseEvent event) { + for (ItemStack itemStack : gemCache) { + PlayerInventoryUtil.giveItemStack(player, itemStack); + } + gemCache.clear(); + } + + @Override + public Inventory getInventory() { + return inventory; + } + + private GemInfo getGemInfo(ItemStack itemStack) { + NBTItem nbtItem = new NBTItem(itemStack); + if (nbtItem.hasKey("gemType") && nbtItem.hasKey("gemLevel")) { + return new GemInfo(nbtItem.getString("gemType"), Integer.parseInt(nbtItem.getString("gemLevel"))); + } + return null; + } + + private int getGemSlotIndex(int slot) { + for (int i = 0; i < SLOT_ARRAY_2.size(); i++) { + if (slot == SLOT_ARRAY_2.get(i)) { + return i; + } + } + return -1; + } + + private void addGem(ItemStack itemStack, Inventory inventory, int slot) { + if (gemCache.size() >= 3) { + return; + } + int amount = itemStack.getAmount(); + ItemStack gem = itemStack.clone(); + ItemStack itemStack1 = itemStack.clone(); + if (amount > 1) { + gem.setAmount(1); + itemStack1.setAmount(amount - 1); + } else { + itemStack1 = null; + } + if (cacheGemInfo == null) { + cacheGemInfo = getGemInfo(itemStack); + } + gemCache.add(gem); + update(); + inventory.setItem(slot, itemStack1); + player.playSound(player.getLocation(), Sound.UI_BUTTON_CLICK, 1, 1); + } + + private void removeGem(int index) { + if (!PlayerInventoryUtil.hasFreeSlot(player)) { + player.sendMessage("§8§l[§c§l!§8§l] §7您的背包空间不足,无法拿下宝石"); + return; + } + ItemStack gem = gemCache.get(index); + PlayerInventoryUtil.giveItemStack(player, gem); + gemCache.remove(index); + if (gemCache.size() == 0) { + cacheGemInfo = null; + cacheGemCraftData = null; + } + update(); + } + + private void update() { + for (int i = 0; i < SLOT_ARRAY_2.size(); i++) { + if (gemCache.size() > i) { + inventory.setItem(SLOT_ARRAY_2.get(i), gemCache.get(i)); + } else { + inventory.setItem(SLOT_ARRAY_2.get(i), ITEM_2); + } + } + if (gemCache.size() == 3) { + inventory.setItem(SLOT_1, ITEM_6); + } else { + inventory.setItem(SLOT_1, ITEM_5); + } + } + + private boolean canCraft() { + return gemCache.size() == 3; + } + + private void craft() { + if (!PlayerInventoryUtil.hasFreeSlot(player)) { + player.sendMessage("§8§l[§c§l!§8§l] §7您的背包空间不足,无法合成"); + return; + } + gemCache.clear(); + boolean success = RandomUtil.random(cacheGemCraftData.getChance()); + ItemStack result = ItemKuAPI.getItems(success ? cacheGemCraftData.getResult() : cacheGemCraftData.getFailResult()); + if (success) { + player.playSound(player.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 1, 1); + player.sendMessage("§8§l[§a§l!§8§l] §7恭喜您,合成成功!"); + } else { + player.playSound(player.getLocation(), Sound.ENTITY_BLAZE_DEATH, 1, 1); + player.sendMessage("§8§l[§e§l!§8§l] §7很遗憾,合成失败"); + } + new GemResultGui(player, result, success).open(); + } + + private class GemInfo { + + private final String type; + private final int level; + + public GemInfo(String type, int level) { + this.type = type; + this.level = level; + } + + public String getType() { + return type; + } + + public int getLevel() { + return level; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + GemInfo gemInfo = (GemInfo) o; + return level == gemInfo.level && Objects.equals(type, gemInfo.type); + } + + @Override + public int hashCode() { + int result = Objects.hashCode(type); + result = 31 * result + level; + return result; + } + + @Override + public String toString() { + return "GemInfo{" + + "type='" + type + '\'' + + ", level=" + level + + '}'; + } + } + + static { + ITEM_1 = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 15); + ITEM_2 = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 0); + ITEM_3 = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 5); + ITEM_4 = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 14); + ITEM_5 = new ItemStack(Material.BARRIER); + ITEM_6 = new ItemStack(Material.ANVIL); + ItemMeta itemMeta1 = ITEM_1.getItemMeta(); + itemMeta1.setDisplayName(" "); + ITEM_1.setItemMeta(itemMeta1); + ItemMeta itemMeta2 = ITEM_2.getItemMeta(); + itemMeta2.setDisplayName("§a宝石孔"); + ITEM_2.setItemMeta(itemMeta2); + ItemMeta itemMeta3 = ITEM_3.getItemMeta(); + itemMeta3.setDisplayName(" "); + ITEM_3.setItemMeta(itemMeta3); + ItemMeta itemMeta4 = ITEM_4.getItemMeta(); + itemMeta4.setDisplayName(" "); + ITEM_4.setItemMeta(itemMeta4); + ItemMeta itemMeta5 = ITEM_5.getItemMeta(); + itemMeta5.setDisplayName("§c请先放入宝石"); + ITEM_5.setItemMeta(itemMeta5); + ItemMeta itemMeta6 = ITEM_6.getItemMeta(); + itemMeta6.setDisplayName("§f点击合成"); + ITEM_6.setItemMeta(itemMeta6); + } + +} diff --git a/src/main/java/com/io/yutian/gemsystem/gui/GemResultGui.java b/src/main/java/com/io/yutian/gemsystem/gui/GemResultGui.java new file mode 100644 index 0000000..4ac3675 --- /dev/null +++ b/src/main/java/com/io/yutian/gemsystem/gui/GemResultGui.java @@ -0,0 +1,82 @@ +package com.io.yutian.gemsystem.gui; + +import com.io.yutian.gemsystem.util.PlayerInventoryUtil; +import org.bukkit.Material; +import org.bukkit.Sound; +import org.bukkit.entity.Player; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.inventory.InventoryCloseEvent; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryHolder; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; + +public class GemResultGui implements InventoryHolder, GuiHolder { + + private static final ItemStack ITEM_1; + private static final ItemStack ITEM_2; + + private static final int[] SLOT_ARRAY = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26}; + private static final int SLOT = 13; + + private Inventory inventory; + private Player player; + + private ItemStack result; + private boolean success; + + public GemResultGui(Player player, ItemStack result, boolean success) { + this.player = player; + this.result = result; + this.success = success; + this.inventory = player.getServer().createInventory(this, 27, "宝石合成"); + for (int slot : SLOT_ARRAY) { + inventory.setItem(slot, success ? ITEM_1 : ITEM_2); + } + this.inventory.setItem(SLOT, result); + } + + @Override + public void onClick(InventoryClickEvent event) { + Inventory clickedInventory = event.getClickedInventory(); + int slot = event.getRawSlot(); + event.setCancelled(true); + if (slot == SLOT) { + if (result != null) { + PlayerInventoryUtil.giveItemStack(player, result); + player.playSound(player.getLocation(), Sound.UI_BUTTON_CLICK, 1, 1); + result = null; + player.closeInventory(); + } + } + } + + @Override + public void onClose(InventoryCloseEvent event) { + if (result != null) { + PlayerInventoryUtil.giveItemStack(player, result); + } + } + + @Override + public void open() { + player.openInventory(inventory); + } + + @Override + public Inventory getInventory() { + return inventory; + } + + static { + ITEM_1 = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 5); + ITEM_2 = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 14); + ItemMeta itemMeta1 = ITEM_1.getItemMeta(); + itemMeta1.setDisplayName("§a成功"); + ITEM_1.setItemMeta(itemMeta1); + ItemMeta itemMeta2 = ITEM_2.getItemMeta(); + itemMeta2.setDisplayName("§c失败"); + ITEM_2.setItemMeta(itemMeta2); + } + +} diff --git a/src/main/java/com/io/yutian/gemsystem/gui/GuiHolder.java b/src/main/java/com/io/yutian/gemsystem/gui/GuiHolder.java new file mode 100644 index 0000000..18956f4 --- /dev/null +++ b/src/main/java/com/io/yutian/gemsystem/gui/GuiHolder.java @@ -0,0 +1,14 @@ +package com.io.yutian.gemsystem.gui; + +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.inventory.InventoryCloseEvent; + +public interface GuiHolder { + + void onClick(InventoryClickEvent event); + + void onClose(InventoryCloseEvent event); + + void open(); + +} diff --git a/src/main/java/com/io/yutian/gemsystem/listener/GuiListener.java b/src/main/java/com/io/yutian/gemsystem/listener/GuiListener.java new file mode 100644 index 0000000..41dae47 --- /dev/null +++ b/src/main/java/com/io/yutian/gemsystem/listener/GuiListener.java @@ -0,0 +1,33 @@ +package com.io.yutian.gemsystem.listener; + +import com.io.yutian.gemsystem.gui.GuiHolder; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.inventory.InventoryCloseEvent; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryHolder; + +public class GuiListener implements Listener { + + @EventHandler + public void onInventoryClick(InventoryClickEvent event) { + Inventory inventory = event.getInventory(); + InventoryHolder holder = inventory.getHolder(); + if (holder != null && holder instanceof GuiHolder) { + GuiHolder guiHolder = (GuiHolder) holder; + guiHolder.onClick(event); + } + } + + @EventHandler + public void onInventoryClose(InventoryCloseEvent event) { + Inventory inventory = event.getInventory(); + InventoryHolder holder = inventory.getHolder(); + if (holder != null && holder instanceof GuiHolder) { + GuiHolder guiHolder = (GuiHolder) holder; + guiHolder.onClose(event); + } + } + +} \ No newline at end of file diff --git a/src/main/java/com/io/yutian/gemsystem/util/PlayerInventoryUtil.java b/src/main/java/com/io/yutian/gemsystem/util/PlayerInventoryUtil.java new file mode 100644 index 0000000..76dedb4 --- /dev/null +++ b/src/main/java/com/io/yutian/gemsystem/util/PlayerInventoryUtil.java @@ -0,0 +1,41 @@ +package com.io.yutian.gemsystem.util; + +import org.bukkit.Material; +import org.bukkit.entity.Item; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; + +public class PlayerInventoryUtil { + + public static boolean hasFreeSlot(Player player) { + return getFreeSize(player) > 0; + } + + public static int getFreeSize(Player player) { + int index = -1; + int freesize = 0; + while (index < 35) { + index++; + ItemStack i = player.getInventory().getItem(index); + if (i == null) { + freesize++; + continue; + } + if (i.getType().equals(Material.AIR)) { + freesize++; + } + } + return freesize; + } + + public static void giveItemStack(Player player, ItemStack itemStack) { + if (PlayerInventoryUtil.hasFreeSlot(player)) { + player.getInventory().addItem(itemStack); + } else { + Item item = player.getLocation().getWorld().dropItem(player.getLocation(), itemStack); + item.setCanMobPickup(false); + item.setPickupDelay(20); + } + } + +} diff --git a/src/main/java/com/io/yutian/gemsystem/util/RandomUtil.java b/src/main/java/com/io/yutian/gemsystem/util/RandomUtil.java new file mode 100644 index 0000000..d3370d9 --- /dev/null +++ b/src/main/java/com/io/yutian/gemsystem/util/RandomUtil.java @@ -0,0 +1,59 @@ +package com.io.yutian.gemsystem.util; + +import org.bukkit.Color; +import org.bukkit.Location; +import org.bukkit.World; + +import java.util.List; +import java.util.Random; + +public class RandomUtil { + + public static boolean random(double d) { + return d >= new Random().nextFloat(); + } + + public static Color getRandomColor() { + Random random = new Random(); + int r = random.nextInt(256); + int g = random.nextInt(256); + int b = random.nextInt(256); + return Color.fromRGB(r, g, b); + } + + public static Location getRandomLocation(World world, double minX, double maxX, double minZ, double maxZ) { + double rx = getRandomDouble(minX, maxX, 3); + double rz = getRandomDouble(minZ, maxZ, 3); + int y = world.getHighestBlockAt((int)rx, (int)rz).getY()+1; + Location location = new Location(world, rx, y, rz); + return location; + } + + public static E getRandomElement(List list) { + if (list.size() == 0) { + return null; + } + return list.get(getRandomInt(0, list.size()-1)); + } + + public static String getRandomString(String[] array) { + Random r = new Random(); + return array[getRandomInt(0, array.length)]; + } + + public static int getRandomInt(int min, int max) { + if (min == max) { + return max; + } + Random r = new Random(); + int i = min < max ? min : max; + int a = min < max ? max : min; + return r.nextInt(a - i + 1) + i; + } + + public static double getRandomDouble(double min, double max, int scl) { + int pow = (int) Math.pow(10, scl); + return Math.floor((Math.random() * (max - min) + min) * pow) / pow; + } + +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml new file mode 100644 index 0000000..436655a --- /dev/null +++ b/src/main/resources/config.yml @@ -0,0 +1,14 @@ +gems: + 血腥宝石: + 1: + chance: 0.8 + result: 血腥宝石2 + fail-result: 血腥宝石碎片 + 2: + chance: 0.6 + result: 血腥宝石3 + fail-result: 血腥宝石碎片 + 3: + chance: 0.5 + result: 血腥宝石4 + fail-result: 血腥宝石碎片 \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..baa8859 --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,5 @@ +name: GemSystem +version: '${project.version}' +main: com.io.yutian.gemsystem.GemSystem +commands: + gemsystem: \ No newline at end of file