Refactor and cleanup, awaiting new model.
This commit is contained in:
parent
55fbcb712e
commit
90c23b9d57
10 changed files with 193 additions and 138 deletions
|
@ -25,16 +25,25 @@
|
|||
package techreborn.blockentity.machine.tier1;
|
||||
|
||||
import net.minecraft.block.BlockState;
|
||||
import net.minecraft.block.entity.BlockEntity;
|
||||
import net.minecraft.block.entity.HopperBlockEntity;
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.inventory.Inventory;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.item.Items;
|
||||
import net.minecraft.sound.SoundCategory;
|
||||
import net.minecraft.util.Tickable;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.math.Direction;
|
||||
import reborncore.api.items.InventoryBase;
|
||||
import reborncore.client.screen.BuiltScreenHandlerProvider;
|
||||
import reborncore.client.screen.builder.BuiltScreenHandler;
|
||||
import reborncore.client.screen.builder.ScreenHandlerBuilder;
|
||||
import reborncore.common.blockentity.MachineBaseBlockEntity;
|
||||
import reborncore.common.util.RebornInventory;
|
||||
import techreborn.blockentity.machine.GenericMachineBlockEntity;
|
||||
import techreborn.blockentity.machine.misc.ChargeOMatBlockEntity;
|
||||
import techreborn.blockentity.storage.item.StorageUnitBaseBlockEntity;
|
||||
import techreborn.blocks.misc.BlockRubberLog;
|
||||
import techreborn.config.TechRebornConfig;
|
||||
import techreborn.init.ModSounds;
|
||||
|
@ -42,108 +51,110 @@ import techreborn.init.TRBlockEntities;
|
|||
import techreborn.init.TRContent;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class TapperBlockEntity extends GenericMachineBlockEntity implements BuiltScreenHandlerProvider {
|
||||
import static reborncore.api.items.InventoryUtils.getInventoryAt;
|
||||
|
||||
public class TapperBlockEntity extends MachineBaseBlockEntity {
|
||||
|
||||
private static final int OUTPUT_SLOT = 0;
|
||||
|
||||
private RebornInventory<TapperBlockEntity> inventory = new RebornInventory<>(1, "TapperBlockEntity", 64, this);
|
||||
|
||||
private static final int ENERGY_SLOT = 0;
|
||||
private static final int OUTPUT_SLOT = 1;
|
||||
|
||||
//TODO LIST
|
||||
// Check tree structure
|
||||
// Move off to own functions
|
||||
// Run every like 5 seconds or something slow
|
||||
// GUI
|
||||
// Textures
|
||||
// Orientable
|
||||
// Use energy (check before)
|
||||
// Ensure storage before sapping
|
||||
// States
|
||||
|
||||
public TapperBlockEntity() {
|
||||
super(TRBlockEntities.TAPPER, "Tapper", TechRebornConfig.tapperMaxInput, TechRebornConfig.tapperMaxEnergy, TRContent.Machine.EXTRACTOR.block, ENERGY_SLOT);
|
||||
this.inventory = new RebornInventory<>(2, "TapperBlockEntity", 64, this);
|
||||
super(TRBlockEntities.TAPPER);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tick() {
|
||||
super.tick();
|
||||
|
||||
if(world == null || world.isClient) return;
|
||||
|
||||
// TODO cleanup and refactor (MVP)
|
||||
HashMap<BlockPos, BlockState> rubberLogs = new HashMap<>();
|
||||
|
||||
if (world.getTime() % 100 != 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
BlockPos originPos = this.pos.offset(Direction.NORTH);
|
||||
BlockState originState = world.getBlockState(originPos);
|
||||
if(originState.getBlock() != TRContent.RUBBER_LOG) return; // TODO CHECK STRUCTURE
|
||||
Inventory invBelow = getInventoryBelow();
|
||||
|
||||
rubberLogs.put(originPos, originState);
|
||||
if(originState.getBlock() != TRContent.RUBBER_LOG || invBelow == null) return;
|
||||
|
||||
HashMap<BlockPos, BlockState> sapLogs = new HashMap<>();
|
||||
|
||||
if(originState.get(BlockRubberLog.HAS_SAP)) {
|
||||
sapLogs.put(originPos, originState);
|
||||
}
|
||||
|
||||
// Get rubber logs with sap above origin
|
||||
addLogsWithSap(originPos, sapLogs);
|
||||
|
||||
// Harvest the sap to inventory, if possible.
|
||||
if(harvestSap(sapLogs, invBelow)){
|
||||
world.playSound(pos.getX(),pos.getY(),pos.getZ(), ModSounds.SAP_EXTRACT, SoundCategory.BLOCKS, 0.6F, 1F, false);
|
||||
}
|
||||
}
|
||||
|
||||
private Inventory getInventoryBelow() {
|
||||
return getInventoryAt(this.getWorld(), this.pos.offset(Direction.DOWN));
|
||||
}
|
||||
|
||||
private boolean harvestSap(HashMap<BlockPos, BlockState> sapLogs, Inventory invBelow){
|
||||
// Used for sound
|
||||
boolean hasSapped = false;
|
||||
|
||||
for (Map.Entry<BlockPos, BlockState> entry : sapLogs.entrySet()) {
|
||||
BlockPos pos = entry.getKey();
|
||||
BlockState state = entry.getValue();
|
||||
|
||||
ItemStack out = new ItemStack(TRContent.Parts.SAP, 1);
|
||||
out = HopperBlockEntity.transfer(null, invBelow, out, Direction.UP);
|
||||
if(out.isEmpty()){
|
||||
world.setBlockState(pos, state.with(BlockRubberLog.HAS_SAP, false).with(BlockRubberLog.SAP_SIDE, Direction.fromHorizontal(0)));
|
||||
hasSapped = true;
|
||||
}else{
|
||||
// Can't deposit into inventory, don't sap
|
||||
return hasSapped;
|
||||
}
|
||||
}
|
||||
|
||||
return hasSapped;
|
||||
}
|
||||
|
||||
private void addLogsWithSap(BlockPos originPos, HashMap<BlockPos, BlockState> sapLogs){
|
||||
boolean shouldExit = false;
|
||||
|
||||
BlockPos current = originPos;
|
||||
// Progress Up
|
||||
// Progress Up (Gravity fed, won't consider sap under current log), origin log has already been checked)
|
||||
while(!shouldExit){
|
||||
current = current.offset(Direction.UP);
|
||||
|
||||
BlockState state = world.getBlockState(current);
|
||||
if(state.getBlock() == TRContent.RUBBER_LOG){
|
||||
rubberLogs.put(current, state);
|
||||
}else{
|
||||
shouldExit = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Progress Down
|
||||
shouldExit = false;
|
||||
current = originPos;
|
||||
while(!shouldExit){
|
||||
current = current.offset(Direction.DOWN);
|
||||
|
||||
BlockState state = world.getBlockState(current);
|
||||
if(state.getBlock() == TRContent.RUBBER_LOG){
|
||||
rubberLogs.put(current, state);
|
||||
}else{
|
||||
shouldExit = true;
|
||||
}
|
||||
}
|
||||
|
||||
int yield = 0;
|
||||
|
||||
// Harvest sap
|
||||
for (Map.Entry<BlockPos, BlockState> entry : rubberLogs.entrySet()) {
|
||||
BlockPos pos = entry.getKey();
|
||||
BlockState state = entry.getValue();
|
||||
if(state.get(BlockRubberLog.HAS_SAP)){
|
||||
world.setBlockState(pos, state.with(BlockRubberLog.HAS_SAP, false).with(BlockRubberLog.SAP_SIDE, Direction.fromHorizontal(0)));
|
||||
yield++;
|
||||
}
|
||||
}
|
||||
|
||||
if(yield > 0){
|
||||
world.playSound(pos.getX(),pos.getY(),pos.getZ(), ModSounds.SAP_EXTRACT, SoundCategory.BLOCKS, 0.6F, 1F, false);
|
||||
|
||||
|
||||
if(inventory.getStack(OUTPUT_SLOT).isEmpty()){
|
||||
ItemStack yieldStack = TRContent.Parts.SAP.getStack();
|
||||
yieldStack.setCount(yield);
|
||||
this.inventory.setStack(OUTPUT_SLOT,yieldStack);
|
||||
}else{
|
||||
ItemStack currentStack = inventory.getStack(OUTPUT_SLOT);
|
||||
if(currentStack.getCount() + yield <= inventory.getStackLimit()){
|
||||
currentStack.increment(yield);
|
||||
// TODO check room before harvesting
|
||||
inventory.setStack(OUTPUT_SLOT, currentStack);
|
||||
if( state.get(BlockRubberLog.HAS_SAP)){
|
||||
sapLogs.put(current, state);
|
||||
}
|
||||
}else{
|
||||
shouldExit = true;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// IContainerProvider
|
||||
@Override
|
||||
public BuiltScreenHandler createScreenHandler(int syncID, final PlayerEntity player) {
|
||||
return new ScreenHandlerBuilder("tapper").player(player.inventory).inventory().hotbar().addInventory().blockEntity(this)
|
||||
.energySlot(0, 8, 72).outputSlot(1, 101, 45).syncEnergyValue()
|
||||
.addInventory().create(this, syncID);
|
||||
public boolean hasSlotConfig() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canBeUpgraded() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue