Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

So I'm developing my mod, but have run into a brick wall of sorts. I am trying to make it so that is an entity dies(In this case a skeleton) and the player kills it, it checks the players inventory for an item. If the player has the item it will replace it with another, but I cannot find a way to access the player's inventory with the LivingDropsEvent

 

The code:

 

 

else if (drops.entityLiving instanceof EntitySkeleton){

drops.source.

if (drops.source.getDamageType().equals("player")){

for (ItemStack stack : drops.source.getSourceOfDamage().inventory.Maininventory){

if (stack == null){

continue;

}

 

else {

if (stack.getItem() instanceof ItemTaintedEssence){

//Find a way to reference ItemStacks and use the

//SplitStack method with the size of the stack-1 as a parameter

for (int i = 0; i < drops.source.getEntity().inventory.Maininventory.length; i++){

if (drops.source.getEntity().inventory.Maininventory == stack){

drops.source.getSourceOfDamage().inventory.Maininventory.splitStack(stack.stackSize-1);

}

}

}

 

}

 

}

 

}

 

 

Every time I call (drops is an instance of LivingDropsEvent) drops.source.anything().inventory.mainInventory I get an error. Any help is appreciated, thanks.

Well, your code didn't work partly because the drops object passed into the event if from the dying entity, not from the player. 

 

What you need is to access the player that killed the entity.  I believe it would look something like this:

    @SubscribeEvent(priority=EventPriority.NORMAL, receiveCanceled=true)
    public void onEvent(LivingDropsEvent event)
    {
    	EntityLivingBase attacker = event.entityLiving.getLastAttacker();
    	if (attacker instanceof EntityPlayer)
    	{
                EntityPlayer attackingPlayer = (EntityPlayer)attacker;
    		// do what you want with attackingPlayer inventory here
    	}
    }

 

You need to understand better how Java works with referencing objects.  Basically you can string various fields and methods together but only as long as they belong and are publicly accessible.  And if you need to access a field of a subtype then you need to understand how to "cast" the object so that Java provides the required access.

 

Anyway, hope this helps.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

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

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.