2016-08-03 04:01:47 +02:00
|
|
|
/*
|
|
|
|
* 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
|
2016-08-03 04:01:47 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
package de.ellpeck.actuallyadditions.mod.tile;
|
|
|
|
|
2018-06-24 02:44:47 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import javax.annotation.Nullable;
|
|
|
|
|
2016-08-03 04:01:47 +02:00
|
|
|
import de.ellpeck.actuallyadditions.api.ActuallyAdditionsAPI;
|
|
|
|
import de.ellpeck.actuallyadditions.api.recipe.EmpowererRecipe;
|
2016-11-16 20:31:16 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
|
2016-08-03 04:01:47 +02:00
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
import net.minecraft.tileentity.TileEntity;
|
|
|
|
import net.minecraft.util.EnumFacing;
|
|
|
|
import net.minecraft.util.EnumParticleTypes;
|
|
|
|
import net.minecraft.util.math.BlockPos;
|
|
|
|
import net.minecraft.world.WorldServer;
|
|
|
|
|
2018-06-24 02:44:47 +02:00
|
|
|
public class TileEntityEmpowerer extends TileEntityInventoryBase {
|
2016-08-03 04:01:47 +02:00
|
|
|
|
2016-12-09 21:36:43 +01:00
|
|
|
public int processTime;
|
2016-12-22 21:56:08 +01:00
|
|
|
public int recipeForRenderIndex = -1;
|
2016-12-09 21:36:43 +01:00
|
|
|
private int lastRecipe;
|
2016-08-03 04:01:47 +02:00
|
|
|
|
2018-06-24 02:44:47 +02:00
|
|
|
public TileEntityEmpowerer() {
|
2016-08-03 04:01:47 +02:00
|
|
|
super(1, "empowerer");
|
|
|
|
}
|
|
|
|
|
2018-06-24 02:44:47 +02:00
|
|
|
@Deprecated //Use findMatchingRecipe
|
|
|
|
public static List<EmpowererRecipe> getRecipesForInput(ItemStack input) {
|
2016-10-21 00:13:21 +02:00
|
|
|
List<EmpowererRecipe> recipesThatWork = new ArrayList<EmpowererRecipe>();
|
2018-06-24 02:44:47 +02:00
|
|
|
if (StackUtil.isValid(input)) {
|
|
|
|
for (EmpowererRecipe recipe : ActuallyAdditionsAPI.EMPOWERER_RECIPES) {
|
|
|
|
if (recipe.getInput().apply(input)) {
|
2016-10-21 00:13:21 +02:00
|
|
|
recipesThatWork.add(recipe);
|
2016-09-12 20:45:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-21 00:13:21 +02:00
|
|
|
return recipesThatWork;
|
2016-09-12 20:45:29 +02:00
|
|
|
}
|
2018-06-24 02:44:47 +02:00
|
|
|
|
|
|
|
public static boolean isPossibleInput(ItemStack stack) {
|
|
|
|
for(EmpowererRecipe r : ActuallyAdditionsAPI.EMPOWERER_RECIPES) if(r.getInput().apply(stack)) 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) {
|
|
|
|
if (r.matches(base, stand1, stand2, stand3, stand4)) return r;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2016-09-12 20:45:29 +02:00
|
|
|
|
2016-08-03 04:01:47 +02:00
|
|
|
@Override
|
2018-06-24 02:44:47 +02:00
|
|
|
public void updateEntity() {
|
2016-08-03 04:01:47 +02:00
|
|
|
super.updateEntity();
|
|
|
|
|
2018-06-24 02:44:47 +02:00
|
|
|
if (!this.world.isRemote) {
|
|
|
|
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);
|
2016-10-21 00:13:21 +02:00
|
|
|
|
2018-06-24 02:44:47 +02:00
|
|
|
this.processTime++;
|
|
|
|
boolean done = this.processTime >= recipe.getTime();
|
2016-10-21 00:13:21 +02:00
|
|
|
|
2018-06-24 02:44:47 +02:00
|
|
|
for (TileEntityDisplayStand stand : stands) {
|
|
|
|
stand.storage.extractEnergyInternal(recipe.getEnergyPerStand() / recipe.getTime(), false);
|
2016-08-03 04:01:47 +02:00
|
|
|
|
2018-06-24 02:44:47 +02:00
|
|
|
if (done) {
|
|
|
|
stand.inv.getStackInSlot(0).shrink(1);
|
2016-08-08 21:39:00 +02:00
|
|
|
}
|
2018-06-24 02:44:47 +02:00
|
|
|
}
|
2016-08-03 04:01:47 +02:00
|
|
|
|
2018-06-24 02:44:47 +02:00
|
|
|
if (this.processTime % 5 == 0 && this.world instanceof WorldServer) {
|
|
|
|
((WorldServer) this.world).spawnParticle(EnumParticleTypes.FIREWORKS_SPARK, false, this.pos.getX() + 0.5, this.pos.getY() + 1.1, this.pos.getZ() + 0.5, 2, 0, 0, 0, 0.1D);
|
|
|
|
}
|
2016-08-03 04:06:41 +02:00
|
|
|
|
2018-06-24 02:44:47 +02:00
|
|
|
if (done) {
|
|
|
|
((WorldServer) this.world).spawnParticle(EnumParticleTypes.END_ROD, false, this.pos.getX() + 0.5, this.pos.getY() + 1.1, this.pos.getZ() + 0.5, 100, 0, 0, 0, 0.25D);
|
2016-08-03 04:01:47 +02:00
|
|
|
|
2018-06-24 02:44:47 +02:00
|
|
|
this.inv.setStackInSlot(0, recipe.getOutput().copy());
|
|
|
|
this.markDirty();
|
2016-12-09 21:36:43 +01:00
|
|
|
|
2018-06-24 02:44:47 +02:00
|
|
|
this.processTime = 0;
|
|
|
|
this.recipeForRenderIndex = -1;
|
2016-08-03 04:01:47 +02:00
|
|
|
}
|
2018-06-24 02:44:47 +02:00
|
|
|
} else {
|
|
|
|
this.processTime = 0;
|
|
|
|
this.recipeForRenderIndex = -1;
|
2016-08-03 04:01:47 +02:00
|
|
|
}
|
2016-12-09 21:36:43 +01:00
|
|
|
|
2018-06-24 02:44:47 +02:00
|
|
|
if (this.lastRecipe != this.recipeForRenderIndex) {
|
|
|
|
this.lastRecipe = this.recipeForRenderIndex;
|
|
|
|
this.sendUpdate();
|
|
|
|
}
|
2016-08-03 04:01:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-24 02:44:47 +02:00
|
|
|
private TileEntityDisplayStand[] getNearbyStands() {
|
|
|
|
TileEntityDisplayStand[] stands = new TileEntityDisplayStand[4];
|
2016-08-03 04:01:47 +02:00
|
|
|
|
2018-06-24 02:44:47 +02:00
|
|
|
for (int i = 0; i < EnumFacing.HORIZONTALS.length; i++) {
|
2016-08-03 04:01:47 +02:00
|
|
|
EnumFacing facing = EnumFacing.HORIZONTALS[i];
|
2016-08-03 13:36:42 +02:00
|
|
|
BlockPos offset = this.pos.offset(facing, 3);
|
2016-11-26 21:32:27 +01:00
|
|
|
TileEntity tile = this.world.getTileEntity(offset);
|
2018-06-24 02:44:47 +02:00
|
|
|
if (tile instanceof TileEntityDisplayStand) stands[i] = (TileEntityDisplayStand) tile;
|
|
|
|
else return null;
|
2016-08-03 04:01:47 +02:00
|
|
|
}
|
|
|
|
|
2018-06-24 02:44:47 +02:00
|
|
|
return stands;
|
2016-08-03 04:01:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-06-24 02:44:47 +02:00
|
|
|
public void writeSyncableNBT(NBTTagCompound compound, NBTType type) {
|
2016-08-03 04:01:47 +02:00
|
|
|
super.writeSyncableNBT(compound, type);
|
2018-06-24 02:44:47 +02:00
|
|
|
if (type == NBTType.SAVE_TILE) {
|
2016-08-03 04:01:47 +02:00
|
|
|
compound.setInteger("ProcessTime", this.processTime);
|
|
|
|
}
|
2018-06-24 02:44:47 +02:00
|
|
|
if (type == NBTType.SYNC) {
|
2016-12-09 21:36:43 +01:00
|
|
|
compound.setInteger("RenderIndex", this.recipeForRenderIndex);
|
|
|
|
}
|
2016-08-03 04:01:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-06-24 02:44:47 +02:00
|
|
|
public void readSyncableNBT(NBTTagCompound compound, NBTType type) {
|
2016-08-03 04:01:47 +02:00
|
|
|
super.readSyncableNBT(compound, type);
|
2018-06-24 02:44:47 +02:00
|
|
|
if (type == NBTType.SAVE_TILE) {
|
2016-08-03 04:01:47 +02:00
|
|
|
this.processTime = compound.getInteger("ProcessTime");
|
|
|
|
}
|
2018-06-24 02:44:47 +02:00
|
|
|
if (type == NBTType.SYNC) {
|
2016-12-09 21:36:43 +01:00
|
|
|
this.recipeForRenderIndex = compound.getInteger("RenderIndex");
|
|
|
|
}
|
2016-08-03 04:01:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-06-24 02:44:47 +02:00
|
|
|
public boolean shouldSyncSlots() {
|
2016-08-03 04:01:47 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-06-24 02:44:47 +02:00
|
|
|
public boolean canInsert(int index, ItemStack stack, boolean automation) {
|
2018-06-23 01:39:30 +02:00
|
|
|
return !automation || !getRecipesForInput(stack).isEmpty();
|
2016-08-03 04:01:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-06-24 02:44:47 +02:00
|
|
|
public boolean canExtract(int index, ItemStack stack, boolean automation) {
|
2018-06-23 01:39:30 +02:00
|
|
|
return !automation || getRecipesForInput(stack).isEmpty();
|
2016-08-03 04:01:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-06-24 02:44:47 +02:00
|
|
|
public int getMaxStackSize(int slot) {
|
2016-08-03 04:01:47 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|