#Custom Snowball Entity data?

1 messages · Page 1 of 1 (latest)

primal pawn
#

Is there any possible way i can apply custom entity data to a snowball? I wish to use an event to throw a snowball that does stuff but yet not effect all snowballs in the world.

timber shell
#

You can apply metadata to a snowball.

flint wing
#

You can use its persistent data container

primal pawn
timber shell
#

Metadata would be for the entity. The PDC value would be for the ItemStack.

flint wing
#

wat

#

you can have pdc for entities as well

flint wing
#

metadata is unreliable and should be distanced from at all cost in principle

#

it's transient in such an arbitrary way sadly

primal pawn
#

I'm guessing metadata is when i also setmeta to my item for custom names?

flint wing
#

no

primal pawn
#

That would be NBT?

timber shell
#

That’s ItemMeta.

primal pawn
#

Ah

flint wing
#

but the implementation is deplorably fragile

primal pawn
#

hmm

flint wing
#

like its just one big map and when entities are considered invalid the values will just unload

primal pawn
#

Ah that sucks

flint wing
#

which uses nbt

#

but thats not a big deal in general

primal pawn
#

So how might i add a custom tag that i can use to do things with the snowball later?

#

Cause all i see in the pdh thing you sent is to get the data containter

#

container*

timber shell
#

I think the PDC requires the use of NamespacedKeys. So you could make one of those and use it later.

#

Or initialize it in a class and use it as needed.

flint wing
#
var pdc = entity.getPersistentDataContainer();
var pdc = itemMeta.getPersistentDataContainer();
var key = new NamespacedKey(pluginInstance, "KeyName");

//set, remember for itemMeta you need to use itemStack.setItemMeta(itemMeta) afterwards
pdc.set(key,PersistentDataType.BYTE,Byte.valueOf((byte)1));

boolean isFlagged = pdc.getOrDefault(key,PersistentDataType.BYTE,Byte.valueOf((byte)0)) == 1;
#

or sth

#

it was some months age since I truly touched spigot

#

but thats the fundamentals

primal pawn
#

I am still confused...

timber shell
#

I thought that you needed to set the PDC values before you set the ItemMeta since it’s part of the ItemMeta class?

flint wing
#

yes exactly

timber shell
#

In your example you set it before you add the PDC. 😛

flint wing
#

?

#

//set, remember for itemMeta you need to use itemStack.setItemMeta(itemMeta) afterwards

timber shell
#

But right under that you are setting a value to it. Would that update on the item even though you already set the ItemMeta?

flint wing
#

I never set the item meta back in that example

#

hence the comment

#

but yes itemmetas are just snapshots

primal pawn
#

So... How would i set a tag as though i used /tag?

flint wing
#

oh thats scoreboard

#

but just

#

entity.addScoreboardTag(string)

#

and you also got removeScoreboardTag

primal pawn
#

Yeah that stuff, but i want to apply the tag to the snowball and then have each snowball have it's own timer (so they don't all trigger on the same tick) and then say explode after 20 ticks or something

primal pawn
#

Or can i make the entity with the tag then spawn it?

flint wing
#

I am quite sure you can invoke that method in the spawn callback

#

with the Consumer

primal pawn
#

How might i do that?

Cause this doesn't error but i'm not sure how to get past it:

Snowball snowball = player.getWorld().spawn(player.getEyeLocation(), Snowball.class);```
#

With the spawn thing i assume i could go

Snowball snowball = player.getWorld().spawn(player.getEyeLocation(), Snowball.class, false, snowball.addScoreboardTag("String stuff"));```
#

But i don't think that would work

#

cause well snowball isn't defined yet

#

I guess spawn then do snowball.addScoreboardTag(string) after?

flint wing
#

no

primal pawn
#

oh

flint wing
#

false, ent -> ent.addScoreboardTag("String stuff")

primal pawn
#

ah

flint wing
#

its a consumer, so a bit special but ye

#

there are some great tutorials on functional interfaces if you wanna learn that

primal pawn
#

maybe

#

Now i just have to figure out how to make the tagged snowballs have their own timer then do something after that timer

flint wing
#

perhaps store time stamps in the form of longs

#

by namely using System::currentTimeMillis

primal pawn
#

I could just use the snowball life time right?

#

something along the lines of

if snowball lifetime = 20 do stuff

#

The only thing is i'm used to looping things (cause i mainly coded python) and so i'm not sure how the snowball would know when it's lifetime is at 20 since i'm not checking on a loop

flint wing
#

compare absolute time as I mentioned

primal pawn
#

But how would i compare that on a loop and not just once when my interact event is over?

flint wing
#

you dont use a loop

#

or well

primal pawn
#

I know but i'm not sure what else to call it

flint wing
#

what exactly are you trying to do with these snowballs

primal pawn
#

When i use a snowball with a custom itemstack stuff (which i already have made and working) I wish for the event to throw a custom snowball with a tag like "shield" or something, and then after 1 second the snowball spawns in a small wall and gets rid of the snowball

#

But i don't want all snowballs to do that so i can do different stuff with different itemstacks that are all using snowballs

flint wing
#

oh right

#

well you're better off the scheduler then

primal pawn
#

ah

#

I don't know what is either

flint wing
#

take a look at that :3

#

basically scheduling code execution in bukkit

primal pawn
#

ah

flint wing
#

in py you'd probably use a thread along with time.sleep or sth

primal pawn
#

Yeah

#

Is it async so other things can run at the same time?

flint wing
#

well

#

it queues the runnable to a queue

primal pawn
#

hm

#

So if i had 2 snowballs spawned at once would the second one wait for the other to finish?

flint wing
#

and then executed on the main thread when the right tick fires

#

nah

#

Bukkit.getScheduler().runTaskLater(plugin,() -> {

},20L);

#

code inside that runs on the server thread after 20 ticks

primal pawn
#

Ah

#

okay so... Do i need to implement scheduler like i do listener?

#

also if so does that mean i need a new class for this and can't put it in the same class as my event?

flint wing
#

the only thing that can effectively change its execution is if the tps is lower, or if the server unprecedentedly shutdowns

primal pawn
#

Ah

flint wing
#

no

#

you dont implement it

primal pawn
#

👍

flint wing
#

its just an anonymous function that you pass

#

similar to pythons lambda

#

but can be multiline in java

primal pawn
#

I think i have managed to confuse myself again

#

cause bukkit is red

#

Because it can't resolve the symbol

#

BukkitScheduler is fine though

#

🤦 I probably need to pass plugin to the method

#

BukkitScheduler scheduler = plugin.getServer().getScheduler(); this works?

flint wing
#

yes

primal pawn
#

How do i run code on the tagged entities?

#

well.. Execute code at tagged entities location then remove the entity

#

also what is the 20L * 30L mean in terms of time?

#

ah nvm

#

20 is the ticks and then the 30 is turning the ticks into seconds

#

So i can just put 20 for 1 second

primal pawn
#

(i'll be back later, i'm gonna eat then go play outside for a bit)

primal pawn
#

(back-ish)