Jump to content

Launch fireworks from player's location (and one other small question)


Recommended Posts

Posted

So I'm making a small levelling system. My original plan was to have fireworks launch when a player levels up, but after a couple of days of code-diving, I've been unable to figure this out. I've narrowed it down (I think) to creating a new EntityFireworkRocket and putting it into the world at the player's location, but have been unable to get it to work.

 

Does anyone know how to do this?

 

Also, as a sidenote, I've also been fairly unsuccessful in getting a mob to spawn only in villages. If anyone has any pointers on that, it would be much appreciated.

Posted

This is the code I use for handling players' levels, exp, etc:

 

 

 

public class ExtendedPlayerProperties implements IExtendedEntityProperties {

public static final String EXT_PROPERTY_NAME = "ExtendedPlayer";
public static final int LEVEL_WATCHER = 20;
public static final int XP_WATCHER = 21;

private final EntityPlayer player;
public int expToLevel, expOverflow, startingLevel, startingXp;

private int base;
private double exponent, multiplier;

public ExtendedPlayerProperties(EntityPlayer player)
{
	this.player = player;
	this.startingLevel = 1;
	this.expOverflow = 0;
	this.startingXp = 0;

	this.base = 2000;
	this.multiplier = 0.1;
	this.exponent = 3;

	this.expToLevel = (int)(this.multiplier*((Math.pow((double)this.startingLevel, this.exponent))+this.base));

	this.player.getDataWatcher().addObject(LEVEL_WATCHER, this.startingLevel);
	this.player.getDataWatcher().addObject(XP_WATCHER, this.startingXp);
}

public static final void register(EntityPlayer player)
{
	player.registerExtendedProperties(ExtendedPlayerProperties.EXT_PROPERTY_NAME, new ExtendedPlayerProperties(player));
}

public static final ExtendedPlayerProperties fetchProperties(EntityPlayer player)
{
	return (ExtendedPlayerProperties)player.getExtendedProperties(EXT_PROPERTY_NAME);
}

@Override
public void saveNBTData(NBTTagCompound tagComp)
{
	NBTTagCompound properties = new NBTTagCompound();

	properties.setInteger("CurrentLevel", this.player.getDataWatcher().getWatchableObjectInt(LEVEL_WATCHER));
	properties.setInteger("CurrentExp", this.player.getDataWatcher().getWatchableObjectInt(XP_WATCHER));
	properties.setInteger("ExpRemaining", this.expToLevel);

	tagComp.setTag(EXT_PROPERTY_NAME, properties);

	System.out.println("(TEST) SAVING DATA");
}

@Override
public void loadNBTData(NBTTagCompound tagComp)
{
	NBTTagCompound properties = (NBTTagCompound) tagComp.getTag(EXT_PROPERTY_NAME);

	this.player.getDataWatcher().updateObject(LEVEL_WATCHER, properties.getInteger("CurrentLevel"));
	this.player.getDataWatcher().updateObject(XP_WATCHER, properties.getInteger("CurrentExp"));
	this.expToLevel = properties.getInteger("ExpRemaining");

	System.out.println("(TEST) Level: " +this.player.getDataWatcher().getWatchableObjectInt(LEVEL_WATCHER)+ ", Exp to next level: " +this.expToLevel);
}

private static final String getSaveKey(EntityPlayer player)
{
	return player.getCommandSenderName() + ":" + EXT_PROPERTY_NAME;
}

public static final void saveProxyData(EntityPlayer player)
{
	ExtendedPlayerProperties playerData = ExtendedPlayerProperties.fetchProperties(player);
	NBTTagCompound savedData = new NBTTagCompound();
	playerData.saveNBTData(savedData);
	ServerProxy.storeEntityData(getSaveKey(player), savedData);
}

public static final void loadProxyData(EntityPlayer player)
{
	ExtendedPlayerProperties playerData = ExtendedPlayerProperties.fetchProperties(player);
	NBTTagCompound savedData = ServerProxy.getEntityData(getSaveKey(player));

	if (savedData != null)
	{
		playerData.loadNBTData(savedData);	
	}

	MCHardcore.packetHandler.sendTo(new SyncPlayerPropsPacket(player), (EntityPlayerMP) player);
}

@Override
public void init(Entity entity, World world)
{

}

public void addExp(int amount)
{
	expToLevel -= amount;
	int totalXpToLevel = calculateNewExpToLevel(getLevel());
	int currentXp = totalXpToLevel-expToLevel;

	this.player.getDataWatcher().updateObject(XP_WATCHER, currentXp);

	System.out.println("Exp: " +expToLevel);

	if(expToLevel <= 0)
	{
		if(expToLevel == 0)
		{
			this.player.getDataWatcher().updateObject(XP_WATCHER, 0);

			levelUp();
		}
		else if(expToLevel < 0)
		{
			this.expOverflow = expToLevel;

			this.player.getDataWatcher().updateObject(XP_WATCHER, expToLevel*-1);

			levelUp();
		}
	}
}

public int getLevel()
{
	return this.player.getDataWatcher().getWatchableObjectInt(LEVEL_WATCHER);
}

public void levelUp()
{
	int level = this.player.getDataWatcher().getWatchableObjectInt(LEVEL_WATCHER);

	this.player.getDataWatcher().updateObject(LEVEL_WATCHER, level+1);

	expToLevel = calculateNewExpToLevel(this.player.getDataWatcher().getWatchableObjectInt(LEVEL_WATCHER));
	this.expOverflow = 0;

	this.player.refreshDisplayName();
}

public int calculateNewExpToLevel(int level)
{
	return (int)(this.multiplier*((Math.pow((double)level, this.exponent))+this.base)) + this.expOverflow;
}

public int getCurrentXp()
{
	return this.player.getDataWatcher().getWatchableObjectInt(XP_WATCHER);
}

}

 

 

 

And the class which adds exp to players, handles display name changes, etc:

 

 

 

public class LevelHandler {

@SubscribeEvent
public void onEntityConstructing(EntityConstructing event)
{
	if(event.entity instanceof EntityPlayer && ExtendedPlayerProperties.fetchProperties((EntityPlayer)event.entity) == null)
	{
		ExtendedPlayerProperties.register((EntityPlayer)event.entity);
	}
}

@SubscribeEvent
public void joinEvent(EntityJoinWorldEvent event)
{
	if(!event.entity.worldObj.isRemote && event.entity instanceof EntityPlayer)
	{
		NBTTagCompound playerData = ServerProxy.getEntityData(((EntityPlayer)event.entity).getCommandSenderName());
		if(playerData != null)
		{
			((ExtendedPlayerProperties)(event.entity.getExtendedProperties(ExtendedPlayerProperties.EXT_PROPERTY_NAME))).loadNBTData(playerData);
		}
	}
}

@SubscribeEvent
public void onEntityDeath(LivingDeathEvent event)
{
	if(!event.entity.worldObj.isRemote && event.entity instanceof EntityPlayer)
	{
		NBTTagCompound playerData = new NBTTagCompound();
		((ExtendedPlayerProperties)(event.entity.getExtendedProperties(ExtendedPlayerProperties.EXT_PROPERTY_NAME))).saveNBTData(playerData);
		ServerProxy.storeEntityData(((EntityPlayer)event.entity).getCommandSenderName(), playerData);
		ExtendedPlayerProperties.saveProxyData((EntityPlayer)event.entity);

	}


	Entity entity = event.entity;
	DamageSource sauce = event.source;

	if(sauce.getSourceOfDamage() instanceof EntityPlayer)
	{

		if(ExtendedPlayerProperties.fetchProperties((EntityPlayer)sauce.getSourceOfDamage()) != null)
		{
			EntityPlayer player = (EntityPlayer)sauce.getSourceOfDamage();

			ExtendedPlayerProperties playerProperties = ExtendedPlayerProperties.fetchProperties(player);

			double mobHP = ((EntityLivingBase)event.entity).getEntityAttribute(SharedMonsterAttributes.maxHealth).getBaseValue();

			int expValue = 0;

			if(entity instanceof EntityMob)
			{
				expValue = (int)((mobHP*5)/4);
			}
			else if(entity instanceof EntityAnimal)
			{
				expValue = (int)((mobHP*3)/2);
			}
			else if(entity instanceof EntityPlayer)
			{
				ExtendedPlayerProperties mobProps = ExtendedPlayerProperties.fetchProperties((EntityPlayer)entity);
				int mobLevel = mobProps.getLevel();
				int base = 30;
				int modifier = (int) Math.floor(Math.pow((double)mobLevel, 2.5));

				expValue = 30 + modifier;
			}

			playerProperties.addExp(expValue);

		}
	}
}

@SubscribeEvent
public void nameFormat(NameFormat event)
{

	if(ExtendedPlayerProperties.fetchProperties(event.entityPlayer) != null)
	{
		System.out.println("[MCHardcore] Reformatting name for "+event.username);
		ExtendedPlayerProperties props = ExtendedPlayerProperties.fetchProperties(event.entityPlayer);
		int level = props.getLevel();
		event.displayname = event.username + " \u00A7a(Lvl " + level + ")";
	}
}

}

 

 

Posted

Hi

 

When you say it doesn't work, what do you mean exactly?  What do you expect to happen, and what actually happens?

 

Pls show your code for launching the rocket, assuming that's the part that doesn't work...

 

-TGG

Posted

Ah, sorry, I posted the version where I had removed the code.

 

I tried this:

 

EntityFireworkRocket firework = new EntityFireworkRocket(this.player.worldObj);
firework.setPosition(this.player.posX, this.player.posY, this.player.posZ);
this.player.worldObj.spawnEntityInWorld(firework);

 

By not working, I mean literally nothing happened.

Posted

Change firework.setPosition to

 

firework.setLocationandAngles(this.player.posX,this.player.posY,this.player.posZ,this.player.cameraYaw,this.player.cameraPitch);

 

I don't know, but I think the firework's data can be empty.

-Mitchellbrine

 

Minecraft can do ANYTHING, it's coded in Java and you got the full power of Java behind you when you code. So nothing is impossible.

It may be freaking fucking hard though, but still possible ;)

 

If you create a topic on Modder Support, live by this motto:

I don't want your charity, I want your information
Posted

Hi

 

You need to spawn on server not client.

 

This is the vanilla code for spawning the rocket, From ItemFirework.onItemUse(), try that:

 

    public boolean onItemUse(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, World par3World, int wx, int wy, int wz, int side, float hitVecX, float hitVecY, float hitVecZ)

    {

        if (!par3World.isRemote)

        {

            EntityFireworkRocket entityfireworkrocket = new EntityFireworkRocket(par3World, (double)((float) wx + hitVecX), (double)((float) wy + hitVecY), (double)((float) wz + hitVecZ), par1ItemStack);

            par3World.spawnEntityInWorld(entityfireworkrocket);

 

wx is world x coordinate

hitVecX is a number between 0 to 1 giving the position on the block where the player was looking (crosshairs) - or in other words the location where the player clicked.

 

-TGG

 

 

Posted

Thanks, that got the fireworks to spawn. One more thing, do you know how you add the information to the rocket entity for explosion type, flight length, etc? I'm assuming it's something to do with the addInformation() method.

Posted

Hi

 

I don't know much about fireworks.  Looking at the classes, I don't think addInformation() has anything to do with changing the properties, it just provides a "tooltip" for the existing properties of the firework.

 

The properties of the firework are contained in its NBT information.  This seems to be created in RecipeFireworks, mostly in matches() which caches the result to be later retrieved by getCraftingResult().  It builds up the NBT tag depending on the recipe and then attaches it to the Item using .setTagCompound(nbttagcompound);

 

You could figure out what the NBT tags are from looking at the code (perhaps a breakpoint to inspect what the final nbttagcompound is).

 

-TGG

Posted

I've spent a while looking at this, and I've figured out what some of the NBT tags are (mostly by comparing the code in RecipeFireworks to the crafting recipes on the wiki), but I've still been unable to add them to the firework itself. Does anyone know how this is done?

Posted

I've spent a while looking at this, and I've figured out what some of the NBT tags are (mostly by comparing the code in RecipeFireworks to the crafting recipes on the wiki), but I've still been unable to add them to the firework itself. Does anyone know how this is done?

Hi 

Did you try my suggestion

> It builds up the NBT tag depending on the recipe and then attaches it to the Item using .setTagCompound(nbttagcompound);

 

If so, please show your code.

 

The bits in vanilla which do it are here

                this.fireworkCraftingResult = new ItemStack(Item.firework);

                nbttagcompound = new NBTTagCompound();
                if (l > 0)
                {
                    nbttagcompound1 = new NBTTagCompound("Fireworks");
                    NBTTagList nbttaglist = new NBTTagList("Explosions");

                    for (int l1 = 0; l1 < par1InventoryCrafting.getSizeInventory(); ++l1)
                    {
                        ItemStack itemstack1 = par1InventoryCrafting.getStackInSlot(l1);

                        if (itemstack1 != null && itemstack1.itemID == Item.fireworkCharge.itemID && itemstack1.hasTagCompound() && itemstack1.getTagCompound().hasKey("Explosion"))
                        {
                            nbttaglist.appendTag(itemstack1.getTagCompound().getCompoundTag("Explosion"));
                        }
                    }

                    nbttagcompound1.setTag("Explosions", nbttaglist);
                    nbttagcompound1.setByte("Flight", (byte)j);
                    nbttagcompound.setTag("Fireworks", nbttagcompound1);
                }

                this.fireworkCraftingResult.setTagCompound(nbttagcompound);
                return true;

 

 

If you don't understand how to use NBT, try tracing through the RecipeFireworks to watch how it builds up the NBT and assigns it to the itemstack.

 

-TGG

Posted

Yep, I just got this working, thanks. I was being dumb and trying to set the tag compound AFTER creating the EntityFireworkRocket, rather than setting the tag compound to the ItemStack itself.

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.