Jump to content

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


Zer0HD2

Recommended Posts

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.

Link to comment
Share on other sites

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 + ")";
	}
}

}

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Hello! My friends and I were attempting to add a few extra mods to the Create Chronicles modpack, and all was well until people started crashing when they opened their inventories. Any help finding the culprit would be MUCH appreciated, I've been scratching my head for the past few days on what went wrong. https://paste.ee/p/8pajP
    • >>>KLIK LOGIN DISINI SAYANG<<< >>>KLIK DAFTAR DISINI SAYANG<<< Pendahuluan Dalam dunia perjudian online, slot menjadi salah satu permainan yang paling diminati. Dengan munculnya berbagai platform, Togel2Win hadir sebagai salah satu pilihan menarik, terutama dengan fitur anti rungkad yang dijanjikan. Artikel ini akan membahas tentang Togel2Win, keunggulan slot terbaru, dan bagaimana server Thailand berperan dalam meningkatkan pengalaman bermain. Apa Itu Togel2Win? Togel2Win adalah platform permainan yang menawarkan berbagai jenis permainan, termasuk slot dan togel. Dengan antarmuka yang ramah pengguna dan beragam pilihan permainan, situs ini bertujuan untuk memberikan pengalaman bermain yang menyenangkan dan menguntungkan bagi para pemain. Keunggulan Slot Togel2Win Fitur Anti Rungkad: Salah satu keunggulan utama dari Togel2Win adalah fitur anti rungkad yang dirancang untuk mengurangi kemungkinan gangguan saat bermain. Ini memastikan bahwa pemain dapat menikmati permainan tanpa gangguan teknis, meningkatkan kenyamanan dan fokus. Beragam Pilihan Slot: Togel2Win menawarkan berbagai jenis slot, dari yang klasik hingga yang modern dengan grafis menawan dan tema yang menarik. Ini memberikan variasi yang cukup bagi pemain untuk menemukan permainan yang sesuai dengan preferensi mereka. Server Thailand yang Stabil: Server yang berlokasi di Thailand memberikan koneksi yang cepat dan stabil. Ini sangat penting untuk pengalaman bermain yang lancar, terutama saat bermain slot yang memerlukan respons cepat. Bonus dan Promosi Menarik: Togel2Win sering menawarkan bonus dan promosi yang menarik untuk menarik pemain baru dan mempertahankan loyalitas pemain yang sudah ada. Ini bisa berupa bonus deposit, putaran gratis, atau program loyalitas. Tips untuk Pemain Slot di Togel2Win Pilih Slot dengan RTP Tinggi: Sebelum memulai permainan, pastikan untuk memilih slot dengan tingkat pengembalian pemain (RTP) yang tinggi untuk meningkatkan peluang menang. Kelola Anggaran: Tentukan batasan anggaran sebelum bermain dan patuhi itu. Ini membantu mencegah kerugian besar dan menjaga pengalaman bermain tetap menyenangkan. Manfaatkan Bonus: Jangan ragu untuk memanfaatkan bonus dan promosi yang ditawarkan. Ini bisa memberikan tambahan modal untuk bermain lebih lama. Kesimpulan Togel2Win merupakan pilihan menarik bagi para penggemar slot, terutama dengan fitur anti rungkad dan server yang stabil. Dengan berbagai pilihan permainan dan bonus yang menggiurkan, Togel2Win siap memberikan pengalaman bermain yang tak terlupakan. Jika Anda mencari platform slot yang andal dan menyenangkan, Togel2Win bisa menjadi solusi yang tepat.
    • I'm trying to make my own modpack, but sometimes, in certain areas of the world, the game just says "server closed". Minecraft doesn't close, it just returns to the menu. When I tried to figure it out on my own and understand the logs, I didn't understand anything (English is not my native language, so it's difficult for me). I've been trying to solve the problem for the third month. So I ask if anyone is good at this and it's not difficult for you, to help me with this. If you need details, ask. I'll describe everything. What it looks like Logs
  • Topics

×
×
  • Create New...

Important Information

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