Jump to content

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


HenryRichard

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:

@Override
    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
                    entity.setLocationAndAngles(
                            pos.getX() + 0.5D, pos.getY(), pos.getZ() + 0.5D,
                            MathHelper.nextFloat(world.getRandom(), -180f, 179.9f), 0.0F
                    );
                    world.addEntity(entity);
                    entity.spawnExplosionParticle();
                } 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)
                                .withRandom(world.getRandom())
                                .withParameter(LootParameters.THIS_ENTITY, entity)
                                .withParameter(LootParameters.POSITION, pos)
                                .withParameter(LootParameters.DAMAGE_SOURCE, DamageSource.causePlayerDamage(fakePlayer))
                                .build(LootParameterSets.ENTITY);
                        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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

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