Data container test, add graphics to android station

This commit is contained in:
DBotThePony 2021-08-09 19:31:36 +07:00
parent 42044ff74a
commit e5f0e563a4
Signed by: DBot
GPG Key ID: DCC23B5715498507
10 changed files with 192 additions and 25 deletions

View File

@ -31,7 +31,7 @@ import java.util.List;
public class BlockEntityAndroidStation extends BlockEntity implements MenuProvider {
@Override
public Component getDisplayName() {
return new TranslatableComponent("container.android_station");
return new TranslatableComponent("container.otm.android_station");
}
@Nullable
@ -40,7 +40,7 @@ public class BlockEntityAndroidStation extends BlockEntity implements MenuProvid
return new AndroidStationMenu(containerID, inventory, new AndroidStationMenu.AndroidStationContainer(ply), this);
}
private final MatteryMachineEnergyStorage energy = new MatteryMachineEnergyStorage(MatteryMachineEnergyStorage.MachineType.WORKER);
private final MatteryMachineEnergyStorage energy = new MatteryMachineEnergyStorage(MatteryMachineEnergyStorage.MachineType.WORKER, new BigDecimal(1_000_000_000_000L));
public BlockEntityAndroidStation(BlockPos p_155229_, BlockState p_155230_) {
super(OverdriveThatMatters.ANDROID_STATION_FACTORY, p_155229_, p_155230_);

View File

@ -123,7 +123,7 @@ public class AndroidCapability implements ICapabilityProvider, IAndroidCapabilit
}
BigDecimal new_energy = energy_stored.subtract(howMuch, MatteryCapability.ROUND_RULES).max(BigDecimal.ZERO);
BigDecimal diff = energy_stored.subtract(new_energy);
BigDecimal diff = energy_stored.subtract(new_energy, MatteryCapability.ROUND_RULES);
if (!simulate) {
energy_stored = new_energy;
@ -153,7 +153,7 @@ public class AndroidCapability implements ICapabilityProvider, IAndroidCapabilit
}
BigDecimal new_energy = energy_stored.add(howMuch, MatteryCapability.ROUND_RULES).min(energy_stored_max);
BigDecimal diff = new_energy.subtract(energy_stored);
BigDecimal diff = new_energy.subtract(energy_stored, MatteryCapability.ROUND_RULES);
if (!simulate) {
energy_stored = new_energy;

View File

@ -27,42 +27,42 @@ public interface IMatteryEnergyStorage extends IEnergyStorage {
@Nonnull
default BigDecimal extractEnergyOuter(long howMuch, boolean simulate) {
return extractEnergyOuter(new BigDecimal(howMuch), simulate);
return extractEnergyOuter(new BigDecimal(howMuch, MatteryCapability.ROUND_RULES), simulate);
}
@Nonnull
default BigDecimal extractEnergyOuter(int howMuch, boolean simulate) {
return extractEnergyOuter(new BigDecimal(howMuch), simulate);
return extractEnergyOuter(new BigDecimal(howMuch, MatteryCapability.ROUND_RULES), simulate);
}
@Nonnull
default BigDecimal receiveEnergyOuter(long howMuch, boolean simulate) {
return receiveEnergyOuter(new BigDecimal(howMuch), simulate);
return receiveEnergyOuter(new BigDecimal(howMuch, MatteryCapability.ROUND_RULES), simulate);
}
@Nonnull
default BigDecimal receiveEnergyOuter(int howMuch, boolean simulate) {
return receiveEnergyOuter(new BigDecimal(howMuch), simulate);
return receiveEnergyOuter(new BigDecimal(howMuch, MatteryCapability.ROUND_RULES), simulate);
}
@Nonnull
default BigDecimal extractEnergyInner(long howMuch, boolean simulate) {
return receiveEnergyOuter(new BigDecimal(howMuch), simulate);
return receiveEnergyOuter(new BigDecimal(howMuch, MatteryCapability.ROUND_RULES), simulate);
}
@Nonnull
default BigDecimal extractEnergyInner(int howMuch, boolean simulate) {
return receiveEnergyOuter(new BigDecimal(howMuch), simulate);
return receiveEnergyOuter(new BigDecimal(howMuch, MatteryCapability.ROUND_RULES), simulate);
}
@Nonnull
default BigDecimal receiveEnergyInner(long howMuch, boolean simulate) {
return receiveEnergyOuter(new BigDecimal(howMuch), simulate);
return receiveEnergyOuter(new BigDecimal(howMuch, MatteryCapability.ROUND_RULES), simulate);
}
@Nonnull
default BigDecimal receiveEnergyInner(int howMuch, boolean simulate) {
return receiveEnergyOuter(new BigDecimal(howMuch), simulate);
return receiveEnergyOuter(new BigDecimal(howMuch, MatteryCapability.ROUND_RULES), simulate);
}
@Nonnull
@ -77,7 +77,7 @@ public interface IMatteryEnergyStorage extends IEnergyStorage {
@Override
default int receiveEnergy(int maxReceive, boolean simulate) {
BigDecimal toReceive = new BigDecimal(maxReceive);
BigDecimal toReceive = new BigDecimal(maxReceive, MatteryCapability.ROUND_RULES);
int received = receiveEnergyOuter(toReceive, true).intValue();
if (received == 0) {
@ -85,12 +85,12 @@ public interface IMatteryEnergyStorage extends IEnergyStorage {
return 0;
}
return receiveEnergyOuter(new BigDecimal(received), simulate).intValue();
return receiveEnergyOuter(new BigDecimal(received, MatteryCapability.ROUND_RULES), simulate).intValue();
}
@Override
default int extractEnergy(int maxReceive, boolean simulate) {
BigDecimal toReceive = new BigDecimal(maxReceive);
BigDecimal toReceive = new BigDecimal(maxReceive, MatteryCapability.ROUND_RULES);
int extracted = extractEnergyOuter(toReceive, true).intValue();
if (extracted == 0) {
@ -98,7 +98,7 @@ public interface IMatteryEnergyStorage extends IEnergyStorage {
return 0;
}
return extractEnergyOuter(new BigDecimal(extracted), simulate).intValue();
return extractEnergyOuter(new BigDecimal(extracted, MatteryCapability.ROUND_RULES), simulate).intValue();
}
@Override

View File

@ -14,7 +14,7 @@ public class MatteryCapability {
@CapabilityInject(IAndroidCapability.class)
public static Capability<IAndroidCapability> ANDROID = null;
public static final MathContext ROUND_RULES = new MathContext(0, RoundingMode.HALF_UP);
public static final MathContext ROUND_RULES = new MathContext(16, RoundingMode.HALF_UP);
public static final BigDecimal INT_MAX_VALUE = new BigDecimal(Integer.MAX_VALUE);
public static final BigDecimal INT_MIN_VALUE = new BigDecimal(Integer.MIN_VALUE);

View File

@ -23,15 +23,15 @@ public class MatteryMachineEnergyStorage implements IMatteryEnergyStorage, INBTS
protected BigDecimal max_output;
protected final MachineType machine_type;
public MatteryMachineEnergyStorage(BigDecimal capacity, MachineType type) {
this(capacity, type, DEFAULT_MAX_RECEIVE, DEFAULT_MAX_EXTRACT);
public MatteryMachineEnergyStorage(MachineType type, BigDecimal capacity) {
this(type, capacity, DEFAULT_MAX_RECEIVE, DEFAULT_MAX_EXTRACT);
}
public MatteryMachineEnergyStorage(MachineType type) {
this(DEFAULT_MAX_CAPACITY, type);
this(type, DEFAULT_MAX_CAPACITY);
}
public MatteryMachineEnergyStorage(BigDecimal capacity, MachineType type, BigDecimal maxReceive, BigDecimal maxExtract) {
public MatteryMachineEnergyStorage(MachineType type, BigDecimal capacity, BigDecimal maxReceive, BigDecimal maxExtract) {
energy_stored_max = capacity;
max_input = maxReceive;
max_output = maxExtract;

View File

@ -14,7 +14,9 @@ import net.minecraftforge.energy.IEnergyStorage;
import ru.dbotthepony.mc.otm.OverdriveThatMatters;
import ru.dbotthepony.mc.otm.block.entity.BlockEntityAndroidStation;
import ru.dbotthepony.mc.otm.capability.IAndroidCapability;
import ru.dbotthepony.mc.otm.capability.IMatteryEnergyStorage;
import ru.dbotthepony.mc.otm.capability.MatteryCapability;
import ru.dbotthepony.mc.otm.menu.data.BigDecimalDataContainer;
import javax.annotation.Nullable;
@ -61,6 +63,8 @@ public class AndroidStationMenu extends AbstractContainerMenu {
private Inventory inventory;
private Player ply;
private AndroidStationContainer container;
public BigDecimalDataContainer energy;
public BigDecimalDataContainer max_energy;
// Clientside
public AndroidStationMenu(int containerID, Inventory inventory) {
@ -75,17 +79,45 @@ public class AndroidStationMenu extends AbstractContainerMenu {
this.tile = tile;
this.ply = inventory.player;
this.addSlot(new AndroidBatterySlot(this.container, 0, 0, 0));
if (tile == null) {
energy = new BigDecimalDataContainer();
max_energy = new BigDecimalDataContainer();
} else {
energy = new BigDecimalDataContainer() {
@Override
protected void updateValue() {
LazyOptional<IMatteryEnergyStorage> capability = tile.getCapability(MatteryCapability.ENERGY);
if (capability.isPresent())
setDecimal(capability.resolve().get().getBatteryLevel());
}
};
max_energy = new BigDecimalDataContainer() {
@Override
protected void updateValue() {
LazyOptional<IMatteryEnergyStorage> capability = tile.getCapability(MatteryCapability.ENERGY);
if (capability.isPresent())
setDecimal(capability.resolve().get().getMaxBatteryLevel());
}
};
}
this.addSlot(new AndroidBatterySlot(this.container, 0, 39, 15));
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 9; ++j) {
this.addSlot(new Slot(inventory, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
this.addSlot(new Slot(inventory, j + i * 9 + 9, 8 + j * 18, 97 + i * 18));
}
}
for (int k = 0; k < 9; ++k) {
this.addSlot(new Slot(inventory, k, 8 + k * 18, 142));
this.addSlot(new Slot(inventory, k, 8 + k * 18, 155));
}
this.addDataSlots(energy);
this.addDataSlots(max_energy);
}
@Override

View File

@ -0,0 +1,101 @@
package ru.dbotthepony.mc.otm.menu.data;
import net.minecraft.world.inventory.ContainerData;
import java.math.BigDecimal;
import java.math.BigInteger;
public class BigDecimalDataContainer implements ContainerData {
public static final int NETWORK_PAYLOAD_SIZE = 8;
private BigDecimal value;
private int[] buffer = new int[NETWORK_PAYLOAD_SIZE + 1];
public BigDecimal getDecimal() {
if (value != null) {
return value;
}
byte[] _build = new byte[NETWORK_PAYLOAD_SIZE * 4];
int build_index = 0;
// read payload
for (int i = 1; i <= NETWORK_PAYLOAD_SIZE; i++) {
_build[build_index] = (byte) ((buffer[i] & 0xFF000000) >> 24);
_build[build_index + 1] = (byte) ((buffer[i] & 0xFF0000) >> 16);
_build[build_index + 2] = (byte) ((buffer[i] & 0xFF00) >> 8);
_build[build_index + 3] = (byte) (buffer[i] & 0xFF);
build_index += 4;
}
byte[] build = new byte[_build[0] & 0xFF];
for (int i = 0; i < build.length; i++) {
build[i] = _build[i + 1];
}
value = new BigDecimal(new BigInteger(build), buffer[0]);
return value;
}
public void setDecimal(BigDecimal decimal) {
if (decimal.equals(value)) {
return;
}
value = decimal;
int scale = decimal.scale();
BigInteger integer = decimal.unscaledValue();
buffer[0] = scale;
byte[] _build = integer.toByteArray();
byte[] build = new byte[NETWORK_PAYLOAD_SIZE * 4];
// insert, shift by one
for (int i = 0; i < Math.min(_build.length, NETWORK_PAYLOAD_SIZE * 4 - 1); i++)
build[i + 1] = _build[i];
// tell how many bytes are in there
build[0] = (byte) _build.length;
int build_index = 0;
// write
for (int i = 1; i <= NETWORK_PAYLOAD_SIZE; i++) {
buffer[i] = (build[build_index] & 0xFF) << 24 |
(build[build_index + 1] & 0xFF) << 16 |
(build[build_index + 2] & 0xFF) << 8 |
(build[build_index + 3] & 0xFF);
build_index += 4;
}
}
// override
protected void updateValue() {
}
@Override
public int get(int index) {
updateValue();
return buffer[index];
}
@Override
public void set(int index, int _value) {
if (buffer[index] == _value)
return;
value = null;
buffer[index] = _value;
}
@Override
public int getCount() {
return NETWORK_PAYLOAD_SIZE + 1;
}
}

View File

@ -7,11 +7,25 @@ import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
import ru.dbotthepony.mc.otm.OverdriveThatMatters;
import ru.dbotthepony.mc.otm.capability.MatteryCapability;
import ru.dbotthepony.mc.otm.menu.AndroidStationMenu;
import java.math.BigDecimal;
public class AndroidStationScreen extends AbstractContainerScreen<AndroidStationMenu> {
private static final ResourceLocation CONTAINER_BACKGROUND = new ResourceLocation(OverdriveThatMatters.MOD_ID, "textures/gui/android_station.png");
private final AndroidStationMenu station;
public AndroidStationScreen(AndroidStationMenu p_97741_, Inventory p_97742_, Component p_97743_) {
super(p_97741_, p_97742_, p_97743_);
station = p_97741_;
imageWidth = 176;
imageHeight = 178;
titleLabelY = 4;
inventoryLabelY = imageHeight - 92;
}
@Override
@ -22,7 +36,27 @@ public class AndroidStationScreen extends AbstractContainerScreen<AndroidStation
}
@Override
protected void renderBg(PoseStack p_97787_, float p_97788_, int p_97789_, int p_97790_) {
protected void renderBg(PoseStack pose, float p_97788_, int p_97789_, int p_97790_) {
RenderSystem.setShader(GameRenderer::getPositionTexShader);
RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F);
RenderSystem.setShaderTexture(0, CONTAINER_BACKGROUND);
int i = (width - imageWidth) / 2;
int j = (height - imageHeight) / 2;
// PoseStack, x, y, image_start_x, image_start_y, rect_size_x, rect_size_y
this.blit(pose, i, j, 0, 0, this.imageWidth, this.imageHeight);
BigDecimal energy = station.energy.getDecimal();
BigDecimal max_energy = station.max_energy.getDecimal();
double level;
if (max_energy.equals(BigDecimal.ZERO)) {
level = 0d;
} else {
level = energy.divide(max_energy, MatteryCapability.ROUND_RULES).doubleValue();
}
if (level > 0.01)
this.blit(pose, i + 13, j + 15 + 46 - (int) (level * 45d), 176, 0, 7, (int) (level * 45d));
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB