Переполнение EnergyStorage

Версия Minecraft
1.7.10
API
Forge
36
1
5
Привет!
Для своего механизма добавил энергию:
Java:
import cofh.api.energy.EnergyStorage;
...
public EnergyStorage storage = new EnergyStorage(200000);
Столкнулся с проблемой, что когда в механизм попадает более 32 тысячи энергии, счетчик энергии в GUI начинается заново:
1659958956774.png 1659959023300.png

Подскажите пожалуйста, как отображать энергию полностью?
 
Последнее редактирование:
Решение
sendProgressBarUpdate() использует тип short для передачи, потому число "обрезается". Простой выход который обычно используют – передача больших чисел двумя такими пакетами. С помощью битовых операций число делится на части, передаётся и затем объединяется. Пример реализации смотрите в GUI других подобных мезанизмов.
36
1
5
Модифицирую код из Ex Astris:
Java:
public class ContainerHammerAutomatic extends Container {

    public TileEntityHammerAutomatic hammer;
    private int lastEnergy;

    public ContainerHammerAutomatic(InventoryPlayer invPlayer, TileEntityHammerAutomatic entity) {
        hammer = entity;
        this.addSlotToContainer(new SlotHammerAutomatic(hammer, 0, 8, 35));

        int i;

        for (i = 0; i < 4; i++) {
            for (int j = 0; j < 5; j++) {
                this.addSlotToContainer(new SlotClosed(hammer, 1 + (i * 5) + j, 57 + (j * 18), 8 + (i * 18)));
            }
        }

        for (i = 0; i < 3; i++) {
            for (int j = 0; j < 9; j++) {
                this.addSlotToContainer(new Slot(invPlayer, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
            }
        }


        for (i = 0; i < 9; i++) {
            this.addSlotToContainer(new Slot(invPlayer, i, 8 + i * 18, 142));
        }

        this.addSlotToContainer(new SlotHammerAutomatic(hammer, 21, 8, 62));
        this.addSlotToContainer(new SlotHammerAutomatic(hammer, 22, 33, 62));
    }

    @Override
    public boolean canInteractWith(EntityPlayer player)
    {
        return hammer.isUseableByPlayer(player);
    }

    @Override
    public ItemStack transferStackInSlot(EntityPlayer player, int slotNum) {
        ItemStack stack = null;
        Slot slot = this.getSlot(slotNum);
        if (slot != null & slot.getHasStack()) {
            ItemStack stackInSlot = slot.getStack();
            stack = stackInSlot.copy();
            if (slotNum <= 20 || slotNum == 57 || slotNum == 58) {
                if (!this.mergeItemStack(stackInSlot, 21, 57, true))
                    return null;
            }
            else if (this.hammer.canInsertItem(0, stackInSlot, 0)) {
                if (!this.mergeItemStack(stackInSlot, 0, 1, false))
                    return null;
            }
            else if (this.hammer.canInsertItem(21, stackInSlot, 0)) {
                if (!this.mergeItemStack(stackInSlot, 57, 58, false))
                    return null;
            }

            else if (this.hammer.canInsertItem(22, stackInSlot, 0)) {
                if (!this.mergeItemStack(stackInSlot, 58, 59, false))
                    return null;
            }

            if (stackInSlot.stackSize == 0) {
                slot.putStack(null);
            } else {
                slot.onSlotChanged();
            }

            if (stackInSlot.stackSize == stack.stackSize) {
                return null;
            }
            slot.onPickupFromSlot(player, stackInSlot);
        }

        return stack;
    }

    @Override
    public void detectAndSendChanges() {
            super.detectAndSendChanges();

            for (int i = 0; i < this.crafters.size(); i++) {
                ICrafting icrafting = (ICrafting) this.crafters.get(i);
                if (this.lastEnergy != hammer.getEnergyStored(null)) {
                    icrafting.sendProgressBarUpdate(this, 0, hammer.getEnergyStored(null));
                }
            }
            this.lastEnergy = hammer.getEnergyStored(null);
    }

    @Override
    public void updateProgressBar(int par1, int par2) {
        hammer.setEnergyStored(par2);
    }

}

Java:
public class GuiHammerAutomatic extends GuiContainer {

    public static final ResourceLocation texture = new ResourceLocation(VictoryExAddonConfig.MODID, "textures/gui/sieveautomatic.png");
    public TileEntityHammerAutomatic hammer;
    
    public GuiHammerAutomatic(InventoryPlayer invPlayer, TileEntityHammerAutomatic entity) {
        super(new ContainerHammerAutomatic(invPlayer, entity));
        hammer = entity;
        xSize = 176;
        ySize = 166;
    }
    
    @Override
    protected void drawGuiContainerBackgroundLayer(float p_146976_1_, int p_146976_2_, int p_146976_3_) {
        GL11.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
        this.mc.getTextureManager().bindTexture(texture);
        int k = (this.width - this.xSize) / 2;
        int l = (this.height - this.ySize) / 2;
        this.drawTexturedModalRect(k, l, 0, 0, this.xSize, this.ySize);
        if (this.hammer.mode != TileEntityHammerAutomatic.HammerMode.EMPTY) {
            int i1 = 15 - (int)(this.hammer.getVolume() * 15.0);
            this.drawTexturedModalRect(k + 32, l + 36, 176, 0, i1, 15);
        }
        int i1 = (int)(this.hammer.storage.getEnergyStored() / (float)this.hammer.storage.getMaxEnergyStored() * 70.0);
        this.drawTexturedModalRect(k + 152, l + 8 + (70 - i1), 191, 0, 16, i1);
    }

    @Override
    protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) {
        int x = (this.width - this.xSize) / 2 + 152;
        int y = (this.height - this.ySize) / 2;

        if (mouseX > x && mouseX < x + 16 && mouseY < y + 69 && mouseY > y - 51) {
            List<String> lines = new ArrayList<String>();
            lines.add(this.hammer.getEnergyStored(null) + " RF");
            lines.add("Consuming "+this.hammer.getEffectiveEnergy()+ "RF/t");
            this.func_146283_a(lines, mouseX - x + 152, mouseY - y + 10);
        }
    }

}
 
1,074
72
372
sendProgressBarUpdate() использует тип short для передачи, потому число "обрезается". Простой выход который обычно используют – передача больших чисел двумя такими пакетами. С помощью битовых операций число делится на части, передаётся и затем объединяется. Пример реализации смотрите в GUI других подобных мезанизмов.
 
  • Like
Реакции: ghas
Сверху