From adbc374c111bca9cbcc0afc473acb082acfaae39 Mon Sep 17 00:00:00 2001 From: mezz Date: Thu, 18 Jan 2018 13:23:30 -0800 Subject: [PATCH] Make FluidUtil sounds server-side, add simulate option for tryFill/EmptyContainerAndStow (#4623) --- .../net/minecraftforge/fluids/FluidUtil.java | 81 +++++++++++++++---- 1 file changed, 65 insertions(+), 16 deletions(-) diff --git a/src/main/java/net/minecraftforge/fluids/FluidUtil.java b/src/main/java/net/minecraftforge/fluids/FluidUtil.java index 5d4368d9d..f98d0c7b9 100644 --- a/src/main/java/net/minecraftforge/fluids/FluidUtil.java +++ b/src/main/java/net/minecraftforge/fluids/FluidUtil.java @@ -101,10 +101,10 @@ public class FluidUtil IItemHandler playerInventory = player.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null); if (playerInventory != null) { - FluidActionResult fluidActionResult = tryFillContainerAndStow(heldItem, handler, playerInventory, Integer.MAX_VALUE, player); + FluidActionResult fluidActionResult = tryFillContainerAndStow(heldItem, handler, playerInventory, Integer.MAX_VALUE, player, true); if (!fluidActionResult.isSuccess()) { - fluidActionResult = tryEmptyContainerAndStow(heldItem, handler, playerInventory, Integer.MAX_VALUE, player); + fluidActionResult = tryEmptyContainerAndStow(heldItem, handler, playerInventory, Integer.MAX_VALUE, player, true); } if (fluidActionResult.isSuccess()) @@ -122,7 +122,7 @@ public class FluidUtil * * @param container The container to be filled. Will not be modified. * Separate handling must be done to reduce the stack size, stow containers, etc, on success. - * See {@link #tryFillContainerAndStow(ItemStack, IFluidHandler, IItemHandler, int, EntityPlayer)}. + * See {@link #tryFillContainerAndStow(ItemStack, IFluidHandler, IItemHandler, int, EntityPlayer, boolean)}. * @param fluidSource The fluid handler to be drained. * @param maxAmount The largest amount of fluid that should be transferred. * @param player The player to make the filling noise. Pass null for no noise. @@ -145,7 +145,7 @@ public class FluidUtil if (player != null) { SoundEvent soundevent = simulatedTransfer.getFluid().getFillSound(simulatedTransfer); - player.playSound(soundevent, 1f, 1f); + player.world.playSound(null, player.posX, player.posY + 0.5, player.posZ, soundevent, SoundCategory.BLOCKS, 1.0F, 1.0F); } } else @@ -165,7 +165,7 @@ public class FluidUtil * * @param container The filled container. Will not be modified. * Separate handling must be done to reduce the stack size, stow containers, etc, on success. - * See {@link #tryEmptyContainerAndStow(ItemStack, IFluidHandler, IItemHandler, int, EntityPlayer)}. + * See {@link #tryEmptyContainerAndStow(ItemStack, IFluidHandler, IItemHandler, int, EntityPlayer, boolean)}. * @param fluidDestination The fluid handler to be filled by the container. * @param maxAmount The largest amount of fluid that should be transferred. * @param player Player for making the bucket drained sound. Pass null for no noise. @@ -188,7 +188,7 @@ public class FluidUtil if (player != null) { SoundEvent soundevent = transfer.getFluid().getEmptySound(transfer); - player.playSound(soundevent, 1f, 1f); + player.world.playSound(null, player.posX, player.posY + 0.5, player.posZ, soundevent, SoundCategory.BLOCKS, 1.0F, 1.0F); } ItemStack resultContainer = containerFluidHandler.getContainer(); return new FluidActionResult(resultContainer); @@ -223,9 +223,34 @@ public class FluidUtil * @param player The player that gets the items the inventory can't take. * Can be null, only used if the inventory cannot take the filled stack. * @return a {@link FluidActionResult} holding the result and the resulting container. The resulting container is empty on failure. + * @deprecated use {@link #tryFillContainerAndStow(ItemStack, IFluidHandler, IItemHandler, int, EntityPlayer, boolean)} */ + @Deprecated // TODO remove in 1.13 @Nonnull public static FluidActionResult tryFillContainerAndStow(@Nonnull ItemStack container, IFluidHandler fluidSource, IItemHandler inventory, int maxAmount, @Nullable EntityPlayer player) + { + return tryFillContainerAndStow(container, fluidSource, inventory, maxAmount, player, true); + } + + /** + * Takes an Fluid Container Item and tries to fill it from the given tank. + * If the player is in creative mode, the container will not be modified on success, and no additional items created. + * If the input itemstack has a stacksize > 1 it will stow the filled container in the given inventory. + * If the inventory does not accept it, it will be given to the player or dropped at the players feet. + * If player is null in this case, the action will be aborted. + * + * @param container The Fluid Container ItemStack to fill. + * Will not be modified directly, if modifications are necessary a modified copy is returned in the result. + * @param fluidSource The fluid source to fill from + * @param inventory An inventory where any additionally created item (filled container if multiple empty are present) are put + * @param maxAmount Maximum amount of fluid to take from the tank. + * @param player The player that gets the items the inventory can't take. + * Can be null, only used if the inventory cannot take the filled stack. + * @param doFill true if the container should actually be filled, false if it should be simulated. + * @return a {@link FluidActionResult} holding the result and the resulting container. The resulting container is empty on failure. + */ + @Nonnull + public static FluidActionResult tryFillContainerAndStow(@Nonnull ItemStack container, IFluidHandler fluidSource, IItemHandler inventory, int maxAmount, @Nullable EntityPlayer player, boolean doFill) { if (container.isEmpty()) { @@ -234,7 +259,7 @@ public class FluidUtil if (player != null && player.capabilities.isCreativeMode) { - FluidActionResult filledReal = tryFillContainer(container, fluidSource, maxAmount, player, true); + FluidActionResult filledReal = tryFillContainer(container, fluidSource, maxAmount, player, doFill); if (filledReal.isSuccess()) { return new FluidActionResult(container); // creative mode: item does not change @@ -242,7 +267,7 @@ public class FluidUtil } else if (container.getCount() == 1) // don't need to stow anything, just fill the container stack { - FluidActionResult filledReal = tryFillContainer(container, fluidSource, maxAmount, player, true); + FluidActionResult filledReal = tryFillContainer(container, fluidSource, maxAmount, player, doFill); if (filledReal.isSuccess()) { return filledReal; @@ -257,11 +282,11 @@ public class FluidUtil ItemStack remainder = ItemHandlerHelper.insertItemStacked(inventory, filledSimulated.getResult(), true); if (remainder.isEmpty() || player != null) { - FluidActionResult filledReal = tryFillContainer(container, fluidSource, maxAmount, player, true); - remainder = ItemHandlerHelper.insertItemStacked(inventory, filledReal.getResult(), false); + FluidActionResult filledReal = tryFillContainer(container, fluidSource, maxAmount, player, doFill); + remainder = ItemHandlerHelper.insertItemStacked(inventory, filledReal.getResult(), !doFill); // give it to the player or drop it at their feet - if (!remainder.isEmpty() && player != null) + if (!remainder.isEmpty() && player != null && doFill) { ItemHandlerHelper.giveItemToPlayer(player, remainder); } @@ -290,9 +315,33 @@ public class FluidUtil * @param maxAmount Maximum amount of fluid to take from the tank. * @param player The player that gets the items the inventory can't take. Can be null, only used if the inventory cannot take the filled stack. * @return a {@link FluidActionResult} holding the result and the resulting container. The resulting container is empty on failure. + * @deprecated use {@link #tryEmptyContainerAndStow(ItemStack, IFluidHandler, IItemHandler, int, EntityPlayer, boolean)} */ + @Deprecated // TODO: remove in 1.13 @Nonnull public static FluidActionResult tryEmptyContainerAndStow(@Nonnull ItemStack container, IFluidHandler fluidDestination, IItemHandler inventory, int maxAmount, @Nullable EntityPlayer player) + { + return tryEmptyContainerAndStow(container, fluidDestination, inventory, maxAmount, player, true); + } + + /** + * Takes an Fluid Container Item, tries to empty it into the fluid handler, and stows it in the given inventory. + * If the player is in creative mode, the container will not be modified on success, and no additional items created. + * If the input itemstack has a stacksize > 1 it will stow the emptied container in the given inventory. + * If the inventory does not accept the emptied container, it will be given to the player or dropped at the players feet. + * If player is null in this case, the action will be aborted. + * + * @param container The filled Fluid Container Itemstack to empty. + * Will not be modified directly, if modifications are necessary a modified copy is returned in the result. + * @param fluidDestination The fluid destination to fill from the fluid container. + * @param inventory An inventory where any additionally created item (filled container if multiple empty are present) are put + * @param maxAmount Maximum amount of fluid to take from the tank. + * @param player The player that gets the items the inventory can't take. Can be null, only used if the inventory cannot take the filled stack. + * @param doDrain true if the container should actually be drained, false if it should be simulated. + * @return a {@link FluidActionResult} holding the result and the resulting container. The resulting container is empty on failure. + */ + @Nonnull + public static FluidActionResult tryEmptyContainerAndStow(@Nonnull ItemStack container, IFluidHandler fluidDestination, IItemHandler inventory, int maxAmount, @Nullable EntityPlayer player, boolean doDrain) { if (container.isEmpty()) { @@ -301,7 +350,7 @@ public class FluidUtil if (player != null && player.capabilities.isCreativeMode) { - FluidActionResult emptiedReal = tryEmptyContainer(container, fluidDestination, maxAmount, player, true); + FluidActionResult emptiedReal = tryEmptyContainer(container, fluidDestination, maxAmount, player, doDrain); if (emptiedReal.isSuccess()) { return new FluidActionResult(container); // creative mode: item does not change @@ -309,7 +358,7 @@ public class FluidUtil } else if (container.getCount() == 1) // don't need to stow anything, just fill and edit the container stack { - FluidActionResult emptiedReal = tryEmptyContainer(container, fluidDestination, maxAmount, player, true); + FluidActionResult emptiedReal = tryEmptyContainer(container, fluidDestination, maxAmount, player, doDrain); if (emptiedReal.isSuccess()) { return emptiedReal; @@ -324,11 +373,11 @@ public class FluidUtil ItemStack remainder = ItemHandlerHelper.insertItemStacked(inventory, emptiedSimulated.getResult(), true); if (remainder.isEmpty() || player != null) { - FluidActionResult emptiedReal = tryEmptyContainer(container, fluidDestination, maxAmount, player, true); - remainder = ItemHandlerHelper.insertItemStacked(inventory, emptiedReal.getResult(), false); + FluidActionResult emptiedReal = tryEmptyContainer(container, fluidDestination, maxAmount, player, doDrain); + remainder = ItemHandlerHelper.insertItemStacked(inventory, emptiedReal.getResult(), !doDrain); // give it to the player or drop it at their feet - if (!remainder.isEmpty() && player != null) + if (!remainder.isEmpty() && player != null && doDrain) { ItemHandlerHelper.giveItemToPlayer(player, remainder); }