#Applying potion to certain players
1 messages · Page 1 of 1 (latest)
ahh
So
sup mate
What you want
All players with your helmet get a potion every X seconds, all others should not
Right?
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
if you get out of the beacon range, it stops
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
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
so something like
btw it would be nice if your screenshots would show a bit more context
wait lemme try stuff out and get back to ya
ok next time i will
';' expected
why da heck
oops
Okay I will now literally write it for you
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
oh wait
you get spoonfed once
in the PotionEffect thing, replace the 2 with a 40
40 ticks = 2 seconds
im not that dumb lol
yeah I thought it was using seconds
what is happening?
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
Show your code again pls
of what
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
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
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
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?
yes i did ofc
that's in your onEnable()?
send your latest.log pls
oh i see why
ok i will
oh didnt notice that
org.bukkit.inventory.ItemStack.hasItemMeta()" because "itemToCheck" is null
whoa you have a ton of errors
anyway you do not even need any ArmorEquipEvent
I'd just completely remove that event
nice
1 line of code messed up the whole plugin
how the hell
ty for the help good human
np 😄
@bold pike are you still there?
yes
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
a custom block? wdym?
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
for a spigot beginner
no it basically just provides a PDC for blocks
alright
but on blockplaceevent, you have to manually add the information to the block again
I think there's an example plugin linked in the thread or github repo
yes you can include it with maven into your plugin
i cant find the file download
for my library?
maven downloads it automatically from the repository
ohh
you dont need to download it yourself
just follow the instructions there in the "maven" part
yes
ja ja ich lehre deutsch
nein, es ist ganz gut 😄
ok danke
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
better would be
"Ich hätte gerne ..."
Okay Ill give an example, one sec
@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
a reference to your main class, yes
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"));
sorry I made mistake above
move the second last ) behind "mydata"
cbd.set(new NamespacedKey(SMPCore.getInstance(), "mydata"), PersistentDataType.STRING, "my custom data");
it says that getItemMeta COULD be null
but only when the player doesnt have an item in their hand
has() needs to be provided a NamespacedKey
so that means uh
declaration: package: org.bukkit.persistence, interface: PersistentDataContainer
ok
that means you must do things like
if(pdc.has(new NamespacedKey(myPlugin,"mydata"),PersistentDataType.STRING))
yo @bold pike quick question before closing this thread
yes?
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?
you're doing it wrong
you are getting an int between 0 and 10000
so 10000 possible values
yeah
then you check 4 times for a specific value
meaning every of your drop changes is 1/10000 = 0.01%
but then
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
why does this work then?
it's 1/6 chance everytime
because there you are checking if the number is smaller and not equal to your desired chance
ah ok
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:
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?
ok
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
ok brb
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
Block#getDrops doesnt set the drops for this specific block, but rather just returns a list of drops that this block WOULD drop
simply drop the item yourself
e.getBlock().getWorld().dropItemNaturally(e.getBlock().getLocation(), new ItemStack(Material.IRON_INGOT));
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);
}
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
np 🙂
still cant figure out on how to do the java thing
System.out.println("text")
????
yaay
ngl i feel like minikloon