Jump to content

[1.10.2] Using DataManager to sync client and server


Osuology

Recommended Posts

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.

 

 

Link to comment
Share on other sites

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());
    }
}

 

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

 

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());
    }
}

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

BTW when I said move I meant copy otherwise it won't work if you switch to a different item.

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.

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.

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

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

Announcements



×
×
  • Create New...

Important Information

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