This commit is contained in:
YuTian 2024-08-12 17:40:52 +08:00
commit 3dbdbd25ec
13 changed files with 762 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
/.idea/
/GemSystem.iml
/target/

91
pom.xml Normal file
View File

@ -0,0 +1,91 @@
<?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>com.io.yutian</groupId>
<artifactId>GemSystem</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>GemSystem</name>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<repositories>
<repository>
<id>papermc-repo</id>
<url>https://repo.papermc.io/repository/maven-public/</url>
</repository>
<repository>
<id>sonatype</id>
<url>https://oss.sonatype.org/content/groups/public/</url>
</repository>
<repository>
<id>public-rpg</id>
<url>https://repo.aurora-pixels.com/repository/public-rpg/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.destroystokyo.paper</groupId>
<artifactId>paper-api</artifactId>
<version>1.12.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.yaohun.itemku</groupId>
<artifactId>AuItemStackLibrary</artifactId>
<version>1.12.2</version>
</dependency>
<dependency>
<groupId>me.Demon.DemonPlugin</groupId>
<artifactId>DemonAPI</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>com.yaohun.nbtapi</groupId>
<artifactId>NBT-API</artifactId>
<version>1.12.2</version>
</dependency>
</dependencies>
</project>

View File

@ -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;
}
}

View File

@ -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<String, Map<Integer, GemCraftData>> 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<Integer, GemCraftData> 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<Integer, GemCraftData> 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<Integer, GemCraftData> craftDatas = gemCraftDatas.get(gemType);
return craftDatas.containsKey(level);
}
public static GemCraftData getGemCraftData(String gemType, int level) {
if (!hasGemCraftData(gemType)) {
return null;
}
Map<Integer, GemCraftData> craftDatas = gemCraftDatas.get(gemType);
if (!craftDatas.containsKey(level)) {
return null;
}
return craftDatas.get(level);
}
}

View File

@ -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;
}
}

View File

@ -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<Integer> 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<ItemStack> 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);
}
}

View File

@ -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);
}
}

View File

@ -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();
}

View File

@ -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);
}
}
}

View File

@ -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);
}
}
}

View File

@ -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> E getRandomElement(List<E> 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;
}
}

View File

@ -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: 血腥宝石碎片

View File

@ -0,0 +1,5 @@
name: GemSystem
version: '${project.version}'
main: com.io.yutian.gemsystem.GemSystem
commands:
gemsystem: