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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • yeah i did. tried activating and deactivating mods at random until it stopped crashing but it didn't change anything ty!
    • also, just had to set up account to post this and noticed like 3 times I had to pick the right segments of images to verify before moving on.  I can't imagine that there are enough threat actors with their attention on minecraft modding communities to justify that level of protection.  Maybe the guys making these amazing and cool mods are kind of jerks about it, hahah?  Maybe, like other companies, customer service or satisfaction isn't that high a priority.  I don't feel like they get paid very much for making Forge and Minecraft Mods, I mean I wasn't required to pay anything to the creators to set this up.   Anyway, just wanted to vent.
    • Ok, I found this site while searching for the missing version .jar file, which didn't show up in a net query through DuckDuckGo.  The thread on this site was closed after a comment from someone listed as a coder who stated that it was meant to work in this way, without a .jar file which is no longer necessary.  From earlier posts it seems that this person believed the OP had issues due to use of non vanilla launcher, which to me means he thinks they use a client or something like this.  The OP immediately said that they use vanilla, but doesn't seem like the coder believed them.  They began asking for logs, OP did not provide them.  No need to go further into details. I just reinstalled Windows on an SSD and just installed Minecraft Java, Optifine, Shader, Forge.  That is all that exists on my pc right now, and I had the same issue with 1.19.2 Forge versions not creating .jar file. Now I admit that I am likely missing something here and there is a technicality that isn't being fulfilled, but I have to say that it is not due to using a modified launcher.  All i have done is install optifine, put a shader into the shader folder, installed forge.  No mods yet, nothing.  What is interesting, and is the only reason I felt the need to post here, is that when I went into minecraft library folder and copied forge-1.19.2-43.1.65-client.jar, (I renamed it without the -client,) the Forge version starts up just fine with no further issues. From my point of view it seems that coders are dismissing some valid issues out of hand.  I mean, there are lots of really stupid and lazy people using computers and playing games, but not all of them are really stupid and lazy.  We all have bias, I know that I do as well, and I think it's helpful to be aware of our bias as far as we have the ability to be aware of it. Again, it could be that my assumption that there is a problem with the installer not creating a version .jar file, but I couldn't get it to work, always failed with the same message and then put the .jar file in the version folder and worked without a hitch. Oh, almost forgot, I also went into each installation and changed from 2 to 20GB RAM for the game.  In the past I had issues crashing with many mods installed due to lower RAM allocations and I have 48GB, so felt that was safe enough either way.  Maybe that caused the issue, for all I know.   Just saying, would have been nice if the coder had said go to (generic/vanilla path) and copy (version I used as example) forge-1.19.2-43.1.65-client, paste into forge version folder, rename to forge-1.19.2-43.1.65 and see if that does it.
    • What would make my mod incompatible? I'm very afraid of this. I was making some changes to the game using events. but they would only change items created in my own mod. What is the chance of incompatibility?
    • Hello, Im playing on 1.16.5 and Its keep crashing whenever I try to join a world and I get an error message saying "The game crashed whilst ticking memory connection Error: java.lang.NullPointerException: Ticking memory connection". Heres the crash log https://pastebin.com/rqZtSBms
  • Topics

×
×
  • Create New...

Important Information

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