Jump to content

[1.11.2] NBT is not read correctly


Minebot1708

Recommended Posts

There is a switch in the GUI of the block. When switching, a packet is sent to the server with the switch values and the block position. When the server receives the packet, it sets the value in TileEntity by position and synchronizes it with the world.notifyBlockUpdate. NBT is normally written and read from the server and client, I checked using system.out.println in update. But if I re-enter the world, then NBT is considered a different meaning. While trying to understand why this is, I noticed that in the game pause sometimes writeToNBT from TileEntity is not called. When writeToNBT is called when paused, when you re-enter the world, NBT is read out normally. (If something is not clear, then ask me, because my English is at the level of a google translator)
 

GUI (The method of clicking on the switch is onMouseClicked in framePrivate):

Spoiler

public class FcGui extends GuiContainer{
    private ArrayList<Button> buttons = new ArrayList<Button>();
    private Slider slider;
    private FrameOfModules frame;
    private PrivateFrame framePrivate;
    private EnergyProgressBar bar;
    private FieldIndicator indicator;
    private IInventory playerInv;
    private TileEntityFC te;

    public FcGui(IInventory playerInv, final TileEntityFC te) {
        super(new FcContainer(playerInv, te));

        this.playerInv = playerInv;
        this.te = te;

        this.xSize = 196;
        this.ySize = 200;

        frame = new FrameOfModules(3, Module.getLocalizedNames(te.getAcceptedModules()));
        framePrivate = new PrivateFrame() {
            @Override
            public void onMouseClicked(boolean isPublic) {
                if (isPublic)
                    te.setPublic();
                else
                    te.setPrivate();
                NetworkHandler.sendToServer(new PacketPublicPrivate(te.getPos(), isPublic));
                ((FcContainer)inventorySlots).changeSlots(isPublic);
            }
        };
        bar = new EnergyProgressBar(true);
        slider = new Slider(7, 83, 20) {
            @Override
            public void onSliderPosChanged(int sliderPos) {
                if (te.getFrequency() != sliderPos) {
                    NetworkHandler.sendToServer(new PacketFrequency(te.getPos(), sliderPos));
                    te.setFrequency(sliderPos);
                }
            }
        };

        ResourceLocation b_r = new ResourceLocation("meem:textures/gui/buttons/left_right/button_right.png");
        ResourceLocation b_r_h = new ResourceLocation("meem:textures/gui/buttons/left_right/button_right_hover.png");
        ResourceLocation b_r_c = new ResourceLocation("meem:textures/gui/buttons/left_right/button_right_click.png");
        ResourceLocation b_l = new ResourceLocation("meem:textures/gui/buttons/left_right/button_left.png");
        ResourceLocation b_l_h = new ResourceLocation("meem:textures/gui/buttons/left_right/button_left_hover.png");
        ResourceLocation b_l_c = new ResourceLocation("meem:textures/gui/buttons/left_right/button_left_click.png");

        buttons.add(new Button(9, 16, 66, 35, b_r, b_r_h, b_r_c) {
            @Override
            public void onButtonClicked() {
                int radius = te.getRadius() + (isShiftKeyDown() ? 10 : 1);
                if (radius > te.getMaxRadius())
                    te.setRadius(te.getMaxRadius());
                else
                    te.setRadius(radius);
                NetworkHandler.sendToServer(new PacketFieldCreator(te.getPos(), te.getVoltage(), te.getRadius()));
            }
        });
        buttons.add(new Button(9, 16, 17, 35, b_l, b_l_h, b_l_c) {
            @Override
            public void onButtonClicked() {
                int radius = te.getRadius() - (isShiftKeyDown() ? 10 : 1);
                if (radius < 1)
                    te.setRadius(1);
                else
                    te.setRadius(radius);
                NetworkHandler.sendToServer(new PacketFieldCreator(te.getPos(), te.getVoltage(), te.getRadius()));
            }
        });
        indicator = new FieldIndicator(7, 6);
    }

    @Override
    protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) {
        GlStateManager.color(1.0f, 1.0f, 1.0f, 1.0f);
        this.mc.getTextureManager().bindTexture(new ResourceLocation("meem:textures/gui/fcgui.png"));
        this.drawTexturedModalRect(this.guiLeft, this.guiTop, 0, 0, this.xSize-20, this.ySize);
        frame.draw(mc, 177 + guiLeft, guiTop, mouseX, mouseY);
        framePrivate.drawFrame(mc, guiLeft - 26, guiTop, te.isPublic());
    }

    @Override
    protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException {
        super.mouseClicked(mouseX, mouseY, mouseButton);
        for (int i = 0; i < buttons.size(); i++)
            buttons.get(i).mouseDown(mouseX - getGuiLeft(), mouseY - getGuiTop());
        slider.mouseDown(mouseX - getGuiLeft(), mouseY - getGuiTop());
        framePrivate.mouseDown();
    }

    @Override
    protected void mouseReleased(int mouseX, int mouseY, int state) {
        super.mouseReleased(mouseX, mouseY, state);
        for (int i = 0; i < buttons.size(); i++)
            buttons.get(i).mouseUp(mouseX - getGuiLeft(), mouseY - getGuiTop());
    }

    @Override
    protected void mouseClickMove(int mouseX, int mouseY, int clickedMouseButton, long timeSinceLastClick) {
        super.mouseClickMove(mouseX, mouseY, clickedMouseButton, timeSinceLastClick);
        slider.mousePressed(mouseX - getGuiLeft(), mouseY - getGuiTop());
    }

    @Override
    protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) {
        this.fontRendererObj.drawString("Field Converter", 88 - this.fontRendererObj.getStringWidth("Field Converter") / 2, 6, 4210752);            //#404040

        fontRendererObj.drawString("Information:", 85, 20, 4210752);
        String[] info = te.getModuleInfo();
        if (info != null)
            for (int i = 0; i < info.length; i++)
                fontRendererObj.drawString(info[i], 85, 30 + i * 10, 4210752);
        else {
            fontRendererObj.drawString("No functional", 85, 30, 4210752);
            fontRendererObj.drawString("module", 85, 40, 4210752);
        }

        fontRendererObj.drawString("Radius", 30, 27, 4210752);
        fontRendererObj.drawString(te.getRadius()+"", 46 - fontRendererObj.getStringWidth(te.getRadius()+"")/2, 40,4210752);

        fontRendererObj.drawString("Frequency", 88 - this.fontRendererObj.getStringWidth("Frequency") / 2, 65, 4210752);
        fontRendererObj.drawString((float)te.getFrequency()/2f+" Hz", 120, 80, 4210752);

        for (int i = 0; i < buttons.size(); i++)
            buttons.get(i).draw(mc, mouseX - getGuiLeft(), mouseY - getGuiTop());
        slider.draw(mc, te.getFrequency());
        indicator.draw(mc, te.hasField());
        bar.draw(mc, 7, 99, mouseX - guiLeft, mouseY - guiTop, te.getVoltage(), te.getMaxVoltage());
        framePrivate.draw(mc, -26, 0, mouseX - guiLeft, mouseY - guiTop, te.isPublic());
    }

    @Override
    public boolean doesGuiPauseGame() {
        return false;
    }
}

 

 

Packet Class:

Spoiler

public class PacketPublicPrivate extends AbstractPacket<PacketPublicPrivate> {
    protected BlockPos pos;
    protected boolean isPublic;

    public PacketPublicPrivate(){}

    public PacketPublicPrivate(BlockPos pos, boolean isPublic){
        this.pos = pos;
        this.isPublic = isPublic;
    }

    @Override
    public void toBytes(final ByteBuf buf) {
        buf.writeBoolean(isPublic);
        buf.writeInt(pos.getX());
        buf.writeInt(pos.getY());
        buf.writeInt(pos.getZ());
    }

    @Override
    public void fromBytes(final ByteBuf buf) {
        isPublic = buf.readBoolean();
        int m, n, b;
        m = buf.readInt();
        n = buf.readInt();
        b = buf.readInt();
        pos = new BlockPos(m, n, b);
    }

    @Override
    public void handleClientSide(PacketPublicPrivate message, EntityPlayer player) {
    }

    @Override
    public void handleServerSide(PacketPublicPrivate message, EntityPlayer player) {
        IPublicPrivateHandler te = (IPublicPrivateHandler) player.world.getTileEntity(message.pos);
        if (message.isPublic)
            te.setPublic();
        else
            te.setPrivate();
    }
}

public abstract class AbstractPacket<REQ extends IMessage> implements IMessage, IMessageHandler<REQ, REQ>
{
    @Override
    public REQ onMessage(final REQ message, final MessageContext ctx)
    {
        if(ctx.side == Side.SERVER)
        {
            System.out.println("PacketToServer");
            handleServerSide(message, ctx.getServerHandler().playerEntity);
        }
        else
        {
            System.out.println("PacketToClient");
            handleClientSide(message, FMLClientHandler.instance().getClientPlayerEntity());
        }
        return null;
    }

    public abstract void handleClientSide(final REQ message, final EntityPlayer player);
    public abstract void handleServerSide(final REQ message, final EntityPlayer player);
}

 

 

TileEntity:

Spoiler

public class TileEntityFC extends FieldReceiverEnergyStandart implements IInventory, IModuleProvider, IFieldCreator, IPublicPrivateHandler, ITickable {

    protected boolean active;
    protected ItemStack[] inventory;
    protected String customName;
    protected int radius;
    protected int maxRadius;
    protected int lossReduce;
    protected int[] ids;

    public TileEntityFC(){
        this.maxVoltage = 150;
        this.maxRadius = 10;
        radius = 1;
        this.frequency = 0;
        inventory = new ItemStack[]{ItemStack.EMPTY, ItemStack.EMPTY, ItemStack.EMPTY, ItemStack.EMPTY, ItemStack.EMPTY, ItemStack.EMPTY,ItemStack.EMPTY, ItemStack.EMPTY};
        active = true;
    }
    @Override
    public void update(){
        System.out.println("isPublic: " + isPublic());
    }

    @Override
    public boolean isWork() {
        return active && hasFuncModule();
    }

    @Override
    public void onField(){
        ItemStack stack = getFuncModule();
        if (!stack.isEmpty())
            ((ModuleFunctional)stack.getItem()).firstUpdate(new FuncArgs(world, getPos(), radius, voltage, lossReduce, isPublic(), ids));
        createField();
    }

    @Override
    public int getSizeInventory() {
        return 8;
    }

    @Override
    public boolean isEmpty(){
        boolean result = true;
        for (int i = 0; i < inventory.length; i++)
            if (inventory[i] != ItemStack.EMPTY) {
                result = false;
                break;
            }
        return result;
    }

    @Override
    public ItemStack getStackInSlot(int index) {
        if (index < 0 || index >= this.getSizeInventory())
            return ItemStack.EMPTY;
        return this.inventory[index];
    }

    @Override
    public ItemStack decrStackSize(int index, int count) {
        if (!this.getStackInSlot(index).isEmpty()) {
            ItemStack itemstack;

            if (this.getStackInSlot(index).getCount() <= count) {
                itemstack = this.getStackInSlot(index);
                this.setInventorySlotContents(index, ItemStack.EMPTY);
                this.markDirty();
                return itemstack;
            } else {
                itemstack = this.getStackInSlot(index).splitStack(count);

                if (this.getStackInSlot(index).getCount() <= 0) {
                    this.setInventorySlotContents(index, ItemStack.EMPTY);
                } else {
                    //Just to show that changes happened
                    this.setInventorySlotContents(index, this.getStackInSlot(index));
                }

                this.markDirty();
                return itemstack;
            }
        } else {
            return ItemStack.EMPTY;
        }
    }

    @Override
    public ItemStack removeStackFromSlot(int index) {
        ItemStack result = inventory[index].copy();
        if (inventory[index].getItem() instanceof ModuleFunctional)
            ((ModuleFunctional)inventory[index].getItem()).removeModule(new FuncArgs(world, getPos(), radius, voltage, lossReduce, isPublic(), ids));
        inventory[index] = ItemStack.EMPTY;
        this.markDirty();
        return result;
    }

    @Override
    public void setInventorySlotContents(int index, ItemStack stack) {
        if (index < 0 || index >= this.getSizeInventory())
            return;

        if (!stack.isEmpty() && stack.getCount() > this.getInventoryStackLimit())
            stack.setCount(this.getInventoryStackLimit());

        if (!isEmpty() && stack.getCount() == 0)
            stack = ItemStack.EMPTY;

        this.inventory[index] = stack;
        this.markDirty();
        if (stack.getItem() instanceof ModuleFunctional)
            ((ModuleFunctional)stack.getItem()).putModule(new FuncArgs(world, getPos(), radius, voltage, lossReduce, isPublic(), ids));
    }

    @Override
    public int getInventoryStackLimit() {
        return 64;
    }

    @Override
    public boolean isUsableByPlayer(EntityPlayer player) {
        return this.world.getTileEntity(this.getPos()) == this && player.getDistanceSq(this.pos.add(0.5, 0.5, 0.5)) <= 64;
    }

    @Override
    public void openInventory(EntityPlayer player) {
        markUpdate();
        ItemStack stack = getFuncModule();
        if (!stack.isEmpty())
            ((ModuleFunctional)stack.getItem()).openInventory(new FuncArgs(world, getPos(), radius, voltage, lossReduce, isPublic(), ids));
    }

    @Override
    public void closeInventory(EntityPlayer player) {
        markUpdate();
        ItemStack stack = getFuncModule();
        if (!stack.isEmpty())
            ((ModuleFunctional)stack.getItem()).closeInventory(new FuncArgs(world, getPos(), radius, voltage, lossReduce, isPublic(), ids));
    }

    @Override
    public boolean isItemValidForSlot(int index, ItemStack stack) {
        return isAccepted(stack.getItem()) && (stack.getItem() instanceof ModuleFunctional ? !hasFuncModule() : true);
    }

    @Override
    public int getField(int id) {
        return 0;
    }

    @Override
    public void setField(int id, int value) {

    }

    @Override
    public int getFieldCount() {
        return 0;
    }

    @Override
    public void clear() {
        for (int i = 0; i < inventory.length; i++)
            inventory[i] = ItemStack.EMPTY;
        this.markDirty();
    }

    @Override
    public String getName() {
        return this.hasCustomName() ? this.customName : "container.tile_entity_FC";
    }

    @Override
    public boolean hasCustomName() {
        return this.customName != null && !this.customName.equals("");
    }

    public String getCustomName() {
        return this.customName;
    }

    public void setCustomName(String customName) {
        this.customName = customName;
    }

    @Override
    public NBTTagCompound getUpdateTag() {
        return writeToNBT(new NBTTagCompound());
    }

    @Override
    @Nullable
    public SPacketUpdateTileEntity getUpdatePacket() {
        return new SPacketUpdateTileEntity(getPos(), 4, getUpdateTag());
    }

    @Override
    public void onDataPacket(net.minecraft.network.NetworkManager net, net.minecraft.network.play.server.SPacketUpdateTileEntity pkt) {
        readFromNBT(pkt.getNbtCompound());
    }

    @Override
    public NBTTagCompound writeToNBT(NBTTagCompound nbt) {
        super.writeToNBT(nbt);


        if (ids != null){
            nbt.setBoolean("isPublic", false);
            nbt.setIntArray("ids", ids);
        }
        else
            nbt.setBoolean("isPublic", true);

        nbt.setBoolean("isActive", active);
        nbt.setInteger("FieldMaxRadius", maxRadius);
        nbt.setInteger("FieldRadius", radius);
        nbt = writeToNBTFieldStats(nbt);

        NBTTagList list = new NBTTagList();
        for (int i = 0; i < this.getSizeInventory(); ++i) {
            if (!this.getStackInSlot(i).isEmpty()) {
                NBTTagCompound stackTag = new NBTTagCompound();
                stackTag.setByte("Slot", (byte) i);
                this.getStackInSlot(i).writeToNBT(stackTag);
                list.appendTag(stackTag);
            }
        }
        nbt.setTag("Items", list);

        if (this.hasCustomName()) {
            nbt.setString("CustomName", this.getCustomName());
        }
        return nbt;
    }

    @Override
    public void readFromNBT(NBTTagCompound nbt) {
        super.readFromNBT(nbt);

        if (!nbt.getBoolean("isPublic")) {
            ids = nbt.getIntArray("ids");
        }
        else
            ids = null;

        active = nbt.getBoolean("isActive");
        maxRadius = nbt.getInteger("FieldMaxRadius");
        radius = nbt.getInteger("FieldRadius");
        readFromNBTFieldStats(nbt);

        NBTTagList list = nbt.getTagList("Items", 10);
        for (int i = 0; i < list.tagCount(); ++i) {
            NBTTagCompound stackTag = list.getCompoundTagAt(i);
            int slot = stackTag.getByte("Slot") & 255;
            //this.setInventorySlotContents(slot, new ItemStack(stackTag));
            inventory[slot] = new ItemStack(stackTag);
        }
        if (nbt.hasKey("CustomName", 8)) {
            this.setCustomName(nbt.getString("CustomName"));
        }
    }

    @Override
    public void updateValueModules(){
        int cVoltage = 0;
        int cRadius = 0;
        int cLossReduction = 0;

        for (int i = 1; i < inventory.length; i++)
            if (inventory[i] != ItemStack.EMPTY){
                if (inventory[i].getItem() instanceof ItemVoltageModule)
                    cVoltage += inventory[i].getCount();
                else if (inventory[i].getItem() instanceof ItemRadiusModule)
                    cRadius += inventory[i].getCount();
                else if (inventory[i].getItem() instanceof ItemLossReductionModule)
                    cLossReduction += inventory[i].getCount();
            }

        maxVoltage = 150 + 150 * cVoltage;
        maxRadius = 10 + 5 * cRadius;
        lossReduce = cLossReduction;
        try {
            if (voltage > maxVoltage)
                setVoltage(maxVoltage);
            if (radius > maxRadius)
                setRadius(maxRadius);
        }
        catch (NullPointerException e){System.out.println("Try to save, when world did not loaded");}
    }

    @Override
    public boolean isAccepted(Item item) {
        Item[] items = getAcceptedModules();
        for (int i = 0; i < items.length; i++)
            if (items[i].equals(item))
                return true;
        return false;
    }

    @Override
    public Item[] getAcceptedModules() {
        return new Item[]{
            ModItems.voltageModule,
            ModItems.lossReduceModule,
            ModItems.radiusModule,
            ModItems.lifeModule,
            ModItems.electricalModule,
            ModItems.aggressiveModule,
            ModItems.strangeModule,
            ModItems.luckModule
        };
    }

    @Override
    public int getModuleCount() {
        return 3;
    }

    public boolean hasFuncModule(){
        for (int i = 0; i < inventory.length; i++)
            if (inventory[i].getItem() instanceof ModuleFunctional)
                return true;
        return false;
    }

    public ItemStack getFuncModule(){
        for (int i = 0; i < inventory.length; i++)
            if (inventory[i].getItem() instanceof ModuleFunctional)
                return inventory[i];
        return ItemStack.EMPTY;
    }

    @Override
    public void createField() {
        ItemStack stack = getFuncModule();
        if (!stack.isEmpty())
            ((ModuleFunctional)stack.getItem()).lastUpdate(new FuncArgs(world, getPos(), radius, voltage, lossReduce, isPublic(), ids));
    }

    @Override
    public int getMaxRadius() {
        return maxRadius;
    }

    @Override
    public int getRadius() {
        return radius;
    }

    @Override
    public void setRadius(int radius) {
        this.radius = radius;
        ItemStack stack = getFuncModule();
        if (!stack.isEmpty())
            ((ModuleFunctional)stack.getItem()).changeRadius(new FuncArgs(world, getPos(), radius, voltage, lossReduce, isPublic(), ids));
    }

    public void setActive(boolean active) {
        this.active = active;
        ItemStack stack = getFuncModule();
        if (!stack.isEmpty())
            ((ModuleFunctional)stack.getItem()).changeActive(new FuncArgs(world, getPos(), radius, voltage, lossReduce, isPublic(), ids), active);
    }

    public boolean getActive(){
        return active;
    }

    public String[] getModuleInfo(){
        ItemStack stack = getFuncModule();
        if (!stack.isEmpty())
            return ((ModuleFunctional)stack.getItem()).getInfo();
        return null;
    }

    @Override
    public void cardsChanged() {
        if (!isPublic())
            setPrivate();
    }

    @Override
    public void setPrivate() {
        int[] toArray = new int[5];
        for (int i = 0; i < 5; i++)
            if (!inventory[i].isEmpty()) {
                try { toArray[i] = inventory[i].getTagCompound().getInteger("id"); }
                catch (NullPointerException e){ toArray[i] = 0; }
            }
        ids = toArray;
        markUpdate();
    }

    @Override
    public void setPublic() {
        ids = null;
        markUpdate();
    }

    @Override
    public boolean isPublic() {
        return ids == null;
    }
}

 

TileEntity parent:

Spoiler

public abstract class FieldReceiverEnergyStandart extends TileEntity implements IFieldReceiverEnergy {

    protected BlockPos link;
    protected int voltage;
    protected int maxVoltage;
    protected int frequency;

    @Override
    public void applyStats(int voltage) {
        this.voltage = voltage;
        if (voltage > maxVoltage)
            exceedingVoltage(voltage);
        markUpdate();
    }

    @Override
    public void applyCreatorsAround(){
        List<TileEntity> tes = EventsHandler.radiusFilter(getPos(), world.loadedTileEntityList, 100);
        for (int i = 0; i < tes.size(); i++) {
            if (tes.get(i) instanceof IFieldCreatorEnergy)
                ((IFieldCreatorEnergy) tes.get(i)).applyLinkedBlocks();
        }
    }

    @Override
    public abstract boolean isWork();

    @Override
    public abstract void onField();

    @Override
    public int getVoltage(){ return voltage; }

    @Override
    public int getMaxVoltage() {
        return maxVoltage;
    }

    @Override
    public int getFrequency(){ return frequency;}

    @Override
    public BlockPos getLink(){
        return link;
    }

    @Override
    public void setLink(BlockPos pos){
        if (pos == null)
            voltage = 0;
        link = pos;
        markUpdate();
    }

    @Override
    public boolean hasField(){
        return link != null;
    }

    @Override
    public void setFrequency(int frequency){
        this.frequency = frequency;
        applyCreatorsAround();
        markUpdate();
    }

    @Override
    public void setVoltage(int voltage){
        this.voltage = voltage;
        markUpdate();
    }

    public void exceedingVoltage(int voltage){
        if (link != null)
            ((IFieldCreatorEnergy)world.getTileEntity(link)).brokeLink(getPos());
        world.createExplosion(new EntityEgg(world, getPos().getX(), getPos().getY(), getPos().getZ()), getPos().getX(), getPos().getY(), getPos().getZ(), voltage/maxVoltage, true);
    }

    protected void markUpdate(){
        world.notifyBlockUpdate(getPos(), getBlockType().getDefaultState(), getBlockType().getDefaultState(), 0);
    }

    public NBTTagCompound writeToNBTFieldStats(NBTTagCompound nbt){
        if (hasField()) {
            nbt.setBoolean("HasLink", true);
            nbt.setInteger("LinkX", link.getX());
            nbt.setInteger("LinkY", link.getY());
            nbt.setInteger("LinkZ", link.getZ());
        }
        else
            nbt.setBoolean("HasLink", false);

        nbt.setInteger("FieldVoltage", voltage);
        nbt.setInteger("FieldMaxVoltage", maxVoltage);
        nbt.setInteger("FieldFrequency", frequency);
        return  nbt;
    }

    public void readFromNBTFieldStats(NBTTagCompound nbt){
        if (nbt.getBoolean("HasLink")){
            link = new BlockPos(nbt.getInteger("LinkX"), nbt.getInteger("LinkY"), nbt.getInteger("LinkZ"));
        }
        else
            link = null;
        voltage = nbt.getInteger("FieldVoltage");
        maxVoltage = nbt.getInteger("FieldMaxVoltage");
        frequency = nbt.getInteger("FieldFrequency");
    }
}

 

Link to comment
Share on other sites

I can't see what's wrong.

I don't like the way you've constructed your packet class (or its name), but it looks like it's doing safe things and as far as I can tell the TE is correctly saving its data to disk and reading it back again. So I'm not really sure what's up.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

14 minutes ago, Draco18s said:

I can't see what's wrong.

I don't like the way you've constructed your packet class (or its name), but it looks like it's doing safe things and as far as I can tell the TE is correctly saving its data to disk and reading it back again. So I'm not really sure what's up.

The most interesting thing is that only "isPublic" is reset. All other values are perfectly readable when you re-enter the world

Link to comment
Share on other sites

Ah!

 

        if (!nbt.getBoolean("isPublic")) {
            ids = nbt.getIntArray("ids");
        }
        else
            ids = null;

 

You never set the isPublic field. You read the bool from the nbt and do the right thing reading more data,  but you never set the field. 

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

4 hours ago, Draco18s said:

Ah!

 


        if (!nbt.getBoolean("isPublic")) {
            ids = nbt.getIntArray("ids");
        }
        else
            ids = null;

 

You never set the isPublic field. You read the bool from the nbt and do the right thing reading more data,  but you never set the field. 

I do not have an isPublic field. IsPublic is used as a key to understand whether ids are null or not. Therefore, everything should work.  And I just realized that my NBT has stopped working on all the blocks. There are no exceptions. Because of what it can happen?

Edited by Minebot1708
Link to comment
Share on other sites

1 hour ago, Jay Avery said:

There's no isPublic field, so what do you mean by this?

 

This is key in NBTTagCompound. In any case, I further wrote that NBT generally ceased to work everywhere. Something is wrong with my code :)

Edited by Minebot1708
Link to comment
Share on other sites

7 hours ago, Jay Avery said:

What exactly do you mean by NBT stopped working? What do you find in the debugger? Are the read/write methods being called? Are the values null or incorrect? What was the last thing you changed before the problem started?

I will give an example of a bug. I go into the world, in the GUI of the blocks I change the variables. These variables are normally synchronized between the client and the server. If I get out of the world, and then come back, then those changes to the variables that I did will not happen. That is, the readFromNBT method will load those values of the variables that were during the  the last login in world, without taking my changes into account. But sometimes all the same readFromNBT loads the correct data when entering the world. In the debugger, I realized that when data is not synchronized during a restart, then when write-out of the world, writeToNBT is not called. When there is no bug, then before the last exit from the world, writeToNBT is called.
Null is nowhere to be found. Before the advent of this problem, I made that switch and added two variables to read / write. And I'll add that can not be called writeToNBT only for my TileEntity. When a bug occurs, TileEntity of vanilla does not suffer. At least in the chests things are save

Link to comment
Share on other sites

8 hours ago, Minebot1708 said:

The problem was that I did not call markDirty

markDirty is still around in 1.11.2? I thought we were supposed to change to some other method name about an update, or am I misremembering a different class?

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

Link to comment
Share on other sites

18 minutes ago, jeffryfisher said:

markDirty is still around in 1.11.2?

TileEntity::markDirty is indeed still around, even in 1.12(and I checked - it does the same thing). It marks the chunk it is in as "dirty(1.12 name)/modified(1.11.x name)" aka "needs to be saved to disk as there were changes" and updates the comparator output for the block.

Edited by V0idWa1k3r
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.