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

I assume DataManager is the same as DataWatcher for 1.7.10, but that might be my problem

 

If they are the same though, I'm just trying to detect whether the player is holding a item and if so, they can fly. I think what I have would work, but the problem is that I'm getting that information from the client which it obviously doesn't store, the server stores that. So I don't quite understand how to retrieve that information from the server.

 

 

  • Author

Well, my code as is causes a crash saying that the player is ticking.

 

My EventHandler:

 

package com.osuology.EventHandler;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.network.datasync.EntityDataManager;
import net.minecraft.util.EnumHand;
import net.minecraftforge.event.entity.living.LivingEvent.LivingUpdateEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

public class EH {

public EH()
{

}

@SubscribeEvent
public void CanFly(LivingUpdateEvent event)
{
	//System.out.println("Some event called; is this the client side? " + event.getEntity().worldObj.isRemote);
	if (event.getEntity() instanceof EntityPlayer)
	{
		//EntityDataManager dw = event.getEntity().getDataManager();
		EntityPlayer player = (EntityPlayer) event.getEntity();
		ItemStack heldItem = player.getHeldItem(EnumHand.MAIN_HAND);
		ItemStack offItem = player.getHeldItem(EnumHand.OFF_HAND);
		if (heldItem.getItem() != null)
		{
			if (heldItem.getItem() == Items.NETHER_STAR || offItem.getItem() == Items.NETHER_STAR)
			{
				player.capabilities.allowFlying = true;
			}
			else if (player.isCreative() != true)
			{
				player.capabilities.allowFlying = false;
			}
		}
	} 
}

}

 

 

And my main mod file:

 

package com.osuology.insanediff;

import com.osuology.EventHandler.EH;

import net.minecraft.init.Blocks;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;

@Mod(modid = insanediff.MODID, version = insanediff.VERSION)
public class insanediff
{
    public static final String MODID = "insanediff";
    public static final String VERSION = "1.0";
    
    @EventHandler
    public void init(FMLInitializationEvent event)
    {
        System.out.println("Insane Difficulty Mod installed!");
    }
    
    @EventHandler
    public void postInit(FMLPostInitializationEvent event)
    {
    	MinecraftForge.EVENT_BUS.register(new EH());
    }
}

 

  • Author

Oh I wasn't aware of PlayerTickEvent existing. I changed that.

 

Yes it is a NullPointerException, but checking if it's null doesn't help at all it still gives me a null pointer exception.

 

Anyways new code:

 

EventHandler:

 

package com.osuology.EventHandler;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.network.datasync.EntityDataManager;
import net.minecraft.util.EnumHand;
import net.minecraftforge.event.entity.living.LivingEvent.LivingUpdateEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent.PlayerTickEvent;

public class EH {

public EH()
{

}

@SubscribeEvent
public void CanFly(PlayerTickEvent event)
{
	//System.out.println("Some event called; is this the client side? " + event.getEntity().worldObj.isRemote);
	ItemStack heldItem = null;
	ItemStack offItem = null;
	EntityPlayer player = (EntityPlayer) event.player;
	if (player.getHeldItem(EnumHand.MAIN_HAND) != null)
	{
		heldItem = player.getHeldItem(EnumHand.MAIN_HAND);
		offItem = player.getHeldItem(EnumHand.OFF_HAND);
	}
	if (heldItem.getItem() != null)
	{
		if (heldItem.getItem() == Items.NETHER_STAR || offItem.getItem() == Items.NETHER_STAR)
		{
			player.capabilities.allowFlying = true;				
		}
		else if (player.isCreative() != true)
		{
			player.capabilities.allowFlying = false;
		}

		} 
	}
}


 

 

The mod class is the exact same.

 

EDIT: Never mind, I shouldn't call .getItem to check if it's null. It works now. Thanks!

  • Author

Actually, it's not working. When I hold the nether star I can fly, and if I switch to empty hand I still fly. And it crashes if I hold something else that's not a nether star.

  • Author

 

package com.osuology.EventHandler;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.network.datasync.EntityDataManager;
import net.minecraft.util.EnumHand;
import net.minecraftforge.event.entity.living.LivingEvent.LivingUpdateEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent.PlayerTickEvent;

public class EH {

ItemStack heldItem = null;
ItemStack offItem = null;

public EH()
{

}

@SubscribeEvent
public void CanFly(PlayerTickEvent event)
{
	//System.out.println("Some event called; is this the client side? " + event.getEntity().worldObj.isRemote);
	EntityPlayer player = (EntityPlayer) event.player;
	if (player.getHeldItem(EnumHand.MAIN_HAND) != null)
	{
		heldItem = player.getHeldItem(EnumHand.MAIN_HAND);
		offItem = player.getHeldItem(EnumHand.OFF_HAND);
	}

	if (heldItem != null)
	{
		if (heldItem.getItem() == Items.NETHER_STAR || offItem.getItem() == Items.NETHER_STAR)
		{
			player.capabilities.allowFlying = true;				
		}
		else
		{
			if (player.isCreative() == false)
				player.capabilities.allowFlying = false;
		}

		} 
	}
}


 

 

That's my event handler of course. My main mod file is:

 

package com.osuology.insanediff;

import com.osuology.EventHandler.EH;

import net.minecraft.init.Blocks;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;

@Mod(modid = insanediff.MODID, version = insanediff.VERSION)
public class insanediff
{
    public static final String MODID = "insanediff";
    public static final String VERSION = "1.0";
    
    @EventHandler
    public void init(FMLInitializationEvent event)
    {
        System.out.println("Insane Difficulty Mod installed!");
    }
    
    @EventHandler
    public void postInit(FMLPostInitializationEvent event)
    {
    	MinecraftForge.EVENT_BUS.register(new EH());
    }
}

 

You never check if the offHand is != null

 

if (heldItem != null)
	{
		if (heldItem.getItem() == Items.NETHER_STAR || offItem.getItem() == Items.NETHER_STAR)
		{
			player.capabilities.allowFlying = true;				
		}
		else
		{
			if (player.isCreative() == false)
				player.capabilities.allowFlying = false;
		}

		} 

 

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

  • Author

I'm aware but that doesn't actually cause the problem. It still crashes after adding offHand != null.

You could write it like that:

boolean netherStarMain = player.getHeldItem(EnumHand.MAIN_HAND) != null && player.getHeldItem(EnumHand.MAIN_HAND).getItem() == Items.NETHER_STAR;
boolean netherStarOff = player.getHeldItem(EnumHand.OFF_HAND) != null && player.getHeldItem(EnumHand.OFF_HAND).getItem() == Items.NETHER_STAR;


if (   netherStarMain || netherStarOff ){

 

You could also put both boolean statements directly into the if-condition, but that might be a bit unreadable.

  • Author

Well, first of all, I just found out that putting || offItem != null in the if loop causes the problem. I have gotten it to work. mostly. The only problem is when I switch from the nether star to empty hand, it doesn't change anything at all I still can fly.

 

@pWn3d I probably should do it that way, except offhand and main hand as separate if loops.

You only tell it to turn fly off if the players heldItem not the Stack is != null.

 

if (heldItem != null)
	{
		if (heldItem.getItem() == Items.NETHER_STAR || offItem.getItem() == Items.NETHER_STAR)
		{
			player.capabilities.allowFlying = true;				
		}
// Move this else down one bracket.
		else
		{
			if (player.isCreative() == false)
				player.capabilities.allowFlying = false;
		}

		} 
	}

 

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

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.