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

134 lines
4.4 KiB
Java
Raw Normal View History

2016-06-05 04:05:37 +02:00
package de.ellpeck.actuallyadditions.mod.tile;
import java.util.UUID;
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
2016-06-05 04:05:37 +02:00
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.energy.CapabilityEnergy;
2016-11-26 08:58:42 +01:00
import net.minecraftforge.energy.IEnergyStorage;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.wrapper.PlayerInvWrapper;
2016-06-05 04:05:37 +02:00
2019-05-02 09:10:29 +02:00
public class TileEntityPlayerInterface extends TileEntityBase implements IEnergyDisplay {
2016-06-05 04:05:37 +02:00
public static final int DEFAULT_RANGE = 32;
2016-11-26 20:43:50 +01:00
private final CustomEnergyStorage storage = new CustomEnergyStorage(30000, 50, 0);
2016-07-07 17:59:45 +02:00
public UUID connectedPlayer;
2016-12-18 17:50:31 +01:00
public String playerName;
private IItemHandler playerHandler;
private EntityPlayer oldPlayer;
private int oldEnergy;
2016-06-05 04:05:37 +02:00
private int range;
2019-05-02 09:10:29 +02:00
public TileEntityPlayerInterface() {
super("playerInterface");
2016-06-05 04:05:37 +02:00
}
2019-05-02 09:10:29 +02:00
private EntityPlayer getPlayer() {
if (this.connectedPlayer != null) {
2016-11-26 21:32:27 +01:00
EntityPlayer player = this.world.getPlayerEntityByUUID(this.connectedPlayer);
2019-05-02 09:10:29 +02:00
if (player != null) {
if (player.getDistance(this.pos.getX(), this.pos.getY(), this.pos.getZ()) <= this.range) { return player; }
2016-06-05 04:05:37 +02:00
}
}
return null;
}
@Override
2019-05-02 09:10:29 +02:00
public IItemHandler getItemHandler(EnumFacing facing) {
EntityPlayer player = this.getPlayer();
2019-05-02 09:10:29 +02:00
if (this.oldPlayer != player) {
this.oldPlayer = player;
this.playerHandler = player == null ? null : new PlayerInvWrapper(player.inventory);
}
return this.playerHandler;
}
2016-06-05 04:05:37 +02:00
@Override
2019-05-02 09:10:29 +02:00
public void updateEntity() {
2016-06-05 04:05:37 +02:00
super.updateEntity();
2019-05-02 09:10:29 +02:00
if (!this.world.isRemote) {
2016-06-05 04:05:37 +02:00
boolean changed = false;
2016-11-26 21:32:27 +01:00
this.range = TileEntityPhantomface.upgradeRange(DEFAULT_RANGE, this.world, this.pos);
2016-06-05 04:05:37 +02:00
EntityPlayer player = this.getPlayer();
2019-05-02 09:10:29 +02:00
if (player != null) {
for (int i = 0; i < player.inventory.getSizeInventory(); i++) {
if (this.storage.getEnergyStored() > 0) {
2016-06-05 04:05:37 +02:00
ItemStack slot = player.inventory.getStackInSlot(i);
2019-05-02 09:10:29 +02:00
if (StackUtil.isValid(slot) && slot.getCount() == 1) {
int received = 0;
2019-05-02 09:10:29 +02:00
if (slot.hasCapability(CapabilityEnergy.ENERGY, null)) {
IEnergyStorage cap = slot.getCapability(CapabilityEnergy.ENERGY, null);
2019-05-02 09:10:29 +02:00
if (cap != null) {
received = cap.receiveEnergy(this.storage.getEnergyStored(), false);
}
}
2019-05-02 09:10:29 +02:00
if (received > 0) {
this.storage.extractEnergyInternal(received, false);
}
2016-06-05 04:05:37 +02:00
}
2019-05-02 09:10:29 +02:00
} else {
2016-06-05 04:05:37 +02:00
break;
}
}
}
2019-05-02 09:10:29 +02:00
if (changed) {
2016-06-05 04:05:37 +02:00
this.markDirty();
this.sendUpdate();
}
2019-05-02 09:10:29 +02:00
if (this.storage.getEnergyStored() != this.oldEnergy && this.sendUpdateWithInterval()) {
this.oldEnergy = this.storage.getEnergyStored();
}
2016-06-05 04:05:37 +02:00
}
}
@Override
2019-05-02 09:10:29 +02:00
public void writeSyncableNBT(NBTTagCompound compound, NBTType type) {
2016-10-21 00:03:58 +02:00
super.writeSyncableNBT(compound, type);
2016-06-05 04:05:37 +02:00
this.storage.writeToNBT(compound);
2019-05-02 09:10:29 +02:00
if (this.connectedPlayer != null && type != NBTType.SAVE_BLOCK) {
2016-06-05 04:05:37 +02:00
compound.setUniqueId("Player", this.connectedPlayer);
compound.setString("PlayerName", this.playerName);
2016-06-05 04:05:37 +02:00
}
}
@Override
2019-05-02 09:10:29 +02:00
public void readSyncableNBT(NBTTagCompound compound, NBTType type) {
2016-10-21 00:03:58 +02:00
super.readSyncableNBT(compound, type);
2016-06-05 04:05:37 +02:00
this.storage.readFromNBT(compound);
2019-05-02 09:10:29 +02:00
if (compound.hasKey("PlayerLeast") && type != NBTType.SAVE_BLOCK) {
2016-06-05 04:05:37 +02:00
this.connectedPlayer = compound.getUniqueId("Player");
this.playerName = compound.getString("PlayerName");
2016-06-05 04:05:37 +02:00
}
}
@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;
}
2016-11-26 08:58:42 +01:00
@Override
2019-05-02 09:10:29 +02:00
public IEnergyStorage getEnergyStorage(EnumFacing facing) {
2016-11-26 08:58:42 +01:00
return this.storage;
}
2016-06-05 04:05:37 +02:00
}