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

110 lines
3.5 KiB
Java
Raw Normal View History

/*
* 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
*/
package de.ellpeck.actuallyadditions.mod.tile;
import de.ellpeck.actuallyadditions.api.misc.IDisplayStandItem;
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
import net.minecraft.block.Block;
2021-02-27 16:33:00 +01:00
import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
2021-02-26 22:15:48 +01:00
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.util.Direction;
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;
2019-05-02 09:10:29 +02:00
public class TileEntityDisplayStand extends TileEntityInventoryBase implements IEnergyDisplay {
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);
private int oldEnergy;
2019-05-02 09:10:29 +02:00
public TileEntityDisplayStand() {
2021-02-27 16:33:00 +01:00
super(ActuallyTiles.DISPLAYSTAND_TILE.get(), 1);
}
@Override
2019-05-02 09:10:29 +02:00
public void updateEntity() {
super.updateEntity();
2019-05-02 09:10:29 +02:00
if (!this.world.isRemote) {
if (StackUtil.isValid(this.inv.getStackInSlot(0)) && !this.isRedstonePowered) {
IDisplayStandItem item = this.convertToDisplayStandItem(this.inv.getStackInSlot(0).getItem());
2019-05-02 09:10:29 +02:00
if (item != null) {
int energy = item.getUsePerTick(this.inv.getStackInSlot(0), this, this.ticksElapsed);
2019-05-02 09:10:29 +02:00
if (this.storage.getEnergyStored() >= energy) {
if (item.update(this.inv.getStackInSlot(0), this, this.ticksElapsed)) {
this.storage.extractEnergyInternal(energy, false);
}
}
}
}
2019-05-02 09:10:29 +02:00
if (this.oldEnergy != this.storage.getEnergyStored() && this.sendUpdateWithInterval()) {
this.oldEnergy = this.storage.getEnergyStored();
}
}
}
@Override
2019-05-02 09:10:29 +02:00
public boolean shouldSyncSlots() {
return true;
}
@Override
2021-02-26 22:15:48 +01:00
public void writeSyncableNBT(CompoundNBT compound, NBTType type) {
super.writeSyncableNBT(compound, type);
this.storage.writeToNBT(compound);
}
@Override
2021-02-26 22:15:48 +01:00
public void readSyncableNBT(CompoundNBT compound, NBTType type) {
super.readSyncableNBT(compound, type);
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) {
Block block = Block.getBlockFromItem(item);
2021-02-26 22:15:48 +01:00
if (block instanceof IDisplayStandItem) {
return (IDisplayStandItem) block;
}
}
return null;
}
@Override
2019-05-02 09:10:29 +02:00
public CustomEnergyStorage getEnergyStorage() {
return this.storage;
}
@Override
2019-05-02 09:10:29 +02:00
public boolean needsHoldShift() {
return false;
}
@Override
2019-05-02 09:10:29 +02:00
public int getMaxStackSize(int slot) {
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
public ItemStack getStack() {
return this.inv.getStackInSlot(0);
}
}