Jump to content

[1.15.2] Issues with getting an Entity's loot table


Recommended Posts

I'm attempting to make two ore blocks which, when broken, will spawn a slime and a magma cube respectively. When the world's difficulty is set to Peaceful, I'd like to just spawn the drops, which will be affected by the player's tool's fortune level the same way a normal kill would be affected by looting. I'd like this to be compatible with any datapack or mod that modifies the loot tables for slimes and magma cubes. So far, I have the following code:

    public void spawnAdditionalDrops(BlockState state, World world, BlockPos pos, ItemStack stack) {
        super.spawnAdditionalDrops(state, world, pos, stack);
        if (!world.isRemote && world.getGameRules().getBoolean(GameRules.DO_TILE_DROPS) && EnchantmentHelper.getEnchantmentLevel(Enchantments.SILK_TOUCH, stack) == 0) {

            SlimeEntity entity = slimeType.create(world);
            if(entity != null) {
                entity.setSlimeSize(1, true);

                if (world.getDifficulty() != Difficulty.PEACEFUL) {
                    //spawn the entity
                            pos.getX() + 0.5D, pos.getY(), pos.getZ() + 0.5D,
                            MathHelper.nextFloat(world.getRandom(), -180f, 179.9f), 0.0F
                } else {
                    //get entity drops and spawn them in instead
                    if(world.getServer() != null) {
                        int fortuneLevel = EnchantmentHelper.getEnchantmentLevel(Enchantments.FORTUNE, stack);
                        ItemStack weapon = new ItemStack(Items.IRON_SWORD);
                        EnchantmentHelper.setEnchantments(ImmutableMap.of(Enchantments.LOOTING, fortuneLevel), weapon);
                        FakePlayer fakePlayer = FakePlayerFactory.getMinecraft((ServerWorld) world);
                        fakePlayer.setItemStackToSlot(EquipmentSlotType.MAINHAND, weapon);

                        LootTable loot = world.getServer().getLootTableManager().getLootTableFromLocation(entity.getLootTableResourceLocation());
                        LootContext context = new LootContext.Builder((ServerWorld) world)
                                .withParameter(LootParameters.THIS_ENTITY, entity)
                                .withParameter(LootParameters.POSITION, pos)
                                .withParameter(LootParameters.DAMAGE_SOURCE, DamageSource.causePlayerDamage(fakePlayer))
                        List<ItemStack> drops = loot.generate(context);

                        for (ItemStack drop : drops) {
                            spawnAsEntity(world, pos, drop);

The entity spawning works perfectly; there's no issue there. The issues arise when working with the loot tables. First, I can't seem to get the fortune/looting to work at all - I suspect it has to do with me getting the fake player wrong. There may be a much better way of achieving the same effect without using a fake player at all - if so I'd much prefer that. Second, the magma cube block doesn't ever drop magma cream, despite it supposedly being a 1/4 chance. Anyone have any ideas?


Side note - I'm using an access transformer to make setSlimeSize public (it's normally protected).

I'll put something here when I have something of value I need to put at the end of every post. For now it's this mostly pointless text.

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in

Sign In Now

  • Create New...

Important Information

By using this site, you agree to our Terms of Use.