Jump to content

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


slugslug

Recommended Posts

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]

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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. 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

 

 

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.

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • I have done this now but have got the error:   'food(net.minecraft.world.food.FoodProperties)' in 'net.minecraft.world.item.Item.Properties' cannot be applied to                '(net.minecraftforge.registries.RegistryObject<net.minecraft.world.item.Item>)' public static final RegistryObject<Item> LEMON_JUICE = ITEMS.register( "lemon_juice", () -> new Item( new HoneyBottleItem.Properties().stacksTo(1).food( (new FoodProperties.Builder()) .nutrition(3) .saturationMod(0.25F) .effect(() -> new MobEffectInstance(MobEffects.DAMAGE_RESISTANCE, 1500), 0.01f ) .build() ) )); The code above is from the ModFoods class, the one below from the ModItems class. public static final RegistryObject<Item> LEMON_JUICE = ITEMS.register("lemon_juice", () -> new Item(new Item.Properties().food(ModFoods.LEMON_JUICE)));   I shall keep going between them to try and figure out the cause. I am sorry if this is too much for you to help with, though I thank you greatly for your patience and all the effort you have put in to help me.
    • I have been following these exact tutorials for quite a while, I must agree that they are amazing and easy to follow. I have registered the item in the ModFoods class, I tried to do it in ModItems (Where all the items should be registered) but got errors, I think I may need to revert this and figure it out from there. Once again, thank you for your help! 👍 Just looking back, I have noticed in your code you added ITEMS.register, which I am guessing means that they are being registered in ModFoods, I shall go through the process of trial and error to figure this out.
    • ♈+2349027025197ஜ Are you a pastor, business man or woman, politician, civil engineer, civil servant, security officer, entrepreneur, Job seeker, poor or rich Seeking how to join a brotherhood for protection and wealth here’s is your opportunity, but you should know there’s no ritual without repercussions but with the right guidance and support from this great temple your destiny is certain to be changed for the better and equally protected depending if you’re destined for greatness Call now for enquiry +2349027025197☎+2349027025197₩™ I want to join ILLUMINATI occult without human sacrificeGREATORLDRADO BROTHERHOOD OCCULT , Is The Club of the Riches and Famous; is the world oldest and largest fraternity made up of 3 Millions Members. We are one Family under one father who is the Supreme Being. In Greatorldrado BROTHERHOOD we believe that we were born in paradise and no member should struggle in this world. Hence all our new members are given Money Rewards once they join in order to upgrade their lifestyle.; interested viewers should contact us; on. +2349027025197 ۝ஐℰ+2349027025197 ₩Greatorldrado BROTHERHOOD OCCULT IS A SACRED FRATERNITY WITH A GRAND LODGE TEMPLE SITUATED IN G.R.A PHASE 1 PORT HARCOURT NIGERIA, OUR NUMBER ONE OBLIGATION IS TO MAKE EVERY INITIATE MEMBER HERE RICH AND FAMOUS IN OTHER RISE THE POWERS OF GUARDIANS OF AGE+. +2349027025197   SEARCHING ON HOW TO JOIN THE Greatorldrado BROTHERHOOD MONEY RITUAL OCCULT IS NOT THE PROBLEM BUT MAKE SURE YOU'VE THOUGHT ABOUT IT VERY WELL BEFORE REACHING US HERE BECAUSE NOT EVERYONE HAS THE HEART TO DO WHAT IT TAKES TO BECOME ONE OF US HERE, BUT IF YOU THINK YOU'RE SERIOUS MINDED AND READY TO RUN THE SPIRITUAL RACE OF LIFE IN OTHER TO ACQUIRE ALL YOU NEED HERE ON EARTH CONTACT SPIRITUAL GRANDMASTER NOW FOR INQUIRY +2349027025197   +2349027025197 Are you a pastor, business man or woman, politician, civil engineer, civil servant, security officer, entrepreneur, Job seeker, poor or rich Seeking how to join
    • Hi, I'm trying to use datagen to create json files in my own mod. This is my ModRecipeProvider class. public class ModRecipeProvider extends RecipeProvider implements IConditionBuilder { public ModRecipeProvider(PackOutput pOutput) { super(pOutput); } @Override protected void buildRecipes(Consumer<FinishedRecipe> pWriter) { ShapedRecipeBuilder.shaped(RecipeCategory.MISC, ModBlocks.COMPRESSED_DIAMOND_BLOCK.get()) .pattern("SSS") .pattern("SSS") .pattern("SSS") .define('S', ModItems.COMPRESSED_DIAMOND.get()) .unlockedBy(getHasName(ModItems.COMPRESSED_DIAMOND.get()), has(ModItems.COMPRESSED_DIAMOND.get())) .save(pWriter); ShapelessRecipeBuilder.shapeless(RecipeCategory.MISC, ModItems.COMPRESSED_DIAMOND.get(),9) .requires(ModBlocks.COMPRESSED_DIAMOND_BLOCK.get()) .unlockedBy(getHasName(ModBlocks.COMPRESSED_DIAMOND_BLOCK.get()), has(ModBlocks.COMPRESSED_DIAMOND_BLOCK.get())) .save(pWriter); ShapedRecipeBuilder.shaped(RecipeCategory.MISC, ModItems.COMPRESSED_DIAMOND.get()) .pattern("SSS") .pattern("SSS") .pattern("SSS") .define('S', Blocks.DIAMOND_BLOCK) .unlockedBy(getHasName(ModItems.COMPRESSED_DIAMOND.get()), has(ModItems.COMPRESSED_DIAMOND.get())) .save(pWriter); } } When I try to run the runData client, it shows an error:  Caused by: java.lang.IllegalStateException: Duplicate recipe compressed:compressed_diamond I know that it's caused by the fact that there are two recipes for the ModItems.COMPRESSED_DIAMOND. But I need both of these recipes, because I need a way to craft ModItems.COMPRESSED_DIAMOND_BLOCK and restore 9 diamond blocks from ModItems.COMPRESSED_DIAMOND. Is there a way to solve this?
  • Topics

×
×
  • Create New...

Important Information

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