Moving to source sets
This commit is contained in:
@@ -1,838 +0,0 @@
|
||||
/*
|
||||
* This file is part of Applied Energistics 2.
|
||||
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
|
||||
*
|
||||
* Applied Energistics 2 is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Applied Energistics 2 is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
|
||||
*/
|
||||
|
||||
package appeng.tile.storage;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import alexiil.mc.lib.attributes.fluid.volume.FluidVolume;
|
||||
import alexiil.mc.lib.attributes.item.FixedItemInv;
|
||||
import net.minecraft.block.BlockState;
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.screen.ScreenHandlerType;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.network.PacketByteBuf;
|
||||
import net.minecraft.util.Tickable;
|
||||
import net.minecraft.block.entity.BlockEntityType;
|
||||
import net.minecraft.util.math.Direction;
|
||||
import net.minecraftforge.common.capabilities.Capability;
|
||||
import net.minecraftforge.common.util.LazyOptional;
|
||||
import net.minecraftforge.fluids.FluidAttributes;
|
||||
import net.minecraftforge.fluids.IFluidTank;
|
||||
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
|
||||
import net.minecraftforge.fluids.capability.IFluidHandler;
|
||||
|
||||
import appeng.api.AEApi;
|
||||
import appeng.api.config.AccessRestriction;
|
||||
import appeng.api.config.Actionable;
|
||||
import appeng.api.config.PowerMultiplier;
|
||||
import appeng.api.config.SecurityPermissions;
|
||||
import appeng.api.config.Settings;
|
||||
import appeng.api.config.SortDir;
|
||||
import appeng.api.config.SortOrder;
|
||||
import appeng.api.config.ViewItems;
|
||||
import appeng.api.implementations.tiles.IColorableTile;
|
||||
import appeng.api.implementations.tiles.IMEChest;
|
||||
import appeng.api.networking.GridFlags;
|
||||
import appeng.api.networking.IGrid;
|
||||
import appeng.api.networking.IGridNode;
|
||||
import appeng.api.networking.energy.IEnergyGrid;
|
||||
import appeng.api.networking.events.MENetworkCellArrayUpdate;
|
||||
import appeng.api.networking.events.MENetworkChannelsChanged;
|
||||
import appeng.api.networking.events.MENetworkEventSubscribe;
|
||||
import appeng.api.networking.events.MENetworkPowerStatusChange;
|
||||
import appeng.api.networking.events.MENetworkPowerStorage;
|
||||
import appeng.api.networking.events.MENetworkPowerStorage.PowerEventType;
|
||||
import appeng.api.networking.security.IActionHost;
|
||||
import appeng.api.networking.security.IActionSource;
|
||||
import appeng.api.networking.security.ISecurityGrid;
|
||||
import appeng.api.networking.storage.IBaseMonitor;
|
||||
import appeng.api.networking.storage.IStorageGrid;
|
||||
import appeng.api.storage.IMEInventoryHandler;
|
||||
import appeng.api.storage.IMEMonitor;
|
||||
import appeng.api.storage.IMEMonitorHandlerReceiver;
|
||||
import appeng.api.storage.IStorageChannel;
|
||||
import appeng.api.storage.IStorageMonitorable;
|
||||
import appeng.api.storage.IStorageMonitorableAccessor;
|
||||
import appeng.api.storage.ITerminalHost;
|
||||
import appeng.api.storage.cells.CellState;
|
||||
import appeng.api.storage.cells.ICellGuiHandler;
|
||||
import appeng.api.storage.cells.ICellHandler;
|
||||
import appeng.api.storage.cells.ICellInventory;
|
||||
import appeng.api.storage.cells.ICellInventoryHandler;
|
||||
import appeng.api.storage.channels.IFluidStorageChannel;
|
||||
import appeng.api.storage.channels.IItemStorageChannel;
|
||||
import appeng.api.storage.data.IAEFluidStack;
|
||||
import appeng.api.storage.data.IAEItemStack;
|
||||
import appeng.api.storage.data.IAEStack;
|
||||
import appeng.api.util.AEColor;
|
||||
import appeng.api.util.IConfigManager;
|
||||
import appeng.capabilities.Capabilities;
|
||||
import appeng.container.implementations.MEMonitorableContainer;
|
||||
import appeng.fluids.container.FluidTerminalContainer;
|
||||
import appeng.fluids.util.AEFluidStack;
|
||||
import appeng.helpers.IPriorityHost;
|
||||
import appeng.me.GridAccessException;
|
||||
import appeng.me.helpers.MEMonitorHandler;
|
||||
import appeng.me.helpers.MachineSource;
|
||||
import appeng.me.storage.MEInventoryHandler;
|
||||
import appeng.tile.grid.AENetworkPowerBlockEntity;
|
||||
import appeng.tile.inventory.AppEngInternalInventory;
|
||||
import appeng.util.ConfigManager;
|
||||
import appeng.util.IConfigManagerHost;
|
||||
import appeng.util.Platform;
|
||||
import appeng.util.helpers.ItemHandlerUtil;
|
||||
import appeng.util.inv.InvOperation;
|
||||
import appeng.util.inv.WrapperChainedItemHandler;
|
||||
import appeng.util.inv.filter.IAEItemFilter;
|
||||
import appeng.util.item.AEItemStack;
|
||||
|
||||
public class ChestBlockEntity extends AENetworkPowerBlockEntity
|
||||
implements IMEChest, ITerminalHost, IPriorityHost, IConfigManagerHost, IColorableTile, Tickable {
|
||||
private final AppEngInternalInventory inputInventory = new AppEngInternalInventory(this, 1);
|
||||
private final AppEngInternalInventory cellInventory = new AppEngInternalInventory(this, 1);
|
||||
private final FixedItemInv internalInventory = new WrapperChainedItemHandler(this.inputInventory,
|
||||
this.cellInventory);
|
||||
|
||||
private final IActionSource mySrc = new MachineSource(this);
|
||||
private final IConfigManager config = new ConfigManager(this);
|
||||
private long lastStateChange = 0;
|
||||
private int priority = 0;
|
||||
private int state = 0;
|
||||
private boolean wasActive = false;
|
||||
private AEColor paintedColor = AEColor.TRANSPARENT;
|
||||
private boolean isCached = false;
|
||||
private ChestMonitorHandler cellHandler;
|
||||
private Accessor accessor;
|
||||
private IFluidHandler fluidHandler;
|
||||
|
||||
public ChestBlockEntity(BlockEntityType<?> tileEntityTypeIn) {
|
||||
super(tileEntityTypeIn);
|
||||
this.setInternalMaxPower(PowerMultiplier.CONFIG.multiply(40));
|
||||
this.getProxy().setFlags(GridFlags.REQUIRE_CHANNEL);
|
||||
this.config.registerSetting(Settings.SORT_BY, SortOrder.NAME);
|
||||
this.config.registerSetting(Settings.VIEW_MODE, ViewItems.ALL);
|
||||
this.config.registerSetting(Settings.SORT_DIRECTION, SortDir.ASCENDING);
|
||||
|
||||
this.setInternalPublicPowerStorage(true);
|
||||
this.setInternalPowerFlow(AccessRestriction.WRITE);
|
||||
|
||||
this.inputInventory.setFilter(new InputInventoryFilter());
|
||||
this.cellInventory.setFilter(new CellInventoryFilter());
|
||||
}
|
||||
|
||||
public ItemStack getCell() {
|
||||
return this.cellInventory.getInvStack(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void PowerEvent(final PowerEventType x) {
|
||||
if (x == PowerEventType.REQUEST_POWER) {
|
||||
try {
|
||||
this.getProxy().getGrid().postEvent(new MENetworkPowerStorage(this, PowerEventType.REQUEST_POWER));
|
||||
} catch (final GridAccessException e) {
|
||||
// :(
|
||||
}
|
||||
} else {
|
||||
this.recalculateDisplay();
|
||||
}
|
||||
}
|
||||
|
||||
private void recalculateDisplay() {
|
||||
final int oldState = this.state;
|
||||
|
||||
for (int x = 0; x < this.getCellCount(); x++) {
|
||||
this.state |= (this.getCellStatus(x).ordinal() << (3 * x));
|
||||
}
|
||||
|
||||
if (this.isPowered()) {
|
||||
this.state |= 0x40;
|
||||
} else {
|
||||
this.state &= ~0x40;
|
||||
}
|
||||
|
||||
final boolean currentActive = this.getProxy().isActive();
|
||||
if (this.wasActive != currentActive) {
|
||||
this.wasActive = currentActive;
|
||||
try {
|
||||
this.getProxy().getGrid().postEvent(new MENetworkCellArrayUpdate());
|
||||
} catch (final GridAccessException e) {
|
||||
// :P
|
||||
}
|
||||
}
|
||||
|
||||
if (oldState != this.state) {
|
||||
this.markForUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCellCount() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void updateHandler() {
|
||||
if (!this.isCached) {
|
||||
this.cellHandler = null;
|
||||
this.accessor = null;
|
||||
this.fluidHandler = null;
|
||||
|
||||
final ItemStack is = this.getCell();
|
||||
if (!is.isEmpty()) {
|
||||
this.isCached = true;
|
||||
ICellHandler cellHandler = AEApi.instance().registries().cell().getHandler(is);
|
||||
if (cellHandler != null) {
|
||||
double power = 1.0;
|
||||
|
||||
for (IStorageChannel channel : AEApi.instance().storage().storageChannels()) {
|
||||
final ICellInventoryHandler<IAEItemStack> newCell = cellHandler.getCellInventory(is, this,
|
||||
channel);
|
||||
if (newCell != null) {
|
||||
power += cellHandler.cellIdleDrain(is, newCell);
|
||||
this.cellHandler = this.wrap(newCell);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
this.getProxy().setIdlePowerUsage(power);
|
||||
this.accessor = new Accessor();
|
||||
|
||||
if (this.cellHandler != null && this.cellHandler.getChannel() == AEApi.instance().storage()
|
||||
.getStorageChannel(IFluidStorageChannel.class)) {
|
||||
this.fluidHandler = new FluidHandler();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private <T extends IAEStack<T>> ChestMonitorHandler<T> wrap(final IMEInventoryHandler<T> h) {
|
||||
if (h == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final MEInventoryHandler<T> ih = new MEInventoryHandler<T>(h, h.getChannel());
|
||||
ih.setPriority(this.priority);
|
||||
|
||||
final ChestMonitorHandler<T> g = new ChestMonitorHandler<T>(ih);
|
||||
g.addListener(new ChestNetNotifier<T>(h.getChannel()), g);
|
||||
|
||||
return g;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CellState getCellStatus(final int slot) {
|
||||
if (isClient()) {
|
||||
return CellState.values()[(this.state >> (slot * 3)) & 3];
|
||||
}
|
||||
|
||||
this.updateHandler();
|
||||
|
||||
final ItemStack cell = this.getCell();
|
||||
final ICellHandler ch = AEApi.instance().registries().cell().getHandler(cell);
|
||||
|
||||
if (this.cellHandler != null && ch != null) {
|
||||
return ch.getStatusForCell(cell, this.cellHandler.getInternalHandler());
|
||||
}
|
||||
|
||||
return CellState.ABSENT;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public Item getCellItem(int slot) {
|
||||
if (slot != 0) {
|
||||
return null;
|
||||
}
|
||||
ItemStack cell = getCell();
|
||||
return cell.isEmpty() ? null : cell.getItem();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPowered() {
|
||||
if (isClient()) {
|
||||
return (this.state & 0x40) == 0x40;
|
||||
}
|
||||
|
||||
boolean gridPowered = this.getAECurrentPower() > 64;
|
||||
|
||||
if (!gridPowered) {
|
||||
try {
|
||||
gridPowered = this.getProxy().getEnergy().isNetworkPowered();
|
||||
} catch (final GridAccessException ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
return super.getAECurrentPower() > 1 || gridPowered;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCellBlinking(final int slot) {
|
||||
final long now = this.world.getGameTime();
|
||||
if (now - this.lastStateChange > 8) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return ((this.state >> (slot * 3 + 2)) & 0x01) == 0x01;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected double extractAEPower(final double amt, final Actionable mode) {
|
||||
double stash = 0.0;
|
||||
|
||||
try {
|
||||
final IEnergyGrid eg = this.getProxy().getEnergy();
|
||||
stash = eg.extractAEPower(amt, mode, PowerMultiplier.ONE);
|
||||
if (stash >= amt) {
|
||||
return stash;
|
||||
}
|
||||
} catch (final GridAccessException e) {
|
||||
// no grid :(
|
||||
}
|
||||
|
||||
// local battery!
|
||||
return super.extractAEPower(amt - stash, mode) + stash;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tick() {
|
||||
if (this.world.isClient) {
|
||||
return;
|
||||
}
|
||||
|
||||
final double idleUsage = this.getProxy().getIdlePowerUsage();
|
||||
|
||||
try {
|
||||
if (!this.getProxy().getEnergy().isNetworkPowered()) {
|
||||
final double powerUsed = this.extractAEPower(idleUsage, Actionable.MODULATE, PowerMultiplier.CONFIG); // drain
|
||||
if (powerUsed + 0.1 >= idleUsage != (this.state & 0x40) > 0) {
|
||||
this.recalculateDisplay();
|
||||
}
|
||||
}
|
||||
} catch (final GridAccessException e) {
|
||||
final double powerUsed = this.extractAEPower(this.getProxy().getIdlePowerUsage(), Actionable.MODULATE,
|
||||
PowerMultiplier.CONFIG); // drain
|
||||
if (powerUsed + 0.1 >= idleUsage != (this.state & 0x40) > 0) {
|
||||
this.recalculateDisplay();
|
||||
}
|
||||
}
|
||||
|
||||
if (!ItemHandlerUtil.isEmpty(this.inputInventory)) {
|
||||
this.tryToStoreContents();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void writeToStream(final PacketByteBuf data) throws IOException {
|
||||
super.writeToStream(data);
|
||||
|
||||
if (this.world.getGameTime() - this.lastStateChange > 8) {
|
||||
this.state = 0;
|
||||
} else {
|
||||
this.state &= 0x24924924; // just keep the blinks...
|
||||
}
|
||||
|
||||
for (int x = 0; x < this.getCellCount(); x++) {
|
||||
this.state |= (this.getCellStatus(x).ordinal() << (3 * x));
|
||||
}
|
||||
|
||||
if (this.isPowered()) {
|
||||
this.state |= 0x40;
|
||||
} else {
|
||||
this.state &= ~0x40;
|
||||
}
|
||||
|
||||
data.writeByte(this.state);
|
||||
data.writeByte(this.paintedColor.ordinal());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean readFromStream(final PacketByteBuf data) throws IOException {
|
||||
final boolean c = super.readFromStream(data);
|
||||
|
||||
final int oldState = this.state;
|
||||
|
||||
this.state = data.readByte();
|
||||
final AEColor oldPaintedColor = this.paintedColor;
|
||||
this.paintedColor = AEColor.values()[data.readByte()];
|
||||
|
||||
this.lastStateChange = this.world.getGameTime();
|
||||
|
||||
return oldPaintedColor != this.paintedColor || (this.state & 0xDB6DB6DB) != (oldState & 0xDB6DB6DB) || c;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromTag(BlockState state, final CompoundTag data) {
|
||||
super.fromTag(state, data);
|
||||
this.config.readFromNBT(data);
|
||||
this.priority = data.getInt("priority");
|
||||
if (data.contains("paintedColor")) {
|
||||
this.paintedColor = AEColor.values()[data.getByte("paintedColor")];
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompoundTag toTag(final CompoundTag data) {
|
||||
super.toTag(data);
|
||||
this.config.writeToNBT(data);
|
||||
data.putInt("priority", this.priority);
|
||||
data.putByte("paintedColor", (byte) this.paintedColor.ordinal());
|
||||
return data;
|
||||
}
|
||||
|
||||
@MENetworkEventSubscribe
|
||||
public void powerRender(final MENetworkPowerStatusChange c) {
|
||||
this.recalculateDisplay();
|
||||
}
|
||||
|
||||
@MENetworkEventSubscribe
|
||||
public void channelRender(final MENetworkChannelsChanged c) {
|
||||
this.recalculateDisplay();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public <T extends IAEStack<T>> IMEMonitor<T> getInventory(IStorageChannel<T> channel) {
|
||||
this.updateHandler();
|
||||
|
||||
if (this.cellHandler != null && this.cellHandler.getChannel() == channel) {
|
||||
return this.cellHandler;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FixedItemInv getInternalInventory() {
|
||||
return this.internalInventory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChangeInventory(final FixedItemInv inv, final int slot, final InvOperation mc,
|
||||
final ItemStack removed, final ItemStack added) {
|
||||
if (inv == this.cellInventory) {
|
||||
this.cellHandler = null;
|
||||
this.isCached = false; // recalculate the storage cell.
|
||||
|
||||
try {
|
||||
this.getProxy().getGrid().postEvent(new MENetworkCellArrayUpdate());
|
||||
final IStorageGrid gs = this.getProxy().getStorage();
|
||||
Platform.postChanges(gs, removed, added, this.mySrc);
|
||||
} catch (final GridAccessException ignored) {
|
||||
|
||||
}
|
||||
|
||||
// update the neighbors
|
||||
if (this.world != null) {
|
||||
Platform.notifyBlocksOfNeighbors(this.world, this.pos);
|
||||
this.markForUpdate();
|
||||
}
|
||||
}
|
||||
if (inv == this.inputInventory && mc == InvOperation.INSERT) {
|
||||
this.tryToStoreContents();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected FixedItemInv getItemHandlerForSide(@Nonnull Direction side) {
|
||||
if (side == this.getForward()) {
|
||||
return this.cellInventory;
|
||||
} else {
|
||||
return this.inputInventory;
|
||||
}
|
||||
}
|
||||
|
||||
private void tryToStoreContents() {
|
||||
if (!ItemHandlerUtil.isEmpty(this.inputInventory)) {
|
||||
this.updateHandler();
|
||||
|
||||
if (this.cellHandler != null && this.cellHandler.getChannel() == AEApi.instance().storage()
|
||||
.getStorageChannel(IItemStorageChannel.class)) {
|
||||
final IAEItemStack returns = Platform.poweredInsert(this, this.cellHandler,
|
||||
AEItemStack.fromItemStack(this.inputInventory.getInvStack(0)), this.mySrc);
|
||||
|
||||
if (returns == null) {
|
||||
this.inputInventory.setInvStack(0, ItemStack.EMPTY);
|
||||
} else {
|
||||
this.inputInventory.setInvStack(0, returns.createItemStack());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<IMEInventoryHandler> getCellArray(final IStorageChannel channel) {
|
||||
if (this.getProxy().isActive()) {
|
||||
this.updateHandler();
|
||||
|
||||
if (this.cellHandler != null && this.cellHandler.getChannel() == channel) {
|
||||
return Collections.singletonList(this.cellHandler);
|
||||
}
|
||||
}
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPriority() {
|
||||
return this.priority;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPriority(final int newValue) {
|
||||
this.priority = newValue;
|
||||
this.cellHandler = null;
|
||||
this.isCached = false; // recalculate the storage cell.
|
||||
|
||||
try {
|
||||
this.getProxy().getGrid().postEvent(new MENetworkCellArrayUpdate());
|
||||
} catch (final GridAccessException e) {
|
||||
// :P
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void blinkCell(final int slot) {
|
||||
final long now = this.world.getGameTime();
|
||||
if (now - this.lastStateChange > 8) {
|
||||
this.state = 0;
|
||||
}
|
||||
this.lastStateChange = now;
|
||||
|
||||
this.state |= 1 << (slot * 3 + 2);
|
||||
|
||||
this.recalculateDisplay();
|
||||
}
|
||||
|
||||
@Override
|
||||
public IConfigManager getConfigManager() {
|
||||
return this.config;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateSetting(final IConfigManager manager, final Settings settingName, final Enum<?> newValue) {
|
||||
|
||||
}
|
||||
|
||||
public boolean openGui(final PlayerEntity p) {
|
||||
this.updateHandler();
|
||||
if (this.cellHandler != null) {
|
||||
final ICellHandler ch = AEApi.instance().registries().cell().getHandler(this.getCell());
|
||||
|
||||
if (ch != null) {
|
||||
final ICellGuiHandler chg = AEApi.instance().registries().cell()
|
||||
.getGuiHandler(this.cellHandler.getChannel(), this.getCell());
|
||||
if (chg != null) {
|
||||
chg.openChestGui(p, this, ch, this.cellHandler, this.getCell(), this.cellHandler.getChannel());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AEColor getColor() {
|
||||
return this.paintedColor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean recolourBlock(final Direction side, final AEColor newPaintedColor, final PlayerEntity who) {
|
||||
if (this.paintedColor == newPaintedColor) {
|
||||
return false;
|
||||
}
|
||||
|
||||
this.paintedColor = newPaintedColor;
|
||||
this.saveChanges();
|
||||
this.markForUpdate();
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveChanges(final ICellInventory<?> cellInventory) {
|
||||
if (cellInventory != null) {
|
||||
cellInventory.persist();
|
||||
}
|
||||
this.world.markDirty(this.pos, this);
|
||||
}
|
||||
|
||||
private class ChestNetNotifier<T extends IAEStack<T>> implements IMEMonitorHandlerReceiver<T> {
|
||||
|
||||
private final IStorageChannel<T> chan;
|
||||
|
||||
public ChestNetNotifier(final IStorageChannel<T> chan) {
|
||||
this.chan = chan;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid(final Object verificationToken) {
|
||||
ChestBlockEntity.this.updateHandler();
|
||||
if (ChestBlockEntity.this.cellHandler != null
|
||||
&& this.chan == ChestBlockEntity.this.cellHandler.getChannel()) {
|
||||
return verificationToken == ChestBlockEntity.this.cellHandler;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postChange(final IBaseMonitor<T> monitor, final Iterable<T> change, final IActionSource source) {
|
||||
if (source == ChestBlockEntity.this.mySrc
|
||||
|| source.machine().map(machine -> machine == ChestBlockEntity.this).orElse(false)) {
|
||||
try {
|
||||
if (ChestBlockEntity.this.getProxy().isActive()) {
|
||||
ChestBlockEntity.this.getProxy().getStorage().postAlterationOfStoredItems(this.chan, change,
|
||||
ChestBlockEntity.this.mySrc);
|
||||
}
|
||||
} catch (final GridAccessException e) {
|
||||
// :(
|
||||
}
|
||||
}
|
||||
|
||||
ChestBlockEntity.this.blinkCell(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onListUpdate() {
|
||||
// not used here
|
||||
}
|
||||
}
|
||||
|
||||
private class ChestMonitorHandler<T extends IAEStack<T>> extends MEMonitorHandler<T> {
|
||||
|
||||
public ChestMonitorHandler(final IMEInventoryHandler<T> t) {
|
||||
super(t);
|
||||
}
|
||||
|
||||
private ICellInventoryHandler<T> getInternalHandler() {
|
||||
final IMEInventoryHandler<T> h = this.getHandler();
|
||||
if (h instanceof MEInventoryHandler) {
|
||||
return (ICellInventoryHandler<T>) ((MEInventoryHandler<T>) h).getInternal();
|
||||
}
|
||||
return (ICellInventoryHandler<T>) this.getHandler();
|
||||
}
|
||||
|
||||
@Override
|
||||
public T injectItems(final T input, final Actionable mode, final IActionSource src) {
|
||||
if (src.player().map(player -> !this.securityCheck(player, SecurityPermissions.INJECT)).orElse(false)) {
|
||||
return input;
|
||||
}
|
||||
return super.injectItems(input, mode, src);
|
||||
}
|
||||
|
||||
private boolean securityCheck(final PlayerEntity player, final SecurityPermissions requiredPermission) {
|
||||
if (ChestBlockEntity.this.getTile() instanceof IActionHost && requiredPermission != null) {
|
||||
|
||||
final IGridNode gn = ((IActionHost) ChestBlockEntity.this.getTile()).getActionableNode();
|
||||
if (gn != null) {
|
||||
final IGrid g = gn.getGrid();
|
||||
if (g != null) {
|
||||
final boolean requirePower = false;
|
||||
if (requirePower) {
|
||||
final IEnergyGrid eg = g.getCache(IEnergyGrid.class);
|
||||
if (!eg.isNetworkPowered()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
final ISecurityGrid sg = g.getCache(ISecurityGrid.class);
|
||||
if (sg.hasPermission(player, requiredPermission)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T extractItems(final T request, final Actionable mode, final IActionSource src) {
|
||||
if (src.player().map(player -> !this.securityCheck(player, SecurityPermissions.EXTRACT)).orElse(false)) {
|
||||
return null;
|
||||
}
|
||||
return super.extractItems(request, mode, src);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public <T> LazyOptional<T> getCapability(Capability<T> capability, @Nullable Direction facing) {
|
||||
this.updateHandler();
|
||||
if (capability == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY && this.fluidHandler != null
|
||||
&& facing != this.getForward()) {
|
||||
return (LazyOptional<T>) LazyOptional.of(() -> this.fluidHandler);
|
||||
}
|
||||
if (capability == Capabilities.STORAGE_MONITORABLE_ACCESSOR && this.accessor != null
|
||||
&& facing != this.getForward()) {
|
||||
return (LazyOptional<T>) LazyOptional.of(() -> this.accessor);
|
||||
}
|
||||
return super.getCapability(capability, facing);
|
||||
}
|
||||
|
||||
private class Accessor implements IStorageMonitorableAccessor {
|
||||
@Nullable
|
||||
@Override
|
||||
public IStorageMonitorable getInventory(IActionSource src) {
|
||||
if (Platform.canAccess(ChestBlockEntity.this.getProxy(), src)) {
|
||||
return ChestBlockEntity.this;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private class FluidHandler implements IFluidHandler, IFluidTank {
|
||||
|
||||
private boolean canAcceptLiquids() {
|
||||
return ChestBlockEntity.this.cellHandler != null && ChestBlockEntity.this.cellHandler.getChannel() == AEApi
|
||||
.instance().storage().getStorageChannel(IFluidStorageChannel.class);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public FluidVolume getFluid() {
|
||||
return FluidVolumeUtil.EMPTY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getFluidAmount() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCapacity() {
|
||||
return canAcceptLiquids() ? FluidAttributes.BUCKET_VOLUME : 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isFluidValid(FluidVolume stack) {
|
||||
return canAcceptLiquids();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTanks() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public FluidVolume getFluidInTank(int tank) {
|
||||
return FluidVolumeUtil.EMPTY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTankCapacity(int tank) {
|
||||
return tank == 0 ? FluidAttributes.BUCKET_VOLUME : 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isFluidValid(int tank, @Nonnull FluidVolume stack) {
|
||||
return tank == 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int fill(FluidVolume resource, FluidAction action) {
|
||||
ChestBlockEntity.this.updateHandler();
|
||||
if (canAcceptLiquids()) {
|
||||
final IAEFluidStack results = Platform.poweredInsert(ChestBlockEntity.this,
|
||||
ChestBlockEntity.this.cellHandler, AEFluidStack.fromFluidStack(resource),
|
||||
ChestBlockEntity.this.mySrc,
|
||||
action == FluidAction.EXECUTE ? Actionable.MODULATE : Actionable.SIMULATE);
|
||||
|
||||
if (results == null) {
|
||||
return resource.getAmount();
|
||||
}
|
||||
return resource.getAmount() - (int) results.getStackSize();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public FluidVolume drain(FluidVolume resource, FluidAction action) {
|
||||
return FluidVolumeUtil.EMPTY;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public FluidVolume drain(int maxDrain, FluidAction action) {
|
||||
return FluidVolumeUtil.EMPTY;
|
||||
}
|
||||
}
|
||||
|
||||
private class InputInventoryFilter implements IAEItemFilter {
|
||||
@Override
|
||||
public boolean allowExtract(FixedItemInv inv, int slot, int amount) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean allowInsert(FixedItemInv inv, int slot, ItemStack stack) {
|
||||
if (ChestBlockEntity.this.isPowered()) {
|
||||
ChestBlockEntity.this.updateHandler();
|
||||
return ChestBlockEntity.this.cellHandler != null && ChestBlockEntity.this.cellHandler
|
||||
.getChannel() == AEApi.instance().storage().getStorageChannel(IItemStorageChannel.class);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static class CellInventoryFilter implements IAEItemFilter {
|
||||
|
||||
@Override
|
||||
public boolean allowExtract(FixedItemInv inv, int slot, int amount) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean allowInsert(FixedItemInv inv, int slot, ItemStack stack) {
|
||||
return AEApi.instance().registries().cell().getHandler(stack) != null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack getItemStackRepresentation() {
|
||||
return AEApi.instance().definitions().blocks().chest().maybeStack(1).orElse(ItemStack.EMPTY);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScreenHandlerType<?> getContainerType() {
|
||||
this.updateHandler();
|
||||
if (this.cellHandler != null) {
|
||||
if (this.cellHandler.getChannel() == AEApi.instance().storage()
|
||||
.getStorageChannel(IItemStorageChannel.class)) {
|
||||
return MEMonitorableContainer.TYPE;
|
||||
}
|
||||
if (this.cellHandler.getChannel() == AEApi.instance().storage()
|
||||
.getStorageChannel(IFluidStorageChannel.class)) {
|
||||
return FluidTerminalContainer.TYPE;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,457 +0,0 @@
|
||||
/*
|
||||
* This file is part of Applied Energistics 2.
|
||||
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
|
||||
*
|
||||
* Applied Energistics 2 is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Applied Energistics 2 is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
|
||||
*/
|
||||
|
||||
package appeng.tile.storage;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.EnumSet;
|
||||
import java.util.IdentityHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import alexiil.mc.lib.attributes.item.FixedItemInv;
|
||||
import net.minecraft.block.BlockState;
|
||||
import net.minecraft.block.entity.BlockEntityType;
|
||||
import net.minecraft.screen.ScreenHandlerType;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.network.PacketByteBuf;
|
||||
import net.minecraft.util.Direction;
|
||||
import net.minecraft.util.Identifier;
|
||||
import net.minecraftforge.registries.ForgeRegistries;
|
||||
|
||||
import appeng.api.AEApi;
|
||||
import appeng.api.implementations.tiles.IChestOrDrive;
|
||||
import appeng.api.networking.GridFlags;
|
||||
import appeng.api.networking.events.MENetworkCellArrayUpdate;
|
||||
import appeng.api.networking.events.MENetworkChannelsChanged;
|
||||
import appeng.api.networking.events.MENetworkEventSubscribe;
|
||||
import appeng.api.networking.events.MENetworkPowerStatusChange;
|
||||
import appeng.api.networking.security.IActionSource;
|
||||
import appeng.api.networking.storage.IStorageGrid;
|
||||
import appeng.api.storage.IMEInventoryHandler;
|
||||
import appeng.api.storage.IStorageChannel;
|
||||
import appeng.api.storage.cells.CellState;
|
||||
import appeng.api.storage.cells.ICellHandler;
|
||||
import appeng.api.storage.cells.ICellInventory;
|
||||
import appeng.api.storage.cells.ICellInventoryHandler;
|
||||
import appeng.api.storage.data.IAEItemStack;
|
||||
import appeng.api.storage.data.IAEStack;
|
||||
import appeng.api.util.AECableType;
|
||||
import appeng.api.util.AEPartLocation;
|
||||
import appeng.api.util.DimensionalCoord;
|
||||
import appeng.block.storage.DriveSlotsState;
|
||||
import appeng.client.render.model.DriveModelData;
|
||||
import appeng.container.implementations.DriveContainer;
|
||||
import appeng.helpers.IPriorityHost;
|
||||
import appeng.me.GridAccessException;
|
||||
import appeng.me.helpers.MachineSource;
|
||||
import appeng.me.storage.DriveWatcher;
|
||||
import appeng.tile.grid.AENetworkInvBlockEntity;
|
||||
import appeng.tile.inventory.AppEngCellInventory;
|
||||
import appeng.util.Platform;
|
||||
import appeng.util.inv.InvOperation;
|
||||
import appeng.util.inv.filter.IAEItemFilter;
|
||||
|
||||
public class DriveBlockEntity extends AENetworkInvBlockEntity implements IChestOrDrive, IPriorityHost {
|
||||
|
||||
private static final int BIT_POWER_MASK = 0x80000000;
|
||||
private static final int BIT_BLINK_MASK = 0x24924924;
|
||||
private static final int BIT_STATE_MASK = 0xDB6DB6DB;
|
||||
|
||||
private final AppEngCellInventory inv = new AppEngCellInventory(this, 10);
|
||||
private final ICellHandler[] handlersBySlot = new ICellHandler[10];
|
||||
private final DriveWatcher<IAEItemStack>[] invBySlot = new DriveWatcher[10];
|
||||
private final IActionSource mySrc;
|
||||
private boolean isCached = false;
|
||||
private Map<IStorageChannel<? extends IAEStack<?>>, List<IMEInventoryHandler>> inventoryHandlers;
|
||||
private int priority = 0;
|
||||
private boolean wasActive = false;
|
||||
// This is only used on the client
|
||||
private final Item[] cellItems = new Item[10];
|
||||
|
||||
/**
|
||||
* The state of all cells inside a drive as bitset, using the following format.
|
||||
*
|
||||
* Bit 31: power state. 0 = off, 1 = on. Bit 30: undefined Bit 29-0: 3 bits as
|
||||
* state of each cell with the cell in slot 0 located in the 3 least significant
|
||||
* bits.
|
||||
*
|
||||
* Cell states: Bit 2: blink. 0 = off, 1 = on. Bit 1-0: cell status
|
||||
*
|
||||
*
|
||||
*/
|
||||
private int state = 0;
|
||||
|
||||
public DriveBlockEntity(BlockEntityType<?> tileEntityTypeIn) {
|
||||
super(tileEntityTypeIn);
|
||||
this.mySrc = new MachineSource(this);
|
||||
this.getProxy().setFlags(GridFlags.REQUIRE_CHANNEL);
|
||||
this.inv.setFilter(new CellValidInventoryFilter());
|
||||
this.inventoryHandlers = new IdentityHashMap<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setOrientation(Direction inForward, Direction inUp) {
|
||||
super.setOrientation(inForward, inUp);
|
||||
this.getProxy().setValidSides(EnumSet.complementOf(EnumSet.of(inForward)));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void writeToStream(final PacketByteBuf data) throws IOException {
|
||||
super.writeToStream(data);
|
||||
int newState = 0;
|
||||
|
||||
if (this.getProxy().isActive()) {
|
||||
newState |= BIT_POWER_MASK;
|
||||
}
|
||||
for (int x = 0; x < this.getCellCount(); x++) {
|
||||
newState |= (this.getCellStatus(x).ordinal() << (3 * x));
|
||||
}
|
||||
data.writeInt(newState);
|
||||
|
||||
writeCellItemIds(data);
|
||||
}
|
||||
|
||||
private void writeCellItemIds(PacketByteBuf data) {
|
||||
List<Identifier> cellItemIds = new ArrayList<>(getCellCount());
|
||||
byte[] bm = new byte[getCellCount()];
|
||||
for (int x = 0; x < this.getCellCount(); x++) {
|
||||
Item item = getCellItem(x);
|
||||
if (item != null && item.getRegistryName() != null) {
|
||||
Identifier itemId = item.getRegistryName();
|
||||
int idx = cellItemIds.indexOf(itemId);
|
||||
if (idx == -1) {
|
||||
cellItemIds.add(itemId);
|
||||
bm[x] = (byte) cellItemIds.size(); // We use 1-based in bm[]
|
||||
} else {
|
||||
bm[x] = (byte) (1 + idx); // 1-based indexing!!
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Write out the list of unique cell item ids
|
||||
data.writeByte(cellItemIds.size());
|
||||
for (Identifier itemId : cellItemIds) {
|
||||
data.writeResourceLocation(itemId);
|
||||
}
|
||||
// Then the lookup table for each slot
|
||||
for (int i = 0; i < getCellCount(); i++) {
|
||||
data.writeByte(bm[i]);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean readFromStream(final PacketByteBuf data) throws IOException {
|
||||
boolean c = super.readFromStream(data);
|
||||
final int oldState = this.state;
|
||||
this.state = data.readInt();
|
||||
|
||||
c |= this.readCellItemIDs(data);
|
||||
|
||||
return (this.state & BIT_STATE_MASK) != (oldState & BIT_STATE_MASK) || c;
|
||||
}
|
||||
|
||||
private boolean readCellItemIDs(final PacketByteBuf data) {
|
||||
int uniqueStrCount = data.readByte();
|
||||
String[] uniqueStrs = new String[uniqueStrCount];
|
||||
for (int i = 0; i < uniqueStrCount; i++) {
|
||||
uniqueStrs[i] = data.readString();
|
||||
}
|
||||
|
||||
boolean changed = false;
|
||||
for (int i = 0; i < getCellCount(); i++) {
|
||||
byte idx = data.readByte();
|
||||
|
||||
// an index of 0 indicates the slot is empty
|
||||
Item item = null;
|
||||
if (idx > 0) {
|
||||
--idx;
|
||||
String itemId = uniqueStrs[idx];
|
||||
item = Registry.ITEM.get(new Identifier(itemId));
|
||||
}
|
||||
if (cellItems[i] != item) {
|
||||
changed = true;
|
||||
cellItems[i] = item;
|
||||
}
|
||||
}
|
||||
|
||||
return changed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCellCount() {
|
||||
return 10;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public Item getCellItem(int slot) {
|
||||
// Client-side we'll need to actually use the synced state
|
||||
if (world == null || world.isClient) {
|
||||
return cellItems[slot];
|
||||
}
|
||||
|
||||
ItemStack stackInSlot = inv.getInvStack(slot);
|
||||
if (!stackInSlot.isEmpty()) {
|
||||
return stackInSlot.getItem();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CellState getCellStatus(final int slot) {
|
||||
if (Platform.isClient()) {
|
||||
return CellState.values()[(this.state >> (slot * 3)) & 3];
|
||||
}
|
||||
|
||||
final DriveWatcher handler = this.invBySlot[slot];
|
||||
if (handler == null) {
|
||||
return CellState.ABSENT;
|
||||
}
|
||||
|
||||
return handler.getStatus();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPowered() {
|
||||
if (isClient()) {
|
||||
return (this.state & BIT_POWER_MASK) == BIT_POWER_MASK;
|
||||
}
|
||||
|
||||
return this.getProxy().isActive();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCellBlinking(final int slot) {
|
||||
return ((this.state >> (slot * 3 + 2)) & 0x01) == 0x01;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromTag(BlockState state, final CompoundTag data) {
|
||||
super.fromTag(state, data);
|
||||
this.isCached = false;
|
||||
this.priority = data.getInt("priority");
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompoundTag toTag(final CompoundTag data) {
|
||||
super.toTag(data);
|
||||
data.putInt("priority", this.priority);
|
||||
return data;
|
||||
}
|
||||
|
||||
@MENetworkEventSubscribe
|
||||
public void powerRender(final MENetworkPowerStatusChange c) {
|
||||
this.recalculateDisplay();
|
||||
}
|
||||
|
||||
private void recalculateDisplay() {
|
||||
final boolean currentActive = this.getProxy().isActive();
|
||||
int newState = 0;
|
||||
|
||||
if (currentActive) {
|
||||
newState |= BIT_POWER_MASK;
|
||||
}
|
||||
|
||||
if (this.wasActive != currentActive) {
|
||||
this.wasActive = currentActive;
|
||||
try {
|
||||
this.getProxy().getGrid().postEvent(new MENetworkCellArrayUpdate());
|
||||
} catch (final GridAccessException e) {
|
||||
// :P
|
||||
}
|
||||
}
|
||||
|
||||
for (int x = 0; x < this.getCellCount(); x++) {
|
||||
newState |= (this.getCellStatus(x).ordinal() << (3 * x));
|
||||
}
|
||||
|
||||
if (newState != this.state) {
|
||||
this.state = newState;
|
||||
this.markForUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
@MENetworkEventSubscribe
|
||||
public void channelRender(final MENetworkChannelsChanged c) {
|
||||
this.recalculateDisplay();
|
||||
}
|
||||
|
||||
@Override
|
||||
public AECableType getCableConnectionType(final AEPartLocation dir) {
|
||||
return AECableType.SMART;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DimensionalCoord getLocation() {
|
||||
return new DimensionalCoord(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FixedItemInv getInternalInventory() {
|
||||
return this.inv;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChangeInventory(final FixedItemInv inv, final int slot, final InvOperation mc,
|
||||
final ItemStack removed, final ItemStack added) {
|
||||
if (this.isCached) {
|
||||
this.isCached = false; // recalculate the storage cell.
|
||||
this.updateState();
|
||||
}
|
||||
|
||||
try {
|
||||
this.getProxy().getGrid().postEvent(new MENetworkCellArrayUpdate());
|
||||
|
||||
final IStorageGrid gs = this.getProxy().getStorage();
|
||||
Platform.postChanges(gs, removed, added, this.mySrc);
|
||||
} catch (final GridAccessException ignored) {
|
||||
}
|
||||
|
||||
this.markForUpdate();
|
||||
}
|
||||
|
||||
private void updateState() {
|
||||
if (!this.isCached) {
|
||||
final Collection<IStorageChannel<? extends IAEStack<?>>> storageChannels = AEApi.instance().storage()
|
||||
.storageChannels();
|
||||
storageChannels.forEach(channel -> this.inventoryHandlers.put(channel, new ArrayList<>(10)));
|
||||
|
||||
double power = 2.0;
|
||||
|
||||
for (int x = 0; x < this.inv.getSlotCount(); x++) {
|
||||
final ItemStack is = this.inv.getInvStack(x);
|
||||
this.invBySlot[x] = null;
|
||||
this.handlersBySlot[x] = null;
|
||||
|
||||
if (!is.isEmpty()) {
|
||||
this.handlersBySlot[x] = AEApi.instance().registries().cell().getHandler(is);
|
||||
|
||||
if (this.handlersBySlot[x] != null) {
|
||||
for (IStorageChannel<? extends IAEStack<?>> channel : storageChannels) {
|
||||
|
||||
ICellInventoryHandler cell = this.handlersBySlot[x].getCellInventory(is, this, channel);
|
||||
|
||||
if (cell != null) {
|
||||
this.inv.setHandler(x, cell);
|
||||
power += this.handlersBySlot[x].cellIdleDrain(is, cell);
|
||||
|
||||
final DriveWatcher<IAEItemStack> ih = new DriveWatcher(cell, is, this.handlersBySlot[x],
|
||||
this);
|
||||
ih.setPriority(this.priority);
|
||||
this.invBySlot[x] = ih;
|
||||
this.inventoryHandlers.get(channel).add(ih);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.getProxy().setIdlePowerUsage(power);
|
||||
|
||||
this.isCached = true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReady() {
|
||||
super.onReady();
|
||||
this.updateState();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<IMEInventoryHandler> getCellArray(final IStorageChannel channel) {
|
||||
if (this.getProxy().isActive()) {
|
||||
this.updateState();
|
||||
|
||||
return this.inventoryHandlers.get(channel);
|
||||
}
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPriority() {
|
||||
return this.priority;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPriority(final int newValue) {
|
||||
this.priority = newValue;
|
||||
this.saveChanges();
|
||||
|
||||
this.isCached = false; // recalculate the storage cell.
|
||||
this.updateState();
|
||||
|
||||
try {
|
||||
this.getProxy().getGrid().postEvent(new MENetworkCellArrayUpdate());
|
||||
} catch (final GridAccessException e) {
|
||||
// :P
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void blinkCell(final int slot) {
|
||||
this.state |= 1 << (slot * 3 + 2);
|
||||
|
||||
this.recalculateDisplay();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveChanges(final ICellInventory<?> cellInventory) {
|
||||
this.world.markDirty(this.pos, this);
|
||||
}
|
||||
|
||||
private class CellValidInventoryFilter implements IAEItemFilter {
|
||||
|
||||
@Override
|
||||
public boolean allowExtract(FixedItemInv inv, int slot, int amount) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean allowInsert(FixedItemInv inv, int slot, ItemStack stack) {
|
||||
return !stack.isEmpty() && AEApi.instance().registries().cell().isCellHandled(stack);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack getItemStackRepresentation() {
|
||||
return AEApi.instance().definitions().blocks().drive().maybeStack(1).orElse(ItemStack.EMPTY);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DriveModelData getRenderAttachmentData() {
|
||||
return new DriveModelData(getUp(), getForward(), DriveSlotsState.fromChestOrDrive(this));
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScreenHandlerType<?> getContainerType() {
|
||||
return DriveContainer.TYPE;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,448 +0,0 @@
|
||||
/*
|
||||
* This file is part of Applied Energistics 2.
|
||||
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
|
||||
*
|
||||
* Applied Energistics 2 is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Applied Energistics 2 is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
|
||||
*/
|
||||
|
||||
package appeng.tile.storage;
|
||||
|
||||
import java.util.IdentityHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import alexiil.mc.lib.attributes.item.FixedItemInv;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.block.BlockState;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.block.entity.BlockEntityType;
|
||||
import net.minecraft.util.math.Direction;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import appeng.api.AEApi;
|
||||
import appeng.api.config.Actionable;
|
||||
import appeng.api.config.FullnessMode;
|
||||
import appeng.api.config.OperationMode;
|
||||
import appeng.api.config.RedstoneMode;
|
||||
import appeng.api.config.Settings;
|
||||
import appeng.api.config.Upgrades;
|
||||
import appeng.api.config.YesNo;
|
||||
import appeng.api.implementations.IUpgradeableHost;
|
||||
import appeng.api.networking.GridFlags;
|
||||
import appeng.api.networking.IGridNode;
|
||||
import appeng.api.networking.energy.IEnergySource;
|
||||
import appeng.api.networking.security.IActionSource;
|
||||
import appeng.api.networking.ticking.IGridTickable;
|
||||
import appeng.api.networking.ticking.TickRateModulation;
|
||||
import appeng.api.networking.ticking.TickingRequest;
|
||||
import appeng.api.storage.IMEInventory;
|
||||
import appeng.api.storage.IMEMonitor;
|
||||
import appeng.api.storage.IStorageChannel;
|
||||
import appeng.api.storage.data.IAEStack;
|
||||
import appeng.api.storage.data.IItemList;
|
||||
import appeng.api.util.AECableType;
|
||||
import appeng.api.util.AEPartLocation;
|
||||
import appeng.api.util.DimensionalCoord;
|
||||
import appeng.api.util.IConfigManager;
|
||||
import appeng.core.settings.TickRates;
|
||||
import appeng.me.GridAccessException;
|
||||
import appeng.me.helpers.MachineSource;
|
||||
import appeng.parts.automation.BlockUpgradeInventory;
|
||||
import appeng.parts.automation.UpgradeInventory;
|
||||
import appeng.tile.grid.AENetworkInvBlockEntity;
|
||||
import appeng.tile.inventory.AppEngInternalInventory;
|
||||
import appeng.util.ConfigManager;
|
||||
import appeng.util.IConfigManagerHost;
|
||||
import appeng.util.InventoryAdaptor;
|
||||
import appeng.util.Platform;
|
||||
import appeng.util.helpers.ItemHandlerUtil;
|
||||
import appeng.util.inv.AdaptorFixedInv;
|
||||
import appeng.util.inv.InvOperation;
|
||||
import appeng.util.inv.WrapperChainedItemHandler;
|
||||
import appeng.util.inv.WrapperFilteredItemHandler;
|
||||
import appeng.util.inv.filter.AEItemFilters;
|
||||
|
||||
public class IOPortBlockEntity extends AENetworkInvBlockEntity
|
||||
implements IUpgradeableHost, IConfigManagerHost, IGridTickable {
|
||||
private static final int NUMBER_OF_CELL_SLOTS = 6;
|
||||
private static final int NUMBER_OF_UPGRADE_SLOTS = 3;
|
||||
|
||||
private final ConfigManager manager;
|
||||
|
||||
private final AppEngInternalInventory inputCells = new AppEngInternalInventory(this, NUMBER_OF_CELL_SLOTS);
|
||||
private final AppEngInternalInventory outputCells = new AppEngInternalInventory(this, NUMBER_OF_CELL_SLOTS);
|
||||
private final FixedItemInv combinedInventory = new WrapperChainedItemHandler(this.inputCells, this.outputCells);
|
||||
|
||||
private final FixedItemInv inputCellsExt = new WrapperFilteredItemHandler(this.inputCells,
|
||||
AEItemFilters.INSERT_ONLY);
|
||||
private final FixedItemInv outputCellsExt = new WrapperFilteredItemHandler(this.outputCells,
|
||||
AEItemFilters.EXTRACT_ONLY);
|
||||
|
||||
private final UpgradeInventory upgrades;
|
||||
private final IActionSource mySrc;
|
||||
private YesNo lastRedstoneState;
|
||||
private ItemStack currentCell;
|
||||
private Map<IStorageChannel<?>, IMEInventory<?>> cachedInventories;
|
||||
|
||||
public IOPortBlockEntity(BlockEntityType<?> tileEntityTypeIn) {
|
||||
super(tileEntityTypeIn);
|
||||
this.getProxy().setFlags(GridFlags.REQUIRE_CHANNEL);
|
||||
this.manager = new ConfigManager(this);
|
||||
this.manager.registerSetting(Settings.REDSTONE_CONTROLLED, RedstoneMode.IGNORE);
|
||||
this.manager.registerSetting(Settings.FULLNESS_MODE, FullnessMode.EMPTY);
|
||||
this.manager.registerSetting(Settings.OPERATION_MODE, OperationMode.EMPTY);
|
||||
this.mySrc = new MachineSource(this);
|
||||
this.lastRedstoneState = YesNo.UNDECIDED;
|
||||
|
||||
final Block ioPortBlock = AEApi.instance().definitions().blocks().iOPort().maybeBlock().get();
|
||||
this.upgrades = new BlockUpgradeInventory(ioPortBlock, this, NUMBER_OF_UPGRADE_SLOTS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompoundTag toTag(final CompoundTag data) {
|
||||
super.toTag(data);
|
||||
this.manager.writeToNBT(data);
|
||||
this.upgrades.writeToNBT(data, "upgrades");
|
||||
data.putInt("lastRedstoneState", this.lastRedstoneState.ordinal());
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromTag(BlockState state, final CompoundTag data) {
|
||||
super.fromTag(state, data);
|
||||
this.manager.readFromNBT(data);
|
||||
this.upgrades.readFromNBT(data, "upgrades");
|
||||
if (data.contains("lastRedstoneState")) {
|
||||
this.lastRedstoneState = YesNo.values()[data.getInt("lastRedstoneState")];
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public AECableType getCableConnectionType(final AEPartLocation dir) {
|
||||
return AECableType.SMART;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DimensionalCoord getLocation() {
|
||||
return new DimensionalCoord(this);
|
||||
}
|
||||
|
||||
private void updateTask() {
|
||||
try {
|
||||
if (this.hasWork()) {
|
||||
this.getProxy().getTick().wakeDevice(this.getProxy().getNode());
|
||||
} else {
|
||||
this.getProxy().getTick().sleepDevice(this.getProxy().getNode());
|
||||
}
|
||||
} catch (final GridAccessException e) {
|
||||
// :P
|
||||
}
|
||||
}
|
||||
|
||||
public void updateRedstoneState() {
|
||||
final YesNo currentState = this.world.getReceivedRedstonePower(this.pos) != 0 ? YesNo.YES : YesNo.NO;
|
||||
if (this.lastRedstoneState != currentState) {
|
||||
this.lastRedstoneState = currentState;
|
||||
this.updateTask();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean getRedstoneState() {
|
||||
if (this.lastRedstoneState == YesNo.UNDECIDED) {
|
||||
this.updateRedstoneState();
|
||||
}
|
||||
|
||||
return this.lastRedstoneState == YesNo.YES;
|
||||
}
|
||||
|
||||
private boolean isEnabled() {
|
||||
if (this.getInstalledUpgrades(Upgrades.REDSTONE) == 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
final RedstoneMode rs = (RedstoneMode) this.manager.getSetting(Settings.REDSTONE_CONTROLLED);
|
||||
if (rs == RedstoneMode.HIGH_SIGNAL) {
|
||||
return this.getRedstoneState();
|
||||
}
|
||||
return !this.getRedstoneState();
|
||||
}
|
||||
|
||||
@Override
|
||||
public IConfigManager getConfigManager() {
|
||||
return this.manager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FixedItemInv getInventoryByName(final String name) {
|
||||
if (name.equals("upgrades")) {
|
||||
return this.upgrades;
|
||||
}
|
||||
|
||||
if (name.equals("cells")) {
|
||||
return this.combinedInventory;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateSetting(final IConfigManager manager, final Settings settingName, final Enum<?> newValue) {
|
||||
this.updateTask();
|
||||
}
|
||||
|
||||
private boolean hasWork() {
|
||||
if (this.isEnabled()) {
|
||||
return !ItemHandlerUtil.isEmpty(this.inputCells);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FixedItemInv getInternalInventory() {
|
||||
return this.combinedInventory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChangeInventory(final FixedItemInv inv, final int slot, final InvOperation mc,
|
||||
final ItemStack removed, final ItemStack added) {
|
||||
if (this.inputCells == inv) {
|
||||
this.updateTask();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected FixedItemInv getItemHandlerForSide(final Direction facing) {
|
||||
if (facing == this.getUp() || facing == this.getUp().getOpposite()) {
|
||||
return this.inputCellsExt;
|
||||
} else {
|
||||
return this.outputCellsExt;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public TickingRequest getTickingRequest(final IGridNode node) {
|
||||
return new TickingRequest(TickRates.IOPort.getMin(), TickRates.IOPort.getMax(), !this.hasWork(), false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TickRateModulation tickingRequest(final IGridNode node, final int ticksSinceLastCall) {
|
||||
if (!this.getProxy().isActive()) {
|
||||
return TickRateModulation.IDLE;
|
||||
}
|
||||
|
||||
TickRateModulation ret = TickRateModulation.SLEEP;
|
||||
long itemsToMove = 256;
|
||||
|
||||
switch (this.getInstalledUpgrades(Upgrades.SPEED)) {
|
||||
case 1:
|
||||
itemsToMove *= 2;
|
||||
break;
|
||||
case 2:
|
||||
itemsToMove *= 4;
|
||||
break;
|
||||
case 3:
|
||||
itemsToMove *= 8;
|
||||
break;
|
||||
}
|
||||
|
||||
try {
|
||||
final IEnergySource energy = this.getProxy().getEnergy();
|
||||
for (int x = 0; x < NUMBER_OF_CELL_SLOTS; x++) {
|
||||
final ItemStack is = this.inputCells.getInvStack(x);
|
||||
if (!is.isEmpty()) {
|
||||
boolean shouldMove = true;
|
||||
|
||||
for (IStorageChannel<? extends IAEStack<?>> c : AEApi.instance().storage().storageChannels()) {
|
||||
if (itemsToMove > 0) {
|
||||
final IMEMonitor<? extends IAEStack<?>> network = this.getProxy().getStorage()
|
||||
.getInventory(c);
|
||||
final IMEInventory<?> inv = this.getInv(is, c);
|
||||
|
||||
if (inv == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (this.manager.getSetting(Settings.OPERATION_MODE) == OperationMode.EMPTY) {
|
||||
itemsToMove = this.transferContents(energy, inv, network, itemsToMove, c);
|
||||
} else {
|
||||
itemsToMove = this.transferContents(energy, network, inv, itemsToMove, c);
|
||||
}
|
||||
|
||||
shouldMove &= this.shouldMove(inv);
|
||||
|
||||
if (itemsToMove > 0) {
|
||||
ret = TickRateModulation.IDLE;
|
||||
} else {
|
||||
ret = TickRateModulation.URGENT;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (itemsToMove > 0 && shouldMove && this.moveSlot(x)) {
|
||||
ret = TickRateModulation.URGENT;
|
||||
} else {
|
||||
ret = TickRateModulation.URGENT;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
} catch (final GridAccessException e) {
|
||||
ret = TickRateModulation.IDLE;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInstalledUpgrades(final Upgrades u) {
|
||||
return this.upgrades.getInstalledUpgrades(u);
|
||||
}
|
||||
|
||||
private IMEInventory<?> getInv(final ItemStack is, final IStorageChannel<?> chan) {
|
||||
if (this.currentCell != is) {
|
||||
this.currentCell = is;
|
||||
this.cachedInventories = new IdentityHashMap<>();
|
||||
|
||||
for (IStorageChannel<? extends IAEStack<?>> c : AEApi.instance().storage().storageChannels()) {
|
||||
this.cachedInventories.put(c, AEApi.instance().registries().cell().getCellInventory(is, null, c));
|
||||
}
|
||||
}
|
||||
|
||||
return this.cachedInventories.get(chan);
|
||||
}
|
||||
|
||||
private long transferContents(final IEnergySource energy, final IMEInventory src, final IMEInventory destination,
|
||||
long itemsToMove, final IStorageChannel chan) {
|
||||
final IItemList<? extends IAEStack> myList;
|
||||
if (src instanceof IMEMonitor) {
|
||||
myList = ((IMEMonitor) src).getStorageList();
|
||||
} else {
|
||||
myList = src.getAvailableItems(src.getChannel().createList());
|
||||
}
|
||||
|
||||
itemsToMove *= chan.transferFactor();
|
||||
|
||||
boolean didStuff;
|
||||
|
||||
do {
|
||||
didStuff = false;
|
||||
|
||||
for (final IAEStack s : myList) {
|
||||
final long totalStackSize = s.getStackSize();
|
||||
if (totalStackSize > 0) {
|
||||
final IAEStack stack = destination.injectItems(s, Actionable.SIMULATE, this.mySrc);
|
||||
|
||||
long possible = 0;
|
||||
if (stack == null) {
|
||||
possible = totalStackSize;
|
||||
} else {
|
||||
possible = totalStackSize - stack.getStackSize();
|
||||
}
|
||||
|
||||
if (possible > 0) {
|
||||
possible = Math.min(possible, itemsToMove);
|
||||
s.setStackSize(possible);
|
||||
|
||||
final IAEStack extracted = src.extractItems(s, Actionable.MODULATE, this.mySrc);
|
||||
if (extracted != null) {
|
||||
possible = extracted.getStackSize();
|
||||
final IAEStack failed = Platform.poweredInsert(energy, destination, extracted, this.mySrc);
|
||||
|
||||
if (failed != null) {
|
||||
possible -= failed.getStackSize();
|
||||
src.injectItems(failed, Actionable.MODULATE, this.mySrc);
|
||||
}
|
||||
|
||||
if (possible > 0) {
|
||||
itemsToMove -= possible;
|
||||
didStuff = true;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} while (itemsToMove > 0 && didStuff);
|
||||
|
||||
return itemsToMove / chan.transferFactor();
|
||||
}
|
||||
|
||||
private boolean shouldMove(final IMEInventory<?> inv) {
|
||||
final FullnessMode fm = (FullnessMode) this.manager.getSetting(Settings.FULLNESS_MODE);
|
||||
|
||||
if (inv != null) {
|
||||
return this.matches(fm, inv);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean moveSlot(final int x) {
|
||||
final InventoryAdaptor ad = new AdaptorFixedInv(this.outputCells);
|
||||
if (ad.addItems(this.inputCells.getInvStack(x)).isEmpty()) {
|
||||
this.inputCells.setInvStack(x, ItemStack.EMPTY);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean matches(final FullnessMode fm, final IMEInventory src) {
|
||||
if (fm == FullnessMode.HALF) {
|
||||
return true;
|
||||
}
|
||||
|
||||
final IItemList<? extends IAEStack> myList;
|
||||
|
||||
if (src instanceof IMEMonitor) {
|
||||
myList = ((IMEMonitor) src).getStorageList();
|
||||
} else {
|
||||
myList = src.getAvailableItems(src.getChannel().createList());
|
||||
}
|
||||
|
||||
if (fm == FullnessMode.EMPTY) {
|
||||
return myList.isEmpty();
|
||||
}
|
||||
|
||||
final IAEStack test = myList.getFirstItem();
|
||||
if (test != null) {
|
||||
test.setStackSize(1);
|
||||
return src.injectItems(test, Actionable.SIMULATE, this.mySrc) != null;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the items in the upgrade slots to the drop list.
|
||||
*
|
||||
* @param w world
|
||||
* @param pos pos of block entity
|
||||
* @param drops drops of block entity
|
||||
*/
|
||||
@Override
|
||||
public void getDrops(final World w, final BlockPos pos, final List<ItemStack> drops) {
|
||||
super.getDrops(w, pos, drops);
|
||||
|
||||
for (int upgradeIndex = 0; upgradeIndex < this.upgrades.getSlotCount(); upgradeIndex++) {
|
||||
final ItemStack stackInSlot = this.upgrades.getInvStack(upgradeIndex);
|
||||
|
||||
if (!stackInSlot.isEmpty()) {
|
||||
drops.add(stackInSlot);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,163 @@
|
||||
/*
|
||||
* This file is part of Applied Energistics 2.
|
||||
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
|
||||
*
|
||||
* Applied Energistics 2 is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Applied Energistics 2 is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
|
||||
*/
|
||||
|
||||
package appeng.tile.storage;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.client.block.ChestAnimationProgress;
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.network.PacketByteBuf;
|
||||
import net.minecraft.sound.SoundEvents;
|
||||
import net.minecraft.util.Tickable;
|
||||
import net.minecraft.block.entity.BlockEntityType;
|
||||
import net.minecraft.sound.SoundCategory;
|
||||
import net.minecraft.util.math.MathHelper;
|
||||
import alexiil.mc.lib.attributes.item.FixedItemInv;
|
||||
|
||||
import appeng.block.storage.SkyChestBlock;
|
||||
import appeng.tile.AEBaseInvBlockEntity;
|
||||
import appeng.tile.inventory.AppEngInternalInventory;
|
||||
import appeng.util.inv.InvOperation;
|
||||
|
||||
public class SkyChestBlockEntity extends AEBaseInvBlockEntity implements Tickable, ChestAnimationProgress {
|
||||
|
||||
private final AppEngInternalInventory inv = new AppEngInternalInventory(this, 9 * 4);
|
||||
|
||||
// server
|
||||
private int numPlayersUsing;
|
||||
// client..
|
||||
private long lastEvent;
|
||||
private float lidAngle;
|
||||
private float prevLidAngle;
|
||||
|
||||
public SkyChestBlockEntity(BlockEntityType<? extends SkyChestBlockEntity> type) {
|
||||
super(type);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void writeToStream(final PacketByteBuf data) throws IOException {
|
||||
super.writeToStream(data);
|
||||
data.writeBoolean(this.getPlayerOpen() > 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean readFromStream(final PacketByteBuf data) throws IOException {
|
||||
final boolean c = super.readFromStream(data);
|
||||
final int wasOpen = this.getPlayerOpen();
|
||||
this.setPlayerOpen(data.readBoolean() ? 1 : 0);
|
||||
|
||||
if (wasOpen != this.getPlayerOpen()) {
|
||||
this.setLastEvent(System.currentTimeMillis());
|
||||
}
|
||||
|
||||
return c; // TESR yo!
|
||||
}
|
||||
|
||||
@Override
|
||||
public FixedItemInv getInternalInventory() {
|
||||
return this.inv;
|
||||
}
|
||||
|
||||
public void openInventory(final PlayerEntity player) {
|
||||
if (!player.isSpectator()) {
|
||||
this.setPlayerOpen(this.getPlayerOpen() + 1);
|
||||
onOpenOrClose();
|
||||
|
||||
if (this.getPlayerOpen() == 1) {
|
||||
this.getWorld().playSound(player, this.pos.getX() + 0.5D, this.pos.getY() + 0.5D,
|
||||
this.pos.getZ() + 0.5D, SoundEvents.BLOCK_CHEST_OPEN, SoundCategory.BLOCKS, 0.5F,
|
||||
this.getWorld().random.nextFloat() * 0.1F + 0.9F);
|
||||
this.markForUpdate();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void closeInventory(final PlayerEntity player) {
|
||||
if (!player.isSpectator()) {
|
||||
this.setPlayerOpen(this.getPlayerOpen() - 1);
|
||||
onOpenOrClose();
|
||||
|
||||
if (this.getPlayerOpen() < 0) {
|
||||
this.setPlayerOpen(0);
|
||||
}
|
||||
|
||||
if (this.getPlayerOpen() == 0) {
|
||||
this.getWorld().playSound(player, this.pos.getX() + 0.5D, this.pos.getY() + 0.5D,
|
||||
this.pos.getZ() + 0.5D, SoundEvents.BLOCK_CHEST_CLOSE, SoundCategory.BLOCKS, 0.5F,
|
||||
this.getWorld().random.nextFloat() * 0.1F + 0.9F);
|
||||
this.markForUpdate();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// See ChestTileEntity
|
||||
private void onOpenOrClose() {
|
||||
Block block = getCachedState().getBlock();
|
||||
if (block instanceof SkyChestBlock) {
|
||||
this.world.addSyncedBlockEvent(this.pos, block, 1, this.numPlayersUsing);
|
||||
this.world.updateNeighborsAlways(this.pos, block);
|
||||
// FIXME: Uhm, we are we doing this?
|
||||
this.world.updateNeighborsAlways(this.pos.down(), block);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tick() {
|
||||
this.prevLidAngle = this.lidAngle;
|
||||
if (this.numPlayersUsing == 0 && this.lidAngle > 0.0F || this.numPlayersUsing > 0 && this.lidAngle < 1.0F) {
|
||||
if (this.numPlayersUsing > 0) {
|
||||
this.lidAngle += 0.1F;
|
||||
} else {
|
||||
this.lidAngle -= 0.1F;
|
||||
}
|
||||
|
||||
this.lidAngle = MathHelper.clamp(this.lidAngle, 0, 1);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChangeInventory(final FixedItemInv inv, final int slot, final InvOperation mc,
|
||||
final ItemStack removed, final ItemStack added) {
|
||||
|
||||
}
|
||||
|
||||
public int getPlayerOpen() {
|
||||
return this.numPlayersUsing;
|
||||
}
|
||||
|
||||
private void setPlayerOpen(final int playerOpen) {
|
||||
this.numPlayersUsing = playerOpen;
|
||||
}
|
||||
|
||||
public long getLastEvent() {
|
||||
return this.lastEvent;
|
||||
}
|
||||
|
||||
private void setLastEvent(final long lastEvent) {
|
||||
this.lastEvent = lastEvent;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getAnimationProgress(float partialTicks) {
|
||||
return MathHelper.lerp(partialTicks, this.prevLidAngle, this.lidAngle);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user