KubeJS

KubeJS

69M Downloads

[1.16.5] entity.drop addDrop with chance not working when chance below 1

pjc21 opened this issue ยท 2 comments

commented

When adding drops with chance if chance is below 1 it seems to break/stops adding. Not getting any errors in log

var zombieDrops = [
	{ item: 'minecraft:stone_sword', chance: 1.0 },
	{ item: 'minecraft:apple', chance: 0.9 },
	{ item: '4x minecraft:diamond', chance: 0.5 },
	{ item: 'minecraft:gold_ingot', chance: 0.75 },
	{ item: 'minecraft:emerald', chance: 1.0 }    
]

onEvent('entity.drops', event => {
	
	var entity = event.getEntity()
	
	if (entity.getName() == "Zombie") {
		
		var dropsList = event.getDrops()
		
		dropsList.clear()
		
		utils.listOf(zombieDrops).forEach(function(drop) {
			event.addDrop(Item.of(drop.item, drop.chance))
		})
	}
}) 

Only the sword will be added. If I change the apple's chance to 1 then just the sword & apple are added,, never the emerald or anything else. Seems to break after the 1st chance below 1.
Not sure if I'm doing something wrong but seems like maybe event.getEntity().level.random.nextFloat() is always returning null.

Assigning a random and checking against that works as expected like below.

onEvent('entity.drops', event => {
	
	var entity = event.getEntity()
	
	if (entity.getName() == "Zombie") {
		
		var dropsList = event.getDrops()
		
		dropsList.clear()
		
		utils.listOf(zombieDrops).forEach(function(drop) {
			
			var random = utils.getRandom().nextFloat()

			if (random < drop.chance) {
				event.addDrop(Item.of(drop.item))
			}
		})
	}
})  
commented

I'm pretty sure Item.of(..) tries to parse the second number as the item count which is an integer, so you may want to do Item.of(first).chance(second) instead ^^

commented

Alternatively, event.addDrop(item, chance) also exists and does exactly what you want without the extra boilerplate