2016-06-06 21:27:09 +02:00
|
|
|
/*
|
|
|
|
* This file ("TileEntityDisplayStand.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-06-06 21:27:09 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
package de.ellpeck.actuallyadditions.mod.tile;
|
|
|
|
|
|
|
|
import de.ellpeck.actuallyadditions.api.misc.IDisplayStandItem;
|
2021-08-31 00:44:39 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.blocks.ActuallyBlocks;
|
2016-11-16 20:31:16 +01:00
|
|
|
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.world.item.BlockItem;
|
|
|
|
import net.minecraft.world.item.Item;
|
|
|
|
import net.minecraft.world.item.ItemStack;
|
|
|
|
import net.minecraft.world.level.Level;
|
|
|
|
import net.minecraft.world.level.block.Block;
|
|
|
|
import net.minecraft.world.level.block.entity.BlockEntity;
|
|
|
|
import net.minecraft.world.level.block.state.BlockState;
|
2021-02-27 16:33:00 +01:00
|
|
|
import net.minecraftforge.common.util.LazyOptional;
|
2016-11-26 08:58:42 +01:00
|
|
|
import net.minecraftforge.energy.IEnergyStorage;
|
2016-06-06 21:27:09 +02:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public class TileEntityDisplayStand extends TileEntityInventoryBase implements IEnergyDisplay {
|
2016-06-06 21:27:09 +02:00
|
|
|
|
2016-11-26 20:43:50 +01:00
|
|
|
public final CustomEnergyStorage storage = new CustomEnergyStorage(80000, 1000, 0);
|
2021-02-27 16:33:00 +01:00
|
|
|
public final LazyOptional<IEnergyStorage> lazyEnergy = LazyOptional.of(() -> this.storage);
|
2016-06-06 21:27:09 +02:00
|
|
|
private int oldEnergy;
|
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
public TileEntityDisplayStand(BlockPos pos, BlockState state) {
|
|
|
|
super(ActuallyBlocks.DISPLAY_STAND.getTileEntityType(), pos, state, 1);
|
2016-06-06 21:27:09 +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 TileEntityDisplayStand tile) {
|
|
|
|
tile.clientTick();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static <T extends BlockEntity> void serverTick(Level level, BlockPos pos, BlockState state, T t) {
|
|
|
|
if (t instanceof TileEntityDisplayStand tile) {
|
|
|
|
tile.serverTick();
|
2016-06-06 21:27:09 +02:00
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
if (StackUtil.isValid(tile.inv.getStackInSlot(0)) && !tile.isRedstonePowered) {
|
|
|
|
IDisplayStandItem item = tile.convertToDisplayStandItem(tile.inv.getStackInSlot(0).getItem());
|
2019-05-02 09:10:29 +02:00
|
|
|
if (item != null) {
|
2024-03-02 21:23:08 +01:00
|
|
|
int energy = item.getUsePerTick(tile.inv.getStackInSlot(0), tile, tile.ticksElapsed);
|
|
|
|
if (tile.storage.getEnergyStored() >= energy) {
|
|
|
|
if (item.update(tile.inv.getStackInSlot(0), tile, tile.ticksElapsed)) {
|
|
|
|
tile.storage.extractEnergyInternal(energy, false);
|
2016-06-06 21:27:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
if (tile.oldEnergy != tile.storage.getEnergyStored() && tile.sendUpdateWithInterval()) {
|
|
|
|
tile.oldEnergy = tile.storage.getEnergyStored();
|
2016-06-06 21:27:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public boolean shouldSyncSlots() {
|
2016-06-06 21:27:09 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public void writeSyncableNBT(CompoundTag compound, NBTType type) {
|
2016-07-02 15:01:46 +02:00
|
|
|
super.writeSyncableNBT(compound, type);
|
2016-06-06 21:27:09 +02:00
|
|
|
this.storage.writeToNBT(compound);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public void readSyncableNBT(CompoundTag compound, NBTType type) {
|
2016-07-02 15:01:46 +02:00
|
|
|
super.readSyncableNBT(compound, type);
|
2016-06-06 21:27:09 +02:00
|
|
|
this.storage.readFromNBT(compound);
|
|
|
|
}
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
private IDisplayStandItem convertToDisplayStandItem(Item item) {
|
|
|
|
if (item instanceof IDisplayStandItem) {
|
|
|
|
return (IDisplayStandItem) item;
|
2021-02-27 16:33:00 +01:00
|
|
|
} else if (item instanceof BlockItem) {
|
2021-08-22 17:09:06 +02:00
|
|
|
Block block = Block.byItem(item);
|
2021-02-26 22:15:48 +01:00
|
|
|
if (block instanceof IDisplayStandItem) {
|
|
|
|
return (IDisplayStandItem) block;
|
|
|
|
}
|
2016-06-06 21:27:09 +02:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public CustomEnergyStorage getEnergyStorage() {
|
2016-07-21 13:22:55 +02:00
|
|
|
return this.storage;
|
2016-06-06 21:27:09 +02:00
|
|
|
}
|
|
|
|
|
2016-07-02 18:39:47 +02:00
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public boolean needsHoldShift() {
|
2016-08-31 16:56:32 +02:00
|
|
|
return false;
|
2016-07-02 18:39:47 +02:00
|
|
|
}
|
|
|
|
|
2016-06-06 21:27:09 +02:00
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public int getMaxStackSize(int slot) {
|
2016-06-06 21:27:09 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2016-11-26 08:58:42 +01:00
|
|
|
|
|
|
|
@Override
|
2021-02-27 16:33:00 +01:00
|
|
|
public LazyOptional<IEnergyStorage> getEnergyStorage(Direction facing) {
|
|
|
|
return this.lazyEnergy;
|
2016-11-26 08:58:42 +01:00
|
|
|
}
|
2019-02-27 19:53:05 +01:00
|
|
|
|
2018-06-24 02:44:47 +02:00
|
|
|
public ItemStack getStack() {
|
|
|
|
return this.inv.getStackInSlot(0);
|
|
|
|
}
|
2016-06-06 21:27:09 +02:00
|
|
|
}
|