Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Need some help with code


Recommended Posts

So i have my mod created, but someone has found an issue. Basically when using my mod with any other mod that allows flight via armor, or ring, or any way, my mod disables flight for ANY other mod out there. For example, my mod adds some armor, more specifically Topaz Armor that can fly with double tapping space bar. Say, I use the ArmorPlus mod with mine which has armor called The Ultimate Armor for example, if I double tap with that armor, I can fly. however if i use my mod with armorplus mod together, my mod still flys with my armor, but disables all flight for ArmorPlus mod, or for that matter any other mod. How can I fix this? I was told to do this but am confused.


"he explained that you don't need to (and shouldn't) change the flag every player tick. Just check when armor is equipped and then change the flag then and only then".


Here is my Event Handler for Flight for Topaz Armor.




Any help is greatly appreciated, thank you.


Link to comment
Share on other sites

I could be wrong, but I think it would be better to do this on something like an armor item's onUpdate event..by using the PlayerTick event, you're messing with anything any other mod might be using that event for..when you set the value of player.capabilities.allowFlying to false, you're doing this on a global scale..


Also..whoever told you that this doesn't need to happen every tick is correct..doing a check every tick should be fine (I would assume)... If it has changed from it's previous state, then update player accordingly

Link to comment
Share on other sites

Okay, for your Item (assuming it extends Item (net.minecraft.item.Item), you can override the onUpdate method which takes the following parameters: ItemStack, World, Entity, int, boolean


from Entity you can check that it is an instance of EntityPlayer.

if it is an instance of EntityPlayer, you can get the player's inventory to check if they have the required pieces of armor equipped

if they have all of the required pieces, you can check for capabilities.isFlying and so on...

Link to comment
Share on other sites

You DO need to use the PlayerTickEvent because Armor doesn't tick when it's not worn*, so the only way to remove the flying effect when no longer wearing your armor is via a tick event.


The trick to mod compatibility is to set allowFlying to true every tick that it should be true, but only set it to false ONCE when the condition is no longer met. To do that, you need a separate boolean 'wasFlyingAllowed'. This way, you only toggle flying off once, and any other mod that says flying is still allowed sets it during their tick event.


Note that it's still not perfect - if your tick event is last to evaluate, there will be one tick in which the player cannot fly and will begin to fall (if you also set #isFlying to false), even though the very next tick they will be allowed to fly again. It's a wonder there isn't yet a universal way to ensure inter-mod flying compatibility (at least that I've heard of), given how many mods add it.


* Yes, it still has #onUpdate called while in the player's inventory, but that won't happen if the armor is destroyed, moved to another inventory directly, etc. #onUpdate is not a reliable method of toggling off an ability.

Link to comment
Share on other sites

I was under the impression everything that is in InventoryPlayer is ticked..good to know =D

You are not wrong; where you err is in assuming that armor is always going to remain in InventoryPlayer.


I edited in some examples - armor breaks, is tossed or otherwise moved out of inventory directly from the equipment slot, or a mod simply sets it the slot to null or a different item. In all of those cases, the armor will not get a chance to have an update tick after being unequipped, and you'll be unable to toggle off whatever ability you had toggled on.


If, however, you were using AttributeModifiers rather than relying on update ticks, then it would be fine. Too bad there isn't an 'allow flying' attribute modifier... hmm... 'flySpeed' as a new SharedMonsterAttribute, anyone? :P

Link to comment
Share on other sites

All you need to do is add 'public boolean wasFlyingAllowed = false' to your event handler, set it to true when your armor is all on, and then only disable flying if it's true and not all of your armor is allowed:

// Note: you should check for NULL on all of these or you WILL crash
if (boots.getItem() == SlurpiesDonglesItems.topaz_boots && chestplate.getItem() == SlurpiesDonglesItems.topaz_chestplate && leggings.getItem() == SlurpiesDonglesItems.topaz_leggings && helmet.getItem() == SlurpiesDonglesItems.topaz_helmet) {
    event.player.capabilities.allowFlying = true;
    event.player.fallDistance = 0.0F;
    wasFlyingAllowed = true;
} elseif (wasFlyingAllowed) {
    wasFlyingAllowed = false; // now you won't be constantly setting allowFlying to false
    if (!event.player.capabilities.isCreativeMode) {
        event.player.capabilities.isFlying = false;
        event.player.capabilities.allowFlying = false;

Link to comment
Share on other sites

You didn't use the code as I wrote it, and you are using local variables instead of class fields.


- wasFlyingAllowed needs to be a class field so that it is remembered from tick to tick rather than being recalculated every tick.


- You don't need allowFlying at all - remove it.


- You moved your code to disable flying inside of your check for if (allowFlying), meaning that as soon as you are not allowed to fly, it is impossible for your code to disable it...


- Please take time to study basic Java and logic:

if (wasFlyingAllowed) {

} else if (wasFlyingAllowed) {  // this is the same condition you just checked - how can it possibly ever run?
  // dead code

Link to comment
Share on other sites

If your need is general programming ability rather than specific Forge-wrapper tips, then you should seek out a personal friend or family member who can tutor you in programming/Java. You should never ever ask someone here to write your code for you (remember this forum's description: "This is not Java school").

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

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.

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.


  • Create New...

Important Information

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