Refactored drive state sync

Removed blinking state for now
This commit is contained in:
yueh
2020-06-29 11:45:25 +02:00
parent 400fcdb633
commit 77b544ce4f
8 changed files with 89 additions and 38 deletions
@@ -37,6 +37,9 @@ public enum DriveSlotState implements IStringSerializable {
// Online and free space
ONLINE("online"),
// Online and not space
NOT_EMPTY("not_empty"),
// Types full, space left
TYPES_FULL("types_full"),
@@ -60,8 +63,9 @@ public enum DriveSlotState implements IStringSerializable {
case ABSENT:
return DriveSlotState.EMPTY;
case EMPTY:
case NOT_EMPTY:
return DriveSlotState.ONLINE;
case NOT_EMPTY:
return DriveSlotState.NOT_EMPTY;
case TYPES_FULL:
return DriveSlotState.TYPES_FULL;
case FULL:
@@ -43,7 +43,8 @@ public class DriveLedTileEntityRenderer extends TileEntityRenderer<DriveTileEnti
STATE_COLORS = new EnumMap<>(DriveSlotState.class);
STATE_COLORS.put(DriveSlotState.OFFLINE, new Vector3f(0, 0, 0));
STATE_COLORS.put(DriveSlotState.ONLINE, new Vector3f(0, 1, 0));
STATE_COLORS.put(DriveSlotState.TYPES_FULL, new Vector3f(0, 0.667f, 0));
STATE_COLORS.put(DriveSlotState.NOT_EMPTY, new Vector3f(0f, 0.667f, 1));
STATE_COLORS.put(DriveSlotState.TYPES_FULL, new Vector3f(1, 0.667f, 0));
STATE_COLORS.put(DriveSlotState.FULL, new Vector3f(1, 0, 0));
}
@@ -110,6 +110,13 @@ import appeng.util.item.AEItemStack;
public class ChestTileEntity extends AENetworkPowerTileEntity
implements IMEChest, ITerminalHost, IPriorityHost, IConfigManagerHost, IColorableTile, ITickableTileEntity {
private static final int BIT_POWER_MASK = Byte.MIN_VALUE;
private static final int BIT_STATE_MASK = 0b111;
private static final int BIT_CELL_STATE_MASK = 0b111;
private static final int BIT_CELL_STATE_BITS = 3;
private final AppEngInternalInventory inputInventory = new AppEngInternalInventory(this, 1);
private final AppEngInternalInventory cellInventory = new AppEngInternalInventory(this, 1);
private final IItemHandler internalInventory = new WrapperChainedItemHandler(this.inputInventory,
@@ -163,13 +170,13 @@ public class ChestTileEntity extends AENetworkPowerTileEntity
final int oldState = this.state;
for (int x = 0; x < this.getCellCount(); x++) {
this.state |= (this.getCellStatus(x).ordinal() << (3 * x));
this.state |= (this.getCellStatus(x).ordinal() << (BIT_CELL_STATE_BITS * x));
}
if (this.isPowered()) {
this.state |= 0x40;
this.state |= BIT_POWER_MASK;
} else {
this.state &= ~0x40;
this.state &= ~BIT_POWER_MASK;
}
final boolean currentActive = this.getProxy().isActive();
@@ -245,7 +252,7 @@ public class ChestTileEntity extends AENetworkPowerTileEntity
@Override
public CellState getCellStatus(final int slot) {
if (isRemote()) {
return CellState.values()[(this.state >> (slot * 3)) & 3];
return CellState.values()[(this.state >> (slot * BIT_CELL_STATE_BITS)) & BIT_CELL_STATE_MASK];
}
this.updateHandler();
@@ -273,7 +280,7 @@ public class ChestTileEntity extends AENetworkPowerTileEntity
@Override
public boolean isPowered() {
if (isRemote()) {
return (this.state & 0x40) == 0x40;
return (this.state & BIT_POWER_MASK) == BIT_POWER_MASK;
}
boolean gridPowered = this.getAECurrentPower() > 64;
@@ -290,12 +297,7 @@ public class ChestTileEntity extends AENetworkPowerTileEntity
@Override
public boolean isCellBlinking(final int slot) {
final long now = this.world.getGameTime();
if (now - this.lastStateChange > 8) {
return false;
}
return ((this.state >> (slot * 3 + 2)) & 0x01) == 0x01;
return false;
}
@Override
@@ -327,14 +329,14 @@ public class ChestTileEntity extends AENetworkPowerTileEntity
try {
if (!this.getProxy().getEnergy().isNetworkPowered()) {
final double powerUsed = this.extractAEPower(idleUsage, Actionable.MODULATE, PowerMultiplier.CONFIG); // drain
if (powerUsed + 0.1 >= idleUsage != (this.state & 0x40) > 0) {
if (powerUsed + 0.1 >= idleUsage != (this.state & BIT_POWER_MASK) > 0) {
this.recalculateDisplay();
}
}
} catch (final GridAccessException e) {
final double powerUsed = this.extractAEPower(this.getProxy().getIdlePowerUsage(), Actionable.MODULATE,
PowerMultiplier.CONFIG); // drain
if (powerUsed + 0.1 >= idleUsage != (this.state & 0x40) > 0) {
if (powerUsed + 0.1 >= idleUsage != (this.state & BIT_POWER_MASK) > 0) {
this.recalculateDisplay();
}
}
@@ -348,20 +350,16 @@ public class ChestTileEntity extends AENetworkPowerTileEntity
protected void writeToStream(final PacketBuffer data) throws IOException {
super.writeToStream(data);
if (this.world.getGameTime() - this.lastStateChange > 8) {
this.state = 0;
} else {
this.state &= 0x24924924; // just keep the blinks...
}
this.state = 0;
for (int x = 0; x < this.getCellCount(); x++) {
this.state |= (this.getCellStatus(x).ordinal() << (3 * x));
}
if (this.isPowered()) {
this.state |= 0x40;
this.state |= BIT_POWER_MASK;
} else {
this.state &= ~0x40;
this.state &= ~BIT_POWER_MASK;
}
data.writeByte(this.state);
@@ -519,7 +517,7 @@ public class ChestTileEntity extends AENetworkPowerTileEntity
}
this.lastStateChange = now;
this.state |= 1 << (slot * 3 + 2);
this.state |= 1 << (slot * BIT_CELL_STATE_BITS + 2);
this.recalculateDisplay();
}
@@ -77,9 +77,11 @@ import appeng.util.inv.filter.IAEItemFilter;
public class DriveTileEntity extends AENetworkInvTileEntity implements IChestOrDrive, IPriorityHost {
private static final int BIT_POWER_MASK = 0x80000000;
private static final int BIT_BLINK_MASK = 0x24924924;
private static final int BIT_STATE_MASK = 0xDB6DB6DB;
private 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];
@@ -95,13 +97,18 @@ public class DriveTileEntity extends AENetworkInvTileEntity implements IChestOrD
/**
* The state of all cells inside a drive as bitset, using the following format.
*
* Bit 31: power state. 0 = off, 1 = on. Bit 30: undefined Bit 29-0: 3 bits as
* state of each cell with the cell in slot 0 located in the 3 least significant
* bits.
* - 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: blink. 0 = off, 1 = on. Bit 1-0: cell status
* Cell states:
*
* - Bit 2-0: {@link CellState} ordinal
*
*
*
*/
private int state = 0;
@@ -128,8 +135,11 @@ public class DriveTileEntity extends AENetworkInvTileEntity implements IChestOrD
newState |= BIT_POWER_MASK;
}
for (int x = 0; x < this.getCellCount(); x++) {
newState |= (this.getCellStatus(x).ordinal() << (3 * x));
final int o = this.getCellStatus(x).ordinal();
final int i = (o << (BIT_CELL_STATE_BITS * x));
newState |= i;
}
data.writeInt(newState);
writeCellItemIds(data);
@@ -224,7 +234,8 @@ public class DriveTileEntity extends AENetworkInvTileEntity implements IChestOrD
@Override
public CellState getCellStatus(final int slot) {
if (Platform.isClient()) {
return CellState.values()[(this.state >> (slot * 3)) & 3];
final int cellState = ((this.state >> (slot * BIT_CELL_STATE_BITS)) & BIT_CELL_STATE_MASK);
return CellState.values()[cellState];
}
final DriveWatcher handler = this.invBySlot[slot];
@@ -246,7 +257,7 @@ public class DriveTileEntity extends AENetworkInvTileEntity implements IChestOrD
@Override
public boolean isCellBlinking(final int slot) {
return ((this.state >> (slot * 3 + 2)) & 0x01) == 0x01;
return false;
}
@Override
@@ -286,7 +297,7 @@ public class DriveTileEntity extends AENetworkInvTileEntity implements IChestOrD
}
for (int x = 0; x < this.getCellCount(); x++) {
newState |= (this.getCellStatus(x).ordinal() << (3 * x));
newState |= (this.getCellStatus(x).ordinal() << (BIT_CELL_STATE_BITS * x));
}
if (newState != this.state) {
@@ -416,8 +427,6 @@ public class DriveTileEntity extends AENetworkInvTileEntity implements IChestOrD
@Override
public void blinkCell(final int slot) {
this.state |= 1 << (slot * 3 + 2);
this.recalculateDisplay();
}