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

[1.12.2] player.isElytraFlying() is not being set to true when flying with custom elytra code


Rosy162
 Share

Recommended Posts

ok so i have a very specific issue that i dont know how to solve. im trying to make a mod which uses the baubles api that allows you to place the elytra in the baubles (body) slot. ive got pretty much everything to work but in EntityLivingBase, the method updateElytra() is checking if the chestplate slot has the elytra and if it doesnt it sets the player.isElytraFlying() method to false, regardless of if you are actually flying with the elytra. this is causing issues minecraft uses this method to decide when to calculate the elytra damage and so on... when im flying with the elytra in the baubles slot, player.isElytraFlying() is returning true and false repeatedly because of this method. i need it to always return true when you are flying but idk how to do that. hopefully someone smarter than me can help .-. sorry if im being stupid. here is the code for my custom elytra item:

 

 

@Mod.EventBusSubscriber
public class BaubleItemElytra extends ItemElytra implements IBauble {
	private static boolean render = true;
	public Method setFlagMethod;
	public BaubleItemElytra() {
		super();
		this.setUnlocalizedName("elytra");
		try {
          	// this method is what is used to make the player fly, it is private so i get it here ._.
			try {
				setFlagMethod = Entity.class.getDeclaredMethod("setFlag", int.class, boolean.class);
			} catch (Exception e) {
				setFlagMethod = Entity.class.getDeclaredMethod("func_70052_a", int.class, boolean.class);
			}
			setFlagMethod.setAccessible(true);
		} catch (NoSuchMethodException e) {}
	}
	@Override
	public BaubleType getBaubleType(ItemStack stack) {
		return BaubleType.BODY;
	}
  	//register item
	@SubscribeEvent
	public static void register(RegistryEvent.Register<Item> event) {
		event.getRegistry().register((new BaubleItemElytra().setRegistryName("minecraft", "elytra")));
	}
  	//registers the elytra render once,
  	//BaubleLayerElytra is just the same as the vanilla LayerElytra but it checks for the bauble slot
	@SubscribeEvent
	public static void onPlayerRender(RenderPlayerEvent.Pre event) {
		if (render) {
			event.getRenderer().addLayer(new BaubleLayerElytra(event.getRenderer()));
			render = false; 
		}
	}
  	//cant equip if you have elytra equipped already
	@Override
	public boolean canEquip(ItemStack itemstack, EntityLivingBase player) {
		return !slotHasElytra((EntityPlayer)player);
	}
	@Override
	public void onEquipped(ItemStack itemstack, EntityLivingBase player) {
		player.playSound(SoundEvents.ITEM_ARMOR_EQIIP_ELYTRA, .75F, 1.9f);
		//chatMessage(player, "equipped");
	}
	@Override
	public void onUnequipped(ItemStack itemstack, EntityLivingBase player) {
		//chatMessage(player, "unequipped");
		setFlying(player, false);
		player.playSound(SoundEvents.ITEM_ARMOR_EQIIP_ELYTRA, .75F, 1.8f);
	}
	public static boolean slotHasElytra(EntityPlayer player) {
		return player.getItemStackFromSlot(EntityEquipmentSlot.CHEST).getItem() instanceof ItemElytra;
	}
	@Override
	public void onWornTick(ItemStack itemstack, EntityLivingBase player) {
		//unequips elytra in bauble if elytra is in chestplate slot
		if (slotHasElytra((EntityPlayer)player)) {
			((EntityPlayer)player).addItemStackToInventory(itemstack.copy());
			BaublesApi.getBaublesHandler((EntityPlayer)player).setStackInSlot(5, ItemStack.EMPTY);;
		}
      
		boolean flag = player.isElytraFlying();
		//chatMessage(player, "check flying:" + player.isElytraFlying());
		if (flag && !player.onGround && !player.isRiding() && isUsable(itemstack)) {
			flag = true;
			if ((player.getTicksElytraFlying() + 1) % 20 == 0) {
				//itemstack.damageItem(1, player);
			}
		} else {
			flag = false;
          	//if you press jump in the right conditions you fly
			if (Minecraft.getMinecraft().gameSettings.keyBindJump.isPressed() && !player.isElytraFlying() && isUsable(itemstack) && player.motionY < 0.0D && !player.isInLava() && !player.isInWater() && !player.onGround) {
				flag = true;
			}
		}
		if (flag != player.isElytraFlying()) {
			setFlying(player, flag);
			//chatMessage(player, "flying set to: " + flag);
		}
	}
  	//sets the player to the flying state
	public void setFlying(EntityLivingBase player, boolean fly) {
		try { setFlagMethod.invoke(player, 7, fly); } 
		catch (Exception e) {}
	}
	private void chatMessage(EntityLivingBase player, String text) {
		((EntityPlayer)player).sendMessage(new TextComponentString(text));
	}
  	//right clicking puts it in the bauble body slot if its empty, if its full it puts it in the chestplate
	@Override
	public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, EnumHand hand) {
		player.playSound(SoundEvents.ITEM_ARMOR_EQIIP_ELYTRA, .75F, 1.9f);
		if (!world.isRemote) {
			IBaublesItemHandler baubles = BaublesApi.getBaublesHandler(player);
			ItemStack stack = baubles.getStackInSlot(5);
			if ((stack == null || stack.isEmpty()) && baubles.isItemValidForSlot(5, player.getHeldItem(hand), player)) {
				baubles.setStackInSlot(5, player.getHeldItem(hand).copy());
				if (!player.capabilities.isCreativeMode) {
					player.inventory.setInventorySlotContents(player.inventory.currentItem, ItemStack.EMPTY);
				}
				//onEquipped(player.getHeldItem(hand), player);
			} else {
				super.onItemRightClick(world, player, hand);
			}	
		}
		return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, player.getHeldItem(hand));
	}
}

 

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share



×
×
  • Create New...

Important Information

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