Jump to content

[1.8.9] Custom Block Container..


Phonixe

Recommended Posts

Hello, so i tried to create a custom block that has an inventory with a gui, however whenever i right click the block the game crashes.

 

Crash report :

http://pastebin.com/vr4JeQwP

 

Custom Block :

 

public class Phoerite_Bed_Cabinet extends Block {

public static PropertyDirection FACING = PropertyDirection.create("facing", EnumFacing.Plane.HORIZONTAL);


public Phoerite_Bed_Cabinet(Material materialIn) {
	super(materialIn);
	this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH));
}

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

@Override
public EnumWorldBlockLayer getBlockLayer() {
	return EnumWorldBlockLayer.CUTOUT;
}

@Override
public IBlockState onBlockPlaced(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer) {
	IBlockState state = super.onBlockPlaced(worldIn, pos, facing, hitX, hitY, hitZ, meta, placer);
	return state.withProperty(FACING, placer.getHorizontalFacing());
}

@Override
public void addCollisionBoxesToList(World worldIn, BlockPos pos, IBlockState state, AxisAlignedBB mask, List<AxisAlignedBB> list, Entity collidingEntity) {

super.addCollisionBoxesToList(worldIn, pos, state, mask, list, collidingEntity);
int metadata = getMetaFromState(state);
    super.setBlockBounds( 0.0F, 0F, 0.0F, 1.0F, 1.0F, 1.0F);
    
}
    
    @Override
    public int getMetaFromState(IBlockState state) {
    	
    	return ((EnumFacing) state.getValue(FACING)).getHorizontalIndex();
    }
    
    @Override
    public IBlockState getStateFromMeta(int meta) {
    	
    	return this.getDefaultState().withProperty(FACING, EnumFacing.getHorizontal(meta));
    }
    
    @Override
    protected BlockState createBlockState() {
    	return new BlockState(this, new IProperty[] {FACING});
    }
    
    @Override
    public void breakBlock(World world, BlockPos pos, IBlockState blockstate) {
        TileEntity_Phoerite_Bed_Cabinet te = (TileEntity_Phoerite_Bed_Cabinet) world.getTileEntity(pos);
        InventoryHelper.dropInventoryItems(world, pos, te);
        super.breakBlock(world, pos, blockstate);
    }


    @Override
    public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) {
        if (stack.hasDisplayName()) {
            ((TileEntity_Phoerite_Bed_Cabinet) worldIn.getTileEntity(pos)).setCustomName(stack.getDisplayName());
        }
    }
    
    @Override
    public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumFacing side, float hitX, float hitY, float hitZ) {
        if (!world.isRemote) {
            player.openGui(PhoenixCraft.INSTANCE, GuiHandler.PHOERITE_BED_CABINET_GUI, world, pos.getX(), pos.getY(), pos.getZ());
        }
        return true;
    }

}

 

Gui :

 

public class Gui_Phoerite_Bed_Cabinet extends GuiContainer {

private IInventory playerInv;
private TileEntity_Phoerite_Bed_Cabinet te;

public Gui_Phoerite_Bed_Cabinet(IInventory playerInv, TileEntity_Phoerite_Bed_Cabinet te) {
        super(new Container_Bed_Cabinet(playerInv, te));

        this.playerInv = playerInv;
        this.te = te;
        
        this.xSize = 176;
        this.ySize = 166;
    }

    @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("pc:textures/gui/container/phoerite_bed_cabinet.png"));
        this.drawTexturedModalRect(this.guiLeft, this.guiTop, 0, 0, this.xSize, this.ySize);
    	
    }
    
    @Override
    protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) {
    	
    	String s = this.te.getDisplayName().getUnformattedText();
        this.fontRendererObj.drawString(s, 88 - this.fontRendererObj.getStringWidth(s) / 2, 6, 4210752);            
        this.fontRendererObj.drawString(this.playerInv.getDisplayName().getUnformattedText(), 8, 72, 4210752);      
        
    }

}

 

Gui Handler :

 

public class GuiHandler implements IGuiHandler {

public static final int PHOERITE_BED_CABINET_GUI = 0;

@Override
public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {

	if (ID == PHOERITE_BED_CABINET_GUI) {
        return new Container_Bed_Cabinet(player.inventory, (TileEntity_Phoerite_Bed_Cabinet) world.getTileEntity(new BlockPos(x, y, z)));
	}
    return null;
    
}

@Override
public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {

	if (ID == PHOERITE_BED_CABINET_GUI) {
        return new Gui_Phoerite_Bed_Cabinet(player.inventory, (TileEntity_Phoerite_Bed_Cabinet) world.getTileEntity(new BlockPos(x, y, z)));
	}
	return null;
}
}

 

TE :

 

public class TileEntity_Phoerite_Bed_Cabinet extends TileEntity implements IInventory {

private ItemStack[] inventory;
    private String customName;

    public TileEntity_Phoerite_Bed_Cabinet() {
        this.inventory = new ItemStack[this.getSizeInventory()];
    }

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

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

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

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

@Override
public IChatComponent getDisplayName() {
	return this.hasCustomName() ? new ChatComponentText(this.getName()) : new ChatComponentTranslation(this.getName());
}

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

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

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

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

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

            this.markDirty();
            return itemstack;
        }
    } else {
        return null;
    }
}



@Override
public ItemStack removeStackFromSlot(int index) {
	ItemStack stack = this.getStackInSlot(index);
    this.setInventorySlotContents(index, null);
    return stack;
}

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

    if (stack != null && stack.stackSize > this.getInventoryStackLimit())
        stack.stackSize = this.getInventoryStackLimit();
        
    if (stack != null && stack.stackSize == 0)
        stack = null;

    this.inventory[index] = stack;
    this.markDirty();
}

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

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

@Override
public void openInventory(EntityPlayer player) {

}

@Override
public void closeInventory(EntityPlayer player) {

}

@Override
public boolean isItemValidForSlot(int index, ItemStack stack) {
	return 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 < this.getSizeInventory(); i++)
        this.setInventorySlotContents(i, null);
}

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

    NBTTagList list = new NBTTagList();
    for (int i = 0; i < this.getSizeInventory(); ++i) {
        if (this.getStackInSlot(i) != null) {
            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());
    }
}


@Override
public void readFromNBT(NBTTagCompound nbt) {
    super.readFromNBT(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, ItemStack.loadItemStackFromNBT(stackTag));
    }

    if (nbt.hasKey("CustomName", ) {
        this.setCustomName(nbt.getString("CustomName"));
    }
}

}

 

Container :

 

public class Container_Bed_Cabinet extends Container {

private TileEntity_Phoerite_Bed_Cabinet te;

public Container_Bed_Cabinet(IInventory playerInv, TileEntity_Phoerite_Bed_Cabinet te) {
	this.te = te;

    for (int y = 0; y < 3; ++y) {
        for (int x = 0; x < 3; ++x) {
            this.addSlotToContainer(new Slot(te, x + y * 3, 62 + x * 18, 17 + y * 18));
        }
    }

    for (int y = 0; y < 3; ++y) {
        for (int x = 0; x < 9; ++x) {
            this.addSlotToContainer(new Slot(playerInv, x + y * 9 + 9, 8 + x * 18, 84 + y * 18));
        }
    }

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

    @Override
    public boolean canInteractWith(EntityPlayer playerIn) {
    	return this.te.isUseableByPlayer(playerIn);
    }
    
    @Override
    public ItemStack transferStackInSlot(EntityPlayer playerIn, int fromSlot) {
    	
    	ItemStack previous = null;
        Slot slot = (Slot) this.inventorySlots.get(fromSlot);

        if (slot != null && slot.getHasStack()) {
            ItemStack current = slot.getStack();
            previous = current.copy();
            
            if (fromSlot < 9) {
                if (!this.mergeItemStack(current, 9, 45, true))
                    return null;
            } else {
                if (!this.mergeItemStack(current, 0, 9, false))
                    return null;
            }


            if (current.stackSize == 0)
                slot.putStack((ItemStack) null);
            else
                slot.onSlotChanged();

            if (current.stackSize == previous.stackSize)
                return null;
            slot.onPickupFromSlot(playerIn, current);
        }
        return previous;
    	
    }

}

 

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.



×
×
  • Create New...

Important Information

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