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

196 lines
7 KiB
Java
Raw Normal View History

/*
* This file ("TileEntityEmpowerer.java") is part of the Actually Additions mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
* http://ellpeck.de/actaddlicense
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 Ellpeck
*/
package de.ellpeck.actuallyadditions.mod.tile;
import de.ellpeck.actuallyadditions.api.ActuallyAdditionsAPI;
import de.ellpeck.actuallyadditions.api.recipe.EmpowererRecipe;
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;
import net.minecraft.item.ItemStack;
2021-02-26 22:15:48 +01:00
import net.minecraft.nbt.CompoundNBT;
2021-02-27 16:33:00 +01:00
import net.minecraft.particles.ParticleTypes;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Direction;
import net.minecraft.util.math.BlockPos;
2021-02-27 16:33:00 +01:00
import net.minecraft.world.server.ServerWorld;
2021-02-26 22:15:48 +01:00
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import de.ellpeck.actuallyadditions.mod.tile.TileEntityBase.NBTType;
public class TileEntityEmpowerer extends TileEntityInventoryBase {
public int processTime;
2016-12-22 21:56:08 +01:00
public int recipeForRenderIndex = -1;
private int lastRecipe;
public TileEntityEmpowerer() {
2021-02-27 16:33:00 +01:00
super(ActuallyTiles.EMPOWERER_TILE.get(), 1);
}
@Deprecated //Use findMatchingRecipe
public static List<EmpowererRecipe> getRecipesForInput(ItemStack input) {
2019-02-27 19:53:05 +01:00
List<EmpowererRecipe> recipesThatWork = new ArrayList<>();
if (StackUtil.isValid(input)) {
2021-02-27 16:33:00 +01:00
// TODO: [port] VALIDATOR OR REMOVE
for (EmpowererRecipe recipe : ActuallyAdditionsAPI.EMPOWERER_RECIPES) {
2021-02-27 16:33:00 +01:00
if (recipe.getInput().test(input)) {
recipesThatWork.add(recipe);
2016-09-12 20:45:29 +02:00
}
}
}
return recipesThatWork;
2016-09-12 20:45:29 +02:00
}
2018-08-10 05:04:07 +02:00
public static boolean isPossibleInput(ItemStack stack) {
2021-02-26 22:15:48 +01:00
for (EmpowererRecipe r : ActuallyAdditionsAPI.EMPOWERER_RECIPES) {
2021-02-27 16:33:00 +01:00
// TODO: [port] move to proper recipe system
if (r.getInput().test(stack)) {
2021-02-26 22:15:48 +01:00
return true;
}
}
return false;
}
@Nullable
public static EmpowererRecipe findMatchingRecipe(ItemStack base, ItemStack stand1, ItemStack stand2, ItemStack stand3, ItemStack stand4) {
for (EmpowererRecipe r : ActuallyAdditionsAPI.EMPOWERER_RECIPES) {
2021-02-26 22:15:48 +01:00
if (r.matches(base, stand1, stand2, stand3, stand4)) {
return r;
}
}
return null;
}
2016-09-12 20:45:29 +02:00
@Override
public void updateEntity() {
super.updateEntity();
if (!this.level.isClientSide) {
TileEntityDisplayStand[] stands = this.getNearbyStands();
if (stands != null) {
EmpowererRecipe recipe = findMatchingRecipe(this.inv.getStackInSlot(0), stands[0].getStack(), stands[1].getStack(), stands[2].getStack(), stands[3].getStack());
if (recipe != null) {
this.recipeForRenderIndex = ActuallyAdditionsAPI.EMPOWERER_RECIPES.indexOf(recipe);
2018-08-12 16:44:35 +02:00
boolean hasPower = true;
for (TileEntityDisplayStand stand : stands) {
2021-02-26 22:15:48 +01:00
if (stand.storage.getEnergyStored() < recipe.getEnergyPerStand() / recipe.getTime()) {
hasPower = false;
}
2018-08-12 16:44:35 +02:00
}
2018-08-12 16:44:35 +02:00
if (hasPower) {
this.processTime++;
boolean done = this.processTime >= recipe.getTime();
for (TileEntityDisplayStand stand : stands) {
stand.storage.extractEnergyInternal(recipe.getEnergyPerStand() / recipe.getTime(), false);
if (done) {
stand.inv.getStackInSlot(0).shrink(1);
stand.setChanged();
2018-08-12 16:44:35 +02:00
}
}
if (this.processTime % 5 == 0 && this.level instanceof ServerWorld) {
((ServerWorld) this.level).sendParticles(ParticleTypes.FIREWORK, this.worldPosition.getX() + 0.5, this.worldPosition.getY() + 1.1, this.worldPosition.getZ() + 0.5, 2, 0, 0, 0, 0.1D);
2018-08-12 16:44:35 +02:00
}
2016-08-03 04:06:41 +02:00
2018-08-12 16:44:35 +02:00
if (done) {
((ServerWorld) this.level).sendParticles(ParticleTypes.END_ROD, this.worldPosition.getX() + 0.5, this.worldPosition.getY() + 1.1, this.worldPosition.getZ() + 0.5, 100, 0, 0, 0, 0.25D);
2018-08-12 16:44:35 +02:00
this.inv.setStackInSlot(0, recipe.getOutput().copy());
this.setChanged();
2018-08-12 16:44:35 +02:00
this.processTime = 0;
this.recipeForRenderIndex = -1;
}
}
} else {
this.processTime = 0;
this.recipeForRenderIndex = -1;
}
if (this.lastRecipe != this.recipeForRenderIndex) {
this.lastRecipe = this.recipeForRenderIndex;
this.sendUpdate();
}
}
}
}
private TileEntityDisplayStand[] getNearbyStands() {
TileEntityDisplayStand[] stands = new TileEntityDisplayStand[4];
2021-02-27 16:33:00 +01:00
// TODO: [port] validate this
for (int i = 0; i < 3; i++) {
Direction facing = Direction.from2DDataValue(i);
BlockPos offset = this.worldPosition.relative(facing, 3);
TileEntity tile = this.level.getBlockEntity(offset);
2021-02-26 22:15:48 +01:00
if (tile instanceof TileEntityDisplayStand) {
stands[i] = (TileEntityDisplayStand) tile;
} else {
return null;
}
}
return stands;
}
@Override
2021-02-26 22:15:48 +01:00
public void writeSyncableNBT(CompoundNBT compound, NBTType type) {
super.writeSyncableNBT(compound, type);
if (type == NBTType.SAVE_TILE) {
2021-02-27 16:33:00 +01:00
compound.putInt("ProcessTime", this.processTime);
}
if (type == NBTType.SYNC) {
2021-02-27 16:33:00 +01:00
compound.putInt("RenderIndex", this.recipeForRenderIndex);
}
}
@Override
2021-02-26 22:15:48 +01:00
public void readSyncableNBT(CompoundNBT compound, NBTType type) {
super.readSyncableNBT(compound, type);
if (type == NBTType.SAVE_TILE) {
2021-02-27 16:33:00 +01:00
this.processTime = compound.getInt("ProcessTime");
}
if (type == NBTType.SYNC) {
2021-02-27 16:33:00 +01:00
this.recipeForRenderIndex = compound.getInt("RenderIndex");
}
}
@Override
public boolean shouldSyncSlots() {
return true;
}
@Override
2018-08-10 05:04:07 +02:00
public IAcceptor getAcceptor() {
return (slot, stack, automation) -> !automation || isPossibleInput(stack);
}
@Override
2018-08-10 05:04:07 +02:00
public IRemover getRemover() {
2019-02-27 19:53:05 +01:00
return (slot, automation) -> !automation || !isPossibleInput(this.inv.getStackInSlot(0));
}
@Override
public int getMaxStackSize(int slot) {
return 1;
}
}