Mine-and-blade-admin / Battlegear2

GNU General Public License v3.0
54 stars 54 forks source link

Modification of AbstractMBArrow.java for compatibility with mod entities that extend InventoryPlayer #232

Closed Goujer closed 8 years ago

Goujer commented 8 years ago

This should allow entities that have inventories extending InventoryPlayer to pick up arrows without causing a casting error with InventoryPlayerBattle that leads to a crash. Ex: LittleMaidMob

GotoLink commented 8 years ago

Why would modded entities call onCollideWithPlayer(EntityPlayer) ? Why would the EntityPlayer arg not have the inventory Battlegear gave them ? Your code doesn't allow to pick up the arrow if the inventory is not InventoryPlayerBattle.

Goujer commented 8 years ago

I should have provided a bit more context, I apologize.

LittleMaidMob has two entity classes for one mob, one called EntityLittleMaid and EntityLittleMaidAvatar. Most things are handled by EntityLittleMaid but to allow the maids to use player methods and do player actions EntityLittleMaid will use EntityLittleMaidAvatar which extends EntityPlayer to handle player style actions; As a result of this EntityLittleMaidAvatar has an inventory class that extends InventoryPlayer called InventoryLittleMaid. When a maid goes to pick up an arrow on the ground it will call the arrow's onCollideWithPlayer(EntityPlayer) EntityPlayer being its EntityLittleMaidAvatar. When BattleGear is installed the onCollideWithPlayer(EntityPlayer) method belongs to AbstractMBArrow.java which will then call tryPickArrow(EntityPlayer) and then attempt to cast InventoryPlayerBattle to EntityPlayer.inventory, but because InventoryLittleMaid extends InventoryPlayer not InventoryPlayerBattle; which will causing a casting error and crash the game.

The change would check if the EntityPlayer's Inventory does indeed extend InventoryPlayerBattle and will skip the casting and BattleGear related tasks if it doesn't, return true and proceed to execute the rest of onCollideWithPlayer(EntityPlayer) which I believe is the same as the vanilla onCollideWithPlayer(EntityPlayer), which should allow the entity to pick up the arrow normally.

Thank you for your patience, this error has been long requested to be resolved so LittleMaid and BattleGear can at least be ran together, and I can't find anyway to solve it on the LittleMaid side and it seems the easiest solution would be this change. I have no delusions that other mods may be implemented this way but this compatibility would at least allow players to enjoy both mods at the same time.

GotoLink commented 8 years ago

Alright, then the only issue is returning true in #tryPickArrow as if the arrow is picked, which it won't by returning early. Remove the else block to leave the default InventoryPlayer#addItemStackToInventory running, or return false if the LittleMaid mod can't support that.

Goujer commented 8 years ago

It should be able to handle that, I'll test this tonight and see if any more problems occur with this but it shouldn't.

Thank you again for your patience.

Goujer commented 8 years ago

Tested, and it works maids will pick up the arrows and from what I can tell the player interacts with the arrows the same way as well, I'm not an expert at BattleGear so I'm only assuming this so you may want to test it as well. I made this change to the 1.7.10 branch but it will probably need to be put in others to ensure compatibility with LittleMaid versions that support 1.8 and higher.