Osuology Posted August 4, 2016 Share Posted August 4, 2016 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. Quote Link to comment Share on other sites More sharing options...
Osuology Posted August 4, 2016 Author Share Posted August 4, 2016 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()); } } Quote Link to comment Share on other sites More sharing options...
Osuology Posted August 5, 2016 Author Share Posted August 5, 2016 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! Quote Link to comment Share on other sites More sharing options...
Osuology Posted August 5, 2016 Author Share Posted August 5, 2016 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. Quote Link to comment Share on other sites More sharing options...
Animefan8888 Posted August 5, 2016 Share Posted August 5, 2016 Post new code please. Quote 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 More sharing options...
Osuology Posted August 5, 2016 Author Share Posted August 5, 2016 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()); } } Quote Link to comment Share on other sites More sharing options...
Animefan8888 Posted August 5, 2016 Share Posted August 5, 2016 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; } } Quote 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 More sharing options...
Osuology Posted August 5, 2016 Author Share Posted August 5, 2016 I'm aware but that doesn't actually cause the problem. It still crashes after adding offHand != null. Quote Link to comment Share on other sites More sharing options...
Animefan8888 Posted August 5, 2016 Share Posted August 5, 2016 Error log please. Quote 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 More sharing options...
pWn3d Posted August 5, 2016 Share Posted August 5, 2016 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. Quote Link to comment Share on other sites More sharing options...
Osuology Posted August 5, 2016 Author Share Posted August 5, 2016 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. Quote Link to comment Share on other sites More sharing options...
Animefan8888 Posted August 5, 2016 Share Posted August 5, 2016 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; } } } Quote 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 More sharing options...
Osuology Posted August 5, 2016 Author Share Posted August 5, 2016 Woops. I'm really tired. Ok thanks a lot! Quote Link to comment Share on other sites More sharing options...
Animefan8888 Posted August 5, 2016 Share Posted August 5, 2016 BTW when I said move I meant copy otherwise it won't work if you switch to a different item. Quote 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 More sharing options...
Recommended Posts
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.