1.12.2 Pouvoir faire un craft avec un stack d'un item



  • Bonsoir,

    Je revient à nouveau vers vous non pas pour un problème, mais j'aimerais pouvoir faire en sorte que mon craft ce réalise avec pas un seul item dans chaque slot, mais avec un stack de le l'item dans chaque slot.

    Voici le code :

    import java.util.Iterator;
    import java.util.Map.Entry;
    
    import fr.vesperia.vesperiamod.init.ModItems;
    import net.minecraft.item.Item;
    import net.minecraft.item.ItemStack;
    
    public class RecipesParticleMachine
    {
    	private static final HashMap <ItemStack[], ItemStack>recipes = new HashMap<ItemStack[], ItemStack>();
    	static 
    	{
    		addRecipe(ModItems.litchi, ModItems.litchi, ModItems.pyrite);
    	}
    	
    	private static void addRecipe(Item ingredient1, Item ingredient2, Item resultat1) 
    	{
    		addRecipe(new ItemStack(ingredient1), new ItemStack(ingredient2), new ItemStack(resultat1));
    	}
    
    	private static void addRecipe(ItemStack ingredient1, ItemStack ingredient2, ItemStack resultat1) 
    	{
    		recipes.put(new ItemStack[]{ingredient1, ingredient2}, resultat1);
    	}
    	
    	private static boolean areKeysEqual(ItemStack[] key1, ItemStack[] key2) 
    	{
    	    if(key1.length != key2.length) return false;
    	    for(int i = 0; i < key1.length; i++) 
    	    {
    	        ItemStack s1 = key1[i];
    	        ItemStack s2 = key2[i];
    	        if(s1.isEmpty() && !s2.isEmpty()) return false;
    	        if(!s1.isEmpty() && s2.isEmpty()) return false;
    	        if(s1.getItem() != s2.getItem()) return false;
    	        if(s1.getItemDamage() != s2.getItemDamage()) return false;
    	    }
    	    return true;
    	}
    	
    	public static ItemStack getRecipeResult(ItemStack[] ingredients) 
    	{
    	    Iterator<Entry<ItemStack[], ItemStack>> it = recipes.entrySet().iterator();
    	    while(it.hasNext()) 
    	    {
    	        Entry <ItemStack[], ItemStack>entry = it.next();
    	        if(areKeysEqual(entry.getKey(), ingredients)) 
    	        {
    	            return entry.getValue();
    	        }
    	    }
    	    return null;
    	}
    }
    
    import fr.vesperia.vesperiamod.recipes.RecipesAlloyMachine;
    import fr.vesperia.vesperiamod.recipes.RecipesParticleMachine;
    import net.minecraft.entity.player.EntityPlayer;
    import net.minecraft.entity.player.InventoryPlayer;
    import net.minecraft.inventory.Container;
    import net.minecraft.inventory.ItemStackHelper;
    import net.minecraft.item.Item;
    import net.minecraft.item.ItemStack;
    import net.minecraft.nbt.NBTTagCompound;
    import net.minecraft.tileentity.TileEntityLockable;
    import net.minecraft.util.ITickable;
    import net.minecraft.util.NonNullList;
    import net.minecraftforge.event.ForgeEventFactory;
    
    public class TileEntityParticleMachine extends TileEntityLockable implements ITickable 
    {
    	private NonNullList<ItemStack> stacks = NonNullList.withSize(4, ItemStack.EMPTY);
    	private String customName;
    	private int	timePassed = 0;
    	private static int	burningTimeLeft	= 0;
    	
    	@Override
    	public void readFromNBT(NBTTagCompound compound) 
    	{
    		super.readFromNBT(compound);
    		this.stacks = NonNullList.<ItemStack>withSize(this.getSizeInventory(), ItemStack.EMPTY);
    		ItemStackHelper.loadAllItems(compound, this.stacks);
    		if (compound.hasKey("CustomName", 4)) 
    		{
    			this.customName = compound.getString("CustomName");
    		}
    		this.burningTimeLeft = compound.getInteger("burningTimeLeft");
    		this.timePassed = compound.getInteger("timePassed");
    	}
    	
    	@Override
    	public NBTTagCompound writeToNBT(NBTTagCompound compound) 
    	{
    		super.writeToNBT(compound);
    		ItemStackHelper.saveAllItems(compound, this.stacks);
    		if (this.hasCustomName()) 
    		{
    			compound.setString("CustomName", this.customName);
    		}
    		compound.setInteger("burningTimeLeft", this.burningTimeLeft);
    		compound.setInteger("timePassed", this.timePassed);
    		return compound;
    	}
    	
    	@Override
    	public boolean hasCustomName()
    	{
    		return false;
    	}
    
    	@Override
    	public String getName()
    	{
    		return hasCustomName() ? this.customName : "tile.particle_machine";
    	}
    	public void setCustomName(String name) 
    	{
    		this.customName = name;
    	}
    	
    	public static boolean isItemFuel(ItemStack stack)
        {
            return getItemBurnTime(stack) > 0;
        }
    
    	@Override
    	public int getField(int id) 
    	{
    		switch (id) 
    		{
    		case 0:
    			return this.burningTimeLeft;
    		case 1:
    			return this.timePassed;
    		}
    		return 0;
    	}
    	
    	@Override
    	public void setField(int id, int value) 
    	{
    		switch (id) 
    		{
    		case 0:
    			this.burningTimeLeft = value;
    			break;
    		case 1:
    			this.timePassed = value;
    		}
    	}
    
    	@Override
    	public int getFieldCount() 
    	{
    		return 2;
    	}
    	
    	@Override
    	public int getSizeInventory() 
    	{
    		return this.stacks.size();
    	}
    
    	@Override
    	public ItemStack getStackInSlot(int index) 
    	{
    		return this.stacks.get(index);
    	}
    
    	@Override
    	public ItemStack decrStackSize(int index, int count) 
    	{
    		return ItemStackHelper.getAndSplit(this.stacks, index, count);
    	}
    	
    	@Override
    	public ItemStack removeStackFromSlot(int index)
    	{
    		return ItemStackHelper.getAndRemove(stacks, index);
    	}
    
    	@Override
    	public void setInventorySlotContents(int index, ItemStack stack) 
    	{
    		this.stacks.set(index, stack);
    		if (stack.getCount() > this.getInventoryStackLimit()) 
    		{
    			stack.setCount(this.getInventoryStackLimit());
    		}
    	}
    
    	@Override
    	public int getInventoryStackLimit() 
    	{
    		return 64;
    	}
    	
    	@Override
    	public boolean isEmpty() 
    	{
    		for(ItemStack stack : this.stacks)
    		{
    			if (!stack.isEmpty()) 
    			{
    				return false;
    			}
    		}
    		return true;
    	}
    	
    	public static int getItemBurnTime(ItemStack stack)
        {
    		if (stack.isEmpty())
            {
                return 0;
            }
    		else
    		{
    			int burnTime = ForgeEventFactory.getItemBurnTime(stack);
                if (burnTime >= 0) return burnTime;
                Item item = stack.getItem();
                if (item == ModItems.enchanting_coal)
                {
                    return 2000;
                }
    		}
    		return burningTimeLeft;
        }
    	
    	@Override
    	public void clear()
    	{
    		for(int i = 0; i < this.stacks.size(); i++) 
    		{
    			this.stacks.set(i, ItemStack.EMPTY);
    		}
    	}
    
    	@Override
    	public void openInventory(EntityPlayer player) {}
    
    	@Override
    	public void closeInventory(EntityPlayer player) {}
    
    	@Override
    	public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn)
    	{
    		return null;
    	}
    	
    	@Override
    	public String getGuiID() 
    	{
    		return null;
    	}
    
    	@Override
    	public boolean isItemValidForSlot(int index, ItemStack stack) 
    	{
    		return true;
    	}
    	
    	public boolean isUsableByPlayer(EntityPlayer player) 
    	{
    		return this.world.getTileEntity(this.pos) != this ? false : player
    				.getDistanceSq((double) this.pos.getX() + 0.5D,
    						(double) this.pos.getY() + 0.5D,
    						(double) this.pos.getZ() + 0.5D) <= 64.0D;
    	}
    	
    	public boolean hasFuelEmpty() 
    	{
    	    return this.getStackInSlot(2).isEmpty();
    	}
    	
    	public ItemStack getRecipeResult() 
    	{
    	    return RecipesParticleMachine.getRecipeResult(new ItemStack[] 
    	    		{
    	    				this.getStackInSlot(0), this.getStackInSlot(1)
    	            });
    	}
    	
    	public boolean canSmelt() 
    	{
    	    ItemStack result = this.getRecipeResult();
    	    if (result != null) 
    	    {
    	        ItemStack slot3 = this.getStackInSlot(3);
    	        if (slot3.isEmpty())
    	            return true;
    	        if (slot3.getItem() == result.getItem() && slot3.getItemDamage() == result.getItemDamage())
    	        {
    	            int newStackSize = slot3.getCount() + result.getCount();
    	            if (newStackSize <= this.getInventoryStackLimit() && newStackSize <= slot3.getMaxStackSize()) 
    	            {
    	                return true;
    	            }
    	        }
    	    }
    	    return false;
    	}
    	
    	public void smelt() 
    	{
    	    ItemStack result = this.getRecipeResult();
    	    this.decrStackSize(0, 1);
    	    this.decrStackSize(1, 1);
    	    ItemStack stack3 = this.getStackInSlot(3);
    	    if (stack3.isEmpty()) 
    	    {
    	        this.setInventorySlotContents(3, result.copy());
    	    } 
    	    else 
    	    {
    	        stack3.setCount(stack3.getCount() + result.getCount());
    	    }
    	}
    	
    	public int getFullRecipeTime() 
    	{
    	    return 200;
    	}
    	
    	public int getFullBurnTime() 
    	{
    	    return 300;
    	}public boolean isBurning()
    	{
    	    return burningTimeLeft > 0;
    	}
    	
    	@Override
    	public void update() 
    	{
    	    if (!this.world.isRemote) 
    	    {
    	        if (this.isBurning()) 
    	        {
    	            this.burningTimeLeft--;
    	        }
    	        if (!this.isBurning() && this.canSmelt() && !this.hasFuelEmpty()) 
    	        {
    	            this.burningTimeLeft = this.getFullBurnTime();
    	            this.decrStackSize(2, 1);
    	        }
    	        if (this.isBurning() && this.canSmelt()) 
    	        {
    	            this.timePassed++;
    	            if (timePassed >= this.getFullRecipeTime()) 
    	            {
    	                timePassed = 0;
    	                this.smelt();
    	            }
    	        } 
    	        else 
    	        {
    	            timePassed = 0;
    	        }
    	        this.markDirty();
    	    }
    	}
    }
    

    Merci d'avance.


Log in to reply