2016-06-05 04:05:37 +02:00
|
|
|
/*
|
|
|
|
* This file ("TileEntityPlayerInterface.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
|
|
|
|
*
|
|
|
|
* © 2015-2016 Ellpeck
|
|
|
|
*/
|
|
|
|
|
|
|
|
package de.ellpeck.actuallyadditions.mod.tile;
|
|
|
|
|
|
|
|
import cofh.api.energy.EnergyStorage;
|
|
|
|
import cofh.api.energy.IEnergyContainerItem;
|
2016-07-25 02:07:16 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
|
|
|
|
import de.ellpeck.actuallyadditions.mod.util.compat.TeslaUtil;
|
|
|
|
import net.darkhax.tesla.api.ITeslaConsumer;
|
2016-06-05 04:05:37 +02:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
2016-07-25 02:07:16 +02:00
|
|
|
import net.minecraft.item.Item;
|
2016-06-05 04:05:37 +02:00
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
import net.minecraft.util.EnumFacing;
|
|
|
|
|
|
|
|
import java.util.UUID;
|
|
|
|
|
2016-08-31 20:16:36 +02:00
|
|
|
public class TileEntityPlayerInterface extends TileEntityInventoryBase implements ICustomEnergyReceiver, IEnergyDisplay{
|
2016-06-05 04:05:37 +02:00
|
|
|
|
|
|
|
public static final int DEFAULT_RANGE = 32;
|
2016-06-17 23:50:38 +02:00
|
|
|
private final EnergyStorage storage = new EnergyStorage(30000);
|
2016-07-07 17:59:45 +02:00
|
|
|
public UUID connectedPlayer;
|
2016-07-14 02:11:41 +02:00
|
|
|
public String playerName;
|
2016-07-06 21:56:15 +02:00
|
|
|
private int oldEnergy;
|
2016-06-05 04:05:37 +02:00
|
|
|
private int range;
|
|
|
|
|
|
|
|
public TileEntityPlayerInterface(){
|
|
|
|
super(0, "playerInterface");
|
|
|
|
}
|
|
|
|
|
|
|
|
private EntityPlayer getPlayer(){
|
|
|
|
if(this.connectedPlayer != null){
|
|
|
|
EntityPlayer player = this.worldObj.getPlayerEntityByUUID(this.connectedPlayer);
|
|
|
|
if(player != null){
|
|
|
|
if(player.getDistance(this.pos.getX(), this.pos.getY(), this.pos.getZ()) <= this.range){
|
|
|
|
return player;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void updateEntity(){
|
|
|
|
super.updateEntity();
|
|
|
|
if(!this.worldObj.isRemote){
|
|
|
|
boolean changed = false;
|
|
|
|
|
|
|
|
this.range = TileEntityPhantomface.upgradeRange(DEFAULT_RANGE, this.worldObj, this.pos);
|
|
|
|
|
|
|
|
EntityPlayer player = this.getPlayer();
|
|
|
|
if(player != null){
|
|
|
|
for(int i = 0; i < player.inventory.getSizeInventory(); i++){
|
|
|
|
if(this.storage.getEnergyStored() > 0){
|
|
|
|
ItemStack slot = player.inventory.getStackInSlot(i);
|
2016-07-25 02:07:16 +02:00
|
|
|
if(slot != null){
|
|
|
|
Item item = slot.getItem();
|
|
|
|
|
|
|
|
int received = 0;
|
|
|
|
if(item instanceof IEnergyContainerItem){
|
|
|
|
received = ((IEnergyContainerItem)item).receiveEnergy(slot, this.storage.getEnergyStored(), false);
|
|
|
|
}
|
|
|
|
else if(ActuallyAdditions.teslaLoaded && slot.hasCapability(TeslaUtil.teslaConsumer, null)){
|
|
|
|
ITeslaConsumer cap = slot.getCapability(TeslaUtil.teslaConsumer, null);
|
|
|
|
if(cap != null){
|
|
|
|
received = (int)cap.givePower(this.storage.getEnergyStored(), false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(received > 0){
|
|
|
|
this.storage.extractEnergy(received, false);
|
|
|
|
}
|
2016-06-05 04:05:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(changed){
|
|
|
|
this.markDirty();
|
|
|
|
this.sendUpdate();
|
|
|
|
}
|
2016-07-06 21:56:15 +02:00
|
|
|
|
|
|
|
if(this.storage.getEnergyStored() != this.oldEnergy && this.sendUpdateWithInterval()){
|
|
|
|
this.oldEnergy = this.storage.getEnergyStored();
|
|
|
|
}
|
2016-06-05 04:05:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-07-02 15:01:46 +02:00
|
|
|
public void writeSyncableNBT(NBTTagCompound compound, NBTType type){
|
2016-06-05 04:05:37 +02:00
|
|
|
this.storage.writeToNBT(compound);
|
2016-07-02 15:01:46 +02:00
|
|
|
if(this.connectedPlayer != null && type != NBTType.SAVE_BLOCK){
|
2016-06-05 04:05:37 +02:00
|
|
|
compound.setUniqueId("Player", this.connectedPlayer);
|
2016-07-14 02:11:41 +02:00
|
|
|
compound.setString("PlayerName", this.playerName);
|
2016-06-05 04:05:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-07-02 15:01:46 +02:00
|
|
|
public void readSyncableNBT(NBTTagCompound compound, NBTType type){
|
2016-06-05 04:05:37 +02:00
|
|
|
this.storage.readFromNBT(compound);
|
2016-07-02 15:01:46 +02:00
|
|
|
if(compound.hasKey("PlayerLeast") && type != NBTType.SAVE_BLOCK){
|
2016-06-05 04:05:37 +02:00
|
|
|
this.connectedPlayer = compound.getUniqueId("Player");
|
2016-07-14 02:11:41 +02:00
|
|
|
this.playerName = compound.getString("PlayerName");
|
2016-06-05 04:05:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int receiveEnergy(EnumFacing from, int maxReceive, boolean simulate){
|
|
|
|
return this.storage.receiveEnergy(maxReceive, simulate);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getEnergyStored(EnumFacing from){
|
|
|
|
return this.storage.getEnergyStored();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getMaxEnergyStored(EnumFacing from){
|
|
|
|
return this.storage.getMaxEnergyStored();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canConnectEnergy(EnumFacing from){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int[] getSlotsForFace(EnumFacing side){
|
|
|
|
if(this.getPlayer() != null){
|
|
|
|
int[] theInt = new int[this.getSizeInventory()];
|
|
|
|
for(int i = 0; i < theInt.length; i++){
|
|
|
|
theInt[i] = i;
|
|
|
|
}
|
|
|
|
return theInt;
|
|
|
|
}
|
|
|
|
return new int[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getInventoryStackLimit(){
|
|
|
|
EntityPlayer player = this.getPlayer();
|
|
|
|
return player != null ? player.inventory.getInventoryStackLimit() : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void clear(){
|
|
|
|
EntityPlayer player = this.getPlayer();
|
|
|
|
if(player != null){
|
|
|
|
player.inventory.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setInventorySlotContents(int i, ItemStack stack){
|
|
|
|
EntityPlayer player = this.getPlayer();
|
|
|
|
if(player != null){
|
|
|
|
player.inventory.setInventorySlotContents(i, stack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getSizeInventory(){
|
|
|
|
EntityPlayer player = this.getPlayer();
|
|
|
|
if(player != null){
|
|
|
|
return player.inventory.getSizeInventory();
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ItemStack getStackInSlot(int i){
|
|
|
|
EntityPlayer player = this.getPlayer();
|
|
|
|
if(player != null){
|
|
|
|
return player.inventory.getStackInSlot(i);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ItemStack decrStackSize(int i, int j){
|
|
|
|
EntityPlayer player = this.getPlayer();
|
|
|
|
if(player != null){
|
|
|
|
ItemStack stack = player.inventory.decrStackSize(i, j);
|
|
|
|
if(stack != null){
|
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ItemStack removeStackFromSlot(int index){
|
|
|
|
EntityPlayer player = this.getPlayer();
|
|
|
|
if(player != null){
|
|
|
|
ItemStack stack = player.inventory.removeStackFromSlot(index);
|
|
|
|
if(stack != null){
|
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isItemValidForSlot(int i, ItemStack stack){
|
|
|
|
EntityPlayer player = this.getPlayer();
|
|
|
|
return player != null && player.inventory.isItemValidForSlot(i, stack);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canInsertItem(int slot, ItemStack stack, EnumFacing side){
|
|
|
|
return this.isItemValidForSlot(slot, stack);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canExtractItem(int slot, ItemStack stack, EnumFacing side){
|
|
|
|
return true;
|
|
|
|
}
|
2016-06-08 18:57:57 +02:00
|
|
|
|
|
|
|
@Override
|
2016-07-21 13:22:55 +02:00
|
|
|
public EnergyStorage getEnergyStorage(){
|
|
|
|
return this.storage;
|
2016-06-08 18:57:57 +02:00
|
|
|
}
|
2016-07-02 18:39:47 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean needsHoldShift(){
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-05 04:05:37 +02:00
|
|
|
}
|