Cleanup and drop when no log connected

This commit is contained in:
Justin Vitale 2020-07-08 03:34:36 +10:00
parent 7d04348853
commit 80716876b2
2 changed files with 48 additions and 54 deletions

View file

@ -24,21 +24,17 @@
package techreborn.blockentity.machine.tier1; package techreborn.blockentity.machine.tier1;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.entity.HopperBlockEntity; import net.minecraft.block.entity.HopperBlockEntity;
import net.minecraft.entity.LivingEntity; import net.minecraft.entity.ItemEntity;
import net.minecraft.inventory.Inventory; import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.sound.SoundCategory; import net.minecraft.sound.SoundCategory;
import net.minecraft.state.property.BooleanProperty;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction; import net.minecraft.util.math.Direction;
import net.minecraft.world.World;
import reborncore.common.blockentity.MachineBaseBlockEntity; import reborncore.common.blockentity.MachineBaseBlockEntity;
import reborncore.common.blocks.BlockMachineBase;
import reborncore.common.util.RebornInventory;
import techreborn.blocks.machine.tier1.ResinBasinBlock; import techreborn.blocks.machine.tier1.ResinBasinBlock;
import techreborn.blocks.misc.BlockRubberLog; import techreborn.blocks.misc.BlockRubberLog;
import techreborn.config.TechRebornConfig; import techreborn.config.TechRebornConfig;
@ -46,9 +42,6 @@ import techreborn.init.ModSounds;
import techreborn.init.TRBlockEntities; import techreborn.init.TRBlockEntities;
import techreborn.init.TRContent; import techreborn.init.TRContent;
import java.util.HashMap;
import java.util.Map;
import static reborncore.api.items.InventoryUtils.getInventoryAt; import static reborncore.api.items.InventoryUtils.getInventoryAt;
public class ResinBasinBlockEntity extends MachineBaseBlockEntity { public class ResinBasinBlockEntity extends MachineBaseBlockEntity {
@ -67,19 +60,19 @@ public class ResinBasinBlockEntity extends MachineBaseBlockEntity {
@Override @Override
public void tick() { public void tick() {
super.tick(); super.tick();
if(world == null || world.isClient) return; if (world == null || world.isClient) return;
boolean shouldUpdateState = false; boolean shouldUpdateState = false;
if(isPouring){ if (isPouring) {
pouringTimer--; pouringTimer--;
// Play pouring audio // Play pouring audio
if(world.getTime() % 20 == 0){ if (world.getTime() % 20 == 0) {
world.playSound(null, pos, ModSounds.SAP_EXTRACT, SoundCategory.BLOCKS, 1F, 1F); world.playSound(null, pos, ModSounds.SAP_EXTRACT, SoundCategory.BLOCKS, 1F, 1F);
} }
if(pouringTimer == 0){ if (pouringTimer == 0) {
isPouring = false; isPouring = false;
isFull = true; isFull = true;
shouldUpdateState = true; shouldUpdateState = true;
@ -87,12 +80,14 @@ public class ResinBasinBlockEntity extends MachineBaseBlockEntity {
} }
// Try and deposit // Try and deposit
if(isFull){ if (isFull) {
// Find a rubber log // Find a rubber log
Inventory invBelow = getInventoryBelow(); Inventory invBelow = getInventoryBelow();
if(invBelow != null) { if (invBelow != null) {
ItemStack out = new ItemStack(TRContent.Parts.SAP, 1); ItemStack out = new ItemStack(TRContent.Parts.SAP, 1);
out = HopperBlockEntity.transfer(null, invBelow, out, Direction.UP); out = HopperBlockEntity.transfer(null, invBelow, out, Direction.UP);
if (out.isEmpty()) { if (out.isEmpty()) {
// Successfully deposited // Successfully deposited
isFull = false; isFull = false;
@ -101,12 +96,23 @@ public class ResinBasinBlockEntity extends MachineBaseBlockEntity {
} }
} }
if(!isFull && !isPouring) { boolean readyToHarvest = !isFull && !isPouring;
// Ensuring it's placed on a log
if ((readyToHarvest || world.getTime() % 20 == 0) && !validPlacement()) {
// Not placed on log, drop on ground
world.setBlockState(pos, Blocks.AIR.getDefaultState());
ItemEntity itemEntity = new ItemEntity(world, pos.getX(), pos.getY(), pos.getZ(), new ItemStack(TRContent.Machine.RESIN_BASIN));
world.spawnEntity(itemEntity);
return;
}
if (readyToHarvest) {
// Check for rubber // Check for rubber
if (world.getTime() % TechRebornConfig.checkForSapTime == 0) { if (world.getTime() % TechRebornConfig.checkForSapTime == 0) {
BlockPos targetRubber = getLogWithSap(); BlockPos targetRubber = getLogWithSap();
if (targetRubber != null){ if (targetRubber != null) {
// We have a valid sap log, harvest it // We have a valid sap log, harvest it
world.setBlockState(targetRubber, world.getBlockState(targetRubber).with(BlockRubberLog.HAS_SAP, false).with(BlockRubberLog.SAP_SIDE, Direction.fromHorizontal(0))); world.setBlockState(targetRubber, world.getBlockState(targetRubber).with(BlockRubberLog.HAS_SAP, false).with(BlockRubberLog.SAP_SIDE, Direction.fromHorizontal(0)));
isPouring = true; isPouring = true;
@ -116,7 +122,7 @@ public class ResinBasinBlockEntity extends MachineBaseBlockEntity {
} }
} }
if(shouldUpdateState){ if (shouldUpdateState) {
setPouringState(isPouring); setPouringState(isPouring);
setFullState(isFull); setFullState(isFull);
} }
@ -134,7 +140,7 @@ public class ResinBasinBlockEntity extends MachineBaseBlockEntity {
public void fromTag(BlockState blockState, CompoundTag tagCompound) { public void fromTag(BlockState blockState, CompoundTag tagCompound) {
super.fromTag(blockState, tagCompound); super.fromTag(blockState, tagCompound);
if(tagCompound.contains("isFull")){ if (tagCompound.contains("isFull")) {
this.isFull = tagCompound.getBoolean("isFull"); this.isFull = tagCompound.getBoolean("isFull");
} }
} }
@ -143,7 +149,7 @@ public class ResinBasinBlockEntity extends MachineBaseBlockEntity {
public void onLoad() { public void onLoad() {
super.onLoad(); super.onLoad();
if(world == null || world.isClient) return; if (world == null || world.isClient) return;
// Set facing // Set facing
direction = world.getBlockState(pos).get(ResinBasinBlock.FACING).getOpposite(); direction = world.getBlockState(pos).get(ResinBasinBlock.FACING).getOpposite();
@ -153,17 +159,17 @@ public class ResinBasinBlockEntity extends MachineBaseBlockEntity {
return getInventoryAt(this.getWorld(), this.pos.offset(Direction.DOWN)); return getInventoryAt(this.getWorld(), this.pos.offset(Direction.DOWN));
} }
private boolean validPlacement() {
return world.getBlockState(this.pos.offset(direction)).getBlock() == TRContent.RUBBER_LOG;
}
private BlockPos getLogWithSap(){
private BlockPos getLogWithSap() {
// Checking origin block // Checking origin block
BlockPos originPos = this.pos.offset(direction); BlockPos originPos = this.pos.offset(direction);
BlockState originState = world.getBlockState(originPos); BlockState originState = world.getBlockState(originPos);
if(originState.getBlock() != TRContent.RUBBER_LOG){ if (originState.get(BlockRubberLog.HAS_SAP)) {
return null;
}
if(originState.get(BlockRubberLog.HAS_SAP)) {
return originPos; return originPos;
} }
@ -171,15 +177,15 @@ public class ResinBasinBlockEntity extends MachineBaseBlockEntity {
BlockPos current = originPos; BlockPos current = originPos;
// Progress Up // Progress Up
while(!shouldExit){ while (!shouldExit) {
current = current.offset(Direction.UP); current = current.offset(Direction.UP);
BlockState state = world.getBlockState(current); BlockState state = world.getBlockState(current);
if(state.getBlock() == TRContent.RUBBER_LOG){ if (state.getBlock() == TRContent.RUBBER_LOG) {
if( state.get(BlockRubberLog.HAS_SAP)){ if (state.get(BlockRubberLog.HAS_SAP)) {
return current; return current;
} }
}else{ } else {
shouldExit = true; shouldExit = true;
} }
} }
@ -187,15 +193,15 @@ public class ResinBasinBlockEntity extends MachineBaseBlockEntity {
current = originPos; current = originPos;
shouldExit = false; shouldExit = false;
// Progress Down // Progress Down
while(!shouldExit){ while (!shouldExit) {
current = current.offset(Direction.DOWN); current = current.offset(Direction.DOWN);
BlockState state = world.getBlockState(current); BlockState state = world.getBlockState(current);
if(state.getBlock() == TRContent.RUBBER_LOG){ if (state.getBlock() == TRContent.RUBBER_LOG) {
if(state.get(BlockRubberLog.HAS_SAP)){ if (state.get(BlockRubberLog.HAS_SAP)) {
return current; return current;
} }
}else{ } else {
shouldExit = true; shouldExit = true;
} }
} }
@ -204,14 +210,14 @@ public class ResinBasinBlockEntity extends MachineBaseBlockEntity {
return null; return null;
} }
private void setPouringState(boolean value){ private void setPouringState(boolean value) {
if(world != null){ if (world != null) {
world.setBlockState(pos, world.getBlockState(pos).with(ResinBasinBlock.POURING, value)); world.setBlockState(pos, world.getBlockState(pos).with(ResinBasinBlock.POURING, value));
} }
} }
private void setFullState(boolean value){ private void setFullState(boolean value) {
if(world != null){ if (world != null) {
world.setBlockState(pos, world.getBlockState(pos).with(ResinBasinBlock.FULL, value)); world.setBlockState(pos, world.getBlockState(pos).with(ResinBasinBlock.FULL, value));
} }
} }

View file

@ -4,7 +4,6 @@ import net.minecraft.block.*;
import net.minecraft.block.entity.BlockEntity; import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.ItemEntity; import net.minecraft.entity.ItemEntity;
import net.minecraft.entity.LivingEntity; import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.state.StateManager; import net.minecraft.state.StateManager;
import net.minecraft.state.property.BooleanProperty; import net.minecraft.state.property.BooleanProperty;
@ -12,22 +11,13 @@ import net.minecraft.state.property.DirectionProperty;
import net.minecraft.state.property.Properties; import net.minecraft.state.property.Properties;
import net.minecraft.text.LiteralText; import net.minecraft.text.LiteralText;
import net.minecraft.text.TranslatableText; import net.minecraft.text.TranslatableText;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Formatting;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction; import net.minecraft.util.math.Direction;
import net.minecraft.util.shape.VoxelShape; import net.minecraft.util.shape.VoxelShape;
import net.minecraft.util.shape.VoxelShapes; import net.minecraft.util.shape.VoxelShapes;
import net.minecraft.world.BlockView; import net.minecraft.world.BlockView;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraft.world.WorldView;
import reborncore.api.blockentity.IMachineGuiHandler;
import reborncore.common.BaseBlockEntityProvider; import reborncore.common.BaseBlockEntityProvider;
import reborncore.common.blockentity.MachineBaseBlockEntity;
import reborncore.common.blocks.BlockMachineBase;
import techreborn.blocks.GenericMachineBlock;
import techreborn.init.TRContent; import techreborn.init.TRContent;
import java.util.function.Supplier; import java.util.function.Supplier;
@ -49,7 +39,7 @@ public class ResinBasinBlock extends BaseBlockEntityProvider {
@Override @Override
public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) { public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) {
return VoxelShapes.cuboid(0, 0, 0,1,15/16f,1); return VoxelShapes.cuboid(0, 0, 0, 1, 15 / 16f, 1);
} }
public void setFacing(Direction facing, World world, BlockPos pos) { public void setFacing(Direction facing, World world, BlockPos pos) {
@ -72,19 +62,17 @@ public class ResinBasinBlock extends BaseBlockEntityProvider {
@Override @Override
public void onPlaced(World worldIn, BlockPos pos, BlockState state, LivingEntity placer, ItemStack stack) { public void onPlaced(World worldIn, BlockPos pos, BlockState state, LivingEntity placer, ItemStack stack) {
super.onPlaced(worldIn, pos, state, placer, stack); super.onPlaced(worldIn, pos, state, placer, stack);
if (worldIn.isClient) return;
if(worldIn.isClient) return;
Direction facing = placer.getHorizontalFacing().getOpposite(); Direction facing = placer.getHorizontalFacing().getOpposite();
setFacing(facing, worldIn, pos); setFacing(facing, worldIn, pos);
// Drop item if not next to log and yell at user // Drop item if not next to log and yell at user
if(worldIn.getBlockState(pos.offset(facing.getOpposite())).getBlock() != TRContent.RUBBER_LOG){ if (worldIn.getBlockState(pos.offset(facing.getOpposite())).getBlock() != TRContent.RUBBER_LOG) {
worldIn.setBlockState(pos, Blocks.AIR.getDefaultState()); worldIn.setBlockState(pos, Blocks.AIR.getDefaultState());
ItemEntity itemEntity = new ItemEntity(worldIn, pos.getX(), pos.getY(), pos.getZ(), new ItemStack(this.asBlock())); ItemEntity itemEntity = new ItemEntity(worldIn, pos.getX(), pos.getY(), pos.getZ(), new ItemStack(this.asBlock()));
worldIn.spawnEntity(itemEntity); worldIn.spawnEntity(itemEntity);
placer.sendSystemMessage(new LiteralText(new TranslatableText(this.getTranslationKey()).getString() + new TranslatableText("techreborn.tooltip.invalid_basin_placement").getString()),null); placer.sendSystemMessage(new LiteralText(new TranslatableText(this.getTranslationKey()).getString() + new TranslatableText("techreborn.tooltip.invalid_basin_placement").getString()), null);
return;
} }
} }