From 80716876b2f3611a73330c4a09ca531b59a37c51 Mon Sep 17 00:00:00 2001 From: Justin Vitale <4002351+justinvvitale@users.noreply.github.com> Date: Wed, 8 Jul 2020 03:34:36 +1000 Subject: [PATCH] Cleanup and drop when no log connected --- .../machine/tier1/ResinBasinBlockEntity.java | 82 ++++++++++--------- .../blocks/machine/tier1/ResinBasinBlock.java | 20 +---- 2 files changed, 48 insertions(+), 54 deletions(-) diff --git a/src/main/java/techreborn/blockentity/machine/tier1/ResinBasinBlockEntity.java b/src/main/java/techreborn/blockentity/machine/tier1/ResinBasinBlockEntity.java index f2bbd7910..2445b6e0e 100644 --- a/src/main/java/techreborn/blockentity/machine/tier1/ResinBasinBlockEntity.java +++ b/src/main/java/techreborn/blockentity/machine/tier1/ResinBasinBlockEntity.java @@ -24,21 +24,17 @@ package techreborn.blockentity.machine.tier1; -import net.minecraft.block.Block; import net.minecraft.block.BlockState; +import net.minecraft.block.Blocks; import net.minecraft.block.entity.HopperBlockEntity; -import net.minecraft.entity.LivingEntity; +import net.minecraft.entity.ItemEntity; import net.minecraft.inventory.Inventory; import net.minecraft.item.ItemStack; import net.minecraft.nbt.CompoundTag; import net.minecraft.sound.SoundCategory; -import net.minecraft.state.property.BooleanProperty; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Direction; -import net.minecraft.world.World; import reborncore.common.blockentity.MachineBaseBlockEntity; -import reborncore.common.blocks.BlockMachineBase; -import reborncore.common.util.RebornInventory; import techreborn.blocks.machine.tier1.ResinBasinBlock; import techreborn.blocks.misc.BlockRubberLog; import techreborn.config.TechRebornConfig; @@ -46,9 +42,6 @@ import techreborn.init.ModSounds; import techreborn.init.TRBlockEntities; import techreborn.init.TRContent; -import java.util.HashMap; -import java.util.Map; - import static reborncore.api.items.InventoryUtils.getInventoryAt; public class ResinBasinBlockEntity extends MachineBaseBlockEntity { @@ -67,19 +60,19 @@ public class ResinBasinBlockEntity extends MachineBaseBlockEntity { @Override public void tick() { super.tick(); - if(world == null || world.isClient) return; + if (world == null || world.isClient) return; boolean shouldUpdateState = false; - if(isPouring){ + if (isPouring) { pouringTimer--; // Play pouring audio - if(world.getTime() % 20 == 0){ + if (world.getTime() % 20 == 0) { world.playSound(null, pos, ModSounds.SAP_EXTRACT, SoundCategory.BLOCKS, 1F, 1F); } - if(pouringTimer == 0){ + if (pouringTimer == 0) { isPouring = false; isFull = true; shouldUpdateState = true; @@ -87,12 +80,14 @@ public class ResinBasinBlockEntity extends MachineBaseBlockEntity { } // Try and deposit - if(isFull){ + if (isFull) { // Find a rubber log Inventory invBelow = getInventoryBelow(); - if(invBelow != null) { + if (invBelow != null) { + ItemStack out = new ItemStack(TRContent.Parts.SAP, 1); out = HopperBlockEntity.transfer(null, invBelow, out, Direction.UP); + if (out.isEmpty()) { // Successfully deposited 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 if (world.getTime() % TechRebornConfig.checkForSapTime == 0) { BlockPos targetRubber = getLogWithSap(); - if (targetRubber != null){ + if (targetRubber != null) { // 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))); isPouring = true; @@ -116,7 +122,7 @@ public class ResinBasinBlockEntity extends MachineBaseBlockEntity { } } - if(shouldUpdateState){ + if (shouldUpdateState) { setPouringState(isPouring); setFullState(isFull); } @@ -134,7 +140,7 @@ public class ResinBasinBlockEntity extends MachineBaseBlockEntity { public void fromTag(BlockState blockState, CompoundTag tagCompound) { super.fromTag(blockState, tagCompound); - if(tagCompound.contains("isFull")){ + if (tagCompound.contains("isFull")) { this.isFull = tagCompound.getBoolean("isFull"); } } @@ -143,7 +149,7 @@ public class ResinBasinBlockEntity extends MachineBaseBlockEntity { public void onLoad() { super.onLoad(); - if(world == null || world.isClient) return; + if (world == null || world.isClient) return; // Set facing 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)); } + private boolean validPlacement() { + return world.getBlockState(this.pos.offset(direction)).getBlock() == TRContent.RUBBER_LOG; + } - private BlockPos getLogWithSap(){ + + private BlockPos getLogWithSap() { // Checking origin block BlockPos originPos = this.pos.offset(direction); BlockState originState = world.getBlockState(originPos); - if(originState.getBlock() != TRContent.RUBBER_LOG){ - return null; - } - - if(originState.get(BlockRubberLog.HAS_SAP)) { + if (originState.get(BlockRubberLog.HAS_SAP)) { return originPos; } @@ -171,15 +177,15 @@ public class ResinBasinBlockEntity extends MachineBaseBlockEntity { BlockPos current = originPos; // Progress Up - while(!shouldExit){ + while (!shouldExit) { current = current.offset(Direction.UP); BlockState state = world.getBlockState(current); - if(state.getBlock() == TRContent.RUBBER_LOG){ - if( state.get(BlockRubberLog.HAS_SAP)){ + if (state.getBlock() == TRContent.RUBBER_LOG) { + if (state.get(BlockRubberLog.HAS_SAP)) { return current; } - }else{ + } else { shouldExit = true; } } @@ -187,15 +193,15 @@ public class ResinBasinBlockEntity extends MachineBaseBlockEntity { current = originPos; shouldExit = false; // Progress Down - while(!shouldExit){ + while (!shouldExit) { current = current.offset(Direction.DOWN); BlockState state = world.getBlockState(current); - if(state.getBlock() == TRContent.RUBBER_LOG){ - if(state.get(BlockRubberLog.HAS_SAP)){ + if (state.getBlock() == TRContent.RUBBER_LOG) { + if (state.get(BlockRubberLog.HAS_SAP)) { return current; } - }else{ + } else { shouldExit = true; } } @@ -204,14 +210,14 @@ public class ResinBasinBlockEntity extends MachineBaseBlockEntity { return null; } - private void setPouringState(boolean value){ - if(world != null){ + private void setPouringState(boolean value) { + if (world != null) { world.setBlockState(pos, world.getBlockState(pos).with(ResinBasinBlock.POURING, value)); } } - private void setFullState(boolean value){ - if(world != null){ + private void setFullState(boolean value) { + if (world != null) { world.setBlockState(pos, world.getBlockState(pos).with(ResinBasinBlock.FULL, value)); } } diff --git a/src/main/java/techreborn/blocks/machine/tier1/ResinBasinBlock.java b/src/main/java/techreborn/blocks/machine/tier1/ResinBasinBlock.java index 33954b92d..2305a20c8 100644 --- a/src/main/java/techreborn/blocks/machine/tier1/ResinBasinBlock.java +++ b/src/main/java/techreborn/blocks/machine/tier1/ResinBasinBlock.java @@ -4,7 +4,6 @@ import net.minecraft.block.*; import net.minecraft.block.entity.BlockEntity; import net.minecraft.entity.ItemEntity; import net.minecraft.entity.LivingEntity; -import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.ItemStack; import net.minecraft.state.StateManager; 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.text.LiteralText; 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.Direction; import net.minecraft.util.shape.VoxelShape; import net.minecraft.util.shape.VoxelShapes; import net.minecraft.world.BlockView; import net.minecraft.world.World; -import net.minecraft.world.WorldView; -import reborncore.api.blockentity.IMachineGuiHandler; import reborncore.common.BaseBlockEntityProvider; -import reborncore.common.blockentity.MachineBaseBlockEntity; -import reborncore.common.blocks.BlockMachineBase; -import techreborn.blocks.GenericMachineBlock; import techreborn.init.TRContent; import java.util.function.Supplier; @@ -49,7 +39,7 @@ public class ResinBasinBlock extends BaseBlockEntityProvider { @Override 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) { @@ -72,19 +62,17 @@ public class ResinBasinBlock extends BaseBlockEntityProvider { @Override public void onPlaced(World worldIn, BlockPos pos, BlockState state, LivingEntity placer, ItemStack stack) { super.onPlaced(worldIn, pos, state, placer, stack); - - if(worldIn.isClient) return; + if (worldIn.isClient) return; Direction facing = placer.getHorizontalFacing().getOpposite(); setFacing(facing, worldIn, pos); // 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()); ItemEntity itemEntity = new ItemEntity(worldIn, pos.getX(), pos.getY(), pos.getZ(), new ItemStack(this.asBlock())); worldIn.spawnEntity(itemEntity); - placer.sendSystemMessage(new LiteralText(new TranslatableText(this.getTranslationKey()).getString() + new TranslatableText("techreborn.tooltip.invalid_basin_placement").getString()),null); - return; + placer.sendSystemMessage(new LiteralText(new TranslatableText(this.getTranslationKey()).getString() + new TranslatableText("techreborn.tooltip.invalid_basin_placement").getString()), null); } }