Jump to content

[1.9] Changing state of block 'resets' TileEntity attached to it


DARKHAWX

Recommended Posts

Hi there,

 

So I have a tile entity that upon 'crafting' will execute a countdown from 200 to 0, decreasing by one each time the tile entity is updated. At certain intervals through this countdown I change the blockstate of the attached block. However it appears that in doing so it will also 'reset' the countdown.

 

TileEntity:

 

package com.darkhawx.planck.main.tileEntities;

import com.darkhawx.planck.api.crafting.RecipeAltar;
import com.darkhawx.planck.main.blocks.BlockAltar;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.passive.EntitySheep;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ITickable;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

import java.util.ArrayList;
import java.util.List;

public class TileAltar extends TileEntity implements ITickable {

    private List<ItemStack> items = new ArrayList<>();
    private ItemStack output;
    private int craftingTime;
    private boolean crafting;
    private float bloodAmount;

    public boolean isRecipe = false;

    public void update() {
        if (!worldObj.isRemote) {
            this.isRecipe = isRecipe();
            if (this.isRecipe) {
                //Something?
            }

            if (this.crafting) {
                this.craftingTime = 201;
                this.crafting = false;
            }
            else if (this.craftingTime < 0) {
                this.craftingTime = 0;
            } else if (this.craftingTime > 0) {
                switch (this.craftingTime) {
                    case 1:
                        BlockAltar.setState(worldObj, getPos(), BlockAltar.EnumAltarState.INACTIVE);
                        finishCraft(worldObj, getPos(), this);
                        System.out.println("inactive");
                        break;
                    case 25:
                        BlockAltar.setState(worldObj, getPos(), BlockAltar.EnumAltarState.END);
                        System.out.println("end");
                        break;
                    case 125:
                        BlockAltar.setState(worldObj, getPos(), BlockAltar.EnumAltarState.CRAFTING);
                        System.out.println("crafting");
                        break;
                    case 200:
                        BlockAltar.setState(worldObj, getPos(), BlockAltar.EnumAltarState.BLOOD);
                        System.out.println("blood");
                        break;
                }
                this.craftingTime--;
                System.out.println("CraftingTime: " + this.craftingTime);
            }
        }
    }

    public boolean isRecipe() {
        output = RecipeAltar.checkRecipe(items);
        if (output != null) {
            return true;
        }
        return false;
    }


    public static void craft(World worldIn, BlockPos pos, TileAltar altar) {
        if (altar.craftingTime == 0 && !altar.crafting) {
            System.out.println("blood: " + altar.bloodAmount);
            if (altar.bloodAmount >= altar.getRecipe().getBlood()) {
                System.out.println("craft");
                altar.crafting = true;
            }
        }
    }

    private static void finishCraft(World worldIn, BlockPos pos, TileAltar altar) {
        if (altar.getOutput() != null) {
            System.out.println("crafted");
            altar.bloodAmount = 0;
            altar.markDirty();
            if (!worldIn.isRemote) {
                EntityItem entity = new EntityItem(worldIn, pos.getX() + 0.5D, (double) pos.getY() + 1.1D, (double) pos.getZ() + 0.5D, altar.getOutput());
                entity.setVelocity(0.0D, 0.0D, 0.0D);
                worldIn.spawnEntityInWorld(entity);
            }
            altar.items.clear();
        }
    }

    public static boolean addItem(World worldObj, ItemStack item, TileAltar altar) {
        if (item != null) {
            altar.markDirty();

            if (worldObj != null) {
                IBlockState state = worldObj.getBlockState(altar.getPos());
                worldObj.notifyBlockUpdate(altar.getPos(), state, state, 3);
            }

            if (altar.items.size() <  {
                altar.items.add(item);
                return true;
            } else {
                return false;
            }
        }
        return false;
    }

    private void addItem(ItemStack item) {
        markDirty();
        worldObj.notifyBlockUpdate(this.getPos(), worldObj.getBlockState(this.getPos()), worldObj.getBlockState(this.getPos()), 3);
        if (items.size() <  {
            items.add(item);
        }
    }

    public static void removeItems(World worldIn, BlockPos pos, TileAltar altar) {
        System.out.println("removed");
        altar.markDirty();
        if (altar.getItems().size() != 0) {
            if (!worldIn.isRemote) {
                for (ItemStack item : altar.getItems()) {
                    EntityItem entity = new EntityItem(worldIn, pos.getX() + 0.5D, (double) pos.getY() + 1.1D, (double) pos.getZ() + 0.5D, item);
                    worldIn.spawnEntityInWorld(entity);
                }
            }
            altar.items.clear();
        }
    }

    public static void removeLastItem(World worldIn, BlockPos pos, TileAltar altar) {
        altar.markDirty();
        if (altar.getItems().size() != 0) {
            if (!worldIn.isRemote) {
                ItemStack item = altar.getItems().get(altar.getItems().size());
                EntityItem entity = new EntityItem(worldIn, pos.getX() + 0.5D, (double) pos.getY() + 1.1D, (double) pos.getZ() + 0.5D, item);
                worldIn.spawnEntityInWorld(entity);
            }
            altar.items.remove(altar.items.size());
        }
    }

//    @Override
//    public NBTTagCompound getUpdateTag() {
//        // getUpdateTag() is called whenever the chunkdata is sent to the
//        // client. In contrast getUpdatePacket() is called when the tile entity
//        // itself wants to sync to the client. In many cases you want to send
//        // over the same information in getUpdateTag() as in getUpdatePacket().
//        return writeToNBT(new NBTTagCompound());
//    }
//
//    @Override
//    public SPacketUpdateTileEntity getUpdatePacket() {
//        // Prepare a packet for syncing our TE to the client. Since we only have to sync the stack
//        // and that's all we have we just write our entire NBT here. If you have a complex
//        // tile entity that doesn't need to have all information on the client you can write
//        // a more optimal NBT here.
//        NBTTagCompound nbtTag = new NBTTagCompound();
//        this.writeToNBT(nbtTag);
//        return new SPacketUpdateTileEntity(getPos(), 1, nbtTag);
//    }

    public boolean isCrafting() {
        return this.craftingTime >= 0;
    }

    public List<ItemStack> getItems() {return this.items;}

    public ItemStack getOutput() {return this.output;}

    public float getBlood() {return this.bloodAmount;}

    public RecipeAltar getRecipe() {
        return RecipeAltar.getRecipe(items);
    }

    public int getCraftingTime() {
        return this.craftingTime;
    }

    public void addBlood(float bloodAmount) {
        this.bloodAmount += bloodAmount;
    }

    public static float getBloodForEntity(EntityLivingBase entity) {
        return entity.getMaxHealth() * 10.0F;
    }

    @Override
    public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity packet) {
        // Here we get the packet from the server and read it into our client side tile entity
        this.readFromNBT(packet.getNbtCompound());
    }

    @Override
    public void readFromNBT(NBTTagCompound compound) {
        super.readFromNBT(compound);
        for (int i = 0; i < 8; i++) {
            if (compound.hasKey("input" + i)) {
                addItem(ItemStack.loadItemStackFromNBT(compound.getCompoundTag("input" + i)));
                //items.add(i, ItemStack.loadItemStackFromNBT(compound.getCompoundTag("input" + i)));
            }
        }
    }

    @Override
    public void writeToNBT(NBTTagCompound compound) {
        super.writeToNBT(compound);
        for (int i = 0; i < 8; i++) {
            if (items.size() > i) {
                NBTTagCompound tagCompound = new NBTTagCompound();
                ItemStack item = items.get(i);
                item.writeToNBT(tagCompound);
                compound.setTag("input" + i, tagCompound);
            }
        }
    }
}

 

 

How would I go about preventing this?

 

 

P.S: The items that have been currently added to the TE are rendered above it. Due to the way my code is setup it turns out that the finishCraft() method only gets ran server side (a tile entities worldObj is not remote), which means to the client the items haven't disappeared. How would I go about rectifying this? Would modifying the way data is stored in the nbt work? Or would I need to somehow tell the client that the items have disappeared?

 

P.P.S: The commented out NBT related methods were taken from some random tutorial ages ago. They seem to have some relevance to syncing data between client and server, however they are outdated and do not work.

No signature for you!

Link to comment
Share on other sites

override

doesRefresh

and return false.

  • Like 1

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

Override

TileEntity#shouldRefresh

to return

oldState.getBlock() != newSate.getBlock()

. This way the

TileEntity

will only be recreated when the

Block

changes.

 

P.S: The items that have been currently added to the TE are rendered above it. Due to the way my code is setup it turns out that the finishCraft() method only gets ran server side (a tile entities worldObj is not remote), which means to the client the items haven't disappeared. How would I go about rectifying this? Would modifying the way data is stored in the nbt work? Or would I need to somehow tell the client that the items have disappeared?

 

P.P.S: The commented out NBT related methods were taken from some random tutorial ages ago. They seem to have some relevance to syncing data between client and server, however they are outdated and do not work.

 

The commented out methods aren't outdated, they're for 1.9.4.

 

If the client needs some data from the

TileEntity

to render your block, send it in the update (a.k.a description) packet.

 

In 1.9, override

TileEntity#getDescriptionPacket

to write the data that needs syncing to a compound tag and then create and return an

SPacketUpdateTileEntity

with the position and NBT. Override

TileEntity#onDataPacket

to read from the packet's NBT.

 

In 1.9.4, override

TileEntity#getUpdateTag

to write the data that needs syncing to a compound tag and return it. Override

TileEntity#getUpdatePacket

to create and return an

SPacketUpdateTileEntity

with the position and update tag. Override

TileEntity#onDataPacket

to read from the packet's NBT.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

override

doesRefresh

and return false.

Uh-oh, then the TE might never go away even when it should. I think Choonster is on the right track, and for some kinds of mods, one might even want more conditions controlling whether to replace the TE.

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

Override

TileEntity#shouldRefresh

to return

oldState.getBlock() != newSate.getBlock()

. This way the

TileEntity

will only be recreated when the

Block

changes.

 

P.S: The items that have been currently added to the TE are rendered above it. Due to the way my code is setup it turns out that the finishCraft() method only gets ran server side (a tile entities worldObj is not remote), which means to the client the items haven't disappeared. How would I go about rectifying this? Would modifying the way data is stored in the nbt work? Or would I need to somehow tell the client that the items have disappeared?

 

P.P.S: The commented out NBT related methods were taken from some random tutorial ages ago. They seem to have some relevance to syncing data between client and server, however they are outdated and do not work.

 

The commented out methods aren't outdated, they're for 1.9.4.

 

If the client needs some data from the

TileEntity

to render your block, send it in the update (a.k.a description) packet.

 

In 1.9, override

TileEntity#getDescriptionPacket

to write the data that needs syncing to a compound tag and then create and return an

SPacketUpdateTileEntity

with the position and NBT. Override

TileEntity#onDataPacket

to read from the packet's NBT.

 

In 1.9.4, override

TileEntity#getUpdateTag

to write the data that needs syncing to a compound tag and return it. Override

TileEntity#getUpdatePacket

to create and return an

SPacketUpdateTileEntity

with the position and update tag. Override

TileEntity#onDataPacket

to read from the packet's NBT.

 

Thanks!

No signature for you!

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.