Jump to content

[1.7.10]Problem with spawning item entities? Please help


GTRxConfusion

Recommended Posts

Im trying to work on my autosmelt enchant, and im trying to get it to work with fortune. Whenever i break a smeltable block with a pick that has my enchant but not fortune, it drops an item just fine, but if i dont pick that item up and break another block the second item drops, and as usual, the stacks merge, but when i go to pick up the stack of two on the ground i only get one item and not two.

 

heres my code

package com.gtr.progressionplus.utils;

import java.util.ArrayList;
import java.util.Random;
import java.util.Set;

import scala.Console;
import net.minecraft.block.Block;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.item.EntityXPOrb;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemPickaxe;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.FurnaceRecipes;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.world.World;
import net.minecraftforge.event.world.BlockEvent.BreakEvent;

import com.google.common.collect.Sets;
import com.gtr.progressionplus.common.ModItems;
import com.gtr.progressionplus.enchantment.EnchantInferno;

import cpw.mods.fml.common.eventhandler.SubscribeEvent;

public class IHookContainer
{
    private static final Set exclude = Sets.newHashSet(new Block[]
    { Blocks.planks, Blocks.bookshelf, Blocks.log, Blocks.log2, Blocks.chest, Blocks.pumpkin, Blocks.lit_pumpkin });
    
    @SuppressWarnings("null")
    @SubscribeEvent
    public void breakEvent(BreakEvent event)
    {
        EntityPlayer player = event.getPlayer();
        Block mined = event.block;
        int meta = event.blockMetadata;
        World world = event.world;
        int i = event.x;
        int j = event.y;
        int k = event.z;
        boolean inferno = false;
        int fortune = 0;
        int level = 0;
        boolean flag = false;
        ItemStack itemstack = player.getCurrentEquippedItem();
        if (!player.capabilities.isCreativeMode && !world.isRemote && itemstack != null)
        {
            // itemstack.damageItem(1, player);
            if (itemstack.getItem() instanceof ItemPickaxe)
            {
                if (player.isSneaking() == false)
                {
                    flag = inferno(world, player, meta, mined, i, j, k);
                }
            }
            event.setCanceled(flag);
        }
    }
    
    @SuppressWarnings("null")
    private boolean inferno(World world, EntityPlayer player, int meta, Block mined, int i, int j, int k)
    {
        ItemStack itemstack = player.getCurrentEquippedItem();
        boolean inferno = false;
        int fortune = getEnchantLevel("fortune", itemstack);
        int level = getEnchantLevel("inferno", itemstack);
        if (fortune == -1)
        {
            fortune = 0;
        }
        if (level == -1)
        {
            level = 0;
        }
        Random rand = new Random();
        if (level > 0)
            inferno = true;
        if (inferno)
        {
            ArrayList<ItemStack> items = mined.getDrops(world, i, j, k, meta, fortune);
            {
            }
            ArrayList<ItemStack> todrop = new ArrayList();
            if (!world.isRemote)
            {
                if (items != null && items.size() > 0)
                {
                    for (ItemStack item : items)
                    {
                        if (FurnaceRecipes.smelting().getSmeltingResult(item) != null)
                        {
                            ItemStack smelted = FurnaceRecipes.smelting().getSmeltingResult(item);
                            if (smelted.getItem() == Items.iron_ingot || smelted.getItem() == Items.gold_ingot || smelted.getItem() == ModItems.nadium_ingot)
                            {
                                smelted.stackSize = Blocks.diamond_ore.quantityDropped(meta, fortune, rand);
                            }
                            else
                            {
                                smelted.stackSize = mined.quantityDropped(meta, fortune, rand);
                            }
                            todrop.add(smelted);
                        }
                        else
                        {
                            item.stackSize = mined.quantityDropped(meta, fortune, rand);
                            todrop.add(item);
                        }
                    }
                    if (todrop.size() > 0)
                    {
                        float f = 0.7F;
                        double d0 = (double) (rand.nextFloat() * f) + (double) (1.0F - f) * 0.5D;
                        double d1 = (double) (rand.nextFloat() * f) + (double) (1.0F - f) * 0.5D;
                        double d2 = (double) (rand.nextFloat() * f) + (double) (1.0F - f) * 0.5D;
                        for (ItemStack item : todrop)
                        {
                            EntityItem entityitem = new EntityItem(world, (double) i + d0, (double) j + d1, (double) k + d2, item);
                            entityitem.delayBeforeCanPickup = 10;
                            world.spawnEntityInWorld(entityitem);
                        }
                    }
                }
                world.setBlock(i, j, k, Blocks.air);
                return true;
            }
        }
        return false;
    }
    
    public int getEnchantLevel(String name, ItemStack itemstack)
    {
        int infernolvl = 0;
        int fortunelvl = 0;
        if (itemstack != null)
        {
            NBTTagList ench = itemstack.getEnchantmentTagList();
            if (ench != null)
            {
                for (int x = 0; x < ench.tagCount(); x++)
                {
                    NBTTagCompound nbt = (NBTTagCompound) (ench).getCompoundTagAt(x);
                    int id = nbt.getInteger("id");
                    if (id == EnchantInferno.effectid)
                    {
                        infernolvl = nbt.getInteger("lvl");
                    }
                    if (id == Enchantment.fortune.effectId)
                    {
                        fortunelvl = nbt.getInteger("lvl");
                    }
                }
            }
        }
        if (name == "inferno")
        {
            return infernolvl;
        }
        if (name == "fortune")
        {
            return fortunelvl;
        }
        return -1;
    }
}

 

 

Link to comment
Share on other sites

This code is to get my enchant to work with fortune, so yes diamonds should be dropped. but not 7, as the max amount of diamonds that would ever drop on fortune 3 with diamond ore is 4. How do i use harvestdrops?

 

Edit: should i have my inferno method return a list of items? and have the event use those items?

 

Link to comment
Share on other sites

should this work? (didnt fully check it yet, as im not completely sure about all of it)

package com.gtr.progressionplus.utils;

import java.util.ArrayList;
import java.util.Random;
import java.util.Set;

import scala.Console;
import net.minecraft.block.Block;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.item.EntityXPOrb;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemPickaxe;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.FurnaceRecipes;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.world.World;
import net.minecraftforge.event.world.BlockEvent.BreakEvent;
import net.minecraftforge.event.world.BlockEvent.HarvestDropsEvent;

import com.google.common.collect.Sets;
import com.gtr.progressionplus.common.ModItems;
import com.gtr.progressionplus.enchantment.EnchantInferno;

import cpw.mods.fml.common.eventhandler.SubscribeEvent;

public class IHookContainer
{
    private static final Set exclude = Sets.newHashSet(new Block[]
    { Blocks.planks, Blocks.bookshelf, Blocks.log, Blocks.log2, Blocks.chest, Blocks.pumpkin, Blocks.lit_pumpkin });
    
    @SuppressWarnings("null")
    @SubscribeEvent
    public void breakEvent(HarvestDropsEvent event)
    {
        EntityPlayer player = event.harvester;
        Block mined = event.block;
        int meta = event.blockMetadata;
        World world = event.world;
        int i = event.x;
        int j = event.y;
        int k = event.z;
        boolean inferno = false;
        int fortune = 0;
        int level = 0;
        boolean flag = false;
        ItemStack itemstack = player.getCurrentEquippedItem();
        if (!player.capabilities.isCreativeMode && !world.isRemote && itemstack != null)
        {
            // itemstack.damageItem(1, player);
            if (itemstack.getItem() instanceof ItemPickaxe)
            {
                if (player.isSneaking() == false)
                {
                    ArrayList<ItemStack> before = event.drops;
                    event.drops.clear();
                    event.drops.addAll(inferno(world, player, meta, mined, i, j, k,before));
                }
            }
            event.setCanceled(flag);
        }
    }
    
    @SuppressWarnings("null")
    private ArrayList<ItemStack> inferno(World world, EntityPlayer player, int meta, Block mined, int i, int j, int k, ArrayList<ItemStack> before)
    {
        ItemStack itemstack = player.getCurrentEquippedItem();
        boolean inferno = false;
        int fortune = getEnchantLevel("fortune", itemstack);
        int level = getEnchantLevel("inferno", itemstack);
        ArrayList<ItemStack> todrop = new ArrayList();
        if (fortune == -1)
        {
            fortune = 0;
        }
        if (level == -1)
        {
            level = 0;
        }
        Random rand = new Random();
        if (level > 0)
            inferno = true;
        if (inferno)
        {
            if (!world.isRemote)
            {
                if (before != null && before.size() > 0)
                {
                    for (ItemStack item : before)
                    {
                        if (FurnaceRecipes.smelting().getSmeltingResult(item) != null)
                        {
                            ItemStack smelted = FurnaceRecipes.smelting().getSmeltingResult(item);
                            if (smelted.getItem() == Items.iron_ingot || smelted.getItem() == Items.gold_ingot || smelted.getItem() == ModItems.nadium_ingot)
                            {
                                smelted.stackSize = Blocks.diamond_ore.quantityDropped(meta, fortune, rand);
                            }
                            else
                            {
                                smelted.stackSize = mined.quantityDropped(meta, fortune, rand);
                            }
                            todrop.add(smelted);
                        }
                        else
                        {
                            item.stackSize = mined.quantityDropped(meta, fortune, rand);
                            todrop.add(item);
                        }
                    }
                    return todrop;
                }
                world.setBlock(i, j, k, Blocks.air);
                return todrop;
            }
        }
        return todrop;
    }
    
    public int getEnchantLevel(String name, ItemStack itemstack)
    {
        int infernolvl = 0;
        int fortunelvl = 0;
        if (itemstack != null)
        {
            NBTTagList ench = itemstack.getEnchantmentTagList();
            if (ench != null)
            {
                for (int x = 0; x < ench.tagCount(); x++)
                {
                    NBTTagCompound nbt = (NBTTagCompound) (ench).getCompoundTagAt(x);
                    int id = nbt.getInteger("id");
                    if (id == EnchantInferno.effectid)
                    {
                        infernolvl = nbt.getInteger("lvl");
                    }
                    if (id == Enchantment.fortune.effectId)
                    {
                        fortunelvl = nbt.getInteger("lvl");
                    }
                }
            }
        }
        if (name == "inferno")
        {
            return infernolvl;
        }
        if (name == "fortune")
        {
            return fortunelvl;
        }
        return -1;
    }
}

Link to comment
Share on other sites

package com.gtr.progressionplus.utils;

import java.util.ArrayList;
import java.util.Random;
import java.util.Set;

import scala.Console;
import net.minecraft.block.Block;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.item.EntityXPOrb;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemPickaxe;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.FurnaceRecipes;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.world.World;
import net.minecraftforge.event.world.BlockEvent.BreakEvent;
import net.minecraftforge.event.world.BlockEvent.HarvestDropsEvent;

import com.google.common.collect.Sets;
import com.gtr.progressionplus.common.ModItems;
import com.gtr.progressionplus.enchantment.EnchantInferno;

import cpw.mods.fml.common.eventhandler.SubscribeEvent;

public class IHookContainer
{
    private static final Set exclude = Sets.newHashSet(new Block[]
    { Blocks.planks, Blocks.bookshelf, Blocks.log, Blocks.log2, Blocks.chest, Blocks.pumpkin, Blocks.lit_pumpkin });
    
    @SuppressWarnings("null")
    @SubscribeEvent
    public void breakEvent(HarvestDropsEvent event)
    {
        EntityPlayer player = event.harvester;
        Block mined = event.block;
        int meta = event.blockMetadata;
        World world = event.world;
        int i = event.x;
        int j = event.y;
        int k = event.z;
        Random rand = new Random();
        boolean inferno = false;
        ItemStack itemstack = player.getCurrentEquippedItem();
        int infernolvl = getEnchantLevel("inferno", itemstack);
        if (!player.capabilities.isCreativeMode && !world.isRemote && itemstack != null)
        {
            // itemstack.damageItem(1, player);
            if (itemstack.getItem() instanceof ItemPickaxe)
            {
                if (player.isSneaking() == false && infernolvl > 0)
                {
                    for (ItemStack item : event.drops)
                    {
                        if (FurnaceRecipes.smelting().getSmeltingResult(item) != null)
                        {
                            event.drops.remove(item);
                            ItemStack smelted = FurnaceRecipes.smelting().getSmeltingResult(item);
                            if (smelted.getItem() == Items.iron_ingot || smelted.getItem() == Items.gold_ingot || smelted.getItem() == ModItems.nadium_ingot)
                            {
                                smelted.stackSize = Blocks.diamond_ore.quantityDropped(meta, event.fortuneLevel, rand);
                            }
                            else
                            {
                                smelted.stackSize = mined.quantityDropped(meta, event.fortuneLevel, rand);
                            }
                            event.drops.add(smelted);
                        }
                        else
                        {
                            event.drops.remove(item);
                            item.stackSize = mined.quantityDropped(meta, event.fortuneLevel, rand);
                            event.drops.add(item);
                        }
                    }
                }
            }
        }
    }
    
    public int getEnchantLevel(String name, ItemStack itemstack)
    {
        int infernolvl = 0;
        int fortunelvl = 0;
        if (itemstack != null)
        {
            NBTTagList ench = itemstack.getEnchantmentTagList();
            if (ench != null)
            {
                for (int x = 0; x < ench.tagCount(); x++)
                {
                    NBTTagCompound nbt = (NBTTagCompound) (ench).getCompoundTagAt(x);
                    int id = nbt.getInteger("id");
                    if (id == EnchantInferno.effectid)
                    {
                        infernolvl = nbt.getInteger("lvl");
                    }
                    if (id == Enchantment.fortune.effectId)
                    {
                        fortunelvl = nbt.getInteger("lvl");
                    }
                }
            }
        }
        if (name == "inferno")
        {
            return infernolvl;
        }
        if (name == "fortune")
        {
            return fortunelvl;
        }
        return -1;
    }
}

any better? sorry my mind is slow and i derp sometimes

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



×
×
  • Create New...

Important Information

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