#Iterating and removing

1 messages ยท Page 1 of 1 (latest)

lapis violet
#

hi

#

lets talk here

#

Can you show your loop code if you still got questions?

fallen sun
#

Trying the iterator rn

#

1 sec

lapis violet
#

sure

fallen sun
#

Is there a way to put while iterating?

lapis violet
#

sure

#

one sec

fallen sun
#

ty

lapis violet
#

Just do iterator.add(newElement)

#

oh wait

#

there isn't

#

I'd just do this:

#

Create a list with all new items before your loop

#

then in your loop, all the items to the list

#

after you're done looping, just use addAll or add on your collection

#

if you show your code we might find a better solution

fallen sun
#

Yeah that would probably work

#

1 sec tho

#

uh

#

ill try that

lapis violet
#

btw

#

are you looping over a list?

fallen sun
#

oh

#

forgot to say it lol

#

hashmap

lapis violet
#

then simply create a new HashMap before your loop, and add all elements that you freshly added after your original loop

#

sth like this:

#
HashMap<String,String> myMap = new HashMap<>();
        
        HashMap<String,String> newEntries = new HashMap<>();
        
        Iterator<Map.Entry<String,String>> iterator = myMap.entrySet().iterator();
        while(iterator.hasNext()) {
            Map.Entry<String,String> entry = iterator.next();
            
            // To remove something:
            iterator.remove();
            
            // To add something:
            newEntries.put("bla","blabla");
        }
        
        myMap.entrySet().addAll(newEntries.entrySet());
fallen sun
#

Ok ill try

lapis violet
#

with maps it's a bit ugly because you have to use the EntrySet

#

but it'll work

fallen sun
#

Uhm

#

no errors but its not working

#

1 sec

#

Ok that part works

#

This part isnt though

#

its shooting fireballs

#

oh oops too small

lapis violet
#

send it as text pls

fallen sun
#
                            if(item.getType() == (Material.FLINT_AND_STEEL)) {
                                if(item.getItemMeta() instanceof Damageable) {
                                    Damageable itemMeta = (Damageable)item.getItemMeta();
                                    itemMeta.setDamage(itemMeta.getDamage() - 1);
                                    
                                    item.setItemMeta(itemMeta);
                                    
                                    SmallFireball fireball = player.launchProjectile(SmallFireball.class, player.getLocation().getDirection());
                                    fireballs.put(fireball, 6f);
                                }
                                break;
                            }
                        }```This is my code for shooting
#

discord makes it messy ig

lapis violet
#

btw first a tiny thing

fallen sun
#

Item not taking damage

lapis violet
#

item.getType() returns an enum

#

you can use == instead of .equals

#

is easier to read

fallen sun
#

oh

lapis violet
#

what exactly isnt working?

fallen sun
#

It shoots fireballs but no flint and steels take damage

lapis violet
#

yeah

fallen sun
#

Im trying to make fireballs shoot out but take durability from the first flint and steel in the inventory

lapis violet
#

you must INCREMENT the damage ๐Ÿ˜›

#

you are repairing it by one every time

#

damage is 0 for a new item

fallen sun
#

oh

lapis violet
#

and then the number goes up

fallen sun
#

OH

#

WHY

#

e

lapis violet
#

it's "damage", not "health" ๐Ÿ˜„

fallen sun
#

Why cant they just call it durability?

lapis violet
#

I have a good reason

#

you can check if an item is new when damage is 0

#

but when durability is 37

#

it's almost broken when it's diamond but for gold it would be almost new

#

so you always know 0 is undamaged

#

without having to check what the max durability is

fallen sun
#

hmm

#

true

#

but cant you just see if its durability is equal to its maximum durability?

lapis violet
#

sure but

#

nothing is better

#

they just made a choice to call it damage and not durability

fallen sun
#

Wait items dont break automatically after they have less than 1 durability?

#

whyyyyyyy

lapis violet
#

they should

#

or wait

fallen sun
lapis violet
#

I think it breaks when it has exactly 0

#

or when it goes fro m0 to -1

#

sth like that

fallen sun
#

Well it should have had 0 at one point

lapis violet
#

let me check it myself

fallen sun
#

Next im going to make myself suffer and try to make a water hose

#

once this starts working

#

so

#

Same code mostly

#

just water bottles

lapis violet
#

yeah it really doesnt break

#

strange^^

fallen sun
#

Idk how to break it lol

#

Item is null?

#

says item is null while iterating

#

through player inventory

#

It shouldnt be

#

I tried using the list thing again

#

I think its because its making a new instance of item and settings that to air

#
                            if(item.getType() == (Material.FLINT_AND_STEEL) && !item.getItemMeta().getDisplayName().equals(ChatColor.RED + "Flame Thrower")) {
                                if(item.getItemMeta() instanceof Damageable) {
                                    Damageable itemMeta = (Damageable)item.getItemMeta();
                                    itemMeta.setDamage(itemMeta.getDamage() + 1);
                                    
                                    item.setItemMeta(itemMeta);
                                    
                                    if(itemMeta.getDamage() <= 0) {
                                        
                                    }
                                                                        
                                    SmallFireball fireball = player.launchProjectile(SmallFireball.class, player.getLocation().getDirection());
                                    fireballs.put(fireball, 6f);
                                }
                                break;
                            }
                        }
                        
                        toDelete.forEach(item -> {
                            item.setType(Material.AIR);
                        });```
#

and not the one the player has

#

But how can I remove the one the player has?

lapis violet