Skip to content

Commit

Permalink
fix(avaritia): optimize InfinityBucketItem interactions with fluid ha…
Browse files Browse the repository at this point in the history
…ndling

Refactor InfinityBucketItem to improve fluid insertion, draining, and associated sound
effects. Ensure consistent behavior when interacting with fluid blocks and modify the
inventoryTick method to handle empty fluid lists properly. Update playFluidSound method
to differentiate between filling and emptying actions based on context.
  • Loading branch information
cnlimiter committed Aug 1, 2024
1 parent b8dc07d commit d94c7ea
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public int fill(FluidStack resource, IFluidHandler.FluidAction action) {
FluidStack fluidStack = this.getFluid();
if (!fluidStack.isEmpty() && fluidStack.isFluidEqual(resource)) {
if (action.execute()) {
InfinityBucketItem.drainFluid(this.container, (long)resource.getAmount());
return InfinityBucketItem.drainFluid(this.container, resource.getAmount());
}
return fluidStack;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,22 @@ public void appendHoverText(@NotNull ItemStack pStack, @Nullable Level pLevel, @
itemStack.getOrCreateTag().put(FLUIDS_NBT, items);
return InteractionResultHolder.sidedSuccess(itemStack, pLevel.isClientSide);
}

BlockHitResult result = getPlayerPOVHitResult(pLevel, pPlayer, ClipContext.Fluid.SOURCE_ONLY);
if (result.getType() != HitResult.Type.BLOCK) return InteractionResultHolder.pass(itemStack);
BlockState target = pLevel.getBlockState(result.getBlockPos());
if (pLevel.mayInteract(pPlayer, result.getBlockPos()) && target.getBlock() instanceof BucketPickup drainable) {
ItemStack r = drainable.pickupBlock(pLevel, result.getBlockPos(), target);

BlockPos resultBlockPos = result.getBlockPos();
BlockState target = pLevel.getBlockState(resultBlockPos);

if (pLevel.mayInteract(pPlayer, resultBlockPos) && target.getBlock() instanceof BucketPickup drainable) {
ItemStack r = drainable.pickupBlock(pLevel, resultBlockPos, target);
if (!r.isEmpty() && r.getItem() instanceof BucketItem bucketItem) {
Fluid fluid = bucketItem.getFluid();
this.insertFluid(itemStack, fluid, 1000);
playFillingSound(pPlayer, pLevel, result.getBlockPos(), fluid);
fillFluid(itemStack, fluid, 1000);
this.playFluidSound(pPlayer, pLevel, resultBlockPos, fluid, false);
}
} else {
BlockPos newPos = result.getBlockPos().offset(result.getDirection().getNormal());
BlockPos newPos = resultBlockPos.offset(result.getDirection().getNormal());
if (pLevel.mayInteract(pPlayer, newPos)) {
Fluid fluid = this.getFirstAndDecrease(itemStack, 1000);
if (fluid != Fluids.EMPTY) {
Expand All @@ -101,7 +105,7 @@ public void appendHoverText(@NotNull ItemStack pStack, @Nullable Level pLevel, @
if (!pLevel.isClientSide && here.canBeReplaced(fluid))
pLevel.destroyBlock(newPos, true);
if (pLevel.setBlock(newPos, fluid.defaultFluidState().createLegacyBlock(), 11) && !here.getFluidState().isSource())
this.playEmptyingSound(pPlayer, pLevel, newPos, fluid);
this.playFluidSound(pPlayer, pLevel, newPos, fluid, true);
}
}
}
Expand All @@ -114,7 +118,7 @@ public void appendHoverText(@NotNull ItemStack pStack, @Nullable Level pLevel, @
public void inventoryTick(@NotNull ItemStack pStack, @NotNull Level pLevel, @NotNull Entity pEntity, int pSlotId, boolean pIsSelected) {
super.inventoryTick(pStack, pLevel, pEntity, pSlotId, pIsSelected);
ListTag items = pStack.getOrCreateTag().getList(FLUIDS_NBT, Tag.TAG_COMPOUND);
if (pEntity instanceof Player player && player.getInventory().getSelected() == pStack && items.size() > 0) {
if (pEntity instanceof Player player && player.getInventory().getSelected() == pStack && !items.isEmpty()) {
CompoundTag compound = items.getCompound(0);
ResourceLocation id = new ResourceLocation(compound.getString(FLUID_ID_KEY));
long amount = compound.getLong(FLUID_AMOUNT_KEY);
Expand Down Expand Up @@ -143,10 +147,11 @@ public Fluid getFirstAndDecrease(ItemStack stack, long minimumAmount) {
return Fluids.EMPTY;
}

public void insertFluid(ItemStack stack, Fluid fluid, long amount) {
public static void fillFluid(ItemStack stack, Fluid fluid, long amount) {
ResourceLocation fluidId = ForgeRegistries.FLUIDS.getKey(fluid);
ListTag items = stack.getOrCreateTag().getList(FLUIDS_NBT, Tag.TAG_COMPOUND);
for (int i = 0; i < items.size(); i++) {

for(int i = 0; i < items.size(); ++i) {
CompoundTag compound = items.getCompound(i);
ResourceLocation id = new ResourceLocation(compound.getString(FLUID_ID_KEY));
if (fluidId.equals(id)) {
Expand All @@ -167,10 +172,12 @@ public void insertFluid(ItemStack stack, Fluid fluid, long amount) {
stack.getOrCreateTag().put(FLUIDS_NBT, items);
}

protected void playEmptyingSound(Player player, LevelAccessor world, BlockPos pos, Fluid fluid) {
SoundEvent soundEvent = fluid.is(FluidTags.LAVA) ? SoundEvents.BUCKET_EMPTY_LAVA : SoundEvents.BUCKET_EMPTY;
protected void playFluidSound(Player player, LevelAccessor world, BlockPos pos, Fluid fluid, boolean place) {
SoundEvent soundEvent = fluid.is(FluidTags.LAVA)
? (place ? SoundEvents.BUCKET_EMPTY_LAVA : SoundEvents.BUCKET_FILL_LAVA)
: (place ? SoundEvents.BUCKET_EMPTY : SoundEvents.BUCKET_FILL);
world.playSound(player, pos, soundEvent, SoundSource.BLOCKS, 1.0F, 1.0F);
world.gameEvent(player, GameEvent.FLUID_PLACE, pos);
world.gameEvent(player, place ? GameEvent.FLUID_PLACE : GameEvent.FLUID_PICKUP, pos);
}

protected void playFillingSound(Player player, LevelAccessor world, BlockPos pos, Fluid fluid) {
Expand All @@ -180,34 +187,12 @@ protected void playFillingSound(Player player, LevelAccessor world, BlockPos pos
}


@Override
public ICapabilityProvider initCapabilities(ItemStack stack, @Nullable CompoundTag nbt) {
return new InfinityBucketWrapper(stack);
}

public static void fillFluid(ItemStack stack, Fluid fluid, long amount) {
ResourceLocation fluidId = ForgeRegistries.FLUIDS.getKey(fluid);
ListTag items = stack.getOrCreateTag().getList(FLUIDS_NBT, CompoundTag.TAG_COMPOUND);

for(int i = 0; i < items.size(); ++i) {
CompoundTag compound = items.getCompound(i);
ResourceLocation id = new ResourceLocation(compound.getString(FLUID_ID_KEY));
if (fluidId.equals(id)) {
long a = compound.getLong(FLUID_AMOUNT_KEY);
if (Integer.MAX_VALUE - a >= amount) {
a += amount;
compound.putLong(FLUID_AMOUNT_KEY, a);
} else {
compound.putLong(FLUID_AMOUNT_KEY, Integer.MAX_VALUE);
}
return;
}
}
CompoundTag compound = new CompoundTag();
compound.putString(FLUID_ID_KEY, fluidId.toString());
compound.putLong(FLUID_AMOUNT_KEY, amount);
items.add(compound);
stack.getOrCreateTag().put(FLUIDS_NBT, items);
}

public static FluidStack drainFluid(ItemStack stack, long minimumAmount) {
ListTag items = stack.getOrCreateTag().getList(FLUIDS_NBT, CompoundTag.TAG_COMPOUND);
Expand Down

0 comments on commit d94c7ea

Please sign in to comment.