The stack variable in updateFOV is null, but you're calling getItem() on it, causing the NPE crash. You need to check if stack is null and if it is, return from the method instead of continuing on with all the IZoom stuff.
To implement IZoom in your item class, add implements IZoom after extends Item and override the getZoomFactor() method. Eclipse and IDEA should be able to auto-generate a stub for the method when you add the interface.
Chisel Facades: For all your decorative pipe-hiding needs.
Please don't PM me to ask for help or to join your mod development team. Asking your question in a public thread preserves it for people who are having the same problem in the future. I'm not interested in developing mods with people.
In your Spyglass class, you never set the item in use, so it is impossible for your item to be used.
@Override
public int getMaxItemUseDuration(ItemStack stack) {
return 72000;
}
@Override
public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player) {
player.setItemInUse(stack, getMaxItemUseDuration(stack));
}
Add those to your class and you should be able to get a zoom, although I don't understand why you always set the zoom to 2.0F if it's not 1.0F - that defeats the whole purpose of the IZoom interface.
The Meaning of Life, the Universe, and Everything.
Join Date:
5/26/2013
Posts:
52
Minecraft:
Jared_Brian_
Member Details
oh i thought that was the point of implementing the IZoom interface into the class and oh i actually ment it to be if its not 1.0 to set it to 1.0 and when it is equal to 1.0 set it too 2.0 that way it toggles the view
The point of the interface is it lets each implementation decide what the zoom factor should be, rather than hard-coding 1.0F and 2.0F into your FOVUpdateEvent. This way, you can have one spyglass that doubles the zoom, and another that triples the zoom, and so on, as well as other items like microscopes, binoculars, whatever, all with varying levels of zoom.
You may also want to consider that the zoom only really zooms if it is a gradual change from the player's current view to the final magnified view - your current code may be quite jarring to suddenly jump to 2.0F zoom factor from 1 and vice versa.
so should i put a for loop in there so that it decreases to 1 or increases to the value more slowly?
No, because the for loop would iterate all at once, rather than each tick, and you end up with the same result at a higher cost. Go back and look at my code to see how it's done.
If you are still not getting zoom, make sure your event is actually working: put a 'System.out.println("FOV Update Event");' as the very first line in your 'public void updateFOV(FOVUpdateEvent event) {' method. Play the game. If your console is not getting spammed like crazy, then you did not properly register your event handler.
That's good. The next step in debugging is to add the same kind of print statements under all of your conditions, so you can see exactly what is going on and find out where your logic is failing:
@SubscribeEvent
public void updateFOV(FOVUpdateEvent event) {
ItemStack stack = event.entity.getItemInUse();
System.out.println("FOV event; item in use: " + stack); // add the stack so you can see what item is being used, if any
if (stack != null) {
boolean flag = stack.getItem() instanceof IZoom;
System.out.println("Stack not null; is IZoom? " + flag); // tells you if the item is an IZoom item
if (flag) { // You are only allowing IZoom items through here, so if it's anything else, the FOV will not be affected
// you already know it's an IZoom due to the check above, no need to check again
// float ZoomFactor = (flag ? ((IZoom) stack.getItem()).getZoomFactor() : 0.15F);
float zoomFactor = ((IZoom) stack.getItem()).getZoomFactor();
System.out.println("Zoom factor from IZoom item: " + zoomFactor);
if(zoomFactor!= 1.0) {
zoomFactor= 1.0F;
}
else { // an else statement - interesting choice here, I wonder if the following println will print? [answer: no]
System.out.println("Updating FOV with new zoomFactor");
event.newfov = zoomFactor;
}
}
}
}
boolean flag = stack.getItem() instanceof IZoom;
System.out.println("Stack not null; is IZoom? " + flag); // tells you if the item is an IZoom item
if (flag) {
float zoomFactor = ((IZoom) stack.getItem()).getZoomFactor();
System.out.println("Zoom factor from IZoom item: " + zoomFactor);
System.out.println("Updating FOV with new zoomFactor");
event.newfov = zoomFactor;
}
}
}
}
im sorry to everyone who had to deal with my extreme noobyness
but also Thank you so much to everyone for helping me
To implement an interface, you do exactly that:
Then add the required interface methods.
To implement IZoom in your item class, add implements IZoom after extends Item and override the getZoomFactor() method. Eclipse and IDEA should be able to auto-generate a stub for the method when you add the interface.
This tutorial explains interfaces in more detail.
Chisel Facades: For all your decorative pipe-hiding needs.
Please don't PM me to ask for help or to join your mod development team. Asking your question in a public thread preserves it for people who are having the same problem in the future. I'm not interested in developing mods with people.
item class:
package com.planetaryalignmentmod.items;
import com.planetaryalignmentmod.lib.IZoom;
import com.planetaryalignmentmod.lib.ModCreativeTabs;
import com.planetaryalignmentmod.lib.PlanetaryAlignmentMod;
import cpw.mods.fml.common.registry.GameRegistry;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.MinecraftForge;
public class Spyglass extends Item implements IZoom {
private String name = "spyglass";
public Spyglass(){
this.setUnlocalizedName(PlanetaryAlignmentMod.MODID + "_" + name);
this.setCreativeTab(ModCreativeTabs.tabPlanetaryAlignment);
GameRegistry.registerItem(this, name);
this.setTextureName(PlanetaryAlignmentMod.MODID + ":" + name);
this.setMaxStackSize(1);
}
public boolean isItemTool(ItemStack itemStack)
{
return super.isItemTool(itemStack);
}
@Override
public float getZoomFactor() {
return 5;
}
}
ModEvents:
package com.planetaryalignmentmod.lib;
import com.planetaryalignmentmod.items.ModItems;
import net.minecraft.item.ItemStack;
import net.minecraftforge.client.event.FOVUpdateEvent;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
public class ModEvents {
@SubscribeEvent
public void updateFOV(FOVUpdateEvent event) {
ItemStack stack = event.entity.getItemInUse();
if (stack != null) {
boolean flag = stack.getItem() instanceof IZoom;
if (flag) {
float ZoomFactor = (flag ? ((IZoom) stack.getItem()).getZoomFactor() : 0.15F);
if(ZoomFactor != 1.0){
event.newfov = 2.0F;
}
else{
event.newfov = 1.0F;
}
}
}
}
}
Add those to your class and you should be able to get a zoom, although I don't understand why you always set the zoom to 2.0F if it's not 1.0F - that defeats the whole purpose of the IZoom interface.
You may also want to consider that the zoom only really zooms if it is a gradual change from the player's current view to the final magnified view - your current code may be quite jarring to suddenly jump to 2.0F zoom factor from 1 and vice versa.
item class:
import com.planetaryalignmentmod.lib.IZoom;
import com.planetaryalignmentmod.lib.ModCreativeTabs;
import com.planetaryalignmentmod.lib.PlanetaryAlignmentMod;
import cpw.mods.fml.common.registry.GameRegistry;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
public class Spyglass extends Item implements IZoom {
private String name = "spyglass";
public Spyglass(){
this.setUnlocalizedName(PlanetaryAlignmentMod.MODID + "_" + name);
this.setCreativeTab(ModCreativeTabs.tabPlanetaryAlignment);
GameRegistry.registerItem(this, name);
this.setTextureName(PlanetaryAlignmentMod.MODID + ":" + name);
this.setMaxStackSize(1);
}
public boolean isItemTool(ItemStack itemStack)
{
return super.isItemTool(itemStack);
}
@Override
public float getZoomFactor() {
return 5;
}
@Override
public int getMaxItemUseDuration(ItemStack stack) {
return 72000;
}
@Override
public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player) {
player.setItemInUse(stack, getMaxItemUseDuration(stack));
return stack;
}
}
Mod Events:
import com.planetaryalignmentmod.items.ModItems;
import net.minecraft.item.ItemStack;
import net.minecraftforge.client.event.FOVUpdateEvent;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
public class ModEvents {
@SubscribeEvent
public void updateFOV(FOVUpdateEvent event) {
ItemStack stack = event.entity.getItemInUse();
if (stack != null) {
boolean flag = stack.getItem() instanceof IZoom;
if (flag) {
float ZoomFactor = (flag ? ((IZoom) stack.getItem()).getZoomFactor() : 0.15F);
if(ZoomFactor != 1.0){
ZoomFactor = 1.0F;
}
else{
event.newfov = ZoomFactor;
}
}
}
}
}
i appreciate all the help
No, because the for loop would iterate all at once, rather than each tick, and you end up with the same result at a higher cost. Go back and look at my code to see how it's done.
If you are still not getting zoom, make sure your event is actually working: put a 'System.out.println("FOV Update Event");' as the very first line in your 'public void updateFOV(FOVUpdateEvent event) {' method. Play the game. If your console is not getting spammed like crazy, then you did not properly register your event handler.
and the console is spamming the text
Stack not null; is IZoom? true
Zoom factor from IZoom item: 2.0
but no "Updating FOV with new zoomFactor"
Remove the damn "if" condition.
it finally works
all i did is took out that if and else stamtments
heres the event code:
package com.planetaryalignmentmod.lib;
import com.planetaryalignmentmod.items.ModItems;
import net.minecraft.item.ItemStack;
import net.minecraftforge.client.event.FOVUpdateEvent;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
public class ModEvents {
@SubscribeEvent
public void updateFOV(FOVUpdateEvent event) {
//System.out.println("FOV Update Event");
ItemStack stack = event.entity.getItemInUse();
if (stack != null) {
boolean flag = stack.getItem() instanceof IZoom;
System.out.println("Stack not null; is IZoom? " + flag); // tells you if the item is an IZoom item
if (flag) {
float zoomFactor = ((IZoom) stack.getItem()).getZoomFactor();
System.out.println("Zoom factor from IZoom item: " + zoomFactor);
System.out.println("Updating FOV with new zoomFactor");
event.newfov = zoomFactor;
}
}
}
}
im sorry to everyone who had to deal with my extreme noobyness
but also Thank you so much to everyone for helping me