Posted May 30, 201411 yr My custom furnace that i'm making is freezing my game!! when i right click on it it will have major lag! And. don't say i'm missing methods, because i am, i'm not finished with it yet, so don't say i'm missing methods please! I'm experienced with java but not too TOO advanced. Main mod class: package net.CraigTheMailman.CraftyMod; import net.CraigTheMailman.CraftyMod.blocks.AlloyFurnace; import net.CraigTheMailman.CraftyMod.handler.GuiHandler; import net.CraigTheMailman.CraftyMod.tileentity.TileEntityAlloyFurnace; import net.minecraft.block.Block; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.item.Item; import cpw.mods.fml.common.Mod; import cpw.mods.fml.common.Mod.EventHandler; import cpw.mods.fml.common.Mod.Instance; import cpw.mods.fml.common.event.FMLInitializationEvent; import cpw.mods.fml.common.event.FMLPostInitializationEvent; import cpw.mods.fml.common.event.FMLPreInitializationEvent; import cpw.mods.fml.common.network.NetworkRegistry; import cpw.mods.fml.common.registry.GameRegistry; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; @Mod(modid = CraftyMod.modid, name = "Crafty Mod", version = CraftyMod.version) public class CraftyMod { public static final String modid = "craftymod"; public static final String version = "Alpha v0.1"; //Creative Tab public static CreativeTabs tabC; @Instance(modid) public static CraftyMod instance; //Items //Blocks //Machines public static Block blockAlloyFurnaceIdle; public static Block blockAlloyFurnaceActive; public static final int guiIDAlloyFurnace = 0; @EventHandler public void PreInit(FMLPreInitializationEvent preEvent) { //Creative Tab tabC = new CreativeTabs("CraftyMod") { @Override @SideOnly(Side.CLIENT) public Item getTabIconItem() { return Item.getItemFromBlock(blockAlloyFurnaceIdle); } }; //Initialization //AlloyFurnace blockAlloyFurnaceIdle = new AlloyFurnace(false).setCreativeTab(tabC).setBlockName("AlloyFurnace_off"); blockAlloyFurnaceActive = new AlloyFurnace(true).setBlockName("AlloyFurnace_on"); //GameReg //Machines GameRegistry.registerBlock(blockAlloyFurnaceIdle, "AlloyFurnace_off"); GameRegistry.registerBlock(blockAlloyFurnaceActive, "AlloyFurnace_on"); } @EventHandler public void Init(FMLInitializationEvent event) { GameRegistry.registerTileEntity(TileEntityAlloyFurnace.class, "AlloyFurnace"); NetworkRegistry.INSTANCE.registerGuiHandler(this, new GuiHandler()); } @EventHandler public void PostInit(FMLPostInitializationEvent postEvent) { } } Gui Handler: package net.CraigTheMailman.CraftyMod.handler; import net.CraigTheMailman.CraftyMod.CraftyMod; import net.CraigTheMailman.CraftyMod.container.ContainerAlloyFurnace; import net.CraigTheMailman.CraftyMod.tileentity.TileEntityAlloyFurnace; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.ResourceLocation; import net.minecraft.world.World; import cpw.mods.fml.common.network.IGuiHandler; public class GuiHandler implements IGuiHandler { @Override public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) { TileEntity entity = world.getTileEntity(x, y, z); if(entity != null) { switch(ID) { case CraftyMod.guiIDAlloyFurnace: if(entity instanceof TileEntityAlloyFurnace) { return new ContainerAlloyFurnace(player.inventory,(TileEntityAlloyFurnace)entity); } return null; } } return null; } @Override public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) { TileEntity entity = world.getTileEntity(x, y, z); if(entity != null) { switch(ID) { case CraftyMod.guiIDAlloyFurnace: if(entity instanceof TileEntityAlloyFurnace) { return new GuiAlloyFurnace(player.inventory,(TileEntityAlloyFurnace)entity); } return null; } } return null; } } Gui class for block: package net.CraigTheMailman.CraftyMod.gui; import org.lwjgl.opengl.GL11; import net.CraigTheMailman.CraftyMod.CraftyMod; import net.CraigTheMailman.CraftyMod.container.ContainerAlloyFurnace; import net.CraigTheMailman.CraftyMod.tileentity.TileEntityAlloyFurnace; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.inventory.GuiContainer; import net.minecraft.client.resources.I18n; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.util.ResourceLocation; public class GuiAlloyFurnace extends GuiContainer { public static final ResourceLocation bground = new ResourceLocation(CraftyMod.modid + ":" + "textures/gui/guialloyfurnace.png"); public TileEntityAlloyFurnace alloyFurnace; public GuiAlloyFurnace(InventoryPlayer inventoryPlayer, TileEntityAlloyFurnace entity) { super(new ContainerAlloyFurnace(inventoryPlayer, entity)); this.alloyFurnace = entity; this.xSize = 176; this.ySize = 166; } @Override public void drawGuiContainerForegroundLayer(int par1, int par2) { String name = this.alloyFurnace.hasCustomInventoryName() ? this.alloyFurnace.getInventoryName() : I18n.format(this.alloyFurnace.getInventoryName(), new Object[0]); this.fontRendererObj.drawString(name, this.xSize / 2 - this.fontRendererObj.getStringWidth(name) / 2, 6, 4210752); this.fontRendererObj.drawString(I18n.format("container.inventory", new Object[0]), 128, this.ySize - 96 + 2, 4210752); } @Override protected void drawGuiContainerBackgroundLayer(float var1, int var2, int var3) { GL11.glColor4f(1F, 1F, 1F, 1F); Minecraft.getMinecraft().getTextureManager().bindTexture(bground); drawTexturedModalRect(guiLeft, guiTop, 0, 0, xSize, ySize); } } Block class: package net.CraigTheMailman.CraftyMod.blocks; import net.CraigTheMailman.CraftyMod.CraftyMod; import net.CraigTheMailman.CraftyMod.tileentity.TileEntityAlloyFurnace; import net.minecraft.block.Block; import net.minecraft.block.BlockContainer; import net.minecraft.block.material.Material; import net.minecraft.client.renderer.texture.IIconRegister; import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.IIcon; import net.minecraft.util.MathHelper; import net.minecraft.world.World; import cpw.mods.fml.common.network.internal.FMLNetworkHandler; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; public class AlloyFurnace extends BlockContainer { private final boolean isActive; @SideOnly(Side.CLIENT) private IIcon iconFront; @SideOnly(Side.CLIENT) private IIcon iconTop; private static boolean keepInventory; public AlloyFurnace(boolean isActive) { super(Material.iron); this.isActive = isActive; } @SideOnly(Side.CLIENT) public void registerBlockIcons(IIconRegister iconRegister) { this.blockIcon = iconRegister.registerIcon(CraftyMod.modid + ":" + "Alloy_side"); this.iconFront = iconRegister.registerIcon(CraftyMod.modid + ":" + "Alloy" + (this.isActive ? "_on" : "_off")); this.iconTop = iconRegister.registerIcon(CraftyMod.modid + ":" + "Alloy_top_" + (this.isActive ? "on" : "off")); } @Override @SideOnly(Side.CLIENT) public IIcon getIcon(int side, int metadata) { return metadata == 0 && side == 3 ? this.iconFront : side == 1 ? this.iconTop : (side == 0 ? this.iconTop : (side == metadata ? this.iconFront : this.blockIcon)); } public Item getItemDropped(World world, int x, int y, int z) { return Item.getItemFromBlock(CraftyMod.blockAlloyFurnaceIdle); } @Override public void onBlockAdded(World world, int x, int y, int z) { super.onBlockAdded(world, x, y, z); this.setDefaultDirection(world, x, y, z); } private void setDefaultDirection(World world, int x, int y, int z) { if(!world.isRemote) { Block b1 = world.getBlock(x, y, z - 1); Block b2 = world.getBlock(x, y, z + 1); Block b3 = world.getBlock(x - 1, y, z); Block b4 = world.getBlock(x + 1, y, z); byte b0 = 3; if(b1.func_149730_j() && !b2.func_149730_j()) { b0 = 3; } if(b2.func_149730_j() && !b1.func_149730_j()) { b0 = 2; } if(b3.func_149730_j() && !b4.func_149730_j()) { b0 = 5; } if(b4.func_149730_j() && !b3.func_149730_j()) { b0 = 4; } world.setBlockMetadataWithNotify(x, y, z, b0, 2); } } public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float hitX, float hitY, float hitZ) { if(!world.isRemote) { FMLNetworkHandler.openGui(player, CraftyMod.instance, CraftyMod.guiIDAlloyFurnace, world, x, y, z); } return true; } @Override public TileEntity createNewTileEntity(World var1, int var2) { return new TileEntityAlloyFurnace(); } //TODO randomDisplayTick public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase entityplayer, ItemStack itemstack) { int l = MathHelper.floor_double((double)(entityplayer.rotationYaw * 4.0F / 360F) + 0.5D) & 3; if(l == 0) { world.setBlockMetadataWithNotify(x, y, z, 2, 2); } if(l == 1) { world.setBlockMetadataWithNotify(x, y, z, 5, 2); } if(l == 2) { world.setBlockMetadataWithNotify(x, y, z, 3, 2); } if(l == 3) { world.setBlockMetadataWithNotify(x, y, z, 4, 2); } if(itemstack.hasDisplayName()) { ((TileEntityAlloyFurnace)world.getTileEntity(x, y, z)).setGuiDisplayName(itemstack.getDisplayName()); } } public static void updateAlloyFurnaceBlockState(boolean active, World worldObj, int xCoord, int yCoord, int zCoord) { int i = worldObj.getBlockMetadata(xCoord, yCoord, zCoord); TileEntity tileentity = worldObj.getTileEntity(xCoord, yCoord, zCoord); keepInventory = true; if(active) { worldObj.setBlock(xCoord, yCoord, zCoord, CraftyMod.blockAlloyFurnaceActive); }else { worldObj.setBlock(xCoord, yCoord, zCoord, CraftyMod.blockAlloyFurnaceIdle); } keepInventory = false; worldObj.setBlockMetadataWithNotify(xCoord, yCoord, zCoord, i, 2); if(tileentity != null) { tileentity.validate(); worldObj.setTileEntity(xCoord, yCoord, zCoord, tileentity); } } } Container: package net.CraigTheMailman.CraftyMod.container; import net.CraigTheMailman.CraftyMod.tileentity.TileEntityAlloyFurnace; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.Container; import net.minecraft.inventory.ICrafting; import net.minecraft.inventory.Slot; import net.minecraft.inventory.SlotFurnace; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; public class ContainerAlloyFurnace extends Container { private TileEntityAlloyFurnace alloyFurnace; public int lastBurnTime; public int lastCurrentItemBurnTime; public int lastCookTime; public ContainerAlloyFurnace(InventoryPlayer inventory, TileEntityAlloyFurnace tileentity) { this.alloyFurnace = tileentity; this.addSlotToContainer(new Slot(tileentity, 0, 56, 35)); this.addSlotToContainer(new Slot(tileentity, 1, 8, 62)); this.addSlotToContainer(new SlotFurnace(inventory.player, tileentity, 2, 116, 35)); for(int i = 0; i < 3; i++) { for(int j = 0; j < 9; i++) { this.addSlotToContainer(new Slot(inventory, j + i * 9 + 9, 8 + j * 18, 94 + i * 18)); } } for(int i = 0; i < 9; i++) { this.addSlotToContainer(new Slot(inventory, i, 8 + i * 18, 142)); } } @Override public void addCraftingToCrafters(ICrafting icrafting) { super.addCraftingToCrafters(icrafting); icrafting.sendProgressBarUpdate(this, 0, this.alloyFurnace.cookTime); icrafting.sendProgressBarUpdate(this, 1, this.alloyFurnace.burnTime); icrafting.sendProgressBarUpdate(this, 2, this.alloyFurnace.currentItemBurnTime); } @Override public void detectAndSendChanges() { super.detectAndSendChanges(); for(int i = 0; i < this.crafters.size(); i++) { ICrafting icrafting = (ICrafting) this.crafters.get(i); if(this.lastCookTime != this.alloyFurnace.cookTime) { icrafting.sendProgressBarUpdate(this, 0, this.alloyFurnace.cookTime); } if(this.lastBurnTime != this.alloyFurnace.burnTime) { icrafting.sendProgressBarUpdate(this, 1, this.alloyFurnace.burnTime); } if(this.lastCurrentItemBurnTime != this.alloyFurnace.currentItemBurnTime) { icrafting.sendProgressBarUpdate(this, 2, this.alloyFurnace.currentItemBurnTime); } } this.lastCookTime = this.alloyFurnace.cookTime; this.lastBurnTime = this.alloyFurnace.burnTime; this.lastCurrentItemBurnTime = this.alloyFurnace.currentItemBurnTime; } @Override @SideOnly(Side.CLIENT) public void updateProgressBar(int slot, int newValue) { } @Override public boolean canInteractWith(EntityPlayer var1) { return true; } } Tile Entity: package net.CraigTheMailman.CraftyMod.tileentity; import net.CraigTheMailman.CraftyMod.blocks.AlloyFurnace; import net.minecraft.block.Block; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.init.Blocks; import net.minecraft.init.Items; import net.minecraft.inventory.ISidedInventory; import net.minecraft.item.Item; import net.minecraft.item.ItemBlock; import net.minecraft.item.ItemStack; import net.minecraft.item.crafting.FurnaceRecipes; import net.minecraft.tileentity.TileEntity; import cpw.mods.fml.common.registry.GameRegistry; public class TileEntityAlloyFurnace extends TileEntity implements ISidedInventory { private String localizeName; private ItemStack[] slots = new ItemStack[3]; public int furnaceSpeed = 200; public int burnTime; public int currentItemBurnTime; public int cookTime; private static final int[] slots_top = new int[]{0}; private static final int[] slots_bottom = new int[]{2, 1}; private static final int[] slots_side = new int[]{1}; public void setGuiDisplayName(String displayName) { this.localizeName = displayName; } public String getInventoryName() { return this.hasCustomInventoryName() ? this.localizeName : "container.alloyFurnace"; } public boolean hasCustomInventoryName() { return this.localizeName != null && this.localizeName.length() > 0; } public int getSizeInventory() { return this.slots.length; } @Override public ItemStack getStackInSlot(int var1) { return this.slots[var1]; } @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 null; } @Override public ItemStack getStackInSlotOnClosing(int i) { if(this.slots != null) { ItemStack itemstack = this.slots; this.slots = null; return itemstack; } return null; } @Override public void setInventorySlotContents(int i, ItemStack itemstack) { this.slots = itemstack; if(itemstack != null && itemstack.stackSize > this.getInventoryStackLimit()) { itemstack.stackSize = this.getInventoryStackLimit(); } } @Override public int getInventoryStackLimit() { return 64; } @Override public boolean isUseableByPlayer(EntityPlayer player) { return this.worldObj.getTileEntity(this.xCoord, this.yCoord, this.zCoord) != this ? false : player.getDistanceSq((double)this.xCoord + 0.5D, (double)this.yCoord + 0.5D, (double)this.zCoord + 0.5D) <= 64.0D; } @Override public void openInventory() {} @Override public void closeInventory() {} @Override public boolean isItemValidForSlot(int i, ItemStack itemstack) { return i == 2 ? false :(i == 1 ? isItemFuel(itemstack) : true); } public static boolean isItemFuel(ItemStack itemstack) { return getItemBurnTime(itemstack) > 0; } private static int getItemBurnTime(ItemStack itemstack) { if(itemstack == null) { return 0; }else { Item item = itemstack.getItem(); if(item instanceof ItemBlock && Block.getBlockFromItem(item) != Blocks.air) { Block block = Block.getBlockFromItem(item); //if(item == (item goes here) return (ticks); return GameRegistry.getFuelValue(itemstack); } } return 0; } public boolean isBurning() { return this.burnTime > 0; } @Override public void updateEntity() { boolean flag = this.burnTime > 0; boolean flag1 = false; if(this.isBurning()) { this.burnTime--; } if(!this.worldObj.isRemote) { if(this.burnTime == 0 && this.canSmelt()) { this.currentItemBurnTime = this.burnTime = getItemBurnTime(this.slots[1]); if(this.isBurning()) { flag1 = true; if(this.slots[1] != null) { this.slots[1].stackSize--; if(this.slots[1].stackSize == 0) { this.slots[1] = this.slots[1].getItem().getContainerItem(this.slots[1]); } } } } if(this.isBurning() && this.canSmelt()) { this.cookTime++; if(this.cookTime == this.furnaceSpeed) { this.cookTime = 0; this.smeltItem(); flag1 = true; } }else { this.cookTime = 0; } if(flag != this.isBurning()) { flag1 = true; AlloyFurnace.updateAlloyFurnaceBlockState(this.burnTime > 0, this.worldObj, this.xCoord, this.yCoord, this.zCoord); } } if(flag1) { this.markDirty(); } } public boolean canSmelt() { if(this.slots[0] == null) { return false; }else { ItemStack itemstack = FurnaceRecipes.smelting().getSmeltingResult(this.slots[0]); if(itemstack == null) return false; if(this.slots[2] == null) return true; if(this.slots[2].isItemEqual(itemstack)) return false; int result = this.slots[2].stackSize + itemstack.stackSize; return (result <= getInventoryStackLimit() && result <= itemstack.getMaxStackSize()); } } public void smeltItem() { if(this.canSmelt()) { ItemStack itemstack = FurnaceRecipes.smelting().getSmeltingResult(this.slots[0]); if(this.slots[2] == null) { this.slots[2] = itemstack.copy(); }else if(this.slots[2].isItemEqual(itemstack)) { this.slots[2].stackSize += itemstack.stackSize; } this.slots[0].stackSize--; if(this.slots[0].stackSize <= 0) { this.slots[0] = null; } } } @Override public int[] getAccessibleSlotsFromSide(int var1) { return var1 == 0 ? slots_bottom : (var1 == 1 ? slots_top : slots_side); } @Override public boolean canInsertItem(int i, ItemStack itemstack, int j) { return this.isItemValidForSlot(i, itemstack); } @Override public boolean canExtractItem(int i, ItemStack itemstack, int j) { return j != 0 || i != 1 || itemstack.getItem() == Items.bucket; } }
May 30, 201411 yr One thing to note. Don't register or create blocks/items during class construction <clinit>. You need to do them like you did with your creative tab. Example: public static Block block; @EventHandler public void preInit(PreInitializationEvent event) { block = new TutorialBlock().setUnlocalizedName("tutBlock"); } //Note: This is an "example."\\
May 31, 201411 yr Author So I am supposed to put the public static Block block Under a @sideonly? Or am I supposed to Just put the public static Block block Under the preinit?
May 31, 201411 yr Author @diesieben07 then what is my problem with my furnace code, besides missing methods of course cause i'm not finished with it
May 31, 201411 yr Author @diesieben07 By freezing I mean the furnace will not open the GUI, then it overloads my memory With java heapspace (Already know what that means)
May 31, 201411 yr Then show the crash report... Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support. 1.12 -> 1.13 primer by williewillus. 1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support. http://www.howoldisminecraft1710.today/
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.