Files
src
Code
2 301
src/main/java/com/donutsmp/shop/DonutSMPToolsShop.java
package com.donutsmp.shop;
import com.cryptomorin.xseries.XEnchantment;
import com.cryptomorin.xseries.XMaterial;
import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.economy.EconomyResponse;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.NamespacedKey;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class DonutSMPToolsShop extends JavaPlugin implements Listener, CommandExecutor {
private static final String GUI_TITLE = ChatColor.DARK_PURPLE + "" + ChatColor.BOLD + "DonutSMP Tools Shop";
private static final double TOOL_PRICE = 1_000_000.0;
private Economy economy;
private NamespacedKey toolKey;
@Override
public void onEnable() {
toolKey = new NamespacedKey(this, "custom_tool_id");
if (!setupEconomy()) {
getLogger().severe("Vault or an economy provider was not found. Disabling plugin.");
getServer().getPluginManager().disablePlugin(this);
return;
}
if (getCommand("donutsmptools") == null) {
getLogger().severe("The donutsmptools command is missing from plugin.yml.");
getServer().getPluginManager().disablePlugin(this);
return;
}
getCommand("donutsmptools").setExecutor(this);
getServer().getPluginManager().registerEvents(this, this);
getLogger().info("DonutSMP Tools Shop enabled.");
}
private boolean setupEconomy() {
if (getServer().getPluginManager().getPlugin("Vault") == null) {
return false;
}
RegisteredServiceProvider<Economy> registration = getServer().getServicesManager().getRegistration(Economy.class);
if (registration == null) {
return false;
}
economy = registration.getProvider();
return economy != null;
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player player)) {
sender.sendMessage("This command can only be executed by players!");
return true;
}
openShopGUI(player);
return true;
}
private void openShopGUI(Player player) {
Inventory gui = Bukkit.createInventory(null, 27, GUI_TITLE);
setShopItem(gui, 11, "DIAMOND_PICKAXE", ChatColor.LIGHT_PURPLE + "" + ChatColor.BOLD + "Amethyst Pickaxe",
ChatColor.GRAY + "Ability: " + ChatColor.LIGHT_PURPLE + "3x3 (9 Blocks) Area Mining");
setShopItem(gui, 13, "DIAMOND_AXE", ChatColor.LIGHT_PURPLE + "" + ChatColor.BOLD + "Amethyst Axe",
ChatColor.GRAY + "Ability: " + ChatColor.RED + "Ultra High Efficiency & Damage");
setShopItem(gui, 15, "DIAMOND_SHOVEL", ChatColor.LIGHT_PURPLE + "" + ChatColor.BOLD + "Amethyst Shovel",
ChatColor.GRAY + "Ability: " + ChatColor.YELLOW + "Instant Digging");
player.openInventory(gui);
}
private void setShopItem(Inventory inventory, int slot, String material, String name, String ability) {
ItemStack item = createShopDisplayItem(material, name, ability);
if (item != null) {
inventory.setItem(slot, item);
}
}
private ItemStack createShopDisplayItem(String materialName, String name, String specialLore) {
ItemStack item = XMaterial.matchXMaterial(materialName).map(XMaterial::parseItem).orElse(null);
if (item == null) {
return null;
}
ItemMeta meta = item.getItemMeta();
if (meta == null) {
return null;
}
meta.setDisplayName(name);
List<String> lore = new ArrayList<>();
lore.add(specialLore);
lore.add(ChatColor.GRAY + "Price: " + ChatColor.GREEN + "$1,000,000");
lore.add("");
lore.add(ChatColor.YELLOW + "Click to buy!");
meta.setLore(lore);
item.setItemMeta(meta);
return item;
}
private ItemStack createCustomTool(String toolType) {
String materialName;
switch (toolType) {
case "amethyst_pickaxe" -> materialName = "DIAMOND_PICKAXE";
case "amethyst_axe" -> materialName = "DIAMOND_AXE";
case "amethyst_shovel" -> materialName = "DIAMOND_SHOVEL";
default -> {
return null;
}
}
ItemStack item = XMaterial.matchXMaterial(materialName).map(XMaterial::parseItem).orElse(null);
if (item == null) {
return null;
}
ItemMeta meta = item.getItemMeta();
if (meta == null) {
return null;
}
String toolName = switch (toolType) {
case "amethyst_pickaxe" -> "Amethyst Pickaxe";
case "amethyst_axe" -> "Amethyst Axe";
default -> "Amethyst Shovel";
};
meta.setDisplayName(ChatColor.LIGHT_PURPLE + "" + ChatColor.BOLD + toolName);
List<String> lore = new ArrayList<>();
lore.add(ChatColor.LIGHT_PURPLE + "✦ DonutSMP Special Tool");
if (toolType.equals("amethyst_pickaxe")) {
lore.add(ChatColor.GRAY + "Mines a " + ChatColor.GOLD + "3x3 (9 Blocks)" + ChatColor.GRAY + " area at once!");
}
meta.setLore(lore);
switch (toolType) {
case "amethyst_pickaxe" -> {
addEnchantment(meta, "EFFICIENCY", 5);
addEnchantment(meta, "UNBREAKING", 3);
addEnchantment(meta, "FORTUNE", 3);
}
case "amethyst_axe" -> {
addEnchantment(meta, "EFFICIENCY", 6);
addEnchantment(meta, "UNBREAKING", 3);
addEnchantment(meta, "SHARPNESS", 5);
}
case "amethyst_shovel" -> {
addEnchantment(meta, "EFFICIENCY", 5);
addEnchantment(meta, "UNBREAKING", 3);
}
default -> {
return null;
}
}
meta.getPersistentDataContainer().set(toolKey, PersistentDataType.STRING, toolType);
item.setItemMeta(meta);
return item;
}
private void addEnchantment(ItemMeta meta, String enchantmentName, int level) {
XEnchantment.matchXEnchantment(enchantmentName)
.ifPresent(enchantment -> meta.addEnchant(enchantment.getEnchant(), level, true));
}
@EventHandler
public void onInventoryClick(InventoryClickEvent event) {
if (!GUI_TITLE.equals(event.getView().getTitle())) {
return;
}
event.setCancelled(true);
if (!(event.getWhoClicked() instanceof Player player) || event.getRawSlot() < 0
|| event.getRawSlot() >= event.getView().getTopInventory().getSize()) {
return;
}