From a115fbd736388a1062e1ed0b43645b5c59533607 Mon Sep 17 00:00:00 2001 From: drcrazy Date: Fri, 10 Jan 2020 16:09:03 +0300 Subject: [PATCH] Dispenser can use cells. Closes #1948 --- .../java/techreborn/TechRebornClient.java | 4 +- .../IndustrialElectrolyzerBlockEntity.java | 6 +- .../IndustrialCentrifugeBlockEntity.java | 6 +- .../techreborn/compat/libcd/LibCDPlugin.java | 4 +- .../java/techreborn/events/ModRegistry.java | 4 +- src/main/java/techreborn/init/TRContent.java | 4 +- .../techreborn/init/TRDispenserBehavior.java | 62 +++++++- ...mDynamicCell.java => DynamicCellItem.java} | 144 +++++++++++------- .../java/techreborn/utils/RecipeUtils.java | 12 +- 9 files changed, 165 insertions(+), 81 deletions(-) rename src/main/java/techreborn/items/{ItemDynamicCell.java => DynamicCellItem.java} (76%) diff --git a/src/main/java/techreborn/TechRebornClient.java b/src/main/java/techreborn/TechRebornClient.java index 142124646..21236aa6a 100644 --- a/src/main/java/techreborn/TechRebornClient.java +++ b/src/main/java/techreborn/TechRebornClient.java @@ -52,7 +52,7 @@ import techreborn.events.StackToolTipHandler; import techreborn.init.ModFluids; import techreborn.init.TRBlockEntities; import techreborn.init.TRContent; -import techreborn.items.ItemDynamicCell; +import techreborn.items.DynamicCellItem; import techreborn.items.ItemFrequencyTransmitter; import javax.annotation.Nullable; @@ -127,7 +127,7 @@ public class TechRebornClient implements ClientModInitializer { StackToolTipHandler.setup(); GuiBase.wrenchStack = new ItemStack(TRContent.WRENCH); - GuiBase.fluidCellProvider = ItemDynamicCell::getCellWithFluid; + GuiBase.fluidCellProvider = DynamicCellItem::getCellWithFluid; StackInfoHUD.registerElement(new ItemFrequencyTransmitter.StackInfoFreqTransmitter()); diff --git a/src/main/java/techreborn/blockentity/machine/tier1/IndustrialElectrolyzerBlockEntity.java b/src/main/java/techreborn/blockentity/machine/tier1/IndustrialElectrolyzerBlockEntity.java index 5811a6ae9..911e748a7 100644 --- a/src/main/java/techreborn/blockentity/machine/tier1/IndustrialElectrolyzerBlockEntity.java +++ b/src/main/java/techreborn/blockentity/machine/tier1/IndustrialElectrolyzerBlockEntity.java @@ -35,7 +35,7 @@ import techreborn.config.TechRebornConfig; import techreborn.init.ModRecipes; import techreborn.init.TRContent; import techreborn.init.TRBlockEntities; -import techreborn.items.ItemDynamicCell; +import techreborn.items.DynamicCellItem; import techreborn.blockentity.machine.GenericMachineBlockEntity; public class IndustrialElectrolyzerBlockEntity extends GenericMachineBlockEntity implements IContainerProvider { @@ -53,8 +53,8 @@ public class IndustrialElectrolyzerBlockEntity extends GenericMachineBlockEntity public BuiltContainer createContainer(int syncID, final PlayerEntity player) { return new ContainerBuilder("industrialelectrolyzer").player(player.inventory).inventory().hotbar() .addInventory().blockEntity(this) - .filterSlot(1, 47, 72, stack -> ItemUtils.isItemEqual(stack, ItemDynamicCell.getEmptyCell(1), true, true)) - .filterSlot(0, 81, 72, stack -> !ItemUtils.isItemEqual(stack, ItemDynamicCell.getEmptyCell(1), true, true)) + .filterSlot(1, 47, 72, stack -> ItemUtils.isItemEqual(stack, DynamicCellItem.getEmptyCell(1), true, true)) + .filterSlot(0, 81, 72, stack -> !ItemUtils.isItemEqual(stack, DynamicCellItem.getEmptyCell(1), true, true)) .outputSlot(2, 51, 24).outputSlot(3, 71, 24).outputSlot(4, 91, 24).outputSlot(5, 111, 24) .energySlot(6, 8, 72).syncEnergyValue().syncCrafterValue().addInventory().create(this, syncID); } diff --git a/src/main/java/techreborn/blockentity/machine/tier3/IndustrialCentrifugeBlockEntity.java b/src/main/java/techreborn/blockentity/machine/tier3/IndustrialCentrifugeBlockEntity.java index 7e161ce4c..37b947c9e 100644 --- a/src/main/java/techreborn/blockentity/machine/tier3/IndustrialCentrifugeBlockEntity.java +++ b/src/main/java/techreborn/blockentity/machine/tier3/IndustrialCentrifugeBlockEntity.java @@ -40,7 +40,7 @@ import techreborn.config.TechRebornConfig; import techreborn.init.ModRecipes; import techreborn.init.TRContent; import techreborn.init.TRBlockEntities; -import techreborn.items.ItemDynamicCell; +import techreborn.items.DynamicCellItem; import java.util.List; @@ -59,8 +59,8 @@ public class IndustrialCentrifugeBlockEntity extends GenericMachineBlockEntity i public BuiltContainer createContainer(int syncID, final PlayerEntity player) { return new ContainerBuilder("centrifuge").player(player.inventory).inventory().hotbar() .addInventory().blockEntity(this) - .filterSlot(1, 40, 54, stack -> ItemUtils.isItemEqual(stack, ItemDynamicCell.getEmptyCell(1), true, true)) - .filterSlot(0, 40, 34, stack -> !ItemUtils.isItemEqual(stack, ItemDynamicCell.getEmptyCell(1), true, true)) + .filterSlot(1, 40, 54, stack -> ItemUtils.isItemEqual(stack, DynamicCellItem.getEmptyCell(1), true, true)) + .filterSlot(0, 40, 34, stack -> !ItemUtils.isItemEqual(stack, DynamicCellItem.getEmptyCell(1), true, true)) .outputSlot(2, 82, 44).outputSlot(3, 101, 25) .outputSlot(4, 120, 44).outputSlot(5, 101, 63).energySlot(6, 8, 72).syncEnergyValue() .syncCrafterValue().addInventory().create(this, syncID); diff --git a/src/main/java/techreborn/compat/libcd/LibCDPlugin.java b/src/main/java/techreborn/compat/libcd/LibCDPlugin.java index 876b0014a..96aedbab6 100644 --- a/src/main/java/techreborn/compat/libcd/LibCDPlugin.java +++ b/src/main/java/techreborn/compat/libcd/LibCDPlugin.java @@ -30,13 +30,13 @@ import io.github.cottonmc.libcd.api.tweaker.TweakerManager; import net.minecraft.util.Identifier; import net.minecraft.util.registry.Registry; import techreborn.TechReborn; -import techreborn.items.ItemDynamicCell; +import techreborn.items.DynamicCellItem; public class LibCDPlugin implements LibCDInitializer { @Override public void initTweakers(TweakerManager manager) { manager.addTweaker("techreborn.TRTweaker", TRTweaker.INSTANCE); - manager.addStackFactory(new Identifier(TechReborn.MOD_ID, "cell"), (id) -> ItemDynamicCell.getCellWithFluid(Registry.FLUID.get(id))); + manager.addStackFactory(new Identifier(TechReborn.MOD_ID, "cell"), (id) -> DynamicCellItem.getCellWithFluid(Registry.FLUID.get(id))); } @Override diff --git a/src/main/java/techreborn/events/ModRegistry.java b/src/main/java/techreborn/events/ModRegistry.java index a0b308bb9..fcbb4a828 100644 --- a/src/main/java/techreborn/events/ModRegistry.java +++ b/src/main/java/techreborn/events/ModRegistry.java @@ -54,7 +54,7 @@ import techreborn.init.TRArmorMaterial; import techreborn.init.TRContent; import techreborn.init.TRContent.*; import techreborn.init.TRToolTier; -import techreborn.items.ItemDynamicCell; +import techreborn.items.DynamicCellItem; import techreborn.items.ItemFrequencyTransmitter; import techreborn.items.ItemManual; import techreborn.items.ItemScrapBox; @@ -231,7 +231,7 @@ public class ModRegistry { RebornRegistry.registerItem(TRContent.SCRAP_BOX = InitUtils.setup(new ItemScrapBox(), "scrap_box")); RebornRegistry.registerItem(TRContent.MANUAL = InitUtils.setup(new ItemManual(), "manual")); RebornRegistry.registerItem(TRContent.DEBUG_TOOL = InitUtils.setup(new DebugToolItem(), "debug_tool")); - RebornRegistry.registerItem(TRContent.CELL = InitUtils.setup(new ItemDynamicCell(), "cell")); + RebornRegistry.registerItem(TRContent.CELL = InitUtils.setup(new DynamicCellItem(), "cell")); TechReborn.LOGGER.debug("TechReborns Items Loaded"); } diff --git a/src/main/java/techreborn/init/TRContent.java b/src/main/java/techreborn/init/TRContent.java index 5bd84c0a7..1a1613a4f 100644 --- a/src/main/java/techreborn/init/TRContent.java +++ b/src/main/java/techreborn/init/TRContent.java @@ -78,7 +78,7 @@ import techreborn.blocks.transformers.BlockMVTransformer; import techreborn.client.EGui; import techreborn.config.TechRebornConfig; import techreborn.entities.EntityNukePrimed; -import techreborn.items.ItemDynamicCell; +import techreborn.items.DynamicCellItem; import techreborn.items.ItemUpgrade; import techreborn.items.armor.ItemQuantumSuit; import techreborn.utils.InitUtils; @@ -147,7 +147,7 @@ public class TRContent { public static Item FREQUENCY_TRANSMITTER; public static Item SCRAP_BOX; public static Item MANUAL; - public static ItemDynamicCell CELL; + public static DynamicCellItem CELL; //Quantum Suit public static ItemQuantumSuit QUANTUM_HELMET; diff --git a/src/main/java/techreborn/init/TRDispenserBehavior.java b/src/main/java/techreborn/init/TRDispenserBehavior.java index dd8982ae6..be5f72262 100644 --- a/src/main/java/techreborn/init/TRDispenserBehavior.java +++ b/src/main/java/techreborn/init/TRDispenserBehavior.java @@ -1,13 +1,23 @@ package techreborn.init; +import net.minecraft.block.Block; +import net.minecraft.block.BlockState; import net.minecraft.block.DispenserBlock; +import net.minecraft.block.FluidDrainable; import net.minecraft.block.dispenser.ItemDispenserBehavior; +import net.minecraft.block.entity.DispenserBlockEntity; +import net.minecraft.fluid.BaseFluid; +import net.minecraft.fluid.Fluid; +import net.minecraft.fluid.Fluids; import net.minecraft.item.ItemStack; import net.minecraft.util.math.BlockPointer; +import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Direction; import net.minecraft.util.math.Position; +import net.minecraft.world.IWorld; import reborncore.common.crafting.RebornRecipe; import techreborn.config.TechRebornConfig; +import techreborn.items.DynamicCellItem; import java.util.List; import java.util.Random; @@ -34,9 +44,53 @@ public class TRDispenserBehavior { } ); } -// DispenserBlock.registerBehavior(TRContent.CELL, new ItemDispenserBehavior(){ -// -// }); - + DispenserBlock.registerBehavior(TRContent.CELL, new ItemDispenserBehavior(){ + public ItemStack dispenseSilently(BlockPointer pointer, ItemStack stack) { + DynamicCellItem cell = (DynamicCellItem)stack.getItem(); + IWorld iWorld = pointer.getWorld(); + BlockPos blockPos = pointer.getBlockPos().offset(pointer.getBlockState().get(DispenserBlock.FACING)); + BlockState blockState = iWorld.getBlockState(blockPos); + Block block = blockState.getBlock(); + if (cell.getFluid(stack) == Fluids.EMPTY){ + // fill cell + if (block instanceof FluidDrainable) { + Fluid fluid = ((FluidDrainable)block).tryDrainFluid(iWorld, blockPos, blockState); + if (!(fluid instanceof BaseFluid)) { + return super.dispenseSilently(pointer, stack); + } else { + ItemStack filledCell = DynamicCellItem.getCellWithFluid(fluid, 1); + if (stack.getCount() == 1) { + stack = filledCell; + } else { + stack.decrement(1); + if (((DispenserBlockEntity)pointer.getBlockEntity()).addToFirstFreeSlot(filledCell) < 0) { + this.dispense(pointer, filledCell); + } + } + return stack; + } + } else { + return super.dispenseSilently(pointer, stack); + } + } + else { + // drain cell + if (cell.placeFluid(null, pointer.getWorld(), blockPos, null, stack)) { + ItemStack emptyCell = cell.getEmpty(); + if(stack.getCount() == 1) { + stack = emptyCell; + } else { + stack.decrement(1); + if (((DispenserBlockEntity)pointer.getBlockEntity()).addToFirstFreeSlot(emptyCell) < 0) { + this.dispense(pointer, emptyCell); + } + } + return stack; + } else { + return this.dispense(pointer, stack); + } + } + } + }); } } diff --git a/src/main/java/techreborn/items/ItemDynamicCell.java b/src/main/java/techreborn/items/DynamicCellItem.java similarity index 76% rename from src/main/java/techreborn/items/ItemDynamicCell.java rename to src/main/java/techreborn/items/DynamicCellItem.java index 92b688fda..b57ec97b4 100644 --- a/src/main/java/techreborn/items/ItemDynamicCell.java +++ b/src/main/java/techreborn/items/DynamicCellItem.java @@ -26,7 +26,10 @@ package techreborn.items; import net.minecraft.block.BlockState; import net.minecraft.block.FluidDrainable; +import net.minecraft.block.FluidFillable; +import net.minecraft.block.Material; import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.fluid.BaseFluid; import net.minecraft.fluid.Fluid; import net.minecraft.fluid.Fluids; import net.minecraft.item.Item; @@ -66,12 +69,78 @@ import javax.annotation.Nullable; /** * Created by modmuss50 on 17/05/2016. */ -public class ItemDynamicCell extends Item implements ItemFluidInfo { +public class DynamicCellItem extends Item implements ItemFluidInfo { - public ItemDynamicCell() { + public DynamicCellItem() { super( new Item.Settings().group(TechReborn.ITEMGROUP).maxCount(16)); } + // Thanks vanilla :) + private void playEmptyingSound(@Nullable PlayerEntity playerEntity, IWorld world, BlockPos blockPos, Fluid fluid) { + SoundEvent soundEvent = fluid.matches(FluidTags.LAVA) ? SoundEvents.ITEM_BUCKET_EMPTY_LAVA : SoundEvents.ITEM_BUCKET_EMPTY; + world.playSound(playerEntity, blockPos, soundEvent, SoundCategory.BLOCKS, 1.0F, 1.0F); + } + + public static ItemStack getCellWithFluid(Fluid fluid, int stackSize) { + Validate.notNull(fluid); + ItemStack stack = new ItemStack(TRContent.CELL); + ItemNBTHelper.getNBT(stack).putString("fluid", Registry.FLUID.getId(fluid).toString()); + stack.setCount(stackSize); + return stack; + } + + public static ItemStack getCellWithFluid(Fluid fluid) { + return getCellWithFluid(fluid, 1); + } + + public static ItemStack getEmptyCell(int amount) { + return new ItemStack(TRContent.CELL, amount); + } + + private void insertOrDropStack(PlayerEntity playerEntity, ItemStack stack){ + if(!playerEntity.inventory.insertStack(stack)){ + playerEntity.dropStack(stack); + } + } + + public boolean placeFluid(@Nullable PlayerEntity player, World world, BlockPos pos, @Nullable BlockHitResult hitResult, ItemStack filledCell){ + Fluid fluid = getFluid(filledCell); + if (fluid == Fluids.EMPTY) { + return false; + } + + BlockState blockState = world.getBlockState(pos); + Material material = blockState.getMaterial(); + boolean canPlace = blockState.canBucketPlace(fluid); + + if (!blockState.isAir() && !canPlace && (!(blockState.getBlock() instanceof FluidFillable) || !((FluidFillable)blockState.getBlock()).canFillWithFluid(world, pos, blockState, fluid))) { + return hitResult != null && this.placeFluid(player, world, hitResult.getBlockPos().offset(hitResult.getSide()), null, filledCell); + } else { + if (world.dimension.doesWaterVaporize() && fluid.matches(FluidTags.WATER)) { + 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.0D, 0.0D, 0.0D); + } + } else if (blockState.getBlock() instanceof FluidFillable && fluid == Fluids.WATER) { + if (((FluidFillable)blockState.getBlock()).tryFillWithFluid(world, pos, blockState, ((BaseFluid)fluid).getStill(false))) { + this.playEmptyingSound(player, world, pos, fluid); + } + } else { + if (!world.isClient && canPlace && !material.isLiquid()) { + world.breakBlock(pos, true); + } + + this.playEmptyingSound(player, world, pos, fluid); + world.setBlockState(pos, fluid.getDefaultState().getBlockState(), 11); + } + return true; + } + } + @Override public void appendStacks(ItemGroup tab, DefaultedList subItems) { if (!isIn(tab)) { @@ -95,43 +164,6 @@ public class ItemDynamicCell extends Item implements ItemFluidInfo { return super.getName(itemStack); } - - - public static ItemStack getCellWithFluid(Fluid fluid, int stackSize) { - Validate.notNull(fluid); - ItemStack stack = new ItemStack(TRContent.CELL); - ItemNBTHelper.getNBT(stack).putString("fluid", Registry.FLUID.getId(fluid).toString()); - stack.setCount(stackSize); - return stack; - } - - public static ItemStack getEmptyCell(int amount) { - return new ItemStack(TRContent.CELL, amount); - } - - public static ItemStack getCellWithFluid(Fluid fluid) { - return getCellWithFluid(fluid, 1); - } - - @Override - public ItemStack getEmpty() { - return new ItemStack(this); - } - - @Override - public ItemStack getFull(Fluid fluid) { - return getCellWithFluid(fluid); - } - - @Override - public Fluid getFluid(ItemStack itemStack) { - CompoundTag tag = itemStack.getTag(); - if(tag != null && tag.contains("fluid")){ - return Registry.FLUID.get(new Identifier(tag.getString("fluid"))); - } - return Fluids.EMPTY; - } - @Override public TypedActionResult use(World world, PlayerEntity player, Hand hand) { ItemStack stack = player.getStackInHand(hand); @@ -170,15 +202,7 @@ public class ItemDynamicCell extends Item implements ItemFluidInfo { } else { BlockState placeState = world.getBlockState(placePos); if(placeState.canBucketPlace(containedFluid)){ - if (world.dimension.doesWaterVaporize() && containedFluid.matches(FluidTags.WATER)) { - world.playSound(player, placePos, 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)placePos.getX() + Math.random(), (double)placePos.getY() + Math.random(), (double)placePos.getZ() + Math.random(), 0.0D, 0.0D, 0.0D); - } - } else { - world.setBlockState(placePos, containedFluid.getDefaultState().getBlockState()); - playEmptyingSound(player, world, placePos, containedFluid); - } + placeFluid(player, world, placePos, blockHitResult, stack); if(stack.getCount() == 1) { stack = getEmpty(); @@ -195,15 +219,23 @@ public class ItemDynamicCell extends Item implements ItemFluidInfo { return TypedActionResult.fail(stack); } - private void insertOrDropStack(PlayerEntity playerEntity, ItemStack stack){ - if(!playerEntity.inventory.insertStack(stack)){ - playerEntity.dropStack(stack); - } + // ItemFluidInfo + @Override + public ItemStack getEmpty() { + return new ItemStack(this); } - // Thanks vanilla :) - private void playEmptyingSound(@Nullable PlayerEntity playerEntity, IWorld world, BlockPos blockPos, Fluid fluid) { - SoundEvent soundEvent = fluid.matches(FluidTags.LAVA) ? SoundEvents.ITEM_BUCKET_EMPTY_LAVA : SoundEvents.ITEM_BUCKET_EMPTY; - world.playSound(playerEntity, blockPos, soundEvent, SoundCategory.BLOCKS, 1.0F, 1.0F); + @Override + public ItemStack getFull(Fluid fluid) { + return getCellWithFluid(fluid); + } + + @Override + public Fluid getFluid(ItemStack itemStack) { + CompoundTag tag = itemStack.getTag(); + if(tag != null && tag.contains("fluid")){ + return Registry.FLUID.get(new Identifier(tag.getString("fluid"))); + } + return Fluids.EMPTY; } } diff --git a/src/main/java/techreborn/utils/RecipeUtils.java b/src/main/java/techreborn/utils/RecipeUtils.java index add6ce722..0a28d3664 100644 --- a/src/main/java/techreborn/utils/RecipeUtils.java +++ b/src/main/java/techreborn/utils/RecipeUtils.java @@ -25,21 +25,19 @@ package techreborn.utils; import net.minecraft.item.ItemStack; -import net.minecraft.recipe.Ingredient; import net.minecraft.recipe.Recipe; import net.minecraft.recipe.RecipeType; import net.minecraft.world.World; -import techreborn.items.ItemDynamicCell; - -import java.util.List; -import java.util.stream.Collectors; +import techreborn.items.DynamicCellItem; import javax.annotation.Nonnull; +import java.util.List; +import java.util.stream.Collectors; public class RecipeUtils { @Nonnull public static ItemStack getEmptyCell(int stackSize) { - return ItemDynamicCell.getEmptyCell(stackSize); + return DynamicCellItem.getEmptyCell(stackSize); } /** @@ -49,7 +47,7 @@ public class RecipeUtils { */ public static > ItemStack getMatchingRecipes(World world, RecipeType type, ItemStack input){ return getRecipes(world, type).stream() - .filter(recipe -> recipe.getPreviewInputs().size() == 1 && ((Ingredient)recipe.getPreviewInputs().get(0)).test(input)) + .filter(recipe -> recipe.getPreviewInputs().size() == 1 && recipe.getPreviewInputs().get(0).test(input)) .map(Recipe::getOutput) .findFirst() .orElse(ItemStack.EMPTY);