Lots more moved
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
package appeng.tile.crafting;
|
||||
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
/**
|
||||
* Stores client-side only state about the ongoing animation for a molecular
|
||||
* assembler.
|
||||
*/
|
||||
public class AssemblerAnimationStatus {
|
||||
|
||||
private final ItemStack is;
|
||||
|
||||
private final byte speed;
|
||||
|
||||
private final int ticksRequired;
|
||||
|
||||
private float accumulatedTicks;
|
||||
|
||||
private float ticksUntilParticles;
|
||||
|
||||
public AssemblerAnimationStatus(byte speed, ItemStack is) {
|
||||
this.speed = speed;
|
||||
this.is = is;
|
||||
this.ticksRequired = (int) Math.ceil(Math.max(1, 100.0f / speed)) + 2;
|
||||
}
|
||||
|
||||
public ItemStack getIs() {
|
||||
return is;
|
||||
}
|
||||
|
||||
public byte getSpeed() {
|
||||
return speed;
|
||||
}
|
||||
|
||||
public float getAccumulatedTicks() {
|
||||
return accumulatedTicks;
|
||||
}
|
||||
|
||||
public void setAccumulatedTicks(float accumulatedTicks) {
|
||||
this.accumulatedTicks = accumulatedTicks;
|
||||
}
|
||||
|
||||
public float getTicksUntilParticles() {
|
||||
return ticksUntilParticles;
|
||||
}
|
||||
|
||||
public void setTicksUntilParticles(float ticksUntilParticles) {
|
||||
this.ticksUntilParticles = ticksUntilParticles;
|
||||
}
|
||||
|
||||
public boolean isExpired() {
|
||||
return accumulatedTicks > ticksRequired;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,357 @@
|
||||
/*
|
||||
* 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.crafting;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.EnumSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Optional;
|
||||
|
||||
import appeng.util.BlockUpdateFlag;
|
||||
import net.minecraft.block.BlockState;
|
||||
import net.minecraft.block.entity.BlockEntity;
|
||||
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.BlockView;
|
||||
|
||||
import appeng.api.AEApi;
|
||||
import appeng.api.config.Actionable;
|
||||
import appeng.api.implementations.IPowerChannelState;
|
||||
import appeng.api.networking.GridFlags;
|
||||
import appeng.api.networking.IGridHost;
|
||||
import appeng.api.networking.events.MENetworkChannelsChanged;
|
||||
import appeng.api.networking.events.MENetworkEventSubscribe;
|
||||
import appeng.api.networking.events.MENetworkPowerStatusChange;
|
||||
import appeng.api.storage.IMEInventory;
|
||||
import appeng.api.storage.channels.IItemStorageChannel;
|
||||
import appeng.api.storage.data.IAEItemStack;
|
||||
import appeng.api.util.AEPartLocation;
|
||||
import appeng.api.util.WorldCoord;
|
||||
import appeng.block.crafting.AbstractCraftingUnitBlock;
|
||||
import appeng.block.crafting.AbstractCraftingUnitBlock.CraftingUnitType;
|
||||
import appeng.me.cluster.IAECluster;
|
||||
import appeng.me.cluster.IAEMultiBlock;
|
||||
import appeng.me.cluster.implementations.CraftingCPUCalculator;
|
||||
import appeng.me.cluster.implementations.CraftingCPUCluster;
|
||||
import appeng.me.helpers.AENetworkProxy;
|
||||
import appeng.me.helpers.AENetworkProxyMultiblock;
|
||||
import appeng.tile.grid.AENetworkBlockEntity;
|
||||
import appeng.util.Platform;
|
||||
|
||||
public class CraftingBlockEntity extends AENetworkBlockEntity implements IAEMultiBlock, IPowerChannelState {
|
||||
|
||||
private final CraftingCPUCalculator calc = new CraftingCPUCalculator(this);
|
||||
private CompoundTag previousState = null;
|
||||
private boolean isCoreBlock = false;
|
||||
private CraftingCPUCluster cluster;
|
||||
|
||||
public CraftingBlockEntity(BlockEntityType<?> tileEntityTypeIn) {
|
||||
super(tileEntityTypeIn);
|
||||
this.getProxy().setFlags(GridFlags.MULTIBLOCK, GridFlags.REQUIRE_CHANNEL);
|
||||
this.getProxy().setValidSides(EnumSet.noneOf(Direction.class));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AENetworkProxy createProxy() {
|
||||
return new AENetworkProxyMultiblock(this, "proxy", this.getItemFromTile(this), true);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ItemStack getItemFromTile(final Object obj) {
|
||||
Optional<ItemStack> is;
|
||||
|
||||
if (((CraftingBlockEntity) obj).isAccelerator()) {
|
||||
is = AEApi.instance().definitions().blocks().craftingAccelerator().maybeStack(1);
|
||||
} else {
|
||||
is = AEApi.instance().definitions().blocks().craftingUnit().maybeStack(1);
|
||||
}
|
||||
|
||||
return is.orElseGet(() -> super.getItemFromTile(obj));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canBeRotated() {
|
||||
return true;// return BlockCraftingUnit.checkType( world.getBlockMetadata( xCoord, yCoord,
|
||||
// zCoord ),
|
||||
// BlockCraftingUnit.BASE_MONITOR );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setName(final String name) {
|
||||
super.setName(name);
|
||||
if (this.cluster != null) {
|
||||
this.cluster.updateName();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isAccelerator() {
|
||||
if (this.world == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
final AbstractCraftingUnitBlock unit = (AbstractCraftingUnitBlock) this.world.getBlockState(this.pos)
|
||||
.getBlock();
|
||||
return unit.type == CraftingUnitType.ACCELERATOR;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReady() {
|
||||
super.onReady();
|
||||
this.getProxy().setVisualRepresentation(this.getItemFromTile(this));
|
||||
this.updateMultiBlock();
|
||||
}
|
||||
|
||||
public void updateMultiBlock() {
|
||||
this.calc.calculateMultiblock(this.world, this.getLocation());
|
||||
}
|
||||
|
||||
public void updateStatus(final CraftingCPUCluster c) {
|
||||
if (this.cluster != null && this.cluster != c) {
|
||||
this.cluster.breakCluster();
|
||||
}
|
||||
|
||||
this.cluster = c;
|
||||
this.updateMeta(true);
|
||||
}
|
||||
|
||||
public void updateMeta(final boolean updateFormed) {
|
||||
if (this.world == null || this.notLoaded() || this.isRemoved()) {
|
||||
return;
|
||||
}
|
||||
|
||||
final boolean formed = this.isFormed();
|
||||
boolean power = false;
|
||||
|
||||
if (this.getProxy().isReady()) {
|
||||
power = this.getProxy().isActive();
|
||||
}
|
||||
|
||||
final BlockState current = this.world.getBlockState(this.pos);
|
||||
|
||||
// The tile might try to update while being destroyed
|
||||
if (current.getBlock() instanceof AbstractCraftingUnitBlock) {
|
||||
final BlockState newState = current.with(AbstractCraftingUnitBlock.POWERED, power)
|
||||
.with(AbstractCraftingUnitBlock.FORMED, formed);
|
||||
|
||||
if (current != newState) {
|
||||
// Not using flag 2 here (only send to clients, prevent block update) will cause
|
||||
// infinite loops
|
||||
// In case there is an inconsistency in the crafting clusters.
|
||||
this.world.setBlockState(this.pos, newState, BlockUpdateFlag.BLOCK_UPDATE);
|
||||
}
|
||||
}
|
||||
|
||||
if (updateFormed) {
|
||||
if (formed) {
|
||||
this.getProxy().setValidSides(EnumSet.allOf(Direction.class));
|
||||
} else {
|
||||
this.getProxy().setValidSides(EnumSet.noneOf(Direction.class));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isFormed() {
|
||||
if (isClient()) {
|
||||
return this.world.getBlockState(this.pos).get(AbstractCraftingUnitBlock.FORMED);
|
||||
}
|
||||
return this.cluster != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompoundTag toTag(final CompoundTag data) {
|
||||
super.toTag(data);
|
||||
data.putBoolean("core", this.isCoreBlock());
|
||||
if (this.isCoreBlock() && this.cluster != null) {
|
||||
this.cluster.writeToNBT(data);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromTag(BlockState state, final CompoundTag data) {
|
||||
super.fromTag(state, data);
|
||||
this.setCoreBlock(data.getBoolean("core"));
|
||||
if (this.isCoreBlock()) {
|
||||
if (this.cluster != null) {
|
||||
this.cluster.readFromNBT(data);
|
||||
} else {
|
||||
this.setPreviousState(data.copy());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disconnect(final boolean update) {
|
||||
if (this.cluster != null) {
|
||||
this.cluster.destroy();
|
||||
if (update) {
|
||||
this.updateMeta(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public IAECluster getCluster() {
|
||||
return this.cluster;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@MENetworkEventSubscribe
|
||||
public void onPowerStateChange(final MENetworkChannelsChanged ev) {
|
||||
this.updateMeta(false);
|
||||
}
|
||||
|
||||
@MENetworkEventSubscribe
|
||||
public void onPowerStateChange(final MENetworkPowerStatusChange ev) {
|
||||
this.updateMeta(false);
|
||||
}
|
||||
|
||||
public boolean isStatus() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isStorage() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public int getStorageBytes() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public void breakCluster() {
|
||||
if (this.cluster != null) {
|
||||
this.cluster.cancel();
|
||||
final IMEInventory<IAEItemStack> inv = this.cluster.getInventory();
|
||||
|
||||
final LinkedList<WorldCoord> places = new LinkedList<>();
|
||||
|
||||
final Iterator<IGridHost> i = this.cluster.getTiles();
|
||||
while (i.hasNext()) {
|
||||
final IGridHost h = i.next();
|
||||
if (h == this) {
|
||||
places.add(new WorldCoord(this));
|
||||
} else {
|
||||
final BlockEntity te = (BlockEntity) h;
|
||||
|
||||
for (final AEPartLocation d : AEPartLocation.SIDE_LOCATIONS) {
|
||||
final WorldCoord wc = new WorldCoord(te);
|
||||
wc.add(d, 1);
|
||||
if (this.world.isAir(wc.getPos())) {
|
||||
places.add(wc);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Collections.shuffle(places);
|
||||
|
||||
if (places.isEmpty()) {
|
||||
throw new IllegalStateException(
|
||||
this.cluster + " does not contain any kind of blocks, which were destroyed.");
|
||||
}
|
||||
|
||||
for (IAEItemStack ais : inv.getAvailableItems(
|
||||
AEApi.instance().storage().getStorageChannel(IItemStorageChannel.class).createList())) {
|
||||
ais = ais.copy();
|
||||
ais.setStackSize(ais.getDefinition().getMaxCount());
|
||||
while (true) {
|
||||
final IAEItemStack g = inv.extractItems(ais.copy(), Actionable.MODULATE,
|
||||
this.cluster.getActionSource());
|
||||
if (g == null) {
|
||||
break;
|
||||
}
|
||||
|
||||
final WorldCoord wc = places.poll();
|
||||
places.add(wc);
|
||||
|
||||
Platform.spawnDrops(this.world, wc.getPos(), Collections.singletonList(g.createItemStack()));
|
||||
}
|
||||
}
|
||||
|
||||
this.cluster.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPowered() {
|
||||
if (isClient()) {
|
||||
return this.world.getBlockState(this.pos).get(AbstractCraftingUnitBlock.POWERED);
|
||||
}
|
||||
return this.getProxy().isActive();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isActive() {
|
||||
if (Platform.isServer()) {
|
||||
return this.getProxy().isActive();
|
||||
}
|
||||
return this.isPowered() && this.isFormed();
|
||||
}
|
||||
|
||||
public boolean isCoreBlock() {
|
||||
return this.isCoreBlock;
|
||||
}
|
||||
|
||||
public void setCoreBlock(final boolean isCoreBlock) {
|
||||
this.isCoreBlock = isCoreBlock;
|
||||
}
|
||||
|
||||
public CompoundTag getPreviousState() {
|
||||
return this.previousState;
|
||||
}
|
||||
|
||||
public void setPreviousState(final CompoundTag previousState) {
|
||||
this.previousState = previousState;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getRenderAttachmentData() {
|
||||
return new CraftingCubeModelData(getUp(), getForward(), getConnections());
|
||||
}
|
||||
|
||||
protected EnumSet<Direction> getConnections() {
|
||||
if (world == null) {
|
||||
return EnumSet.noneOf(Direction.class);
|
||||
}
|
||||
|
||||
EnumSet<Direction> connections = EnumSet.noneOf(Direction.class);
|
||||
|
||||
for (Direction facing : Direction.values()) {
|
||||
if (this.isConnected(world, pos, facing)) {
|
||||
connections.add(facing);
|
||||
}
|
||||
}
|
||||
|
||||
return connections;
|
||||
}
|
||||
|
||||
private boolean isConnected(BlockView world, BlockPos pos, Direction side) {
|
||||
BlockPos adjacentPos = pos.offset(side);
|
||||
return world.getBlockState(adjacentPos).getBlock() instanceof AbstractCraftingUnitBlock;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package appeng.tile.crafting;
|
||||
|
||||
import java.util.EnumSet;
|
||||
import java.util.Objects;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
|
||||
import net.minecraft.util.math.Direction;
|
||||
|
||||
import appeng.client.render.model.AEModelData;
|
||||
|
||||
public class CraftingCubeModelData extends AEModelData {
|
||||
|
||||
// Contains information on which sides of the block are connected to other parts
|
||||
// of a formed crafting cube
|
||||
private final EnumSet<Direction> connections;
|
||||
|
||||
public CraftingCubeModelData(Direction up, Direction forward, EnumSet<Direction> connections) {
|
||||
super(up, forward);
|
||||
this.connections = Preconditions.checkNotNull(connections);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCacheable() {
|
||||
return false; // Too many variants
|
||||
}
|
||||
|
||||
public EnumSet<Direction> getConnections() {
|
||||
return connections;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
if (!super.equals(o)) {
|
||||
return false;
|
||||
}
|
||||
CraftingCubeModelData that = (CraftingCubeModelData) o;
|
||||
return connections.equals(that.connections);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(super.hashCode(), connections);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,176 @@
|
||||
/*
|
||||
* 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.crafting;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Optional;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import net.fabricmc.api.Environment;
|
||||
import net.minecraft.block.BlockState;
|
||||
import net.minecraft.block.entity.BlockEntityType;
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.network.PacketByteBuf;
|
||||
import net.minecraft.util.math.Direction;
|
||||
import net.fabricmc.api.EnvType;
|
||||
|
||||
|
||||
import appeng.api.AEApi;
|
||||
import appeng.api.implementations.tiles.IColorableTile;
|
||||
import appeng.api.storage.data.IAEItemStack;
|
||||
import appeng.api.util.AEColor;
|
||||
import appeng.util.item.AEItemStack;
|
||||
|
||||
public class CraftingMonitorBlockEntity extends CraftingBlockEntity implements IColorableTile {
|
||||
|
||||
@Environment(EnvType.CLIENT)
|
||||
private Integer dspList;
|
||||
|
||||
@Environment(EnvType.CLIENT)
|
||||
private boolean updateList;
|
||||
|
||||
private IAEItemStack dspPlay;
|
||||
private AEColor paintedColor = AEColor.TRANSPARENT;
|
||||
|
||||
public CraftingMonitorBlockEntity(BlockEntityType<?> tileEntityTypeIn) {
|
||||
super(tileEntityTypeIn);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean readFromStream(final PacketByteBuf data) throws IOException {
|
||||
final boolean c = super.readFromStream(data);
|
||||
final AEColor oldPaintedColor = this.paintedColor;
|
||||
this.paintedColor = AEColor.values()[data.readByte()];
|
||||
|
||||
final boolean hasItem = data.readBoolean();
|
||||
|
||||
if (hasItem) {
|
||||
this.dspPlay = AEItemStack.fromPacket(data);
|
||||
} else {
|
||||
this.dspPlay = null;
|
||||
}
|
||||
|
||||
this.setUpdateList(true);
|
||||
return oldPaintedColor != this.paintedColor || c; // tesr!
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void writeToStream(final PacketByteBuf data) throws IOException {
|
||||
super.writeToStream(data);
|
||||
data.writeByte(this.paintedColor.ordinal());
|
||||
|
||||
if (this.dspPlay == null) {
|
||||
data.writeBoolean(false);
|
||||
} else {
|
||||
data.writeBoolean(true);
|
||||
this.dspPlay.writeToPacket(data);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromTag(BlockState state, final CompoundTag data) {
|
||||
super.fromTag(state, data);
|
||||
if (data.contains("paintedColor")) {
|
||||
this.paintedColor = AEColor.values()[data.getByte("paintedColor")];
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompoundTag toTag(final CompoundTag data) {
|
||||
super.toTag(data);
|
||||
data.putByte("paintedColor", (byte) this.paintedColor.ordinal());
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAccelerator() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isStatus() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public void setJob(final IAEItemStack is) {
|
||||
if ((is == null) != (this.dspPlay == null)) {
|
||||
this.dspPlay = is == null ? null : is.copy();
|
||||
this.markForUpdate();
|
||||
} else if (is != null && this.dspPlay != null) {
|
||||
if (is.getStackSize() != this.dspPlay.getStackSize()) {
|
||||
this.dspPlay = is.copy();
|
||||
this.markForUpdate();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public IAEItemStack getJobProgress() {
|
||||
return this.dspPlay; // AEItemStack.create( new ItemStack( Items.DIAMOND, 64 ) );
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
|
||||
public Integer getDisplayList() {
|
||||
return this.dspList;
|
||||
}
|
||||
|
||||
public void setDisplayList(final Integer dspList) {
|
||||
this.dspList = dspList;
|
||||
}
|
||||
|
||||
public boolean isUpdateList() {
|
||||
return this.updateList;
|
||||
}
|
||||
|
||||
public void setUpdateList(final boolean updateList) {
|
||||
this.updateList = updateList;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ItemStack getItemFromTile(final Object obj) {
|
||||
final Optional<ItemStack> is = AEApi.instance().definitions().blocks().craftingMonitor().maybeStack(1);
|
||||
|
||||
return is.orElseGet(() -> super.getItemFromTile(obj));
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public CraftingMonitorModelData getRenderAttachmentData() {
|
||||
return new CraftingMonitorModelData(getUp(), getForward(), getConnections(), getColor());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package appeng.tile.crafting;
|
||||
|
||||
import java.util.EnumSet;
|
||||
import java.util.Objects;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
|
||||
import net.minecraft.util.math.Direction;
|
||||
|
||||
import appeng.api.util.AEColor;
|
||||
|
||||
public class CraftingMonitorModelData extends CraftingCubeModelData {
|
||||
|
||||
private final AEColor color;
|
||||
|
||||
public CraftingMonitorModelData(Direction up, Direction forward, EnumSet<Direction> connections, AEColor color) {
|
||||
super(up, forward, connections);
|
||||
this.color = Preconditions.checkNotNull(color);
|
||||
}
|
||||
|
||||
public AEColor getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
if (!super.equals(o)) {
|
||||
return false;
|
||||
}
|
||||
CraftingMonitorModelData that = (CraftingMonitorModelData) o;
|
||||
return color == that.color;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(super.hashCode(), color);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
* 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.crafting;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import net.minecraft.block.entity.BlockEntityType;
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
import appeng.api.AEApi;
|
||||
import appeng.api.definitions.IBlocks;
|
||||
import appeng.block.crafting.AbstractCraftingUnitBlock;
|
||||
|
||||
public class CraftingStorageBlockEntity extends CraftingBlockEntity {
|
||||
private static final int KILO_SCALAR = 1024;
|
||||
|
||||
public CraftingStorageBlockEntity(BlockEntityType<?> tileEntityTypeIn) {
|
||||
super(tileEntityTypeIn);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ItemStack getItemFromTile(final Object obj) {
|
||||
final IBlocks blocks = AEApi.instance().definitions().blocks();
|
||||
final int storage = ((CraftingBlockEntity) obj).getStorageBytes() / KILO_SCALAR;
|
||||
|
||||
Optional<ItemStack> is;
|
||||
|
||||
switch (storage) {
|
||||
case 1:
|
||||
is = blocks.craftingStorage1k().maybeStack(1);
|
||||
break;
|
||||
case 4:
|
||||
is = blocks.craftingStorage4k().maybeStack(1);
|
||||
break;
|
||||
case 16:
|
||||
is = blocks.craftingStorage16k().maybeStack(1);
|
||||
break;
|
||||
case 64:
|
||||
is = blocks.craftingStorage64k().maybeStack(1);
|
||||
break;
|
||||
default:
|
||||
is = Optional.empty();
|
||||
break;
|
||||
}
|
||||
|
||||
return is.orElseGet(() -> super.getItemFromTile(obj));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAccelerator() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isStorage() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getStorageBytes() {
|
||||
if (this.world == null || this.notLoaded() || this.isRemoved()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
final AbstractCraftingUnitBlock unit = (AbstractCraftingUnitBlock) this.world.getBlockState(this.pos)
|
||||
.getBlock();
|
||||
switch (unit.type) {
|
||||
default:
|
||||
case STORAGE_1K:
|
||||
return 1024;
|
||||
case STORAGE_4K:
|
||||
return 4 * 1024;
|
||||
case STORAGE_16K:
|
||||
return 16 * 1024;
|
||||
case STORAGE_64K:
|
||||
return 64 * 1024;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,574 @@
|
||||
/*
|
||||
* 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.crafting;
|
||||
|
||||
import alexiil.mc.lib.attributes.Simulation;
|
||||
import alexiil.mc.lib.attributes.item.FixedItemInv;
|
||||
import appeng.api.AEApi;
|
||||
import appeng.api.config.*;
|
||||
import appeng.api.definitions.ITileDefinition;
|
||||
import appeng.api.implementations.IPowerChannelState;
|
||||
import appeng.api.implementations.IUpgradeableHost;
|
||||
import appeng.api.implementations.tiles.ICraftingMachine;
|
||||
import appeng.api.networking.IGridNode;
|
||||
import appeng.api.networking.crafting.ICraftingPatternDetails;
|
||||
import appeng.api.networking.events.MENetworkEventSubscribe;
|
||||
import appeng.api.networking.events.MENetworkPowerStatusChange;
|
||||
import appeng.api.networking.ticking.IGridTickable;
|
||||
import appeng.api.networking.ticking.TickRateModulation;
|
||||
import appeng.api.networking.ticking.TickingRequest;
|
||||
import appeng.api.storage.data.IAEItemStack;
|
||||
import appeng.api.util.AECableType;
|
||||
import appeng.api.util.AEPartLocation;
|
||||
import appeng.api.util.DimensionalCoord;
|
||||
import appeng.api.util.IConfigManager;
|
||||
import appeng.container.ContainerNull;
|
||||
import appeng.core.sync.network.NetworkHandler;
|
||||
import appeng.core.sync.network.TargetPoint;
|
||||
import appeng.core.sync.packets.AssemblerAnimationPacket;
|
||||
import appeng.items.misc.EncodedPatternItem;
|
||||
import appeng.me.GridAccessException;
|
||||
import appeng.parts.automation.DefinitionUpgradeInventory;
|
||||
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.InvOperation;
|
||||
import appeng.util.inv.WrapperChainedItemHandler;
|
||||
import appeng.util.inv.WrapperFilteredItemHandler;
|
||||
import appeng.util.inv.filter.IAEItemFilter;
|
||||
import appeng.util.item.AEItemStack;
|
||||
import net.fabricmc.api.EnvType;
|
||||
import net.fabricmc.api.Environment;
|
||||
import net.minecraft.block.BlockState;
|
||||
import net.minecraft.block.entity.BlockEntity;
|
||||
import net.minecraft.block.entity.BlockEntityType;
|
||||
import net.minecraft.inventory.CraftingInventory;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.network.PacketByteBuf;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.math.Direction;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
public class MolecularAssemblerBlockEntity extends AENetworkInvBlockEntity
|
||||
implements IUpgradeableHost, IConfigManagerHost, IGridTickable, ICraftingMachine, IPowerChannelState {
|
||||
|
||||
public static final String INVENTORY_MAIN = "molecular_assembler";
|
||||
|
||||
private final CraftingInventory craftingInv;
|
||||
private final AppEngInternalInventory gridInv = new AppEngInternalInventory(this, 9 + 1, 1);
|
||||
private final AppEngInternalInventory patternInv = new AppEngInternalInventory(this, 1, 1);
|
||||
private final FixedItemInv gridInvExt = new WrapperFilteredItemHandler(this.gridInv, new CraftingGridFilter());
|
||||
private final FixedItemInv internalInv = new WrapperChainedItemHandler(this.gridInv, this.patternInv);
|
||||
private final IConfigManager settings;
|
||||
private final UpgradeInventory upgrades;
|
||||
private boolean isPowered = false;
|
||||
private AEPartLocation pushDirection = AEPartLocation.INTERNAL;
|
||||
private ItemStack myPattern = ItemStack.EMPTY;
|
||||
private ICraftingPatternDetails myPlan = null;
|
||||
private double progress = 0;
|
||||
private boolean isAwake = false;
|
||||
private boolean forcePlan = false;
|
||||
private boolean reboot = true;
|
||||
|
||||
@Environment(EnvType.CLIENT)
|
||||
private AssemblerAnimationStatus animationStatus;
|
||||
|
||||
public MolecularAssemblerBlockEntity(BlockEntityType<?> tileEntityTypeIn) {
|
||||
super(tileEntityTypeIn);
|
||||
final ITileDefinition assembler = AEApi.instance().definitions().blocks().molecularAssembler();
|
||||
|
||||
this.settings = new ConfigManager(this);
|
||||
this.settings.registerSetting(Settings.REDSTONE_CONTROLLED, RedstoneMode.IGNORE);
|
||||
this.getProxy().setIdlePowerUsage(0.0);
|
||||
this.upgrades = new DefinitionUpgradeInventory(assembler, this, this.getUpgradeSlots());
|
||||
this.craftingInv = new CraftingInventory(new ContainerNull(), 3, 3);
|
||||
|
||||
}
|
||||
|
||||
private int getUpgradeSlots() {
|
||||
return 5;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean pushPattern(final ICraftingPatternDetails patternDetails, final CraftingInventory table,
|
||||
final Direction where) {
|
||||
if (this.myPattern.isEmpty()) {
|
||||
boolean isEmpty = ItemHandlerUtil.isEmpty(this.gridInv) && ItemHandlerUtil.isEmpty(this.patternInv);
|
||||
|
||||
if (isEmpty && patternDetails.isCraftable()) {
|
||||
this.forcePlan = true;
|
||||
this.myPlan = patternDetails;
|
||||
this.pushDirection = AEPartLocation.fromFacing(where);
|
||||
|
||||
for (int x = 0; x < table.size(); x++) {
|
||||
this.gridInv.setInvStack(x, table.getStack(x), Simulation.ACTION);
|
||||
}
|
||||
|
||||
this.updateSleepiness();
|
||||
this.saveChanges();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void updateSleepiness() {
|
||||
final boolean wasEnabled = this.isAwake;
|
||||
this.isAwake = this.myPlan != null && this.hasMats() || this.canPush();
|
||||
if (wasEnabled != this.isAwake) {
|
||||
try {
|
||||
if (this.isAwake) {
|
||||
this.getProxy().getTick().wakeDevice(this.getProxy().getNode());
|
||||
} else {
|
||||
this.getProxy().getTick().sleepDevice(this.getProxy().getNode());
|
||||
}
|
||||
} catch (final GridAccessException e) {
|
||||
// :P
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean canPush() {
|
||||
return !this.gridInv.getInvStack(9).isEmpty();
|
||||
}
|
||||
|
||||
private boolean hasMats() {
|
||||
if (this.myPlan == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int x = 0; x < this.craftingInv.size(); x++) {
|
||||
this.craftingInv.setStack(x, this.gridInv.getInvStack(x));
|
||||
}
|
||||
|
||||
return !this.myPlan.getOutput(this.craftingInv, this.getWorld()).isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean acceptsPlans() {
|
||||
return ItemHandlerUtil.isEmpty(this.patternInv);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInstalledUpgrades(final Upgrades u) {
|
||||
return this.upgrades.getInstalledUpgrades(u);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean readFromStream(final PacketByteBuf data) throws IOException {
|
||||
final boolean c = super.readFromStream(data);
|
||||
final boolean oldPower = this.isPowered;
|
||||
this.isPowered = data.readBoolean();
|
||||
return this.isPowered != oldPower || c;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void writeToStream(final PacketByteBuf data) throws IOException {
|
||||
super.writeToStream(data);
|
||||
data.writeBoolean(this.isPowered);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompoundTag toTag(final CompoundTag data) {
|
||||
super.toTag(data);
|
||||
if (this.forcePlan && this.myPlan != null) {
|
||||
final ItemStack pattern = this.myPlan.getPattern();
|
||||
if (!pattern.isEmpty()) {
|
||||
final CompoundTag compound = new CompoundTag();
|
||||
pattern.toTag(compound);
|
||||
data.put("myPlan", compound);
|
||||
data.putInt("pushDirection", this.pushDirection.ordinal());
|
||||
}
|
||||
}
|
||||
|
||||
this.upgrades.writeToNBT(data, "upgrades");
|
||||
this.settings.writeToNBT(data);
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromTag(BlockState state, final CompoundTag data) {
|
||||
super.fromTag(state, data);
|
||||
if (data.contains("myPlan")) {
|
||||
final ItemStack myPat = ItemStack.fromTag(data.getCompound("myPlan"));
|
||||
|
||||
if (!myPat.isEmpty() && myPat.getItem() instanceof EncodedPatternItem) {
|
||||
final World w = this.getWorld();
|
||||
final EncodedPatternItem iep = (EncodedPatternItem) myPat.getItem();
|
||||
final ICraftingPatternDetails ph = iep.getPatternForItem(myPat, w);
|
||||
if (ph != null && ph.isCraftable()) {
|
||||
this.forcePlan = true;
|
||||
this.myPlan = ph;
|
||||
this.pushDirection = AEPartLocation.fromOrdinal(data.getInt("pushDirection"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.upgrades.readFromNBT(data, "upgrades");
|
||||
this.settings.readFromNBT(data);
|
||||
this.recalculatePlan();
|
||||
}
|
||||
|
||||
private void recalculatePlan() {
|
||||
this.reboot = true;
|
||||
|
||||
if (this.forcePlan) {
|
||||
return;
|
||||
}
|
||||
|
||||
final ItemStack is = this.patternInv.getInvStack(0);
|
||||
|
||||
if (!is.isEmpty() && is.getItem() instanceof EncodedPatternItem) {
|
||||
if (!ItemStack.areItemsEqual(is, this.myPattern)) {
|
||||
final World w = this.getWorld();
|
||||
final EncodedPatternItem iep = (EncodedPatternItem) is.getItem();
|
||||
final ICraftingPatternDetails ph = iep.getPatternForItem(is, w);
|
||||
|
||||
if (ph != null && ph.isCraftable()) {
|
||||
this.progress = 0;
|
||||
this.myPattern = is;
|
||||
this.myPlan = ph;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
this.progress = 0;
|
||||
this.forcePlan = false;
|
||||
this.myPlan = null;
|
||||
this.myPattern = ItemStack.EMPTY;
|
||||
this.pushDirection = AEPartLocation.INTERNAL;
|
||||
}
|
||||
|
||||
this.updateSleepiness();
|
||||
}
|
||||
|
||||
@Override
|
||||
public AECableType getCableConnectionType(final AEPartLocation dir) {
|
||||
return AECableType.COVERED;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DimensionalCoord getLocation() {
|
||||
return new DimensionalCoord(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IConfigManager getConfigManager() {
|
||||
return this.settings;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FixedItemInv getInventoryByName(final String name) {
|
||||
if (name.equals("upgrades")) {
|
||||
return this.upgrades;
|
||||
}
|
||||
|
||||
if (name.equals("molecular_assembler")) {
|
||||
return this.internalInv;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateSetting(final IConfigManager manager, final Settings settingName, final Enum<?> newValue) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public FixedItemInv getInternalInventory() {
|
||||
return this.internalInv;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected FixedItemInv getItemHandlerForSide(Direction side) {
|
||||
return this.gridInvExt;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChangeInventory(final FixedItemInv inv, final int slot, final InvOperation mc,
|
||||
final ItemStack removed, final ItemStack added) {
|
||||
if (inv == this.gridInv || inv == this.patternInv) {
|
||||
this.recalculatePlan();
|
||||
}
|
||||
}
|
||||
|
||||
public int getCraftingProgress() {
|
||||
return (int) this.progress;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getDrops(final World w, final BlockPos pos, final List<ItemStack> drops) {
|
||||
super.getDrops(w, pos, drops);
|
||||
|
||||
for (int h = 0; h < this.upgrades.getSlotCount(); h++) {
|
||||
final ItemStack is = this.upgrades.getInvStack(h);
|
||||
if (!is.isEmpty()) {
|
||||
drops.add(is);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public TickingRequest getTickingRequest(final IGridNode node) {
|
||||
this.recalculatePlan();
|
||||
this.updateSleepiness();
|
||||
return new TickingRequest(1, 1, !this.isAwake, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TickRateModulation tickingRequest(final IGridNode node, int ticksSinceLastCall) {
|
||||
if (!this.gridInv.getInvStack(9).isEmpty()) {
|
||||
this.pushOut(this.gridInv.getInvStack(9));
|
||||
|
||||
// did it eject?
|
||||
if (this.gridInv.getInvStack(9).isEmpty()) {
|
||||
this.saveChanges();
|
||||
}
|
||||
|
||||
this.ejectHeldItems();
|
||||
this.updateSleepiness();
|
||||
this.progress = 0;
|
||||
return this.isAwake ? TickRateModulation.IDLE : TickRateModulation.SLEEP;
|
||||
}
|
||||
|
||||
if (this.myPlan == null) {
|
||||
this.updateSleepiness();
|
||||
return TickRateModulation.SLEEP;
|
||||
}
|
||||
|
||||
if (this.reboot) {
|
||||
ticksSinceLastCall = 1;
|
||||
}
|
||||
|
||||
if (!this.isAwake) {
|
||||
return TickRateModulation.SLEEP;
|
||||
}
|
||||
|
||||
this.reboot = false;
|
||||
int speed = 10;
|
||||
switch (this.upgrades.getInstalledUpgrades(Upgrades.SPEED)) {
|
||||
case 0:
|
||||
this.progress += this.userPower(ticksSinceLastCall, speed = 10, 1.0);
|
||||
break;
|
||||
case 1:
|
||||
this.progress += this.userPower(ticksSinceLastCall, speed = 13, 1.3);
|
||||
break;
|
||||
case 2:
|
||||
this.progress += this.userPower(ticksSinceLastCall, speed = 17, 1.7);
|
||||
break;
|
||||
case 3:
|
||||
this.progress += this.userPower(ticksSinceLastCall, speed = 20, 2.0);
|
||||
break;
|
||||
case 4:
|
||||
this.progress += this.userPower(ticksSinceLastCall, speed = 25, 2.5);
|
||||
break;
|
||||
case 5:
|
||||
this.progress += this.userPower(ticksSinceLastCall, speed = 50, 5.0);
|
||||
break;
|
||||
}
|
||||
|
||||
if (this.progress >= 100) {
|
||||
for (int x = 0; x < this.craftingInv.size(); x++) {
|
||||
this.craftingInv.setStack(x, this.gridInv.getInvStack(x));
|
||||
}
|
||||
|
||||
this.progress = 0;
|
||||
final ItemStack output = this.myPlan.getOutput(this.craftingInv, this.getWorld());
|
||||
if (!output.isEmpty()) {
|
||||
// FIXME FABRIC BasicEventHooks.firePlayerCraftingEvent((PlayerEntity) FakePlayer.getOrCreate((ServerWorld) this.getWorld()), output,
|
||||
// FIXME FABRIC this.craftingInv);
|
||||
|
||||
this.pushOut(output.copy());
|
||||
|
||||
for (int x = 0; x < this.craftingInv.size(); x++) {
|
||||
this.gridInv.setInvStack(x, Platform.getRecipeRemainder(this.craftingInv.getStack(x)), Simulation.ACTION);
|
||||
}
|
||||
|
||||
if (ItemHandlerUtil.isEmpty(this.patternInv)) {
|
||||
this.forcePlan = false;
|
||||
this.myPlan = null;
|
||||
this.pushDirection = AEPartLocation.INTERNAL;
|
||||
}
|
||||
|
||||
this.ejectHeldItems();
|
||||
|
||||
final IAEItemStack item = AEItemStack.fromItemStack(output);
|
||||
if (item != null) {
|
||||
final TargetPoint where = new TargetPoint(this.pos.getX(), this.pos.getY(), this.pos.getZ(), 32,
|
||||
this.world);
|
||||
NetworkHandler.instance()
|
||||
.sendToAllAround(new AssemblerAnimationPacket(this.pos, (byte) speed, item), where);
|
||||
}
|
||||
|
||||
this.saveChanges();
|
||||
this.updateSleepiness();
|
||||
return this.isAwake ? TickRateModulation.IDLE : TickRateModulation.SLEEP;
|
||||
}
|
||||
}
|
||||
|
||||
return TickRateModulation.FASTER;
|
||||
}
|
||||
|
||||
private void ejectHeldItems() {
|
||||
if (this.gridInv.getInvStack(9).isEmpty()) {
|
||||
for (int x = 0; x < 9; x++) {
|
||||
final ItemStack is = this.gridInv.getInvStack(x);
|
||||
if (!is.isEmpty()) {
|
||||
if (this.myPlan == null || !this.myPlan.isValidItemForSlot(x, is, this.world)) {
|
||||
this.gridInv.setInvStack(9, is, Simulation.ACTION);
|
||||
this.gridInv.setInvStack(x, ItemStack.EMPTY, Simulation.ACTION);
|
||||
this.saveChanges();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private int userPower(final int ticksPassed, final int bonusValue, final double acceleratorTax) {
|
||||
try {
|
||||
return (int) (this.getProxy().getEnergy().extractAEPower(ticksPassed * bonusValue * acceleratorTax,
|
||||
Actionable.MODULATE, PowerMultiplier.CONFIG) / acceleratorTax);
|
||||
} catch (final GridAccessException e) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
private void pushOut(ItemStack output) {
|
||||
if (this.pushDirection == AEPartLocation.INTERNAL) {
|
||||
for (final Direction d : Direction.values()) {
|
||||
output = this.pushTo(output, d);
|
||||
}
|
||||
} else {
|
||||
output = this.pushTo(output, this.pushDirection.getFacing());
|
||||
}
|
||||
|
||||
if (output.isEmpty() && this.forcePlan) {
|
||||
this.forcePlan = false;
|
||||
this.recalculatePlan();
|
||||
}
|
||||
|
||||
this.gridInv.setInvStack(9, output, Simulation.ACTION);
|
||||
}
|
||||
|
||||
private ItemStack pushTo(ItemStack output, final Direction d) {
|
||||
if (output.isEmpty()) {
|
||||
return output;
|
||||
}
|
||||
|
||||
final BlockEntity te = this.getWorld().getBlockEntity(this.pos.offset(d));
|
||||
|
||||
if (te == null) {
|
||||
return output;
|
||||
}
|
||||
|
||||
final InventoryAdaptor adaptor = InventoryAdaptor.getAdaptor(te, d.getOpposite());
|
||||
|
||||
if (adaptor == null) {
|
||||
return output;
|
||||
}
|
||||
|
||||
final int size = output.getCount();
|
||||
output = adaptor.addItems(output);
|
||||
final int newSize = output.isEmpty() ? 0 : output.getCount();
|
||||
|
||||
if (size != newSize) {
|
||||
this.saveChanges();
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
@MENetworkEventSubscribe
|
||||
public void onPowerEvent(final MENetworkPowerStatusChange p) {
|
||||
this.updatePowerState();
|
||||
}
|
||||
|
||||
private void updatePowerState() {
|
||||
boolean newState = false;
|
||||
|
||||
try {
|
||||
newState = this.getProxy().isActive() && this.getProxy().getEnergy().extractAEPower(1, Actionable.SIMULATE,
|
||||
PowerMultiplier.CONFIG) > 0.0001;
|
||||
} catch (final GridAccessException ignored) {
|
||||
|
||||
}
|
||||
|
||||
if (newState != this.isPowered) {
|
||||
this.isPowered = newState;
|
||||
this.markForUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPowered() {
|
||||
return this.isPowered;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isActive() {
|
||||
return this.isPowered;
|
||||
}
|
||||
|
||||
@Environment(EnvType.CLIENT)
|
||||
public void setAnimationStatus(@Nullable AssemblerAnimationStatus status) {
|
||||
this.animationStatus = status;
|
||||
}
|
||||
|
||||
@Environment(EnvType.CLIENT)
|
||||
@Nullable
|
||||
public AssemblerAnimationStatus getAnimationStatus() {
|
||||
return this.animationStatus;
|
||||
}
|
||||
|
||||
private class CraftingGridFilter implements IAEItemFilter {
|
||||
private boolean hasPattern() {
|
||||
return MolecularAssemblerBlockEntity.this.myPlan != null
|
||||
&& !ItemHandlerUtil.isEmpty(MolecularAssemblerBlockEntity.this.patternInv);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean allowExtract(FixedItemInv inv, int slot, int amount) {
|
||||
return slot == 9;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean allowInsert(FixedItemInv inv, int slot, ItemStack stack) {
|
||||
if (slot >= 9) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (this.hasPattern()) {
|
||||
return MolecularAssemblerBlockEntity.this.myPlan.isValidItemForSlot(slot, stack,
|
||||
MolecularAssemblerBlockEntity.this.getWorld());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
/*
|
||||
* 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.crafting;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import appeng.mixins.RenderPhaseMixin;
|
||||
import net.fabricmc.api.EnvType;
|
||||
import net.minecraft.client.MinecraftClient;
|
||||
import net.minecraft.client.render.*;
|
||||
import net.minecraft.client.render.block.entity.BlockEntityRenderDispatcher;
|
||||
import net.minecraft.client.render.block.entity.BlockEntityRenderer;
|
||||
import net.minecraft.client.render.item.ItemRenderer;
|
||||
import net.minecraft.client.texture.SpriteAtlasTexture;
|
||||
import net.minecraft.client.util.ModelIdentifier;
|
||||
import net.minecraft.client.util.math.MatrixStack;
|
||||
|
||||
import org.lwjgl.opengl.GL11;
|
||||
|
||||
import net.minecraft.client.render.model.BakedModel;
|
||||
import net.minecraft.client.render.model.json.ModelTransformation;
|
||||
import net.minecraft.item.BlockItem;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.Identifier;
|
||||
import net.fabricmc.api.Environment;
|
||||
|
||||
import appeng.client.render.effects.ParticleTypes;
|
||||
import appeng.core.AppEng;
|
||||
|
||||
/**
|
||||
* Renders the item currently being crafted by the molecular assembler, as well
|
||||
* as the light strip when it's powered.
|
||||
*/
|
||||
@Environment(EnvType.CLIENT)
|
||||
public class MolecularAssemblerRenderer extends BlockEntityRenderer<MolecularAssemblerBlockEntity> {
|
||||
|
||||
public static final Identifier LIGHTS_MODEL = new Identifier(AppEng.MOD_ID,
|
||||
"block/molecular_assembler_lights");
|
||||
|
||||
private static final RenderLayer MC_161917_RENDERTYPE_FIX = createRenderType();
|
||||
|
||||
private final Random particleRandom = new Random();
|
||||
|
||||
public MolecularAssemblerRenderer(BlockEntityRenderDispatcher rendererDispatcherIn) {
|
||||
super(rendererDispatcherIn);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(MolecularAssemblerBlockEntity molecularAssembler, float partialTicks, MatrixStack ms,
|
||||
VertexConsumerProvider bufferIn, int combinedLightIn, int combinedOverlayIn) {
|
||||
|
||||
AssemblerAnimationStatus status = molecularAssembler.getAnimationStatus();
|
||||
if (status != null) {
|
||||
if (!MinecraftClient.getInstance().isPaused()) {
|
||||
if (status.isExpired()) {
|
||||
molecularAssembler.setAnimationStatus(null);
|
||||
}
|
||||
|
||||
status.setAccumulatedTicks(status.getAccumulatedTicks() + partialTicks);
|
||||
status.setTicksUntilParticles(status.getTicksUntilParticles() - partialTicks);
|
||||
}
|
||||
|
||||
renderStatus(molecularAssembler, ms, bufferIn, combinedLightIn, status);
|
||||
}
|
||||
|
||||
if (molecularAssembler.isPowered()) {
|
||||
renderPowerLight(ms, bufferIn, combinedLightIn, combinedOverlayIn);
|
||||
}
|
||||
}
|
||||
|
||||
private void renderPowerLight(MatrixStack ms, VertexConsumerProvider bufferIn, int combinedLightIn,
|
||||
int combinedOverlayIn) {
|
||||
// Render the translucent light overlay here instead of in the block, because
|
||||
// thanks to the following MC
|
||||
// bug, our particles would otherwise not be visible (because the glass pane
|
||||
// would also render as translucent,
|
||||
// even the fully transparent part)
|
||||
// https://bugs.mojang.com/browse/MC-161917
|
||||
MinecraftClient minecraft = MinecraftClient.getInstance();
|
||||
BakedModel lightsModel = minecraft.getBakedModelManager().getModel(new ModelIdentifier(LIGHTS_MODEL, ""));
|
||||
VertexConsumer buffer = bufferIn.getBuffer(MC_161917_RENDERTYPE_FIX);
|
||||
|
||||
minecraft.getBlockRenderManager().getModelRenderer().render(ms.peek(), buffer, null,
|
||||
lightsModel, 1, 1, 1, combinedLightIn, combinedOverlayIn);
|
||||
}
|
||||
|
||||
private void renderStatus(MolecularAssemblerBlockEntity molecularAssembler, MatrixStack ms,
|
||||
VertexConsumerProvider bufferIn, int combinedLightIn, AssemblerAnimationStatus status) {
|
||||
double centerX = molecularAssembler.getPos().getX() + 0.5f;
|
||||
double centerY = molecularAssembler.getPos().getY() + 0.5f;
|
||||
double centerZ = molecularAssembler.getPos().getZ() + 0.5f;
|
||||
|
||||
// Spawn crafting FX that fly towards the block's center
|
||||
MinecraftClient minecraft = MinecraftClient.getInstance();
|
||||
if (status.getTicksUntilParticles() <= 0) {
|
||||
status.setTicksUntilParticles(4);
|
||||
|
||||
if (AppEng.instance().shouldAddParticles(particleRandom)) {
|
||||
for (int x = 0; x < (int) Math.ceil(status.getSpeed() / 5.0); x++) {
|
||||
minecraft.particleManager.addParticle(ParticleTypes.CRAFTING, centerX, centerY, centerZ, 0, 0, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ItemStack is = status.getIs();
|
||||
|
||||
ItemRenderer itemRenderer = minecraft.getItemRenderer();
|
||||
ms.push();
|
||||
ms.translate(0.5, 0.5, 0.5); // Translate to center of block
|
||||
|
||||
if (!(is.getItem() instanceof BlockItem)) {
|
||||
ms.translate(0, -0.3f, 0);
|
||||
} else {
|
||||
ms.translate(0, -0.2f, 0);
|
||||
}
|
||||
|
||||
itemRenderer.renderItem(is, ModelTransformation.Mode.GROUND, combinedLightIn,
|
||||
OverlayTexture.DEFAULT_UV, ms, bufferIn);
|
||||
ms.pop();
|
||||
}
|
||||
|
||||
/**
|
||||
* See above for when this can be removed. It creates a RenderType that is
|
||||
* equivalent to {@link RenderLayer#getTranslucent()}, but enables alpha testing.
|
||||
* This prevents the fully transparents parts of the rendered block model from
|
||||
* occluding our particles.
|
||||
*/
|
||||
private static RenderLayer createRenderType() {
|
||||
RenderPhase.Texture mipmapBlockAtlasTexture = new RenderPhase.Texture(
|
||||
SpriteAtlasTexture.BLOCK_ATLAS_TEX, false, true);
|
||||
RenderPhase.Lightmap disableLightmap = new RenderPhase.Lightmap(false);
|
||||
RenderLayer.MultiPhaseParameters glState = RenderLayer.MultiPhaseParameters.builder()
|
||||
.texture(mipmapBlockAtlasTexture)
|
||||
.transparency(RenderPhaseMixin.getTranslucentTransparency()).alpha(new RenderPhase.Alpha(0.05F))
|
||||
.lightmap(disableLightmap).build(true);
|
||||
|
||||
return RenderLayer.of("ae2_translucent_alphatest", VertexFormats.POSITION_COLOR_TEXTURE_LIGHT,
|
||||
GL11.GL_QUADS, 256, glState);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
* 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.grid;
|
||||
|
||||
import net.minecraft.block.BlockState;
|
||||
import net.minecraft.block.entity.BlockEntityType;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
|
||||
import appeng.api.networking.IGridNode;
|
||||
import appeng.api.networking.security.IActionHost;
|
||||
import appeng.api.util.AECableType;
|
||||
import appeng.api.util.AEPartLocation;
|
||||
import appeng.api.util.DimensionalCoord;
|
||||
import appeng.me.helpers.AENetworkProxy;
|
||||
import appeng.me.helpers.IGridProxyable;
|
||||
import appeng.tile.AEBaseBlockEntity;
|
||||
|
||||
public class AENetworkBlockEntity extends AEBaseBlockEntity implements IActionHost, IGridProxyable {
|
||||
|
||||
private final AENetworkProxy gridProxy = this.createProxy();
|
||||
|
||||
public AENetworkBlockEntity(BlockEntityType<?> tileEntityTypeIn) {
|
||||
super(tileEntityTypeIn);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromTag(BlockState state, final CompoundTag data) {
|
||||
super.fromTag(state, data);
|
||||
this.getProxy().readFromNBT(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompoundTag toTag(final CompoundTag data) {
|
||||
super.toTag(data);
|
||||
this.getProxy().writeToNBT(data);
|
||||
return data;
|
||||
}
|
||||
|
||||
protected AENetworkProxy createProxy() {
|
||||
return new AENetworkProxy(this, "proxy", this.getItemFromTile(this), true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IGridNode getGridNode(final AEPartLocation dir) {
|
||||
return this.getProxy().getNode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public AECableType getCableConnectionType(final AEPartLocation dir) {
|
||||
return AECableType.SMART;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChunkUnloaded() {
|
||||
super.onChunkUnloaded();
|
||||
this.getProxy().onChunkUnloaded();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReady() {
|
||||
super.onReady();
|
||||
this.getProxy().onReady();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void markRemoved() {
|
||||
super.markRemoved();
|
||||
this.getProxy().remove();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancelRemoval() {
|
||||
super.cancelRemoval();
|
||||
this.getProxy().validate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public AENetworkProxy getProxy() {
|
||||
return this.gridProxy;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DimensionalCoord getLocation() {
|
||||
return new DimensionalCoord(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void gridChanged() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public IGridNode getActionableNode() {
|
||||
return this.getProxy().getNode();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
* 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.grid;
|
||||
|
||||
import net.minecraft.block.BlockState;
|
||||
import net.minecraft.block.entity.BlockEntityType;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
|
||||
import appeng.api.networking.IGridNode;
|
||||
import appeng.api.networking.security.IActionHost;
|
||||
import appeng.api.util.AEPartLocation;
|
||||
import appeng.me.helpers.AENetworkProxy;
|
||||
import appeng.me.helpers.IGridProxyable;
|
||||
import appeng.tile.AEBaseInvBlockEntity;
|
||||
|
||||
public abstract class AENetworkInvBlockEntity extends AEBaseInvBlockEntity implements IActionHost, IGridProxyable {
|
||||
|
||||
private final AENetworkProxy gridProxy = new AENetworkProxy(this, "proxy", this.getItemFromTile(this), true);
|
||||
|
||||
public AENetworkInvBlockEntity(BlockEntityType<?> tileEntityTypeIn) {
|
||||
super(tileEntityTypeIn);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromTag(BlockState state, final CompoundTag data) {
|
||||
super.fromTag(state, data);
|
||||
this.getProxy().readFromNBT(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompoundTag toTag(final CompoundTag data) {
|
||||
super.toTag(data);
|
||||
this.getProxy().writeToNBT(data);
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AENetworkProxy getProxy() {
|
||||
return this.gridProxy;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void gridChanged() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public IGridNode getGridNode(final AEPartLocation dir) {
|
||||
return this.getProxy().getNode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChunkUnloaded() {
|
||||
super.onChunkUnloaded();
|
||||
this.getProxy().onChunkUnloaded();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReady() {
|
||||
super.onReady();
|
||||
this.getProxy().onReady();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void markRemoved() {
|
||||
super.markRemoved();
|
||||
this.getProxy().remove();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancelRemoval() {
|
||||
super.cancelRemoval();
|
||||
this.getProxy().validate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public IGridNode getActionableNode() {
|
||||
return this.getProxy().getNode();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
/*
|
||||
* 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.grindstone;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import net.minecraft.block.BlockState;
|
||||
import net.minecraft.block.entity.BlockEntity;
|
||||
import net.minecraft.block.entity.BlockEntityType;
|
||||
import net.minecraft.network.PacketByteBuf;
|
||||
import net.minecraft.util.Tickable;
|
||||
import net.minecraft.util.math.Direction;
|
||||
|
||||
import appeng.api.implementations.tiles.ICrankable;
|
||||
import appeng.tile.AEBaseBlockEntity;
|
||||
|
||||
public class CrankBlockEntity extends AEBaseBlockEntity implements Tickable {
|
||||
|
||||
private final int ticksPerRotation = 18;
|
||||
|
||||
// sided values..
|
||||
private float visibleRotation = 0; // This is in degrees
|
||||
private int charge = 0;
|
||||
|
||||
private int hits = 0;
|
||||
private int rotation = 0;
|
||||
|
||||
public CrankBlockEntity(BlockEntityType<?> tileEntityTypeIn) {
|
||||
super(tileEntityTypeIn);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tick() {
|
||||
if (this.rotation > 0) {
|
||||
this.setVisibleRotation(this.getVisibleRotation() - 360.0f / (this.ticksPerRotation));
|
||||
this.charge++;
|
||||
if (this.charge >= this.ticksPerRotation) {
|
||||
this.charge -= this.ticksPerRotation;
|
||||
final ICrankable g = this.getGrinder();
|
||||
if (g != null) {
|
||||
g.applyTurn();
|
||||
}
|
||||
}
|
||||
|
||||
this.rotation--;
|
||||
}
|
||||
}
|
||||
|
||||
private ICrankable getGrinder() {
|
||||
if (isClient()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final Direction grinder = this.getUp().getOpposite();
|
||||
final BlockEntity te = this.world.getBlockEntity(this.pos.offset(grinder));
|
||||
if (te instanceof ICrankable) {
|
||||
return (ICrankable) te;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean readFromStream(final PacketByteBuf data) throws IOException {
|
||||
final boolean c = super.readFromStream(data);
|
||||
this.rotation = data.readInt();
|
||||
return c;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void writeToStream(final PacketByteBuf data) throws IOException {
|
||||
super.writeToStream(data);
|
||||
data.writeInt(this.rotation);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setOrientation(final Direction inForward, final Direction inUp) {
|
||||
super.setOrientation(inForward, inUp);
|
||||
final BlockState state = this.world.getBlockState(this.pos);
|
||||
state.getBlock().neighborUpdate(state, this.world, this.pos, state.getBlock(), this.pos, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* return true if this should count towards stats.
|
||||
*/
|
||||
public boolean power() {
|
||||
if (isClient()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (this.rotation < 3) {
|
||||
final ICrankable g = this.getGrinder();
|
||||
if (g != null) {
|
||||
if (g.canTurn()) {
|
||||
this.hits = 0;
|
||||
this.rotation += this.ticksPerRotation;
|
||||
this.markForUpdate();
|
||||
return true;
|
||||
} else {
|
||||
this.hits++;
|
||||
if (this.hits > 10) {
|
||||
this.world.breakBlock(this.pos, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public float getVisibleRotation() {
|
||||
return this.visibleRotation;
|
||||
}
|
||||
|
||||
private void setVisibleRotation(final float visibleRotation) {
|
||||
this.visibleRotation = visibleRotation;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
/*
|
||||
* 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.grindstone;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import alexiil.mc.lib.attributes.Simulation;
|
||||
import alexiil.mc.lib.attributes.item.FixedItemInv;
|
||||
import net.minecraft.block.BlockState;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.block.entity.BlockEntityType;
|
||||
import net.minecraft.util.math.Direction;
|
||||
|
||||
import appeng.api.implementations.tiles.ICrankable;
|
||||
import appeng.recipes.handlers.GrinderOptionalResult;
|
||||
import appeng.recipes.handlers.GrinderRecipe;
|
||||
import appeng.recipes.handlers.GrinderRecipes;
|
||||
import appeng.tile.AEBaseInvBlockEntity;
|
||||
import appeng.tile.inventory.AppEngInternalInventory;
|
||||
import appeng.util.InventoryAdaptor;
|
||||
import appeng.util.Platform;
|
||||
import appeng.util.inv.AdaptorFixedInv;
|
||||
import appeng.util.inv.InvOperation;
|
||||
import appeng.util.inv.WrapperFilteredItemHandler;
|
||||
import appeng.util.inv.filter.IAEItemFilter;
|
||||
|
||||
public class GrinderBlockEntity extends AEBaseInvBlockEntity implements ICrankable {
|
||||
private static final int SLOT_PROCESSING = 6;
|
||||
|
||||
private final AppEngInternalInventory inv = new AppEngInternalInventory(this, 7);
|
||||
private final FixedItemInv invExt = new WrapperFilteredItemHandler(this.inv, new GrinderFilter());
|
||||
private int points;
|
||||
|
||||
public GrinderBlockEntity(BlockEntityType<?> tileEntityTypeIn) {
|
||||
super(tileEntityTypeIn);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setOrientation(final Direction inForward, final Direction inUp) {
|
||||
super.setOrientation(inForward, inUp);
|
||||
final BlockState state = this.world.getBlockState(this.pos);
|
||||
state.getBlock().neighborUpdate(state, this.world, this.pos, state.getBlock(), this.pos, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FixedItemInv getInternalInventory() {
|
||||
return this.inv;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected FixedItemInv getItemHandlerForSide(Direction side) {
|
||||
return this.invExt;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChangeInventory(final FixedItemInv inv, final int slot, final InvOperation mc,
|
||||
final ItemStack removed, final ItemStack added) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canTurn() {
|
||||
if (isClient()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (this.inv.getInvStack(6).isEmpty()) // Add if there isn't one...
|
||||
{
|
||||
for (int x = 0; x < 3; x++) {
|
||||
ItemStack item = this.inv.getInvStack(x);
|
||||
if (item.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
GrinderRecipe r = GrinderRecipes.findForInput(world, item);
|
||||
if (r != null) {
|
||||
final ItemStack ais = item.copy();
|
||||
ais.setCount(r.getIngredientCount());
|
||||
item.decrement(r.getIngredientCount());
|
||||
|
||||
if (item.getCount() <= 0) {
|
||||
item = ItemStack.EMPTY;
|
||||
}
|
||||
|
||||
this.inv.setInvStack(x, item, Simulation.ACTION);
|
||||
this.inv.setInvStack(6, ais, Simulation.ACTION);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void applyTurn() {
|
||||
if (isClient()) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.points++;
|
||||
|
||||
final ItemStack processing = this.inv.getInvStack(SLOT_PROCESSING);
|
||||
GrinderRecipe r = GrinderRecipes.findForInput(world, processing);
|
||||
if (r != null) {
|
||||
if (r.getTurns() > this.points) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.points = 0;
|
||||
final InventoryAdaptor sia = new AdaptorFixedInv(this.inv.getSubInv(3, 6));
|
||||
|
||||
this.addItem(sia, r.getOutput());
|
||||
|
||||
for (GrinderOptionalResult optionalResult : r.getOptionalResults()) {
|
||||
final float chance = (Platform.getRandomInt() % 2000) / 2000.0f;
|
||||
|
||||
if (chance <= optionalResult.getChance()) {
|
||||
this.addItem(sia, optionalResult.getResult());
|
||||
}
|
||||
}
|
||||
|
||||
this.inv.setInvStack(6, ItemStack.EMPTY, Simulation.ACTION);
|
||||
}
|
||||
}
|
||||
|
||||
private void addItem(final InventoryAdaptor sia, final ItemStack output) {
|
||||
if (output.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
final ItemStack notAdded = sia.addItems(output);
|
||||
if (!notAdded.isEmpty()) {
|
||||
final List<ItemStack> out = new ArrayList<>();
|
||||
out.add(notAdded);
|
||||
|
||||
Platform.spawnDrops(this.world, this.pos.offset(this.getForward()), out);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canCrankAttach(final Direction directionToCrank) {
|
||||
return this.getUp() == directionToCrank;
|
||||
}
|
||||
|
||||
private class GrinderFilter implements IAEItemFilter {
|
||||
@Override
|
||||
public boolean allowExtract(FixedItemInv inv, int slotIndex, int amount) {
|
||||
return slotIndex >= 3 && slotIndex <= 5;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean allowInsert(FixedItemInv inv, int slotIndex, ItemStack stack) {
|
||||
if (!GrinderRecipes.isValidIngredient(world, stack)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return slotIndex >= 0 && slotIndex <= 2;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
|
||||
package appeng.tile.inventory;
|
||||
|
||||
import alexiil.mc.lib.attributes.Simulation;
|
||||
import alexiil.mc.lib.attributes.item.filter.ItemFilter;
|
||||
import alexiil.mc.lib.attributes.item.impl.DelegatingFixedItemInv;
|
||||
import appeng.api.AEApi;
|
||||
import appeng.api.storage.cells.ICellInventory;
|
||||
import appeng.api.storage.cells.ICellInventoryHandler;
|
||||
import appeng.util.inv.IAEAppEngInventory;
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
public class AppEngCellInventory extends DelegatingFixedItemInv {
|
||||
private static final ItemFilter CELL_FILTER = stack -> !stack.isEmpty()
|
||||
&& AEApi.instance().registries().cell().isCellHandled(stack);
|
||||
|
||||
private final ICellInventoryHandler<?>[] handlerForSlot;
|
||||
|
||||
public AppEngCellInventory(final IAEAppEngInventory host, final int slots) {
|
||||
super(new AppEngInternalInventory(host, slots, 1));
|
||||
this.handlerForSlot = new ICellInventoryHandler[slots];
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemFilter getFilterForSlot(int slot) {
|
||||
return CELL_FILTER;
|
||||
}
|
||||
|
||||
public void setHandler(final int slot, final ICellInventoryHandler<?> handler) {
|
||||
this.handlerForSlot[slot] = handler;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setInvStack(int slot, ItemStack to, Simulation simulation) {
|
||||
this.persist(slot);
|
||||
boolean result = super.setInvStack(slot, to, simulation);
|
||||
this.cleanup(slot);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack getInvStack(int slot) {
|
||||
this.persist(slot);
|
||||
return super.getInvStack(slot);
|
||||
}
|
||||
|
||||
private void persist(int slot) {
|
||||
if (this.handlerForSlot[slot] != null) {
|
||||
final ICellInventory<?> ci = this.handlerForSlot[slot].getCellInv();
|
||||
if (ci != null) {
|
||||
ci.persist();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void cleanup(int slot) {
|
||||
if (this.handlerForSlot[slot] != null) {
|
||||
final ICellInventory<?> ci = this.handlerForSlot[slot].getCellInv();
|
||||
|
||||
if (ci == null || ci.getItemStack() != super.getInvStack(slot)) {
|
||||
this.handlerForSlot[slot] = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,219 @@
|
||||
/*
|
||||
* 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.misc;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import alexiil.mc.lib.attributes.item.FixedItemInv;
|
||||
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.BlockPos;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import appeng.api.config.CopyMode;
|
||||
import appeng.api.config.Settings;
|
||||
import appeng.api.config.Upgrades;
|
||||
import appeng.api.implementations.IUpgradeableHost;
|
||||
import appeng.api.storage.cells.ICellWorkbenchItem;
|
||||
import appeng.api.util.IConfigManager;
|
||||
import appeng.tile.AEBaseBlockEntity;
|
||||
import appeng.tile.inventory.AppEngInternalAEInventory;
|
||||
import appeng.tile.inventory.AppEngInternalInventory;
|
||||
import appeng.util.ConfigManager;
|
||||
import appeng.util.IConfigManagerHost;
|
||||
import appeng.util.helpers.ItemHandlerUtil;
|
||||
import appeng.util.inv.IAEAppEngInventory;
|
||||
import appeng.util.inv.InvOperation;
|
||||
|
||||
public class CellWorkbenchBlockEntity extends AEBaseBlockEntity
|
||||
implements IUpgradeableHost, IAEAppEngInventory, IConfigManagerHost {
|
||||
|
||||
private final AppEngInternalInventory cell = new AppEngInternalInventory(this, 1);
|
||||
private final AppEngInternalAEInventory config = new AppEngInternalAEInventory(this, 63);
|
||||
private final ConfigManager manager = new ConfigManager(this);
|
||||
|
||||
private FixedItemInv cacheUpgrades = null;
|
||||
private FixedItemInv cacheConfig = null;
|
||||
private boolean locked = false;
|
||||
|
||||
public CellWorkbenchBlockEntity(BlockEntityType<?> tileEntityTypeIn) {
|
||||
super(tileEntityTypeIn);
|
||||
this.manager.registerSetting(Settings.COPY_MODE, CopyMode.CLEAR_ON_REMOVE);
|
||||
this.cell.setEnableClientEvents(true);
|
||||
}
|
||||
|
||||
public FixedItemInv getCellUpgradeInventory() {
|
||||
if (this.cacheUpgrades == null) {
|
||||
final ICellWorkbenchItem cell = this.getCell();
|
||||
if (cell == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final ItemStack is = this.cell.getInvStack(0);
|
||||
if (is.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final FixedItemInv inv = cell.getUpgradesInventory(is);
|
||||
if (inv == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return this.cacheUpgrades = inv;
|
||||
}
|
||||
return this.cacheUpgrades;
|
||||
}
|
||||
|
||||
public ICellWorkbenchItem getCell() {
|
||||
if (this.cell.getInvStack(0).isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (this.cell.getInvStack(0).getItem() instanceof ICellWorkbenchItem) {
|
||||
return ((ICellWorkbenchItem) this.cell.getInvStack(0).getItem());
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompoundTag toTag(final CompoundTag data) {
|
||||
super.toTag(data);
|
||||
this.cell.writeToNBT(data, "cell");
|
||||
this.config.writeToNBT(data, "config");
|
||||
this.manager.writeToNBT(data);
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromTag(BlockState state, final CompoundTag data) {
|
||||
super.fromTag(state, data);
|
||||
this.cell.readFromNBT(data, "cell");
|
||||
this.config.readFromNBT(data, "config");
|
||||
this.manager.readFromNBT(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FixedItemInv getInventoryByName(final String name) {
|
||||
if (name.equals("config")) {
|
||||
return this.config;
|
||||
}
|
||||
|
||||
if (name.equals("cell")) {
|
||||
return this.cell;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInstalledUpgrades(final Upgrades u) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChangeInventory(final FixedItemInv inv, final int slot, final InvOperation mc,
|
||||
final ItemStack removedStack, final ItemStack newStack) {
|
||||
if (inv == this.cell && !this.locked) {
|
||||
this.locked = true;
|
||||
|
||||
this.cacheUpgrades = null;
|
||||
this.cacheConfig = null;
|
||||
|
||||
final FixedItemInv configInventory = this.getCellConfigInventory();
|
||||
if (configInventory != null) {
|
||||
boolean cellHasConfig = false;
|
||||
for (int x = 0; x < configInventory.getSlotCount(); x++) {
|
||||
if (!configInventory.getInvStack(x).isEmpty()) {
|
||||
cellHasConfig = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (cellHasConfig) {
|
||||
for (int x = 0; x < this.config.getSlotCount(); x++) {
|
||||
this.config.forceSetInvStack(x, configInventory.getInvStack(x));
|
||||
}
|
||||
} else {
|
||||
ItemHandlerUtil.copy(this.config, configInventory, false);
|
||||
}
|
||||
} else if (this.manager.getSetting(Settings.COPY_MODE) == CopyMode.CLEAR_ON_REMOVE) {
|
||||
for (int x = 0; x < this.config.getSlotCount(); x++) {
|
||||
this.config.forceSetInvStack(x, ItemStack.EMPTY);
|
||||
}
|
||||
|
||||
this.saveChanges();
|
||||
}
|
||||
|
||||
this.locked = false;
|
||||
} else if (inv == this.config && !this.locked) {
|
||||
this.locked = true;
|
||||
final FixedItemInv c = this.getCellConfigInventory();
|
||||
if (c != null) {
|
||||
ItemHandlerUtil.copy(this.config, c, false);
|
||||
// copy items back. The ConfigInventory may changed the items on insert
|
||||
ItemHandlerUtil.copy(c, this.config, false);
|
||||
}
|
||||
this.locked = false;
|
||||
}
|
||||
}
|
||||
|
||||
private FixedItemInv getCellConfigInventory() {
|
||||
if (this.cacheConfig == null) {
|
||||
final ICellWorkbenchItem cell = this.getCell();
|
||||
if (cell == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final ItemStack is = this.cell.getInvStack(0);
|
||||
if (is.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final FixedItemInv inv = cell.getConfigInventory(is);
|
||||
if (inv == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
this.cacheConfig = inv;
|
||||
}
|
||||
return this.cacheConfig;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getDrops(final World w, final BlockPos pos, final List<ItemStack> drops) {
|
||||
super.getDrops(w, pos, drops);
|
||||
|
||||
if (this.cell.getInvStack(0) != null) {
|
||||
drops.add(this.cell.getInvStack(0));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public IConfigManager getConfigManager() {
|
||||
return this.manager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateSetting(final IConfigManager manager, final Settings settingName, final Enum<?> newValue) {
|
||||
// nothing here..
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,277 @@
|
||||
/*
|
||||
* 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.misc;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.EnumSet;
|
||||
import java.util.List;
|
||||
|
||||
import alexiil.mc.lib.attributes.Simulation;
|
||||
import net.minecraft.block.entity.BlockEntityType;
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.network.PacketByteBuf;
|
||||
import net.minecraft.util.math.Direction;
|
||||
import alexiil.mc.lib.attributes.item.FixedItemInv;
|
||||
|
||||
import appeng.api.AEApi;
|
||||
import appeng.api.config.Actionable;
|
||||
import appeng.api.config.PowerMultiplier;
|
||||
import appeng.api.config.PowerUnits;
|
||||
import appeng.api.definitions.IItemDefinition;
|
||||
import appeng.api.definitions.IMaterials;
|
||||
import appeng.api.implementations.items.IAEItemPowerStorage;
|
||||
import appeng.api.implementations.tiles.ICrankable;
|
||||
import appeng.api.networking.IGridNode;
|
||||
import appeng.api.networking.ticking.IGridTickable;
|
||||
import appeng.api.networking.ticking.TickRateModulation;
|
||||
import appeng.api.networking.ticking.TickingRequest;
|
||||
import appeng.api.storage.data.IAEItemStack;
|
||||
import appeng.api.util.AECableType;
|
||||
import appeng.api.util.AEPartLocation;
|
||||
import appeng.api.util.DimensionalCoord;
|
||||
import appeng.core.settings.TickRates;
|
||||
import appeng.me.GridAccessException;
|
||||
import appeng.tile.grid.AENetworkPowerBlockEntity;
|
||||
import appeng.tile.inventory.AppEngInternalInventory;
|
||||
import appeng.util.Platform;
|
||||
import appeng.util.inv.InvOperation;
|
||||
import appeng.util.inv.filter.IAEItemFilter;
|
||||
import appeng.util.item.AEItemStack;
|
||||
|
||||
public class ChargerBlockEntity extends AENetworkPowerBlockEntity implements ICrankable, IGridTickable {
|
||||
private static final int POWER_MAXIMUM_AMOUNT = 1600;
|
||||
private static final int POWER_THRESHOLD = POWER_MAXIMUM_AMOUNT - 1;
|
||||
private static final int POWER_PER_CRANK_TURN = 160;
|
||||
|
||||
private final AppEngInternalInventory inv = new AppEngInternalInventory(this, 1, 1, new ChargerInvFilter());
|
||||
|
||||
public ChargerBlockEntity(BlockEntityType<?> tileEntityTypeIn) {
|
||||
super(tileEntityTypeIn);
|
||||
this.getProxy().setValidSides(EnumSet.noneOf(Direction.class));
|
||||
this.getProxy().setFlags();
|
||||
this.setInternalMaxPower(POWER_MAXIMUM_AMOUNT);
|
||||
this.getProxy().setIdlePowerUsage(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AECableType getCableConnectionType(final AEPartLocation dir) {
|
||||
return AECableType.COVERED;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean readFromStream(final PacketByteBuf data) throws IOException {
|
||||
final boolean c = super.readFromStream(data);
|
||||
try {
|
||||
final IAEItemStack item = AEItemStack.fromPacket(data);
|
||||
final ItemStack is = item.createItemStack();
|
||||
this.inv.setInvStack(0, is, Simulation.ACTION);
|
||||
} catch (final Throwable t) {
|
||||
this.inv.setInvStack(0, ItemStack.EMPTY, Simulation.ACTION);
|
||||
}
|
||||
return c; // TESR doesn't need updates!
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void writeToStream(final PacketByteBuf data) throws IOException {
|
||||
super.writeToStream(data);
|
||||
final AEItemStack is = AEItemStack.fromItemStack(this.inv.getInvStack(0));
|
||||
if (is != null) {
|
||||
is.writeToPacket(data);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setOrientation(final Direction inForward, final Direction inUp) {
|
||||
super.setOrientation(inForward, inUp);
|
||||
this.getProxy().setValidSides(EnumSet.of(this.getUp(), this.getUp().getOpposite()));
|
||||
this.setPowerSides(EnumSet.of(this.getUp(), this.getUp().getOpposite()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canTurn() {
|
||||
return this.getInternalCurrentPower() < this.getInternalMaxPower();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void applyTurn() {
|
||||
this.injectExternalPower(PowerUnits.AE, POWER_PER_CRANK_TURN, Actionable.MODULATE);
|
||||
|
||||
final ItemStack myItem = this.inv.getInvStack(0);
|
||||
if (this.getInternalCurrentPower() > POWER_THRESHOLD) {
|
||||
final IMaterials materials = AEApi.instance().definitions().materials();
|
||||
|
||||
if (materials.certusQuartzCrystal().isSameAs(myItem)) {
|
||||
this.extractAEPower(this.getInternalMaxPower(), Actionable.MODULATE, PowerMultiplier.CONFIG);
|
||||
|
||||
materials.certusQuartzCrystalCharged().maybeStack(myItem.getCount())
|
||||
.ifPresent(charged -> this.inv.setInvStack(0, charged, Simulation.ACTION));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canCrankAttach(final Direction directionToCrank) {
|
||||
return this.getUp() == directionToCrank || this.getUp().getOpposite() == directionToCrank;
|
||||
}
|
||||
|
||||
@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) {
|
||||
try {
|
||||
this.getProxy().getTick().wakeDevice(this.getProxy().getNode());
|
||||
} catch (final GridAccessException e) {
|
||||
// :P
|
||||
}
|
||||
|
||||
this.markForUpdate();
|
||||
}
|
||||
|
||||
public void activate(final PlayerEntity player) {
|
||||
if (!Platform.hasPermissions(new DimensionalCoord(this), player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
final ItemStack myItem = this.inv.getInvStack(0);
|
||||
if (myItem.isEmpty()) {
|
||||
ItemStack held = player.inventory.getMainHandStack();
|
||||
|
||||
if (AEApi.instance().definitions().materials().certusQuartzCrystal().isSameAs(held)
|
||||
|| Platform.isChargeable(held)) {
|
||||
held = player.inventory.removeStack(player.inventory.selectedSlot, 1);
|
||||
this.inv.setInvStack(0, held, Simulation.ACTION);
|
||||
}
|
||||
} else {
|
||||
final List<ItemStack> drops = new ArrayList<>();
|
||||
drops.add(myItem);
|
||||
this.inv.setInvStack(0, ItemStack.EMPTY, Simulation.ACTION);
|
||||
Platform.spawnDrops(this.world, this.pos.offset(this.getForward()), drops);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public TickingRequest getTickingRequest(IGridNode node) {
|
||||
return new TickingRequest(TickRates.Charger.getMin(), TickRates.Charger.getMin(), false, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TickRateModulation tickingRequest(IGridNode node, int TicksSinceLastCall) {
|
||||
return this.doWork() ? TickRateModulation.FASTER : TickRateModulation.SLEEP;
|
||||
}
|
||||
|
||||
private boolean doWork() {
|
||||
final ItemStack myItem = this.inv.getInvStack(0);
|
||||
boolean changed = false;
|
||||
|
||||
if (!myItem.isEmpty()) {
|
||||
final IMaterials materials = AEApi.instance().definitions().materials();
|
||||
|
||||
if (Platform.isChargeable(myItem)) {
|
||||
final IAEItemPowerStorage ps = (IAEItemPowerStorage) myItem.getItem();
|
||||
|
||||
if (ps.getAEMaxPower(myItem) > ps.getAECurrentPower(myItem)) {
|
||||
final double chargeRate = AEApi.instance().registries().charger().getChargeRate(myItem.getItem());
|
||||
|
||||
double extractedAmount = this.extractAEPower(chargeRate, Actionable.MODULATE,
|
||||
PowerMultiplier.CONFIG);
|
||||
|
||||
final double missingChargeRate = chargeRate - extractedAmount;
|
||||
final double missingAEPower = ps.getAEMaxPower(myItem) - ps.getAECurrentPower(myItem);
|
||||
final double toExtract = Math.min(missingChargeRate, missingAEPower);
|
||||
|
||||
try {
|
||||
extractedAmount += this.getProxy().getEnergy().extractAEPower(toExtract, Actionable.MODULATE,
|
||||
PowerMultiplier.ONE);
|
||||
} catch (GridAccessException e1) {
|
||||
// Ignore.
|
||||
}
|
||||
|
||||
if (extractedAmount > 0) {
|
||||
final double adjustment = ps.injectAEPower(myItem, extractedAmount, Actionable.MODULATE);
|
||||
|
||||
this.setInternalCurrentPower(this.getInternalCurrentPower() + adjustment);
|
||||
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
} else if (this.getInternalCurrentPower() > POWER_THRESHOLD
|
||||
&& materials.certusQuartzCrystal().isSameAs(myItem)) {
|
||||
if (Platform.getRandomFloat() > 0.8f) // simulate wait
|
||||
{
|
||||
this.extractAEPower(this.getInternalMaxPower(), Actionable.MODULATE, PowerMultiplier.CONFIG);
|
||||
|
||||
materials.certusQuartzCrystalCharged().maybeStack(myItem.getCount())
|
||||
.ifPresent(charged -> this.inv.setInvStack(0, charged, Simulation.ACTION));
|
||||
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// charge from the network!
|
||||
if (this.getInternalCurrentPower() < POWER_THRESHOLD) {
|
||||
try {
|
||||
final double toExtract = Math.min(800.0, this.getInternalMaxPower() - this.getInternalCurrentPower());
|
||||
final double extracted = this.getProxy().getEnergy().extractAEPower(toExtract, Actionable.MODULATE,
|
||||
PowerMultiplier.ONE);
|
||||
|
||||
this.injectExternalPower(PowerUnits.AE, extracted, Actionable.MODULATE);
|
||||
} catch (final GridAccessException e) {
|
||||
// continue!
|
||||
}
|
||||
|
||||
changed = true;
|
||||
}
|
||||
|
||||
if (changed) {
|
||||
this.markForUpdate();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private class ChargerInvFilter implements IAEItemFilter {
|
||||
@Override
|
||||
public boolean allowInsert(FixedItemInv inv, final int i, final ItemStack itemstack) {
|
||||
final IItemDefinition cert = AEApi.instance().definitions().materials().certusQuartzCrystal();
|
||||
|
||||
return Platform.isChargeable(itemstack) || cert.isSameAs(itemstack);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean allowExtract(FixedItemInv inv, final int slotIndex, int amount) {
|
||||
ItemStack extractedItem = inv.getInvStack(slotIndex);
|
||||
|
||||
if (Platform.isChargeable(extractedItem)) {
|
||||
final IAEItemPowerStorage ips = (IAEItemPowerStorage) extractedItem.getItem();
|
||||
if (ips.getAECurrentPower(extractedItem) >= ips.getAEMaxPower(extractedItem)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return AEApi.instance().definitions().materials().certusQuartzCrystalCharged().isSameAs(extractedItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,270 @@
|
||||
/*
|
||||
* 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.misc;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import alexiil.mc.lib.attributes.AttributeList;
|
||||
import alexiil.mc.lib.attributes.Simulation;
|
||||
import alexiil.mc.lib.attributes.fluid.FluidInsertable;
|
||||
import alexiil.mc.lib.attributes.fluid.FluidVolumeUtil;
|
||||
import alexiil.mc.lib.attributes.fluid.amount.FluidAmount;
|
||||
import alexiil.mc.lib.attributes.fluid.volume.FluidVolume;
|
||||
import alexiil.mc.lib.attributes.item.FixedItemInv;
|
||||
import alexiil.mc.lib.attributes.item.ItemInsertable;
|
||||
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.BlockPos;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import appeng.api.AEApi;
|
||||
import appeng.api.config.CondenserOutput;
|
||||
import appeng.api.config.Settings;
|
||||
import appeng.api.definitions.IMaterials;
|
||||
import appeng.api.implementations.items.IStorageComponent;
|
||||
import appeng.api.networking.security.IActionSource;
|
||||
import appeng.api.storage.IMEMonitor;
|
||||
import appeng.api.storage.IStorageChannel;
|
||||
import appeng.api.storage.IStorageMonitorable;
|
||||
import appeng.api.storage.IStorageMonitorableAccessor;
|
||||
import appeng.api.storage.channels.IFluidStorageChannel;
|
||||
import appeng.api.storage.channels.IItemStorageChannel;
|
||||
import appeng.api.storage.data.IAEFluidStack;
|
||||
import appeng.api.storage.data.IAEStack;
|
||||
import appeng.api.util.IConfigManager;
|
||||
import appeng.api.util.IConfigurableObject;
|
||||
import appeng.tile.AEBaseInvBlockEntity;
|
||||
import appeng.tile.inventory.AppEngInternalInventory;
|
||||
import appeng.util.ConfigManager;
|
||||
import appeng.util.IConfigManagerHost;
|
||||
import appeng.util.inv.InvOperation;
|
||||
import appeng.util.inv.WrapperChainedItemHandler;
|
||||
|
||||
public class CondenserBlockEntity extends AEBaseInvBlockEntity implements IConfigManagerHost, IConfigurableObject {
|
||||
|
||||
public static final int BYTE_MULTIPLIER = 8;
|
||||
|
||||
private final ConfigManager cm = new ConfigManager(this);
|
||||
|
||||
private final AppEngInternalInventory outputSlot = new AppEngInternalInventory(this, 1);
|
||||
private final AppEngInternalInventory storageSlot = new AppEngInternalInventory(this, 1);
|
||||
private final ItemInsertable externalItemInput = new CondenseItemHandler();
|
||||
private final FluidInsertable externalFluidInput = new FluidHandler();
|
||||
private final MEHandler meHandler = new MEHandler();
|
||||
|
||||
private final FixedItemInv combinedInv = new WrapperChainedItemHandler(this.outputSlot,
|
||||
this.storageSlot);
|
||||
|
||||
private double storedPower = 0;
|
||||
|
||||
public CondenserBlockEntity(BlockEntityType<?> tileEntityTypeIn) {
|
||||
super(tileEntityTypeIn);
|
||||
this.cm.registerSetting(Settings.CONDENSER_OUTPUT, CondenserOutput.TRASH);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompoundTag toTag(final CompoundTag data) {
|
||||
super.toTag(data);
|
||||
this.cm.writeToNBT(data);
|
||||
data.putDouble("storedPower", this.getStoredPower());
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromTag(BlockState state, final CompoundTag data) {
|
||||
super.fromTag(state, data);
|
||||
this.cm.readFromNBT(data);
|
||||
this.setStoredPower(data.getDouble("storedPower"));
|
||||
}
|
||||
|
||||
public double getStorage() {
|
||||
final ItemStack is = this.storageSlot.getInvStack(0);
|
||||
if (!is.isEmpty()) {
|
||||
if (is.getItem() instanceof IStorageComponent) {
|
||||
final IStorageComponent sc = (IStorageComponent) is.getItem();
|
||||
if (sc.isStorageComponent(is)) {
|
||||
return sc.getBytes(is) * BYTE_MULTIPLIER;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public void addPower(final double rawPower) {
|
||||
this.setStoredPower(this.getStoredPower() + rawPower);
|
||||
this.setStoredPower(Math.max(0.0, Math.min(this.getStorage(), this.getStoredPower())));
|
||||
|
||||
final double requiredPower = this.getRequiredPower();
|
||||
final ItemStack output = this.getOutput();
|
||||
while (requiredPower <= this.getStoredPower() && !output.isEmpty() && requiredPower > 0) {
|
||||
if (this.canAddOutput(output)) {
|
||||
this.setStoredPower(this.getStoredPower() - requiredPower);
|
||||
this.addOutput(output);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean canAddOutput(final ItemStack output) {
|
||||
return this.outputSlot.setInvStack(0, output, Simulation.SIMULATE);
|
||||
}
|
||||
|
||||
/**
|
||||
* make sure you validate with canAddOutput prior to this.
|
||||
*
|
||||
* @param output to be added output
|
||||
*/
|
||||
private void addOutput(final ItemStack output) {
|
||||
this.outputSlot.setInvStack(0, output, Simulation.ACTION);
|
||||
}
|
||||
|
||||
FixedItemInv getOutputSlot() {
|
||||
return this.outputSlot;
|
||||
}
|
||||
|
||||
private ItemStack getOutput() {
|
||||
final IMaterials materials = AEApi.instance().definitions().materials();
|
||||
|
||||
switch ((CondenserOutput) this.cm.getSetting(Settings.CONDENSER_OUTPUT)) {
|
||||
case MATTER_BALLS:
|
||||
return materials.matterBall().maybeStack(1).orElse(ItemStack.EMPTY);
|
||||
|
||||
case SINGULARITY:
|
||||
return materials.singularity().maybeStack(1).orElse(ItemStack.EMPTY);
|
||||
|
||||
case TRASH:
|
||||
default:
|
||||
return ItemStack.EMPTY;
|
||||
}
|
||||
}
|
||||
|
||||
public double getRequiredPower() {
|
||||
return ((CondenserOutput) this.cm.getSetting(Settings.CONDENSER_OUTPUT)).requiredPower;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FixedItemInv getInternalInventory() {
|
||||
return this.combinedInv;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChangeInventory(final FixedItemInv inv, final int slot, final InvOperation mc,
|
||||
final ItemStack removed, final ItemStack added) {
|
||||
if (inv == this.outputSlot) {
|
||||
this.meHandler.outputChanged(added, removed);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateSetting(final IConfigManager manager, final Settings settingName, final Enum<?> newValue) {
|
||||
this.addPower(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IConfigManager getConfigManager() {
|
||||
return this.cm;
|
||||
}
|
||||
|
||||
public double getStoredPower() {
|
||||
return this.storedPower;
|
||||
}
|
||||
|
||||
private void setStoredPower(final double storedPower) {
|
||||
this.storedPower = storedPower;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addAllAttributes(World world, BlockPos pos, BlockState state, AttributeList<?> to) {
|
||||
super.addAllAttributes(world, pos, state, to);
|
||||
|
||||
to.offer(externalItemInput);
|
||||
to.offer(externalFluidInput);
|
||||
to.offer(outputSlot.getPureExtractable());
|
||||
to.offer(meHandler);
|
||||
}
|
||||
|
||||
private class CondenseItemHandler implements ItemInsertable {
|
||||
|
||||
@Override
|
||||
public ItemStack attemptInsertion(ItemStack stack, Simulation simulation) {
|
||||
if (simulation == Simulation.ACTION && !stack.isEmpty()) {
|
||||
CondenserBlockEntity.this.addPower(stack.getCount());
|
||||
}
|
||||
return ItemStack.EMPTY;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* A fluid handler that exposes a 1 bucket tank that can only be filled, and -
|
||||
* when filled - will add power to this condenser.
|
||||
*/
|
||||
private class FluidHandler implements FluidInsertable {
|
||||
|
||||
@Override
|
||||
public FluidVolume attemptInsertion(FluidVolume fluid, Simulation simulation) {
|
||||
if (simulation == Simulation.ACTION) {
|
||||
final IStorageChannel<IAEFluidStack> chan = AEApi.instance().storage()
|
||||
.getStorageChannel(IFluidStorageChannel.class);
|
||||
CondenserBlockEntity.this
|
||||
.addPower((fluid.isEmpty() ? 0.0 : fluid.getAmount_F().asInexactDouble() * 1000.0) / chan.transferFactor());
|
||||
}
|
||||
|
||||
return FluidVolumeUtil.EMPTY;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public FluidAmount getMinimumAcceptedAmount() {
|
||||
return FluidAmount.of(1, 1000); // 1 millibucket
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This is used to expose a fake ME subnetwork that is only composed of this
|
||||
* condenser tile. The purpose of this is to enable the condenser to override
|
||||
* the {@link appeng.api.storage.IMEInventoryHandler#validForPass(int)} method
|
||||
* to make sure a condenser is only ever used if an item can't go anywhere else.
|
||||
*/
|
||||
private class MEHandler implements IStorageMonitorableAccessor, IStorageMonitorable {
|
||||
private final CondenserItemInventory itemInventory = new CondenserItemInventory(CondenserBlockEntity.this);
|
||||
|
||||
void outputChanged(ItemStack added, ItemStack removed) {
|
||||
this.itemInventory.updateOutput(added, removed);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public IStorageMonitorable getInventory(IActionSource src) {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends IAEStack<T>> IMEMonitor<T> getInventory(IStorageChannel<T> channel) {
|
||||
if (channel == AEApi.instance().storage().getStorageChannel(IItemStorageChannel.class)) {
|
||||
return (IMEMonitor<T>) this.itemInventory;
|
||||
} else {
|
||||
return new CondenserVoidInventory<>(CondenserBlockEntity.this, channel);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
/*
|
||||
* 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.misc;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import alexiil.mc.lib.attributes.Simulation;
|
||||
import alexiil.mc.lib.attributes.item.filter.ConstantItemFilter;
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
import appeng.api.AEApi;
|
||||
import appeng.api.config.AccessRestriction;
|
||||
import appeng.api.config.Actionable;
|
||||
import appeng.api.networking.security.IActionSource;
|
||||
import appeng.api.networking.ticking.TickRateModulation;
|
||||
import appeng.api.storage.IMEMonitor;
|
||||
import appeng.api.storage.IMEMonitorHandlerReceiver;
|
||||
import appeng.api.storage.IStorageChannel;
|
||||
import appeng.api.storage.channels.IItemStorageChannel;
|
||||
import appeng.api.storage.data.IAEItemStack;
|
||||
import appeng.api.storage.data.IItemList;
|
||||
import appeng.me.helpers.BaseActionSource;
|
||||
import appeng.me.storage.ITickingMonitor;
|
||||
import appeng.util.item.AEItemStack;
|
||||
import appeng.util.item.ItemList;
|
||||
|
||||
class CondenserItemInventory implements IMEMonitor<IAEItemStack>, ITickingMonitor {
|
||||
private final HashMap<IMEMonitorHandlerReceiver<IAEItemStack>, Object> listeners = new HashMap<>();
|
||||
private final CondenserBlockEntity target;
|
||||
private boolean hasChanged = true;
|
||||
private final ItemList cachedList = new ItemList();
|
||||
private IActionSource actionSource = new BaseActionSource();
|
||||
private ItemList changeSet = new ItemList();
|
||||
|
||||
CondenserItemInventory(final CondenserBlockEntity te) {
|
||||
this.target = te;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IAEItemStack injectItems(final IAEItemStack input, final Actionable mode, final IActionSource src) {
|
||||
if (mode == Actionable.MODULATE && input != null) {
|
||||
this.target.addPower(input.getStackSize());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IAEItemStack extractItems(final IAEItemStack request, final Actionable mode, final IActionSource src) {
|
||||
AEItemStack ret = null;
|
||||
ItemStack slotItem = this.target.getOutputSlot().getInvStack(0);
|
||||
if (!slotItem.isEmpty() && request.isSameType(slotItem)) {
|
||||
int count = (int) Math.min(request.getStackSize(), Integer.MAX_VALUE);
|
||||
ret = AEItemStack
|
||||
.fromItemStack(this.target.getOutputSlot().getSlot(0).attemptExtraction(ConstantItemFilter.ANYTHING, count, mode == Actionable.SIMULATE ? Simulation.SIMULATE : Simulation.ACTION));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IItemList<IAEItemStack> getAvailableItems(final IItemList<IAEItemStack> out) {
|
||||
if (!this.target.getOutputSlot().getInvStack(0).isEmpty()) {
|
||||
out.add(AEItemStack.fromItemStack(this.target.getOutputSlot().getInvStack(0)));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IItemList<IAEItemStack> getStorageList() {
|
||||
if (this.hasChanged) {
|
||||
this.hasChanged = false;
|
||||
this.cachedList.resetStatus();
|
||||
return this.getAvailableItems(this.cachedList);
|
||||
}
|
||||
return this.cachedList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IStorageChannel<IAEItemStack> getChannel() {
|
||||
return AEApi.instance().storage().getStorageChannel(IItemStorageChannel.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AccessRestriction getAccess() {
|
||||
return AccessRestriction.READ_WRITE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPrioritized(final IAEItemStack input) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canAccept(final IAEItemStack input) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPriority() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSlot() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean validForPass(final int i) {
|
||||
return i == 2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addListener(final IMEMonitorHandlerReceiver<IAEItemStack> l, final Object verificationToken) {
|
||||
this.listeners.put(l, verificationToken);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeListener(final IMEMonitorHandlerReceiver<IAEItemStack> l) {
|
||||
this.listeners.remove(l);
|
||||
}
|
||||
|
||||
public void updateOutput(ItemStack added, ItemStack removed) {
|
||||
this.hasChanged = true;
|
||||
if (!added.isEmpty()) {
|
||||
this.changeSet.add(AEItemStack.fromItemStack(added));
|
||||
}
|
||||
if (!removed.isEmpty()) {
|
||||
this.changeSet.add(AEItemStack.fromItemStack(removed).setStackSize(-removed.getCount()));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public TickRateModulation onTick() {
|
||||
final ItemList currentChanges = this.changeSet;
|
||||
|
||||
if (currentChanges.isEmpty()) {
|
||||
return TickRateModulation.IDLE;
|
||||
}
|
||||
|
||||
this.changeSet = new ItemList();
|
||||
final Iterator<Entry<IMEMonitorHandlerReceiver<IAEItemStack>, Object>> i = this.listeners.entrySet().iterator();
|
||||
while (i.hasNext()) {
|
||||
final Entry<IMEMonitorHandlerReceiver<IAEItemStack>, Object> l = i.next();
|
||||
final IMEMonitorHandlerReceiver<IAEItemStack> key = l.getKey();
|
||||
if (key.isValid(l.getValue())) {
|
||||
key.postChange(this, currentChanges, this.actionSource);
|
||||
} else {
|
||||
i.remove();
|
||||
}
|
||||
}
|
||||
|
||||
return TickRateModulation.URGENT;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setActionSource(IActionSource actionSource) {
|
||||
this.actionSource = actionSource;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
* 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.misc;
|
||||
|
||||
import appeng.api.config.AccessRestriction;
|
||||
import appeng.api.config.Actionable;
|
||||
import appeng.api.networking.security.IActionSource;
|
||||
import appeng.api.storage.IMEMonitor;
|
||||
import appeng.api.storage.IMEMonitorHandlerReceiver;
|
||||
import appeng.api.storage.IStorageChannel;
|
||||
import appeng.api.storage.data.IAEStack;
|
||||
import appeng.api.storage.data.IItemList;
|
||||
|
||||
class CondenserVoidInventory<T extends IAEStack<T>> implements IMEMonitor<T> {
|
||||
|
||||
private final CondenserBlockEntity target;
|
||||
private final IStorageChannel<T> channel;
|
||||
|
||||
CondenserVoidInventory(final CondenserBlockEntity te, final IStorageChannel<T> channel) {
|
||||
this.target = te;
|
||||
this.channel = channel;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T injectItems(final T input, final Actionable mode, final IActionSource src) {
|
||||
if (mode == Actionable.SIMULATE) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (input != null) {
|
||||
this.target.addPower(input.getStackSize() / (double) this.channel.transferFactor());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T extractItems(final T request, final Actionable mode, final IActionSource src) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IItemList<T> getAvailableItems(final IItemList<T> out) {
|
||||
return out;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IItemList<T> getStorageList() {
|
||||
return this.channel.createList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public IStorageChannel<T> getChannel() {
|
||||
return this.channel;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AccessRestriction getAccess() {
|
||||
return AccessRestriction.WRITE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPrioritized(final T input) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canAccept(final T input) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPriority() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSlot() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean validForPass(final int i) {
|
||||
return i == 2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addListener(IMEMonitorHandlerReceiver<T> l, Object verificationToken) {
|
||||
// Not implemented since the Condenser automatically voids everything, and there
|
||||
// are no updates
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeListener(IMEMonitorHandlerReceiver<T> l) {
|
||||
// Not implemented since we don't remember registered listeners anyway
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,443 @@
|
||||
/*
|
||||
* 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.misc;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.EnumSet;
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import alexiil.mc.lib.attributes.Simulation;
|
||||
import alexiil.mc.lib.attributes.item.FixedItemInv;
|
||||
import net.minecraft.block.BlockState;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.network.PacketByteBuf;
|
||||
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.PowerMultiplier;
|
||||
import appeng.api.config.Settings;
|
||||
import appeng.api.config.Upgrades;
|
||||
import appeng.api.definitions.ITileDefinition;
|
||||
import appeng.api.features.InscriberProcessType;
|
||||
import appeng.api.implementations.IUpgradeableHost;
|
||||
import appeng.api.networking.IGridNode;
|
||||
import appeng.api.networking.energy.IEnergyGrid;
|
||||
import appeng.api.networking.energy.IEnergySource;
|
||||
import appeng.api.networking.ticking.IGridTickable;
|
||||
import appeng.api.networking.ticking.TickRateModulation;
|
||||
import appeng.api.networking.ticking.TickingRequest;
|
||||
import appeng.api.util.AECableType;
|
||||
import appeng.api.util.AEPartLocation;
|
||||
import appeng.api.util.IConfigManager;
|
||||
import appeng.core.settings.TickRates;
|
||||
import appeng.me.GridAccessException;
|
||||
import appeng.parts.automation.DefinitionUpgradeInventory;
|
||||
import appeng.parts.automation.UpgradeInventory;
|
||||
import appeng.recipes.handlers.InscriberRecipe;
|
||||
import appeng.tile.grid.AENetworkPowerBlockEntity;
|
||||
import appeng.tile.inventory.AppEngInternalInventory;
|
||||
import appeng.util.ConfigManager;
|
||||
import appeng.util.IConfigManagerHost;
|
||||
import appeng.util.inv.InvOperation;
|
||||
import appeng.util.inv.WrapperChainedItemHandler;
|
||||
import appeng.util.inv.WrapperFilteredItemHandler;
|
||||
import appeng.util.inv.filter.IAEItemFilter;
|
||||
import appeng.util.item.AEItemStack;
|
||||
|
||||
/**
|
||||
* @author AlgorithmX2
|
||||
* @author thatsIch
|
||||
* @version rv2
|
||||
* @since rv0
|
||||
*/
|
||||
public class InscriberBlockEntity extends AENetworkPowerBlockEntity
|
||||
implements IGridTickable, IUpgradeableHost, IConfigManagerHost {
|
||||
private final int maxProcessingTime = 100;
|
||||
|
||||
private final IConfigManager settings;
|
||||
private final UpgradeInventory upgrades;
|
||||
private int processingTime = 0;
|
||||
// cycles from 0 - 16, at 8 it preforms the action, at 16 it re-enables the
|
||||
// normal routine.
|
||||
private boolean smash;
|
||||
private int finalStep;
|
||||
private long clientStart;
|
||||
private final AppEngInternalInventory topItemHandler = new AppEngInternalInventory(this, 1, 1);
|
||||
private final AppEngInternalInventory bottomItemHandler = new AppEngInternalInventory(this, 1, 1);
|
||||
private final AppEngInternalInventory sideItemHandler = new AppEngInternalInventory(this, 2, 1);
|
||||
|
||||
private final FixedItemInv topItemHandlerExtern;
|
||||
private final FixedItemInv bottomItemHandlerExtern;
|
||||
private final FixedItemInv sideItemHandlerExtern;
|
||||
|
||||
private InscriberRecipe cachedTask = null;
|
||||
|
||||
private final FixedItemInv inv = new WrapperChainedItemHandler(this.topItemHandler,
|
||||
this.bottomItemHandler, this.sideItemHandler);
|
||||
|
||||
public InscriberBlockEntity(BlockEntityType<?> tileEntityTypeIn) {
|
||||
super(tileEntityTypeIn);
|
||||
|
||||
this.getProxy().setValidSides(EnumSet.noneOf(Direction.class));
|
||||
this.setInternalMaxPower(1600);
|
||||
this.getProxy().setIdlePowerUsage(0);
|
||||
this.settings = new ConfigManager(this);
|
||||
|
||||
final ITileDefinition inscriberDefinition = AEApi.instance().definitions().blocks().inscriber();
|
||||
this.upgrades = new DefinitionUpgradeInventory(inscriberDefinition, this, this.getUpgradeSlots());
|
||||
|
||||
this.sideItemHandler.setMaxStackSize(1, 64);
|
||||
|
||||
final IAEItemFilter filter = new ItemHandlerFilter();
|
||||
this.topItemHandlerExtern = new WrapperFilteredItemHandler(this.topItemHandler, filter);
|
||||
this.bottomItemHandlerExtern = new WrapperFilteredItemHandler(this.bottomItemHandler, filter);
|
||||
this.sideItemHandlerExtern = new WrapperFilteredItemHandler(this.sideItemHandler, filter);
|
||||
}
|
||||
|
||||
private int getUpgradeSlots() {
|
||||
return 3;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AECableType getCableConnectionType(final AEPartLocation dir) {
|
||||
return AECableType.COVERED;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompoundTag toTag(final CompoundTag data) {
|
||||
super.toTag(data);
|
||||
this.upgrades.writeToNBT(data, "upgrades");
|
||||
this.settings.writeToNBT(data);
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromTag(BlockState state, final CompoundTag data) {
|
||||
super.fromTag(state, data);
|
||||
this.upgrades.readFromNBT(data, "upgrades");
|
||||
this.settings.readFromNBT(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean readFromStream(final PacketByteBuf data) throws IOException {
|
||||
final boolean c = super.readFromStream(data);
|
||||
final int slot = data.readByte();
|
||||
|
||||
final boolean oldSmash = this.isSmash();
|
||||
final boolean newSmash = (slot & 64) == 64;
|
||||
|
||||
if (oldSmash != newSmash && newSmash) {
|
||||
this.setSmash(true);
|
||||
this.setClientStart(System.currentTimeMillis());
|
||||
}
|
||||
|
||||
for (int num = 0; num < this.inv.getSlotCount(); num++) {
|
||||
if ((slot & (1 << num)) > 0) {
|
||||
this.inv.forceSetInvStack(num, AEItemStack.fromPacket(data).createItemStack());
|
||||
} else {
|
||||
this.inv.forceSetInvStack(num, ItemStack.EMPTY);
|
||||
}
|
||||
}
|
||||
this.cachedTask = null;
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void writeToStream(final PacketByteBuf data) throws IOException {
|
||||
super.writeToStream(data);
|
||||
int slot = this.isSmash() ? 64 : 0;
|
||||
|
||||
for (int num = 0; num < this.inv.getSlotCount(); num++) {
|
||||
if (!this.inv.getInvStack(num).isEmpty()) {
|
||||
slot |= (1 << num);
|
||||
}
|
||||
}
|
||||
|
||||
data.writeByte(slot);
|
||||
for (int num = 0; num < this.inv.getSlotCount(); num++) {
|
||||
if ((slot & (1 << num)) > 0) {
|
||||
final AEItemStack st = AEItemStack.fromItemStack(this.inv.getInvStack(num));
|
||||
st.writeToPacket(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setOrientation(final Direction inForward, final Direction inUp) {
|
||||
super.setOrientation(inForward, inUp);
|
||||
this.getProxy().setValidSides(EnumSet.complementOf(EnumSet.of(this.getForward())));
|
||||
this.setPowerSides(EnumSet.complementOf(EnumSet.of(this.getForward())));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getDrops(final World w, final BlockPos pos, final List<ItemStack> drops) {
|
||||
super.getDrops(w, pos, drops);
|
||||
|
||||
for (int h = 0; h < this.upgrades.getSlotCount(); h++) {
|
||||
final ItemStack is = this.upgrades.getInvStack(h);
|
||||
if (!is.isEmpty()) {
|
||||
drops.add(is);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@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) {
|
||||
try {
|
||||
if (slot == 0) {
|
||||
this.setProcessingTime(0);
|
||||
}
|
||||
|
||||
if (!this.isSmash()) {
|
||||
this.markForUpdate();
|
||||
}
|
||||
|
||||
this.cachedTask = null;
|
||||
this.getProxy().getTick().wakeDevice(this.getProxy().getNode());
|
||||
} catch (final GridAccessException e) {
|
||||
// :P
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// @Override
|
||||
@Override
|
||||
public TickingRequest getTickingRequest(final IGridNode node) {
|
||||
return new TickingRequest(TickRates.Inscriber.getMin(), TickRates.Inscriber.getMax(), !this.hasWork(), false);
|
||||
}
|
||||
|
||||
private boolean hasWork() {
|
||||
if (this.getTask() != null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
this.setProcessingTime(0);
|
||||
return this.isSmash();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public InscriberRecipe getTask() {
|
||||
if (this.cachedTask == null && world != null) {
|
||||
ItemStack input = this.sideItemHandler.getInvStack(0);
|
||||
ItemStack plateA = this.topItemHandler.getInvStack(0);
|
||||
ItemStack plateB = this.bottomItemHandler.getInvStack(0);
|
||||
if (input.isEmpty()) {
|
||||
return null; // No input to handle
|
||||
}
|
||||
|
||||
// If the player somehow managed to insert more than one item, we bail here
|
||||
if (input.getCount() > 1 || plateA.getCount() > 1 || plateB.getCount() > 1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
this.cachedTask = InscriberRecipes.findRecipe(world, input, plateA, plateB, true);
|
||||
}
|
||||
return this.cachedTask;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TickRateModulation tickingRequest(final IGridNode node, final int ticksSinceLastCall) {
|
||||
if (this.isSmash()) {
|
||||
this.finalStep++;
|
||||
if (this.finalStep == 8) {
|
||||
final InscriberRecipe out = this.getTask();
|
||||
if (out != null) {
|
||||
final ItemStack outputCopy = out.getOutput().copy();
|
||||
|
||||
if (this.sideItemHandler.setInvStack(1, outputCopy, Simulation.ACTION)) {
|
||||
this.setProcessingTime(0);
|
||||
if (out.getProcessType() == InscriberProcessType.PRESS) {
|
||||
this.topItemHandler.setInvStack(0, ItemStack.EMPTY, Simulation.ACTION);
|
||||
this.bottomItemHandler.setInvStack(0, ItemStack.EMPTY, Simulation.ACTION);
|
||||
}
|
||||
this.sideItemHandler.setInvStack(0, ItemStack.EMPTY, Simulation.ACTION);
|
||||
}
|
||||
}
|
||||
this.saveChanges();
|
||||
} else if (this.finalStep == 16) {
|
||||
this.finalStep = 0;
|
||||
this.setSmash(false);
|
||||
this.markForUpdate();
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
final IEnergyGrid eg = this.getProxy().getEnergy();
|
||||
IEnergySource src = this;
|
||||
|
||||
// Base 1, increase by 1 for each card
|
||||
final int speedFactor = 1 + this.upgrades.getInstalledUpgrades(Upgrades.SPEED);
|
||||
final int powerConsumption = 10 * speedFactor;
|
||||
final double powerThreshold = powerConsumption - 0.01;
|
||||
double powerReq = this.extractAEPower(powerConsumption, Actionable.SIMULATE, PowerMultiplier.CONFIG);
|
||||
|
||||
if (powerReq <= powerThreshold) {
|
||||
src = eg;
|
||||
powerReq = eg.extractAEPower(powerConsumption, Actionable.SIMULATE, PowerMultiplier.CONFIG);
|
||||
}
|
||||
|
||||
if (powerReq > powerThreshold) {
|
||||
src.extractAEPower(powerConsumption, Actionable.MODULATE, PowerMultiplier.CONFIG);
|
||||
|
||||
if (this.getProcessingTime() == 0) {
|
||||
this.setProcessingTime(this.getProcessingTime() + speedFactor);
|
||||
} else {
|
||||
this.setProcessingTime(this.getProcessingTime() + ticksSinceLastCall * speedFactor);
|
||||
}
|
||||
}
|
||||
} catch (final GridAccessException e) {
|
||||
// :P
|
||||
}
|
||||
|
||||
if (this.getProcessingTime() > this.getMaxProcessingTime()) {
|
||||
this.setProcessingTime(this.getMaxProcessingTime());
|
||||
final InscriberRecipe out = this.getTask();
|
||||
if (out != null) {
|
||||
final ItemStack outputCopy = out.getOutput().copy();
|
||||
if (this.sideItemHandler.setInvStack(1, outputCopy, Simulation.SIMULATE)) {
|
||||
this.setSmash(true);
|
||||
this.finalStep = 0;
|
||||
this.markForUpdate();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return this.hasWork() ? TickRateModulation.URGENT : TickRateModulation.SLEEP;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IConfigManager getConfigManager() {
|
||||
return this.settings;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FixedItemInv getInventoryByName(final String name) {
|
||||
if (name.equals("inv")) {
|
||||
return this.getInternalInventory();
|
||||
}
|
||||
|
||||
if (name.equals("upgrades")) {
|
||||
return this.upgrades;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected FixedItemInv getItemHandlerForSide(@Nonnull Direction facing) {
|
||||
if (facing == this.getUp()) {
|
||||
return this.topItemHandlerExtern;
|
||||
} else if (facing == this.getUp().getOpposite()) {
|
||||
return this.bottomItemHandlerExtern;
|
||||
} else {
|
||||
return this.sideItemHandlerExtern;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInstalledUpgrades(final Upgrades u) {
|
||||
return this.upgrades.getInstalledUpgrades(u);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateSetting(final IConfigManager manager, final Settings settingName, final Enum<?> newValue) {
|
||||
}
|
||||
|
||||
public long getClientStart() {
|
||||
return this.clientStart;
|
||||
}
|
||||
|
||||
private void setClientStart(final long clientStart) {
|
||||
this.clientStart = clientStart;
|
||||
}
|
||||
|
||||
public boolean isSmash() {
|
||||
return this.smash;
|
||||
}
|
||||
|
||||
public void setSmash(final boolean smash) {
|
||||
this.smash = smash;
|
||||
}
|
||||
|
||||
public int getMaxProcessingTime() {
|
||||
return this.maxProcessingTime;
|
||||
}
|
||||
|
||||
public int getProcessingTime() {
|
||||
return this.processingTime;
|
||||
}
|
||||
|
||||
private void setProcessingTime(final int processingTime) {
|
||||
this.processingTime = processingTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* This is an item handler that exposes the inscribers inventory while providing
|
||||
* simulation capabilities that do not reset the progress if there's already an
|
||||
* item in a slot. Previously, the progress of the inscriber was reset when
|
||||
* another mod attempted insertion of items when there were already items in the
|
||||
* slot.
|
||||
*/
|
||||
private class ItemHandlerFilter implements IAEItemFilter {
|
||||
@Override
|
||||
public boolean allowExtract(FixedItemInv inv, int slot, int amount) {
|
||||
if (InscriberBlockEntity.this.isSmash()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return inv == InscriberBlockEntity.this.topItemHandler || inv == InscriberBlockEntity.this.bottomItemHandler
|
||||
|| slot == 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean allowInsert(FixedItemInv inv, int slot, ItemStack stack) {
|
||||
// output slot
|
||||
if (slot == 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (InscriberBlockEntity.this.isSmash()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (inv == InscriberBlockEntity.this.topItemHandler || inv == InscriberBlockEntity.this.bottomItemHandler) {
|
||||
if (AEApi.instance().definitions().materials().namePress().isSameAs(stack)) {
|
||||
return true;
|
||||
}
|
||||
return InscriberRecipes.isValidOptionalIngredient(getWorld(), stack);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
package appeng.tile.misc;
|
||||
|
||||
import appeng.api.AEApi;
|
||||
import appeng.api.definitions.IComparableDefinition;
|
||||
import appeng.api.features.InscriberProcessType;
|
||||
import appeng.core.AppEng;
|
||||
import appeng.items.materials.MaterialItem;
|
||||
import appeng.recipes.handlers.InscriberRecipe;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.recipe.Ingredient;
|
||||
import net.minecraft.text.LiteralText;
|
||||
import net.minecraft.util.Identifier;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* This class indexes all inscriber recipes to find valid inputs for the top and
|
||||
* bottom optional slots. This speeds up checks whether inputs for those two
|
||||
* slots are valid.
|
||||
*/
|
||||
public final class InscriberRecipes {
|
||||
|
||||
public static final Identifier NAMEPLATE_RECIPE_ID = new Identifier(AppEng.MOD_ID, "nameplate");
|
||||
|
||||
private InscriberRecipes() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an unmodifiable view of all registered inscriber recipes.
|
||||
*/
|
||||
public static Iterable<InscriberRecipe> getRecipes(World world) {
|
||||
return world.getRecipeManager().method_30027(InscriberRecipe.TYPE);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static InscriberRecipe findRecipe(World world, ItemStack input, ItemStack plateA, ItemStack plateB,
|
||||
boolean supportNamePress) {
|
||||
if (supportNamePress) {
|
||||
IComparableDefinition namePress = AEApi.instance().definitions().materials().namePress();
|
||||
boolean isNameA = namePress.isSameAs(plateA);
|
||||
boolean isNameB = namePress.isSameAs(plateB);
|
||||
|
||||
if ((isNameA && isNameB) || isNameA && plateB.isEmpty()) {
|
||||
return makeNamePressRecipe(input, plateA, plateB);
|
||||
} else if (plateA.isEmpty() && isNameB) {
|
||||
return makeNamePressRecipe(input, plateB, plateA);
|
||||
}
|
||||
}
|
||||
|
||||
for (final InscriberRecipe recipe : getRecipes(world)) {
|
||||
// The recipe can be flipped at will
|
||||
final boolean matchA = recipe.getTopOptional().test(plateA) && recipe.getBottomOptional().test(plateB);
|
||||
final boolean matchB = recipe.getTopOptional().test(plateB) && recipe.getBottomOptional().test(plateA);
|
||||
|
||||
if (matchA || matchB) {
|
||||
if (recipe.getMiddleInput().test(input)) {
|
||||
return recipe;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static InscriberRecipe makeNamePressRecipe(ItemStack input, ItemStack plateA, ItemStack plateB) {
|
||||
String name = "";
|
||||
|
||||
if (!plateA.isEmpty()) {
|
||||
final CompoundTag tag = plateA.getOrCreateTag();
|
||||
name += tag.getString(MaterialItem.TAG_INSCRIBE_NAME);
|
||||
}
|
||||
|
||||
if (!plateB.isEmpty()) {
|
||||
final CompoundTag tag = plateB.getOrCreateTag();
|
||||
name += " " + tag.getString(MaterialItem.TAG_INSCRIBE_NAME);
|
||||
}
|
||||
|
||||
final Ingredient startingItem = Ingredient.ofStacks(input.copy());
|
||||
final ItemStack renamedItem = input.copy();
|
||||
|
||||
if (!name.isEmpty()) {
|
||||
renamedItem.setCustomName(new LiteralText(name));
|
||||
} else {
|
||||
renamedItem.setCustomName(null);
|
||||
}
|
||||
|
||||
final InscriberProcessType type = InscriberProcessType.INSCRIBE;
|
||||
|
||||
return new InscriberRecipe(NAMEPLATE_RECIPE_ID, "", startingItem, renamedItem,
|
||||
plateA.isEmpty() ? Ingredient.EMPTY : Ingredient.ofStacks(plateA),
|
||||
plateB.isEmpty() ? Ingredient.EMPTY : Ingredient.ofStacks(plateB), type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if there is an inscriber recipe that supports the given combination of
|
||||
* top/bottom presses. Both the given combination and the reverse will be
|
||||
* searched.
|
||||
*/
|
||||
public static boolean isValidOptionalIngredientCombination(World world, ItemStack pressA, ItemStack pressB) {
|
||||
for (InscriberRecipe recipe : getRecipes(world)) {
|
||||
if (recipe.getTopOptional().test(pressA) && recipe.getBottomOptional().test(pressB)
|
||||
|| recipe.getTopOptional().test(pressB) && recipe.getBottomOptional().test(pressA)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if there is an inscriber recipe that would use the given item stack as
|
||||
* an optional ingredient. Bottom and top can be used interchangeably here,
|
||||
* because the inscriber will flip the recipe if needed.
|
||||
*/
|
||||
public static boolean isValidOptionalIngredient(World world, ItemStack is) {
|
||||
for (InscriberRecipe recipe : getRecipes(world)) {
|
||||
if (recipe.getTopOptional().test(is) || recipe.getBottomOptional().test(is)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,312 @@
|
||||
/*
|
||||
* This file is part of Applied Energistics 2.
|
||||
* Copyright (c) 2013 - 2015, 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.misc;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.EnumSet;
|
||||
import java.util.List;
|
||||
|
||||
import alexiil.mc.lib.attributes.AttributeList;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
|
||||
import net.minecraft.block.BlockState;
|
||||
import net.minecraft.inventory.CraftingInventory;
|
||||
import net.minecraft.screen.ScreenHandlerType;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.network.PacketByteBuf;
|
||||
import net.minecraft.block.entity.BlockEntity;
|
||||
import net.minecraft.block.entity.BlockEntityType;
|
||||
import net.minecraft.util.math.Direction;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.World;
|
||||
import alexiil.mc.lib.attributes.item.FixedItemInv;
|
||||
|
||||
import appeng.api.AEApi;
|
||||
import appeng.api.config.Actionable;
|
||||
import appeng.api.config.Upgrades;
|
||||
import appeng.api.networking.IGridNode;
|
||||
import appeng.api.networking.crafting.ICraftingLink;
|
||||
import appeng.api.networking.crafting.ICraftingPatternDetails;
|
||||
import appeng.api.networking.crafting.ICraftingProviderHelper;
|
||||
import appeng.api.networking.events.MENetworkChannelsChanged;
|
||||
import appeng.api.networking.events.MENetworkEventSubscribe;
|
||||
import appeng.api.networking.events.MENetworkPowerStatusChange;
|
||||
import appeng.api.networking.ticking.IGridTickable;
|
||||
import appeng.api.networking.ticking.TickRateModulation;
|
||||
import appeng.api.networking.ticking.TickingRequest;
|
||||
import appeng.api.storage.data.IAEItemStack;
|
||||
import appeng.api.util.AECableType;
|
||||
import appeng.api.util.AEPartLocation;
|
||||
import appeng.api.util.DimensionalCoord;
|
||||
import appeng.api.util.IConfigManager;
|
||||
import appeng.container.implementations.InterfaceContainer;
|
||||
import appeng.helpers.DualityInterface;
|
||||
import appeng.helpers.IInterfaceHost;
|
||||
import appeng.helpers.IPriorityHost;
|
||||
import appeng.tile.grid.AENetworkInvBlockEntity;
|
||||
import appeng.util.Platform;
|
||||
import appeng.util.inv.IInventoryDestination;
|
||||
import appeng.util.inv.InvOperation;
|
||||
|
||||
public class InterfaceBlockEntity extends AENetworkInvBlockEntity
|
||||
implements IGridTickable, IInventoryDestination, IInterfaceHost, IPriorityHost {
|
||||
|
||||
private final DualityInterface duality = new DualityInterface(this.getProxy(), this);
|
||||
|
||||
// Indicates that this interface has no specific direction set
|
||||
private boolean omniDirectional = true;
|
||||
|
||||
public InterfaceBlockEntity(BlockEntityType<?> tileEntityTypeIn) {
|
||||
super(tileEntityTypeIn);
|
||||
}
|
||||
|
||||
@MENetworkEventSubscribe
|
||||
public void stateChange(final MENetworkChannelsChanged c) {
|
||||
this.duality.notifyNeighbors();
|
||||
}
|
||||
|
||||
@MENetworkEventSubscribe
|
||||
public void stateChange(final MENetworkPowerStatusChange c) {
|
||||
this.duality.notifyNeighbors();
|
||||
}
|
||||
|
||||
public void setSide(final Direction facing) {
|
||||
if (isClient()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Direction newForward = facing;
|
||||
|
||||
if (!this.omniDirectional && this.getForward() == facing.getOpposite()) {
|
||||
newForward = facing;
|
||||
} else if (!this.omniDirectional
|
||||
&& (this.getForward() == facing || this.getForward() == facing.getOpposite())) {
|
||||
this.omniDirectional = true;
|
||||
} else if (this.omniDirectional) {
|
||||
newForward = facing.getOpposite();
|
||||
this.omniDirectional = false;
|
||||
} else {
|
||||
newForward = Platform.rotateAround(this.getForward(), facing);
|
||||
}
|
||||
|
||||
if (this.omniDirectional) {
|
||||
this.setOrientation(Direction.NORTH, Direction.UP);
|
||||
} else {
|
||||
Direction newUp = Direction.UP;
|
||||
if (newForward == Direction.UP || newForward == Direction.DOWN) {
|
||||
newUp = Direction.NORTH;
|
||||
}
|
||||
this.setOrientation(newForward, newUp);
|
||||
}
|
||||
|
||||
this.configureNodeSides();
|
||||
this.markForUpdate();
|
||||
this.saveChanges();
|
||||
}
|
||||
|
||||
private void configureNodeSides() {
|
||||
if (this.omniDirectional) {
|
||||
this.getProxy().setValidSides(EnumSet.allOf(Direction.class));
|
||||
} else {
|
||||
this.getProxy().setValidSides(EnumSet.complementOf(EnumSet.of(this.getForward())));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getDrops(final World w, final BlockPos pos, final List<ItemStack> drops) {
|
||||
this.duality.addDrops(drops);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void gridChanged() {
|
||||
this.duality.gridChanged();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReady() {
|
||||
this.configureNodeSides();
|
||||
|
||||
super.onReady();
|
||||
this.duality.initialize();
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompoundTag toTag(final CompoundTag data) {
|
||||
super.toTag(data);
|
||||
data.putBoolean("omniDirectional", this.omniDirectional);
|
||||
this.duality.writeToNBT(data);
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromTag(BlockState state, final CompoundTag data) {
|
||||
super.fromTag(state, data);
|
||||
this.omniDirectional = data.getBoolean("omniDirectional");
|
||||
|
||||
this.duality.readFromNBT(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean readFromStream(final PacketByteBuf data) throws IOException {
|
||||
final boolean c = super.readFromStream(data);
|
||||
boolean oldOmniDirectional = this.omniDirectional;
|
||||
this.omniDirectional = data.readBoolean();
|
||||
return oldOmniDirectional != this.omniDirectional || c;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void writeToStream(final PacketByteBuf data) throws IOException {
|
||||
super.writeToStream(data);
|
||||
data.writeBoolean(this.omniDirectional);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AECableType getCableConnectionType(final AEPartLocation dir) {
|
||||
return this.duality.getCableConnectionType(dir);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DimensionalCoord getLocation() {
|
||||
return this.duality.getLocation();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canInsert(final ItemStack stack) {
|
||||
return this.duality.canInsert(stack);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FixedItemInv getInventoryByName(final String name) {
|
||||
return this.duality.getInventoryByName(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TickingRequest getTickingRequest(final IGridNode node) {
|
||||
return this.duality.getTickingRequest(node);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TickRateModulation tickingRequest(final IGridNode node, final int ticksSinceLastCall) {
|
||||
return this.duality.tickingRequest(node, ticksSinceLastCall);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FixedItemInv getInternalInventory() {
|
||||
return this.duality.getInternalInventory();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChangeInventory(final FixedItemInv inv, final int slot, final InvOperation mc,
|
||||
final ItemStack removed, final ItemStack added) {
|
||||
this.duality.onChangeInventory(inv, slot, mc, removed, added);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DualityInterface getInterfaceDuality() {
|
||||
return this.duality;
|
||||
}
|
||||
|
||||
@Override
|
||||
public EnumSet<Direction> getTargets() {
|
||||
if (this.omniDirectional) {
|
||||
return EnumSet.allOf(Direction.class);
|
||||
}
|
||||
return EnumSet.of(this.getForward());
|
||||
}
|
||||
|
||||
@Override
|
||||
public BlockEntity getBlockEntity() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IConfigManager getConfigManager() {
|
||||
return this.duality.getConfigManager();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean pushPattern(final ICraftingPatternDetails patternDetails, final CraftingInventory table) {
|
||||
return this.duality.pushPattern(patternDetails, table);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isBusy() {
|
||||
return this.duality.isBusy();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void provideCrafting(final ICraftingProviderHelper craftingTracker) {
|
||||
this.duality.provideCrafting(craftingTracker);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInstalledUpgrades(final Upgrades u) {
|
||||
return this.duality.getInstalledUpgrades(u);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ImmutableSet<ICraftingLink> getRequestedJobs() {
|
||||
return this.duality.getRequestedJobs();
|
||||
}
|
||||
|
||||
@Override
|
||||
public IAEItemStack injectCraftedItems(final ICraftingLink link, final IAEItemStack items, final Actionable mode) {
|
||||
return this.duality.injectCraftedItems(link, items, mode);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void jobStateChange(final ICraftingLink link) {
|
||||
this.duality.jobStateChange(link);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPriority() {
|
||||
return this.duality.getPriority();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPriority(final int newValue) {
|
||||
this.duality.setPriority(newValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return True if this interface is omni-directional.
|
||||
*/
|
||||
public boolean isOmniDirectional() {
|
||||
return this.omniDirectional;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addAllAttributes(World world, BlockPos pos, BlockState state, AttributeList<?> to) {
|
||||
super.addAllAttributes(world, pos, state, to);
|
||||
|
||||
duality.addAllAttributes(to);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack getItemStackRepresentation() {
|
||||
return AEApi.instance().definitions().blocks().iface().maybeStack(1).orElse(ItemStack.EMPTY);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScreenHandlerType<?> getContainerType() {
|
||||
return InterfaceContainer.TYPE;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,249 @@
|
||||
/*
|
||||
* 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.misc;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import io.netty.buffer.Unpooled;
|
||||
|
||||
import net.minecraft.block.BlockState;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.network.PacketByteBuf;
|
||||
import net.minecraft.block.entity.BlockEntityType;
|
||||
import net.minecraft.util.math.Direction;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.math.Vec3d;
|
||||
import net.minecraft.world.LightType;
|
||||
|
||||
import appeng.api.util.AEColor;
|
||||
import appeng.block.paint.PaintSplotches;
|
||||
import appeng.helpers.Splotch;
|
||||
import appeng.items.misc.PaintBallItem;
|
||||
import appeng.tile.AEBaseBlockEntity;
|
||||
|
||||
public class PaintSplotchesBlockEntity extends AEBaseBlockEntity {
|
||||
|
||||
private static final int LIGHT_PER_DOT = 12;
|
||||
|
||||
private int isLit = 0;
|
||||
private List<Splotch> dots = null;
|
||||
|
||||
public PaintSplotchesBlockEntity(BlockEntityType<?> tileEntityTypeIn) {
|
||||
super(tileEntityTypeIn);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canBeRotated() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompoundTag toTag(final CompoundTag data) {
|
||||
super.toTag(data);
|
||||
final PacketByteBuf myDat = new PacketByteBuf(Unpooled.buffer());
|
||||
this.writeBuffer(myDat);
|
||||
if (myDat.hasArray()) {
|
||||
data.putByteArray("dots", myDat.array());
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
private void writeBuffer(final PacketByteBuf out) {
|
||||
if (this.dots == null) {
|
||||
out.writeByte(0);
|
||||
return;
|
||||
}
|
||||
|
||||
out.writeByte(this.dots.size());
|
||||
|
||||
for (final Splotch s : this.dots) {
|
||||
s.writeToStream(out);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromTag(BlockState state, final CompoundTag data) {
|
||||
super.fromTag(state, data);
|
||||
if (data.contains("dots")) {
|
||||
this.readBuffer(new PacketByteBuf(Unpooled.copiedBuffer(data.getByteArray("dots"))));
|
||||
}
|
||||
}
|
||||
|
||||
private void readBuffer(final PacketByteBuf in) {
|
||||
final byte howMany = in.readByte();
|
||||
|
||||
if (howMany == 0) {
|
||||
this.isLit = 0;
|
||||
this.dots = null;
|
||||
return;
|
||||
}
|
||||
|
||||
this.dots = new ArrayList(howMany);
|
||||
for (int x = 0; x < howMany; x++) {
|
||||
this.dots.add(new Splotch(in));
|
||||
}
|
||||
|
||||
this.isLit = 0;
|
||||
for (final Splotch s : this.dots) {
|
||||
if (s.isLumen()) {
|
||||
this.isLit += LIGHT_PER_DOT;
|
||||
}
|
||||
}
|
||||
|
||||
this.maxLit();
|
||||
}
|
||||
|
||||
private void maxLit() {
|
||||
if (this.isLit > 14) {
|
||||
this.isLit = 14;
|
||||
}
|
||||
|
||||
if (this.world != null) {
|
||||
this.world.getLightLevel(LightType.BLOCK, this.pos);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void writeToStream(final PacketByteBuf data) throws IOException {
|
||||
super.writeToStream(data);
|
||||
this.writeBuffer(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean readFromStream(final PacketByteBuf data) throws IOException {
|
||||
super.readFromStream(data);
|
||||
this.readBuffer(data);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void neighborUpdate() {
|
||||
if (this.dots == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (final Direction side : Direction.values()) {
|
||||
if (!this.isSideValid(side)) {
|
||||
this.removeSide(side);
|
||||
}
|
||||
}
|
||||
|
||||
this.updateData();
|
||||
}
|
||||
|
||||
public boolean isSideValid(final Direction side) {
|
||||
final BlockPos p = this.pos.offset(side);
|
||||
final BlockState blk = this.world.getBlockState(p);
|
||||
return blk.isSideSolidFullSquare(world, p, side.getOpposite());
|
||||
}
|
||||
|
||||
private void removeSide(final Direction side) {
|
||||
final Iterator<Splotch> i = this.dots.iterator();
|
||||
while (i.hasNext()) {
|
||||
final Splotch s = i.next();
|
||||
if (s.getSide() == side) {
|
||||
i.remove();
|
||||
}
|
||||
}
|
||||
|
||||
this.markForUpdate();
|
||||
this.saveChanges();
|
||||
}
|
||||
|
||||
private void updateData() {
|
||||
this.isLit = 0;
|
||||
for (final Splotch s : this.dots) {
|
||||
if (s.isLumen()) {
|
||||
this.isLit += LIGHT_PER_DOT;
|
||||
}
|
||||
}
|
||||
|
||||
this.maxLit();
|
||||
|
||||
if (this.dots.isEmpty()) {
|
||||
this.dots = null;
|
||||
}
|
||||
|
||||
if (this.dots == null) {
|
||||
this.world.removeBlock(this.pos, false);
|
||||
}
|
||||
}
|
||||
|
||||
public void cleanSide(final Direction side) {
|
||||
if (this.dots == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.removeSide(side);
|
||||
|
||||
this.updateData();
|
||||
}
|
||||
|
||||
public int getLightLevel() {
|
||||
return this.isLit;
|
||||
}
|
||||
|
||||
public void addBlot(final ItemStack type, final Direction side, final Vec3d hitVec) {
|
||||
final BlockPos p = this.pos.offset(side);
|
||||
|
||||
final BlockState blk = this.world.getBlockState(p);
|
||||
if (blk.isSideSolidFullSquare(this.world, p, side.getOpposite())) {
|
||||
final PaintBallItem ipb = (PaintBallItem) type.getItem();
|
||||
|
||||
final AEColor col = ipb.getColor();
|
||||
final boolean lit = ipb.isLumen();
|
||||
|
||||
if (this.dots == null) {
|
||||
this.dots = new ArrayList<>();
|
||||
}
|
||||
|
||||
if (this.dots.size() > 20) {
|
||||
this.dots.remove(0);
|
||||
}
|
||||
|
||||
this.dots.add(new Splotch(col, lit, side, hitVec));
|
||||
if (lit) {
|
||||
this.isLit += LIGHT_PER_DOT;
|
||||
}
|
||||
|
||||
this.maxLit();
|
||||
this.markForUpdate();
|
||||
this.saveChanges();
|
||||
}
|
||||
}
|
||||
|
||||
public Collection<Splotch> getDots() {
|
||||
if (this.dots == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
return this.dots;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getRenderAttachmentData() {
|
||||
return new PaintSplotches(getDots());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
/*
|
||||
* 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.misc;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.EnumSet;
|
||||
|
||||
import net.minecraft.block.entity.BlockEntityType;
|
||||
import net.minecraft.network.PacketByteBuf;
|
||||
import net.minecraft.util.math.Direction;
|
||||
|
||||
import appeng.api.implementations.IPowerChannelState;
|
||||
import appeng.api.implementations.tiles.ICrystalGrowthAccelerator;
|
||||
import appeng.api.networking.events.MENetworkEventSubscribe;
|
||||
import appeng.api.networking.events.MENetworkPowerStatusChange;
|
||||
import appeng.api.util.AECableType;
|
||||
import appeng.api.util.AEPartLocation;
|
||||
import appeng.me.GridAccessException;
|
||||
import appeng.tile.grid.AENetworkBlockEntity;
|
||||
import appeng.util.Platform;
|
||||
|
||||
public class QuartzGrowthAcceleratorBlockEntity extends AENetworkBlockEntity
|
||||
implements IPowerChannelState, ICrystalGrowthAccelerator {
|
||||
|
||||
private boolean hasPower = false;
|
||||
|
||||
public QuartzGrowthAcceleratorBlockEntity(BlockEntityType<?> tileEntityTypeIn) {
|
||||
super(tileEntityTypeIn);
|
||||
this.getProxy().setValidSides(EnumSet.noneOf(Direction.class));
|
||||
this.getProxy().setFlags();
|
||||
this.getProxy().setIdlePowerUsage(8);
|
||||
}
|
||||
|
||||
@MENetworkEventSubscribe
|
||||
public void onPower(final MENetworkPowerStatusChange ch) {
|
||||
this.markForUpdate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public AECableType getCableConnectionType(final AEPartLocation dir) {
|
||||
return AECableType.COVERED;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean readFromStream(final PacketByteBuf data) throws IOException {
|
||||
final boolean c = super.readFromStream(data);
|
||||
final boolean hadPower = this.isPowered();
|
||||
this.setPowered(data.readBoolean());
|
||||
return this.isPowered() != hadPower || c;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToStream(final PacketByteBuf data) throws IOException {
|
||||
super.writeToStream(data);
|
||||
try {
|
||||
data.writeBoolean(this.getProxy().getEnergy().isNetworkPowered());
|
||||
} catch (final GridAccessException e) {
|
||||
data.writeBoolean(false);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setOrientation(final Direction inForward, final Direction inUp) {
|
||||
super.setOrientation(inForward, inUp);
|
||||
this.getProxy().setValidSides(EnumSet.of(this.getUp(), this.getUp().getOpposite()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPowered() {
|
||||
if (Platform.isServer()) {
|
||||
try {
|
||||
return this.getProxy().getEnergy().isNetworkPowered();
|
||||
} catch (final GridAccessException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return this.hasPower;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isActive() {
|
||||
return this.isPowered();
|
||||
}
|
||||
|
||||
private void setPowered(final boolean hasPower) {
|
||||
this.hasPower = hasPower;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,335 @@
|
||||
/*
|
||||
* This file is part of Applied Energistics 2.
|
||||
* Copyright (c) 2013 - 2015, 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.misc;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.EnumSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import alexiil.mc.lib.attributes.item.FixedItemInv;
|
||||
import net.minecraft.block.BlockState;
|
||||
import net.minecraft.block.entity.BlockEntityType;
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.nbt.Tag;
|
||||
import net.minecraft.network.PacketByteBuf;
|
||||
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.SecurityPermissions;
|
||||
import appeng.api.config.Settings;
|
||||
import appeng.api.config.SortDir;
|
||||
import appeng.api.config.SortOrder;
|
||||
import appeng.api.config.ViewItems;
|
||||
import appeng.api.events.LocatableEventAnnounce;
|
||||
import appeng.api.events.LocatableEventAnnounce.LocatableEvent;
|
||||
import appeng.api.features.ILocatable;
|
||||
import appeng.api.features.IPlayerRegistry;
|
||||
import appeng.api.implementations.items.IBiometricCard;
|
||||
import appeng.api.implementations.tiles.IColorableTile;
|
||||
import appeng.api.networking.GridFlags;
|
||||
import appeng.api.networking.events.MENetworkChannelsChanged;
|
||||
import appeng.api.networking.events.MENetworkEventSubscribe;
|
||||
import appeng.api.networking.events.MENetworkPowerStatusChange;
|
||||
import appeng.api.networking.events.MENetworkSecurityChange;
|
||||
import appeng.api.networking.security.ISecurityProvider;
|
||||
import appeng.api.storage.IMEInventoryHandler;
|
||||
import appeng.api.storage.IMEMonitor;
|
||||
import appeng.api.storage.IStorageChannel;
|
||||
import appeng.api.storage.ITerminalHost;
|
||||
import appeng.api.storage.channels.IItemStorageChannel;
|
||||
import appeng.api.storage.data.IAEItemStack;
|
||||
import appeng.api.storage.data.IAEStack;
|
||||
import appeng.api.util.AECableType;
|
||||
import appeng.api.util.AEColor;
|
||||
import appeng.api.util.AEPartLocation;
|
||||
import appeng.api.util.DimensionalCoord;
|
||||
import appeng.api.util.IConfigManager;
|
||||
import appeng.helpers.PlayerSecurityWrapper;
|
||||
import appeng.me.GridAccessException;
|
||||
import appeng.me.helpers.MEMonitorHandler;
|
||||
import appeng.me.storage.SecurityStationInventory;
|
||||
import appeng.tile.grid.AENetworkBlockEntity;
|
||||
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.IAEAppEngInventory;
|
||||
import appeng.util.inv.InvOperation;
|
||||
import appeng.util.item.AEItemStack;
|
||||
|
||||
public class SecurityStationBlockEntity extends AENetworkBlockEntity implements ITerminalHost, IAEAppEngInventory,
|
||||
ILocatable, IConfigManagerHost, ISecurityProvider, IColorableTile {
|
||||
|
||||
private static int difference = 0;
|
||||
private final AppEngInternalInventory configSlot = new AppEngInternalInventory(this, 1);
|
||||
private final IConfigManager cm = new ConfigManager(this);
|
||||
private final SecurityStationInventory inventory = new SecurityStationInventory(this);
|
||||
private final MEMonitorHandler<IAEItemStack> securityMonitor = new MEMonitorHandler<>(this.inventory);
|
||||
private long securityKey;
|
||||
private AEColor paintedColor = AEColor.TRANSPARENT;
|
||||
private boolean isActive = false;
|
||||
|
||||
public SecurityStationBlockEntity(BlockEntityType<?> tileEntityTypeIn) {
|
||||
super(tileEntityTypeIn);
|
||||
this.getProxy().setFlags(GridFlags.REQUIRE_CHANNEL);
|
||||
this.getProxy().setIdlePowerUsage(2.0);
|
||||
difference++;
|
||||
|
||||
this.securityKey = System.currentTimeMillis() * 10 + difference;
|
||||
if (difference > 10) {
|
||||
difference = 0;
|
||||
}
|
||||
|
||||
this.cm.registerSetting(Settings.SORT_BY, SortOrder.NAME);
|
||||
this.cm.registerSetting(Settings.VIEW_MODE, ViewItems.ALL);
|
||||
this.cm.registerSetting(Settings.SORT_DIRECTION, SortDir.ASCENDING);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChangeInventory(final FixedItemInv inv, final int slot, final InvOperation mc,
|
||||
final ItemStack removedStack, final ItemStack newStack) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getDrops(final World w, final BlockPos pos, final List<ItemStack> drops) {
|
||||
if (!ItemHandlerUtil.isEmpty(this.getConfigSlot())) {
|
||||
drops.add(this.getConfigSlot().getInvStack(0));
|
||||
}
|
||||
|
||||
for (final IAEItemStack ais : this.inventory.getStoredItems()) {
|
||||
drops.add(ais.createItemStack());
|
||||
}
|
||||
}
|
||||
|
||||
IMEInventoryHandler<IAEItemStack> getSecurityInventory() {
|
||||
return this.inventory;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean readFromStream(final PacketByteBuf data) throws IOException {
|
||||
final boolean c = super.readFromStream(data);
|
||||
final boolean wasActive = this.isActive;
|
||||
this.isActive = data.readBoolean();
|
||||
|
||||
final AEColor oldPaintedColor = this.paintedColor;
|
||||
this.paintedColor = AEColor.values()[data.readByte()];
|
||||
|
||||
return oldPaintedColor != this.paintedColor || wasActive != this.isActive || c;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void writeToStream(final PacketByteBuf data) throws IOException {
|
||||
super.writeToStream(data);
|
||||
data.writeBoolean(this.getProxy().isActive());
|
||||
data.writeByte(this.paintedColor.ordinal());
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompoundTag toTag(final CompoundTag data) {
|
||||
super.toTag(data);
|
||||
this.cm.writeToNBT(data);
|
||||
data.putByte("paintedColor", (byte) this.paintedColor.ordinal());
|
||||
|
||||
data.putLong("securityKey", this.securityKey);
|
||||
this.getConfigSlot().writeToNBT(data, "config");
|
||||
|
||||
final CompoundTag storedItems = new CompoundTag();
|
||||
|
||||
int offset = 0;
|
||||
for (final IAEItemStack ais : this.inventory.getStoredItems()) {
|
||||
final CompoundTag it = new CompoundTag();
|
||||
ais.createItemStack().toTag(it);
|
||||
storedItems.put(String.valueOf(offset), it);
|
||||
offset++;
|
||||
}
|
||||
|
||||
data.put("storedItems", storedItems);
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromTag(BlockState state, final CompoundTag data) {
|
||||
super.fromTag(state, data);
|
||||
this.cm.readFromNBT(data);
|
||||
if (data.contains("paintedColor")) {
|
||||
this.paintedColor = AEColor.values()[data.getByte("paintedColor")];
|
||||
}
|
||||
|
||||
this.securityKey = data.getLong("securityKey");
|
||||
this.getConfigSlot().readFromNBT(data, "config");
|
||||
|
||||
final CompoundTag storedItems = data.getCompound("storedItems");
|
||||
for (final Object key : storedItems.getKeys()) {
|
||||
final Tag obj = storedItems.get((String) key);
|
||||
if (obj instanceof CompoundTag) {
|
||||
this.inventory.getStoredItems().add(AEItemStack.fromItemStack(ItemStack.fromTag((CompoundTag) obj)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void inventoryChanged() {
|
||||
try {
|
||||
this.saveChanges();
|
||||
this.getProxy().getGrid().postEvent(new MENetworkSecurityChange());
|
||||
} catch (final GridAccessException e) {
|
||||
// :P
|
||||
}
|
||||
}
|
||||
|
||||
@MENetworkEventSubscribe
|
||||
public void bootUpdate(final MENetworkChannelsChanged changed) {
|
||||
this.markForUpdate();
|
||||
}
|
||||
|
||||
@MENetworkEventSubscribe
|
||||
public void powerUpdate(final MENetworkPowerStatusChange changed) {
|
||||
this.markForUpdate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public AECableType getCableConnectionType(final AEPartLocation dir) {
|
||||
return AECableType.SMART;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChunkUnloaded() {
|
||||
super.onChunkUnloaded();
|
||||
LocatableEventAnnounce.EVENT.invoker().onLocatableAnnounce(this, LocatableEvent.UNREGISTER);
|
||||
this.isActive = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReady() {
|
||||
super.onReady();
|
||||
if (Platform.isServer()) {
|
||||
this.isActive = true;
|
||||
LocatableEventAnnounce.EVENT.invoker().onLocatableAnnounce(this, LocatableEvent.REGISTER);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void markRemoved() {
|
||||
super.markRemoved();
|
||||
LocatableEventAnnounce.EVENT.invoker().onLocatableAnnounce(this, LocatableEvent.UNREGISTER);
|
||||
this.isActive = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DimensionalCoord getLocation() {
|
||||
return new DimensionalCoord(this);
|
||||
}
|
||||
|
||||
public boolean isActive() {
|
||||
if (world != null && !world.isClient) {
|
||||
return isPowered();
|
||||
} else {
|
||||
return this.isActive;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends IAEStack<T>> IMEMonitor<T> getInventory(IStorageChannel<T> channel) {
|
||||
if (channel == AEApi.instance().storage().getStorageChannel(IItemStorageChannel.class)) {
|
||||
return (IMEMonitor<T>) this.securityMonitor;
|
||||
}
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getLocatableSerial() {
|
||||
return this.securityKey;
|
||||
}
|
||||
|
||||
public boolean isPowered() {
|
||||
return this.getProxy().isActive();
|
||||
}
|
||||
|
||||
@Override
|
||||
public IConfigManager getConfigManager() {
|
||||
return this.cm;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateSetting(final IConfigManager manager, final Settings settingName, final Enum<?> newValue) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getSecurityKey() {
|
||||
return this.securityKey;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readPermissions(final Map<Integer, EnumSet<SecurityPermissions>> playerPerms) {
|
||||
final IPlayerRegistry pr = AEApi.instance().registries().players();
|
||||
|
||||
// read permissions
|
||||
for (final IAEItemStack ais : this.inventory.getStoredItems()) {
|
||||
final ItemStack is = ais.createItemStack();
|
||||
final Item i = is.getItem();
|
||||
if (i instanceof IBiometricCard) {
|
||||
final IBiometricCard bc = (IBiometricCard) i;
|
||||
bc.registerPermissions(new PlayerSecurityWrapper(playerPerms), pr, is);
|
||||
}
|
||||
}
|
||||
|
||||
// make sure thea admin is Boss.
|
||||
playerPerms.put(this.getProxy().getNode().getPlayerID(), EnumSet.allOf(SecurityPermissions.class));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSecurityEnabled() {
|
||||
return this.isActive && this.getProxy().isActive();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOwner() {
|
||||
return this.getProxy().getNode().getPlayerID();
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
|
||||
public AppEngInternalInventory getConfigSlot() {
|
||||
return this.configSlot;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,283 @@
|
||||
/*
|
||||
* 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.misc;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import alexiil.mc.lib.attributes.Simulation;
|
||||
import alexiil.mc.lib.attributes.item.FixedItemInv;
|
||||
import net.fabricmc.fabric.api.registry.FuelRegistry;
|
||||
import net.minecraft.block.BlockState;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.network.PacketByteBuf;
|
||||
import net.minecraft.block.entity.BlockEntityType;
|
||||
import net.minecraft.util.math.Direction;
|
||||
|
||||
import appeng.api.config.Actionable;
|
||||
import appeng.api.networking.IGridNode;
|
||||
import appeng.api.networking.energy.IEnergyGrid;
|
||||
import appeng.api.networking.ticking.IGridTickable;
|
||||
import appeng.api.networking.ticking.TickRateModulation;
|
||||
import appeng.api.networking.ticking.TickingRequest;
|
||||
import appeng.api.util.AECableType;
|
||||
import appeng.api.util.AEPartLocation;
|
||||
import appeng.api.util.DimensionalCoord;
|
||||
import appeng.core.settings.TickRates;
|
||||
import appeng.me.GridAccessException;
|
||||
import appeng.tile.grid.AENetworkInvBlockEntity;
|
||||
import appeng.tile.inventory.AppEngInternalInventory;
|
||||
import appeng.util.Platform;
|
||||
import appeng.util.inv.InvOperation;
|
||||
import appeng.util.inv.WrapperFilteredItemHandler;
|
||||
import appeng.util.inv.filter.IAEItemFilter;
|
||||
|
||||
public class VibrationChamberBlockEntity extends AENetworkInvBlockEntity implements IGridTickable {
|
||||
public static final double POWER_PER_TICK = 5;
|
||||
public static final int MIN_BURN_SPEED = 20;
|
||||
public static final int MAX_BURN_SPEED = 200;
|
||||
public static final double DILATION_SCALING = 25.0; // x4 ~ 40 AE/t at max
|
||||
private final AppEngInternalInventory inv = new AppEngInternalInventory(this, 1);
|
||||
private final FixedItemInv invExt = new WrapperFilteredItemHandler(this.inv, new FuelSlotFilter());
|
||||
|
||||
private int burnSpeed = 100;
|
||||
private double burnTime = 0;
|
||||
private double maxBurnTime = 0;
|
||||
|
||||
// client side..
|
||||
public boolean isOn;
|
||||
|
||||
public VibrationChamberBlockEntity(BlockEntityType<?> tileEntityTypeIn) {
|
||||
super(tileEntityTypeIn);
|
||||
this.getProxy().setIdlePowerUsage(0);
|
||||
this.getProxy().setFlags();
|
||||
}
|
||||
|
||||
@Override
|
||||
public AECableType getCableConnectionType(final AEPartLocation dir) {
|
||||
return AECableType.COVERED;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean readFromStream(final PacketByteBuf data) throws IOException {
|
||||
final boolean c = super.readFromStream(data);
|
||||
final boolean wasOn = this.isOn;
|
||||
|
||||
this.isOn = data.readBoolean();
|
||||
|
||||
return wasOn != this.isOn || c; // TESR doesn't need updates!
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void writeToStream(final PacketByteBuf data) throws IOException {
|
||||
super.writeToStream(data);
|
||||
data.writeBoolean(this.getBurnTime() > 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompoundTag toTag(final CompoundTag data) {
|
||||
super.toTag(data);
|
||||
data.putDouble("burnTime", this.getBurnTime());
|
||||
data.putDouble("maxBurnTime", this.getMaxBurnTime());
|
||||
data.putInt("burnSpeed", this.getBurnSpeed());
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromTag(BlockState state, final CompoundTag data) {
|
||||
super.fromTag(state, data);
|
||||
this.setBurnTime(data.getDouble("burnTime"));
|
||||
this.setMaxBurnTime(data.getDouble("maxBurnTime"));
|
||||
this.setBurnSpeed(data.getInt("burnSpeed"));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected FixedItemInv getItemHandlerForSide(@Nonnull Direction facing) {
|
||||
return this.invExt;
|
||||
}
|
||||
|
||||
@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.getBurnTime() <= 0) {
|
||||
if (this.canEatFuel()) {
|
||||
try {
|
||||
this.getProxy().getTick().wakeDevice(this.getProxy().getNode());
|
||||
} catch (final GridAccessException e) {
|
||||
// wake up!
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean canEatFuel() {
|
||||
final ItemStack is = this.inv.getInvStack(0);
|
||||
if (!is.isEmpty()) {
|
||||
final int newBurnTime = FuelRegistry.INSTANCE.get(is.getItem());
|
||||
if (newBurnTime > 0 && is.getCount() > 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DimensionalCoord getLocation() {
|
||||
return new DimensionalCoord(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TickingRequest getTickingRequest(final IGridNode node) {
|
||||
if (this.getBurnTime() <= 0) {
|
||||
this.eatFuel();
|
||||
}
|
||||
|
||||
return new TickingRequest(TickRates.VibrationChamber.getMin(), TickRates.VibrationChamber.getMax(),
|
||||
this.getBurnTime() <= 0, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TickRateModulation tickingRequest(final IGridNode node, final int ticksSinceLastCall) {
|
||||
if (this.getBurnTime() <= 0) {
|
||||
this.eatFuel();
|
||||
|
||||
if (this.getBurnTime() > 0) {
|
||||
return TickRateModulation.URGENT;
|
||||
}
|
||||
|
||||
this.setBurnSpeed(100);
|
||||
return TickRateModulation.SLEEP;
|
||||
}
|
||||
|
||||
this.setBurnSpeed(Math.max(MIN_BURN_SPEED, Math.min(this.getBurnSpeed(), MAX_BURN_SPEED)));
|
||||
final double dilation = this.getBurnSpeed() / DILATION_SCALING;
|
||||
|
||||
double timePassed = ticksSinceLastCall * dilation;
|
||||
this.setBurnTime(this.getBurnTime() - timePassed);
|
||||
if (this.getBurnTime() < 0) {
|
||||
timePassed += this.getBurnTime();
|
||||
this.setBurnTime(0);
|
||||
}
|
||||
|
||||
try {
|
||||
final IEnergyGrid grid = this.getProxy().getEnergy();
|
||||
final double newPower = timePassed * POWER_PER_TICK;
|
||||
final double overFlow = grid.injectPower(newPower, Actionable.SIMULATE);
|
||||
|
||||
// burn the over flow.
|
||||
grid.injectPower(Math.max(0.0, newPower - overFlow), Actionable.MODULATE);
|
||||
|
||||
if (overFlow > 0) {
|
||||
this.setBurnSpeed(this.getBurnSpeed() - ticksSinceLastCall);
|
||||
} else {
|
||||
this.setBurnSpeed(this.getBurnSpeed() + ticksSinceLastCall);
|
||||
}
|
||||
|
||||
this.setBurnSpeed(Math.max(MIN_BURN_SPEED, Math.min(this.getBurnSpeed(), MAX_BURN_SPEED)));
|
||||
return overFlow > 0 ? TickRateModulation.SLOWER : TickRateModulation.FASTER;
|
||||
} catch (final GridAccessException e) {
|
||||
this.setBurnSpeed(this.getBurnSpeed() - ticksSinceLastCall);
|
||||
this.setBurnSpeed(Math.max(MIN_BURN_SPEED, Math.min(this.getBurnSpeed(), MAX_BURN_SPEED)));
|
||||
return TickRateModulation.SLOWER;
|
||||
}
|
||||
}
|
||||
|
||||
private void eatFuel() {
|
||||
final ItemStack is = this.inv.getInvStack(0);
|
||||
if (!is.isEmpty()) {
|
||||
final int newBurnTime = FuelRegistry.INSTANCE.get(is.getItem());
|
||||
if (newBurnTime > 0 && is.getCount() > 0) {
|
||||
this.setBurnTime(this.getBurnTime() + newBurnTime);
|
||||
this.setMaxBurnTime(this.getBurnTime());
|
||||
|
||||
final Item fuelItem = is.getItem();
|
||||
is.decrement(1);
|
||||
|
||||
if (is.isEmpty()) {
|
||||
this.inv.setInvStack(0, new ItemStack(fuelItem.getRecipeRemainder()), Simulation.ACTION);
|
||||
} else {
|
||||
this.inv.setInvStack(0, is, Simulation.ACTION);
|
||||
}
|
||||
this.saveChanges();
|
||||
}
|
||||
}
|
||||
|
||||
if (this.getBurnTime() > 0) {
|
||||
try {
|
||||
this.getProxy().getTick().wakeDevice(this.getProxy().getNode());
|
||||
} catch (final GridAccessException e) {
|
||||
// gah!
|
||||
}
|
||||
}
|
||||
|
||||
// state change
|
||||
if ((!this.isOn && this.getBurnTime() > 0) || (this.isOn && this.getBurnTime() <= 0)) {
|
||||
this.isOn = this.getBurnTime() > 0;
|
||||
this.markForUpdate();
|
||||
|
||||
if (this.hasWorld()) {
|
||||
Platform.notifyBlocksOfNeighbors(this.world, this.pos);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int getBurnSpeed() {
|
||||
return this.burnSpeed;
|
||||
}
|
||||
|
||||
private void setBurnSpeed(final int burnSpeed) {
|
||||
this.burnSpeed = burnSpeed;
|
||||
}
|
||||
|
||||
public double getMaxBurnTime() {
|
||||
return this.maxBurnTime;
|
||||
}
|
||||
|
||||
private void setMaxBurnTime(final double maxBurnTime) {
|
||||
this.maxBurnTime = maxBurnTime;
|
||||
}
|
||||
|
||||
public double getBurnTime() {
|
||||
return this.burnTime;
|
||||
}
|
||||
|
||||
private void setBurnTime(final double burnTime) {
|
||||
this.burnTime = burnTime;
|
||||
}
|
||||
|
||||
private static class FuelSlotFilter implements IAEItemFilter {
|
||||
@Override
|
||||
public boolean allowExtract(FixedItemInv inv, int slot, int amount) {
|
||||
return FuelRegistry.INSTANCE.get(inv.getInvStack(slot).getItem()) == 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean allowInsert(FixedItemInv inv, int slot, ItemStack stack) {
|
||||
return FuelRegistry.INSTANCE.get(stack.getItem()) != 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -303,11 +303,14 @@ public class CableBusBlockEntity extends AEBaseBlockEntity implements AEMultiTil
|
||||
public void addAllAttributes(World world, BlockPos pos, BlockState state, AttributeList<?> to) {
|
||||
super.addAllAttributes(world, pos, state, to);
|
||||
|
||||
for (AEPartLocation location : AEPartLocation.values()) {
|
||||
IPart part = this.cb.getPart(location);
|
||||
if (part != null) {
|
||||
part.addAllAttributes(to);
|
||||
}
|
||||
Direction direction = to.getSearchDirection();
|
||||
if (direction == null) {
|
||||
return;
|
||||
}
|
||||
AEPartLocation partLocation = AEPartLocation.fromFacing(direction.getOpposite());
|
||||
IPart part = this.cb.getPart(partLocation);
|
||||
if (part != null) {
|
||||
part.addAllAttributes(to);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,34 +1,192 @@
|
||||
/*
|
||||
* This file is part of Applied Energistics 2.
|
||||
* Copyright (c) 2013 - 2015, 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.networking;
|
||||
|
||||
import appeng.api.networking.IGridHost;
|
||||
import appeng.api.networking.IGridNode;
|
||||
import java.util.EnumSet;
|
||||
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.block.entity.BlockEntityType;
|
||||
import net.minecraft.util.math.Direction;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.math.ChunkPos;
|
||||
import alexiil.mc.lib.attributes.item.FixedItemInv;
|
||||
import alexiil.mc.lib.attributes.item.impl.EmptyFixedItemInv;
|
||||
|
||||
import appeng.api.config.Actionable;
|
||||
import appeng.api.networking.GridFlags;
|
||||
import appeng.api.networking.energy.IEnergyGrid;
|
||||
import appeng.api.networking.events.MENetworkControllerChange;
|
||||
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.pathing.ControllerState;
|
||||
import appeng.api.util.AECableType;
|
||||
import appeng.api.util.AEPartLocation;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import appeng.block.networking.ControllerBlock;
|
||||
import appeng.block.networking.ControllerBlock.ControllerBlockState;
|
||||
import appeng.me.GridAccessException;
|
||||
import appeng.tile.grid.AENetworkPowerBlockEntity;
|
||||
import appeng.util.inv.InvOperation;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
public class ControllerBlockEntity extends AENetworkPowerBlockEntity {
|
||||
private boolean isValid = false;
|
||||
|
||||
// FIXME FABRIC DUMMY
|
||||
public class ControllerBlockEntity implements IGridHost {
|
||||
@Nullable
|
||||
@Override
|
||||
public IGridNode getGridNode(@Nonnull AEPartLocation dir) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public AECableType getCableConnectionType(@Nonnull AEPartLocation dir) {
|
||||
return null;
|
||||
public ControllerBlockEntity(BlockEntityType<?> tileEntityTypeIn) {
|
||||
super(tileEntityTypeIn);
|
||||
this.setInternalMaxPower(8000);
|
||||
this.setInternalPublicPowerStorage(true);
|
||||
this.getProxy().setIdlePowerUsage(3);
|
||||
this.getProxy().setFlags(GridFlags.CANNOT_CARRY, GridFlags.DENSE_CAPACITY);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void securityBreak() {
|
||||
public AECableType getCableConnectionType(final AEPartLocation dir) {
|
||||
return AECableType.DENSE_SMART;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReady() {
|
||||
this.onNeighborChange(true);
|
||||
super.onReady();
|
||||
}
|
||||
|
||||
public void onNeighborChange(final boolean force) {
|
||||
final boolean xx = this.checkController(this.pos.offset(Direction.EAST))
|
||||
&& this.checkController(this.pos.offset(Direction.WEST));
|
||||
final boolean yy = this.checkController(this.pos.offset(Direction.UP))
|
||||
&& this.checkController(this.pos.offset(Direction.DOWN));
|
||||
final boolean zz = this.checkController(this.pos.offset(Direction.NORTH))
|
||||
&& this.checkController(this.pos.offset(Direction.SOUTH));
|
||||
|
||||
// int meta = world.getBlockMetadata( xCoord, yCoord, zCoord );
|
||||
// boolean hasPower = meta > 0;
|
||||
// boolean isConflict = meta == 2;
|
||||
|
||||
final boolean oldValid = this.isValid;
|
||||
|
||||
this.isValid = (xx && !yy && !zz) || (!xx && yy && !zz) || (!xx && !yy && zz)
|
||||
|| ((xx ? 1 : 0) + (yy ? 1 : 0) + (zz ? 1 : 0) <= 1);
|
||||
|
||||
if (oldValid != this.isValid || force) {
|
||||
if (this.isValid) {
|
||||
this.getProxy().setValidSides(EnumSet.allOf(Direction.class));
|
||||
} else {
|
||||
this.getProxy().setValidSides(EnumSet.noneOf(Direction.class));
|
||||
}
|
||||
|
||||
this.updateMeta();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public BlockPos getPos() {
|
||||
return null;
|
||||
private void updateMeta() {
|
||||
if (!this.getProxy().isReady()) {
|
||||
return;
|
||||
}
|
||||
|
||||
ControllerBlockState metaState = ControllerBlockState.offline;
|
||||
|
||||
try {
|
||||
if (this.getProxy().getEnergy().isNetworkPowered()) {
|
||||
metaState = ControllerBlockState.online;
|
||||
|
||||
if (this.getProxy().getPath().getControllerState() == ControllerState.CONTROLLER_CONFLICT) {
|
||||
metaState = ControllerBlockState.conflicted;
|
||||
}
|
||||
}
|
||||
} catch (final GridAccessException e) {
|
||||
metaState = ControllerBlockState.offline;
|
||||
}
|
||||
|
||||
if (this.checkController(this.pos)
|
||||
&& this.world.getBlockState(this.pos).get(ControllerBlock.CONTROLLER_STATE) != metaState) {
|
||||
this.world.setBlockState(this.pos,
|
||||
this.world.getBlockState(this.pos).with(ControllerBlock.CONTROLLER_STATE, metaState));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected double getFunnelPowerDemand(final double maxReceived) {
|
||||
try {
|
||||
final IEnergyGrid grid = this.getProxy().getEnergy();
|
||||
|
||||
return grid.getEnergyDemand(maxReceived);
|
||||
} catch (final GridAccessException e) {
|
||||
// no grid? use local...
|
||||
return super.getFunnelPowerDemand(maxReceived);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected double funnelPowerIntoStorage(final double power, final Actionable mode) {
|
||||
try {
|
||||
final IEnergyGrid grid = this.getProxy().getEnergy();
|
||||
final double leftOver = grid.injectPower(power, mode);
|
||||
|
||||
return leftOver;
|
||||
} catch (final GridAccessException e) {
|
||||
// no grid? use local...
|
||||
return super.funnelPowerIntoStorage(power, mode);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void PowerEvent(final PowerEventType x) {
|
||||
try {
|
||||
this.getProxy().getGrid().postEvent(new MENetworkPowerStorage(this, x));
|
||||
} catch (final GridAccessException e) {
|
||||
// not ready!
|
||||
}
|
||||
}
|
||||
|
||||
@MENetworkEventSubscribe
|
||||
public void onControllerChange(final MENetworkControllerChange status) {
|
||||
this.updateMeta();
|
||||
}
|
||||
|
||||
@MENetworkEventSubscribe
|
||||
public void onPowerChange(final MENetworkPowerStatusChange status) {
|
||||
this.updateMeta();
|
||||
}
|
||||
|
||||
@Override
|
||||
public FixedItemInv getInternalInventory() {
|
||||
return EmptyFixedItemInv.INSTANCE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChangeInventory(final FixedItemInv inv, final int slot, final InvOperation mc,
|
||||
final ItemStack removed, final ItemStack added) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Check for a controller at this coordinates as well as is it loaded.
|
||||
*
|
||||
* @return true if there is a loaded controller
|
||||
*/
|
||||
private boolean checkController(final BlockPos pos) {
|
||||
if (this.world.getChunkManager().isChunkLoaded(pos.getX() >> 4, pos.getZ() >> 4)) {
|
||||
return this.world.getBlockEntity(pos) instanceof ControllerBlockEntity;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* 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.networking;
|
||||
|
||||
import net.minecraft.block.entity.BlockEntityType;
|
||||
|
||||
import appeng.api.config.AccessRestriction;
|
||||
import appeng.api.config.Actionable;
|
||||
import appeng.api.config.PowerMultiplier;
|
||||
import appeng.api.networking.energy.IAEPowerStorage;
|
||||
import appeng.api.util.AECableType;
|
||||
import appeng.api.util.AEPartLocation;
|
||||
import appeng.tile.grid.AENetworkBlockEntity;
|
||||
|
||||
public class CreativeEnergyCellBlockEntity extends AENetworkBlockEntity implements IAEPowerStorage {
|
||||
|
||||
public CreativeEnergyCellBlockEntity(BlockEntityType<?> tileEntityTypeIn) {
|
||||
super(tileEntityTypeIn);
|
||||
this.getProxy().setIdlePowerUsage(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AECableType getCableConnectionType(final AEPartLocation dir) {
|
||||
return AECableType.COVERED;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double injectAEPower(final double amt, final Actionable mode) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getAEMaxPower() {
|
||||
return Long.MAX_VALUE / 10000;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getAECurrentPower() {
|
||||
return Long.MAX_VALUE / 10000;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAEPublicPowerStorage() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AccessRestriction getPowerFlow() {
|
||||
return AccessRestriction.READ_WRITE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double extractAEPower(final double amt, final Actionable mode, final PowerMultiplier pm) {
|
||||
return amt;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* 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.networking;
|
||||
|
||||
import net.minecraft.block.entity.BlockEntityType;
|
||||
|
||||
public class DenseEnergyCellBlockEntity extends EnergyCellBlockEntity {
|
||||
|
||||
public DenseEnergyCellBlockEntity(BlockEntityType<?> tileEntityTypeIn) {
|
||||
super(tileEntityTypeIn);
|
||||
this.setInternalMaxPower(200000 * 8);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* 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.networking;
|
||||
|
||||
import alexiil.mc.lib.attributes.item.impl.EmptyFixedItemInv;
|
||||
import net.minecraft.block.entity.BlockEntityType;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import alexiil.mc.lib.attributes.item.FixedItemInv;
|
||||
|
||||
import appeng.api.config.Actionable;
|
||||
import appeng.api.networking.energy.IEnergyGrid;
|
||||
import appeng.api.util.AECableType;
|
||||
import appeng.api.util.AEPartLocation;
|
||||
import appeng.me.GridAccessException;
|
||||
import appeng.tile.grid.AENetworkPowerBlockEntity;
|
||||
import appeng.util.inv.InvOperation;
|
||||
|
||||
public class EnergyAcceptorBlockEntity extends AENetworkPowerBlockEntity {
|
||||
|
||||
public EnergyAcceptorBlockEntity(BlockEntityType<?> tileEntityTypeIn) {
|
||||
super(tileEntityTypeIn);
|
||||
this.getProxy().setIdlePowerUsage(0.0);
|
||||
this.setInternalMaxPower(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AECableType getCableConnectionType(final AEPartLocation dir) {
|
||||
return AECableType.COVERED;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected double getFunnelPowerDemand(final double maxRequired) {
|
||||
try {
|
||||
final IEnergyGrid grid = this.getProxy().getEnergy();
|
||||
|
||||
return grid.getEnergyDemand(maxRequired);
|
||||
} catch (final GridAccessException e) {
|
||||
return this.getInternalMaxPower();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected double funnelPowerIntoStorage(final double power, final Actionable mode) {
|
||||
try {
|
||||
final IEnergyGrid grid = this.getProxy().getEnergy();
|
||||
final double leftOver = grid.injectPower(power, mode);
|
||||
|
||||
return leftOver;
|
||||
} catch (final GridAccessException e) {
|
||||
return super.funnelPowerIntoStorage(power, mode);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public FixedItemInv getInternalInventory() {
|
||||
return EmptyFixedItemInv.INSTANCE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChangeInventory(final FixedItemInv inv, final int slot, final InvOperation mc,
|
||||
final ItemStack removed, final ItemStack added) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,222 @@
|
||||
/*
|
||||
* 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.networking;
|
||||
|
||||
import net.minecraft.block.BlockState;
|
||||
import net.minecraft.block.entity.BlockEntityType;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
|
||||
import appeng.api.config.AccessRestriction;
|
||||
import appeng.api.config.Actionable;
|
||||
import appeng.api.config.PowerMultiplier;
|
||||
import appeng.api.networking.energy.IAEPowerStorage;
|
||||
import appeng.api.networking.events.MENetworkPowerStorage;
|
||||
import appeng.api.networking.events.MENetworkPowerStorage.PowerEventType;
|
||||
import appeng.api.util.AECableType;
|
||||
import appeng.api.util.AEPartLocation;
|
||||
import appeng.block.networking.EnergyCellBlock;
|
||||
import appeng.me.GridAccessException;
|
||||
import appeng.tile.grid.AENetworkBlockEntity;
|
||||
import appeng.util.SettingsFrom;
|
||||
|
||||
public class EnergyCellBlockEntity extends AENetworkBlockEntity implements IAEPowerStorage {
|
||||
|
||||
private double internalCurrentPower = 0.0;
|
||||
private double internalMaxPower = 200000.0;
|
||||
|
||||
private byte currentMeta = -1;
|
||||
|
||||
public EnergyCellBlockEntity(BlockEntityType<?> tileEntityTypeIn) {
|
||||
super(tileEntityTypeIn);
|
||||
this.getProxy().setIdlePowerUsage(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AECableType getCableConnectionType(final AEPartLocation dir) {
|
||||
return AECableType.COVERED;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReady() {
|
||||
super.onReady();
|
||||
final int value = this.world.getBlockState(this.pos).get(EnergyCellBlock.ENERGY_STORAGE);
|
||||
this.currentMeta = (byte) value;
|
||||
this.changePowerLevel();
|
||||
}
|
||||
|
||||
/**
|
||||
* Given a fill factor, return the storage level (0-7) used for the state of the
|
||||
* block. This is also used for determining the item model.
|
||||
*/
|
||||
public static int getStorageLevelFromFillFactor(double fillFactor) {
|
||||
byte boundMetadata = (byte) (8.0 * (fillFactor));
|
||||
|
||||
if (boundMetadata > 7) {
|
||||
boundMetadata = 7;
|
||||
}
|
||||
if (boundMetadata < 0) {
|
||||
boundMetadata = 0;
|
||||
}
|
||||
return boundMetadata;
|
||||
}
|
||||
|
||||
private void changePowerLevel() {
|
||||
if (this.notLoaded() || this.isRemoved()) {
|
||||
return;
|
||||
}
|
||||
|
||||
int storageLevel = getStorageLevelFromFillFactor(this.internalCurrentPower / this.getInternalMaxPower());
|
||||
|
||||
if (this.currentMeta != storageLevel) {
|
||||
this.currentMeta = (byte) storageLevel;
|
||||
this.world.setBlockState(this.pos,
|
||||
this.world.getBlockState(this.pos).with(EnergyCellBlock.ENERGY_STORAGE, storageLevel));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompoundTag toTag(final CompoundTag data) {
|
||||
super.toTag(data);
|
||||
data.putDouble("internalCurrentPower", this.internalCurrentPower);
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromTag(BlockState state, final CompoundTag data) {
|
||||
super.fromTag(state, data);
|
||||
this.internalCurrentPower = data.getDouble("internalCurrentPower");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canBeRotated() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void uploadSettings(final SettingsFrom from, final CompoundTag compound) {
|
||||
if (from == SettingsFrom.DISMANTLE_ITEM) {
|
||||
this.internalCurrentPower = compound.getDouble("internalCurrentPower");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompoundTag downloadSettings(final SettingsFrom from) {
|
||||
if (from == SettingsFrom.DISMANTLE_ITEM) {
|
||||
final CompoundTag tag = new CompoundTag();
|
||||
tag.putDouble("internalCurrentPower", this.internalCurrentPower);
|
||||
tag.putDouble("internalMaxPower", this.getInternalMaxPower()); // used for tool tip.
|
||||
return tag;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final double injectAEPower(double amt, final Actionable mode) {
|
||||
if (mode == Actionable.SIMULATE) {
|
||||
final double fakeBattery = this.internalCurrentPower + amt;
|
||||
if (fakeBattery > this.getInternalMaxPower()) {
|
||||
return fakeBattery - this.getInternalMaxPower();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (this.internalCurrentPower < 0.01 && amt > 0.01) {
|
||||
this.getProxy().getNode().getGrid()
|
||||
.postEvent(new MENetworkPowerStorage(this, PowerEventType.PROVIDE_POWER));
|
||||
}
|
||||
|
||||
this.internalCurrentPower += amt;
|
||||
if (this.internalCurrentPower > this.getInternalMaxPower()) {
|
||||
amt = this.internalCurrentPower - this.getInternalMaxPower();
|
||||
this.internalCurrentPower = this.getInternalMaxPower();
|
||||
|
||||
this.changePowerLevel();
|
||||
return amt;
|
||||
}
|
||||
|
||||
this.changePowerLevel();
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getAEMaxPower() {
|
||||
return this.getInternalMaxPower();
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getAECurrentPower() {
|
||||
return this.internalCurrentPower;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAEPublicPowerStorage() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AccessRestriction getPowerFlow() {
|
||||
return AccessRestriction.READ_WRITE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final double extractAEPower(final double amt, final Actionable mode, final PowerMultiplier pm) {
|
||||
return pm.divide(this.extractAEPower(pm.multiply(amt), mode));
|
||||
}
|
||||
|
||||
private double extractAEPower(double amt, final Actionable mode) {
|
||||
if (mode == Actionable.SIMULATE) {
|
||||
if (this.internalCurrentPower > amt) {
|
||||
return amt;
|
||||
}
|
||||
return this.internalCurrentPower;
|
||||
}
|
||||
|
||||
final boolean wasFull = this.internalCurrentPower >= this.getInternalMaxPower() - 0.001;
|
||||
|
||||
if (wasFull && amt > 0.001) {
|
||||
try {
|
||||
this.getProxy().getGrid().postEvent(new MENetworkPowerStorage(this, PowerEventType.REQUEST_POWER));
|
||||
} catch (final GridAccessException ignored) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if (this.internalCurrentPower > amt) {
|
||||
this.internalCurrentPower -= amt;
|
||||
|
||||
this.changePowerLevel();
|
||||
return amt;
|
||||
}
|
||||
|
||||
amt = this.internalCurrentPower;
|
||||
this.internalCurrentPower = 0;
|
||||
|
||||
this.changePowerLevel();
|
||||
return amt;
|
||||
}
|
||||
|
||||
private double getInternalMaxPower() {
|
||||
return this.internalMaxPower;
|
||||
}
|
||||
|
||||
void setInternalMaxPower(final double internalMaxPower) {
|
||||
this.internalMaxPower = internalMaxPower;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
/*
|
||||
* 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.networking;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.EnumSet;
|
||||
|
||||
import alexiil.mc.lib.attributes.item.FixedItemInv;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.network.PacketByteBuf;
|
||||
import net.minecraft.block.entity.BlockEntityType;
|
||||
import net.minecraft.util.math.Direction;
|
||||
|
||||
import appeng.api.AEApi;
|
||||
import appeng.api.implementations.IPowerChannelState;
|
||||
import appeng.api.implementations.tiles.IWirelessAccessPoint;
|
||||
import appeng.api.networking.GridFlags;
|
||||
import appeng.api.networking.IGrid;
|
||||
import appeng.api.networking.events.MENetworkChannelsChanged;
|
||||
import appeng.api.networking.events.MENetworkEventSubscribe;
|
||||
import appeng.api.networking.events.MENetworkPowerStatusChange;
|
||||
import appeng.api.util.AECableType;
|
||||
import appeng.api.util.AEPartLocation;
|
||||
import appeng.api.util.DimensionalCoord;
|
||||
import appeng.core.AEConfig;
|
||||
import appeng.me.GridAccessException;
|
||||
import appeng.tile.grid.AENetworkInvBlockEntity;
|
||||
import appeng.tile.inventory.AppEngInternalInventory;
|
||||
import appeng.util.inv.InvOperation;
|
||||
import appeng.util.inv.filter.AEItemDefinitionFilter;
|
||||
|
||||
public class WirelessBlockEntity extends AENetworkInvBlockEntity implements IWirelessAccessPoint, IPowerChannelState {
|
||||
|
||||
public static final int POWERED_FLAG = 1;
|
||||
public static final int CHANNEL_FLAG = 2;
|
||||
|
||||
private final AppEngInternalInventory inv = new AppEngInternalInventory(this, 1);
|
||||
|
||||
private int clientFlags = 0;
|
||||
|
||||
public WirelessBlockEntity(BlockEntityType<?> tileEntityTypeIn) {
|
||||
super(tileEntityTypeIn);
|
||||
this.inv.setFilter(new AEItemDefinitionFilter(AEApi.instance().definitions().materials().wirelessBooster()));
|
||||
this.getProxy().setFlags(GridFlags.REQUIRE_CHANNEL);
|
||||
this.getProxy().setValidSides(EnumSet.noneOf(Direction.class));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setOrientation(final Direction inForward, final Direction inUp) {
|
||||
super.setOrientation(inForward, inUp);
|
||||
this.getProxy().setValidSides(EnumSet.of(this.getForward().getOpposite()));
|
||||
}
|
||||
|
||||
@MENetworkEventSubscribe
|
||||
public void chanRender(final MENetworkChannelsChanged c) {
|
||||
this.markForUpdate();
|
||||
}
|
||||
|
||||
@MENetworkEventSubscribe
|
||||
public void powerRender(final MENetworkPowerStatusChange c) {
|
||||
this.markForUpdate();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean readFromStream(final PacketByteBuf data) throws IOException {
|
||||
final boolean c = super.readFromStream(data);
|
||||
final int old = this.getClientFlags();
|
||||
this.setClientFlags(data.readByte());
|
||||
|
||||
return old != this.getClientFlags() || c;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void writeToStream(final PacketByteBuf data) throws IOException {
|
||||
super.writeToStream(data);
|
||||
this.setClientFlags(0);
|
||||
|
||||
try {
|
||||
if (this.getProxy().getEnergy().isNetworkPowered()) {
|
||||
this.setClientFlags(this.getClientFlags() | POWERED_FLAG);
|
||||
}
|
||||
|
||||
if (this.getProxy().getNode().meetsChannelRequirements()) {
|
||||
this.setClientFlags(this.getClientFlags() | CHANNEL_FLAG);
|
||||
}
|
||||
} catch (final GridAccessException e) {
|
||||
// meh
|
||||
}
|
||||
|
||||
data.writeByte((byte) this.getClientFlags());
|
||||
}
|
||||
|
||||
@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) {
|
||||
// :P
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReady() {
|
||||
this.updatePower();
|
||||
super.onReady();
|
||||
}
|
||||
|
||||
private void updatePower() {
|
||||
this.getProxy().setIdlePowerUsage(AEConfig.instance().wireless_getPowerDrain(this.getBoosters()));
|
||||
}
|
||||
|
||||
private int getBoosters() {
|
||||
final ItemStack boosters = this.inv.getInvStack(0);
|
||||
return boosters == null ? 0 : boosters.getCount();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveChanges() {
|
||||
this.updatePower();
|
||||
super.saveChanges();
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getRange() {
|
||||
return AEConfig.instance().wireless_getMaxRange(this.getBoosters());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isActive() {
|
||||
if (isClient()) {
|
||||
return this.isPowered() && (CHANNEL_FLAG == (this.getClientFlags() & CHANNEL_FLAG));
|
||||
}
|
||||
|
||||
return this.getProxy().isActive();
|
||||
}
|
||||
|
||||
@Override
|
||||
public IGrid getGrid() {
|
||||
try {
|
||||
return this.getProxy().getGrid();
|
||||
} catch (final GridAccessException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPowered() {
|
||||
return POWERED_FLAG == (this.getClientFlags() & POWERED_FLAG);
|
||||
}
|
||||
|
||||
public int getClientFlags() {
|
||||
return this.clientFlags;
|
||||
}
|
||||
|
||||
private void setClientFlags(final int clientFlags) {
|
||||
this.clientFlags = clientFlags;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,300 @@
|
||||
/*
|
||||
* 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.qnb;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.EnumSet;
|
||||
import java.util.Optional;
|
||||
|
||||
import alexiil.mc.lib.attributes.item.FixedItemInv;
|
||||
import net.minecraft.block.Block;
|
||||
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.BlockEntity;
|
||||
import net.minecraft.block.entity.BlockEntityType;
|
||||
import net.minecraft.util.math.Direction;
|
||||
|
||||
import alexiil.mc.lib.attributes.item.impl.EmptyFixedItemInv;
|
||||
|
||||
import appeng.api.AEApi;
|
||||
import appeng.api.definitions.IBlockDefinition;
|
||||
import appeng.api.networking.GridFlags;
|
||||
import appeng.api.networking.events.MENetworkEventSubscribe;
|
||||
import appeng.api.networking.events.MENetworkPowerStatusChange;
|
||||
import appeng.api.util.AECableType;
|
||||
import appeng.api.util.AEPartLocation;
|
||||
import appeng.api.util.DimensionalCoord;
|
||||
import appeng.block.qnb.QnbFormedState;
|
||||
import appeng.me.GridAccessException;
|
||||
import appeng.me.cluster.IAECluster;
|
||||
import appeng.me.cluster.IAEMultiBlock;
|
||||
import appeng.me.cluster.implementations.QuantumCalculator;
|
||||
import appeng.me.cluster.implementations.QuantumCluster;
|
||||
import appeng.tile.grid.AENetworkInvBlockEntity;
|
||||
import appeng.tile.inventory.AppEngInternalInventory;
|
||||
import appeng.util.inv.InvOperation;
|
||||
|
||||
public class QuantumBridgeBlockEntity extends AENetworkInvBlockEntity implements IAEMultiBlock, Tickable {
|
||||
|
||||
private final byte corner = 16;
|
||||
private final AppEngInternalInventory internalInventory = new AppEngInternalInventory(this, 1, 1);
|
||||
private final byte hasSingularity = 32;
|
||||
private final byte powered = 64;
|
||||
|
||||
private final QuantumCalculator calc = new QuantumCalculator(this);
|
||||
private byte constructed = -1;
|
||||
private QuantumCluster cluster;
|
||||
private boolean updateStatus = false;
|
||||
|
||||
public QuantumBridgeBlockEntity(BlockEntityType<?> tileEntityTypeIn) {
|
||||
super(tileEntityTypeIn);
|
||||
this.getProxy().setValidSides(EnumSet.noneOf(Direction.class));
|
||||
this.getProxy().setFlags(GridFlags.DENSE_CAPACITY);
|
||||
this.getProxy().setIdlePowerUsage(22);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tick() {
|
||||
if (this.updateStatus) {
|
||||
this.updateStatus = false;
|
||||
if (this.cluster != null) {
|
||||
this.cluster.updateStatus(true);
|
||||
}
|
||||
this.markForUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void writeToStream(final PacketByteBuf data) throws IOException {
|
||||
super.writeToStream(data);
|
||||
int out = this.constructed;
|
||||
|
||||
if (!this.internalInventory.getInvStack(0).isEmpty() && this.constructed != -1) {
|
||||
out |= this.hasSingularity;
|
||||
}
|
||||
|
||||
if (this.getProxy().isActive() && this.constructed != -1) {
|
||||
out |= this.powered;
|
||||
}
|
||||
|
||||
data.writeByte((byte) out);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean readFromStream(final PacketByteBuf data) throws IOException {
|
||||
final boolean c = super.readFromStream(data);
|
||||
final int oldValue = this.constructed;
|
||||
this.constructed = data.readByte();
|
||||
return this.constructed != oldValue || c;
|
||||
}
|
||||
|
||||
@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 (this.cluster != null) {
|
||||
this.cluster.updateStatus(true);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected FixedItemInv getItemHandlerForSide(Direction side) {
|
||||
if (this.isCenter()) {
|
||||
return this.internalInventory;
|
||||
}
|
||||
return EmptyFixedItemInv.INSTANCE;
|
||||
}
|
||||
|
||||
private boolean isCenter() {
|
||||
return AEApi.instance().definitions().blocks().quantumLink().maybeBlock()
|
||||
.map(link -> getCachedState().getBlock() == link).orElse(false);
|
||||
}
|
||||
|
||||
@MENetworkEventSubscribe
|
||||
public void onPowerStatusChange(final MENetworkPowerStatusChange c) {
|
||||
this.updateStatus = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChunkUnloaded() {
|
||||
this.disconnect(false);
|
||||
super.onChunkUnloaded();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReady() {
|
||||
super.onReady();
|
||||
|
||||
final IBlockDefinition quantumRing = AEApi.instance().definitions().blocks().quantumRing();
|
||||
final Optional<Block> maybeLinkBlock = quantumRing.maybeBlock();
|
||||
final Optional<ItemStack> maybeLinkStack = quantumRing.maybeStack(1);
|
||||
|
||||
final boolean isPresent = maybeLinkBlock.isPresent() && maybeLinkStack.isPresent();
|
||||
|
||||
if (isPresent && getCachedState().getBlock() == maybeLinkBlock.get()) {
|
||||
final ItemStack linkStack = maybeLinkStack.get();
|
||||
|
||||
this.getProxy().setVisualRepresentation(linkStack);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void markRemoved() {
|
||||
this.disconnect(false);
|
||||
super.markRemoved();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disconnect(final boolean affectWorld) {
|
||||
if (this.cluster != null) {
|
||||
if (!affectWorld) {
|
||||
this.cluster.setUpdateStatus(false);
|
||||
}
|
||||
|
||||
this.cluster.destroy();
|
||||
}
|
||||
|
||||
this.cluster = null;
|
||||
|
||||
if (affectWorld) {
|
||||
this.getProxy().setValidSides(EnumSet.noneOf(Direction.class));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public IAECluster getCluster() {
|
||||
return this.cluster;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid() {
|
||||
return !this.isRemoved();
|
||||
}
|
||||
|
||||
public void updateStatus(final QuantumCluster c, final byte flags, final boolean affectWorld) {
|
||||
this.cluster = c;
|
||||
|
||||
if (affectWorld) {
|
||||
if (this.constructed != flags) {
|
||||
this.constructed = flags;
|
||||
this.markForUpdate();
|
||||
}
|
||||
|
||||
if (this.isCorner() || this.isCenter()) {
|
||||
final EnumSet<Direction> sides = EnumSet.noneOf(Direction.class);
|
||||
for (final Direction dir : this.getAdjacentQuantumBridges()) {
|
||||
sides.add(dir);
|
||||
}
|
||||
|
||||
this.getProxy().setValidSides(sides);
|
||||
} else {
|
||||
this.getProxy().setValidSides(EnumSet.allOf(Direction.class));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isCorner() {
|
||||
return (this.constructed & this.getCorner()) == this.getCorner() && this.constructed != -1;
|
||||
}
|
||||
|
||||
public EnumSet<Direction> getAdjacentQuantumBridges() {
|
||||
final EnumSet<Direction> set = EnumSet.noneOf(Direction.class);
|
||||
|
||||
for (final Direction d : Direction.values()) {
|
||||
final BlockEntity te = this.world.getBlockEntity(this.pos.offset(d));
|
||||
if (te instanceof QuantumBridgeBlockEntity) {
|
||||
set.add(d);
|
||||
}
|
||||
}
|
||||
|
||||
return set;
|
||||
}
|
||||
|
||||
public long getQEFrequency() {
|
||||
final ItemStack is = this.internalInventory.getInvStack(0);
|
||||
if (!is.isEmpty()) {
|
||||
final CompoundTag c = is.getTag();
|
||||
if (c != null) {
|
||||
return c.getLong("freq");
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public boolean isPowered() {
|
||||
if (isClient()) {
|
||||
return (this.constructed & this.powered) == this.powered && this.constructed != -1;
|
||||
}
|
||||
|
||||
try {
|
||||
return this.getProxy().getEnergy().isNetworkPowered();
|
||||
} catch (final GridAccessException e) {
|
||||
// :P
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isFormed() {
|
||||
return this.constructed != -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AECableType getCableConnectionType(final AEPartLocation dir) {
|
||||
return AECableType.DENSE_SMART;
|
||||
}
|
||||
|
||||
public void neighborUpdate() {
|
||||
this.calc.calculateMultiblock(this.world, this.getLocation());
|
||||
}
|
||||
|
||||
@Override
|
||||
public DimensionalCoord getLocation() {
|
||||
return new DimensionalCoord(this);
|
||||
}
|
||||
|
||||
public boolean hasQES() {
|
||||
if (this.constructed == -1) {
|
||||
return false;
|
||||
}
|
||||
return (this.constructed & this.hasSingularity) == this.hasSingularity;
|
||||
}
|
||||
|
||||
public void breakCluster() {
|
||||
if (this.cluster != null) {
|
||||
this.cluster.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
public byte getCorner() {
|
||||
return this.corner;
|
||||
}
|
||||
|
||||
@Override
|
||||
public QnbFormedState getRenderAttachmentData() {
|
||||
return new QnbFormedState(getAdjacentQuantumBridges(), isCorner(), isPowered());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,198 @@
|
||||
/*
|
||||
* 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.spatial;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import alexiil.mc.lib.attributes.Simulation;
|
||||
import alexiil.mc.lib.attributes.item.FixedItemInv;
|
||||
import net.minecraft.block.BlockState;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.block.entity.BlockEntityType;
|
||||
import net.minecraft.server.world.ServerWorld;
|
||||
import net.minecraft.util.math.Direction;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import appeng.api.config.Actionable;
|
||||
import appeng.api.config.PowerMultiplier;
|
||||
import appeng.api.config.YesNo;
|
||||
import appeng.api.implementations.TransitionResult;
|
||||
import appeng.api.implementations.items.ISpatialStorageCell;
|
||||
import appeng.api.networking.GridFlags;
|
||||
import appeng.api.networking.IGrid;
|
||||
import appeng.api.networking.energy.IEnergyGrid;
|
||||
import appeng.api.networking.events.MENetworkEvent;
|
||||
import appeng.api.networking.events.MENetworkSpatialEvent;
|
||||
import appeng.api.networking.spatial.ISpatialCache;
|
||||
import appeng.api.util.AECableType;
|
||||
import appeng.api.util.AEPartLocation;
|
||||
import appeng.api.util.DimensionalCoord;
|
||||
import appeng.hooks.TickHandler;
|
||||
import appeng.me.cache.SpatialPylonCache;
|
||||
import appeng.tile.grid.AENetworkInvBlockEntity;
|
||||
import appeng.tile.inventory.AppEngInternalInventory;
|
||||
import appeng.util.IWorldCallable;
|
||||
import appeng.util.Platform;
|
||||
import appeng.util.inv.InvOperation;
|
||||
import appeng.util.inv.WrapperFilteredItemHandler;
|
||||
import appeng.util.inv.filter.IAEItemFilter;
|
||||
|
||||
public class SpatialIOPortBlockEntity extends AENetworkInvBlockEntity implements IWorldCallable<Void> {
|
||||
|
||||
private final AppEngInternalInventory inv = new AppEngInternalInventory(this, 2);
|
||||
private final FixedItemInv invExt = new WrapperFilteredItemHandler(this.inv, new SpatialIOFilter());
|
||||
private YesNo lastRedstoneState = YesNo.UNDECIDED;
|
||||
|
||||
public SpatialIOPortBlockEntity(BlockEntityType<?> tileEntityTypeIn) {
|
||||
super(tileEntityTypeIn);
|
||||
this.getProxy().setFlags(GridFlags.REQUIRE_CHANNEL);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompoundTag toTag(final CompoundTag data) {
|
||||
super.toTag(data);
|
||||
data.putInt("lastRedstoneState", this.lastRedstoneState.ordinal());
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromTag(BlockState state, final CompoundTag data) {
|
||||
super.fromTag(state, data);
|
||||
if (data.contains("lastRedstoneState")) {
|
||||
this.lastRedstoneState = YesNo.values()[data.getInt("lastRedstoneState")];
|
||||
}
|
||||
}
|
||||
|
||||
public boolean getRedstoneState() {
|
||||
if (this.lastRedstoneState == YesNo.UNDECIDED) {
|
||||
this.updateRedstoneState();
|
||||
}
|
||||
|
||||
return this.lastRedstoneState == YesNo.YES;
|
||||
}
|
||||
|
||||
public void updateRedstoneState() {
|
||||
final YesNo currentState = this.world.getReceivedRedstonePower(this.pos) != 0 ? YesNo.YES : YesNo.NO;
|
||||
if (this.lastRedstoneState != currentState) {
|
||||
this.lastRedstoneState = currentState;
|
||||
if (this.lastRedstoneState == YesNo.YES) {
|
||||
this.triggerTransition();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void triggerTransition() {
|
||||
if (Platform.isServer()) {
|
||||
final ItemStack cell = this.inv.getInvStack(0);
|
||||
if (this.isSpatialCell(cell)) {
|
||||
TickHandler.INSTANCE.addCallable(null, this);// this needs to be cross world synced.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isSpatialCell(final ItemStack cell) {
|
||||
if (!cell.isEmpty() && cell.getItem() instanceof ISpatialStorageCell) {
|
||||
final ISpatialStorageCell sc = (ISpatialStorageCell) cell.getItem();
|
||||
return sc != null && sc.isSpatialStorage(cell);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void call(final World world) throws Exception {
|
||||
if (!(world instanceof ServerWorld)) {
|
||||
return null;
|
||||
}
|
||||
ServerWorld serverWorld = (ServerWorld) world;
|
||||
|
||||
final ItemStack cell = this.inv.getInvStack(0);
|
||||
if (this.isSpatialCell(cell) && this.inv.getInvStack(1).isEmpty()) {
|
||||
final IGrid gi = this.getProxy().getGrid();
|
||||
final IEnergyGrid energy = this.getProxy().getEnergy();
|
||||
|
||||
final ISpatialStorageCell sc = (ISpatialStorageCell) cell.getItem();
|
||||
|
||||
final SpatialPylonCache spc = gi.getCache(ISpatialCache.class);
|
||||
if (spc.hasRegion() && spc.isValidRegion()) {
|
||||
final double req = spc.requiredPower();
|
||||
final double pr = energy.extractAEPower(req, Actionable.SIMULATE, PowerMultiplier.CONFIG);
|
||||
if (Math.abs(pr - req) < req * 0.001) {
|
||||
final MENetworkEvent res = gi.postEvent(new MENetworkSpatialEvent(this, req));
|
||||
if (!res.isCanceled()) {
|
||||
int playerId = -1;
|
||||
if (this.getProxy().getSecurity().isAvailable()) {
|
||||
playerId = this.getProxy().getSecurity().getOwner();
|
||||
}
|
||||
|
||||
final TransitionResult tr = sc.doSpatialTransition(cell, serverWorld, spc.getMin(), spc.getMax(),
|
||||
playerId);
|
||||
if (tr.success) {
|
||||
energy.extractAEPower(req, Actionable.MODULATE, PowerMultiplier.CONFIG);
|
||||
this.inv.setInvStack(0, ItemStack.EMPTY, Simulation.ACTION);
|
||||
this.inv.setInvStack(1, cell, Simulation.ACTION);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AECableType getCableConnectionType(final AEPartLocation dir) {
|
||||
return AECableType.SMART;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DimensionalCoord getLocation() {
|
||||
return new DimensionalCoord(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @Nonnull
|
||||
FixedItemInv getItemHandlerForSide(@Nonnull Direction side) {
|
||||
return this.invExt;
|
||||
}
|
||||
|
||||
@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) {
|
||||
|
||||
}
|
||||
|
||||
private class SpatialIOFilter implements IAEItemFilter {
|
||||
@Override
|
||||
public boolean allowExtract(FixedItemInv inv, int slot, int amount) {
|
||||
return slot == 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean allowInsert(FixedItemInv inv, int slot, ItemStack stack) {
|
||||
return (slot == 0 && SpatialIOPortBlockEntity.this.isSpatialCell(stack));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,219 @@
|
||||
/*
|
||||
* 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.spatial;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.EnumSet;
|
||||
|
||||
import net.minecraft.block.entity.BlockEntityType;
|
||||
import net.minecraft.network.PacketByteBuf;
|
||||
import net.minecraft.util.math.Direction;
|
||||
|
||||
import appeng.api.networking.GridFlags;
|
||||
import appeng.api.networking.events.MENetworkChannelsChanged;
|
||||
import appeng.api.networking.events.MENetworkEventSubscribe;
|
||||
import appeng.api.networking.events.MENetworkPowerStatusChange;
|
||||
import appeng.me.GridAccessException;
|
||||
import appeng.me.cluster.IAEMultiBlock;
|
||||
import appeng.me.cluster.implementations.SpatialPylonCalculator;
|
||||
import appeng.me.cluster.implementations.SpatialPylonCluster;
|
||||
import appeng.me.helpers.AENetworkProxy;
|
||||
import appeng.me.helpers.AENetworkProxyMultiblock;
|
||||
import appeng.tile.grid.AENetworkBlockEntity;
|
||||
|
||||
public class SpatialPylonBlockEntity extends AENetworkBlockEntity implements IAEMultiBlock {
|
||||
|
||||
public static final int DISPLAY_END_MIN = 0x01;
|
||||
public static final int DISPLAY_END_MAX = 0x02;
|
||||
public static final int DISPLAY_MIDDLE = 0x01 + 0x02;
|
||||
public static final int DISPLAY_X = 0x04;
|
||||
public static final int DISPLAY_Y = 0x08;
|
||||
public static final int DISPLAY_Z = 0x04 + 0x08;
|
||||
public static final int MB_STATUS = 0x01 + 0x02 + 0x04 + 0x08;
|
||||
|
||||
public static final int DISPLAY_ENABLED = 0x10;
|
||||
public static final int DISPLAY_POWERED_ENABLED = 0x20;
|
||||
public static final int NET_STATUS = 0x10 + 0x20;
|
||||
|
||||
private final SpatialPylonCalculator calc = new SpatialPylonCalculator(this);
|
||||
private int displayBits = 0;
|
||||
private SpatialPylonCluster cluster;
|
||||
private boolean didHaveLight = false;
|
||||
|
||||
public SpatialPylonBlockEntity(BlockEntityType<?> tileEntityTypeIn) {
|
||||
super(tileEntityTypeIn);
|
||||
this.getProxy().setFlags(GridFlags.REQUIRE_CHANNEL, GridFlags.MULTIBLOCK);
|
||||
this.getProxy().setIdlePowerUsage(0.5);
|
||||
this.getProxy().setValidSides(EnumSet.noneOf(Direction.class));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AENetworkProxy createProxy() {
|
||||
return new AENetworkProxyMultiblock(this, "proxy", this.getItemFromTile(this), true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChunkUnloaded() {
|
||||
this.disconnect(false);
|
||||
super.onChunkUnloaded();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReady() {
|
||||
super.onReady();
|
||||
this.neighborUpdate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void markRemoved() {
|
||||
this.disconnect(false);
|
||||
super.markRemoved();
|
||||
}
|
||||
|
||||
public void neighborUpdate() {
|
||||
this.calc.calculateMultiblock(this.world, this.getLocation());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disconnect(final boolean b) {
|
||||
if (this.cluster != null) {
|
||||
this.cluster.destroy();
|
||||
this.updateStatus(null);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public SpatialPylonCluster getCluster() {
|
||||
return this.cluster;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public void updateStatus(final SpatialPylonCluster c) {
|
||||
this.cluster = c;
|
||||
this.getProxy().setValidSides(c == null ? EnumSet.noneOf(Direction.class) : EnumSet.allOf(Direction.class));
|
||||
this.recalculateDisplay();
|
||||
}
|
||||
|
||||
public void recalculateDisplay() {
|
||||
final int oldBits = this.displayBits;
|
||||
|
||||
this.displayBits = 0;
|
||||
|
||||
if (this.cluster != null) {
|
||||
if (this.cluster.getMin().equals(this.getLocation())) {
|
||||
this.displayBits = DISPLAY_END_MIN;
|
||||
} else if (this.cluster.getMax().equals(this.getLocation())) {
|
||||
this.displayBits = DISPLAY_END_MAX;
|
||||
} else {
|
||||
this.displayBits = DISPLAY_MIDDLE;
|
||||
}
|
||||
|
||||
switch (this.cluster.getCurrentAxis()) {
|
||||
case X:
|
||||
this.displayBits |= DISPLAY_X;
|
||||
break;
|
||||
case Y:
|
||||
this.displayBits |= DISPLAY_Y;
|
||||
break;
|
||||
case Z:
|
||||
this.displayBits |= DISPLAY_Z;
|
||||
break;
|
||||
default:
|
||||
this.displayBits = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
try {
|
||||
if (this.getProxy().getEnergy().isNetworkPowered()) {
|
||||
this.displayBits |= DISPLAY_POWERED_ENABLED;
|
||||
}
|
||||
|
||||
if (this.cluster.isValid() && this.getProxy().isActive()) {
|
||||
this.displayBits |= DISPLAY_ENABLED;
|
||||
}
|
||||
} catch (final GridAccessException e) {
|
||||
// nothing?
|
||||
}
|
||||
}
|
||||
|
||||
if (oldBits != this.displayBits) {
|
||||
this.markForUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void markForUpdate() {
|
||||
super.markForUpdate();
|
||||
final boolean hasLight = this.getLightValue() > 0;
|
||||
if (hasLight != this.didHaveLight) {
|
||||
this.didHaveLight = hasLight;
|
||||
this.world.getLightingProvider().checkBlock(this.pos);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canBeRotated() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public int getLightValue() {
|
||||
if ((this.displayBits & DISPLAY_POWERED_ENABLED) == DISPLAY_POWERED_ENABLED) {
|
||||
return 8;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean readFromStream(final PacketByteBuf data) throws IOException {
|
||||
final boolean c = super.readFromStream(data);
|
||||
final int old = this.displayBits;
|
||||
this.displayBits = data.readByte();
|
||||
return old != this.displayBits || c;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void writeToStream(final PacketByteBuf data) throws IOException {
|
||||
super.writeToStream(data);
|
||||
data.writeByte(this.displayBits);
|
||||
}
|
||||
|
||||
@MENetworkEventSubscribe
|
||||
public void powerRender(final MENetworkPowerStatusChange c) {
|
||||
this.recalculateDisplay();
|
||||
}
|
||||
|
||||
@MENetworkEventSubscribe
|
||||
public void activeRender(final MENetworkChannelsChanged c) {
|
||||
this.recalculateDisplay();
|
||||
}
|
||||
|
||||
public int getDisplayBits() {
|
||||
return this.displayBits;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getRenderAttachmentData() {
|
||||
return getDisplayBits();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -21,6 +21,7 @@ package appeng.tile.storage;
|
||||
import alexiil.mc.lib.attributes.AttributeList;
|
||||
import alexiil.mc.lib.attributes.Simulation;
|
||||
import alexiil.mc.lib.attributes.fluid.FluidInsertable;
|
||||
import alexiil.mc.lib.attributes.fluid.FluidVolumeUtil;
|
||||
import alexiil.mc.lib.attributes.fluid.amount.FluidAmount;
|
||||
import alexiil.mc.lib.attributes.fluid.filter.ConstantFluidFilter;
|
||||
import alexiil.mc.lib.attributes.fluid.filter.FluidFilter;
|
||||
@@ -50,6 +51,7 @@ import appeng.api.storage.data.IAEItemStack;
|
||||
import appeng.api.storage.data.IAEStack;
|
||||
import appeng.api.util.AEColor;
|
||||
import appeng.api.util.IConfigManager;
|
||||
import appeng.container.implementations.MEMonitorableContainer;
|
||||
import appeng.fluids.util.AEFluidStack;
|
||||
import appeng.helpers.IPriorityHost;
|
||||
import appeng.me.GridAccessException;
|
||||
@@ -703,6 +705,9 @@ public class ChestBlockEntity extends AENetworkPowerBlockEntity
|
||||
|
||||
FluidAmount filledAmt = remaining != null ? remaining.getAmount() : toInsert.getAmount();
|
||||
FluidAmount remainingAmt = fluidVolume.amount().roundedSub(filledAmt, RoundingMode.DOWN);
|
||||
if (remainingAmt.isZero()) {
|
||||
return FluidVolumeUtil.EMPTY;
|
||||
}
|
||||
return fluidVolume.withAmount(remainingAmt);
|
||||
}
|
||||
|
||||
@@ -756,8 +761,7 @@ public class ChestBlockEntity extends AENetworkPowerBlockEntity
|
||||
if (this.cellHandler != null) {
|
||||
if (this.cellHandler.getChannel() == AEApi.instance().storage()
|
||||
.getStorageChannel(IItemStorageChannel.class)) {
|
||||
throw new IllegalStateException();
|
||||
// FIXME FABRIC return MEMonitorableContainer.TYPE;
|
||||
return MEMonitorableContainer.TYPE;
|
||||
}
|
||||
if (this.cellHandler.getChannel() == AEApi.instance().storage()
|
||||
.getStorageChannel(IFluidStorageChannel.class)) {
|
||||
|
||||
@@ -0,0 +1,451 @@
|
||||
/*
|
||||
* 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.Identifier;
|
||||
|
||||
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;
|
||||
import net.minecraft.util.math.Direction;
|
||||
import net.minecraft.util.registry.Registry;
|
||||
|
||||
public class DriveBlockEntity extends AENetworkInvBlockEntity implements IChestOrDrive, IPriorityHost {
|
||||
|
||||
private static final int BIT_POWER_MASK = Integer.MIN_VALUE;
|
||||
private static final int BIT_STATE_MASK = 0b111111111111111111111111111111;
|
||||
|
||||
private static final int BIT_CELL_STATE_MASK = 0b111;
|
||||
private static final int BIT_CELL_STATE_BITS = 3;
|
||||
|
||||
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: reserved
|
||||
*
|
||||
* - Bit 29-0: 3 bits for the state of each cell
|
||||
*
|
||||
* Cell states:
|
||||
*
|
||||
* - Bit 2-0: {@link CellState} ordinal
|
||||
*
|
||||
*
|
||||
*
|
||||
*/
|
||||
private int state = 0;
|
||||
|
||||
public DriveBlockEntity(BlockEntityType<?> tileEntityTypeIn) {
|
||||
super(tileEntityTypeIn);
|
||||
this.mySrc = new MachineSource(this);
|
||||
this.getProxy().setFlags(GridFlags.REQUIRE_CHANNEL);
|
||||
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++) {
|
||||
final int o = this.getCellStatus(x).ordinal();
|
||||
final int i = (o << (BIT_CELL_STATE_BITS * x));
|
||||
newState |= i;
|
||||
}
|
||||
|
||||
data.writeInt(newState);
|
||||
|
||||
writeCellItemIds(data);
|
||||
}
|
||||
|
||||
private void writeCellItemIds(PacketByteBuf data) {
|
||||
List<Integer> cellItemIds = new ArrayList<>(getCellCount());
|
||||
byte[] bm = new byte[getCellCount()];
|
||||
for (int x = 0; x < this.getCellCount(); x++) {
|
||||
Item item = getCellItem(x);
|
||||
if (item != null) {
|
||||
int itemId = Item.getRawId(item);
|
||||
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 (int itemId : cellItemIds) {
|
||||
data.writeVarInt(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 uniqueIdCount = data.readByte();
|
||||
int[] uniqueIds = new int[uniqueIdCount];
|
||||
for (int i = 0; i < uniqueIdCount; i++) {
|
||||
uniqueIds[i] = data.readVarInt();
|
||||
}
|
||||
|
||||
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;
|
||||
int itemId = uniqueIds[idx];
|
||||
item = Item.byRawId(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()) {
|
||||
final int cellState = ((this.state >> (slot * BIT_CELL_STATE_BITS)) & BIT_CELL_STATE_MASK);
|
||||
return CellState.values()[cellState];
|
||||
}
|
||||
|
||||
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 false;
|
||||
}
|
||||
|
||||
@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() << (BIT_CELL_STATE_BITS * 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.recalculateDisplay();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveChanges(final ICellInventory<?> cellInventory) {
|
||||
this.world.markDirty(this.pos, this);
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,449 @@
|
||||
/*
|
||||
* 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.Simulation;
|
||||
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, Simulation.ACTION);
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user