Jump to content

[1.7.10][Unsloved]Custom chest not keeping orientation when reloaded world


Recommended Posts

Posted

So i have dine multiple tests and the orientation is getting saved and set back but i think the render is not being rotated.  Here is my code:

Renderer

 

 

package com.professorvennie.core.client.renderer.tileentity;

import com.professorvennie.core.tileEntity.TileEntityPlasticChest;
import com.professorvennie.core.lib.Reference;
import net.minecraft.client.model.ModelChest;
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.common.util.ForgeDirection;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;

public class TileEntityRendererPlasticChest extends TileEntitySpecialRenderer {

    private final ModelChest modelChest = new ModelChest();
    private ResourceLocation texture = new ResourceLocation(Reference.MOD_ID, "textures/model/plastic_chest.png");

    @Override
    public void renderTileEntityAt(TileEntity tileEntity, double x, double y, double z, float tick) {
        if (tileEntity instanceof TileEntityPlasticChest){
            TileEntityPlasticChest tileEntityPlasticChest = (TileEntityPlasticChest) tileEntity;
            ForgeDirection direction = null;

            if (tileEntityPlasticChest.getWorldObj() != null){
                direction = tileEntityPlasticChest.getOrientation();
            }
                this.bindTexture(texture);


            GL11.glPushMatrix();
            GL11.glEnable(GL12.GL_RESCALE_NORMAL);
            GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
            GL11.glTranslatef((float) x, (float) y + 1.0F, (float) z + 1.0F);
            GL11.glScalef(1.0F, -1.0F, -1.0F);
            GL11.glTranslatef(0.5F, 0.5F, 0.5F);
            short angle = 0;

            if (direction != null){
                if (direction == ForgeDirection.NORTH){
                    angle = 180;
                }
                else if (direction == ForgeDirection.SOUTH){
                    angle = 0;
                }
                else if (direction == ForgeDirection.WEST){
                    angle = 90;
                }
                else if (direction == ForgeDirection.EAST){
                    angle = -90;
                }
            }

            GL11.glRotatef(angle, 0.0F, 1.0F, 0.0F);
            GL11.glTranslatef(-0.5F, -0.5F, -0.5F);
            float adjustedLidAngle = tileEntityPlasticChest.prevLidAngle + (tileEntityPlasticChest.lidAngle - tileEntityPlasticChest.prevLidAngle) * tick;
            adjustedLidAngle = 1.0F - adjustedLidAngle;
            adjustedLidAngle = 1.0F - adjustedLidAngle * adjustedLidAngle * adjustedLidAngle;
            modelChest.chestLid.rotateAngleX = -(adjustedLidAngle * (float) Math.PI / 2.0F);
            modelChest.renderAll();
            GL11.glDisable(GL12.GL_RESCALE_NORMAL);
            GL11.glPopMatrix();
            GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
        }
    }
}

 

 

TileEntity:

 

 

package com.professorvennie.core.tileEntity;

import com.professorvennie.core.block.ModBlocks;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.util.Constants;
import net.minecraftforge.common.util.ForgeDirection;


public class TileEntityPlasticChest extends TileEntityMod implements IInventory{

    /** The current angle of the lid (between 0 and 1) */
    public float lidAngle;
    /** The angle of the lid last tick */
    public float prevLidAngle;
    /** The number of players currently using this chest */
    public int numPlayersUsing;

    private int ticksSinceSync;

    public ItemStack[] slots;

    public TileEntityPlasticChest(){
        slots = new ItemStack[9*6];
    }

    @Override
    public int getSizeInventory() {
        return slots.length;
    }

    @Override
    public ItemStack getStackInSlot(int i) {
        return slots[i];
    }

    @Override
    public ItemStack decrStackSize(int var1, int var2) {
        if(this.slots[var1] != null){
            ItemStack itemstack;
            if(this.slots[var1].stackSize <= var2){
                itemstack = this.slots[var1];
                this.slots[var1] = null;
                return itemstack;
            }else{
                itemstack = this.slots[var1].splitStack(var2);
                if(this.slots[var1].stackSize == 0){
                    this.slots[var1] = null;
                }
                return itemstack;
            }
        }
        return null;
    }

    @Override
    public ItemStack getStackInSlotOnClosing(int slot) {
        if(this.slots[slot] != null){
            ItemStack itemstack = this.slots[slot];
            this.slots[slot] = null;
            return itemstack;
        }

        return null;
    }

    @Override
    public void setInventorySlotContents(int slot, ItemStack itemStack) {
        this.slots[slot]= itemStack;

        if(itemStack != null && itemStack.stackSize > this.getInventoryStackLimit()){
            itemStack.stackSize = this.getInventoryStackLimit();
        }
    }

    public void readFromNBT(NBTTagCompound nbtTagCompound){
        super.readFromNBT(nbtTagCompound);

        NBTTagList tagList = nbtTagCompound.getTagList("Items", 10);
        slots = new ItemStack[this.getSizeInventory()];
        for (int i = 0; i < tagList.tagCount(); ++i){
            NBTTagCompound tagCompound = tagList.getCompoundTagAt(i);
            byte slotIndex = tagCompound.getByte("Slot");
            if (slotIndex >= 0 && slotIndex < slots.length)
            {
                slots[slotIndex] = ItemStack.loadItemStackFromNBT(tagCompound);
            }
        }
    }

    public void writeToNBT(NBTTagCompound nbtTagCompound){
        super.writeToNBT(nbtTagCompound);

        NBTTagList tagList = new NBTTagList();
        for (int currentIndex = 0; currentIndex < slots.length; ++currentIndex){
            if (slots[currentIndex] != null){
                NBTTagCompound tagCompound = new NBTTagCompound();
                tagCompound.setByte("Slot", (byte) currentIndex);
                slots[currentIndex].writeToNBT(tagCompound);
                tagList.appendTag(tagCompound);
            }
        }
        nbtTagCompound.setTag("Items", tagList);
    }

    @Override
    public String getInventoryName() {
        return this.hasCustomName() ? this.getCustomName() : "container.plasticChest";
    }

    @Override
    public boolean hasCustomInventoryName() {
        return customName != null && customName.length() > 0;
    }

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

    @Override
    public boolean isUseableByPlayer(EntityPlayer entityPlayer) {
        return true;
    }

    @Override
    public void openInventory() {
        this.numPlayersUsing++;
        this.worldObj.addBlockEvent(this.xCoord, this.yCoord, this.zCoord, ModBlocks.plasticChest, 1, this.numPlayersUsing);
    }

    @Override
    public void closeInventory() {
            this.numPlayersUsing--;
            this.worldObj.addBlockEvent(this.xCoord, this.yCoord, this.zCoord, ModBlocks.plasticChest, 1, this.numPlayersUsing);
    }

    @Override
    public boolean isItemValidForSlot(int i, ItemStack itemStack) {
        return true;
    }

    public void updateEntity(){
        super.updateEntity();

        if (++ticksSinceSync % 20 * 4 == 0){
            worldObj.addBlockEvent(xCoord, yCoord, zCoord, ModBlocks.plasticChest, 1, numPlayersUsing);
        }

        prevLidAngle = lidAngle;
        float angleIncrement = 0.1F;
        double adjustedXCoord, adjustedZCoord;

        if (numPlayersUsing > 0 && lidAngle == 0.0F){
            adjustedXCoord = xCoord + 0.5D;
            adjustedZCoord = zCoord + 0.5D;
            worldObj.playSoundEffect(adjustedXCoord, yCoord + 0.5D, adjustedZCoord, "random.chestopen", 0.5F, worldObj.rand.nextFloat() * 0.1F + 0.9F);
        }

        if (numPlayersUsing == 0 && lidAngle > 0.0F || numPlayersUsing > 0 && lidAngle < 1.0F){
            float var8 = lidAngle;

            if (numPlayersUsing > 0){
                lidAngle += angleIncrement;
            }
            else{
                lidAngle -= angleIncrement;
            }

            if (lidAngle > 1.0F){
                lidAngle = 1.0F;
            }

            if (lidAngle < 0.5F && var8 >= 0.5F){
                adjustedXCoord = xCoord + 0.5D;
                adjustedZCoord = zCoord + 0.5D;
                worldObj.playSoundEffect(adjustedXCoord, yCoord + 0.5D, adjustedZCoord, "random.chestclosed", 0.5F, worldObj.rand.nextFloat() * 0.1F + 0.9F);
            }

            if (lidAngle < 0.0F){
                lidAngle = 0.0F;
            }
        }
    }

    @Override
    public boolean receiveClientEvent(int eventID, int numUsingPlayers){
        if (eventID == 1){
            this.numPlayersUsing = numUsingPlayers;
            return true;
        }
        else{
            return super.receiveClientEvent(eventID, numUsingPlayers);
        }
    }
}

TileEntityMod:

 

 

package com.professorvennie.core.tileEntity;

import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.util.ForgeDirection;

public class TileEntityMod extends TileEntity{

    protected ForgeDirection orientation;

    protected String customName;

    public TileEntityMod(){
        orientation = ForgeDirection.SOUTH;
    }

    public ForgeDirection getOrientation(){
        return orientation;
    }

    public void setOrientation(int orientation){
        this.orientation = ForgeDirection.getOrientation(orientation);
    }

    public void setOrientation(ForgeDirection direction){
        this.orientation = direction;
    }

    public String getCustomName(){
        return customName;
    }

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

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

        if (nbtTagCompound.hasKey("orientation")){
            System.out.println("hfdjf");
            this.orientation = ForgeDirection.getOrientation(nbtTagCompound.getInteger("orientation"));
        }

        if (nbtTagCompound.hasKey("customName")){
            this.customName = nbtTagCompound.getString("customName");
        }
    }

    @Override
    public void writeToNBT(NBTTagCompound nbtTagCompound){
        super.writeToNBT(nbtTagCompound);

        nbtTagCompound.setInteger("orientation", orientation.ordinal());

        if (this.hasCustomName()){
            nbtTagCompound.setString("customName", this.customName);
        }
    }

    public boolean hasCustomName()
    {
        return customName != null && customName.length() > 0;
    }
}

[/spoiler]

 

 

 

Block:

 

 

package com.professorvennie.core.block;

import com.professorvennie.core.tileEntity.TileEntityPlasticChest;
import com.professorvennie.core.lib.BlockNames;
import com.professorvennie.core.lib.LibGuiIds;
import com.professorvennie.core.main.MachineryCraft;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;

public class BlockPlasticChest extends BlockContainer {

    protected BlockPlasticChest() {
        super(Material.wood);
        setBlockName(BlockNames.BLOCK_PLASTIC_CHEST);
        setCreativeTab(MachineryCraft.tabMachineryCraft);
        this.setBlockBounds(0.0625F, 0.0F, 0.0625F, 0.9375F, 0.875F, 0.9375F);
    }

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

    @Override
    public int getRenderType() {
        return 22;
    }

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

    @Override
    public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float p_149727_7_, float p_149727_8_, float p_149727_9_) {
            if (!world.isRemote) {
                TileEntityPlasticChest entity = (TileEntityPlasticChest)world.getTileEntity(x, y, z);
                System.out.println(entity.getOrientation());
                player.openGui(MachineryCraft.instance, LibGuiIds.GUIID_PLASTIC_CHEST, world, x, y, z);
            }
            return true;
        }


    @Override
    public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase entityLiving, ItemStack itemStack){
        if (world.getTileEntity(x, y, z) instanceof TileEntityPlasticChest){
            int direction = 0;
            int facing = MathHelper.floor_double(entityLiving.rotationYaw * 4.0F / 360.0F + 0.5D) & 3;

            if (facing == 0){
                direction = ForgeDirection.NORTH.ordinal();
            }
            else if (facing == 1){
                direction = ForgeDirection.EAST.ordinal();
            }
            else if (facing == 2){
                direction = ForgeDirection.SOUTH.ordinal();
            }
            else if (facing == 3){
                direction = ForgeDirection.WEST.ordinal();
            }

            if (itemStack.hasDisplayName()){
                ((TileEntityPlasticChest) world.getTileEntity(x, y, z)).setCustomName(itemStack.getDisplayName());
            }

            ((TileEntityPlasticChest) world.getTileEntity(x, y, z)).setOrientation(direction);
        }
    }

    @Override
    public boolean onBlockEventReceived(World world, int x, int y, int z, int eventId, int eventData){
        super.onBlockEventReceived(world, x, y, z, eventId, eventData);
        TileEntity tileentity = world.getTileEntity(x, y, z);
        return tileentity != null && tileentity.receiveClientEvent(eventId, eventData);
    }

    @Override
    public TileEntity createNewTileEntity(World world, int i) {
        return new TileEntityPlasticChest();
    }
}

[/spoiler]

Posted

Hi

 

I suggest you add more System.out.println(direction); to track it down- whenever you set or get it basically.  That should show you pretty quickly where you're going wrong.

 

eg

    public ForgeDirection getOrientation(){
        System.out.println("getOrientation() - orientation:", this.orientation);
        return orientation;
    }

    public void setOrientation(int orientation){                                                   // dude, name hiding is very bad practice
        this.orientation = ForgeDirection.getOrientation(orientation);
        System.out.println("setOrientation(int) - orientation:", this.orientation);
    }

    public void setOrientation(ForgeDirection direction){
        this.orientation = direction;
        System.out.println("setOrientation(FOrgeDirection) - orientation:", this.orientation);
    }
    @Override
    public void readFromNBT(NBTTagCompound nbtTagCompound){
        super.readFromNBT(nbtTagCompound);

        if (nbtTagCompound.hasKey("orientation")){
            System.out.println("hfdjf");
            this.orientation = ForgeDirection.getOrientation(nbtTagCompound.getInteger("orientation"));
System.out.println("readFromNBT- orientation:", orientation);
        }

        if (nbtTagCompound.hasKey("customName")){
            this.customName = nbtTagCompound.getString("customName");
        }
    }

    @Override
    public void writeToNBT(NBTTagCompound nbtTagCompound){
        super.writeToNBT(nbtTagCompound);

        nbtTagCompound.setInteger("orientation", orientation.ordinal());
System.out.println("writeToNBT - orientation:", orientation.ordinal());
        if (this.hasCustomName()){
            nbtTagCompound.setString("customName", this.customName);
        }
    }

{etc}

 

-TGG

Posted

I have done that but it confuses me because if a do a print statement in the onockactivated it prints out the correct direction that it was placed even after a world save.  If i do the a print statement in the renderer printing out the direction variable it always prints out south after a world save and is fine before a world save?

 

Edit:

I have done more testing and i have found out that the setOrentation always returns south and i dont know why it does.

Posted

Again i have done more testing and the read and write nbt are printing out the right values so i have no idea what is wroung.

 

Edit:

It does not only print south i derped and but the print statement before i was setting the new orientation so the set orientation does work.  I finaly found out what the promblem is.  It is the getorientation method. It always returns south after a world reload but i cant figure out why. 

 

Posted

Hi

 

What does this line give you

 

            this.orientation = ForgeDirection.getOrientation(nbtTagCompound.getInteger("orientation"));
System.out.println("readFromNBT- orientation:", this.orientation);

 

Actually, would you pls copy the console output from 1) before the save; and then 2) after exiting the world and reloading it

 

-TGG

Posted

You can mark the block for update and not worry about packets.  It will update the NTB data for you.

 

 

Long time Bukkit & Forge Programmer

Happy to try and help

Posted

 

 

Thanks for the help but it is kinda of out dated.  Like packet132TileEntityData doesnt exist so i figured out to change it to S35PacketUpdateTileEntity.  I dont know if thats right and pkt.customParm1 is not there any more so i changed it to pkt.func_148857_g() sense that function was A NBTTagCompund.  Again i dont know if thats right.

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.