NaturesAura/src/main/java/de/ellpeck/naturesaura/blocks/tiles/TileEntityEnderCrate.java

134 lines
4.1 KiB
Java
Raw Normal View History

2019-02-17 22:51:05 +01:00
package de.ellpeck.naturesaura.blocks.tiles;
2019-02-18 19:30:35 +01:00
import de.ellpeck.naturesaura.NaturesAura;
2019-02-18 13:00:54 +01:00
import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk;
2019-02-17 22:51:05 +01:00
import de.ellpeck.naturesaura.api.misc.IWorldData;
2019-02-18 19:30:35 +01:00
import de.ellpeck.naturesaura.blocks.BlockEnderCrate;
2019-10-20 22:30:49 +02:00
import net.minecraft.block.BlockState;
2019-02-17 22:51:05 +01:00
import net.minecraft.item.ItemStack;
2019-10-20 22:30:49 +02:00
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.util.Direction;
2019-02-18 13:00:54 +01:00
import net.minecraft.util.math.BlockPos;
2019-02-17 22:51:05 +01:00
import net.minecraftforge.items.IItemHandlerModifiable;
2019-02-18 13:00:54 +01:00
import javax.annotation.Nonnull;
2019-02-17 22:51:05 +01:00
public class TileEntityEnderCrate extends TileEntityImpl {
2019-02-18 13:00:54 +01:00
private final IItemHandlerModifiable wrappedEnderStorage = new IItemHandlerModifiable() {
@Override
public void setStackInSlot(int slot, @Nonnull ItemStack stack) {
this.getStorage().setStackInSlot(slot, stack);
}
@Override
public int getSlots() {
return this.getStorage().getSlots();
}
@Nonnull
@Override
public ItemStack getStackInSlot(int slot) {
return this.getStorage().getStackInSlot(slot);
}
@Nonnull
@Override
public ItemStack insertItem(int slot, @Nonnull ItemStack stack, boolean simulate) {
ItemStack remain = this.getStorage().insertItem(slot, stack, simulate);
if (!simulate)
2019-02-18 19:30:35 +01:00
TileEntityEnderCrate.this.drainAura((stack.getCount() - remain.getCount()) * 500);
2019-02-18 13:00:54 +01:00
return remain;
}
@Nonnull
@Override
public ItemStack extractItem(int slot, int amount, boolean simulate) {
ItemStack extracted = this.getStorage().extractItem(slot, amount, simulate);
if (!simulate)
2019-02-18 19:30:35 +01:00
TileEntityEnderCrate.this.drainAura(extracted.getCount() * 500);
2019-02-18 13:00:54 +01:00
return extracted;
}
@Override
public int getSlotLimit(int slot) {
return this.getStorage().getSlotLimit(slot);
}
2020-01-21 23:02:39 +01:00
@Override
public boolean isItemValid(int slot, @Nonnull ItemStack stack) {
return this.getStorage().isItemValid(slot, stack);
}
2019-02-18 13:00:54 +01:00
private IItemHandlerModifiable getStorage() {
return IWorldData.getOverworldData(TileEntityEnderCrate.this.world).getEnderStorage(TileEntityEnderCrate.this.name);
}
};
2019-02-17 22:51:05 +01:00
public String name;
2020-01-22 01:32:26 +01:00
public TileEntityEnderCrate() {
super(ModTileEntities.ENDER_CRATE);
2020-01-21 23:02:39 +01:00
}
2019-02-17 22:51:05 +01:00
@Override
2019-10-20 22:30:49 +02:00
public IItemHandlerModifiable getItemHandler(Direction facing) {
2019-02-17 22:51:05 +01:00
if (this.canOpen())
2019-02-18 13:00:54 +01:00
return this.wrappedEnderStorage;
2019-02-17 22:51:05 +01:00
return null;
}
public boolean canOpen() {
return this.name != null;
}
@Override
public void dropInventory() {
}
@Override
2019-10-20 22:30:49 +02:00
public ItemStack getDrop(BlockState state, int fortune) {
2019-02-17 22:51:05 +01:00
ItemStack drop = super.getDrop(state, fortune);
2019-02-18 19:30:35 +01:00
if (this.name != null) {
2020-01-21 23:02:39 +01:00
if (!drop.hasTag())
drop.setTag(new CompoundNBT());
drop.getTag().putString(NaturesAura.MOD_ID + ":ender_name", this.name);
2019-02-18 19:30:35 +01:00
}
2019-02-17 22:51:05 +01:00
return drop;
}
@Override
public void loadDataOnPlace(ItemStack stack) {
super.loadDataOnPlace(stack);
2019-02-18 19:30:35 +01:00
if (!this.world.isRemote) {
String name = BlockEnderCrate.getEnderName(stack);
if (name != null && !name.isEmpty())
this.name = name;
}
2019-02-17 22:51:05 +01:00
}
@Override
2019-10-20 22:30:49 +02:00
public void writeNBT(CompoundNBT compound, SaveType type) {
2019-02-17 22:51:05 +01:00
super.writeNBT(compound, type);
if (type != SaveType.BLOCK) {
if (this.name != null)
2020-01-21 23:02:39 +01:00
compound.putString("name", this.name);
2019-02-17 22:51:05 +01:00
}
}
@Override
2019-10-20 22:30:49 +02:00
public void readNBT(CompoundNBT compound, SaveType type) {
2019-02-17 22:51:05 +01:00
super.readNBT(compound, type);
if (type != SaveType.BLOCK) {
2020-01-21 23:02:39 +01:00
if (compound.contains("name"))
2019-02-17 22:51:05 +01:00
this.name = compound.getString("name");
}
}
2019-02-18 13:00:54 +01:00
public void drainAura(int amount) {
if (amount > 0) {
BlockPos spot = IAuraChunk.getHighestSpot(this.world, this.pos, 35, this.pos);
IAuraChunk.getAuraChunk(this.world, spot).drainAura(spot, amount);
}
}
2019-02-17 22:51:05 +01:00
}