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;
|
|
|
|
|
|
|
|
import de.ellpeck.actuallyadditions.api.ActuallyAdditionsAPI;
|
2021-10-14 01:54:14 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.blocks.ActuallyBlocks;
|
|
|
|
import de.ellpeck.actuallyadditions.mod.crafting.ActuallyRecipes;
|
|
|
|
import de.ellpeck.actuallyadditions.mod.crafting.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;
|
2024-03-02 21:23:08 +01:00
|
|
|
import net.minecraft.core.BlockPos;
|
|
|
|
import net.minecraft.core.Direction;
|
|
|
|
import net.minecraft.core.particles.ParticleTypes;
|
|
|
|
import net.minecraft.nbt.CompoundTag;
|
|
|
|
import net.minecraft.resources.ResourceLocation;
|
|
|
|
import net.minecraft.server.level.ServerLevel;
|
|
|
|
import net.minecraft.world.item.ItemStack;
|
2024-03-04 20:21:48 +01:00
|
|
|
import net.minecraft.world.item.crafting.RecipeHolder;
|
2024-03-02 21:23:08 +01:00
|
|
|
import net.minecraft.world.level.Level;
|
|
|
|
import net.minecraft.world.level.block.entity.BlockEntity;
|
|
|
|
import net.minecraft.world.level.block.state.BlockState;
|
2024-03-04 20:21:48 +01:00
|
|
|
import net.neoforged.neoforge.server.ServerLifecycleHooks;
|
2016-08-03 04:01:47 +02:00
|
|
|
|
2021-02-26 22:15:48 +01:00
|
|
|
import javax.annotation.Nullable;
|
2021-08-22 17:09:06 +02:00
|
|
|
|
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;
|
2024-03-04 20:21:48 +01:00
|
|
|
private RecipeHolder<EmpowererRecipe> currentRecipe = null;
|
|
|
|
private RecipeHolder<EmpowererRecipe> lastRecipe = null;
|
2022-04-02 00:51:55 +02:00
|
|
|
|
2024-03-04 20:21:48 +01:00
|
|
|
public RecipeHolder<EmpowererRecipe> getCurrentRecipe(){
|
2022-04-02 00:51:55 +02:00
|
|
|
return this.currentRecipe;
|
|
|
|
}
|
2016-08-03 04:01:47 +02:00
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
public TileEntityEmpowerer(BlockPos pos, BlockState state) {
|
|
|
|
super(ActuallyBlocks.EMPOWERER.getTileEntityType(), pos, state, 1);
|
2016-08-03 04:01:47 +02:00
|
|
|
}
|
|
|
|
|
2018-06-24 02:44:47 +02:00
|
|
|
public static boolean isPossibleInput(ItemStack stack) {
|
2024-03-04 20:21:48 +01:00
|
|
|
for (RecipeHolder<EmpowererRecipe> r : ServerLifecycleHooks.getCurrentServer().getRecipeManager().getAllRecipesFor(ActuallyRecipes.Types.EMPOWERING.get())) {
|
|
|
|
if (r.value().getInput().test(stack)) {
|
2021-02-26 22:15:48 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2018-06-24 02:44:47 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
2024-03-04 20:21:48 +01:00
|
|
|
public static RecipeHolder<EmpowererRecipe> findMatchingRecipe(ItemStack base, ItemStack stand1, ItemStack stand2, ItemStack stand3, ItemStack stand4) {
|
|
|
|
for (RecipeHolder<EmpowererRecipe> r : ServerLifecycleHooks.getCurrentServer().getRecipeManager().getAllRecipesFor(ActuallyRecipes.Types.EMPOWERING.get())) {
|
|
|
|
if (r.value().matches(base, stand1, stand2, stand3, stand4)) {
|
2021-02-26 22:15:48 +01:00
|
|
|
return r;
|
|
|
|
}
|
2018-06-24 02:44:47 +02:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2016-09-12 20:45:29 +02:00
|
|
|
|
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 TileEntityEmpowerer tile) {
|
|
|
|
tile.clientTick();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static <T extends BlockEntity> void serverTick(Level level, BlockPos pos, BlockState state, T t) {
|
|
|
|
if (t instanceof TileEntityEmpowerer tile) {
|
|
|
|
tile.serverTick();
|
2016-08-03 04:01:47 +02:00
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
TileEntityDisplayStand[] stands = tile.getNearbyStands();
|
2018-06-24 02:44:47 +02:00
|
|
|
if (stands != null) {
|
2024-03-04 20:21:48 +01:00
|
|
|
RecipeHolder<EmpowererRecipe> holder = findMatchingRecipe(tile.inv.getStackInSlot(0), stands[0].getStack(), stands[1].getStack(), stands[2].getStack(), stands[3].getStack());
|
|
|
|
if (holder != null) {
|
|
|
|
tile.currentRecipe = holder;
|
|
|
|
EmpowererRecipe recipe = holder.value();
|
2016-10-21 00:13:21 +02:00
|
|
|
|
2018-08-12 16:44:35 +02:00
|
|
|
boolean hasPower = true;
|
2016-10-21 00:13:21 +02:00
|
|
|
|
2018-06-24 02:44:47 +02:00
|
|
|
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
|
|
|
}
|
2016-08-03 04:01:47 +02:00
|
|
|
|
2018-08-12 16:44:35 +02:00
|
|
|
if (hasPower) {
|
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
tile.processTime++;
|
|
|
|
boolean done = tile.processTime >= recipe.getTime();
|
2018-08-12 16:44:35 +02:00
|
|
|
|
|
|
|
for (TileEntityDisplayStand stand : stands) {
|
|
|
|
stand.storage.extractEnergyInternal(recipe.getEnergyPerStand() / recipe.getTime(), false);
|
|
|
|
|
|
|
|
if (done) {
|
|
|
|
stand.inv.getStackInSlot(0).shrink(1);
|
2021-08-22 17:09:06 +02:00
|
|
|
stand.setChanged();
|
2018-08-12 16:44:35 +02:00
|
|
|
}
|
2016-08-08 21:39:00 +02:00
|
|
|
}
|
2016-08-03 04:01:47 +02:00
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
if (tile.processTime % 5 == 0 && level instanceof ServerLevel) {
|
|
|
|
((ServerLevel) level).sendParticles(ParticleTypes.FIREWORK, pos.getX() + 0.5, pos.getY() + 1.1, pos.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) {
|
2024-03-02 21:23:08 +01:00
|
|
|
((ServerLevel) level).sendParticles(ParticleTypes.END_ROD, pos.getX() + 0.5, pos.getY() + 1.1, pos.getZ() + 0.5, 100, 0, 0, 0, 0.25D);
|
2016-08-03 04:01:47 +02:00
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
tile.inv.setStackInSlot(0, recipe.getOutput().copy());
|
|
|
|
tile.setChanged();
|
2016-12-09 21:36:43 +01:00
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
tile.processTime = 0;
|
|
|
|
tile.currentRecipe = null;
|
2018-08-12 16:44:35 +02:00
|
|
|
}
|
2016-08-03 04:01:47 +02:00
|
|
|
}
|
2018-06-24 02:44:47 +02:00
|
|
|
} else {
|
2024-03-02 21:23:08 +01:00
|
|
|
tile.processTime = 0;
|
|
|
|
tile.currentRecipe = null;
|
2016-08-03 04:01:47 +02:00
|
|
|
}
|
2016-12-09 21:36:43 +01:00
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
if (tile.lastRecipe != tile.currentRecipe) {
|
|
|
|
tile.lastRecipe = tile.currentRecipe;
|
|
|
|
tile.sendUpdate();
|
2018-06-24 02:44:47 +02:00
|
|
|
}
|
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
|
|
|
|
2022-04-02 00:51:55 +02:00
|
|
|
for (int i = 0; i <= 3; i++) {
|
2021-08-22 17:09:06 +02:00
|
|
|
Direction facing = Direction.from2DDataValue(i);
|
|
|
|
BlockPos offset = this.worldPosition.relative(facing, 3);
|
2024-03-02 21:23:08 +01:00
|
|
|
BlockEntity tile = this.level.getBlockEntity(offset);
|
2021-02-26 22:15:48 +01: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
|
2024-03-02 21:23:08 +01:00
|
|
|
public void writeSyncableNBT(CompoundTag 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) {
|
2021-02-27 16:33:00 +01:00
|
|
|
compound.putInt("ProcessTime", this.processTime);
|
2016-08-03 04:01:47 +02:00
|
|
|
}
|
2018-06-24 02:44:47 +02:00
|
|
|
if (type == NBTType.SYNC) {
|
2022-04-02 00:51:55 +02:00
|
|
|
if (this.currentRecipe != null)
|
2024-03-04 20:21:48 +01:00
|
|
|
compound.putString("CurrentRecipe", this.currentRecipe.id().toString());
|
2022-04-02 00:51:55 +02:00
|
|
|
else
|
|
|
|
compound.putString("CurrentRecipe", "");
|
2016-12-09 21:36:43 +01:00
|
|
|
}
|
2016-08-03 04:01:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public void readSyncableNBT(CompoundTag 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) {
|
2021-02-27 16:33:00 +01:00
|
|
|
this.processTime = compound.getInt("ProcessTime");
|
2016-08-03 04:01:47 +02:00
|
|
|
}
|
2022-04-02 00:51:55 +02:00
|
|
|
if (type == NBTType.SYNC && compound.contains("CurrentRecipe")) {
|
2024-03-03 01:20:53 +01:00
|
|
|
if (!compound.getString("CurrentRecipe").isEmpty()) {
|
2022-04-02 00:51:55 +02:00
|
|
|
ResourceLocation id = new ResourceLocation(compound.getString("CurrentRecipe"));
|
2024-03-04 20:21:48 +01:00
|
|
|
for (RecipeHolder<EmpowererRecipe> empowererRecipe : ActuallyAdditionsAPI.EMPOWERER_RECIPES) {
|
|
|
|
if (empowererRecipe.id().equals(id)) {
|
2022-04-02 00:51:55 +02:00
|
|
|
this.currentRecipe = empowererRecipe;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
this.currentRecipe = null;
|
2016-12-09 21:36:43 +01:00
|
|
|
}
|
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-08-10 05:04:07 +02:00
|
|
|
public IAcceptor getAcceptor() {
|
|
|
|
return (slot, stack, automation) -> !automation || isPossibleInput(stack);
|
2016-08-03 04:01:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@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));
|
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;
|
|
|
|
}
|
|
|
|
}
|