|
| 1 | +package zone.moddev.mc.lib.common.fluid; |
| 2 | + |
| 3 | +import java.util.function.Supplier; |
| 4 | + |
| 5 | +import org.slf4j.Logger; |
| 6 | + |
| 7 | +import com.mojang.logging.LogUtils; |
| 8 | + |
| 9 | +import net.minecraft.core.component.DataComponentType; |
| 10 | +import net.minecraft.core.registries.BuiltInRegistries; |
| 11 | +import net.minecraft.core.registries.Registries; |
| 12 | +import net.minecraft.resources.ResourceLocation; |
| 13 | +import net.minecraft.sounds.SoundEvents; |
| 14 | +import net.minecraft.sounds.SoundSource; |
| 15 | +import net.minecraft.stats.Stats; |
| 16 | +import net.minecraft.tags.TagKey; |
| 17 | +import net.minecraft.world.InteractionResult; |
| 18 | +import net.minecraft.world.item.ItemUtils; |
| 19 | +import net.minecraft.world.item.Items; |
| 20 | +import net.minecraft.world.level.material.Fluid; |
| 21 | +import net.minecraft.world.phys.BlockHitResult; |
| 22 | +import net.minecraft.world.phys.HitResult; |
| 23 | + |
| 24 | +import net.neoforged.bus.api.EventPriority; |
| 25 | +import net.neoforged.bus.api.IEventBus; |
| 26 | +import net.neoforged.neoforge.capabilities.Capabilities; |
| 27 | +import net.neoforged.neoforge.capabilities.RegisterCapabilitiesEvent; |
| 28 | +import net.neoforged.neoforge.common.NeoForge; |
| 29 | +import net.neoforged.neoforge.event.entity.player.PlayerInteractEvent; |
| 30 | +import net.neoforged.neoforge.fluids.SimpleFluidContent; |
| 31 | +import net.neoforged.neoforge.fluids.capability.templates.FluidHandlerItemStack; |
| 32 | +import net.neoforged.neoforge.registries.DeferredHolder; |
| 33 | +import net.neoforged.neoforge.registries.DeferredRegister; |
| 34 | + |
| 35 | +import zone.moddev.mc.lib.MMDLib; |
| 36 | + |
| 37 | +/** |
| 38 | + * Lets vanilla glass bottles hold any fluid in {@link #BOTTLEABLE}, without a mixin: |
| 39 | + * |
| 40 | + * <ul> |
| 41 | + * <li>Right-clicking a source block of such a fluid with a glass bottle gives a {@link |
| 42 | + * FluidBottleItem} and leaves the source in place, as water bottles do. |
| 43 | + * <li>Glass bottles and filled bottles get a fluid capability, so tanks and pipes fill and empty |
| 44 | + * them. If another mod already gives glass bottles one, MMDLib logs that and leaves glass |
| 45 | + * bottles to it. |
| 46 | + * </ul> |
| 47 | + * |
| 48 | + * Vanilla's own fluids (water, lava) are always left to vanilla, tagged or not. |
| 49 | + */ |
| 50 | +public final class FluidBottles { |
| 51 | + private static final Logger LOGGER = LogUtils.getLogger(); |
| 52 | + |
| 53 | + /** Source fluids glass bottles can hold. MMDLib ships it empty: packs and mods fill it. */ |
| 54 | + public static final TagKey<Fluid> BOTTLEABLE = |
| 55 | + TagKey.create(Registries.FLUID, ResourceLocation.fromNamespaceAndPath(MMDLib.MODID, "bottleable")); |
| 56 | + |
| 57 | + /** Millibuckets in a bottle: a third of a bucket, the common convention. */ |
| 58 | + public static final int CAPACITY = 333; |
| 59 | + |
| 60 | + private static final DeferredRegister.DataComponents COMPONENTS = |
| 61 | + DeferredRegister.createDataComponents(Registries.DATA_COMPONENT_TYPE, MMDLib.MODID); |
| 62 | + |
| 63 | + /** The fluid a {@link FluidBottleItem} holds. */ |
| 64 | + public static final Supplier<DataComponentType<SimpleFluidContent>> CONTENT = |
| 65 | + COMPONENTS.registerComponentType( |
| 66 | + "bottle_fluid", |
| 67 | + builder -> |
| 68 | + builder |
| 69 | + .persistent(SimpleFluidContent.CODEC) |
| 70 | + .networkSynchronized(SimpleFluidContent.STREAM_CODEC)); |
| 71 | + |
| 72 | + /** {@code mmdlib:fluid_bottle}. */ |
| 73 | + public static final DeferredHolder<net.minecraft.world.item.Item, FluidBottleItem> BOTTLE = |
| 74 | + MMDLib.ITEMS.register( |
| 75 | + "fluid_bottle", () -> new FluidBottleItem(new net.minecraft.world.item.Item.Properties())); |
| 76 | + |
| 77 | + private FluidBottles() {} |
| 78 | + |
| 79 | + /** Registers the component, the capabilities and the bottle-filling hook. */ |
| 80 | + public static void register(IEventBus modBus) { |
| 81 | + COMPONENTS.register(modBus); |
| 82 | + // Lowest priority, so mods registering at the usual priority are seen first. |
| 83 | + modBus.addListener(EventPriority.LOWEST, FluidBottles::capabilities); |
| 84 | + NeoForge.EVENT_BUS.addListener(FluidBottles::fillFromWorld); |
| 85 | + } |
| 86 | + |
| 87 | + /** Whether glass bottles can hold {@code fluid}: tagged, and not one of vanilla's. */ |
| 88 | + public static boolean bottleable(Fluid fluid) { |
| 89 | + return !BuiltInRegistries.FLUID.getKey(fluid).getNamespace().equals("minecraft") |
| 90 | + && fluid.defaultFluidState().is(BOTTLEABLE); |
| 91 | + } |
| 92 | + |
| 93 | + private static void capabilities(RegisterCapabilitiesEvent event) { |
| 94 | + event.registerItem( |
| 95 | + Capabilities.FluidHandler.ITEM, |
| 96 | + (stack, context) -> |
| 97 | + new FluidHandlerItemStack.SwapEmpty( |
| 98 | + CONTENT, stack, new net.minecraft.world.item.ItemStack(Items.GLASS_BOTTLE), CAPACITY) { |
| 99 | + @Override |
| 100 | + public boolean canFillFluidType(net.neoforged.neoforge.fluids.FluidStack fluid) { |
| 101 | + return bottleable(fluid.getFluid()); |
| 102 | + } |
| 103 | + }, |
| 104 | + BOTTLE.get()); |
| 105 | + if (event.isItemRegistered(Capabilities.FluidHandler.ITEM, Items.GLASS_BOTTLE)) { |
| 106 | + LOGGER.info( |
| 107 | + "Another mod already gives glass bottles a fluid capability; MMDLib leaves filling them" |
| 108 | + + " from tanks to it. Right-clicking bottleable fluids still works."); |
| 109 | + return; |
| 110 | + } |
| 111 | + event.registerItem( |
| 112 | + Capabilities.FluidHandler.ITEM, |
| 113 | + (stack, context) -> new EmptyBottleFluidHandler(stack), |
| 114 | + Items.GLASS_BOTTLE); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * A glass bottle used on a bottleable source fills, both sides, before vanilla's bottle handling |
| 119 | + * (which only knows water). Anything else goes on to vanilla. |
| 120 | + */ |
| 121 | + private static void fillFromWorld(PlayerInteractEvent.RightClickItem event) { |
| 122 | + var held = event.getItemStack(); |
| 123 | + if (!held.is(Items.GLASS_BOTTLE)) return; |
| 124 | + var player = event.getEntity(); |
| 125 | + var level = event.getLevel(); |
| 126 | + var hit = player.pick(player.blockInteractionRange(), 0, true); |
| 127 | + if (hit.getType() != HitResult.Type.BLOCK || !(hit instanceof BlockHitResult block)) return; |
| 128 | + var pos = block.getBlockPos(); |
| 129 | + var fluid = level.getFluidState(pos); |
| 130 | + if (!fluid.isSource() || !bottleable(fluid.getType()) || !level.mayInteract(player, pos)) return; |
| 131 | + level.playSound( |
| 132 | + player, |
| 133 | + player.getX(), |
| 134 | + player.getY(), |
| 135 | + player.getZ(), |
| 136 | + SoundEvents.BOTTLE_FILL, |
| 137 | + SoundSource.NEUTRAL, |
| 138 | + 1.0F, |
| 139 | + 1.0F); |
| 140 | + player.awardStat(Stats.ITEM_USED.get(Items.GLASS_BOTTLE)); |
| 141 | + player.setItemInHand( |
| 142 | + event.getHand(), |
| 143 | + ItemUtils.createFilledResult(held, player, FluidBottleItem.filled(fluid.getType()))); |
| 144 | + event.setCanceled(true); |
| 145 | + event.setCancellationResult(InteractionResult.sidedSuccess(level.isClientSide())); |
| 146 | + } |
| 147 | +} |
0 commit comments