|
| 1 | +package com.jsorrell.carpetskyadditions.mixin; |
| 2 | + |
| 3 | +import com.jsorrell.carpetskyadditions.settings.SkyAdditionsSettings; |
| 4 | +import java.util.function.Predicate; |
| 5 | +import net.minecraft.tags.BlockTags; |
| 6 | +import net.minecraft.world.damagesource.DamageSource; |
| 7 | +import net.minecraft.world.entity.Entity; |
| 8 | +import net.minecraft.world.entity.EntityType; |
| 9 | +import net.minecraft.world.entity.item.FallingBlockEntity; |
| 10 | +import net.minecraft.world.entity.item.ItemEntity; |
| 11 | +import net.minecraft.world.item.ItemStack; |
| 12 | +import net.minecraft.world.item.Items; |
| 13 | +import net.minecraft.world.level.Level; |
| 14 | +import net.minecraft.world.level.block.state.BlockState; |
| 15 | +import org.spongepowered.asm.mixin.Mixin; |
| 16 | +import org.spongepowered.asm.mixin.Shadow; |
| 17 | +import org.spongepowered.asm.mixin.injection.At; |
| 18 | +import org.spongepowered.asm.mixin.injection.Inject; |
| 19 | +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; |
| 20 | + |
| 21 | +@Mixin(FallingBlockEntity.class) |
| 22 | +public abstract class FallingBlockEntityMixin extends Entity { |
| 23 | + |
| 24 | + @Shadow |
| 25 | + private BlockState blockState; |
| 26 | + |
| 27 | + public FallingBlockEntityMixin(EntityType<?> entityType, Level level) { |
| 28 | + super(entityType, level); |
| 29 | + } |
| 30 | + |
| 31 | + private void compactEntityToDiamonds(Entity entity) { |
| 32 | + if (entity instanceof ItemEntity e |
| 33 | + && e.getItem().is(Items.COAL_BLOCK) |
| 34 | + && 64 <= e.getItem().getCount()) { |
| 35 | + int numCoalBlocks = e.getItem().getCount(); |
| 36 | + int numDiamonds = numCoalBlocks / 64; |
| 37 | + int remainingCoalBlocks = numCoalBlocks % 64; |
| 38 | + ItemEntity diamondEntity = |
| 39 | + new ItemEntity(e.level(), e.getX(), e.getY(), e.getZ(), new ItemStack(Items.DIAMOND, numDiamonds)); |
| 40 | + diamondEntity.setDefaultPickUpDelay(); |
| 41 | + e.level().addFreshEntity(diamondEntity); |
| 42 | + |
| 43 | + e.getItem().setCount(remainingCoalBlocks); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + @Inject( |
| 48 | + method = "causeFallDamage", |
| 49 | + at = @At(value = "INVOKE", target = "Ljava/util/List;forEach(Ljava/util/function/Consumer;)V")) |
| 50 | + private void compactCoalToDiamonds( |
| 51 | + float fallDistance, float multiplier, DamageSource source, CallbackInfoReturnable<Boolean> cir) { |
| 52 | + if (SkyAdditionsSettings.renewableDiamonds) { |
| 53 | + if (blockState.is(BlockTags.ANVIL)) { |
| 54 | + Predicate<Entity> coalBlockPredicate = entity -> entity instanceof ItemEntity itemEntity |
| 55 | + && itemEntity.getItem().is(Items.COAL_BLOCK); |
| 56 | + this.level() |
| 57 | + .getEntities(this, this.getBoundingBox(), coalBlockPredicate) |
| 58 | + .forEach(this::compactEntityToDiamonds); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | +} |
0 commit comments