Jump to content

[1.7.10] [solved] Retaining items on death/respawn


salvestrom

Recommended Posts

Sooooo... the below code in the spoiler, mostly hijacked from the actual ondeath method and including a modified version of the dropAllItems() method is intended to keep the armours listed on a player when they die. Which it does... at the instant of death, regular items are clearly being ejected while armour in the inventory is kept on the toolbar. Where it all goes wrong is when I hit the Respawn button. At this point everything still on the player is lost.

 

The GuiGameOver class has a line relating to this triggering a method in EntityClientPlayerMP to send a packet labelled "Perform_Respawn" which is where the code descends into a quagmire of functions and lines i do not understand.

 

I've found intriguing lines about clonePlayer and similar stuff to do with recreating inventory, but couldn't find where it's actually called from, let alone if it's what I'm after.

 

Anyway, so close and yet so far.

 

 

 

if(!this.thisworld.getGameRules().getGameRuleBooleanValue("keepInventory")

&& lde.entity instanceof EntityPlayerMP)

{

lde.setCanceled(true);

 

EntityPlayerMP ntt = (EntityPlayerMP)lde.entity;

 

        ntt.mcServer.getConfigurationManager().sendChatMsg(ntt.func_110142_aN().func_151521_b());

 

ItemStack[] ais = ntt.inventory.armorInventory;

 

 

for(int numi = 0; numi < ais.length; ++i)

{

if (ais[num] != null && ((ais[numi].getItem() == w2theJungle.obshelmet)

|| (ais[num].getItem() == w2theJungle.obschest)

|| (ais[num].getItem() == w2theJungle.obsboots)

|| (ais[num].getItem() == w2theJungle.obslegs)

))

{

ais[num] = ais[num];

}

else

{

ntt.func_146097_a(ais[num], true, false);

ais[num] = null;

}

}

 

ItemStack mis[] = ntt.inventory.mainInventory;

 

for(int i = 0; i < mis.length; ++i)

{

 

if(mis[num] != null && ((mis[num].getItem() == w2theJungle.obshelmet)

|| (mis[num].getItem() == w2theJungle.obschest)

|| (mis[num].getItem() == w2theJungle.obsboots)

|| (mis[num].getItem() == w2theJungle.obslegs)

))

{

mis[num] = mis[num];

}

else

{

ntt.func_146097_a(mis[num], true, false);

mis[num] = null;

}

}

 

note: lower half of code was removed since its pure copy/paste from onDeath()

 

 

 

Link to comment
Share on other sites

Thanks, the clone event was just what i needed.

 

Edit: just to be clear for anyone reading this later, both spoiler codes are required.

 

 

 

@SubscribeEvent

public void keepObsidianArmourII(PlayerEvent.Clone pec)

{

if(pec.wasDeath)

{

pec.entityPlayer.inventory.copyInventory(pec.original.inventory);

}

}

 

 

Link to comment
Share on other sites

The only thing my code is not doing from your first reply is pre-storing the items to be spared. It seems redundant given that the clone event is able to transfer over inventory - something that happens naturally on dimension transfer but isn't used on respawn because there's never anything to copy in vanilla.

 

the bus in the main mod file:

 

 

 

@EventHandler

public void load(FMLInitializationEvent vnt)

{

MinecraftForge.EVENT_BUS.register(new JungleLivingEvent());

}

 

 

 

the event class:

 

 

 

public class JungleLivingEvent {

 

@SubscribeEvent

public void keepObsidianArmour(LivingDeathEvent lde)

{

//the code in the first post is here, plus some straight copy/paste from ondeath() (in EntityPlayerMP)

        }

 

//the clone code is also located in this file

 

 

 

 

Link to comment
Share on other sites

"Again"??

 

Anyway, that code in my first post is comprised of three things:

 

1) the onDeath() method from EntityPlayerMP, which is where the livingDeathEvent hook is and which I am cancelling and overwriting.

 

2) the dropAllItems() method from InventoryPlayer, which is called in the onDeath() method. I have added it directly into my event overwrite, rather than call it as seperate method.

 

3) My code which consists of a check if the inventory slot contains my mod armour.

 

It's almost entirely vanilla code, as it appears in the code. I'm using the hook to affect one of only three things it can affect: player inventroy dump. This part was already working before I ever posted. The only hurdle was the fact that the respawn due to death does not copy inventory because in the vanilla game there never is an inventory to copy after death (I didn't get this at the time). You drew my attention to the clone event, which I decided to experiment with ahead of starting from scratch. The clone event provides access to the original inventory, now set by my new version of onDeath() which I can copy using pre-existing methods.

 

That's about all I'm doing.

 

if(I ever get this out there && myCodeBreaksOtherPeoplesMod)

{

      buyDiesieben07Beer == true;

}

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.