Files
src
Code
2 212
src/main/java/com/kodari/customchests/CustomChestsPlugin.java
package com.kodari.customchests;
import com.cryptomorin.xseries.XEnchantment;
import com.cryptomorin.xseries.XMaterial;
import org.bukkit.ChatColor;
import org.bukkit.NamespacedKey;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.Chest;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
public final class CustomChestsPlugin extends JavaPlugin implements Listener {
private NamespacedKey tierKey;
private NamespacedKey lootedKey;
@Override
public void onEnable() {
tierKey = new NamespacedKey(this, "chest_tier");
lootedKey = new NamespacedKey(this, "chest_looted");
getServer().getPluginManager().registerEvents(this, this);
for (Tier tier : Tier.values()) {
registerRecipe(tier);
}
}
private void registerRecipe(Tier tier) {
ShapedRecipe recipe = new ShapedRecipe(
new NamespacedKey(this, tier.id + "_chest"), createChestItem(tier));
recipe.shape("III", "ICI", "III");
recipe.setIngredient('I', material(tier.ingredient));
recipe.setIngredient('C', material("CHEST"));
getServer().addRecipe(recipe);
}
private ItemStack createChestItem(Tier tier) {
ItemStack item = new ItemStack(material("CHEST"));
ItemMeta meta = item.getItemMeta();
meta.setDisplayName(ChatColor.GOLD + tier.displayName + " Chest");
meta.setLore(List.of(ChatColor.GRAY + "Place to reveal its loot."));
meta.getPersistentDataContainer().set(tierKey, PersistentDataType.STRING, tier.id);
item.setItemMeta(meta);
return item;
}
@EventHandler
public void onChestPlace(BlockPlaceEvent event) {
ItemStack item = event.getItemInHand();
if (!item.hasItemMeta()) {
return;
}
String tierId = item.getItemMeta().getPersistentDataContainer()
.get(tierKey, PersistentDataType.STRING);
if (tierId == null) {
return;
}
BlockState state = event.getBlockPlaced().getState();
if (!(state instanceof Chest chest)) {
return;
}
chest.getPersistentDataContainer().set(tierKey, PersistentDataType.STRING, tierId);
chest.update(true, false);
}
@EventHandler
public void onChestOpen(PlayerInteractEvent event) {
if (event.getAction() != Action.RIGHT_CLICK_BLOCK) {
return;
}
Block block = event.getClickedBlock();
if (block == null || !(block.getState() instanceof Chest chest)) {
return;
}
String tierId = chest.getPersistentDataContainer().get(tierKey, PersistentDataType.STRING);
Tier tier = Tier.fromId(tierId);
if (tier == null || chest.getPersistentDataContainer().has(lootedKey, PersistentDataType.BYTE)) {
return;
}
Inventory inventory = chest.getBlockInventory();
List<ItemStack> loot = createLoot(tier);
ThreadLocalRandom random = ThreadLocalRandom.current();
for (ItemStack item : loot) {
inventory.setItem(random.nextInt(inventory.getSize()), item);
}
chest.getPersistentDataContainer().set(lootedKey, PersistentDataType.BYTE, (byte) 1);
chest.update(true, false);
}
private List<ItemStack> createLoot(Tier tier) {
List<ItemStack> loot = new ArrayList<>();
if (tier == Tier.GOLD) {
loot.add(new ItemStack(material("GOLDEN_APPLE")));
}
for (int i = loot.size(); i < 5; i++) {
loot.add(createReward(tier));
}
return loot;
}
private ItemStack createReward(Tier tier) {
ThreadLocalRandom random = ThreadLocalRandom.current();
if (tier == Tier.LAPIS) {
String[] equipment = {
"IRON_HELMET", "IRON_CHESTPLATE", "IRON_LEGGINGS", "IRON_BOOTS",
"GOLDEN_HELMET", "GOLDEN_CHESTPLATE", "GOLDEN_LEGGINGS", "GOLDEN_BOOTS",
"DIAMOND_HELMET", "DIAMOND_CHESTPLATE", "DIAMOND_LEGGINGS", "DIAMOND_BOOTS"
};
ItemStack item = new ItemStack(material(equipment[random.nextInt(equipment.length)]));
addEnchantment(item, "PROTECTION", random.nextInt(1, 5));
addEnchantment(item, "UNBREAKING", random.nextInt(1, 4));
return item;
}
if (tier == Tier.OBSIDIAN) {
ItemStack item = new ItemStack(material("CRYING_OBSIDIAN"), random.nextInt(1, 5));
ItemMeta meta = item.getItemMeta();
meta.setDisplayName(ChatColor.LIGHT_PURPLE + "Crystal Obsidian");
item.setItemMeta(meta);
return item;
}
String[] rewards = tier.rewards;
ItemStack item = new ItemStack(material(rewards[random.nextInt(rewards.length)]));
item.setAmount(random.nextInt(1, Math.min(4, item.getType().getMaxStackSize()) + 1));
return item;
}
private void addEnchantment(ItemStack item, String name, int level) {
XEnchantment.matchXEnchantment(name).ifPresent(enchantment ->
item.addUnsafeEnchantment(enchantment.getEnchant(), level));
}
private Material material(String name) {
return XMaterial.matchXMaterial(name)
.map(XMaterial::parseMaterial)
.orElseThrow(() -> new IllegalArgumentException("Unsupported material: " + name));
}
private enum Tier {
IRON("iron", "Iron", "IRON_INGOT", new String[]{
"IRON_INGOT", "IRON_BLOCK", "IRON_NUGGET", "IRON_SWORD", "IRON_PICKAXE",
"IRON_AXE", "IRON_HELMET", "IRON_CHESTPLATE", "IRON_LEGGINGS", "IRON_BOOTS"
}),
GOLD("gold", "Gold", "GOLD_INGOT", new String[]{
"GOLD_INGOT", "GOLD_BLOCK", "GOLDEN_APPLE", "GOLDEN_CARROT", "GOLDEN_SWORD",
"GOLDEN_PICKAXE", "GOLDEN_HELMET", "GOLDEN_CHESTPLATE", "GOLDEN_LEGGINGS", "GOLDEN_BOOTS"
}),
DIAMOND("diamond", "Diamond", "DIAMOND", new String[]{
"DIAMOND", "DIAMOND_BLOCK", "DIAMOND_SWORD", "DIAMOND_PICKAXE", "DIAMOND_AXE",
"DIAMOND_HELMET", "DIAMOND_CHESTPLATE", "DIAMOND_LEGGINGS", "DIAMOND_BOOTS"
}),
EMERALD("emerald", "Emerald", "EMERALD", new String[]{
"EMERALD", "EMERALD_BLOCK"
}),
LAPIS("lapis", "Lapis", "LAPIS_LAZULI", new String[0]),
OBSIDIAN("obsidian", "Obsidian", "OBSIDIAN", new String[0]);
private final String id;
private final String displayName;
private final String ingredient;
private final String[] rewards;
Tier(String id, String displayName, String ingredient, String[] rewards) {
this.id = id;
this.displayName = displayName;
this.ingredient = ingredient;
this.rewards = rewards;
}
private static Tier fromId(String id) {
if (id == null) {
return null;
}
for (Tier tier : values()) {
if (tier.id.equals(id)) {