Jump to content

[Solved][1.9.x] Problem with items spawned in world


American2050

Recommended Posts

I have a tool, that when you right click, for example on Pumpkins, it has the chance of dropping extra Pumpkins.

 

However, and even when I see the 2 pumpkins in floor, sometimes on pickup it just pick ups one. But sometimes it does pickup 2 pumpkins and I'm not sure what could be wrong here.

 

				else if (checkBlock instanceof BlockPumpkin || checkBlock instanceof BlockMelon){
  				
	    				List<ItemStack> drops = new ArrayList<ItemStack>();
	    				
	    				drops = world.getBlockState(pos).getBlock().getDrops(world, pos, world.getBlockState(pos), 0);
	    				
	    				for(ItemStack drop : drops){
	    					
	    					world.setBlockToAir(pos);
	    					
	    					if(!world.isRemote){
	    						
	    					EntityItem itemDrop = new EntityItem(world, pos.getX()+.5, pos.getY()+.5, pos.getZ()+.5, drop);
	    				   	world.spawnEntityInWorld(itemDrop);
	    				   	
	    				   	rand = (int) (Math.random() * chances);
	    				   	
	    				   	if(rand==0){ EntityItem itemDropExtra = new EntityItem(world, pos.getX()+.5, pos.getY()+.5, pos.getZ()+.5, drop); world.spawnEntityInWorld(itemDropExtra); }
	    				   	
	    					}
	    					
	    				}
	    				stack.damageItem(1, player);
	    				return EnumActionResult.SUCCESS;
	    				
	    			}//END IF PUMPKING/MELON

 

All this is inside the onItemUse method, where some other stuff happens. (Doing stuff in other crops and acting as a how)

 

 

@Override

public EnumActionResult onItemUse(ItemStack stack, EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)

    {

 

 

if(!world.isRemote){

Item tool = stack.getItem();

 

if(tool.equals(ModItems.crop_dongle)){

chances=20;

}

if(tool.equals(ModItems.crop_dongle_stone)){

chances=15;

}

if(tool.equals(ModItems.crop_dongle_iron)){

chances=9;

}

if(tool.equals(ModItems.crop_dongle_gold)){

chances=6;

}

if(tool.equals(ModItems.crop_dongle_diamond)){

chances=3;

}

 

}

 

//HOE

        if (!player.canPlayerEdit(pos.offset(facing), facing, stack))

        {

            return EnumActionResult.FAIL;

        }

        else

        {

            int hook = net.minecraftforge.event.ForgeEventFactory.onHoeUse(stack, player, world, pos);

            if (hook != 0) return hook > 0 ? EnumActionResult.SUCCESS : EnumActionResult.FAIL;

 

            IBlockState iblockstate = world.getBlockState(pos);

            Block block = iblockstate.getBlock();

 

            if (facing != EnumFacing.DOWN && world.isAirBlock(pos.up()))

            {

                if (block == Blocks.grass || block == Blocks.grass_path)

                {

                    this.tillDirt(stack, player, world, pos, Blocks.farmland.getDefaultState());

                    return EnumActionResult.SUCCESS;

                }

 

                if (block == Blocks.dirt)

                {

                    switch ((BlockDirt.DirtType)iblockstate.getValue(BlockDirt.VARIANT))

                    {

                        case DIRT:

                            this.tillDirt(stack, player, world, pos, Blocks.farmland.getDefaultState());

                            return EnumActionResult.SUCCESS;

                        case COARSE_DIRT:

                            this.tillDirt(stack, player, world, pos, Blocks.dirt.getDefaultState().withProperty(BlockDirt.VARIANT, BlockDirt.DirtType.DIRT));

                            return EnumActionResult.SUCCESS;

                          default:

                          return EnumActionResult.PASS;

                    }

                }

            }

 

           

        }

 

 

//DONGLE

IBlockState checkBlockState = world.getBlockState(pos);

Block checkBlock = checkBlockState.getBlock();

 

if(checkBlock!=null)

{

if(checkBlock instanceof BlockCrops)

{

 

 

int age = 0;

   

    String cropName = world.getBlockState(pos).getBlock().getUnlocalizedName().substring(5);

   

    if(cropName.equals("beetroots")){

   

    age = world.getBlockState(pos).getValue(BlockBeetroot.BEETROOT_AGE);

    if(age==3){

   

    List<ItemStack> drops = new ArrayList<ItemStack>();

   

    drops = world.getBlockState(pos).getBlock().getDrops(world, pos, world.getBlockState(pos), 0);

   

    for(ItemStack drop : drops){

   

    world.setBlockState(pos, world.getBlockState(pos).withProperty(BlockBeetroot.BEETROOT_AGE, Integer.valueOf(0)), 3);

   

    if(!world.isRemote){

    EntityItem itemDrop = new EntityItem(world, pos.getX()+.5, pos.getY()+.5, pos.getZ()+.5, drop);

      world.spawnEntityInWorld(itemDrop);

     

      rand = (int) (Math.random() * chances);

     

      if(rand==0){ EntityItem itemDropExtra = new EntityItem(world, pos.getX()+.5, pos.getY()+.5, pos.getZ()+.5, drop); world.spawnEntityInWorld(itemDropExtra); }

     

    }

   

    }

    stack.damageItem(1, player);

    return EnumActionResult.SUCCESS;

   

    }

   

    }

    else {

    age = world.getBlockState(pos).getValue(BlockCrops.AGE);

    if(age==7){

   

    List<ItemStack> drops = new ArrayList<ItemStack>();

   

    drops = world.getBlockState(pos).getBlock().getDrops(world, pos, world.getBlockState(pos), 0);

   

    for(ItemStack drop : drops){

   

    world.setBlockState(pos, world.getBlockState(pos).withProperty(BlockCrops.AGE, Integer.valueOf(0)), 3);

   

    if(!world.isRemote){

    EntityItem itemDrop = new EntityItem(world, pos.getX()+.5, pos.getY()+.5, pos.getZ()+.5, drop);

      world.spawnEntityInWorld(itemDrop);

     

      rand = (int) (Math.random() * chances);

     

      if(rand==0){ EntityItem itemDropExtra = new EntityItem(world, pos.getX()+.5, pos.getY()+.5, pos.getZ()+.5, drop); world.spawnEntityInWorld(itemDropExtra); }

     

    }

   

   

    }

    stack.damageItem(1, player);

    return EnumActionResult.SUCCESS;

   

    }

    }

 

}//END IF CROPS

 

else if (checkBlock instanceof BlockCocoa){

 

int age = world.getBlockState(pos).getValue(BlockCocoa.AGE);

 

    if(age==2){

   

    List<ItemStack> drops = new ArrayList<ItemStack>();

   

    drops = world.getBlockState(pos).getBlock().getDrops(world, pos, world.getBlockState(pos), 0);

   

    for(ItemStack drop : drops){

   

    world.setBlockState(pos, world.getBlockState(pos).withProperty(BlockCocoa.AGE, Integer.valueOf(0)), 3);

   

    if(!world.isRemote){

    EntityItem itemDrop = new EntityItem(world, pos.getX()+.5, pos.getY()+.5, pos.getZ()+.5, drop);

      world.spawnEntityInWorld(itemDrop);

     

      rand = (int) (Math.random() * chances);

     

      if(rand==0){ EntityItem itemDropExtra = new EntityItem(world, pos.getX()+.5, pos.getY()+.5, pos.getZ()+.5, drop); world.spawnEntityInWorld(itemDropExtra); }

     

    }

   

    }

    stack.damageItem(1, player);

    return EnumActionResult.SUCCESS;

   

    }

 

    return EnumActionResult.PASS;

}//END IF COCOA

 

 

 

else if (checkBlock instanceof BlockPumpkin || checkBlock instanceof BlockMelon){

 

    List<ItemStack> drops = new ArrayList<ItemStack>();

   

    drops = world.getBlockState(pos).getBlock().getDrops(world, pos, world.getBlockState(pos), 0);

   

    for(ItemStack drop : drops){

   

    world.setBlockToAir(pos);

   

    if(!world.isRemote){

   

    EntityItem itemDrop = new EntityItem(world, pos.getX()+.5, pos.getY()+.5, pos.getZ()+.5, drop);

      world.spawnEntityInWorld(itemDrop);

     

      rand = (int) (Math.random() * chances);

     

      if(rand==0){ EntityItem itemDropExtra = new EntityItem(world, pos.getX()+.5, pos.getY()+.5, pos.getZ()+.5, drop); world.spawnEntityInWorld(itemDropExtra); }

     

    }

   

    }

    stack.damageItem(1, player);

    return EnumActionResult.SUCCESS;

   

    }//END IF PUMPKING/MELON

 

 

 

}//END IF BLOCK NOT NULL

 

return EnumActionResult.PASS;

 

 

    }//END ONITEMUSE

 

 

 

Any clue on what I'm doing wrong here?

 

I noticed it on the Pumpkins but I bet it's also happening with the extra drops on the crops for sure.

 

SuNHqHQ.png

 

lg7l98g.png

 

Link to comment
Share on other sites

You're doing the stuff on both client and server.

Only spawn items on the server. Moreover your code is a mess.

 

I have the if(!world.isRemote) before spawning the items.

 

So not sure, all the onitemuse should run only on server?

 

Also I noticed when the 2 pumpkins spawn separate, when I pick one, the other also dissapear. Looks like I'm cloning the same item somehow  :-\

Link to comment
Share on other sites

I have the if(!world.isRemote) before spawning the items.
Yes, around the item spawning maybe, but you are still doing other stuff (setting blocks) on the client. Don't.

Also I noticed when the 2 pumpkins spawn separate, when I pick one, the other also dissapear. Looks like I'm cloning the same item somehow  :-\

Cleanup your code so it's actually readable and remove that copy-pasta mess and we can see what is actually happening.

 

Thanks, gonna do some cleaning. The copy pasta isn't a big deal, it's just recreating what a hoe can do.

 

I'm always confuse with the client/server side or the things, and never sure what goes where. But thinking it again, it makes sense that everything inside the onitemuse should be server side only.

 

PS: Thanks for noticing that the code is a "mess" but maybe some tips on what you think I could do to make it better can help me improve  :-[

 

Thanks once again, always helpful  :D

Link to comment
Share on other sites

I'm always confuse with the client/server side or the things, and never sure what goes where. But thinking it again, it makes sense that everything inside the onitemuse should be server side only.

 

Is it direct input from the player?  Is it display output to the player?

 

If you answered "no" to both of these questions, then it happens on the server.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

So I cleaned up the code, do some more testing, but still having the same problem

 

(For example, trying it on Pumpkins, that vanilla default only drops 1)

 

Sometimes 2 items spawn and I correctly pick up 2 items, but sometimes 2 spawn and when I pick them up, it's really just 1 item.

 

package com.mramericanmike.mikedongles.items;

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

import net.minecraft.block.Block;
import net.minecraft.block.BlockBeetroot;
import net.minecraft.block.BlockCocoa;
import net.minecraft.block.BlockCrops;
import net.minecraft.block.BlockDirt;
import net.minecraft.block.BlockMelon;
import net.minecraft.block.BlockPumpkin;
import net.minecraft.block.properties.PropertyInteger;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.SoundEvents;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import com.mramericanmike.mikedongles.ModInfo;
import com.mramericanmike.mikedongles.creativetab.ModCreativeTab;
import com.mramericanmike.mikedongles.init.ModItems;
import com.mramericanmike.mikedongles.init.ModMaterials.ModToolMaterial;

public class CropDongle extends Item
{
    private final float damage;
    
    private int dropExtra = 20;
    private int age = 0;
    private String cropName = "";
    private List<ItemStack> drops = new ArrayList<ItemStack>();
    
    public CropDongle(ModToolMaterial material, String name)
    {
        this.maxStackSize = 1;
        this.setMaxDamage(material.getMaxUses());
        this.damage = material.getDamageVsEntity() + 1.0F;
	this.setUnlocalizedName(ModInfo.MODID.toLowerCase() + ":" + name);
	this.setCreativeTab(ModCreativeTab.MOD_TAB);
    }

@Override
public EnumActionResult onItemUse(ItemStack stack, EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)
    {

	//HOE
    if (!player.canPlayerEdit(pos.offset(facing), facing, stack)) { return EnumActionResult.FAIL; }
    
        else {
        	
            int hook = net.minecraftforge.event.ForgeEventFactory.onHoeUse(stack, player, world, pos);
            if (hook != 0) return hook > 0 ? EnumActionResult.SUCCESS : EnumActionResult.FAIL;

            IBlockState iblockstate = world.getBlockState(pos);
            Block block = iblockstate.getBlock();

            if (facing != EnumFacing.DOWN && world.isAirBlock(pos.up()))
            {
                if (block == Blocks.GRASS || block == Blocks.GRASS_PATH)
                {
                    this.tillDirt(stack, player, world, pos, Blocks.FARMLAND.getDefaultState());
                    return EnumActionResult.SUCCESS;
                }

                if (block == Blocks.DIRT)
                {
                    switch ((BlockDirt.DirtType)iblockstate.getValue(BlockDirt.VARIANT))
                    {
                        case DIRT:
                            this.tillDirt(stack, player, world, pos, Blocks.FARMLAND.getDefaultState());
                            return EnumActionResult.SUCCESS;
                        case COARSE_DIRT:
                            this.tillDirt(stack, player, world, pos, Blocks.DIRT.getDefaultState().withProperty(BlockDirt.VARIANT, BlockDirt.DirtType.DIRT));
                            return EnumActionResult.SUCCESS;
                        default:
                        return EnumActionResult.PASS;
                    }
                }
            }
            
        } //END HOE


		//DONGLE
		IBlockState checkBlockState = world.getBlockState(pos);
		Block checkBlock = checkBlockState.getBlock();

		if(checkBlock!=null)
		{
			if(checkBlock instanceof BlockCrops)
			{
				cropName = world.getBlockState(pos).getBlock().getUnlocalizedName().substring(5);

				//Beetroots
	    		if(cropName.equals("beetroots")){
	    			
	    			age = world.getBlockState(pos).getValue(BlockBeetroot.BEETROOT_AGE);
	    			
	    			if(age==3){
	    				
	    				drops = world.getBlockState(pos).getBlock().getDrops(world, pos, world.getBlockState(pos), 0);
	    				
	    				world.setBlockState(pos, world.getBlockState(pos).withProperty(BlockBeetroot.BEETROOT_AGE, Integer.valueOf(0)), 3);
	    				
	    				this.spawnDrops(drops, world, pos, stack, player);

	    				return EnumActionResult.SUCCESS;
	    				
	    			}
	    			
	    			return EnumActionResult.PASS;
	    			
	    		}//End Beetroots
	    		
	    		//Other crops go with age 7
	    		else {
	    			
	    			age = world.getBlockState(pos).getValue(BlockCrops.AGE);
	    			
	    			if(age==7){
	    				
	    				drops = world.getBlockState(pos).getBlock().getDrops(world, pos, world.getBlockState(pos), 0);

	    				world.setBlockState(pos, world.getBlockState(pos).withProperty(BlockCrops.AGE, Integer.valueOf(0)), 3);

	    				this.spawnDrops(drops, world, pos, stack, player);

	    				return EnumActionResult.SUCCESS;
	    				
	    			}
	    			
	    			return EnumActionResult.PASS;
	    		}

			}//END IF CROPS


			//COCOA
			else if (checkBlock instanceof BlockCocoa){

				age = world.getBlockState(pos).getValue(BlockCocoa.AGE);

	    			if(age==2){
	    				
	    				drops = world.getBlockState(pos).getBlock().getDrops(world, pos, world.getBlockState(pos), 0);

	    				world.setBlockState(pos, world.getBlockState(pos).withProperty(BlockCocoa.AGE, Integer.valueOf(0)), 3);

	    				this.spawnDrops(drops, world, pos, stack, player);

	    				return EnumActionResult.SUCCESS;
	    				
	    				}//END IF AGE 2
	    				
	    			return EnumActionResult.PASS;
	    			
		}//END IF COCOA


			//PUMPKINS AND MELONS
			else if (checkBlock instanceof BlockPumpkin || checkBlock instanceof BlockMelon){
  				
	    				drops = world.getBlockState(pos).getBlock().getDrops(world, pos, world.getBlockState(pos), 0);

	    				world.setBlockToAir(pos);
	    						
	    				this.spawnDrops(drops, world, pos, stack, player);

	    				return EnumActionResult.SUCCESS;
	    				
	    		}//END IF PUMPKING/MELON

		}//END IF BLOCK NOT NULL

		return EnumActionResult.PASS;

    }//END ONITEMUSE


    private int returnToolChances(Item item) {

    	int chances = 20;
    	
    	if(item.equals(ModItems.crop_dongle)){ chances=20; }
	if(item.equals(ModItems.crop_dongle_stone)){ chances=15; }
	if(item.equals(ModItems.crop_dongle_iron)){ chances=9; }
	if(item.equals(ModItems.crop_dongle_gold)){ chances=6; }
	if(item.equals(ModItems.crop_dongle_diamond)){ chances=3; }

	dropExtra = (int) (Math.random() * chances);

	return dropExtra;
}

private void tillDirt(ItemStack stack, EntityPlayer player, World world, BlockPos pos, IBlockState state)
    {
        world.playSound(player, pos, SoundEvents.ITEM_HOE_TILL, SoundCategory.BLOCKS, 1.0F, 1.0F);

        if (!world.isRemote)
        {
            world.setBlockState(pos, state, 11);
            stack.damageItem(1, player);
        }
    }

private void spawnDrops(List<ItemStack> drops, World world, BlockPos pos, ItemStack stack, EntityPlayer player){

	if(!world.isRemote){

	for(ItemStack drop : drops){

		EntityItem itemDrop = new EntityItem(world, pos.getX()+.5, pos.getY()+.5, pos.getZ()+.5, drop);
	   	world.spawnEntityInWorld(itemDrop);
	   	
	   	dropExtra = this.returnToolChances(stack.getItem());
	   	
	   	if(dropExtra==0){
	   	
	   	EntityItem itemDropExtra = new EntityItem(world, pos.getX()+.5, pos.getY()+.5, pos.getZ()+.5, drop);
	   	world.spawnEntityInWorld(itemDropExtra);
	   	
	   	}

	}

	stack.damageItem(1, player);

	}
}
    





    @Override
    @SideOnly(Side.CLIENT)
    public void addInformation(ItemStack stack, EntityPlayer player, List list, boolean bool)
    {

    list.add("Can be used as Hoe");
    
    }


}

 

Really don't know what I'm doing wrong here. I would understand it better if 100% the time I see this behavior, but not, it only does it sometimes.

 

PS: I checked when the Extra drop gets triggered, and it always happens when that it's triggered, that it spawn the extra drop, but it happens what I described. When the extra drop isn't triggered, 100% of the time, only 1 item spawn and its picked correctly.

Link to comment
Share on other sites

Aside from few design mistakes like using if instead of else if (#returnToolChances), I will take a wild guess.

 

for(ItemStack drop : drops)
{
EntityItem itemDrop = new EntityItem(world, pos.getX()+.5, pos.getY()+.5, pos.getZ()+.5, drop);
world.spawnEntityInWorld(itemDrop);
	   	
dropExtra = this.returnToolChances(stack.getItem());

if(dropExtra==0)
{
	EntityItem itemDropExtra = new EntityItem(world, pos.getX()+.5, pos.getY()+.5, pos.getZ()+.5, drop);
	world.spawnEntityInWorld(itemDropExtra);
}
}

 

You are using SAME ItemStack when creating EntityItem, causing some internal ItemStack object error.

Use stack.copy().

 

Note that I really didn't pay much attention to math/other stuff you do.

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

Thanks a lot Ernio. I did suspected that could be the problem, I had no idea how to fix it. Using the .copy for the stack does the trick, I have tried a lot with the Pumpkins and I don't see ghost drops anymore. And as Im using that method for Cocoa, Crops and Melons I bet it fixes problems there also.

 

Thanks once again, I love people trying to help in this forum as always great.

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.