From 69a46a8f4a832248227c67fb41599046da149a9b Mon Sep 17 00:00:00 2001 From: Jack Sorrell Date: Mon, 19 Jun 2023 20:44:03 -0400 Subject: [PATCH] Fix renewableDiamonds --- .../mixin/FallingBlockEntityMixin.java | 62 +++++++++++++++++++ .../mixin/ItemEntityMixin.java | 53 ---------------- .../resources/carpetskyadditions.mixins.json | 2 +- 3 files changed, 63 insertions(+), 54 deletions(-) create mode 100644 src/main/java/com/jsorrell/carpetskyadditions/mixin/FallingBlockEntityMixin.java delete mode 100644 src/main/java/com/jsorrell/carpetskyadditions/mixin/ItemEntityMixin.java diff --git a/src/main/java/com/jsorrell/carpetskyadditions/mixin/FallingBlockEntityMixin.java b/src/main/java/com/jsorrell/carpetskyadditions/mixin/FallingBlockEntityMixin.java new file mode 100644 index 00000000..c6962981 --- /dev/null +++ b/src/main/java/com/jsorrell/carpetskyadditions/mixin/FallingBlockEntityMixin.java @@ -0,0 +1,62 @@ +package com.jsorrell.carpetskyadditions.mixin; + +import com.jsorrell.carpetskyadditions.settings.SkyAdditionsSettings; +import java.util.function.Predicate; +import net.minecraft.tags.BlockTags; +import net.minecraft.world.damagesource.DamageSource; +import net.minecraft.world.entity.Entity; +import net.minecraft.world.entity.EntityType; +import net.minecraft.world.entity.item.FallingBlockEntity; +import net.minecraft.world.entity.item.ItemEntity; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.Items; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.state.BlockState; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +@Mixin(FallingBlockEntity.class) +public abstract class FallingBlockEntityMixin extends Entity { + + @Shadow + private BlockState blockState; + + public FallingBlockEntityMixin(EntityType entityType, Level level) { + super(entityType, level); + } + + private void compactEntityToDiamonds(Entity entity) { + if (entity instanceof ItemEntity e + && e.getItem().is(Items.COAL_BLOCK) + && 64 <= e.getItem().getCount()) { + int numCoalBlocks = e.getItem().getCount(); + int numDiamonds = numCoalBlocks / 64; + int remainingCoalBlocks = numCoalBlocks % 64; + ItemEntity diamondEntity = + new ItemEntity(e.level(), e.getX(), e.getY(), e.getZ(), new ItemStack(Items.DIAMOND, numDiamonds)); + diamondEntity.setDefaultPickUpDelay(); + e.level().addFreshEntity(diamondEntity); + + e.getItem().setCount(remainingCoalBlocks); + } + } + + @Inject( + method = "causeFallDamage", + at = @At(value = "INVOKE", target = "Ljava/util/List;forEach(Ljava/util/function/Consumer;)V")) + private void compactCoalToDiamonds( + float fallDistance, float multiplier, DamageSource source, CallbackInfoReturnable cir) { + if (SkyAdditionsSettings.renewableDiamonds) { + if (blockState.is(BlockTags.ANVIL)) { + Predicate coalBlockPredicate = entity -> entity instanceof ItemEntity itemEntity + && itemEntity.getItem().is(Items.COAL_BLOCK); + this.level() + .getEntities(this, this.getBoundingBox(), coalBlockPredicate) + .forEach(this::compactEntityToDiamonds); + } + } + } +} diff --git a/src/main/java/com/jsorrell/carpetskyadditions/mixin/ItemEntityMixin.java b/src/main/java/com/jsorrell/carpetskyadditions/mixin/ItemEntityMixin.java deleted file mode 100644 index b1d66a5a..00000000 --- a/src/main/java/com/jsorrell/carpetskyadditions/mixin/ItemEntityMixin.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.jsorrell.carpetskyadditions.mixin; - -import com.jsorrell.carpetskyadditions.settings.SkyAdditionsSettings; -import net.minecraft.world.damagesource.DamageSource; -import net.minecraft.world.damagesource.DamageTypes; -import net.minecraft.world.entity.Entity; -import net.minecraft.world.entity.EntityType; -import net.minecraft.world.entity.item.ItemEntity; -import net.minecraft.world.item.ItemStack; -import net.minecraft.world.item.Items; -import net.minecraft.world.level.Level; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; -import org.spongepowered.asm.mixin.injection.callback.LocalCapture; - -@Mixin(ItemEntity.class) -public abstract class ItemEntityMixin extends Entity { - - @Shadow - public abstract ItemStack getItem(); - - public ItemEntityMixin(EntityType type, Level level) { - super(type, level); - } - - private void compactToDiamonds() { - int numCoalBlocks = getItem().getCount(); - int numDiamonds = numCoalBlocks / 64; - int remainingCoalBlocks = numCoalBlocks % 64; - ItemEntity diamondEntity = - new ItemEntity(level(), getX(), getY(), getZ(), new ItemStack(Items.DIAMOND, numDiamonds)); - diamondEntity.setDefaultPickUpDelay(); - level().addFreshEntity(diamondEntity); - - getItem().setCount(remainingCoalBlocks); - } - - @Inject(method = "hurt", locals = LocalCapture.CAPTURE_FAILSOFT, cancellable = true, at = @At(value = "HEAD")) - private void compactCoalToDiamonds(DamageSource source, float amount, CallbackInfoReturnable cir) { - if (SkyAdditionsSettings.renewableDiamonds) { - if (source.is(DamageTypes.FALLING_ANVIL)) { - ItemStack stack = getItem(); - if (Items.COAL_BLOCK.equals(stack.getItem()) && 64 <= stack.getCount()) { - compactToDiamonds(); - cir.setReturnValue(true); - } - } - } - } -} diff --git a/src/main/resources/carpetskyadditions.mixins.json b/src/main/resources/carpetskyadditions.mixins.json index 0bcf36cd..4af648f4 100644 --- a/src/main/resources/carpetskyadditions.mixins.json +++ b/src/main/resources/carpetskyadditions.mixins.json @@ -16,8 +16,8 @@ "EnderDragonMixin", "EnderMan_EndermanLeaveBlockGoalMixin", "EnderMan_EndermanTakeBlockGoalMixin", + "FallingBlockEntityMixin", "FoxMixin", - "ItemEntityMixin", "JigsawStructureAccessor", "LavaFluidMixin", "LightningBoltMixin",