Dispenser can use cells. Closes #1948

This commit is contained in:
drcrazy 2020-01-10 16:09:03 +03:00
parent 8db5cea07c
commit a115fbd736
9 changed files with 165 additions and 81 deletions

View file

@ -52,7 +52,7 @@ import techreborn.events.StackToolTipHandler;
import techreborn.init.ModFluids; import techreborn.init.ModFluids;
import techreborn.init.TRBlockEntities; import techreborn.init.TRBlockEntities;
import techreborn.init.TRContent; import techreborn.init.TRContent;
import techreborn.items.ItemDynamicCell; import techreborn.items.DynamicCellItem;
import techreborn.items.ItemFrequencyTransmitter; import techreborn.items.ItemFrequencyTransmitter;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -127,7 +127,7 @@ public class TechRebornClient implements ClientModInitializer {
StackToolTipHandler.setup(); StackToolTipHandler.setup();
GuiBase.wrenchStack = new ItemStack(TRContent.WRENCH); GuiBase.wrenchStack = new ItemStack(TRContent.WRENCH);
GuiBase.fluidCellProvider = ItemDynamicCell::getCellWithFluid; GuiBase.fluidCellProvider = DynamicCellItem::getCellWithFluid;
StackInfoHUD.registerElement(new ItemFrequencyTransmitter.StackInfoFreqTransmitter()); StackInfoHUD.registerElement(new ItemFrequencyTransmitter.StackInfoFreqTransmitter());

View file

@ -35,7 +35,7 @@ import techreborn.config.TechRebornConfig;
import techreborn.init.ModRecipes; import techreborn.init.ModRecipes;
import techreborn.init.TRContent; import techreborn.init.TRContent;
import techreborn.init.TRBlockEntities; import techreborn.init.TRBlockEntities;
import techreborn.items.ItemDynamicCell; import techreborn.items.DynamicCellItem;
import techreborn.blockentity.machine.GenericMachineBlockEntity; import techreborn.blockentity.machine.GenericMachineBlockEntity;
public class IndustrialElectrolyzerBlockEntity extends GenericMachineBlockEntity implements IContainerProvider { public class IndustrialElectrolyzerBlockEntity extends GenericMachineBlockEntity implements IContainerProvider {
@ -53,8 +53,8 @@ public class IndustrialElectrolyzerBlockEntity extends GenericMachineBlockEntity
public BuiltContainer createContainer(int syncID, final PlayerEntity player) { public BuiltContainer createContainer(int syncID, final PlayerEntity player) {
return new ContainerBuilder("industrialelectrolyzer").player(player.inventory).inventory().hotbar() return new ContainerBuilder("industrialelectrolyzer").player(player.inventory).inventory().hotbar()
.addInventory().blockEntity(this) .addInventory().blockEntity(this)
.filterSlot(1, 47, 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, ItemDynamicCell.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) .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); .energySlot(6, 8, 72).syncEnergyValue().syncCrafterValue().addInventory().create(this, syncID);
} }

View file

@ -40,7 +40,7 @@ import techreborn.config.TechRebornConfig;
import techreborn.init.ModRecipes; import techreborn.init.ModRecipes;
import techreborn.init.TRContent; import techreborn.init.TRContent;
import techreborn.init.TRBlockEntities; import techreborn.init.TRBlockEntities;
import techreborn.items.ItemDynamicCell; import techreborn.items.DynamicCellItem;
import java.util.List; import java.util.List;
@ -59,8 +59,8 @@ public class IndustrialCentrifugeBlockEntity extends GenericMachineBlockEntity i
public BuiltContainer createContainer(int syncID, final PlayerEntity player) { public BuiltContainer createContainer(int syncID, final PlayerEntity player) {
return new ContainerBuilder("centrifuge").player(player.inventory).inventory().hotbar() return new ContainerBuilder("centrifuge").player(player.inventory).inventory().hotbar()
.addInventory().blockEntity(this) .addInventory().blockEntity(this)
.filterSlot(1, 40, 54, 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, ItemDynamicCell.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(2, 82, 44).outputSlot(3, 101, 25)
.outputSlot(4, 120, 44).outputSlot(5, 101, 63).energySlot(6, 8, 72).syncEnergyValue() .outputSlot(4, 120, 44).outputSlot(5, 101, 63).energySlot(6, 8, 72).syncEnergyValue()
.syncCrafterValue().addInventory().create(this, syncID); .syncCrafterValue().addInventory().create(this, syncID);

View file

@ -30,13 +30,13 @@ import io.github.cottonmc.libcd.api.tweaker.TweakerManager;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry; import net.minecraft.util.registry.Registry;
import techreborn.TechReborn; import techreborn.TechReborn;
import techreborn.items.ItemDynamicCell; import techreborn.items.DynamicCellItem;
public class LibCDPlugin implements LibCDInitializer { public class LibCDPlugin implements LibCDInitializer {
@Override @Override
public void initTweakers(TweakerManager manager) { public void initTweakers(TweakerManager manager) {
manager.addTweaker("techreborn.TRTweaker", TRTweaker.INSTANCE); 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 @Override

View file

@ -54,7 +54,7 @@ import techreborn.init.TRArmorMaterial;
import techreborn.init.TRContent; import techreborn.init.TRContent;
import techreborn.init.TRContent.*; import techreborn.init.TRContent.*;
import techreborn.init.TRToolTier; import techreborn.init.TRToolTier;
import techreborn.items.ItemDynamicCell; import techreborn.items.DynamicCellItem;
import techreborn.items.ItemFrequencyTransmitter; import techreborn.items.ItemFrequencyTransmitter;
import techreborn.items.ItemManual; import techreborn.items.ItemManual;
import techreborn.items.ItemScrapBox; 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.SCRAP_BOX = InitUtils.setup(new ItemScrapBox(), "scrap_box"));
RebornRegistry.registerItem(TRContent.MANUAL = InitUtils.setup(new ItemManual(), "manual")); RebornRegistry.registerItem(TRContent.MANUAL = InitUtils.setup(new ItemManual(), "manual"));
RebornRegistry.registerItem(TRContent.DEBUG_TOOL = InitUtils.setup(new DebugToolItem(), "debug_tool")); 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"); TechReborn.LOGGER.debug("TechReborns Items Loaded");
} }

View file

@ -78,7 +78,7 @@ import techreborn.blocks.transformers.BlockMVTransformer;
import techreborn.client.EGui; import techreborn.client.EGui;
import techreborn.config.TechRebornConfig; import techreborn.config.TechRebornConfig;
import techreborn.entities.EntityNukePrimed; import techreborn.entities.EntityNukePrimed;
import techreborn.items.ItemDynamicCell; import techreborn.items.DynamicCellItem;
import techreborn.items.ItemUpgrade; import techreborn.items.ItemUpgrade;
import techreborn.items.armor.ItemQuantumSuit; import techreborn.items.armor.ItemQuantumSuit;
import techreborn.utils.InitUtils; import techreborn.utils.InitUtils;
@ -147,7 +147,7 @@ public class TRContent {
public static Item FREQUENCY_TRANSMITTER; public static Item FREQUENCY_TRANSMITTER;
public static Item SCRAP_BOX; public static Item SCRAP_BOX;
public static Item MANUAL; public static Item MANUAL;
public static ItemDynamicCell CELL; public static DynamicCellItem CELL;
//Quantum Suit //Quantum Suit
public static ItemQuantumSuit QUANTUM_HELMET; public static ItemQuantumSuit QUANTUM_HELMET;

View file

@ -1,13 +1,23 @@
package techreborn.init; package techreborn.init;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.DispenserBlock; import net.minecraft.block.DispenserBlock;
import net.minecraft.block.FluidDrainable;
import net.minecraft.block.dispenser.ItemDispenserBehavior; 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.item.ItemStack;
import net.minecraft.util.math.BlockPointer; import net.minecraft.util.math.BlockPointer;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction; import net.minecraft.util.math.Direction;
import net.minecraft.util.math.Position; import net.minecraft.util.math.Position;
import net.minecraft.world.IWorld;
import reborncore.common.crafting.RebornRecipe; import reborncore.common.crafting.RebornRecipe;
import techreborn.config.TechRebornConfig; import techreborn.config.TechRebornConfig;
import techreborn.items.DynamicCellItem;
import java.util.List; import java.util.List;
import java.util.Random; 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);
}
}
}
});
} }
} }

View file

@ -26,7 +26,10 @@ package techreborn.items;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.block.FluidDrainable; import net.minecraft.block.FluidDrainable;
import net.minecraft.block.FluidFillable;
import net.minecraft.block.Material;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.fluid.BaseFluid;
import net.minecraft.fluid.Fluid; import net.minecraft.fluid.Fluid;
import net.minecraft.fluid.Fluids; import net.minecraft.fluid.Fluids;
import net.minecraft.item.Item; import net.minecraft.item.Item;
@ -66,12 +69,78 @@ import javax.annotation.Nullable;
/** /**
* Created by modmuss50 on 17/05/2016. * 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)); 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 @Override
public void appendStacks(ItemGroup tab, DefaultedList<ItemStack> subItems) { public void appendStacks(ItemGroup tab, DefaultedList<ItemStack> subItems) {
if (!isIn(tab)) { if (!isIn(tab)) {
@ -95,43 +164,6 @@ public class ItemDynamicCell extends Item implements ItemFluidInfo {
return super.getName(itemStack); 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 @Override
public TypedActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) { public TypedActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) {
ItemStack stack = player.getStackInHand(hand); ItemStack stack = player.getStackInHand(hand);
@ -170,15 +202,7 @@ public class ItemDynamicCell extends Item implements ItemFluidInfo {
} else { } else {
BlockState placeState = world.getBlockState(placePos); BlockState placeState = world.getBlockState(placePos);
if(placeState.canBucketPlace(containedFluid)){ if(placeState.canBucketPlace(containedFluid)){
if (world.dimension.doesWaterVaporize() && containedFluid.matches(FluidTags.WATER)) { placeFluid(player, world, placePos, blockHitResult, stack);
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);
}
if(stack.getCount() == 1) { if(stack.getCount() == 1) {
stack = getEmpty(); stack = getEmpty();
@ -195,15 +219,23 @@ public class ItemDynamicCell extends Item implements ItemFluidInfo {
return TypedActionResult.fail(stack); return TypedActionResult.fail(stack);
} }
private void insertOrDropStack(PlayerEntity playerEntity, ItemStack stack){ // ItemFluidInfo
if(!playerEntity.inventory.insertStack(stack)){ @Override
playerEntity.dropStack(stack); public ItemStack getEmpty() {
} return new ItemStack(this);
} }
// Thanks vanilla :) @Override
private void playEmptyingSound(@Nullable PlayerEntity playerEntity, IWorld world, BlockPos blockPos, Fluid fluid) { public ItemStack getFull(Fluid fluid) {
SoundEvent soundEvent = fluid.matches(FluidTags.LAVA) ? SoundEvents.ITEM_BUCKET_EMPTY_LAVA : SoundEvents.ITEM_BUCKET_EMPTY; return getCellWithFluid(fluid);
world.playSound(playerEntity, blockPos, soundEvent, SoundCategory.BLOCKS, 1.0F, 1.0F); }
@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;
} }
} }

View file

@ -25,21 +25,19 @@
package techreborn.utils; package techreborn.utils;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.recipe.Ingredient;
import net.minecraft.recipe.Recipe; import net.minecraft.recipe.Recipe;
import net.minecraft.recipe.RecipeType; import net.minecraft.recipe.RecipeType;
import net.minecraft.world.World; import net.minecraft.world.World;
import techreborn.items.ItemDynamicCell; import techreborn.items.DynamicCellItem;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.util.List;
import java.util.stream.Collectors;
public class RecipeUtils { public class RecipeUtils {
@Nonnull @Nonnull
public static ItemStack getEmptyCell(int stackSize) { public static ItemStack getEmptyCell(int stackSize) {
return ItemDynamicCell.getEmptyCell(stackSize); return DynamicCellItem.getEmptyCell(stackSize);
} }
/** /**
@ -49,7 +47,7 @@ public class RecipeUtils {
*/ */
public static <T extends Recipe<?>> ItemStack getMatchingRecipes(World world, RecipeType<T> type, ItemStack input){ public static <T extends Recipe<?>> ItemStack getMatchingRecipes(World world, RecipeType<T> type, ItemStack input){
return getRecipes(world, type).stream() 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) .map(Recipe::getOutput)
.findFirst() .findFirst()
.orElse(ItemStack.EMPTY); .orElse(ItemStack.EMPTY);