Jump to content

Item Not Doing what it should [Unsolved]


StrangeOne101
 Share

Recommended Posts

You make a class that extends WorldSavedData.

In that class add fields for everything you want to store (if you want to store a number, add an int field, etc.)

In the write/read NBT methods, write those fields to the given NBTTagCompound.

If you change a value, be sure to call markDirty(), otherwise MC won't save the changes.

To initialize your Data (it won't be present the very first time you try to load it {load it via world.loadItemData(YourData.class, "yourIdentifier"); it will return null if not present yet}) you need to create it yourself (YourData data = new YourData("yourIdentifier")) and then save it: world.setItemData(data, "yourIdentifier");

Example for getting some data:

World world = MinecraftServer.getServer().worldServers[0]; // always save our data to the overworld
YourItemData data = (YourItemData)world.loadItemData(YourItemData.class, "yourIdentifier");
if (data == null) {
    // not present yet, create it
    data = new YourItemData("yourIdentifier");
    world.setItemData(data, "yourIdentifier");
}
data.someField = "someRandomData";
data.markDirty();

Link to comment
Share on other sites

Thank you so much. I understand now. I had it completely wrong before.

 

Anyway, basicly all aspects of my mod work apart from this one thing. My staff, when right click, should also summon the event, but doesn't. It says "Hidding Eggs" (which is in the event code, not the item's code), but does nothing else.

 

[spoiler=Staff Code]

package so101.eastersurprise.eggs;

import java.util.List;
import java.util.Random;

import net.minecraft.block.Block;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Icon;
import net.minecraft.world.World;

import cpw.mods.fml.relauncher.*;
import so101.eastersurprise.Main;
import so101.eastersurprise.client.ConfigEasterSurprise;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;

public class ItemEasterStaff extends Item 
{
private Random rand = new Random();
private ConfigEasterSurprise ces = Main.ces;
private RandomEasterHuntEvent rehe = new RandomEasterHuntEvent();
private int randItem;
private boolean lock = false;

protected int[] items;

public ItemEasterStaff(int par1) 
{
	super(par1); 
	setCreativeTab(CreativeTabs.tabMisc); 
	setMaxStackSize(1);
	this.bFull3D = true;

}


public void onUsingItemTick(ItemStack stack, EntityPlayer player, int count)
{
	//par1ItemStack.damageItem(rehe.getHuntProgress(), null);
	//for (int i=0;i<10;i++)
	player.worldObj.spawnParticle("smoke", player.posX-1+rand.nextInt(2)+rand.nextDouble(), player.posY-1+rand.nextDouble(), player.posZ-1+rand.nextInt(2)+rand.nextDouble(), 0.0D, 0.0D, 0.0D);
	if (!lock)
	{
		Main.startHunt(player);
		lock = true;
	}
}

/**Adds the texture from the directory stated**/
@Override
public void updateIcons(IconRegister ir)
{
	iconIndex = ir.registerIcon("EasterSurpriseMod:staff");
}


public String getItemDisplayName(ItemStack is)
{
	return "Easter Staff";
}

public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
    {
	par3EntityPlayer.setItemInUse(par1ItemStack, 5);
	return par1ItemStack; 
    }
}

 

 

[spoiler=Event Code]

package so101.eastersurprise.eggs;

import java.util.Random;

import net.minecraft.client.Minecraft;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.server.MinecraftServer;
import net.minecraft.src.ModLoader;
import net.minecraft.world.World;

import so101.eastersurprise.Main;
import so101.eastersurprise.client.ConfigEasterSurprise;

public class RandomEasterHuntEvent 
{
private Random rand = new Random();

private ConfigEasterSurprise ces = Main.ces;
private int radius;
private int amount;

int i = 0;
int j = 0;

private EntityItem[] item;
private double[] x;
private double[] z;

private String message = "";

public RandomEasterHuntEvent()
{
	radius = ces.huntRadius;
	amount = ces.huntAmount;
	item = new EntityItem[amount];
	x = new double[amount];
	z = new double[amount];

}

public void startEasterHunt(World world, EntityPlayer player)
{
	player.addChatMessage(message+"§7Hidding eggs...");
	if (!world.isRemote)
	{
		for (i = 0; i<amount/2;i++)
		{
			int randomChange = world.rand.nextInt(50);
			int damageValue;
			if (randomChange < 3)
			{
				damageValue = 4;
			}
			else if (randomChange < 20)
			{
				damageValue = 2 + rand.nextInt(2);
			}
			else
			{
				damageValue = 0 + rand.nextInt(2);
			}
			double x = player.posX-(float)radius+(float)world.rand.nextInt(radius*2);
			double z = player.posZ-(float)radius+(float)world.rand.nextInt(radius*2);
			item[i] = new EntityItem(world, x, world.getTopSolidOrLiquidBlock((int)x, (int)z)+ rand.nextInt(16), z, new ItemStack(Main.EasterEgg, 1, damageValue));
			item[i].delayBeforeCanPickup = 0;
			item[i].age = -24000*3;
			world.spawnEntityInWorld(item[i]);
		}

		for (j = 0; j<amount/2;j++)
		{
			int randomChange2 = rand.nextInt(50);
			int damageValue2;
			if (randomChange2 < 3)
			{
				damageValue2 = 4;
			}
			else if (randomChange2 < 20)
			{
				damageValue2 = 2 + rand.nextInt(2);
			}
			else
			{
				damageValue2 = 0 + rand.nextInt(2);
			}
			double x = player.posX-(float)radius+(float)rand.nextInt(radius*2);
			double z = player.posZ-(float)radius+(float)rand.nextInt(radius*2);
			item[i+j] = new EntityItem(world, x, world.getFirstUncoveredBlock((int)x, (int)z), z, new ItemStack(Main.EasterEgg, 1, damageValue2+world.rand.nextInt(1)));
			item[i+j].delayBeforeCanPickup = 0;
			item[i+j].age = -24000*3;
			world.spawnEntityInWorld(item[i+j]);
		}
		player.addChatMessage("§7The eggs have been hidden. Let the Easter Hunt... BEGIN!");

	}
}

public boolean getIsHuntActive()
{
	if (item[0] == null)
	{
		return false;
	}
	return true;
}

public void setHuntCancel(boolean messageUserOfCancel, EntityPlayer entityplayer)
{
	for (int k = 0; k<amount;k++)
	{
		item[k].setDead();
	}
	i = 0;
	j = 0;

	if (messageUserOfCancel)
	{
		entityplayer.addChatMessage("§7Current Easter Hunt canceled.");
	}
}

public void setHuntCancelIfActive(boolean messageUserOfCancel, EntityPlayer entityplayer)
{
	if (this.getIsHuntActive())
	{
		for (int k = 0; k<amount;k++)
		{
			item[k].setDead();
		}
		i = 0;
		j = 0;

		if (messageUserOfCancel)
		{
			entityplayer.addChatMessage("§7Easter hunt is already active. Cancelling...");
		}
	}
}

public void setHuntCancelIfActiveWithNew(boolean messageUserOfCancel, EntityPlayer entityplayer)
{
	if (this.getIsHuntActive())
	{
		for (int k = 0; k<amount;k++)
		{
			item[k].setDead();
		}
		i = 0;
		j = 0;

		if (messageUserOfCancel)
		{
			entityplayer.addChatMessage("§7Easter hunt is already active. Cancelling...");
		}
		message = "§7Starting new Easter Hunt. ";
		this.startEasterHunt(entityplayer.worldObj, entityplayer);
		message = "";
	}
	else 
	{
		this.startEasterHunt(entityplayer.worldObj, entityplayer);
	}
}

public int getHuntProgress()
{
	return i+j;
}
}

 

 

[spoiler=Mod Class Hunt Method]

public static void startHunt(EntityPlayer entityplayer)
{
		hunt.setHuntCancelIfActiveWithNew(true, entityplayer);
}

 

 

Everything else works, so thank you guys so much. I will credit you in the official thread.

The enders are everywhere. You can't escape...

Link to comment
Share on other sites

Hm.

 

First of all, You got me wrong again. I was proposing that You do:

 

public void onUsingItemTick(ItemStack stack, EntityPlayer player, int count)
{
	player.worldObj.spawnParticle("smoke", player.posX-1+rand.nextInt(2)+rand.nextDouble(), player.posY-1+rand.nextDouble(), player.posZ-1+rand.nextInt(2)+rand.nextDouble(), 0.0D, 0.0D, 0.0D);
}

public void onPlayerStoppedUsing(world, stack, player, otherargs)
{
if (!lock)
{
	Main.startHunt(player);
	lock = true;
}
}

 

Now, why it doesn't work? Looks like Main.startHunt is called in remote world, so

if(!world.isRemote)

returns false. There may be issues with getting worldObj from player, as Player has various entities EntityPlayer, EntityPlayerMP, EntitySinglePlayerMP, EntityOtherPlayerMP and so on. It's complicated and may be made even more complicated with updates.

 

My tip? Change hunt.setHuntCancelIfActiveWithNew to accept world parameter, add World worldObj to Hunt class,

and call

 

public void onPlayerStoppedUsing(world, stack, player, otherargs)
{
if (!lock)
{
	Main.startHunt(player,world);
	lock = true;
}
}

Does it work now?

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
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.

 Share



×
×
  • Create New...

Important Information

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