Files
src
build.gradle
settings.gradle
Code
4 116
src/main/java/com/kodari/rainbowpickaxe/RainbowPickaxePlugin.java
package com.kodari.rainbowpickaxe;
import com.cryptomorin.xseries.XEnchantment;
import com.cryptomorin.xseries.XMaterial;
import com.cryptomorin.xseries.XPotion;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.NamespacedKey;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
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 org.bukkit.potion.PotionEffect;
import java.util.Arrays;
public final class RainbowPickaxePlugin extends JavaPlugin {
private NamespacedKey itemKey;
@Override
public void onEnable() {
itemKey = new NamespacedKey(this, "rainbow_pickaxe");
ShapedRecipe recipe = new ShapedRecipe(new NamespacedKey(this, "rainbow_pickaxe_recipe"), createPickaxe());
recipe.shape("DED", " S ", " S ");
recipe.setIngredient('D', XMaterial.matchXMaterial("DIAMOND").get().parseMaterial());
recipe.setIngredient('E', XMaterial.matchXMaterial("EMERALD").get().parseMaterial());
recipe.setIngredient('S', XMaterial.matchXMaterial("STICK").get().parseMaterial());
Bukkit.addRecipe(recipe);
Bukkit.getScheduler().runTaskTimer(this, this::applyHeldEffects, 0L, 20L);
}
private ItemStack createPickaxe() {
ItemStack item = XMaterial.matchXMaterial("DIAMOND_PICKAXE").get().parseItem();
ItemMeta meta = item.getItemMeta();
meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', "&cR&6a&ei&an&bb&9o&dw &fPickaxe"));
meta.setLore(Arrays.asList(ChatColor.GRAY + "Faster than an ordinary diamond pickaxe.",
ChatColor.LIGHT_PURPLE + "Hold for Speed and Haste!"));
meta.getPersistentDataContainer().set(itemKey, PersistentDataType.BYTE, (byte) 1);
XEnchantment.matchXEnchantment("EFFICIENCY").ifPresent(enchantment ->
meta.addEnchant(enchantment.getEnchant(), 6, true));
item.setItemMeta(meta);
return item;
}
private void applyHeldEffects() {
for (Player player : Bukkit.getOnlinePlayers()) {
ItemStack item = player.getInventory().getItemInMainHand();
ItemMeta meta = item.getItemMeta();
if (meta == null || !meta.getPersistentDataContainer().has(itemKey, PersistentDataType.BYTE)) {
continue;
}
applyEffect(player, "SPEED");
applyEffect(player, "HASTE");
}
}
private void applyEffect(Player player, String effectName) {
XPotion.matchXPotion(effectName).ifPresent(potion -> {
PotionEffect effect = potion.buildPotionEffect(60, 0);
PotionEffect current = player.getPotionEffect(effect.getType());
if (current == null || (current.getAmplifier() == 0 && current.getDuration() <= 40)) {
player.addPotionEffect(effect);
}
});
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player)) {
sender.sendMessage("Only players can receive a Rainbow Pickaxe.");
return true;
}
Player player = (Player) sender;
player.getInventory().addItem(createPickaxe()).values().forEach(item ->
player.getWorld().dropItemNaturally(player.getLocation(), item));
player.sendMessage(ChatColor.GREEN + "You received a Rainbow Pickaxe!");
return true;
}
}