diff --git a/build.gradle b/build.gradle index bc91b99..821a0cb 100644 --- a/build.gradle +++ b/build.gradle @@ -52,7 +52,11 @@ processResources { inputs.property "version", project.version filesMatching("fabric.mod.json") { - expand "version": project.version, "fabric_version": project.fabric_version, "loader_version": project.loader_version, "minecraft_version": project.minecraft_version, "cloth_config_version": project.cloth_config_version + expand "version": project.version, + "fabric_version": project.fabric_version, + "loader_version": project.loader_version, + "minecraft_version": project.minecraft_version, + "cloth_config_version": project.cloth_config_version } } @@ -82,4 +86,4 @@ publishing { repositories { } -} \ No newline at end of file +} diff --git a/src/main/java/net/dehydration/block/AbstractCopperCauldronBlock.java b/src/main/java/net/dehydration/block/AbstractCopperCauldronBlock.java index b60da60..2d36769 100644 --- a/src/main/java/net/dehydration/block/AbstractCopperCauldronBlock.java +++ b/src/main/java/net/dehydration/block/AbstractCopperCauldronBlock.java @@ -1,9 +1,11 @@ package net.dehydration.block; -import java.util.Map; - import net.dehydration.block.entity.CopperCauldronBehavior; import net.dehydration.init.BlockInit; +import net.fabricmc.fabric.api.transfer.v1.fluid.FluidStorage; +import net.fabricmc.fabric.api.transfer.v1.fluid.FluidStorageUtil; +import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant; +import net.fabricmc.fabric.api.transfer.v1.storage.Storage; import net.minecraft.block.AbstractBlock; import net.minecraft.block.Block; import net.minecraft.block.BlockState; @@ -29,6 +31,8 @@ import net.minecraft.world.World; import net.minecraft.world.WorldView; +import java.util.Map; + public abstract class AbstractCopperCauldronBlock extends Block { private static final VoxelShape RAYCAST_SHAPE = createCuboidShape(2.0D, 4.0D, 2.0D, 14.0D, 16.0D, 14.0D); protected static final VoxelShape OUTLINE_SHAPE; @@ -49,8 +53,12 @@ protected boolean isEntityTouchingFluid(BlockState state, BlockPos pos, Entity e @Override protected ItemActionResult onUseWithItem(ItemStack stack, BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { + Storage storage = FluidStorage.SIDED.find(world, pos, hit.getSide().getOpposite()); + if (storage != null && FluidStorageUtil.interactWithFluidStorage(storage, player, hand)) { + return ItemActionResult.success(world.isClient()); + } ItemStack itemStack = player.getStackInHand(hand); - CopperCauldronBehavior cauldronBehavior = (CopperCauldronBehavior) this.behaviorMap.get(itemStack.getItem()); + CopperCauldronBehavior cauldronBehavior = this.behaviorMap.get(itemStack.getItem()); return cauldronBehavior.interact(state, world, pos, player, hand, itemStack); } diff --git a/src/main/java/net/dehydration/block/BambooPumpBlock.java b/src/main/java/net/dehydration/block/BambooPumpBlock.java index e32d14f..5092588 100644 --- a/src/main/java/net/dehydration/block/BambooPumpBlock.java +++ b/src/main/java/net/dehydration/block/BambooPumpBlock.java @@ -1,16 +1,15 @@ package net.dehydration.block; -import java.util.List; - -import org.jetbrains.annotations.Nullable; - import com.mojang.serialization.MapCodec; - import net.dehydration.block.entity.BambooPumpEntity; import net.dehydration.init.BlockInit; import net.dehydration.init.ConfigInit; import net.dehydration.init.ItemInit; import net.dehydration.item.LeatherFlask; +import net.fabricmc.fabric.api.transfer.v1.context.ContainerItemContext; +import net.fabricmc.fabric.api.transfer.v1.fluid.FluidStorage; +import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant; +import net.fabricmc.fabric.api.transfer.v1.storage.Storage; import net.minecraft.block.Block; import net.minecraft.block.BlockRenderType; import net.minecraft.block.BlockState; @@ -31,8 +30,8 @@ import net.minecraft.item.ItemPlacementContext; import net.minecraft.item.ItemStack; import net.minecraft.item.Items; -import net.minecraft.loot.context.LootContextParameters; import net.minecraft.loot.context.LootContextParameterSet.Builder; +import net.minecraft.loot.context.LootContextParameters; import net.minecraft.registry.tag.FluidTags; import net.minecraft.sound.SoundCategory; import net.minecraft.sound.SoundEvents; @@ -54,6 +53,9 @@ import net.minecraft.world.World; import net.minecraft.world.WorldAccess; import net.minecraft.world.WorldView; +import org.jetbrains.annotations.Nullable; + +import java.util.List; @SuppressWarnings("deprecation") public class BambooPumpBlock extends BlockWithEntity { @@ -87,7 +89,8 @@ public ItemActionResult onUseWithItem(ItemStack stack, BlockState state, World w ItemStack itemStack = bambooPumpEntity.getStack(0); ItemStack itemStack2 = player.getStackInHand(hand); if (itemStack.isEmpty()) { - if (itemStack2.isOf(Items.BUCKET) || itemStack2.isOf(Items.GLASS_BOTTLE) || (itemStack2.getItem() instanceof LeatherFlask && !LeatherFlask.isFlaskFull(itemStack2))) { + Storage storage = ContainerItemContext.withConstant(itemStack2).find(FluidStorage.ITEM); + if ((storage != null && storage.supportsInsertion()) || (itemStack2.getItem() instanceof LeatherFlask && !LeatherFlask.isFlaskFull(itemStack2))) { if (!world.isClient()) { if (player.isCreative()) { bambooPumpEntity.setStack(0, itemStack2.copy()); @@ -118,7 +121,8 @@ public ItemActionResult onUseWithItem(ItemStack stack, BlockState state, World w } return ItemActionResult.success(world.isClient()); } - if (itemStack.isOf(Items.BUCKET) || itemStack.isOf(Items.GLASS_BOTTLE) || (itemStack.getItem() instanceof LeatherFlask && !LeatherFlask.isFlaskFull(itemStack))) { + Storage storage = ContainerItemContext.withConstant(itemStack).find(FluidStorage.ITEM); + if ((storage != null && storage.supportsInsertion()) || (itemStack.getItem() instanceof LeatherFlask && !LeatherFlask.isFlaskFull(itemStack))) { if (ConfigInit.CONFIG.pump_requires_water) { boolean foundWater = false; for (int i = 0; i < 50; i++) { diff --git a/src/main/java/net/dehydration/block/CampfireCauldronBlock.java b/src/main/java/net/dehydration/block/CampfireCauldronBlock.java index 3a9835d..427fe38 100644 --- a/src/main/java/net/dehydration/block/CampfireCauldronBlock.java +++ b/src/main/java/net/dehydration/block/CampfireCauldronBlock.java @@ -2,29 +2,30 @@ import net.dehydration.block.entity.CampfireCauldronEntity; import net.dehydration.init.BlockInit; -import net.dehydration.init.ItemInit; import net.dehydration.init.SoundInit; -import net.dehydration.item.LeatherFlask; -import net.dehydration.item.component.FlaskComponent; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; -import net.minecraft.block.*; +import net.fabricmc.fabric.api.transfer.v1.fluid.FluidStorage; +import net.fabricmc.fabric.api.transfer.v1.fluid.FluidStorageUtil; +import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant; +import net.fabricmc.fabric.api.transfer.v1.storage.Storage; +import net.minecraft.block.AbstractBlock; +import net.minecraft.block.Block; +import net.minecraft.block.BlockEntityProvider; +import net.minecraft.block.BlockState; +import net.minecraft.block.CampfireBlock; +import net.minecraft.block.HorizontalFacingBlock; +import net.minecraft.block.ShapeContext; import net.minecraft.block.entity.BlockEntity; import net.minecraft.block.entity.BlockEntityTicker; import net.minecraft.block.entity.BlockEntityType; -import net.minecraft.component.DataComponentTypes; -import net.minecraft.component.type.PotionContentsComponent; import net.minecraft.entity.Entity; import net.minecraft.entity.ai.pathing.NavigationType; import net.minecraft.entity.player.PlayerEntity; -import net.minecraft.item.Item; import net.minecraft.item.ItemPlacementContext; import net.minecraft.item.ItemStack; -import net.minecraft.item.Items; -import net.minecraft.potion.Potions; import net.minecraft.registry.tag.BlockTags; import net.minecraft.sound.SoundCategory; -import net.minecraft.sound.SoundEvents; import net.minecraft.state.StateManager; import net.minecraft.state.property.DirectionProperty; import net.minecraft.state.property.IntProperty; @@ -95,105 +96,13 @@ public boolean canPlaceAt(BlockState state, WorldView world, BlockPos pos) { @Override public ItemActionResult onUseWithItem(ItemStack stack, BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { - ItemStack itemStack = player.getStackInHand(hand); - CampfireCauldronEntity campfireCauldronEntity = (CampfireCauldronEntity) world.getBlockEntity(pos); - if (itemStack.isEmpty()) { - return ItemActionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION; - } else { - int i = (Integer) state.get(LEVEL); - Item item = itemStack.getItem(); - if (item == Items.WATER_BUCKET) { - if (i < 4 && !world.isClient()) { - if (!player.isCreative()) { - player.setStackInHand(hand, new ItemStack(Items.BUCKET)); - } - campfireCauldronEntity.onFillingCauldron(); - this.setLevel(world, pos, state, 4); - world.playSound(null, pos, SoundEvents.ITEM_BUCKET_EMPTY, SoundCategory.BLOCKS, 1.0F, 1.0F); - } - return ItemActionResult.success(world.isClient()); - - } else if (item == Items.BUCKET) { - if (i == 4 && !world.isClient()) { - if (!player.isCreative()) { - itemStack.decrement(1); - if (itemStack.isEmpty()) { - player.setStackInHand(hand, new ItemStack(Items.WATER_BUCKET)); - } else if (!player.getInventory().insertStack(new ItemStack(Items.WATER_BUCKET))) { - player.dropItem(new ItemStack(Items.WATER_BUCKET), false); - } - } - this.setLevel(world, pos, state, 0); - world.playSound(null, pos, SoundEvents.ITEM_BUCKET_FILL, SoundCategory.BLOCKS, 1.0F, 1.0F); - } + if (!player.getStackInHand(hand).isEmpty()) { + Storage storage = FluidStorage.SIDED.find(world, pos, hit.getSide().getOpposite()); + if (storage != null && FluidStorageUtil.interactWithFluidStorage(storage, player, hand)) { return ItemActionResult.success(world.isClient()); - - } else { - - ItemStack newItemStack; - if (item == Items.GLASS_BOTTLE) { - if (i > 0 && !world.isClient()) { - if (!player.isCreative()) { - itemStack.decrement(1); - if (this.isPurifiedWater(world, pos)) { - newItemStack = PotionContentsComponent.createStack(Items.POTION, ItemInit.PURIFIED_WATER); - } else { - newItemStack = PotionContentsComponent.createStack(Items.POTION, Potions.WATER); - } - if (player.getStackInHand(hand).isEmpty()) { - player.setStackInHand(hand, newItemStack); - } else if (!player.getInventory().insertStack(newItemStack)) { - player.dropItem(newItemStack, false); - } - } - world.playSound(null, pos, SoundEvents.ITEM_BOTTLE_FILL, SoundCategory.BLOCKS, 1.0F, 1.0F); - this.setLevel(world, pos, state, i - 1); - } - return ItemActionResult.success(world.isClient()); - - } else if (item == Items.POTION && itemStack.get(DataComponentTypes.POTION_CONTENTS) != null && (itemStack.get(DataComponentTypes.POTION_CONTENTS).potion().get() == Potions.WATER - || itemStack.get(DataComponentTypes.POTION_CONTENTS).potion().get() == ItemInit.PURIFIED_WATER)) { - if (i < 4 && !world.isClient()) { - if (!player.isCreative()) { - newItemStack = new ItemStack(Items.GLASS_BOTTLE); - player.setStackInHand(hand, newItemStack); - } - world.playSound(null, pos, SoundEvents.ITEM_BOTTLE_EMPTY, SoundCategory.BLOCKS, 1.0F, 1.0F); - if (itemStack.get(DataComponentTypes.POTION_CONTENTS) != null && itemStack.get(DataComponentTypes.POTION_CONTENTS).potion().get() == Potions.WATER) { - campfireCauldronEntity.onFillingCauldron(); - } - this.setLevel(world, pos, state, i + 1); - } - return ItemActionResult.success(world.isClient()); - - } else { - if (i > 0 && item instanceof LeatherFlask) { - FlaskComponent flaskComponent = itemStack.getOrDefault(ItemInit.FLASK_DATA, FlaskComponent.DEFAULT); - if (flaskComponent.fillLevel() < 2 + ((LeatherFlask) item).getExtraFillLevel()) { - int qualityLevel = 0; - if (this.isPurifiedWater(world, pos)) { - if ((flaskComponent.qualityLevel() == 0 || flaskComponent.fillLevel() == 0)) { - qualityLevel = 0; - } else { - qualityLevel = 1; - } - } else { - qualityLevel = 2; - } - - itemStack.set(ItemInit.FLASK_DATA, new FlaskComponent(flaskComponent.fillLevel() + 1, qualityLevel)); - this.setLevel(world, pos, state, i - 1); - world.playSound(null, pos, SoundInit.FILL_FLASK_EVENT, SoundCategory.NEUTRAL, 1.0F, 1.0F); - return ItemActionResult.success(world.isClient()); - } else { - return ItemActionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION; - } - } else { - return ItemActionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION; - } - } } } + return ItemActionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION; } public void setLevel(World world, BlockPos pos, BlockState state, int level) { diff --git a/src/main/java/net/dehydration/block/entity/BambooPumpEntity.java b/src/main/java/net/dehydration/block/entity/BambooPumpEntity.java index aa2c748..b1cfaf3 100644 --- a/src/main/java/net/dehydration/block/entity/BambooPumpEntity.java +++ b/src/main/java/net/dehydration/block/entity/BambooPumpEntity.java @@ -2,16 +2,22 @@ import net.dehydration.init.BlockInit; import net.dehydration.init.ConfigInit; -import net.dehydration.init.ItemInit; -import net.dehydration.item.LeatherFlask; +import net.dehydration.init.FluidInit; +import net.fabricmc.fabric.api.transfer.v1.context.ContainerItemContext; +import net.fabricmc.fabric.api.transfer.v1.fluid.FluidConstants; +import net.fabricmc.fabric.api.transfer.v1.fluid.FluidStorage; +import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant; +import net.fabricmc.fabric.api.transfer.v1.item.ItemStorage; +import net.fabricmc.fabric.api.transfer.v1.item.ItemVariant; +import net.fabricmc.fabric.api.transfer.v1.storage.Storage; +import net.fabricmc.fabric.api.transfer.v1.storage.base.SingleSlotStorage; +import net.fabricmc.fabric.api.transfer.v1.transaction.Transaction; import net.minecraft.block.BlockState; import net.minecraft.block.entity.BlockEntity; -import net.minecraft.component.type.PotionContentsComponent; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.inventory.Inventories; import net.minecraft.inventory.Inventory; import net.minecraft.item.ItemStack; -import net.minecraft.item.Items; import net.minecraft.nbt.NbtCompound; import net.minecraft.network.packet.s2c.play.BlockEntityUpdateS2CPacket; import net.minecraft.registry.RegistryWrapper.WrapperLookup; @@ -69,28 +75,22 @@ private void sendUpdate() { } private void updateInventory() { - if (!getStack(0).isEmpty()) { - ItemStack itemStack = getStack(0); - if (itemStack.isOf(Items.BUCKET)) { - if (pumpCount > 3) { - if (!this.world.isClient()) - setStack(0, new ItemStack(ItemInit.PURIFIED_BUCKET)); - pumpCount = 0; - cooldown = ConfigInit.CONFIG.pump_cooldown; - } - } else if (itemStack.isOf(Items.GLASS_BOTTLE)) { - if (!this.world.isClient()) { - setStack(0, PotionContentsComponent.createStack(Items.POTION, ItemInit.PURIFIED_WATER)); - } - pumpCount = 0; - cooldown = ConfigInit.CONFIG.pump_cooldown; - } else if (itemStack.getItem() instanceof LeatherFlask) { - if (!this.world.isClient()) { - LeatherFlask.fillFlask(itemStack, 2); - setStack(0, itemStack); + ItemStack itemStack = getStack(0); + if (!itemStack.isEmpty()) { + Storage fluidStorage = null; + Storage itemStorage = ItemStorage.SIDED.find(this.world, this.pos, null); + if (itemStorage instanceof SingleSlotStorage singleSlotStorage) { + fluidStorage = ContainerItemContext.ofSingleSlot(singleSlotStorage).find(FluidStorage.ITEM); + } + if (fluidStorage != null && fluidStorage.supportsInsertion()) { + long amount = pumpCount * FluidConstants.BOTTLE * 2; + try (Transaction transaction = Transaction.openOuter()) { + if (fluidStorage.insert(FluidVariant.of(FluidInit.PURIFIED_WATER), amount, transaction) > 0) { + transaction.commit(); + pumpCount = 0; + cooldown = ConfigInit.CONFIG.pump_cooldown; + } } - pumpCount = 0; - cooldown = ConfigInit.CONFIG.pump_cooldown; } } } diff --git a/src/main/java/net/dehydration/block/entity/CopperCauldronBehavior.java b/src/main/java/net/dehydration/block/entity/CopperCauldronBehavior.java index bffa32e..b7bf700 100644 --- a/src/main/java/net/dehydration/block/entity/CopperCauldronBehavior.java +++ b/src/main/java/net/dehydration/block/entity/CopperCauldronBehavior.java @@ -1,45 +1,34 @@ package net.dehydration.block.entity; import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; - -import java.util.Map; -import java.util.function.Predicate; - import net.dehydration.block.CopperLeveledCauldronBlock; import net.dehydration.init.BlockInit; -import net.dehydration.init.ItemInit; -import net.dehydration.init.SoundInit; -import net.dehydration.item.LeatherFlask; -import net.dehydration.item.component.FlaskComponent; import net.minecraft.block.BlockState; -import net.minecraft.component.DataComponentTypes; -import net.minecraft.component.type.PotionContentsComponent; import net.minecraft.entity.Entity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.item.ItemUsage; import net.minecraft.item.Items; -import net.minecraft.potion.Potions; import net.minecraft.sound.SoundCategory; import net.minecraft.sound.SoundEvent; import net.minecraft.sound.SoundEvents; import net.minecraft.stat.Stats; -import net.minecraft.util.ItemActionResult; import net.minecraft.util.Hand; +import net.minecraft.util.ItemActionResult; import net.minecraft.util.Util; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import net.minecraft.world.event.GameEvent; +import java.util.Map; +import java.util.function.Predicate; + public interface CopperCauldronBehavior { Map EMPTY_COPPER_CAULDRON_BEHAVIOR = createMap(); Map WATER_COPPER_CAULDRON_BEHAVIOR = createMap(); Map POWDER_SNOW_COPPER_CAULDRON_BEHAVIOR = createMap(); Map PURIFIED_WATER_COPPER_CAULDRON_BEHAVIOR = createMap(); - CopperCauldronBehavior FILL_WITH_WATER = (state, world, pos, player, hand, stack) -> { - return fillCauldron(world, pos, player, hand, stack, BlockInit.COPPER_WATER_CAULDRON_BLOCK.getDefaultState().with(CopperLeveledCauldronBlock.LEVEL, 3), SoundEvents.ITEM_BUCKET_EMPTY); - }; CopperCauldronBehavior FILL_WITH_POWDER_SNOW = (state, world, pos, player, hand, stack) -> { return fillCauldron(world, pos, player, hand, stack, BlockInit.COPPER_POWDERED_CAULDRON_BLOCK.getDefaultState().with(CopperLeveledCauldronBlock.LEVEL, 3), @@ -57,101 +46,7 @@ static Object2ObjectOpenHashMap createMap() { ItemActionResult interact(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, ItemStack stack); static void registerBehavior() { - EMPTY_COPPER_CAULDRON_BEHAVIOR.put(ItemInit.PURIFIED_BUCKET, (state, world, pos, player, hand, stack) -> { - return fillCauldron(world, pos, player, hand, stack, BlockInit.COPPER_PURIFIED_WATER_CAULDRON_BLOCK.getDefaultState().with(CopperLeveledCauldronBlock.LEVEL, 3), - SoundEvents.ITEM_BUCKET_EMPTY); - }); - EMPTY_COPPER_CAULDRON_BEHAVIOR.put(Items.POTION, (state, world, pos, player, hand, stack) -> { - if (stack.get(DataComponentTypes.POTION_CONTENTS) != null && stack.get(DataComponentTypes.POTION_CONTENTS).potion().get() == ItemInit.PURIFIED_WATER) { - if (!world.isClient()) { - Item item = stack.getItem(); - player.setStackInHand(hand, ItemUsage.exchangeStack(stack, player, new ItemStack(Items.GLASS_BOTTLE))); - player.incrementStat(Stats.FILL_CAULDRON); - player.incrementStat(Stats.USED.getOrCreateStat(item)); - world.setBlockState(pos, BlockInit.COPPER_PURIFIED_WATER_CAULDRON_BLOCK.getDefaultState()); - world.playSound(null, pos, SoundEvents.ITEM_BOTTLE_EMPTY, SoundCategory.BLOCKS, 1.0F, 1.0F); - world.emitGameEvent(null, GameEvent.FLUID_PLACE, pos); - } - return ItemActionResult.success(world.isClient()); - } else if (stack.get(DataComponentTypes.POTION_CONTENTS) != null && stack.get(DataComponentTypes.POTION_CONTENTS).potion().get() == Potions.WATER) { - if (!world.isClient()) { - Item item = stack.getItem(); - player.setStackInHand(hand, ItemUsage.exchangeStack(stack, player, new ItemStack(Items.GLASS_BOTTLE))); - player.incrementStat(Stats.FILL_CAULDRON); - player.incrementStat(Stats.USED.getOrCreateStat(item)); - world.setBlockState(pos, BlockInit.COPPER_WATER_CAULDRON_BLOCK.getDefaultState()); - world.playSound(null, pos, SoundEvents.ITEM_BOTTLE_EMPTY, SoundCategory.BLOCKS, 1.0F, 1.0F); - world.emitGameEvent(null, GameEvent.FLUID_PLACE, pos); - } - return ItemActionResult.success(world.isClient()); - } else - return ItemActionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION; - }); - for (int i = 0; i < ItemInit.FLASK_ITEM_LIST.size(); i++) { - EMPTY_COPPER_CAULDRON_BEHAVIOR.put(ItemInit.FLASK_ITEM_LIST.get(i), (state, world, pos, player, hand, stack) -> { - if (LeatherFlask.isFlaskEmpty(stack)) { - return ItemActionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION; - } else if (!world.isClient()) { - Item item = stack.getItem(); - FlaskComponent flaskComponent = stack.getOrDefault(ItemInit.FLASK_DATA, FlaskComponent.DEFAULT); - - if (flaskComponent.fillLevel() > 0) { - stack.set(ItemInit.FLASK_DATA, new FlaskComponent(flaskComponent.fillLevel() - 1, flaskComponent.qualityLevel())); - } else { - stack.set(ItemInit.FLASK_DATA, new FlaskComponent(1 + ((LeatherFlask) item).getExtraFillLevel(), flaskComponent.qualityLevel())); - } - if (flaskComponent.qualityLevel() == 0) { - world.setBlockState(pos, BlockInit.COPPER_PURIFIED_WATER_CAULDRON_BLOCK.getDefaultState()); - } else { - world.setBlockState(pos, BlockInit.COPPER_WATER_CAULDRON_BLOCK.getDefaultState()); - } - player.incrementStat(Stats.USE_CAULDRON); - player.incrementStat(Stats.USED.getOrCreateStat(item)); - - world.playSound(null, pos, SoundEvents.ITEM_BOTTLE_EMPTY, SoundCategory.BLOCKS, 1.0F, 1.0F); - world.emitGameEvent(null, GameEvent.FLUID_PLACE, pos); - } - return ItemActionResult.success(world.isClient()); - - }); - } registerBucketBehavior(EMPTY_COPPER_CAULDRON_BEHAVIOR); - WATER_COPPER_CAULDRON_BEHAVIOR.put(Items.BUCKET, (state, world, pos, player, hand, stack) -> { - return emptyCauldron(state, world, pos, player, hand, stack, new ItemStack(Items.WATER_BUCKET), (statex) -> { - return (Integer) statex.get(CopperLeveledCauldronBlock.LEVEL) == 3; - }, SoundEvents.ITEM_BUCKET_FILL); - }); - WATER_COPPER_CAULDRON_BEHAVIOR.put(Items.GLASS_BOTTLE, (state, world, pos, player, hand, stack) -> { - if (!world.isClient()) { - Item item = stack.getItem(); - - player.setStackInHand(hand, ItemUsage.exchangeStack(stack, player, PotionContentsComponent.createStack(Items.POTION, Potions.WATER))); - player.incrementStat(Stats.USE_CAULDRON); - player.incrementStat(Stats.USED.getOrCreateStat(item)); - CopperLeveledCauldronBlock.decrementFluidLevel(state, world, pos); - world.playSound(null, pos, SoundEvents.ITEM_BOTTLE_FILL, SoundCategory.BLOCKS, 1.0F, 1.0F); - world.emitGameEvent(null, GameEvent.FLUID_PICKUP, pos); - } - - return ItemActionResult.success(world.isClient()); - }); - WATER_COPPER_CAULDRON_BEHAVIOR.put(Items.POTION, (state, world, pos, player, hand, stack) -> { - if ((Integer) state.get(CopperLeveledCauldronBlock.LEVEL) != 3 && stack.get(DataComponentTypes.POTION_CONTENTS) != null - && stack.get(DataComponentTypes.POTION_CONTENTS).potion().get() == Potions.WATER) { - if (!world.isClient()) { - player.setStackInHand(hand, ItemUsage.exchangeStack(stack, player, new ItemStack(Items.GLASS_BOTTLE))); - player.incrementStat(Stats.USE_CAULDRON); - player.incrementStat(Stats.USED.getOrCreateStat(stack.getItem())); - world.setBlockState(pos, state.cycle(CopperLeveledCauldronBlock.LEVEL)); - world.playSound(null, pos, SoundEvents.ITEM_BOTTLE_EMPTY, SoundCategory.BLOCKS, 1.0F, 1.0F); - world.emitGameEvent(null, GameEvent.FLUID_PLACE, pos); - } - - return ItemActionResult.success(world.isClient()); - } else { - return ItemActionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION; - } - }); registerBucketBehavior(WATER_COPPER_CAULDRON_BEHAVIOR); POWDER_SNOW_COPPER_CAULDRON_BEHAVIOR.put(Items.BUCKET, (state, world, pos, player, hand, stack) -> { return emptyCauldron(state, world, pos, player, hand, stack, new ItemStack(Items.POWDER_SNOW_BUCKET), (statex) -> { @@ -159,96 +54,10 @@ static void registerBehavior() { }, SoundEvents.ITEM_BUCKET_FILL_POWDER_SNOW); }); registerBucketBehavior(POWDER_SNOW_COPPER_CAULDRON_BEHAVIOR); - - PURIFIED_WATER_COPPER_CAULDRON_BEHAVIOR.put(Items.BUCKET, (state, world, pos, player, hand, stack) -> { - return emptyCauldron(state, world, pos, player, hand, stack, new ItemStack(ItemInit.PURIFIED_BUCKET), (statex) -> { - return (Integer) statex.get(CopperLeveledCauldronBlock.LEVEL) == 3; - }, SoundEvents.ITEM_BUCKET_FILL); - }); - PURIFIED_WATER_COPPER_CAULDRON_BEHAVIOR.put(Items.GLASS_BOTTLE, (state, world, pos, player, hand, stack) -> { - if (!world.isClient()) { - Item item = stack.getItem(); - player.setStackInHand(hand, ItemUsage.exchangeStack(stack, player, PotionContentsComponent.createStack(Items.POTION, ItemInit.PURIFIED_WATER))); - player.incrementStat(Stats.USE_CAULDRON); - player.incrementStat(Stats.USED.getOrCreateStat(item)); - CopperLeveledCauldronBlock.decrementFluidLevel(state, world, pos); - world.playSound((PlayerEntity) null, pos, SoundEvents.ITEM_BOTTLE_FILL, SoundCategory.BLOCKS, 1.0F, 1.0F); - world.emitGameEvent((Entity) null, GameEvent.FLUID_PICKUP, pos); - } - - return ItemActionResult.success(world.isClient()); - }); - PURIFIED_WATER_COPPER_CAULDRON_BEHAVIOR.put(Items.POTION, (state, world, pos, player, hand, stack) -> { - if ((Integer) state.get(CopperLeveledCauldronBlock.LEVEL) != 3 && stack.get(DataComponentTypes.POTION_CONTENTS) != null - && stack.get(DataComponentTypes.POTION_CONTENTS).potion().get() == ItemInit.PURIFIED_WATER) { - if (!world.isClient()) { - player.setStackInHand(hand, ItemUsage.exchangeStack(stack, player, new ItemStack(Items.GLASS_BOTTLE))); - player.incrementStat(Stats.USE_CAULDRON); - player.incrementStat(Stats.USED.getOrCreateStat(stack.getItem())); - world.setBlockState(pos, state.cycle(CopperLeveledCauldronBlock.LEVEL)); - world.playSound((PlayerEntity) null, pos, SoundEvents.ITEM_BOTTLE_EMPTY, SoundCategory.BLOCKS, 1.0F, 1.0F); - world.emitGameEvent((Entity) null, GameEvent.FLUID_PLACE, pos); - } - - return ItemActionResult.success(world.isClient()); - } else { - return ItemActionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION; - } - }); - for (int i = 0; i < ItemInit.FLASK_ITEM_LIST.size(); i++) { - PURIFIED_WATER_COPPER_CAULDRON_BEHAVIOR.put(ItemInit.FLASK_ITEM_LIST.get(i), (state, world, pos, player, hand, stack) -> { - if (!world.isClient()) { - boolean playerIsSneaking = player.isSneaking(); - Item item = stack.getItem(); - FlaskComponent flaskComponent = stack.getOrDefault(ItemInit.FLASK_DATA, FlaskComponent.DEFAULT); - - if (stack.get(ItemInit.FLASK_DATA) == null) { - stack.set(ItemInit.FLASK_DATA, new FlaskComponent(2 + ((LeatherFlask) item).getExtraFillLevel(), 0)); - } - - // Fill cauldron - if (playerIsSneaking) { - if (!LeatherFlask.isFlaskEmpty(stack)) - if (!((CopperLeveledCauldronBlock) state.getBlock()).isFull(state)) { - stack.set(ItemInit.FLASK_DATA, new FlaskComponent(flaskComponent.fillLevel() - 1, flaskComponent.qualityLevel())); - if (flaskComponent.qualityLevel() == 0) { - world.setBlockState(pos, state.cycle(CopperLeveledCauldronBlock.LEVEL)); - } else { - world.setBlockState(pos, - BlockInit.COPPER_WATER_CAULDRON_BLOCK.getDefaultState().with(CopperLeveledCauldronBlock.LEVEL, state.get(CopperLeveledCauldronBlock.LEVEL) + 1)); - } - world.playSound((PlayerEntity) null, pos, SoundInit.EMPTY_FLASK_EVENT, SoundCategory.BLOCKS, 1.0F, 1.0F); - world.emitGameEvent((Entity) null, GameEvent.FLUID_PLACE, pos); - } else { - return ItemActionResult.CONSUME; - } - } else { - // Fill flask - if (!LeatherFlask.isFlaskFull(stack)) { - stack.set(ItemInit.FLASK_DATA, new FlaskComponent(flaskComponent.fillLevel() + 1, flaskComponent.qualityLevel())); - if (flaskComponent.qualityLevel() != 0) { - stack.set(ItemInit.FLASK_DATA, new FlaskComponent(flaskComponent.fillLevel(), 1)); - } - - CopperLeveledCauldronBlock.decrementFluidLevel(state, world, pos); - world.playSound((PlayerEntity) null, pos, SoundInit.FILL_FLASK_EVENT, SoundCategory.BLOCKS, 1.0F, 1.0F); - world.emitGameEvent((Entity) null, GameEvent.FLUID_PICKUP, pos); - } else - return ItemActionResult.CONSUME; - - } - player.incrementStat(Stats.USE_CAULDRON); - player.incrementStat(Stats.USED.getOrCreateStat(item)); - } - - return ItemActionResult.success(world.isClient()); - }); - } registerBucketBehavior(PURIFIED_WATER_COPPER_CAULDRON_BEHAVIOR); } static void registerBucketBehavior(Map behavior) { - behavior.put(Items.WATER_BUCKET, FILL_WITH_WATER); behavior.put(Items.POWDER_SNOW_BUCKET, FILL_WITH_POWDER_SNOW); } diff --git a/src/main/java/net/dehydration/fluid/storage/CampfireCauldronFluidStorage.java b/src/main/java/net/dehydration/fluid/storage/CampfireCauldronFluidStorage.java new file mode 100644 index 0000000..3ba5a2d --- /dev/null +++ b/src/main/java/net/dehydration/fluid/storage/CampfireCauldronFluidStorage.java @@ -0,0 +1,133 @@ +package net.dehydration.fluid.storage; + +import net.dehydration.block.CampfireCauldronBlock; +import net.dehydration.block.entity.CampfireCauldronEntity; +import net.dehydration.init.FluidInit; +import net.fabricmc.fabric.api.transfer.v1.fluid.FluidConstants; +import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant; +import net.fabricmc.fabric.api.transfer.v1.storage.StoragePreconditions; +import net.fabricmc.fabric.api.transfer.v1.storage.base.ResourceAmount; +import net.fabricmc.fabric.api.transfer.v1.storage.base.SingleVariantStorage; +import net.fabricmc.fabric.api.transfer.v1.transaction.TransactionContext; +import net.minecraft.block.BlockState; +import net.minecraft.fluid.Fluids; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; + +/** + * fluid storage for campfire cauldron + */ +public class CampfireCauldronFluidStorage extends SingleVariantStorage { + + private static final long CAPACITY = FluidConstants.BOTTLE * 4L; + + private final World world; + private final BlockPos pos; + private final BlockState state; + private final CampfireCauldronEntity campfireCauldronEntity; + + private int fluidLevel; + private boolean updateBoiling = false; + + public CampfireCauldronFluidStorage(World world, BlockPos pos, BlockState state, CampfireCauldronEntity campfireCauldronEntity) { + this.world = world; + this.pos = pos; + this.state = state; + this.campfireCauldronEntity = campfireCauldronEntity; + this.fluidLevel = state.get(CampfireCauldronBlock.LEVEL); + if (this.fluidLevel > 0) { + this.variant = campfireCauldronEntity.isBoiled ? FluidVariant.of(FluidInit.PURIFIED_WATER) : FluidVariant.of(Fluids.WATER); + this.amount = this.fluidLevel * FluidConstants.BOTTLE; + } + } + + @Override + protected FluidVariant getBlankVariant() { + return FluidVariant.blank(); + } + + @Override + protected long getCapacity(FluidVariant variant) { + return CAPACITY; + } + + @Override + protected boolean canExtract(FluidVariant variant) { + return this.variant.getFluid() == variant.getFluid() && this.fluidLevel > 0; + } + + @Override + protected boolean canInsert(FluidVariant variant) { + return (variant.getFluid() == Fluids.WATER || variant.getFluid() == FluidInit.PURIFIED_WATER) && this.fluidLevel < 4; + } + + @Override + protected void readSnapshot(ResourceAmount snapshot) { + super.readSnapshot(snapshot); + //update values if a snapshot is loaded + this.fluidLevel = Math.min((int) (this.amount / FluidConstants.BOTTLE), 4); + this.updateBoiling = !this.variant.isBlank() && this.variant.getFluid() != FluidInit.PURIFIED_WATER; + } + + @Override + public long insert(FluidVariant insertedVariant, long maxAmount, TransactionContext transaction) { + StoragePreconditions.notBlankNotNegative(insertedVariant, maxAmount); + if (!canInsert(insertedVariant)) { + return 0; + } + updateSnapshots(transaction); + // minimal amount to insert is the bottle amount to increase the fluid level + int levelIncrease = Math.min((int) (maxAmount / FluidConstants.BOTTLE), 4 - this.fluidLevel); + if (levelIncrease <= 0) { + return 0; + } + //update fluid storage + this.fluidLevel += levelIncrease; + long insertedAmount = levelIncrease * FluidConstants.BOTTLE; + this.amount += insertedAmount; + if (this.variant.getFluid() != FluidInit.PURIFIED_WATER || insertedVariant.getFluid() != FluidInit.PURIFIED_WATER) { + this.updateBoiling = true; + } + if (this.variant.isBlank()) { + this.variant = insertedVariant; + } else if (this.variant.getFluid() != insertedVariant.getFluid()) { + this.variant = this.variant.getFluid() != FluidInit.PURIFIED_WATER ? insertedVariant : this.variant; + } + return insertedAmount; + } + + @Override + public long extract(FluidVariant extractedVariant, long maxAmount, TransactionContext transaction) { + StoragePreconditions.notBlankNotNegative(extractedVariant, maxAmount); + if (!canExtract(extractedVariant)) { + return 0; + } + updateSnapshots(transaction); + // minimal amount to extract is the bottle amount to decrease the fluid level + int levelDecrease = Math.min((int) (maxAmount / FluidConstants.BOTTLE), this.fluidLevel); + if (levelDecrease <= 0) { + return 0; + } + //update fluid storage + this.fluidLevel -= levelDecrease; + long extractedAmount = levelDecrease * FluidConstants.BOTTLE; + this.amount -= extractedAmount; + if (this.amount <= 0 || this.fluidLevel <= 0) { + this.fluidLevel = 0; + this.amount = 0; + this.variant = this.getBlankVariant(); + } + return extractedAmount; + } + + @Override + protected void onFinalCommit() { + //update block & block entity + if (this.state.getBlock() instanceof CampfireCauldronBlock campfireCauldronBlock) { + campfireCauldronBlock.setLevel(this.world, this.pos, this.state, this.fluidLevel); + if (this.updateBoiling) { + this.campfireCauldronEntity.onFillingCauldron(); + } + } + } +} diff --git a/src/main/java/net/dehydration/fluid/storage/CopperCauldronFluidStorage.java b/src/main/java/net/dehydration/fluid/storage/CopperCauldronFluidStorage.java new file mode 100644 index 0000000..f069f96 --- /dev/null +++ b/src/main/java/net/dehydration/fluid/storage/CopperCauldronFluidStorage.java @@ -0,0 +1,233 @@ +package net.dehydration.fluid.storage; + +import com.google.common.primitives.Ints; +import net.dehydration.init.BlockInit; +import net.dehydration.init.FluidInit; +import net.fabricmc.fabric.api.transfer.v1.fluid.FluidConstants; +import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant; +import net.fabricmc.fabric.api.transfer.v1.storage.StoragePreconditions; +import net.fabricmc.fabric.api.transfer.v1.storage.base.SingleSlotStorage; +import net.fabricmc.fabric.api.transfer.v1.transaction.TransactionContext; +import net.fabricmc.fabric.api.transfer.v1.transaction.base.SnapshotParticipant; +import net.minecraft.block.Block; +import net.minecraft.block.BlockState; +import net.minecraft.block.LeveledCauldronBlock; +import net.minecraft.fluid.Fluid; +import net.minecraft.fluid.Fluids; +import net.minecraft.state.property.IntProperty; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; + +public class CopperCauldronFluidStorage extends SnapshotParticipant implements SingleSlotStorage { + + private final World world; + private final BlockPos pos; + + private BlockState lastReleasedSnapshot; + + public CopperCauldronFluidStorage(World world, BlockPos pos) { + this.world = world; + this.pos = pos; + } + + // Retrieve the current CauldronFluidContent. + private CopperCauldronFluidContent getCurrentContent() { + return new CopperCauldronFluidContent(createSnapshot().getBlock()); + } + + // Called by insert and extract to update the block state. + private void updateLevel(CopperCauldronFluidContent newContent, int level, TransactionContext transaction) { + updateSnapshots(transaction); + BlockState newState = newContent.block.getDefaultState(); + + if (newContent.levelProperty != null) { + newState = newState.with(newContent.levelProperty, level); + } + + // Set block state without updates. + world.setBlockState(pos, newState, 0); + } + + @Override + public long insert(FluidVariant fluidVariant, long maxAmount, TransactionContext transaction) { + StoragePreconditions.notBlankNotNegative(fluidVariant, maxAmount); + + CopperCauldronFluidContent insertContent = new CopperCauldronFluidContent(fluidVariant.getFluid()); + + int maxLevelsInserted = Ints.saturatedCast(maxAmount / insertContent.amountPerLevel); + + if (getAmount() == 0) { + // Currently empty, so we can accept any fluid. + int levelsInserted = Math.min(maxLevelsInserted, insertContent.maxLevel); + + if (levelsInserted > 0) { + updateLevel(insertContent, levelsInserted, transaction); + } + + return levelsInserted * insertContent.amountPerLevel; + } + + CopperCauldronFluidContent currentContent = getCurrentContent(); + + if (fluidVariant.isOf(currentContent.fluid)) { + // Otherwise we can only accept the same fluid as the current one. + int currentLevel = currentContent.currentLevel(createSnapshot()); + int levelsInserted = Math.min(maxLevelsInserted, currentContent.maxLevel - currentLevel); + + if (levelsInserted > 0) { + updateLevel(currentContent, currentLevel + levelsInserted, transaction); + } + + return levelsInserted * currentContent.amountPerLevel; + } + + return 0; + } + + @Override + public long extract(FluidVariant fluidVariant, long maxAmount, TransactionContext transaction) { + StoragePreconditions.notBlankNotNegative(fluidVariant, maxAmount); + + CopperCauldronFluidContent currentContent = getCurrentContent(); + + if (fluidVariant.isOf(currentContent.fluid)) { + int maxLevelsExtracted = Ints.saturatedCast(maxAmount / currentContent.amountPerLevel); + int currentLevel = currentContent.currentLevel(createSnapshot()); + int levelsExtracted = Math.min(maxLevelsExtracted, currentLevel); + + if (levelsExtracted > 0) { + if (levelsExtracted == currentLevel) { + // Fully extract -> back to empty copper cauldron + updateSnapshots(transaction); + world.setBlockState(pos, BlockInit.COPPER_CAULDRON_BLOCK.getDefaultState(), 0); + } else { + // Otherwise just decrease levels + updateLevel(currentContent, currentLevel - levelsExtracted, transaction); + } + } + + return levelsExtracted * currentContent.amountPerLevel; + } + + return 0; + } + + @Override + public boolean isResourceBlank() { + return getResource().isBlank(); + } + + @Override + public FluidVariant getResource() { + return FluidVariant.of(getCurrentContent().fluid); + } + + @Override + public long getAmount() { + CopperCauldronFluidContent currentContent = getCurrentContent(); + return currentContent.currentLevel(createSnapshot()) * currentContent.amountPerLevel; + } + + @Override + public long getCapacity() { + CopperCauldronFluidContent currentContent = getCurrentContent(); + return currentContent.maxLevel * currentContent.amountPerLevel; + } + + @Override + public BlockState createSnapshot() { + return world.getBlockState(pos); + } + + @Override + public void readSnapshot(BlockState savedState) { + world.setBlockState(pos, savedState, 0); + } + + @Override + protected void releaseSnapshot(BlockState snapshot) { + lastReleasedSnapshot = snapshot; + } + + @Override + public void onFinalCommit() { + BlockState state = createSnapshot(); + BlockState originalState = lastReleasedSnapshot; + + if (originalState != state) { + // Revert change + world.setBlockState(pos, originalState, 0); + // Then do the actual change with normal block updates + world.setBlockState(pos, state); + } + } + + @Override + public String toString() { + return "CopperCauldronStorage[" + world + ", " + pos + "]"; + } + + private static class CopperCauldronFluidContent { + + final Block block; + final Fluid fluid; + final long amountPerLevel; + final int maxLevel; + final IntProperty levelProperty; + + private CopperCauldronFluidContent(Block block) { + if (block == BlockInit.COPPER_WATER_CAULDRON_BLOCK) { + this.block = block; + fluid = Fluids.WATER; + amountPerLevel = FluidConstants.BOTTLE; + maxLevel = 3; + levelProperty = LeveledCauldronBlock.LEVEL; + } else if (block == BlockInit.COPPER_PURIFIED_WATER_CAULDRON_BLOCK) { + this.block = block; + fluid = FluidInit.PURIFIED_WATER; + amountPerLevel = FluidConstants.BOTTLE; + maxLevel = 3; + levelProperty = LeveledCauldronBlock.LEVEL; + } else { + this.block = BlockInit.COPPER_CAULDRON_BLOCK; + fluid = Fluids.EMPTY; + amountPerLevel = FluidConstants.BUCKET; + maxLevel = 1; + levelProperty = null; + } + } + + private CopperCauldronFluidContent(Fluid fluid) { + if (fluid == Fluids.WATER) { + this.fluid = fluid; + block = BlockInit.COPPER_WATER_CAULDRON_BLOCK; + amountPerLevel = FluidConstants.BOTTLE; + maxLevel = 3; + levelProperty = LeveledCauldronBlock.LEVEL; + } else if (fluid == FluidInit.PURIFIED_WATER) { + this.fluid = fluid; + block = BlockInit.COPPER_PURIFIED_WATER_CAULDRON_BLOCK; + amountPerLevel = FluidConstants.BOTTLE; + maxLevel = 3; + levelProperty = LeveledCauldronBlock.LEVEL; + } else { + this.fluid = Fluids.EMPTY; + block = BlockInit.COPPER_CAULDRON_BLOCK; + amountPerLevel = FluidConstants.BUCKET; + maxLevel = 1; + levelProperty = null; + } + } + + private int currentLevel(BlockState state) { + if (fluid == Fluids.EMPTY) { + return 0; + } else if (levelProperty == null) { + return 1; + } else { + return state.get(levelProperty); + } + } + + } +} \ No newline at end of file diff --git a/src/main/java/net/dehydration/fluid/storage/LeatherFlaskFluidStorage.java b/src/main/java/net/dehydration/fluid/storage/LeatherFlaskFluidStorage.java new file mode 100644 index 0000000..98bc1ef --- /dev/null +++ b/src/main/java/net/dehydration/fluid/storage/LeatherFlaskFluidStorage.java @@ -0,0 +1,158 @@ +package net.dehydration.fluid.storage; + +import net.dehydration.init.FluidInit; +import net.dehydration.init.ItemInit; +import net.dehydration.item.LeatherFlask; +import net.dehydration.item.component.FlaskComponent; +import net.fabricmc.fabric.api.transfer.v1.context.ContainerItemContext; +import net.fabricmc.fabric.api.transfer.v1.fluid.FluidConstants; +import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant; +import net.fabricmc.fabric.api.transfer.v1.item.ItemVariant; +import net.fabricmc.fabric.api.transfer.v1.storage.StoragePreconditions; +import net.fabricmc.fabric.api.transfer.v1.storage.base.ResourceAmount; +import net.fabricmc.fabric.api.transfer.v1.storage.base.SingleVariantStorage; +import net.fabricmc.fabric.api.transfer.v1.transaction.TransactionContext; +import net.minecraft.fluid.Fluids; +import net.minecraft.item.ItemStack; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +public class LeatherFlaskFluidStorage extends SingleVariantStorage { + + private final static FluidVariant DIRTY_WATER = FluidVariant.of(Fluids.WATER); + private final static FluidVariant PURIFIED_WATER = FluidVariant.of(FluidInit.PURIFIED_WATER); + + private final ContainerItemContext context; + private final LeatherFlask flaskItem; + private final Map, Integer> qualitySnapshots = new HashMap<>(); + + private int fillLevel = 0; + private int qualityLevel = 0; + + public LeatherFlaskFluidStorage(ContainerItemContext context) { + this.context = context; + ItemVariant itemVariant = context.getItemVariant(); + this.flaskItem = (LeatherFlask) itemVariant.getItem(); + Optional optional = itemVariant.getComponents().get(ItemInit.FLASK_DATA); + if (optional != null) { + optional.ifPresent(data -> { + if (data.fillLevel() > 0) { + this.fillLevel = data.fillLevel(); + this.qualityLevel = data.qualityLevel(); + this.variant = (this.qualityLevel > 0) ? DIRTY_WATER : PURIFIED_WATER; + this.amount = this.fillLevel * FluidConstants.BOTTLE; + } + }); + } + } + + @Override + protected FluidVariant getBlankVariant() { + return FluidVariant.blank(); + } + + private int getMaxLevel() { + return 2 + this.flaskItem.getExtraFillLevel(); + } + + @Override + protected long getCapacity(FluidVariant variant) { + return FluidConstants.BOTTLE * (long) getMaxLevel(); + } + + @Override + protected boolean canExtract(FluidVariant variant) { + return this.variant.getFluid() == variant.getFluid() && this.fillLevel > 0; + //return false; //cannot be extracted from + } + + @Override + protected boolean canInsert(FluidVariant variant) { + return (variant.getFluid() == Fluids.WATER || variant.getFluid() == FluidInit.PURIFIED_WATER) && this.fillLevel < getMaxLevel(); + } + + @Override + public long insert(FluidVariant insertedVariant, long maxAmount, TransactionContext transaction) { + StoragePreconditions.notBlankNotNegative(insertedVariant, maxAmount); + if (!canInsert(insertedVariant)) { + return 0; + } + updateSnapshots(transaction); + // minimal amount to insert is the bottle amount to increase the fill level + int levelIncrease = Math.min((int) (maxAmount / FluidConstants.BOTTLE), getMaxLevel() - this.fillLevel); + if (levelIncrease <= 0) { + return 0; + } + //update fluid storage + if (insertedVariant.getFluid() != FluidInit.PURIFIED_WATER) { + int dirtyAddition = (levelIncrease > this.fillLevel) ? 2 : 1; + this.qualityLevel = Math.min(this.qualityLevel + dirtyAddition, 2); + } + this.fillLevel += levelIncrease; + long insertedAmount = levelIncrease * FluidConstants.BOTTLE; + this.amount += insertedAmount; + this.variant = (this.qualityLevel) > 0 ? DIRTY_WATER : PURIFIED_WATER; + //exchange item + ItemStack stack = context.getItemVariant().toStack(); + stack.set(ItemInit.FLASK_DATA, new FlaskComponent(this.fillLevel, this.qualityLevel)); + if (context.exchange(ItemVariant.of(stack), 1, transaction) == 1) { + return insertedAmount; + } + return 0; + } + + @Override + public long extract(FluidVariant extractedVariant, long maxAmount, TransactionContext transaction) { + StoragePreconditions.notBlankNotNegative(extractedVariant, maxAmount); + if (!canExtract(extractedVariant)) { + return 0; + } + updateSnapshots(transaction); + // minimal amount to extract is the bottle amount to decrease the fill level + int levelDecrease = Math.min((int) (maxAmount / FluidConstants.BOTTLE), this.fillLevel); + if (levelDecrease <= 0) { + return 0; + } + //update fluid storage + this.fillLevel -= levelDecrease; + long extractedAmount = levelDecrease * FluidConstants.BOTTLE; + this.amount -= extractedAmount; + if (this.amount <= 0 || this.fillLevel <= 0) { + this.fillLevel = 0; + this.qualityLevel = 0; + this.amount = 0; + this.variant = this.getBlankVariant(); + } + //exchange item + ItemStack stack = context.getItemVariant().toStack(); + stack.set(ItemInit.FLASK_DATA, new FlaskComponent(this.fillLevel, this.qualityLevel)); + if (context.exchange(ItemVariant.of(stack), 1, transaction) == 1) { + return extractedAmount; + } + return 0; + } + + @Override + protected void releaseSnapshot(ResourceAmount snapshot) { + this.qualitySnapshots.remove(snapshot); + super.releaseSnapshot(snapshot); + } + + @Override + protected ResourceAmount createSnapshot() { + ResourceAmount snapshot = super.createSnapshot(); + this.qualitySnapshots.put(snapshot, this.qualityLevel); + return snapshot; + } + + @Override + protected void readSnapshot(ResourceAmount snapshot) { + super.readSnapshot(snapshot); + //update values if a snapshot is loaded + this.fillLevel = Math.min((int) (this.amount / FluidConstants.BOTTLE), getMaxLevel()); + this.qualityLevel = this.qualitySnapshots.get(snapshot); + } + +} diff --git a/src/main/java/net/dehydration/fluid/storage/PurifiedWaterPotionStorage.java b/src/main/java/net/dehydration/fluid/storage/PurifiedWaterPotionStorage.java new file mode 100644 index 0000000..cfe0888 --- /dev/null +++ b/src/main/java/net/dehydration/fluid/storage/PurifiedWaterPotionStorage.java @@ -0,0 +1,105 @@ +package net.dehydration.fluid.storage; + +import net.dehydration.init.FluidInit; +import net.dehydration.init.ItemInit; +import net.fabricmc.fabric.api.transfer.v1.context.ContainerItemContext; +import net.fabricmc.fabric.api.transfer.v1.fluid.FluidConstants; +import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant; +import net.fabricmc.fabric.api.transfer.v1.item.ItemVariant; +import net.fabricmc.fabric.api.transfer.v1.storage.StoragePreconditions; +import net.fabricmc.fabric.api.transfer.v1.storage.base.ExtractionOnlyStorage; +import net.fabricmc.fabric.api.transfer.v1.storage.base.SingleSlotStorage; +import net.fabricmc.fabric.api.transfer.v1.transaction.TransactionContext; +import net.minecraft.component.DataComponentTypes; +import net.minecraft.component.type.PotionContentsComponent; +import net.minecraft.item.ItemStack; +import net.minecraft.item.Items; +import net.minecraft.potion.Potion; +import net.minecraft.registry.entry.RegistryEntry; +import org.jetbrains.annotations.Nullable; + +import java.util.Optional; + +/** + * Implementation of the storage for a purified water potion. + */ +public class PurifiedWaterPotionStorage implements ExtractionOnlyStorage, SingleSlotStorage { + private static final FluidVariant CONTAINED_FLUID = FluidVariant.of(FluidInit.PURIFIED_WATER); + private static final long CONTAINED_AMOUNT = FluidConstants.BOTTLE; + + @Nullable + public static PurifiedWaterPotionStorage find(ContainerItemContext context) { + return isPurifiedWaterPotion(context) ? new PurifiedWaterPotionStorage(context) : null; + } + + private static boolean isPurifiedWaterPotion(ContainerItemContext context) { + ItemVariant variant = context.getItemVariant(); + Optional potionContents = variant.getComponents().get(DataComponentTypes.POTION_CONTENTS); + RegistryEntry potion = potionContents.map(PotionContentsComponent::potion).orElse(null).orElse(null); + return variant.isOf(Items.POTION) && potion == ItemInit.PURIFIED_WATER; + } + + private final ContainerItemContext context; + + private PurifiedWaterPotionStorage(ContainerItemContext context) { + this.context = context; + } + + private boolean isPurifiedWaterPotion() { + return isPurifiedWaterPotion(context); + } + + private ItemVariant mapToGlassBottle() { + ItemStack newStack = context.getItemVariant().toStack(); + newStack.set(DataComponentTypes.POTION_CONTENTS, PotionContentsComponent.DEFAULT); + return ItemVariant.of(Items.GLASS_BOTTLE, newStack.getComponentChanges()); + } + + @Override + public long extract(FluidVariant resource, long maxAmount, TransactionContext transaction) { + StoragePreconditions.notBlankNotNegative(resource, maxAmount); + + if (!isPurifiedWaterPotion()) return 0; + + if (resource.equals(CONTAINED_FLUID) && maxAmount >= CONTAINED_AMOUNT) { + if (context.exchange(mapToGlassBottle(), 1, transaction) == 1) { + return CONTAINED_AMOUNT; + } + } + + return 0; + } + + @Override + public boolean isResourceBlank() { + return getResource().isBlank(); + } + + @Override + public FluidVariant getResource() { + if (isPurifiedWaterPotion()) { + return CONTAINED_FLUID; + } else { + return FluidVariant.blank(); + } + } + + @Override + public long getAmount() { + if (isPurifiedWaterPotion()) { + return CONTAINED_AMOUNT; + } else { + return 0; + } + } + + @Override + public long getCapacity() { + return getAmount(); + } + + @Override + public String toString() { + return "PurifiedWaterPotionStorage[" + context + "]"; + } +} diff --git a/src/main/java/net/dehydration/init/BlockInit.java b/src/main/java/net/dehydration/init/BlockInit.java index 75fd378..904f6de 100644 --- a/src/main/java/net/dehydration/init/BlockInit.java +++ b/src/main/java/net/dehydration/init/BlockInit.java @@ -1,7 +1,12 @@ package net.dehydration.init; -import net.dehydration.block.*; -import net.dehydration.block.entity.*; +import net.dehydration.block.BambooPumpBlock; +import net.dehydration.block.CampfireCauldronBlock; +import net.dehydration.block.CopperCauldronBlock; +import net.dehydration.block.CopperLeveledCauldronBlock; +import net.dehydration.block.entity.BambooPumpEntity; +import net.dehydration.block.entity.CampfireCauldronEntity; +import net.dehydration.block.entity.CopperCauldronBehavior; import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents; import net.minecraft.block.AbstractBlock; import net.minecraft.block.Block; diff --git a/src/main/java/net/dehydration/init/FluidInit.java b/src/main/java/net/dehydration/init/FluidInit.java index 57f8888..3d26d91 100644 --- a/src/main/java/net/dehydration/init/FluidInit.java +++ b/src/main/java/net/dehydration/init/FluidInit.java @@ -1,15 +1,21 @@ package net.dehydration.init; +import net.dehydration.block.entity.CampfireCauldronEntity; import net.dehydration.fluid.PurifiedWaterFluid; -import net.dehydration.item.PurifiedBucket; +import net.dehydration.fluid.storage.CampfireCauldronFluidStorage; +import net.dehydration.fluid.storage.CopperCauldronFluidStorage; +import net.dehydration.fluid.storage.LeatherFlaskFluidStorage; +import net.dehydration.fluid.storage.PurifiedWaterPotionStorage; import net.fabricmc.fabric.api.transfer.v1.fluid.FluidConstants; import net.fabricmc.fabric.api.transfer.v1.fluid.FluidStorage; -import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant; import net.fabricmc.fabric.api.transfer.v1.fluid.base.EmptyItemFluidStorage; -import net.fabricmc.fabric.api.transfer.v1.fluid.base.FullItemFluidStorage; +import net.fabricmc.fabric.api.transfer.v1.item.ItemVariant; +import net.minecraft.component.DataComponentTypes; +import net.minecraft.component.type.PotionContentsComponent; import net.minecraft.fluid.FlowableFluid; import net.minecraft.fluid.Fluid; import net.minecraft.fluid.FluidState; +import net.minecraft.item.ItemStack; import net.minecraft.item.Items; import net.minecraft.registry.Registries; import net.minecraft.registry.Registry; @@ -20,17 +26,21 @@ public class FluidInit { public static final FlowableFluid PURIFIED_WATER = register("dehydration:purified_water", new PurifiedWaterFluid.Still()); public static void init() { - FluidStorage.GENERAL_COMBINED_PROVIDER.register(context -> { - if (context.getItemVariant().getItem() instanceof PurifiedBucket bucketItem) { - Fluid bucketFluid = FluidInit.PURIFIED_WATER; - if (bucketFluid != null && bucketFluid.getBucketItem() == bucketItem) { - return new FullItemFluidStorage(context, Items.BUCKET, FluidVariant.of(bucketFluid), FluidConstants.BUCKET); - } - } - return null; - }); - - FluidStorage.combinedItemApiProvider(Items.BUCKET).register(context -> new EmptyItemFluidStorage(context, ItemInit.PURIFIED_BUCKET, FluidInit.PURIFIED_WATER, FluidConstants.BUCKET)); + // BucketItem storages are added by Fabric + // Register empty bottle storage for purified water + FluidStorage.combinedItemApiProvider(Items.GLASS_BOTTLE).register(context -> new EmptyItemFluidStorage(context, emptyBottle -> { + ItemStack newStack = emptyBottle.toStack(); + newStack.set(DataComponentTypes.POTION_CONTENTS, new PotionContentsComponent(ItemInit.PURIFIED_WATER)); + return ItemVariant.of(Items.POTION, newStack.getComponentChanges()); + }, FluidInit.PURIFIED_WATER, FluidConstants.BOTTLE)); + // Register purified water potion storage + FluidStorage.combinedItemApiProvider(Items.POTION).register(PurifiedWaterPotionStorage::find); + // Register flask storage + FluidStorage.ITEM.registerForItems((itemStack, context) -> new LeatherFlaskFluidStorage(context), ItemInit.LEATHER_FLASK, ItemInit.IRON_LEATHER_FLASK, ItemInit.GOLDEN_LEATHER_FLASK, ItemInit.DIAMOND_LEATHER_FLASK, ItemInit.NETHERITE_LEATHER_FLASK); + // Register campfire cauldron storage + FluidStorage.SIDED.registerForBlocks((world, pos, state, blockEntity, context) -> new CampfireCauldronFluidStorage(world, pos, state, (CampfireCauldronEntity) blockEntity), BlockInit.CAMPFIRE_CAULDRON_BLOCK); + //register copper cauldron fluid storage + FluidStorage.SIDED.registerForBlocks((world, pos, state, be, context) -> new CopperCauldronFluidStorage(world, pos), BlockInit.COPPER_CAULDRON_BLOCK, BlockInit.COPPER_WATER_CAULDRON_BLOCK, BlockInit.COPPER_PURIFIED_WATER_CAULDRON_BLOCK); } private static T register(String id, T value) { diff --git a/src/main/java/net/dehydration/init/ItemInit.java b/src/main/java/net/dehydration/init/ItemInit.java index 04ac40f..ba77fe7 100644 --- a/src/main/java/net/dehydration/init/ItemInit.java +++ b/src/main/java/net/dehydration/init/ItemInit.java @@ -1,20 +1,17 @@ package net.dehydration.init; -import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.function.UnaryOperator; - +import net.dehydration.block.entity.BambooPumpEntity; import net.dehydration.item.HandbookItem; import net.dehydration.item.LeatherFlask; -import net.dehydration.item.PurifiedBucket; import net.dehydration.item.WaterBowlItem; import net.dehydration.item.component.FlaskComponent; +import net.dehydration.item.storage.BambooPumpItemStorage; import net.fabricmc.fabric.api.itemgroup.v1.FabricItemGroup; import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents; +import net.fabricmc.fabric.api.transfer.v1.item.ItemStorage; import net.minecraft.component.ComponentType; import net.minecraft.entity.effect.StatusEffectInstance; +import net.minecraft.item.BucketItem; import net.minecraft.item.Item; import net.minecraft.item.ItemGroup; import net.minecraft.item.ItemStack; @@ -30,6 +27,12 @@ import net.minecraft.util.Identifier; import net.minecraft.util.dynamic.Codecs; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.function.UnaryOperator; + public class ItemInit { // Item Group public static final RegistryKey DEHYDRATION_ITEM_GROUP = RegistryKey.of(RegistryKeys.ITEM_GROUP, Identifier.of("dehydration", "item_group")); @@ -51,7 +54,7 @@ public class ItemInit { // Handbook public static final Item HANDBOOK = register("handbook", new HandbookItem(new Item.Settings())); // Bucket - public static final Item PURIFIED_BUCKET = register("purified_water_bucket", new PurifiedBucket(new Item.Settings().recipeRemainder(Items.BUCKET).maxCount(1))); + public static final Item PURIFIED_BUCKET = register("purified_water_bucket", new BucketItem(FluidInit.PURIFIED_WATER, new Item.Settings().recipeRemainder(Items.BUCKET).maxCount(1))); // Bowl public static final Item WATER_BOWL = register("water_bowl", new WaterBowlItem(new Item.Settings().maxCount(1), true)); public static final Item PURIFIED_WATER_BOWL = register("purified_water_bowl", new WaterBowlItem(new Item.Settings().maxCount(1), true)); @@ -79,6 +82,8 @@ public static void init() { for (Identifier id : ITEMS.keySet()) { Registry.register(Registries.ITEM, id, ITEMS.get(id)); } + //register item storage for bamboo pump + ItemStorage.SIDED.registerForBlockEntities((blockEntity, context) -> new BambooPumpItemStorage((BambooPumpEntity) blockEntity), BlockInit.BAMBOO_PUMP_ENTITY); } } diff --git a/src/main/java/net/dehydration/item/PurifiedBucket.java b/src/main/java/net/dehydration/item/PurifiedBucket.java deleted file mode 100644 index e89ee03..0000000 --- a/src/main/java/net/dehydration/item/PurifiedBucket.java +++ /dev/null @@ -1,116 +0,0 @@ -package net.dehydration.item; - -import org.jetbrains.annotations.Nullable; - -import net.dehydration.init.FluidInit; -import net.minecraft.advancement.criterion.Criteria; -import net.minecraft.block.Block; -import net.minecraft.block.BlockState; -import net.minecraft.block.FluidFillable; -import net.minecraft.entity.Entity; -import net.minecraft.entity.player.PlayerEntity; -import net.minecraft.item.BucketItem; -import net.minecraft.item.FluidModificationItem; -import net.minecraft.item.Item; -import net.minecraft.item.ItemStack; -import net.minecraft.particle.ParticleTypes; -import net.minecraft.server.network.ServerPlayerEntity; -import net.minecraft.sound.SoundCategory; -import net.minecraft.sound.SoundEvents; -import net.minecraft.stat.Stats; -import net.minecraft.util.Hand; -import net.minecraft.util.TypedActionResult; -import net.minecraft.util.hit.BlockHitResult; -import net.minecraft.util.hit.HitResult; -import net.minecraft.util.math.BlockPos; -import net.minecraft.util.math.Direction; -import net.minecraft.world.RaycastContext; -import net.minecraft.world.World; -import net.minecraft.world.WorldAccess; -import net.minecraft.world.event.GameEvent; - -public class PurifiedBucket extends Item implements FluidModificationItem { - - public PurifiedBucket(Settings settings) { - super(settings); - } - - @Override - public TypedActionResult use(World world, PlayerEntity user, Hand hand) { - ItemStack itemStack = user.getStackInHand(hand); - BlockHitResult blockHitResult = BucketItem.raycast(world, user, RaycastContext.FluidHandling.NONE); - if (blockHitResult.getType() == HitResult.Type.MISS) { - return TypedActionResult.pass(itemStack); - } - if (blockHitResult.getType() == HitResult.Type.BLOCK) { - BlockPos blockPos3; - BlockPos blockPos = blockHitResult.getBlockPos(); - Direction direction = blockHitResult.getSide(); - BlockPos blockPos2 = blockPos.offset(direction); - if (!world.canPlayerModifyAt(user, blockPos) || !user.canPlaceOn(blockPos2, direction, itemStack)) { - return TypedActionResult.fail(itemStack); - } - - BlockState blockState = world.getBlockState(blockPos); - blockPos3 = blockState.getBlock() instanceof FluidFillable ? blockPos : blockPos2; - if (this.placeFluid(user, world, blockPos3, blockHitResult)) { - this.onEmptied(user, world, itemStack, blockPos3); - if (user instanceof ServerPlayerEntity serverPlayerEntity) { - Criteria.PLACED_BLOCK.trigger(serverPlayerEntity, blockPos3, itemStack); - } - user.incrementStat(Stats.USED.getOrCreateStat(this)); - return TypedActionResult.success(BucketItem.getEmptiedStack(itemStack, user), world.isClient()); - } - return TypedActionResult.fail(itemStack); - } - return TypedActionResult.pass(itemStack); - } - - @Override - public void onEmptied(PlayerEntity player, World world, ItemStack stack, BlockPos pos) { - } - - @SuppressWarnings("deprecation") - @Override - public boolean placeFluid(@Nullable PlayerEntity player, World world, BlockPos pos, @Nullable BlockHitResult hitResult) { - boolean bl2; - - BlockState blockState = world.getBlockState(pos); - Block block = blockState.getBlock(); - boolean bl = blockState.canBucketPlace(FluidInit.PURIFIED_WATER); - bl2 = blockState.isAir() || bl || block instanceof FluidFillable fluidFillable && fluidFillable.canFillWithFluid(player, world, pos, blockState, FluidInit.PURIFIED_WATER); - if (!bl2) { - return hitResult != null && this.placeFluid(player, world, hitResult.getBlockPos().offset(hitResult.getSide()), null); - } - if (world.getDimension().ultrawarm()) { - int i = pos.getX(); - int j = pos.getY(); - int k = pos.getZ(); - world.playSound(player, pos, SoundEvents.BLOCK_FIRE_EXTINGUISH, SoundCategory.BLOCKS, 0.5f, 2.6f + (world.random.nextFloat() - world.random.nextFloat()) * 0.8f); - for (int l = 0; l < 8; ++l) { - world.addParticle(ParticleTypes.LARGE_SMOKE, (double) i + Math.random(), (double) j + Math.random(), (double) k + Math.random(), 0.0, 0.0, 0.0); - } - return true; - } - if (block instanceof FluidFillable fluidFillable) { - fluidFillable.tryFillWithFluid(world, pos, blockState, FluidInit.PURIFIED_WATER.getStill(false)); - - this.playEmptyingSound(player, world, pos); - return true; - } - if (!world.isClient() && bl && !blockState.isLiquid()) { - world.breakBlock(pos, true); - } - if (world.setBlockState(pos, FluidInit.PURIFIED_WATER.getDefaultState().getBlockState(), Block.NOTIFY_ALL | Block.REDRAW_ON_MAIN_THREAD) || blockState.getFluidState().isStill()) { - this.playEmptyingSound(player, world, pos); - return true; - } - return false; - } - - private void playEmptyingSound(@Nullable PlayerEntity player, WorldAccess world, BlockPos pos) { - world.playSound(player, pos, SoundEvents.ITEM_BUCKET_EMPTY, SoundCategory.BLOCKS, 1.0f, 1.0f); - world.emitGameEvent((Entity) player, GameEvent.FLUID_PLACE, pos); - } - -} diff --git a/src/main/java/net/dehydration/item/storage/BambooPumpItemStorage.java b/src/main/java/net/dehydration/item/storage/BambooPumpItemStorage.java new file mode 100644 index 0000000..7b3a3c7 --- /dev/null +++ b/src/main/java/net/dehydration/item/storage/BambooPumpItemStorage.java @@ -0,0 +1,42 @@ +package net.dehydration.item.storage; + +import net.dehydration.block.entity.BambooPumpEntity; +import net.fabricmc.fabric.api.transfer.v1.context.ContainerItemContext; +import net.fabricmc.fabric.api.transfer.v1.fluid.FluidStorage; +import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant; +import net.fabricmc.fabric.api.transfer.v1.item.ItemVariant; +import net.fabricmc.fabric.api.transfer.v1.item.base.SingleStackStorage; +import net.fabricmc.fabric.api.transfer.v1.storage.Storage; +import net.minecraft.item.ItemStack; + +public class BambooPumpItemStorage extends SingleStackStorage { + + private final BambooPumpEntity pumpEntity; + + public BambooPumpItemStorage(BambooPumpEntity pumpEntity) { + this.pumpEntity = pumpEntity; + } + + @Override + protected int getCapacity(ItemVariant itemVariant) { + return 1; + } + + @Override + protected ItemStack getStack() { + return pumpEntity.getStack(0); + } + + @Override + protected void setStack(ItemStack stack) { + pumpEntity.setStack(0, stack); + } + + @Override + protected boolean canInsert(ItemVariant variant) { + //only items with a fluid storage are allowed + Storage storage = ContainerItemContext.withConstant(variant, 1).find(FluidStorage.ITEM); + return storage != null; + } + +} diff --git a/src/main/java/net/dehydration/mixin/PotionContentsComponentMixin.java b/src/main/java/net/dehydration/mixin/PotionContentsComponentMixin.java index b3d6920..8492b15 100644 --- a/src/main/java/net/dehydration/mixin/PotionContentsComponentMixin.java +++ b/src/main/java/net/dehydration/mixin/PotionContentsComponentMixin.java @@ -24,9 +24,10 @@ public Optional> potion() { @SuppressWarnings("deprecation") @Inject(method = "Lnet/minecraft/component/type/PotionContentsComponent;getColor()I", at = @At("HEAD"), cancellable = true) private void getColorMixin(CallbackInfoReturnable info) { - if (this.potion().get().matches(ItemInit.PURIFIED_WATER)) { + if(this.potion() == null) + return; + if (this.potion().get().matches(ItemInit.PURIFIED_WATER)) info.setReturnValue(3708358); - } } } diff --git a/src/main/java/net/dehydration/thirst/ThirstHudRender.java b/src/main/java/net/dehydration/thirst/ThirstHudRender.java index fd16fa3..ba773e0 100644 --- a/src/main/java/net/dehydration/thirst/ThirstHudRender.java +++ b/src/main/java/net/dehydration/thirst/ThirstHudRender.java @@ -27,7 +27,7 @@ public class ThirstHudRender { // Could implement HudRenderCallback public static void renderThirstHud(DrawContext context, MinecraftClient client, PlayerEntity playerEntity, int scaledWidth, int scaledHeight, int ticks) { - if (playerEntity != null && !playerEntity.isInvulnerable() && !playerEntity.isCreative() && !client.options.hudHidden) { + if (playerEntity != null && !playerEntity.isInvulnerable() && !playerEntity.isCreative() && !playerEntity.isSpectator() && !client.options.hudHidden) { ThirstManager thirstManager = ((ThirstManagerAccess) playerEntity).getThirstManager(); if (thirstManager.hasThirst()) { int thirst = thirstManager.getThirstLevel(); diff --git a/src/main/resources/assets/dehydration/lang/en_us.json b/src/main/resources/assets/dehydration/lang/en_us.json index 887372e..8e3a816 100644 --- a/src/main/resources/assets/dehydration/lang/en_us.json +++ b/src/main/resources/assets/dehydration/lang/en_us.json @@ -77,6 +77,7 @@ "block.dehydration.campfire_cauldron": "Campfire Cauldron", "block.dehydration.copper_cauldron": "Copper Cauldron", "block.dehydration.water_copper_cauldron": "Water Copper Cauldron", + "block.dehydration.purified_water": "Purified Water", "block.dehydration.purified_water_copper_cauldron": "Purified Water Copper Cauldron", "block.dehydration.powder_snow_copper_cauldron": "Powder Snow Copper Cauldron", "block.dehydration.bamboo_pump": "Bamboo Pump", @@ -84,4 +85,4 @@ "block.dehydration.bamboo_pump.no_water": "Pump found no water", "commands.dehydration.changed": "thirst got changed" -} \ No newline at end of file +} diff --git a/src/main/resources/assets/dehydration/lang/es_mx.json b/src/main/resources/assets/dehydration/lang/es_mx.json index 1d81c71..525b676 100644 --- a/src/main/resources/assets/dehydration/lang/es_mx.json +++ b/src/main/resources/assets/dehydration/lang/es_mx.json @@ -73,6 +73,7 @@ "block.dehydration.campfire_cauldron": "Caldero de fogata", "block.dehydration.copper_cauldron": "Caldero de cobre", "block.dehydration.water_copper_cauldron": "Caldero de cobre con agua", + "block.dehydration.purified_water": "Agua purificada", "block.dehydration.purified_water_copper_cauldron": "Caldero de cobre con agua purificada", "block.dehydration.powder_snow_copper_cauldron": "Caldero de cobre con nieve en polvo ", "block.dehydration.bamboo_pump": "Bomba de agua de bambú", diff --git a/src/main/resources/assets/dehydration/lang/pt_br.json b/src/main/resources/assets/dehydration/lang/pt_br.json index 922a38c..d5371d0 100644 --- a/src/main/resources/assets/dehydration/lang/pt_br.json +++ b/src/main/resources/assets/dehydration/lang/pt_br.json @@ -62,6 +62,7 @@ "block.dehydration.campfire_cauldron": "Caldeirão de Fogueira", "block.dehydration.copper_cauldron": "Caldeirão de Cobre", "block.dehydration.water_copper_cauldron": "Caldeirão De Cobre De Água", + "block.dehydration.purified_water": "Água purificada", "block.dehydration.purified_water_copper_cauldron": "Caldeirão de cobre de água purificada", "block.dehydration.powder_snow_copper_cauldron": "Caldeirão de cobre em pó de neve", "block.dehydration.bamboo_pump": "Bomba de bambu", @@ -69,4 +70,4 @@ "block.dehydration.bamboo_pump.no_water": "A bomba não encontrou água", "commands.dehydration.changed": "A sede mudou" -} \ No newline at end of file +} diff --git a/src/main/resources/assets/dehydration/lang/zh_cn.json b/src/main/resources/assets/dehydration/lang/zh_cn.json index 106a67a..9478897 100644 --- a/src/main/resources/assets/dehydration/lang/zh_cn.json +++ b/src/main/resources/assets/dehydration/lang/zh_cn.json @@ -77,6 +77,7 @@ "block.dehydration.campfire_cauldron": "篝火大锅", "block.dehydration.copper_cauldron": "铜锅", "block.dehydration.water_copper_cauldron": "装水的铜锅", + "block.dehydration.purified_water": "净化水", "block.dehydration.purified_water_copper_cauldron": "装纯净水的铜锅", "block.dehydration.powder_snow_copper_cauldron": "装细雪的铜锅", "block.dehydration.bamboo_pump": "竹泵", @@ -84,4 +85,4 @@ "block.dehydration.bamboo_pump.no_water": "泵不到水", "commands.dehydration.changed": "水分值已改变" -} \ No newline at end of file +} diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index c581751..d85c745 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -46,4 +46,4 @@ "curseforge": 410830 } } -} \ No newline at end of file +}