Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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.

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.