#Iterating and removing
1 messages ยท Page 1 of 1 (latest)
sure
Is there a way to put while iterating?
ty
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
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());
Ok ill try
Uhm
no errors but its not working
1 sec
Ok that part works
This part isnt though
its shooting fireballs
oh oops too small
send it as text pls
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
btw first a tiny thing
Item not taking damage
item.getType() returns an enum
you can use == instead of .equals
is easier to read
oh
what exactly isnt working?
It shoots fireballs but no flint and steels take damage
yeah
Im trying to make fireballs shoot out but take durability from the first flint and steel in the inventory
you must INCREMENT the damage ๐
you are repairing it by one every time
damage is 0 for a new item
oh
and then the number goes up
it's "damage", not "health" ๐
Why cant they just call it durability?
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
hmm
true
but cant you just see if its durability is equal to its maximum durability?
sure but
nothing is better
they just made a choice to call it damage and not durability
Wait items dont break automatically after they have less than 1 durability?
whyyyyyyy
I think it breaks when it has exactly 0
or when it goes fro m0 to -1
sth like that
Well it should have had 0 at one point
let me check it myself
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
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?
you are iterating over an array. all empty slots will be null