Jump to content

Item Not Doing what it should [Unsolved]


StrangeOne101

Recommended Posts

I have two main things I want to get done today, but I need help with them. I have changed the question, too.

 

Edit: Scroll down to the bottom of the page. I have a problem with my item.

 

 

As I've said before, I'm new to forge. So I have no idea.

 

Thanks :)

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

Link to comment
Share on other sites

For the save-related stuff, I'm just getting into it myself. I'll sort of move it up higher on my queue of crap to program and see if I learn anything worth telling you. Hopefully somebody with more (or just some) experience with NBT stuff replies, for both our sakes.

 

How often do you want to check for this? And what are you checking it from?

 

Also,here's probably plenty of non-Minercraft related Java that could do this, too.

 

I'll reply later tonight if I can, if not I'll try some time tomorrow.

Link to comment
Share on other sites

I've messed with NBT data, but only from within a TileEntity, I have no idea how to go about applying that information to the world.

 

NBT reading/writing is pretty easy though.

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

I just want to check if the date (within month) is equal to a value, so then I can run some code, and only allow it to be repeated the year after. Kind of like a one day a year thing. To do that, I need it to record if the event has been run and stuff. Do I need to elaborate more?

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

Link to comment
Share on other sites

I've done a lot of googling and searching but all I can find is tutorials for Tile Entities. It would work, but it's not what I want considering that there will be no way of tracking where it is.

 

So to elaborate, a TileEntity would be good but it uses coords, not a global variable.

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

Link to comment
Share on other sites

I don't know about NBT, but about players logging in, there is a IPlayerTracker interface.

Just create a class like so:

 

public class PlayerTracker implements IPlayerTracker {
@Override
public void onPlayerLogin(EntityPlayer player) {
        //Your code
}
public void onPlayerLogout(EntityPlayer player) {	}
public void onPlayerChangedDimension(EntityPlayer player) {}
public void onPlayerRespawn(EntityPlayer player) {}
}

onLogin method gets called every time a player joins a server (in Single Player too).

 

You register player trackers in Your main mod class, preferably in load method.

GameRegistry.registerPlayerTracker(new PlayerTracker());

 

Link to comment
Share on other sites

I don't know about NBT, but about players logging in, there is a IPlayerTracker interface.

Just create a class like so:

 

public class PlayerTracker implements IPlayerTracker {
@Override
public void onPlayerLogin(EntityPlayer player) {
        //Your code
}
public void onPlayerLogout(EntityPlayer player) {	}
public void onPlayerChangedDimension(EntityPlayer player) {}
public void onPlayerRespawn(EntityPlayer player) {}
}

onLogin method gets called every time a player joins a server (in Single Player too).

 

You register player trackers in Your main mod class, preferably in load method.

GameRegistry.registerPlayerTracker(new PlayerTracker());

Thank you. That's exactly what I need. However, for my event, I need the world instance. I don't know how to get it.

Ok. Now to change my data, what do I do? I actually don't have a clue. Sorry.

Once you created it (note: you must only do this once per world!) you use loadItemData() to get your data back. Then modify it and use markDirty() to notify minecraft it has changed.

What do you mean by "get your data back"?

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

Link to comment
Share on other sites

Thank you all for your help! I've got the player joining event sorted, just not the data thing sorted.

 

I've registered the data class in my load method and I've tried loading it in my event class but I don't know how to access the specific variables in the data class. So how do I do this?

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

Link to comment
Share on other sites

I've also had some other small problems over the last day or so with my mod.

 

I've been spawning in items with different damage values, but some damage values aren't spawned. It will make no sense, so here:

int randomChange = rand.nextInt(50);
			int damageValue;
			if (randomChange < 3)
			{
				damageValue = 4; //rare item damage value
			}
			else if (randomChange < 20)
			{
				damageValue2 = 2 + rand.nextInt(1); //uncommon items, either damage value of 2 or 3. only difference is texture
			}
			else
			{
				damageValue2 = 0 + rand.nextInt(1); //same as above, but for common items.
			}
			EntityItem item = new EntityItem(world, x, y, z, new ItemStack(Main.CustomItem, 1, damageValue));
			world.spawnEntityInWorld(item);

So basicly it only spawns the items with damage values of 0, 2 and 4. I want 1 and 3 also in there. This used to work though! I don't know what has changed since it did.

 

And second basic problem:

@Override
public void onPlayerStoppedUsing(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer, int par4) 
{
//stuff here
}

This isn't getting called, even though it recognizes the method (it has a javadoc).

 

Other big problem: I still can't get the WorldData thingy to work. I need help on this. A PM would be great.

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

Link to comment
Share on other sites

damageValue = 4; //rare item damage value

}

else if (randomChange < 20)

{

damageValue2 =

 

damageValue, damageValue2  - it's not working because of a typo, I believe.

 

 

 

As For onPlayerStoppedUsing, You may need to specify getMaxItemUseDuration or getItemUseAction.

 

Also, do You call

par3EntityPlayer.setItemInUse(par1ItemStack, this.getMaxItemUseDuration(par1ItemStack));

in onItemRightClick?

Link to comment
Share on other sites

damageValue = 4; //rare item damage value

}

else if (randomChange < 20)

{

damageValue2 =

 

damageValue, damageValue2  - it's not working because of a typo, I believe.

 

 

 

As For onPlayerStoppedUsing, You may need to specify getMaxItemUseDuration or getItemUseAction.

 

Also, do You call

par3EntityPlayer.setItemInUse(par1ItemStack, this.getMaxItemUseDuration(par1ItemStack));

in onItemRightClick?

That is a typo, just not in my code. I tried to clear up my variables, sorry.

 

As for the second thing, I'll try it.

 

Edit: That didn't seem to do what I want. I want the item to spawn smoke while the tight mouse button is held down but run an event once released.

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

Link to comment
Share on other sites

From oracle docs:

 

int nextInt(int n)

          Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.

 

 

So, You need to have

rand.nextInt(2).

.

 

I think it may have changed with the last version of Java.

 

As for smoke, use:

 

onUsingItemTick(ItemStack stack, EntityPlayer player, int count)

To spawn smoke, and

onPlayerStoppedUsing

to run the event.

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