Recycler should use energy. Closes #2304

This commit is contained in:
drcrazy 2020-12-18 16:07:39 +03:00
parent 9d4e25377a
commit b720fa99b9
5 changed files with 102 additions and 162 deletions

View file

@ -0,0 +1,68 @@
package techreborn.api.recipe;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.item.ItemStack;
import reborncore.common.crafting.RebornRecipe;
import reborncore.common.recipes.RecipeCrafter;
import reborncore.common.util.RebornInventory;
import techreborn.config.TechRebornConfig;
import techreborn.init.ModRecipes;
import java.util.List;
import java.util.Objects;
public class RecyclerRecipeCrafter extends RecipeCrafter {
public RecyclerRecipeCrafter(BlockEntity blockEntity, RebornInventory<?> inventory, int[] inputSlots, int[] outputSlots) {
super(ModRecipes.RECYCLER, blockEntity, 1, 1, inventory, inputSlots, outputSlots);
}
@Override
public void updateCurrentRecipe() {
currentTickTime = 0;
List<RebornRecipe> recipeList = ModRecipes.RECYCLER.getRecipes(blockEntity.getWorld());
if (recipeList.isEmpty() || !hasAllInputs()) {
setCurrentRecipe(null);
currentNeededTicks = 0;
setIsActive();
return;
}
setCurrentRecipe(recipeList.get(0));
currentNeededTicks = Math.max((int) (currentRecipe.getTime() * (1.0 - getSpeedMultiplier())), 1);
setIsActive();
}
@Override
public boolean hasAllInputs() {
boolean hasItem = false;
// Check if we have at least something in input slots. Foreach input slot in case of several input slots
for (int inputSlot : inputSlots) {
if (inventory.getStack(inputSlot).isEmpty()) continue;
hasItem = true;
break;
}
return hasItem;
}
@Override
public void useAllInputs() {
if (currentRecipe == null) {
return;
}
// Uses input. Foreach input slot in case of several input slots
for (int inputSlot : inputSlots) {
if (inventory.getStack(inputSlot).isEmpty()) continue;
inventory.shrinkSlot(inputSlot, 1);
break;
}
}
@Override
public void fitStack(ItemStack stack, int slot) {
// Dirty hack for chance based crafting
final int randomChance = Objects.requireNonNull(blockEntity.getWorld()).random.nextInt(TechRebornConfig.recyclerChance);
if (randomChance == 1) {
super.fitStack(stack, slot);
}
}
}

View file

@ -24,176 +24,29 @@
package techreborn.blockentity.machine.tier1; package techreborn.blockentity.machine.tier1;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.util.registry.Registry; import net.minecraft.util.registry.Registry;
import reborncore.api.IToolDrop;
import reborncore.api.blockentity.IUpgrade; import reborncore.api.blockentity.IUpgrade;
import reborncore.api.blockentity.InventoryProvider;
import reborncore.client.screen.BuiltScreenHandlerProvider; import reborncore.client.screen.BuiltScreenHandlerProvider;
import reborncore.client.screen.builder.BuiltScreenHandler; import reborncore.client.screen.builder.BuiltScreenHandler;
import reborncore.client.screen.builder.ScreenHandlerBuilder; import reborncore.client.screen.builder.ScreenHandlerBuilder;
import reborncore.common.blockentity.SlotConfiguration;
import reborncore.common.blocks.BlockMachineBase;
import reborncore.common.powerSystem.PowerAcceptorBlockEntity;
import reborncore.common.util.RebornInventory; import reborncore.common.util.RebornInventory;
import team.reborn.energy.EnergySide; import techreborn.api.recipe.RecyclerRecipeCrafter;
import techreborn.blockentity.machine.GenericMachineBlockEntity;
import techreborn.config.TechRebornConfig; import techreborn.config.TechRebornConfig;
import techreborn.init.TRBlockEntities; import techreborn.init.TRBlockEntities;
import techreborn.init.TRContent; import techreborn.init.TRContent;
public class RecyclerBlockEntity extends PowerAcceptorBlockEntity public class RecyclerBlockEntity extends GenericMachineBlockEntity implements BuiltScreenHandlerProvider {
implements IToolDrop, InventoryProvider, BuiltScreenHandlerProvider, SlotConfiguration.SlotFilter {
private final RebornInventory<RecyclerBlockEntity> inventory = new RebornInventory<>(3, "RecyclerBlockEntity", 64, this);
private final int cost = 2;
private final int time = 15;
private final int chance = 6;
private boolean isBurning;
private int progress;
public RecyclerBlockEntity() { public RecyclerBlockEntity() {
super(TRBlockEntities.RECYCLER); super(TRBlockEntities.RECYCLER, "Recycler", TechRebornConfig.recyclerMaxInput, TechRebornConfig.recyclerMaxEnergy, TRContent.Machine.RECYCLER.block, 2);
} final int[] inputs = new int[]{0};
final int[] outputs = new int[]{1};
public int gaugeProgressScaled(int scale) { this.inventory = new RebornInventory<>(3, "RecyclerBlockEntity", 64, this);
return progress * scale / time; this.crafter = new RecyclerRecipeCrafter(this, this.inventory, inputs, outputs);
}
public int getProgress() {
return progress;
}
public void setProgress(int progress) {
this.progress = progress;
}
public void recycleItems() {
ItemStack itemstack = TRContent.Parts.SCRAP.getStack();
final int randomchance = this.world.random.nextInt(chance);
if (randomchance == 1) {
if (inventory.getStack(1).isEmpty()) {
inventory.setStack(1, itemstack.copy());
} else {
inventory.getStack(1).increment(itemstack.getCount());
}
}
inventory.shrinkSlot(0, 1);
}
public boolean canRecycle() {
return !inventory.getStack(0).isEmpty() && hasSlotGotSpace(1);
}
public boolean hasSlotGotSpace(int slot) {
if (inventory.getStack(slot).isEmpty()) {
return true;
} else return inventory.getStack(slot).getCount() < inventory.getStack(slot).getMaxCount();
}
public boolean isBurning() {
return isBurning;
}
public void setBurning(boolean burning) {
this.isBurning = burning;
}
public void updateState() {
final BlockState BlockStateContainer = world.getBlockState(pos);
if (BlockStateContainer.getBlock() instanceof BlockMachineBase) {
final BlockMachineBase blockMachineBase = (BlockMachineBase) BlockStateContainer.getBlock();
boolean shouldBurn = isBurning || (canRecycle() && (getStored(EnergySide.UNKNOWN) > getEuPerTick(cost)));
if (BlockStateContainer.get(BlockMachineBase.ACTIVE) != shouldBurn) {
blockMachineBase.setActive(isBurning, world, pos);
}
}
}
// TilePowerAcceptor
@Override
public void tick() {
super.tick();
if (world.isClient) {
return;
}
charge(2);
boolean updateInventory = false;
if (canRecycle() && !isBurning() && getEnergy() != 0) {
setBurning(true);
} else if (isBurning()) {
if (getStored(EnergySide.UNKNOWN) < getEuPerTick(cost)) {
this.setBurning(false);
}
progress++;
if (progress >= Math.max((int) (time * (1.0 - getSpeedMultiplier())), 1)) {
progress = 0;
recycleItems();
updateInventory = true;
setBurning(false);
}
}
updateState();
if (updateInventory) {
markDirty();
}
}
@Override
public double getBaseMaxPower() {
return TechRebornConfig.recyclerMaxEnergy;
}
@Override
public boolean canProvideEnergy(EnergySide side) {
return false;
}
@Override
public double getBaseMaxOutput() {
return 0;
}
@Override
public double getBaseMaxInput() {
return TechRebornConfig.recyclerMaxInput;
}
// TileMachineBase
@Override
public boolean canBeUpgraded() {
return true;
}
// IToolDrop
@Override
public ItemStack getToolDrop(PlayerEntity entityPlayer) {
return TRContent.Machine.RECYCLER.getStack();
}
@Override
public boolean isStackValid(int slotID, ItemStack stack) {
if (slotID == 0) {
return canRecycle(stack);
}
return false;
}
@Override
public int[] getInputSlots() {
return new int[]{0};
}
// ItemHandlerProvider
@Override
public RebornInventory<RecyclerBlockEntity> getInventory() {
return this.inventory;
} }
public static boolean canRecycle(ItemStack stack) { public static boolean canRecycle(ItemStack stack) {
@ -204,13 +57,12 @@ public class RecyclerBlockEntity extends PowerAcceptorBlockEntity
return !TechRebornConfig.recyclerBlackList.contains(Registry.ITEM.getId(item).toString()); return !TechRebornConfig.recyclerBlackList.contains(Registry.ITEM.getId(item).toString());
} }
// IContainerProvider // BuiltScreenHandlerProvider
@Override @Override
public BuiltScreenHandler createScreenHandler(int syncID, PlayerEntity player) { public BuiltScreenHandler createScreenHandler(int syncID, PlayerEntity player) {
return new ScreenHandlerBuilder("recycler").player(player.inventory).inventory().hotbar().addInventory() return new ScreenHandlerBuilder("recycler").player(player.inventory).inventory().hotbar().addInventory()
.blockEntity(this).slot(0, 55, 45, RecyclerBlockEntity::canRecycle) .blockEntity(this).slot(0, 55, 45, RecyclerBlockEntity::canRecycle)
.outputSlot(1, 101, 45).energySlot(2, 8, 72).syncEnergyValue() .outputSlot(1, 101, 45).energySlot(2, 8, 72).syncEnergyValue()
.sync(this::getProgress, this::setProgress).addInventory().create(this, syncID); .addInventory().create(this, syncID);
} }
} }

View file

@ -45,9 +45,11 @@ public class GuiRecycler extends GuiBase<BuiltScreenHandler> {
super.drawBackground(matrixStack, f, mouseX, mouseY); super.drawBackground(matrixStack, f, mouseX, mouseY);
final GuiBase.Layer layer = GuiBase.Layer.BACKGROUND; final GuiBase.Layer layer = GuiBase.Layer.BACKGROUND;
// Battery slot
drawSlot(matrixStack, 8, 72, layer); drawSlot(matrixStack, 8, 72, layer);
// Input
drawSlot(matrixStack, 55, 45, layer); drawSlot(matrixStack, 55, 45, layer);
// Output
drawOutputSlot(matrixStack, 101, 45, layer); drawOutputSlot(matrixStack, 101, 45, layer);
} }
@ -56,7 +58,7 @@ public class GuiRecycler extends GuiBase<BuiltScreenHandler> {
super.drawForeground(matrixStack, mouseX, mouseY); super.drawForeground(matrixStack, mouseX, mouseY);
final GuiBase.Layer layer = GuiBase.Layer.FOREGROUND; final GuiBase.Layer layer = GuiBase.Layer.FOREGROUND;
builder.drawProgressBar(matrixStack, this, blockEntity.gaugeProgressScaled(100), 100, 76, 48, mouseX, mouseY, GuiBuilder.ProgressDirection.RIGHT, layer); builder.drawProgressBar(matrixStack, this, blockEntity.getProgressScaled(100), 100, 76, 48, mouseX, mouseY, GuiBuilder.ProgressDirection.RIGHT, layer);
builder.drawMultiEnergyBar(matrixStack, this, 9, 19, (int) blockEntity.getEnergy(), (int) blockEntity.getMaxStoredPower(), mouseX, mouseY, 0, layer); builder.drawMultiEnergyBar(matrixStack, this, 9, 19, (int) blockEntity.getEnergy(), (int) blockEntity.getMaxStoredPower(), mouseX, mouseY, 0, layer);
} }
} }

View file

@ -501,12 +501,15 @@ public class TechRebornConfig {
@Config(config = "machines", category = "fusion_reactor", key = "FusionReactorMaxCoilSize", comment = "Fusion Reactor Max Coil size (Radius)") @Config(config = "machines", category = "fusion_reactor", key = "FusionReactorMaxCoilSize", comment = "Fusion Reactor Max Coil size (Radius)")
public static int fusionControlComputerMaxCoilSize = 50; public static int fusionControlComputerMaxCoilSize = 50;
@Config(config = "machines", category = "recycler", key = "RecyclerInput", comment = "Recycler Max Input (Value in EU)") @Config(config = "machines", category = "recycler", key = "RecyclerInput", comment = "Recycler Max Input")
public static int recyclerMaxInput = 32; public static int recyclerMaxInput = 32;
@Config(config = "machines", category = "recycler", key = "RecyclerMaxEnergy", comment = "Recycler Max Energy (Value in EU)") @Config(config = "machines", category = "recycler", key = "RecyclerMaxEnergy", comment = "Recycler Max Energy")
public static int recyclerMaxEnergy = 1000; public static int recyclerMaxEnergy = 1000;
@Config(config = "machines", category = "recycler", key = "RecyclerChance", comment = "Recycler Chance to produce scrap (1 out of chance)")
public static int recyclerChance = 6;
@Config(config = "machines", category = "recycler", key = "RecyclerBlacklist", comment = "Recycler blacklist") @Config(config = "machines", category = "recycler", key = "RecyclerBlacklist", comment = "Recycler blacklist")
public static List<String> recyclerBlackList = Arrays.asList("techreborn:scrap_box", "techreborn:scrap"); public static List<String> recyclerBlackList = Arrays.asList("techreborn:scrap_box", "techreborn:scrap");

View file

@ -0,0 +1,15 @@
{
"type": "techreborn:recycler",
"power": 2,
"time": 25,
"ingredients": [
{
"item": "minecraft:air"
}
],
"results": [
{
"item": "techreborn:scrap"
}
]
}