Help with plugin

Discussion in 'Plugin Development' started by Mr_maderator_UY, Feb 8, 2022.

Thread Status:
Not open for further replies.
  1. Offline

    Mr_maderator_UY

    Hello there, I whant to create plugin that can break ore without pickaxe.
    This plugin also spawn after break an ore ingot, but we get ingots when I in creative. How I can check game mode of player.
    Code:
    package com.ccs.core;
    
    import org.bukkit.GameMode;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    
    
    public class Break implements Listener {
        private final JavaPlugin plugin;
    
        public Break(JavaPlugin plugin) {
            this.plugin = plugin;
        }
    
        @EventHandler
        public void BreakP(BlockBreakEvent event) {
            Block blockBroken = event.getBlock();
            Player targetplayer = ;//idk what i need to write there
            if (targetplayer.getGameMode() == GameMode.SURVIVAL) {
                if (blockBroken.getType() == Material.DIAMOND_ORE) {
                    event.setCancelled(true);
                    blockBroken.setType(Material.AIR);
                    ItemStack diamond = new ItemStack(Material.DIAMOND, 1);
                    blockBroken.getWorld().dropItemNaturally(blockBroken.getLocation(), diamond);
                }
                if (blockBroken.getType() == Material.IRON_ORE) {
                    event.setCancelled(true);
                    blockBroken.setType(Material.AIR);
                    ItemStack iron = new ItemStack(Material.IRON_INGOT, 1);
                    blockBroken.getWorld().dropItemNaturally(blockBroken.getLocation(), iron);
                }
                if (blockBroken.getType() == Material.GOLD_ORE) {
                    event.setCancelled(true);
                    blockBroken.setType(Material.AIR);
                    ItemStack gold = new ItemStack(Material.GOLD_INGOT, 1);
                    blockBroken.getWorld().dropItemNaturally(blockBroken.getLocation(), gold);
                }
                if (blockBroken.getType() == Material.COAL_ORE) {
                    event.setCancelled(true);
                    blockBroken.setType(Material.AIR);
                    ItemStack coal = new ItemStack(Material.COAL, 1);
                    blockBroken.getWorld().dropItemNaturally(blockBroken.getLocation(), coal);
                }
                if (blockBroken.getType() == Material.REDSTONE_ORE) {
                    event.setCancelled(true);
                    blockBroken.setType(Material.AIR);
                    ItemStack redstone = new ItemStack(Material.REDSTONE, 1);
                    blockBroken.getWorld().dropItemNaturally(blockBroken.getLocation(), redstone);
                }
                if (blockBroken.getType() == Material.LAPIS_ORE) {
                    event.setCancelled(true);
                    blockBroken.setType(Material.AIR);
                    ItemStack dye = new ItemStack(Material.LAPIS_LAZULI, 1);
                    blockBroken.getWorld().dropItemNaturally(blockBroken.getLocation(), dye);
                }
    
            }
            if (targetplayer.getGameMode() == GameMode.CREATIVE) {
                if (blockBroken.getType() == Material.DIAMOND_ORE) {
                    event.setCancelled(true);
                    blockBroken.setType(Material.AIR);
                    ItemStack diamond_ore = new ItemStack(Material.DIAMOND_ORE, 1);
                    blockBroken.getWorld().dropItemNaturally(blockBroken.getLocation(), diamond_ore);
                }
                if (blockBroken.getType() == Material.IRON_ORE) {
                    event.setCancelled(true);
                    blockBroken.setType(Material.AIR);
                    ItemStack iron_ore = new ItemStack(Material.IRON_ORE, 1);
                    blockBroken.getWorld().dropItemNaturally(blockBroken.getLocation(), iron_ore);
                }
                if (blockBroken.getType() == Material.GOLD_ORE) {
                    event.setCancelled(true);
                    blockBroken.setType(Material.AIR);
                    ItemStack gold_ore = new ItemStack(Material.GOLD_ORE, 1);
                    blockBroken.getWorld().dropItemNaturally(blockBroken.getLocation(), gold_ore);
                }
                if (blockBroken.getType() == Material.COAL_ORE) {
                    event.setCancelled(true);
                    blockBroken.setType(Material.AIR);
                    ItemStack coal_ore = new ItemStack(Material.COAL_ORE, 1);
                    blockBroken.getWorld().dropItemNaturally(blockBroken.getLocation(), coal_ore);
                }
                if (blockBroken.getType() == Material.REDSTONE_ORE) {
                    event.setCancelled(true);
                    blockBroken.setType(Material.AIR);
                    ItemStack redstone_ore = new ItemStack(Material.REDSTONE_ORE, 1);
                    blockBroken.getWorld().dropItemNaturally(blockBroken.getLocation(), redstone_ore);
                }
                if (blockBroken.getType() == Material.LAPIS_ORE) {
                    event.setCancelled(true);
                    blockBroken.setType(Material.AIR);
                    ItemStack dye_ore = new ItemStack(Material.LAPIS_ORE, 1);
                    blockBroken.getWorld().dropItemNaturally(blockBroken.getLocation(), dye_ore);
                }
    
            }
        }
    
    }
    
     
  2. Offline

    timtower Administrator Administrator Moderator

  3. Offline

    Mr_maderator_UY

    hm, i didn't understand, where i need to put it:confused:
     
  4. Offline

    timtower Administrator Administrator Moderator

  5. Offline

    Mr_maderator_UY

    thanks[diamond]:)

    but how to ignore this code, when player will have pickaxe with skill touch?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Feb 8, 2022
  6. Offline

    timtower Administrator Administrator Moderator

    You check for it
     
  7. Offline

    Mr_maderator_UY

    if(player.getItemInHand.getType == .item.pickaxe)
    {
    //do something}
    but how add enchantings in pickaxe?

    or, I can add command into plug for example:
    execute as @a[nbt={SelectedItem:{id:"minecraft:bone",Count:8b},Inventory:[{Slot:-106b,id:"minecraft:ender_pearl",Count:1b}]}] run scoreboard players set @s PockySouls 1

    I think I don't understand:/

    I found ansver of a qusetion:

    Code:
     @EventHandler
        public void onBlockBreak(BlockBreakEvent e) {
            ItemStack stack = e.getPlayer().getInventory().getItemInMainHand();
           
            if (stack == null || stack.getType().name().contains("AIR")) return;
            if (!stack.getType().name().contains("PICKAXE")) return;
           
            if (hasEnchant(stack, Enchantment.DIG_SPEED)) {
                // do what you want;
            }
        }
    
        public static boolean hasEnchant(ItemStack stack, Enchantment ench) {
            if (!stack.hasItemMeta()) return false;
    
            final ItemMeta meta = stack.getItemMeta();
    
            return meta.hasEnchant(ench);
        }
    where DIG_SPEED - is name of enchant
    AIR - is lick null, because slots in inventorry can not be null
    PICKAXE - is a tool, which you want to check

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Feb 8, 2022
  8. Offline

    Strahan

    Are you targeting 1.8 or older? If not, you should be using getItemInMainHand() under their PlayerInventory object. That said, your code isn't formatted right. You need () after methods. Also no idea what .item.pickaxe is supposed to represent. You need to tell it what Material you want to check. Like
    Code:
    if (player.getInventory().getItemInMainHand().getType() == Material.DIAMOND_PICKAXE) {
      // they are holding a dpick
    }
    I know you can target tools easily with EnchantmentTarget.TOOL but alas that's tools in general, not just pickaxes. There isn't a target just for picks. I suppose one way around it to cover all picks easily would be:
    Code:
    if (p.getInventory().getItemInMainHand().getType().name().indexOf("_PICKAXE") > -1) {
      // they are holding a pick
    }
     
  9. Offline

    Mr_maderator_UY

    but what can i do whith enchants?
     
  10. Offline

    Strahan

    What do you mean? You said earlier about how to add enchants to a pickaxe. If you mean standard enchants:
    Code:
    ItemStack myItem = new ItemStack(Material.DIAMOND_PICKAXE);
    ItemMeta im = myItem.getItemMeta();
    im.addEnchant(Enchantment.DURABILITY, 3, false);
    myItem.setItemMeta(im);
    That would add unbreaking 3 to the item. If you wanted to add unbreaking 10, something that is outside the normal range, you'd set that false to true.

    If you meant a custom enchant, I never really made one but the way I'd go about it is to make my item glow and hide the item stats:
    Code:
    ItemStack myItem = new ItemStack(Material.DIAMOND_PICKAXE);
    ItemMeta im = myItem.getItemMeta();
    im.addEnchant(Enchantment.OXYGEN, 1, false);
    im.addItemFlags(ItemFlag.HIDE_ENCHANTS);
    im.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
    im.setLore(Arrays.asList("The super pick of destruction","","Blows up a lot of stuff"));
    myItem.setItemMeta(im);
    That will give me a pick that glows and doesn't show the usual "+6.25 attack damage". For the enchant to make it glow, pick something that doesn't work on the item. So I chose oxygen as it makes no sense for a pick. You don't want to inadvertently add real enchant effects.

    I use a lore to be able to tell what it is in the event. Well, only since it's 1.8. In modern MC, PersistentDataContainer is what I'd use as it's much better for this. Then I just listen to whatever applicable event (BlockBreakEvent in this case) and I check if the item in hand is the enchanted one and act accordingly.
     
Thread Status:
Not open for further replies.

Share This Page