Jump to content

Recommended Posts

Posted

I have been working a Gui for a block and I have 1 last bug that I cannot seem to resolve. Anyway the problem is that the result of my 2x2 crafting square results in the correct item but the item only appears every other time the 2x2 square is updated. I have a System.out statement in the onInventoryChanged() method. This seems to show that this method runs twice everytime an item is placed. Also when the 1st item is put into the 2x2 square the onInventoryChanged() method runs 4 time. This is the only bug in this code I know of.

 

Mod File

package FriscosPlanets.Machines;

import net.minecraft.src.Block;
import net.minecraft.src.Material;
import net.minecraftforge.client.MinecraftForgeClient;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.Init;
import cpw.mods.fml.common.Mod.Instance;
import cpw.mods.fml.common.Mod.PostInit;
import cpw.mods.fml.common.Mod.PreInit;
import cpw.mods.fml.common.SidedProxy;
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.NetworkMod;
import cpw.mods.fml.common.network.NetworkRegistry;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.common.registry.LanguageRegistry;





@Mod(modid = "FriscosPlanetsMachines", name = "Frisco's Planets Machines", version = "1.0.0")


@NetworkMod(clientSideRequired=true, serverSideRequired=false)


public class ModFriscosPlanetsMachines {


@Instance("FriscosPlanetsMachines")
public static ModFriscosPlanetsMachines instance;

@SidedProxy(clientSide = "FriscosPlanets.Machines.client.ProxyClientMachines", serverSide = "FriscosPlanets.Machines.ProxyCommonMachines")
public static ProxyCommonMachines proxy;


public static int startingBlockID = 500;
public static int startingItemID = 1120;
public static int MixerGuiInt = 0;

public static final Block mixer = new BlockMixer(startingBlockID + 1, Material.iron).setHardness(2.0F).setResistance(3.0F).setBlockName("Mixer");


@PreInit
public void preInit(FMLPreInitializationEvent evt){}


@Init
public void init(FMLInitializationEvent evt){


	proxy.registerRenderers();


	//Mixer
	GameRegistry.registerBlock(mixer);
	LanguageRegistry.addName(mixer, "Mixer");
	GameRegistry.registerTileEntity(TileEntityMixer.class, "Mixer");
	NetworkRegistry.instance().registerGuiHandler(this, new GuiHandler());
	 //MinecraftForgeClient.registerItemRenderer(startingBlockID + 1, new TileEntityMixerRenderer());

}

@PostInit
public void postInit(FMLPostInitializationEvent evt){}


}

 

 

Block File

package FriscosPlanets.Machines;

import java.util.List;

import FriscosPlanets.Core.ModFriscosPlanetsCore;
import FriscosPlanets.Core.ProxyCommonCore;

import cpw.mods.fml.common.Side;
import cpw.mods.fml.common.asm.SideOnly;
import net.minecraft.src.Block;
import net.minecraft.src.BlockContainer;
import net.minecraft.src.CreativeTabs;
import net.minecraft.src.EntityLiving;
import net.minecraft.src.EntityPlayer;
import net.minecraft.src.InventoryPlayer;
import net.minecraft.src.ItemStack;
import net.minecraft.src.Material;
import net.minecraft.src.MathHelper;
import net.minecraft.src.Slot;
import net.minecraft.src.TileEntity;
import net.minecraft.src.World;

public class BlockMixer extends BlockContainer{

public BlockMixer(int par1, Material par2) {
	super(par1, par2);
	this.setCreativeTab(ModFriscosPlanetsCore.tabFriscosPlanets);
	this.setRequiresSelfNotify();
}

public String getTextureFile(){
	return ProxyCommonMachines.BLOCK_PNG;
}


public int getBlockTextureFromSideAndMetadata(int side, int metadata){

	switch(metadata){
	case 0: 
		switch(side){//North
		case 2: return 11;
		default: return 10;
		}
	case 1:
		switch(side){//South
		case 3: return 11;
		default: return 10;
		}
	case 2:
		switch(side){//East
		case 4: return 11;
		default: return 10;
		}
	case 3: 
		switch(side){//West
		case 5: return 11;
		default: return 10;
		}
	}
	return 0;
}

@SideOnly(Side.CLIENT)
    public void getSubBlocks(int par1, CreativeTabs tab, List list){
	list.add(new ItemStack(par1, 1, 1));
    }


public void onBlockPlacedBy(World par1World, int par2, int par3, int par4, EntityLiving par5EntityLiving){

	int var6 = MathHelper.floor_double((double)(par5EntityLiving.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;

        if (var6 == 0)
        {
            par1World.setBlockMetadataWithNotify(par2, par3, par4, 0);
        }

        if (var6 == 1)
        {
            par1World.setBlockMetadataWithNotify(par2, par3, par4, 3);
        }

        if (var6 == 2)
        {
            par1World.setBlockMetadataWithNotify(par2, par3, par4, 1);
        }

        if (var6 == 3)
        {
            par1World.setBlockMetadataWithNotify(par2, par3, par4, 2);
        }

}


public boolean onBlockActivated(World world, int xPos, int yPos, int zPos, EntityPlayer player, int par1, float par2, float par3, float par4){

	if(world.isRemote){
		return true;
	}else{
		player.openGui(ModFriscosPlanetsMachines.instance, ModFriscosPlanetsMachines.MixerGuiInt, world, xPos, yPos, zPos);
		return true;
	}
}

  @Override
      public TileEntity createNewTileEntity(World world) {
              return new TileEntityMixer();
      }

}

 

Container

 

package FriscosPlanets.Machines;

import net.minecraft.src.Container;
import net.minecraft.src.EntityPlayer;
import net.minecraft.src.IInventory;
import net.minecraft.src.InventoryCraftResult;
import net.minecraft.src.InventoryCrafting;
import net.minecraft.src.InventoryPlayer;
import net.minecraft.src.Slot;
import net.minecraft.src.SlotCrafting;
import net.minecraft.src.TileEntity;

public class ContainerMixer extends Container{

protected IInventory tileEntity;

public ContainerMixer (InventoryPlayer inventoryPlayer, TileEntityMixer te){
        tileEntity = te;
        
        bindPlayerInventory(inventoryPlayer);
        
        addSlotToContainer(new Slot(tileEntity, 0, 38, 26));
        addSlotToContainer(new Slot(tileEntity, 1, 56, 26));
        addSlotToContainer(new Slot(tileEntity, 2, 38, 44));
        addSlotToContainer(new Slot(tileEntity, 3, 56, 44));
        addSlotToContainer(new SlotMixerResult((TileEntityMixer) tileEntity, 4, 124, 35));
        
        bindPlayerInventory(inventoryPlayer);
        
}

@Override
public boolean canInteractWith(EntityPlayer var1) {
	return true;
}




    protected void bindPlayerInventory(InventoryPlayer inventoryPlayer) {
        for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 9; j++) {
                        addSlotToContainer(new Slot(inventoryPlayer, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
                }
        }

        for (int i = 0; i < 9; i++) {
                addSlotToContainer(new Slot(inventoryPlayer, i, 8 + i * 18, 142));
        }
}

}

 

GuiHandler

package FriscosPlanets.Machines;

import FriscosPlanets.Machines.client.GuiMixer;
import cpw.mods.fml.common.network.IGuiHandler;
import net.minecraft.src.EntityPlayer;
import net.minecraft.src.TileEntity;
import net.minecraft.src.World;

public class GuiHandler implements IGuiHandler{

@Override
public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
	if(ID == 0){
		return new ContainerMixer(player.inventory, new TileEntityMixer());
	}else{
		return null;
	}
}

public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z){
	if(ID == 0){
		return new GuiMixer(new ContainerMixer(player.inventory, new TileEntityMixer()));
	}else{
		return null;
	}
}
}

 

MixerManager

package FriscosPlanets.Machines;

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

import FriscosPlanets.Core.ModFriscosPlanetsCore;

import net.minecraft.src.Block;
import net.minecraft.src.ItemStack;

public class MixerManager{

private List <ItemStack[]> recipes = new ArrayList<ItemStack[]>();
public static MixerManager instance = new MixerManager();


/**
 * recipes in here
 */
private MixerManager(){

	this.addRecipe(new ItemStack(ModFriscosPlanetsCore.sedimentmixture, 1), new ItemStack[]{ new ItemStack(Block.sand), new ItemStack(Block.gravel), new ItemStack(Block.blockClay), new ItemStack(Block.cobblestone)});
	this.addRecipe(new ItemStack(Block.dirt, 1), new ItemStack[]{new ItemStack(Block.sand)});

}

private void addRecipe(ItemStack result, ItemStack[] materials){

	ItemStack recipe[] = new ItemStack[materials.length + 1];
	recipe[0] = result;

	for(int i = 0; i < materials.length; i++){
		recipe[i + 1] = materials[i];
	}

	recipes.add(recipe);
}


public ItemStack[] getResult(TileEntityMixer tem){
	ItemStack[] result = null;
	ItemStack[] temItems = tem.getInv();								//sets items in mixer

	ArrayList<ItemStack> avalibleItems = new ArrayList<ItemStack>();		
	for(int i = 0; i < temItems.length - 1; i++){
		if(temItems[i] != null){
			avalibleItems.add(temItems[i]);								//gets rid of all null items in the mixer
		}
	}

	if(avalibleItems.isEmpty()){										//if there is nothing in the mixer return nothing
		return null;	
	}

	for(ItemStack[] recipe : recipes){									//goes through list of recipes
		if(recipe.length - 1 != avalibleItems.size()){
			continue;
		}

		if(temItems[4] != null && temItems[4] != recipe[0]){			//checks contents of resulting slot against expected result
			continue;
		}

		ArrayList<ItemStack> tempRecipe = new ArrayList<ItemStack>();	

		for(ItemStack item : recipe){									//copy of recipe items list
			tempRecipe.add(item);
		}

		tempRecipe.remove(0);											//removes result from recipe list

		ArrayList<ItemStack> tempAval = new ArrayList<ItemStack>();
		for(ItemStack item : avalibleItems){
			tempAval.add(item);											//copy of items in mixer list
		}	
		for(int i = 1; i < recipe.length; i++){						//removes the ItemStacks if they are the same
			ItemStack item = recipe[i];
			for(int i1 = 0; i1 < avalibleItems.size(); i1++){
				ItemStack item1 = avalibleItems.get(i1);
				if(item.getItem().shiftedIndex == item1.getItem().shiftedIndex && item.getItemDamage() == item1.getItemDamage()){
					tempRecipe.remove(item);
					tempAval.remove(item1);
					break;
				}
			}
		}

		if(tempRecipe.isEmpty() && tempAval.isEmpty()){					//if the lists are both empty then it matches the recipe
			result = recipe;
		}

	}

	return result;

}
}

 

Tile Entity

 

package FriscosPlanets.Machines;

import java.io.ObjectOutputStream.PutField;


import net.minecraft.src.EntityPlayer;
import net.minecraft.src.IInventory;
import net.minecraft.src.ItemStack;
import net.minecraft.src.NBTTagCompound;
import net.minecraft.src.NBTTagList;
import net.minecraft.src.TileEntity;

public class TileEntityMixer extends TileEntity implements IInventory{

private ItemStack[] inv;
private ItemStack[] currentRecipe;

    public TileEntityMixer(){
            inv = new ItemStack[5];
    }
    
    public ItemStack[] getInv(){
    	return inv;
    }
    
    @Override
    public int getSizeInventory() {
            return inv.length;
    }

    @Override
    public ItemStack getStackInSlot(int slot) {
            return inv[slot];
    }
    
    public void reduceAllSlots(){
    	decrStackSize(0, 1);
    	decrStackSize(1, 1);
    	decrStackSize(2, 1);
    	decrStackSize(3, 1);
    }
    
    
    @Override
    public void setInventorySlotContents(int slot, ItemStack stack){
            inv[slot] = stack;
            if (stack != null && stack.stackSize > getInventoryStackLimit()) {
                    stack.stackSize = getInventoryStackLimit();
            }               
    }

    @Override
    public ItemStack decrStackSize(int slot, int amt) {
            ItemStack stack = getStackInSlot(slot);
            if (stack != null) {
                    if (stack.stackSize <= amt) {
                            setInventorySlotContents(slot, null);
                    } else {
                            stack = stack.splitStack(amt);
                            if (stack.stackSize == 0) {
                                    setInventorySlotContents(slot, null);
                            }
                    }
            }
            return stack;
    }

    @Override
    public ItemStack getStackInSlotOnClosing(int slot) {
            ItemStack stack = getStackInSlot(slot);
            if (stack != null) {
                    setInventorySlotContents(slot, null);
            }
            return stack;
    }
    
    @Override
    public int getInventoryStackLimit() {
            return 64;
    }

    @Override
    public boolean isUseableByPlayer(EntityPlayer player) {
            return worldObj.getBlockTileEntity(xCoord, yCoord, zCoord) == this && player.getDistanceSq(xCoord + 0.5, yCoord + 0.5, zCoord + 0.5) < 64;
    }

    @Override
    public void openChest() {}

    @Override
    public void closeChest() {}
    
    @Override
    public void readFromNBT(NBTTagCompound tagCompound) {
            super.readFromNBT(tagCompound);
            
            NBTTagList tagList = tagCompound.getTagList("Inventory");
            for (int i = 0; i < tagList.tagCount(); i++) {
                    NBTTagCompound tag = (NBTTagCompound) tagList.tagAt(i);
                    byte slot = tag.getByte("Slot");
                    if (slot >= 0 && slot < inv.length) {
                            inv[slot] = ItemStack.loadItemStackFromNBT(tag);
                    }
            }
    }
    
    @Override
    public void onInventoryChanged() {
    	super.onInventoryChanged();
    	checkForRecipe();
    	if(inv[4] != null){
    		//System.out.println(inv[4].getItem().shiftedIndex);
    	}else{
    		//System.out.println("empty");
    	}
    	
    }
  
    
    @Override
    public boolean canUpdate() {
    	return true;
    }
    
    
    
    public void checkForRecipe(){
    	currentRecipe = MixerManager.instance.getResult(this);
    	if(currentRecipe != null){
    		System.out.println("should set");
    		setInventorySlotContents(4, new ItemStack(currentRecipe[0].getItem(), currentRecipe[0].stackSize));
    	}else{
    		setInventorySlotContents(4, null);
    	}
    }

    
    
    @Override
    public void writeToNBT(NBTTagCompound tagCompound) {
            super.writeToNBT(tagCompound);
                            
            NBTTagList itemList = new NBTTagList();
            for (int i = 0; i < inv.length; i++) {
                    ItemStack stack = inv[i];
                    if (stack != null) {
                            NBTTagCompound tag = new NBTTagCompound();
                            tag.setByte("Slot", (byte) i);
                            stack.writeToNBT(tag);
                            itemList.appendTag(tag);
                    }
            }
            tagCompound.setTag("Inventory", itemList);
    }

@Override
public String getInvName() {
	return "Mixer";
}

}

 

Posted

I don't know too much about GUI's and things involved with the creation of them, but I believe that the reason your method is running twice may be caused by the fact that some methods are run by the client AND the server - not independently.

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.