ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityCrusher.java

289 lines
12 KiB
Java
Raw Normal View History

2015-08-29 14:33:25 +02:00
/*
2016-05-16 22:52:27 +02:00
* This file ("TileEntityGrinder.java") is part of the Actually Additions mod for Minecraft.
2015-08-29 14:33:25 +02:00
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
2016-05-16 22:52:27 +02:00
* http://ellpeck.de/actaddlicense
2015-08-29 14:33:25 +02:00
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 Ellpeck
2015-08-29 14:33:25 +02:00
*/
2016-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.mod.tile;
2022-10-19 20:04:42 +02:00
import de.ellpeck.actuallyadditions.api.ActuallyAdditionsAPI;
import de.ellpeck.actuallyadditions.mod.AASounds;
2021-08-31 00:44:39 +02:00
import de.ellpeck.actuallyadditions.mod.blocks.ActuallyBlocks;
2021-10-24 18:49:48 +02:00
import de.ellpeck.actuallyadditions.mod.crafting.CrushingRecipe;
import de.ellpeck.actuallyadditions.mod.inventory.CrusherContainer;
import de.ellpeck.actuallyadditions.mod.network.gui.IButtonReactor;
2018-08-10 05:04:07 +02:00
import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IAcceptor;
import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IRemover;
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
2024-03-02 21:23:08 +01:00
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraftforge.common.util.LazyOptional;
2016-11-26 08:58:42 +01:00
import net.minecraftforge.energy.IEnergyStorage;
import javax.annotation.Nullable;
2022-10-19 20:04:42 +02:00
import java.util.Optional;
2024-03-02 21:23:08 +01:00
public class TileEntityCrusher extends TileEntityInventoryBase implements IButtonReactor, MenuProvider {
2015-05-20 22:39:43 +02:00
2015-10-03 10:16:18 +02:00
public static final int SLOT_INPUT_1 = 0;
public static final int SLOT_OUTPUT_1_1 = 1;
public static final int SLOT_OUTPUT_1_2 = 2;
public static final int SLOT_INPUT_2 = 3;
public static final int SLOT_OUTPUT_2_1 = 4;
public static final int SLOT_OUTPUT_2_2 = 5;
public static final int ENERGY_USE = 40;
2016-11-26 20:43:50 +01:00
public final CustomEnergyStorage storage = new CustomEnergyStorage(60000, 100, 0);
public final LazyOptional<IEnergyStorage> lazyEnergy = LazyOptional.of(() -> this.storage);
2015-10-03 10:16:18 +02:00
public int firstCrushTime;
public int secondCrushTime;
public boolean isDouble;
2016-09-12 20:45:29 +02:00
public boolean isAutoSplit;
private int lastEnergy;
2015-10-03 10:16:18 +02:00
private int lastFirstCrush;
private int lastSecondCrush;
private boolean lastAutoSplit;
private boolean lastCrushed;
2024-03-02 21:23:08 +01:00
public TileEntityCrusher(BlockEntityType<?> type, BlockPos pos, BlockState state, int slots) {
super(type, pos, state, slots);
2015-10-03 10:16:18 +02:00
}
2015-10-03 10:19:40 +02:00
2024-03-02 21:23:08 +01:00
public TileEntityCrusher(BlockPos pos, BlockState state) {
super(ActuallyBlocks.CRUSHER.getTileEntityType(), pos, state, 3);
2015-10-03 10:16:18 +02:00
this.isDouble = false;
}
2015-05-20 22:39:43 +02:00
@Override
2024-03-02 21:23:08 +01:00
public void writeSyncableNBT(CompoundTag compound, NBTType type) {
2018-08-10 05:04:07 +02:00
if (type != NBTType.SAVE_BLOCK) {
compound.putInt("FirstCrushTime", this.firstCrushTime);
compound.putInt("SecondCrushTime", this.secondCrushTime);
compound.putBoolean("IsAutoSplit", this.isAutoSplit);
}
this.storage.writeToNBT(compound);
super.writeSyncableNBT(compound, type);
}
@Override
2024-03-02 21:23:08 +01:00
public void readSyncableNBT(CompoundTag compound, NBTType type) {
2018-08-10 05:04:07 +02:00
if (type != NBTType.SAVE_BLOCK) {
this.firstCrushTime = compound.getInt("FirstCrushTime");
this.secondCrushTime = compound.getInt("SecondCrushTime");
this.isAutoSplit = compound.getBoolean("IsAutoSplit");
}
this.storage.readFromNBT(compound);
super.readSyncableNBT(compound, type);
}
2024-03-02 21:23:08 +01:00
public static <T extends BlockEntity> void clientTick(Level level, BlockPos pos, BlockState state, T t) {
if (t instanceof TileEntityCrusher tile) {
tile.clientTick();
}
}
public static <T extends BlockEntity> void serverTick(Level level, BlockPos pos, BlockState state, T t) {
if (t instanceof TileEntityCrusher tile) {
tile.serverTick();
if (tile.isDouble && tile.isAutoSplit) {
TileEntityPoweredFurnace.autoSplit(tile.inv, SLOT_INPUT_1, SLOT_INPUT_2);
}
boolean crushed = false;
2015-06-21 02:28:49 +02:00
2024-03-02 21:23:08 +01:00
boolean canCrushOnFirst = tile.canCrushOn(SLOT_INPUT_1, SLOT_OUTPUT_1_1, SLOT_OUTPUT_1_2);
boolean canCrushOnSecond = false;
2024-03-02 21:23:08 +01:00
if (tile.isDouble) {
canCrushOnSecond = tile.canCrushOn(SLOT_INPUT_2, SLOT_OUTPUT_2_1, SLOT_OUTPUT_2_2);
2015-10-03 10:16:18 +02:00
}
boolean shouldPlaySound = false;
2018-08-10 05:04:07 +02:00
if (canCrushOnFirst) {
2024-03-02 21:23:08 +01:00
if (tile.storage.getEnergyStored() >= ENERGY_USE) {
if (tile.firstCrushTime % 20 == 0) {
shouldPlaySound = true;
}
2024-03-02 21:23:08 +01:00
tile.firstCrushTime++;
if (tile.firstCrushTime >= tile.getMaxCrushTime()) {
tile.finishCrushing(SLOT_INPUT_1, SLOT_OUTPUT_1_1, SLOT_OUTPUT_1_2);
tile.firstCrushTime = 0;
}
2024-03-02 21:23:08 +01:00
tile.storage.extractEnergyInternal(ENERGY_USE, false);
}
2024-03-02 21:23:08 +01:00
crushed = tile.storage.getEnergyStored() >= ENERGY_USE;
2018-08-10 05:04:07 +02:00
} else {
2024-03-02 21:23:08 +01:00
tile.firstCrushTime = 0;
2015-10-02 16:48:01 +02:00
}
2024-03-02 21:23:08 +01:00
if (tile.isDouble) {
2018-08-10 05:04:07 +02:00
if (canCrushOnSecond) {
2024-03-02 21:23:08 +01:00
if (tile.storage.getEnergyStored() >= ENERGY_USE) {
if (tile.secondCrushTime % 20 == 0) {
shouldPlaySound = true;
}
2024-03-02 21:23:08 +01:00
tile.secondCrushTime++;
if (tile.secondCrushTime >= tile.getMaxCrushTime()) {
tile.finishCrushing(SLOT_INPUT_2, SLOT_OUTPUT_2_1, SLOT_OUTPUT_2_2);
tile.secondCrushTime = 0;
}
2024-03-02 21:23:08 +01:00
tile.storage.extractEnergyInternal(ENERGY_USE, false);
}
2024-03-02 21:23:08 +01:00
crushed = tile.storage.getEnergyStored() >= ENERGY_USE;
2018-08-10 05:04:07 +02:00
} else {
2024-03-02 21:23:08 +01:00
tile.secondCrushTime = 0;
2015-10-02 16:48:01 +02:00
}
}
2024-03-02 21:23:08 +01:00
boolean current = state.getValue(BlockStateProperties.LIT);
2019-03-03 23:09:59 +01:00
boolean changeTo = current;
2024-03-02 21:23:08 +01:00
if (tile.lastCrushed != crushed) {
2021-02-26 22:15:48 +01:00
changeTo = crushed;
}
2024-03-02 21:23:08 +01:00
if (tile.isRedstonePowered) {
2021-02-26 22:15:48 +01:00
changeTo = true;
}
2024-03-02 21:23:08 +01:00
if (!crushed && !tile.isRedstonePowered) {
2021-02-26 22:15:48 +01:00
changeTo = false;
}
2019-03-03 23:09:59 +01:00
if (changeTo != current) {
2024-03-02 21:23:08 +01:00
level.setBlockAndUpdate(pos, state.setValue(BlockStateProperties.LIT, changeTo));
}
2024-03-02 21:23:08 +01:00
tile.lastCrushed = crushed;
2019-03-03 23:09:59 +01:00
2024-03-02 21:23:08 +01:00
if ((tile.lastEnergy != tile.storage.getEnergyStored() || tile.lastFirstCrush != tile.firstCrushTime || tile.lastSecondCrush != tile.secondCrushTime || tile.isAutoSplit != tile.lastAutoSplit) && tile.sendUpdateWithInterval()) {
tile.lastEnergy = tile.storage.getEnergyStored();
tile.lastFirstCrush = tile.firstCrushTime;
tile.lastSecondCrush = tile.secondCrushTime;
tile.lastAutoSplit = tile.isAutoSplit;
}
2018-08-10 05:04:07 +02:00
if (shouldPlaySound) {
2024-03-02 21:23:08 +01:00
level.playSound(null, pos.getX(), pos.getY(), pos.getZ(), AASounds.CRUSHER.get(), SoundSource.BLOCKS, 0.025F, 1.0F);
}
}
}
2016-02-01 17:49:55 +01:00
@Override
2018-08-10 05:04:07 +02:00
public IAcceptor getAcceptor() {
2021-11-21 17:31:57 +01:00
return (slot, stack, automation) -> !automation || (slot == SLOT_INPUT_1 || slot == SLOT_INPUT_2); /*CrusherRecipeRegistry.getRecipeFromInput(stack) != null*/ //TODO
2018-08-10 05:04:07 +02:00
}
@Override
public IRemover getRemover() {
2019-02-27 19:53:05 +01:00
return (slot, automation) -> !automation || slot == SLOT_OUTPUT_1_1 || slot == SLOT_OUTPUT_1_2 || slot == SLOT_OUTPUT_2_1 || slot == SLOT_OUTPUT_2_2;
2016-02-01 17:49:55 +01:00
}
2022-10-19 20:04:42 +02:00
public static Optional<CrushingRecipe> getRecipeForInput(ItemStack itemStack) {
return ActuallyAdditionsAPI.CRUSHER_RECIPES.stream().filter($ -> $.matches(itemStack)).findFirst();
}
2018-08-10 05:04:07 +02:00
public boolean canCrushOn(int theInput, int theFirstOutput, int theSecondOutput) {
2022-10-19 20:04:42 +02:00
ItemStack inputStack = this.inv.getStackInSlot(theInput);
if (!inputStack.isEmpty()) {
Optional<CrushingRecipe> recipeOpt = getRecipeForInput(inputStack);
if (!recipeOpt.isPresent()) {
2021-02-26 22:15:48 +01:00
return false;
}
2022-10-19 20:04:42 +02:00
CrushingRecipe recipe = recipeOpt.get();
ItemStack outputOne = recipe.getOutputOne();
ItemStack outputTwo = recipe.getOutputTwo();
2022-10-19 20:04:42 +02:00
if (!outputOne.isEmpty()) {
2024-03-03 01:20:53 +01:00
return (this.inv.getStackInSlot(theFirstOutput).isEmpty() || ItemStack.isSameItem(this.inv.getStackInSlot(theFirstOutput), outputOne) && this.inv.getStackInSlot(theFirstOutput).getCount() <= this.inv.getStackInSlot(theFirstOutput).getMaxStackSize() - outputOne.getCount()) && (outputTwo.isEmpty() || this.inv.getStackInSlot(theSecondOutput).isEmpty() || ItemStack.isSameItem(this.inv.getStackInSlot(theSecondOutput), outputTwo) && this.inv.getStackInSlot(theSecondOutput).getCount() <= this.inv.getStackInSlot(theSecondOutput).getMaxStackSize() - outputTwo.getCount());
}
}
return false;
}
2018-08-10 05:04:07 +02:00
private int getMaxCrushTime() {
2021-02-26 22:15:48 +01:00
return this.isDouble
? 150
: 100;
2015-10-03 10:19:40 +02:00
}
2018-08-10 05:04:07 +02:00
public void finishCrushing(int theInput, int theFirstOutput, int theSecondOutput) {
2022-10-19 20:04:42 +02:00
Optional<CrushingRecipe> recipeOpt = getRecipeForInput(this.inv.getStackInSlot(theInput));
if (!recipeOpt.isPresent()) {
2021-02-26 22:15:48 +01:00
return;
}
2022-10-19 20:04:42 +02:00
CrushingRecipe recipe = recipeOpt.get();
ItemStack outputOne = recipe.getOutputOne();
2022-10-19 20:04:42 +02:00
if (!outputOne.isEmpty()) {
if (this.inv.getStackInSlot(theFirstOutput).isEmpty()) {
this.inv.setStackInSlot(theFirstOutput, outputOne.copy());
2018-08-10 05:04:07 +02:00
} else if (this.inv.getStackInSlot(theFirstOutput).getItem() == outputOne.getItem()) {
this.inv.setStackInSlot(theFirstOutput, StackUtil.grow(this.inv.getStackInSlot(theFirstOutput), outputOne.getCount()));
2015-10-02 16:48:01 +02:00
}
2015-06-21 02:28:49 +02:00
}
ItemStack outputTwo = recipe.getOutputTwo();
2022-10-19 20:04:42 +02:00
if (!outputTwo.isEmpty()) {
float rand = this.level.random.nextFloat();
2018-08-10 05:04:07 +02:00
if (rand <= recipe.getSecondChance()) {
2022-10-19 20:04:42 +02:00
if (this.inv.getStackInSlot(theSecondOutput).isEmpty()) {
this.inv.setStackInSlot(theSecondOutput, outputTwo.copy());
2018-08-10 05:04:07 +02:00
} else if (this.inv.getStackInSlot(theSecondOutput).getItem() == outputTwo.getItem()) {
this.inv.setStackInSlot(theSecondOutput, StackUtil.grow(this.inv.getStackInSlot(theSecondOutput), outputTwo.getCount()));
2015-10-02 16:48:01 +02:00
}
}
}
2018-08-10 05:04:07 +02:00
this.inv.getStackInSlot(theInput).shrink(1);
}
2018-08-10 05:04:07 +02:00
public int getEnergyScaled(int i) {
return this.storage.getEnergyStored() * i / this.storage.getMaxEnergyStored();
}
2018-08-10 05:04:07 +02:00
public int getFirstTimeToScale(int i) {
return this.firstCrushTime * i / this.getMaxCrushTime();
}
2018-08-10 05:04:07 +02:00
public int getSecondTimeToScale(int i) {
return this.secondCrushTime * i / this.getMaxCrushTime();
2015-04-02 12:06:42 +02:00
}
2015-10-03 10:16:18 +02:00
@Override
2024-03-02 21:23:08 +01:00
public void onButtonPressed(int buttonID, Player player) {
2018-08-10 05:04:07 +02:00
if (buttonID == 0) {
this.isAutoSplit = !this.isAutoSplit;
2021-08-31 00:44:39 +02:00
this.setChanged();
}
}
2016-11-26 08:58:42 +01:00
@Override
public LazyOptional<IEnergyStorage> getEnergyStorage(Direction facing) {
return this.lazyEnergy;
2016-11-26 08:58:42 +01:00
}
@Override
2024-03-02 21:23:08 +01:00
public Component getDisplayName() {
2024-03-03 01:20:53 +01:00
return Component.translatable("container.actuallyadditions.crusher");
}
@Nullable
@Override
2024-03-02 21:23:08 +01:00
public AbstractContainerMenu createMenu(int windowId, Inventory playerInventory, Player player) {
return new CrusherContainer(windowId, playerInventory, this);
}
}