|
| 1 | +package projectbackroom.jonathanx.mixin; |
| 2 | + |
| 3 | +import net.minecraft.block.BlockState; |
| 4 | +import net.minecraft.component.type.PotionContentsComponent; |
| 5 | +import net.minecraft.entity.EquipmentSlot; |
| 6 | +import net.minecraft.entity.player.PlayerEntity; |
| 7 | +import net.minecraft.fluid.Fluid; |
| 8 | +import net.minecraft.fluid.Fluids; |
| 9 | +import net.minecraft.item.*; |
| 10 | +import net.minecraft.potion.Potions; |
| 11 | +import net.minecraft.sound.SoundCategory; |
| 12 | +import net.minecraft.sound.SoundEvents; |
| 13 | +import net.minecraft.text.Text; |
| 14 | +import net.minecraft.util.ActionResult; |
| 15 | +import net.minecraft.util.Hand; |
| 16 | +import net.minecraft.util.hit.BlockHitResult; |
| 17 | +import net.minecraft.world.RaycastContext; |
| 18 | +import net.minecraft.world.World; |
| 19 | +import org.spongepowered.asm.mixin.Mixin; |
| 20 | +import org.spongepowered.asm.mixin.Shadow; |
| 21 | +import org.spongepowered.asm.mixin.Unique; |
| 22 | +import org.spongepowered.asm.mixin.injection.At; |
| 23 | +import org.spongepowered.asm.mixin.injection.Inject; |
| 24 | +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; |
| 25 | +import projectbackroom.jonathanx.blocks.entity.PipeBlockEntity; |
| 26 | +import projectbackroom.jonathanx.blocks.pipes.PipeBlock; |
| 27 | +import projectbackroom.jonathanx.fluid.BackroomsFlowableFluid; |
| 28 | + |
| 29 | +@Mixin(GlassBottleItem.class) |
| 30 | +public abstract class GlassBottleItemMixin extends Item { |
| 31 | + |
| 32 | + @Shadow public abstract ActionResult use(World world, PlayerEntity user, Hand hand); |
| 33 | + |
| 34 | + public GlassBottleItemMixin(Settings settings) { |
| 35 | + super(settings); |
| 36 | + } |
| 37 | + |
| 38 | + @Unique |
| 39 | + private void initBottleCollect(World world, PlayerEntity user, Hand hand){ |
| 40 | + world.playSound(null, user.getX(), user.getY(), user.getZ(), SoundEvents.ITEM_BOTTLE_FILL_DRAGONBREATH, SoundCategory.NEUTRAL, 1.0f, 1.0f); |
| 41 | + ItemStack itemStack = user.getStackInHand(hand); |
| 42 | + itemStack.decrement(1); |
| 43 | + } |
| 44 | + |
| 45 | + @Inject(method = "use", at = @At("RETURN")) |
| 46 | + private void use(World world, PlayerEntity user, Hand hand, CallbackInfoReturnable<ActionResult> cir){ |
| 47 | + BlockHitResult result = GlassBottleItemMixin.raycast(world, user, RaycastContext.FluidHandling.NONE); |
| 48 | + BlockState blockState = world.getBlockState(result.getBlockPos()); |
| 49 | + |
| 50 | + Text cannotCollect = Text.translatable("block.project_backroom.pipe.cannot_collect"); |
| 51 | + |
| 52 | + if (blockState.getBlock() instanceof PipeBlock && blockState.get(PipeBlock.LEAKING)){ |
| 53 | + PipeBlockEntity blockEntity = (PipeBlockEntity) world.getBlockEntity(result.getBlockPos()); |
| 54 | + assert blockEntity != null; |
| 55 | + Fluid fluid = blockEntity.getFluidContainer(); |
| 56 | + if (fluid instanceof BackroomsFlowableFluid backroomsFlowableFluid){ |
| 57 | + if (backroomsFlowableFluid.getBottleItem() != null){ |
| 58 | + initBottleCollect(world, user, hand); |
| 59 | + if (backroomsFlowableFluid.getBottleItem() != null){ |
| 60 | + user.equipStack(EquipmentSlot.MAINHAND, backroomsFlowableFluid.getBottleItem()); |
| 61 | + } else { |
| 62 | + user.sendMessage(cannotCollect, true); |
| 63 | + } |
| 64 | + } |
| 65 | + } else { |
| 66 | + if (fluid == Fluids.WATER){ |
| 67 | + initBottleCollect(world, user, hand); |
| 68 | + user.equipStack(EquipmentSlot.MAINHAND, PotionContentsComponent.createStack(Items.POTION, Potions.WATER)); |
| 69 | + } else { |
| 70 | + user.sendMessage(cannotCollect, true); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | +} |
0 commit comments