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
|
|
|
|
*
|
2017-01-01 16:23:26 +01:00
|
|
|
* © 2015-2017 Ellpeck
|
2016-06-05 04:05:37 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
package de.ellpeck.actuallyadditions.mod.tile;
|
|
|
|
|
2016-11-16 20:31:16 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
|
2021-02-26 22:15:48 +01:00
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
2016-06-05 04:05:37 +02:00
|
|
|
import net.minecraft.item.ItemStack;
|
2021-02-26 22:15:48 +01:00
|
|
|
import net.minecraft.nbt.CompoundNBT;
|
2021-02-27 13:24:45 +01:00
|
|
|
import net.minecraft.util.Direction;
|
2021-02-27 16:33:00 +01:00
|
|
|
import net.minecraftforge.common.util.LazyOptional;
|
2016-11-26 19:00:02 +01:00
|
|
|
import net.minecraftforge.energy.CapabilityEnergy;
|
2016-11-26 08:58:42 +01:00
|
|
|
import net.minecraftforge.energy.IEnergyStorage;
|
2016-12-04 00:10:52 +01:00
|
|
|
import net.minecraftforge.items.IItemHandler;
|
|
|
|
import net.minecraftforge.items.wrapper.PlayerInvWrapper;
|
2016-06-05 04:05:37 +02:00
|
|
|
|
2021-02-26 22:15:48 +01:00
|
|
|
import java.util.UUID;
|
|
|
|
|
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);
|
2021-02-27 16:33:00 +01:00
|
|
|
public final LazyOptional<IEnergyStorage> lazyEnergy = LazyOptional.of(() -> this.storage);
|
2016-07-07 17:59:45 +02:00
|
|
|
public UUID connectedPlayer;
|
2016-12-18 17:50:31 +01:00
|
|
|
public String playerName;
|
2016-12-04 00:10:52 +01:00
|
|
|
private IItemHandler playerHandler;
|
2021-02-26 22:15:48 +01:00
|
|
|
private PlayerEntity oldPlayer;
|
2016-07-06 21:56:15 +02:00
|
|
|
private int oldEnergy;
|
2016-06-05 04:05:37 +02:00
|
|
|
private int range;
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public TileEntityPlayerInterface() {
|
2021-02-27 16:33:00 +01:00
|
|
|
super(ActuallyTiles.PLAYERINTERFACE_TILE.get());
|
2016-06-05 04:05:37 +02:00
|
|
|
}
|
|
|
|
|
2021-02-26 22:15:48 +01:00
|
|
|
private PlayerEntity getPlayer() {
|
2021-02-27 16:33:00 +01:00
|
|
|
if (this.connectedPlayer != null && this.world != null) {
|
|
|
|
PlayerEntity player = this.world.getPlayerByUuid(this.connectedPlayer);
|
2019-05-02 09:10:29 +02:00
|
|
|
if (player != null) {
|
2021-02-27 16:33:00 +01:00
|
|
|
if (player.getDistanceSq(this.pos.getX(), this.pos.getY(), this.pos.getZ()) <= this.range) {
|
2021-02-26 22:15:48 +01:00
|
|
|
return player;
|
|
|
|
}
|
2016-06-05 04:05:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-02-27 16:33:00 +01:00
|
|
|
// TODO: [port] this might not be a stable way of doing this.
|
2016-12-04 00:10:52 +01:00
|
|
|
@Override
|
2021-02-27 16:33:00 +01:00
|
|
|
public LazyOptional<IItemHandler> getItemHandler(Direction facing) {
|
2021-02-26 22:15:48 +01:00
|
|
|
PlayerEntity player = this.getPlayer();
|
2016-12-04 00:10:52 +01:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
if (this.oldPlayer != player) {
|
2016-12-04 00:10:52 +01:00
|
|
|
this.oldPlayer = player;
|
|
|
|
|
2021-02-26 22:15:48 +01:00
|
|
|
this.playerHandler = player == null
|
|
|
|
? null
|
|
|
|
: new PlayerInvWrapper(player.inventory);
|
2016-12-04 00:10:52 +01:00
|
|
|
}
|
|
|
|
|
2021-02-27 16:33:00 +01:00
|
|
|
return LazyOptional.of(() -> this.playerHandler);
|
2016-12-04 00:10:52 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2021-02-26 22:15:48 +01:00
|
|
|
PlayerEntity 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) {
|
2016-07-25 02:07:16 +02:00
|
|
|
|
2021-02-27 16:33:00 +01:00
|
|
|
int received = slot.getCapability(CapabilityEnergy.ENERGY).map(cap -> cap.receiveEnergy(this.storage.getEnergyStored(), false)).orElse(0);
|
2019-05-02 09:10:29 +02:00
|
|
|
if (received > 0) {
|
2016-11-23 18:10:55 +01:00
|
|
|
this.storage.extractEnergyInternal(received, false);
|
2016-07-25 02:07:16 +02:00
|
|
|
}
|
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();
|
|
|
|
}
|
2016-07-06 21:56:15 +02:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
if (this.storage.getEnergyStored() != this.oldEnergy && this.sendUpdateWithInterval()) {
|
2016-07-06 21:56:15 +02:00
|
|
|
this.oldEnergy = this.storage.getEnergyStored();
|
|
|
|
}
|
2016-06-05 04:05:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-02-26 22:15:48 +01:00
|
|
|
public void writeSyncableNBT(CompoundNBT 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) {
|
2021-02-27 16:33:00 +01:00
|
|
|
compound.putUniqueId("Player", this.connectedPlayer);
|
|
|
|
compound.putString("PlayerName", this.playerName);
|
2016-06-05 04:05:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-02-26 22:15:48 +01:00
|
|
|
public void readSyncableNBT(CompoundNBT 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);
|
2021-02-27 16:33:00 +01:00
|
|
|
if (compound.contains("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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-08 18:57:57 +02:00
|
|
|
@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-08 18:57:57 +02:00
|
|
|
}
|
2016-07-02 18:39:47 +02:00
|
|
|
|
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public boolean needsHoldShift() {
|
2016-07-02 18:39:47 +02:00
|
|
|
return false;
|
|
|
|
}
|
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
|
|
|
}
|
2016-06-05 04:05:37 +02:00
|
|
|
}
|