From 90c23b9d577dcfc41ba90bedf9b0f4983209f26d Mon Sep 17 00:00:00 2001 From: Justin Vitale <4002351+justinvvitale@users.noreply.github.com> Date: Thu, 2 Jul 2020 17:20:50 +1000 Subject: [PATCH] Refactor and cleanup, awaiting new model. --- .../machine/tier1/TapperBlockEntity.java | 149 ++++++++++-------- .../blocks/machine/tier1/TapperBlock.java | 24 +++ .../blocks/misc/BlockRubberLog.java | 2 +- src/main/java/techreborn/client/GuiType.java | 1 - .../java/techreborn/client/gui/GuiTapper.java | 60 ------- .../techreborn/config/TechRebornConfig.java | 6 - src/main/java/techreborn/init/TRContent.java | 3 +- .../assets/techreborn/blockstates/tapper.json | 5 + .../block/machines/tier1_machines/tapper.json | 78 +++++++++ .../assets/techreborn/models/item/tapper.json | 3 + 10 files changed, 193 insertions(+), 138 deletions(-) create mode 100644 src/main/java/techreborn/blocks/machine/tier1/TapperBlock.java delete mode 100644 src/main/java/techreborn/client/gui/GuiTapper.java create mode 100644 src/main/resources/assets/techreborn/blockstates/tapper.json create mode 100644 src/main/resources/assets/techreborn/models/block/machines/tier1_machines/tapper.json create mode 100644 src/main/resources/assets/techreborn/models/item/tapper.json diff --git a/src/main/java/techreborn/blockentity/machine/tier1/TapperBlockEntity.java b/src/main/java/techreborn/blockentity/machine/tier1/TapperBlockEntity.java index 0cc6d88b6..25e320d43 100644 --- a/src/main/java/techreborn/blockentity/machine/tier1/TapperBlockEntity.java +++ b/src/main/java/techreborn/blockentity/machine/tier1/TapperBlockEntity.java @@ -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 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 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 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 sapLogs, Inventory invBelow){ + // Used for sound + boolean hasSapped = false; + + for (Map.Entry 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 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 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; } } diff --git a/src/main/java/techreborn/blocks/machine/tier1/TapperBlock.java b/src/main/java/techreborn/blocks/machine/tier1/TapperBlock.java new file mode 100644 index 000000000..ad5a53f92 --- /dev/null +++ b/src/main/java/techreborn/blocks/machine/tier1/TapperBlock.java @@ -0,0 +1,24 @@ +package techreborn.blocks.machine.tier1; + +import net.minecraft.block.BlockState; +import net.minecraft.block.ShapeContext; +import net.minecraft.block.entity.BlockEntity; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.shape.VoxelShape; +import net.minecraft.util.shape.VoxelShapes; +import net.minecraft.world.BlockView; +import reborncore.api.blockentity.IMachineGuiHandler; +import techreborn.blocks.GenericMachineBlock; + +import java.util.function.Supplier; + +public class TapperBlock extends GenericMachineBlock { + public TapperBlock(IMachineGuiHandler gui, Supplier blockEntityClass) { + super(gui, blockEntityClass); + } + + @Override + public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) { + return VoxelShapes.cuboid(2/16f, 0, 2/16f,14/16f,8/16f,14/16f); + } +} diff --git a/src/main/java/techreborn/blocks/misc/BlockRubberLog.java b/src/main/java/techreborn/blocks/misc/BlockRubberLog.java index 023d79689..eb0c8231d 100644 --- a/src/main/java/techreborn/blocks/misc/BlockRubberLog.java +++ b/src/main/java/techreborn/blocks/misc/BlockRubberLog.java @@ -107,7 +107,7 @@ public class BlockRubberLog extends PillarBlock { if (state.get(HAS_SAP)) { return; } - if (random.nextInt(50) == 0) { + if (true) { Direction facing = Direction.fromHorizontal(random.nextInt(4)); if (worldIn.getBlockState(pos.offset(Direction.DOWN, 1)).getBlock() == this && worldIn.getBlockState(pos.up()).getBlock() == this) { diff --git a/src/main/java/techreborn/client/GuiType.java b/src/main/java/techreborn/client/GuiType.java index d32287bbe..0e0cd5689 100644 --- a/src/main/java/techreborn/client/GuiType.java +++ b/src/main/java/techreborn/client/GuiType.java @@ -99,7 +99,6 @@ public final class GuiType implements IMachineGuiHandler public static final GuiType DISTILLATION_TOWER = register("distillation_tower", () -> () -> GuiDistillationTower::new); public static final GuiType ELECTRIC_FURNACE = register("electric_furnace", () -> () -> GuiElectricFurnace::new); public static final GuiType EXTRACTOR = register("extractor", () -> () -> GuiExtractor::new); - public static final GuiType TAPPER = register("tapper", () -> () -> GuiTapper::new); public static final GuiType FUSION_CONTROLLER = register("fusion_controller", () -> () -> GuiFusionReactor::new); public static final GuiType GAS_TURBINE = register("gas_turbine", () -> () -> GuiGasTurbine::new); public static final GuiType GENERATOR = register("generator", () -> () -> GuiGenerator::new); diff --git a/src/main/java/techreborn/client/gui/GuiTapper.java b/src/main/java/techreborn/client/gui/GuiTapper.java deleted file mode 100644 index 7b33a0f65..000000000 --- a/src/main/java/techreborn/client/gui/GuiTapper.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * This file is part of TechReborn, licensed under the MIT License (MIT). - * - * Copyright (c) 2020 TechReborn - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package techreborn.client.gui; - -import net.minecraft.client.util.math.MatrixStack; -import net.minecraft.entity.player.PlayerEntity; -import reborncore.client.gui.builder.GuiBase; -import reborncore.client.gui.guibuilder.GuiBuilder; -import reborncore.client.screen.builder.BuiltScreenHandler; -import techreborn.blockentity.machine.tier1.ExtractorBlockEntity; -import techreborn.blockentity.machine.tier1.TapperBlockEntity; - -public class GuiTapper extends GuiBase { - - TapperBlockEntity blockEntity; - - public GuiTapper(int syncID, final PlayerEntity player, final TapperBlockEntity blockEntity) { - super(player, blockEntity, blockEntity.createScreenHandler(syncID, player)); - this.blockEntity = blockEntity; - } - - @Override - protected void drawBackground(MatrixStack matrixStack, final float f, final int mouseX, final int mouseY) { - super.drawBackground(matrixStack, f, mouseX, mouseY); - final Layer layer = Layer.BACKGROUND; - - drawSlot(matrixStack, 8, 72, layer); - drawOutputSlot(matrixStack, 101, 45, layer); - } - - @Override - protected void drawForeground(MatrixStack matrixStack, final int mouseX, final int mouseY) { - super.drawForeground(matrixStack, mouseX, mouseY); - final Layer layer = Layer.FOREGROUND; - - builder.drawMultiEnergyBar(matrixStack, this, 9, 19, (int) blockEntity.getEnergy(), (int) blockEntity.getMaxPower(), mouseX, mouseY, 0, layer); - } -} diff --git a/src/main/java/techreborn/config/TechRebornConfig.java b/src/main/java/techreborn/config/TechRebornConfig.java index c71ac0c9d..0ce6c59b7 100644 --- a/src/main/java/techreborn/config/TechRebornConfig.java +++ b/src/main/java/techreborn/config/TechRebornConfig.java @@ -342,12 +342,6 @@ public class TechRebornConfig { @Config(config = "machines", category = "extractor", key = "ExtractorMaxEnergy", comment = "Extractor Max Energy (Value in EU)") public static int extractorMaxEnergy = 1_000; - @Config(config = "machines", category = "extractor", key = "TapperInput", comment = "Tapper Max Input (Value in EU)") - public static int tapperMaxInput = 32; - - @Config(config = "machines", category = "extractor", key = "TapperMaxEnergy", comment = "Tapper Max Energy (Value in EU)") - public static int tapperMaxEnergy = 1_000; - @Config(config = "machines", category = "compressor", key = "CompressorInput", comment = "Compressor Max Input (Value in EU)") public static int compressorMaxInput = 32; diff --git a/src/main/java/techreborn/init/TRContent.java b/src/main/java/techreborn/init/TRContent.java index b71984f85..c95f208bd 100644 --- a/src/main/java/techreborn/init/TRContent.java +++ b/src/main/java/techreborn/init/TRContent.java @@ -66,6 +66,7 @@ import techreborn.blocks.lighting.BlockLamp; import techreborn.blocks.machine.tier0.IronAlloyFurnaceBlock; import techreborn.blocks.machine.tier0.IronFurnaceBlock; import techreborn.blocks.machine.tier1.BlockPlayerDetector; +import techreborn.blocks.machine.tier1.TapperBlock; import techreborn.blocks.misc.BlockAlarm; import techreborn.blocks.misc.BlockMachineCasing; import techreborn.blocks.misc.BlockMachineFrame; @@ -520,7 +521,7 @@ public class TRContent { COMPRESSOR(new GenericMachineBlock(GuiType.COMPRESSOR, CompressorBlockEntity::new)), DISTILLATION_TOWER(new GenericMachineBlock(GuiType.DISTILLATION_TOWER, DistillationTowerBlockEntity::new)), EXTRACTOR(new GenericMachineBlock(GuiType.EXTRACTOR, ExtractorBlockEntity::new)), - TAPPER(new GenericMachineBlock(GuiType.TAPPER, TapperBlockEntity::new)), + TAPPER(new TapperBlock(null, TapperBlockEntity::new)), FLUID_REPLICATOR(new GenericMachineBlock(GuiType.FLUID_REPLICATOR, FluidReplicatorBlockEntity::new)), GRINDER(new DataDrivenMachineBlock("techreborn:grinder")), ELECTRIC_FURNACE(new GenericMachineBlock(GuiType.ELECTRIC_FURNACE, ElectricFurnaceBlockEntity::new)), diff --git a/src/main/resources/assets/techreborn/blockstates/tapper.json b/src/main/resources/assets/techreborn/blockstates/tapper.json new file mode 100644 index 000000000..ed6108c02 --- /dev/null +++ b/src/main/resources/assets/techreborn/blockstates/tapper.json @@ -0,0 +1,5 @@ +{ + "variants": { + "": { "model": "techreborn:block/machines/tier1_machines/tapper" } + } +} diff --git a/src/main/resources/assets/techreborn/models/block/machines/tier1_machines/tapper.json b/src/main/resources/assets/techreborn/models/block/machines/tier1_machines/tapper.json new file mode 100644 index 000000000..387a4bd47 --- /dev/null +++ b/src/main/resources/assets/techreborn/models/block/machines/tier1_machines/tapper.json @@ -0,0 +1,78 @@ +{ + "credit": "Made with Blockbench", + "textures": { + "0": "techreborn:block/machines/tier0_machines/machine_side", + "1": "techreborn:block/machines/tier0_machines/fluid_drain_top", + "2": "techreborn:block/machines/tier0_machines/basic_unit_bottom", + "3": "techreborn:block/machines/tier0_machines/crude_storage_unit_bottom", + "particle": "techreborn:block/machines/tier0_machines/machine_side" + }, + "elements": [ + { + "from": [7, 11, -2], + "to": [9, 13, 6], + "rotation": {"angle": 0, "axis": "y", "origin": [16, 13, -10]}, + "faces": { + "north": {"uv": [4, 4, 6, 6], "texture": "#3"}, + "east": {"uv": [4, 4, 12, 6], "texture": "#3"}, + "south": {"uv": [4, 4, 6, 6], "texture": "#3"}, + "west": {"uv": [4, 4, 12, 6], "texture": "#3"}, + "up": {"uv": [4, 4, 6, 12], "texture": "#3"}, + "down": {"uv": [4, 4, 6, 12], "texture": "#3"} + } + }, + { + "from": [6, 8, 6], + "to": [10, 13, 10], + "rotation": {"angle": 0, "axis": "y", "origin": [14, 13, 15]}, + "faces": { + "north": {"uv": [4, 4, 9, 9], "texture": "#3"}, + "east": {"uv": [4, 4, 9, 9], "texture": "#3"}, + "south": {"uv": [4, 4, 9, 9], "texture": "#3"}, + "west": {"uv": [4, 4, 9, 9], "texture": "#3"}, + "up": {"uv": [4, 4, 9, 9], "texture": "#3"}, + "down": {"uv": [4, 4, 9, 9], "texture": "#3"} + } + }, + { + "from": [2, 0, 2], + "to": [14, 8, 14], + "rotation": {"angle": 0, "axis": "y", "origin": [14, 8, 12]}, + "faces": { + "north": {"uv": [0, 0, 16, 16], "texture": "#0"}, + "east": {"uv": [0, 0, 16, 16], "texture": "#0"}, + "south": {"uv": [0, 0, 16, 16], "texture": "#0"}, + "west": {"uv": [0, 0, 16, 16], "texture": "#0"}, + "up": {"uv": [0, 0, 16, 16], "texture": "#1"}, + "down": {"uv": [0, 0, 16, 16], "texture": "#2"} + } + }, + { + "from": [5, 13, 7], + "to": [11, 14, 9], + "rotation": {"angle": 0, "axis": "y", "origin": [16, 13, -2]}, + "faces": { + "north": {"uv": [4, 2, 10, 3], "texture": "#0"}, + "east": {"uv": [4, 2, 6, 3], "texture": "#0"}, + "south": {"uv": [4, 2, 10, 3], "texture": "#0"}, + "west": {"uv": [4, 2, 6, 3], "texture": "#0"}, + "up": {"uv": [4, 2, 10, 4], "texture": "#0"}, + "down": {"uv": [4, 2, 10, 4], "texture": "#0"} + } + }, + { + "from": [7, 13, 5], + "to": [9, 14, 11], + "rotation": {"angle": 0, "axis": "y", "origin": [16, 13, -2]}, + "faces": { + "north": {"uv": [4, 2, 11, 4], "texture": "#0"}, + "east": {"uv": [4, 2, 11, 4], "texture": "#0"}, + "south": {"uv": [4, 2, 11, 4], "texture": "#0"}, + "west": {"uv": [4, 2, 11, 4], "texture": "#0"}, + "up": {"uv": [4, 2, 11, 4], "texture": "#0"}, + "down": {"uv": [4, 2, 11, 4], "texture": "#0"} + } + } + ], + "display": {} +} \ No newline at end of file diff --git a/src/main/resources/assets/techreborn/models/item/tapper.json b/src/main/resources/assets/techreborn/models/item/tapper.json new file mode 100644 index 000000000..8424f5226 --- /dev/null +++ b/src/main/resources/assets/techreborn/models/item/tapper.json @@ -0,0 +1,3 @@ +{ + "parent": "techreborn:block/machines/tier1_machines/tapper" +} \ No newline at end of file