#Applying potion to certain players

1 messages · Page 1 of 1 (latest)

bold pike
rotund tapir
#

ahh

bold pike
#

So

rotund tapir
#

sup mate

bold pike
#

What you want
All players with your helmet get a potion every X seconds, all others should not

#

Right?

rotund tapir
#

yes, i want it so that the player who wears the custom helmet, gets haste

#

and it loops

#

so its like permament haste until you remove the helm

#

like beacons

bold pike
#

yes

#

That's why you do this:

rotund tapir
#

if you get out of the beacon range, it stops

bold pike
#

In your onEnable, create a syncRepeatingTask

#

inside that, loop over all players

#

inside that loop, apply the effect to all players that have your helmet

#

that's it

rotund tapir
#

yes true

#

oh just syncRepeatingTask?

#

thats a thing?

#

stuff here right?

bold pike
#

oh you're using a runnable

#

yeah that you can use too

#

exactly, at //stuff here you add the for loop

#

that goes through all online players

rotund tapir
#

so something like

bold pike
#

btw it would be nice if your screenshots would show a bit more context

rotund tapir
#

wait lemme try stuff out and get back to ya

#

ok next time i will

#

';' expected

#

why da heck

bold pike
#

BRO

#

STOP USING

#

THE WHILE LOOP

rotund tapir
#

oops

bold pike
#

Okay I will now literally write it for you

rotund tapir
#

._.

#

sorry

bold pike
#

np, the problem is just, that you don't try to understand what your code does 😄

#
@Override
    public void onEnable() {
        Bukkit.getScheduler().scheduleSyncRepeatingTask(this, () -> {
            for(Player player : Bukkit.getOnlinePlayers()) {
                if(isMinerHelmet(player.getInventory().getHelmet())) {
                    player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 2, 1));
                }
            }
        }, 20, 20); // 20 means every second
    }
#

The while loop will never end

#

it will run forever

#

and you do not even need it

#

basically the syncRepeatingTask is what we are using instead of your while loop

rotund tapir
#

ok tysm

#

the thing of spoonfeeding is

bold pike
#

oh wait

rotund tapir
#

you get spoonfed once

bold pike
#

in the PotionEffect thing, replace the 2 with a 40

rotund tapir
#

then you dont need it anymore

#

yes ofc

bold pike
#

40 ticks = 2 seconds

rotund tapir
#

im not that dumb lol

bold pike
#

yeah I thought it was using seconds

rotund tapir
#

its all fine

#

idk what is happening

bold pike
#

what is happening?

rotund tapir
#

i will test some stuf

#

if i cant find it out

#

i will ask

#

it doesnt give me the helm

#

lemme recompile

#

nope, no item

bold pike
#

Show your code again pls

rotund tapir
#

of what

bold pike
#

well you say it doesnt give you any item

#

so show the code that gives players an item

rotund tapir
#
    public static ItemStack minerHelmet;

    public static void init(){
        createMinerHelmet();
    }

    public static void createMinerHelmet(){


        {
            ItemStack item = new ItemStack(Material.IRON_HELMET);
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName("§6Miner helmet");
            List<String> minerHelmetLore = new ArrayList<>();
            minerHelmetLore.add("§7Wear this helmet");
            minerHelmetLore.add("§7to gain §e:pick: Haste 1!");
            minerHelmetLore.add("");
            minerHelmetLore.add("§6§lLEGENDARY");
            meta.setLore(minerHelmetLore);
            item.setItemMeta(meta);
            meta.getPersistentDataContainer().set(new NamespacedKey(SMPCore.getInstance(), "minerhelmet"), PersistentDataType.BYTE, (byte) 1);


            minerHelmet = item;
``` this is in main
bold pike
#

you don't set the PDC tag

#

you are using item.setItemMeta and AFTER that add the PDC to the itemmeta

#

switch the item.setItemMeta and meta.getPersistent... lines

rotund tapir
#

ah ok

#

pretty dumb of me

#

nope it still doesnt give me the helm on command

#
public class GiveHelm implements CommandExecutor {
    @Override
    public boolean onCommand(CommandSender sender, Command command, String s, String[] strings){
        if (command.getName().equalsIgnoreCase("givehelm")) {

                ( (Player) sender ).getInventory().addItem(SMPCore.minerHelmet);
            }
        return true;
        }

    }
#

thats my command to give helm

bold pike
#

does the onCommand even run?

#

add a message to see if it actually runs

rotund tapir
#

ok

#

uh oh

#

looks like a command problem

bold pike
#

by the way

#

the if(command.getName()...) is useless

#

your executor will only run for the commands you registered it to

#

you did register the commandexecutor, did you?

rotund tapir
#

yes i did ofc

bold pike
#

show the code pls

#

for registering it

rotund tapir
#

ok

#

getCommand("givehelm").setExecutor(new GiveHelm());

bold pike
#

that's in your onEnable()?

rotund tapir
#

wait what

#

yes

#

all commands stopped

bold pike
#

send your latest.log pls

rotund tapir
#

oh i see why

#

ok i will

#

oh didnt notice that

#

org.bukkit.inventory.ItemStack.hasItemMeta()" because "itemToCheck" is null

bold pike
#

whoa you have a ton of errors

#

anyway you do not even need any ArmorEquipEvent

#

I'd just completely remove that event

rotund tapir
#

okay

#

as i like to call it

#

APIPhobia

#

ooo

#

now it works

bold pike
#

nice

rotund tapir
#

1 line of code messed up the whole plugin

#

how the hell

#

ty for the help good human

bold pike
#

np 😄

rotund tapir
#

@bold pike are you still there?

bold pike
#

yes

rotund tapir
#

so

#

to prevent new players (in trash armor) to mine out my fossil boss. how can i make a custom block for the boss

#

currently i have this

#

thats fine and all

#

but im experiencing problems in block break event

#

which ik how to fix, but not how to fix with custom items

bold pike
#

a custom block? wdym?

rotund tapir
#

a custom itemstack

#

like this, but a block

bold pike
#

all the custom itemmeta is lost when you place your itemstack

#

you have to use my CUstomBlockData library to store information inside blocks

#

it's in the spigot forums

rotund tapir
#

ok

#

is it hard to do?

bold pike
rotund tapir
#

for a spigot beginner

bold pike
#

no it basically just provides a PDC for blocks

rotund tapir
#

alright

bold pike
#

but on blockplaceevent, you have to manually add the information to the block again

rotund tapir
#

oh its an api

#

api/lib

bold pike
#

I think there's an example plugin linked in the thread or github repo

#

yes you can include it with maven into your plugin

rotund tapir
#

i have never figured out how to put it in

#

WHERE IS THEIR JAR DOWNLAOD

bold pike
#

hm?

#

what .jar download?

rotund tapir
#

i cant find the file download

bold pike
#

for my library?

rotund tapir
#

oh its yours

#

lmfao

bold pike
#

maven downloads it automatically from the repository

rotund tapir
#

ohh

bold pike
#

you dont need to download it yourself

#

just follow the instructions there in the "maven" part

rotund tapir
#

thanks Kody Simpson for the confusion!

#

you german i see

bold pike
#

yes

rotund tapir
#

ja ja ich lehre deutsch

bold pike
#

😄

#

viel erfolg!

rotund tapir
#

sorry i only know like

#

uh

#

A2

#

not even A2 lol

#

mein deutsch ist blōd

bold pike
#

nein, es ist ganz gut 😄

rotund tapir
#

ik what that means

#

but idk how to answer

rotund tapir
rotund tapir
#

yo @bold pike i have an honest question

#

is it polite to ask "ich mōchte" in a restaurant?

#

but like how can i set the data?

#

i cant figure it ou t

bold pike
#

"Ich hätte gerne ..."

bold pike
#
@EventHandler
    public void onPlaceBlock(BlockPlaceEvent event) {
        if(event.getItemInHand().getItemMeta().getPersistentDataContainer().has(/* Check if this is your custom item */)) {
            // This is our custom item! Let's set the custom block data
            CustomBlockData cbd = new CustomBlockData(event.getBlock(), yourPlugin);
            cbd.set(new NamespacedKey(myPlugin,"mydata", PersistentDataType.STRING, "my custom data"));
        }
    }

@rotund tapir

rotund tapir
#

thanks

#

what do i replace myPlugin with?

#

main class?

bold pike
#

a reference to your main class, yes

rotund tapir
#
 if (e.getItemInHand().getItemMeta().getPersistentDataContainer().has(SMPCore.boss_spawner)) {
        CustomBlockData cbd = new CustomBlockData(e.getBlock(), SMPCore.getInstance());
        cbd.set(new NamespacedKey(SMPCore.getInstance(), "mydata", PersistentDataType.STRING, "my custom data"));
bold pike
#

sorry I made mistake above

#

move the second last ) behind "mydata"

#

cbd.set(new NamespacedKey(SMPCore.getInstance(), "mydata"), PersistentDataType.STRING, "my custom data");

rotund tapir
#

i see i see

#

and what is this fuss about?

bold pike
#

it says that getItemMeta COULD be null

#

but only when the player doesnt have an item in their hand

rotund tapir
#

not that

#

but on the right

#

its colored red

bold pike
#

has() needs to be provided a NamespacedKey

rotund tapir
#

so that means uh

rotund tapir
#

ok

bold pike
#

that means you must do things like

if(pdc.has(new NamespacedKey(myPlugin,"mydata"),PersistentDataType.STRING))

rotund tapir
#

yo @bold pike quick question before closing this thread

bold pike
#

yes?

rotund tapir
#

so

#

my server has a thing with rng

#

there is a chance to get a thing when breaking a specific block

#

all of them work fine, except

#
 public static void onMine(BlockBreakEvent e) {
        if (e.getBlock().getType() == Material.DIAMOND_ORE || e.getBlock().getType() == Material.DEEPSLATE_DIAMOND_ORE || e.getBlock().getType() == Material.EMERALD_ORE || e.getBlock().getType() == Material.DEEPSLATE_EMERALD_ORE) {
            int random = ThreadLocalRandom.current().nextInt(10000);
            if (random == 1000) {
                e.getBlock().getDrops().add(new ItemStack(Material.IRON_INGOT));
                e.getPlayer().sendMessage("§f§lCOMMON!" + "§7 1x Iron Ingot");


            } else if (random == 100) {
                e.getBlock().getDrops().add(new ItemStack(Material.NETHERITE_SCRAP, 2));
                e.getPlayer().sendMessage("§9§lRARE DROP!" + "§7 2x Netherite Scrap");
            } else if (random == 10) {
                e.getBlock().getDrops().add(new ItemStack(Material.NETHERITE_INGOT, 1));
                e.getPlayer().sendMessage("§9§lRARE DROP!" + "§7 1x Netherite Ingot");
            } else if (random == 1) {
                e.getBlock().getDrops().add(new ItemStack(Material.DIAMOND_BLOCK, 2));
                e.getPlayer().sendMessage("§6§lEPIC DROP!" + "§7 2X Diamond Block");
            }
        }
        }
    }

`

#

oops

#

should i invert the random chances?

#

instead of going high to low, make it go low to high?

bold pike
#

you're doing it wrong

#

you are getting an int between 0 and 10000

#

so 10000 possible values

rotund tapir
#

yeah

bold pike
#

then you check 4 times for a specific value

#

meaning every of your drop changes is 1/10000 = 0.01%

rotund tapir
#

but then

bold pike
#

the chance for random == 1000 is exactly as high as for random = 1

#

0.01%

#

it's like throwing a dice and saying when dice == 3 do this, when dice == 2 do that

rotund tapir
#

why does this work then?

bold pike
#

it's 1/6 chance everytime

#

because there you are checking if the number is smaller and not equal to your desired chance

rotund tapir
#

ah ok

bold pike
#

if (random <= 1000) = 10% chance

#

if(random <= 100) = 1% chance

#

oh btw and your first line, you do this:

        if (e.getBlock().getType() == Material.DIAMOND_ORE || e.getBlock().getType() == Material.DEEPSLATE_DIAMOND_ORE || e.getBlock().getType() == Material.EMERALD_ORE || e.getBlock().getType() == Material.DEEPSLATE_EMERALD_ORE) {
#

I'd rather do it like this:

rotund tapir
#
 int random = ThreadLocalRandom.current().nextInt(10000);
            if (random <= 1) {
                e.getBlock().getDrops().add(new ItemStack(Material.DIAMOND_BLOCK, 2));
                e.getPlayer().sendMessage("§6§lEPIC DROP!" + "§7 2X Diamond Block");
            } else if (random <= 10) {
                e.getBlock().getDrops().add(new ItemStack(Material.NETHERITE_INGOT, 1));
                e.getPlayer().sendMessage("§9§lRARE DROP!" + "§7 1x Netherite Ingot");
            } else if (random <= 100) {

                e.getBlock().getDrops().add(new ItemStack(Material.NETHERITE_SCRAP, 2));
                e.getPlayer().sendMessage("§9§lRARE DROP!" + "§7 2x Netherite Scrap");
            } else if (random <= 1000) {

                e.getBlock().getDrops().add(new ItemStack(Material.IRON_INGOT));
                e.getPlayer().sendMessage("§f§lCOMMON!" + "§7 1x Iron Ingot");
            }
        }
        }
    }
#

so like this?

bold pike
#

wait pls

#

a few things

rotund tapir
#

ok

bold pike
#

first of all I'd replace the check for the blocks

#

hardcoding all that stuff in the event is a bit dirty

#

and yeah, you have to switch the order like you already did

#

oh and also

#

you cannot add to the getDrops list

rotund tapir
#

ok brb

bold pike
#
private static final Set<Material> ores = new HashSet<>(Arrays.asList(
            Material.DIAMOND_ORE,
            Material.DEEPSLATE_DIAMOND_ORE,
            Material.EMERALD_ORE,
            Material.DEEPSLATE_EMERALD_ORE));

    public static void onMine(BlockBreakEvent e) {
        if (!ores.contains(e.getBlock().getType())) return;
        
        // Do you random stuff here
        ...
    }
#

first, you cannot just add to the getDrops list

#

and second, if it would work to add items, that wouldnt affect the drops

rotund tapir
#

yeah i had a problem

#

if i do getPlayer.getInventory.add(blah blah blah)

bold pike
#

Block#getDrops doesnt set the drops for this specific block, but rather just returns a list of drops that this block WOULD drop

rotund tapir
#

and the inventory was full

#

then it wouldnt add

bold pike
#

simply drop the item yourself

rotund tapir
#

getPlayer().drop() ?

#

something like that

#

?

bold pike
#
e.getBlock().getWorld().dropItemNaturally(e.getBlock().getLocation(), new ItemStack(Material.IRON_INGOT));
rotund tapir
#

ok brb gotta go eat lunch/dunch

#

sorry

bold pike
#

but I wouldnt repeat all this logic everytime. Better create a variable "drop" and "message", and at the bottom of your code, check whether they are null, and if not, call the drop logic

#

otherwise you repeat the same two lines 4 times

#

basically sth like this

        ItemStack bonusDrop = null;
        String message = null;

        if (random <= 1000) {
            bonusDrop = new ItemStack(Material.IRON_INGOT);
            message = "§f§lCOMMON!" + "§7 1x Iron Ingot";
        } else if (random <= 100) {
            bonusDrop = new ItemStack(Material.NETHERITE_SCRAP, 2);
            message = "§9§lRARE DROP!" + "§7 2x Netherite Scrap";
         }

         if(bonusDrop != null) {
             e.getBlock().getWorld().dropItemNaturally(e.getBlock().getLocation(), bonusDrop); 
             player.sendMessage(message);
         }
rotund tapir
#

alright thanks mate!

#

oh wow that actually makes sense

#

okay thx

#

too lazy to start up my localhost

#
   private static final Set<Material> ores;

    static{
        ores = new HashSet<>(Arrays.asList(
                Material.DIAMOND_ORE,
                Material.DEEPSLATE_DIAMOND_ORE,
                Material.EMERALD_ORE,
                Material.DEEPSLATE_EMERALD_ORE));
    }

    @EventHandler

    public static void onMine(BlockBreakEvent e) {
        if (!ores.contains(e.getBlock().getType())) return;
        else  {
            int random = ThreadLocalRandom.current().nextInt(10000);
            ItemStack bonusDrop = null;
            String message = null;
            Player player = e.getPlayer();
            if (random <= 1) {
                bonusDrop = new ItemStack(Material.DIAMOND_BLOCK, 2);
                message = "§6§lEPIC DROP!" + "§7 2X Diamond Block";
            } else if (random <= 10) {
           bonusDrop = new ItemStack(Material.NETHERITE_INGOT);
                message = "§9§lRARE DROP!" + "§7 1x Netherite Ingot";
            } else if (random <= 100) {

               bonusDrop = new ItemStack(Material.NETHERITE_SCRAP, 2);
                message = "§9§lRARE DROP!" + "§7 2x Netherite Scrap";
            } else if (random <= 1000) {

                bonusDrop = new ItemStack(Material.IRON_INGOT);
                message = "§f§lCOMMON!" + "§7 1x Iron Ingot";
            } if (bonusDrop != null) {
                e.getBlock().getWorld().dropItemNaturally(e.getBlock().getLocation(), bonusDrop);
                player.sendMessage(message);
            }
        }
        }
    }

this should work right?

#

ok update: it works. thanks

bold pike
#

np 🙂

rotund tapir
#

still cant figure out on how to do the java thing

#
System.out.println("text")
#

????

bold pike
#

@rotund tapir

rotund tapir
#
System.out.println("Hello, world!")
#

there we go

bold pike
#

yaay

rotund tapir
#

ngl i feel like minikloon