Jump to content

[1.7.10] Wrong Event?


Robo51

Recommended Posts

I finally got off my lazy butt and decided to work on my mod again, and I am currently working to fix all the "little" things first.

 

So I have an event that causes the player to take damage in the water if a value is <= 0.

 

Event:

 

 

package robo51.newt.handlers;

 

import net.minecraft.client.Minecraft;

import net.minecraft.entity.EntityLivingBase;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.util.ChatComponentText;

import net.minecraft.util.DamageSource;

import net.minecraft.world.World;

import net.minecraftforge.event.entity.EntityEvent.EntityConstructing;

import net.minecraftforge.event.entity.living.LivingEvent.LivingUpdateEvent;

 

import org.lwjgl.input.Keyboard;

 

import robo51.newt.entity.ExtendedPlayerWater;

import cpw.mods.fml.common.eventhandler.SubscribeEvent;

import cpw.mods.fml.common.gameevent.TickEvent.PlayerTickEvent;

import cpw.mods.fml.relauncher.Side;

import cpw.mods.fml.relauncher.SideOnly;

 

 

public class EventsHandler {

 

@SubscribeEvent

public void playerTickEvent(PlayerTickEvent event) {

EntityLivingBase entity;

 

if (event.player instanceof EntityPlayer) {

EntityPlayer player = (EntityPlayer) event.player;

if(event.player.isWet()) {

int watersealing = ExtendedPlayerWater.getCurrentWaterSealing();

if(watersealing <= 0) {

event.player.attackEntityFrom(DamageSource.drown, 1.0F);

} else {

--watersealing;

ExtendedPlayerWater.newCurrentWaterSealing(watersealing);

if(watersealing == 200) {

if(!event.player.worldObj.isRemote) {

player.addChatMessage(new ChatComponentText("5 Seconds left of WaterSealing."));

}

}

if(watersealing == 50) {

if(!event.player.worldObj.isRemote) {

player.addChatMessage(new ChatComponentText("1 Second left of WaterSealing!"));

}

}

}

}

if(event.player.isInWater()) {

event.player.setAir(10);

if(Keyboard.isKeyDown(57)) {

event.player.motionY = 0.005D;

if(event.player.isCollidedHorizontally) {

event.player.motionY = 0.2D;

}

} else {

event.player.motionY = -1.0D;

}

}

}

}

 

@SubscribeEvent

public void onEntityConstructing(EntityConstructing event) {

if (event.entity instanceof EntityPlayer && ExtendedPlayerWater.get((EntityPlayer) event.entity) == null) {

ExtendedPlayerWater.register((EntityPlayer) event.entity);

}

}

}

 

 

 

However this doesn't seem to do anything now, (it used to work but I remember changing the event a while back).

 

I feel like I'm either using the wrong event for this or not using the current event properly.

 

If someone could help me out with this, maybe point me in the right direction it would be much appreciated.  :)

I'm not trying to be rude it just comes out that way sometimes.

I'm here to try and learn as much as I can, won't you join me?

Link to comment
Share on other sites

Few things:

1. Tick event have Phases - you need to define (by if statement), START or END Phase (otherwise code will be ran twice).

2. You CAN'T use KeyBindings inside this tick event - mouse and keyboard is CLIENT side. You need to send packet that you clicked and/or unclicked key.

3. 20 tick = 1sec (you have 50=1sec and 200=5sec - wtf?)

 

Edit: I also remember you and your problem - we alredy told you all this above.

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

1. What is the difference between the START and END phase? I can't seem to find anything really explaining them.

2. I do remember the keybind problem I forgot to comment it out in the version I posted as I plan to try really hard to understand packets and how they work with the help of tutorials.

3. Those numbers are somewhat arbitrary at the moment  :-[

I'm not trying to be rude it just comes out that way sometimes.

I'm here to try and learn as much as I can, won't you join me?

Link to comment
Share on other sites

1. What is the difference between the START and END phase? I can't seem to find anything really explaining them.

 

Is kind of like the stay of a race and the end. There really isn't much difference at all. But I'm pretty sure the medals are only given our once, at the end.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

1. It's about game loop. 1tick = 50ms, in that time everything happens, updates of world, entities, tileentities, everything. If you'd look at code that initiates all those updates you would see that Forge with phase START is called 1st, then there are entity updates - so methods like onUpdate() inside entity (I like to call it MIDDLE phase or ACTUAL tick) and then END phase. Most of the time you will want to use START.

 

2. Not really much to talk about:

http://www.minecraftforge.net/forum/index.php/topic,20135.0.html

I was learnin on this, so I'll link it:

http://www.minecraftforum.net/forums/mapping-and-modding/mapping-and-modding-tutorials/2137055-1-7-x-1-8-customizing-packet-handling-with

 

3. Yeah, but important.

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

So I decided to redo this little section of code as I always find it easiest to start over.

 

I've started with this:

 

 

 

@SubscribeEvent

public void playerTickEvent(TickEvent.PlayerTickEvent event) {

 

    if(event.side.isServer() && event.phase.equals(TickEvent.Phase.START)) {

   

    if(event.player.isWet()) {

    event.player.attackEntityFrom(DamageSource.drown, 1.0F);

    }

    }

}

 

 

 

However the player takes no damage when in water or rain.

Am I referencing the player wrong here?

I'm not trying to be rude it just comes out that way sometimes.

I'm here to try and learn as much as I can, won't you join me?

Link to comment
Share on other sites

@SubscribeEvent
   public void playerTickEvent(TickEvent.PlayerTickEvent event) {
      
       if(event.phase == Phase.START && !event.player.worldObj.isRemote) {
          
          if(event.player.isWet()) {
             event.player.attackEntityFrom(DamageSource.drown, 1.0F);
          }    
       }
   }

 

^ more suitable code, also - ticks events are fired by FML - you need to register them using FML, not Forge bus.

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

Okay so I fixed up my playerTickEvent and got that all working. :)

 

And now I'm working on the next bit that handles water movement and requires a packet.

This is what I have so far:

 

Inside of the event handler:

 

package robo51.newt.handlers;

 

import org.lwjgl.input.Keyboard;

 

import robo51.newt.Newt;

import robo51.newt.entity.ExtendedPlayerWater;

import robo51.newt.packets.NewtMessage;

import robo51.newt.packets.PacketRegistry;

import net.minecraft.entity.player.EntityPlayerMP;

import net.minecraft.util.ChatComponentText;

import net.minecraft.util.DamageSource;

import cpw.mods.fml.common.eventhandler.SubscribeEvent;

import cpw.mods.fml.common.gameevent.TickEvent;

import cpw.mods.fml.common.gameevent.TickEvent.Phase;

 

 

public class WaterSealantHandler {

 

@SubscribeEvent

  public void playerTickEvent(TickEvent.PlayerTickEvent event) {

   

      if(event.phase == Phase.START && !event.player.worldObj.isRemote) {

       

          if(event.player.isWet()) {

          int watersealing = ExtendedPlayerWater.getCurrentWaterSealing();

          if(watersealing <= 0) {

         

              event.player.attackEntityFrom(DamageSource.drown, 1.0F);

          } else {

          --watersealing;

          ExtendedPlayerWater.newCurrentWaterSealing(watersealing);

          if(watersealing == 100) {

          if(!event.player.worldObj.isRemote) {

          event.player.addChatMessage(new ChatComponentText("5 Seconds left of sealant."));

          }

          }

          if(watersealing == 20) {

          if(!event.player.worldObj.isRemote) {

          event.player.addChatMessage(new ChatComponentText("1 Second left of sealant!"));

          }

          }

          }

          } 

      }

     

      if(event.player.isInWater()) {

      PacketRegistry.network.sendTo(new NewtMessage(), (EntityPlayerMP) event.player);

     

/* if(event.player.isInWater()) {

event.player.setAir(10);

if(Keyboard.isKeyDown(57)) {

event.player.motionY = 0.005D;

if(event.player.isCollidedHorizontally) {

event.player.motionY = 0.2D;

}

} else {

event.player.motionY = -1.0D;

}

}*/

}

}

}

 

 

And this is in the NewtMessage class:

 

package robo51.newt.packets;

 

import io.netty.buffer.ByteBuf;

import cpw.mods.fml.common.network.ByteBufUtils;

import cpw.mods.fml.common.network.simpleimpl.IMessage;

import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;

import cpw.mods.fml.common.network.simpleimpl.MessageContext;

 

public class NewtMessage implements IMessage {

 

private String text;

 

public NewtMessage() {

 

}

 

public NewtMessage(String text) {

this.text = text;

}

 

@Override

public void fromBytes(ByteBuf buf) {

        text = ByteBufUtils.readUTF8String(buf);

}

 

@Override

public void toBytes(ByteBuf buf) {

        ByteBufUtils.writeUTF8String(buf, text);

}

 

public static class Handler implements IMessageHandler<NewtMessage, IMessage> {

 

@Override

public IMessage onMessage(NewtMessage message, MessageContext ctx) {

            System.out.println(String.format("Received %s from %s", message.text, ctx.getServerHandler().playerEntity.getDisplayName()));

            return null;

}

 

}

}

 

 

 

I left the response for the packet as what came out of the tutorial for the moment so I could get the packet working first.

 

However I'm not sure what all needs to be transferred in the packet, I think I need the player?

And If I do need the player how do I properly transfer that since what I'm currently using doesn't work.

I'm not trying to be rude it just comes out that way sometimes.

I'm here to try and learn as much as I can, won't you join me?

Link to comment
Share on other sites

This is totally wrong, do:

1. Make KeyBinding for your key. Subscribe to InputEvent.KeyInputEvent and check wether key was pressed and/or unpressed.

1.1. On key pressed: set som boolean to true and send packet FROM client TO server that key is pressed (true).

1.2. On key unpressed send another packet - false (from client To server) that will tell server that you stopped holding it.

2. Make PlayerTickEvent ONLY (and ONLY) check your boolean on server side, since server will now know that you pressed button (this true/false) you can make if on that boolean and operate on server-side.

2.1. Do your stuff.

 

Notes:

Save that boolean per-player, so inside ExtendedPlayer (IEEP class).

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

So I've been working through the key binding and hit a small snag.

 

The key binding works fine for the client but the server doesn't like it.

I feel like this has to do with which side the key bindings need to be on, but one of the tutorials Ive been reading through mentioned it.

 

This is the KeyBindingHandler:

package robo51.newt.handlers;

 

import net.minecraft.client.settings.KeyBinding;

 

import org.lwjgl.input.Keyboard;

 

import cpw.mods.fml.client.registry.ClientRegistry;

 

public class KeyBindingHandler {

 

public static KeyBinding[] keyBindings;

 

    public static void init() {

   

        keyBindings[0] = new KeyBinding("key.ping", Keyboard.KEY_O, "key.categories.movement");

        keyBindings[1] = new KeyBinding("key.pong", Keyboard.KEY_P, "key.categories.movement");

 

        for (int i = 0; i < keyBindings.length; ++i) {

            ClientRegistry.registerKeyBinding(keyBindings);

        }

       

    }

 

}

 

 

 

And this is the KeyInputHandler:

package robo51.newt.handlers;

 

import cpw.mods.fml.common.eventhandler.SubscribeEvent;

import cpw.mods.fml.common.gameevent.InputEvent;

 

public class KeyInputHandler {

 

    @SubscribeEvent

    public void onKeyInput(InputEvent.KeyInputEvent event) {

        if(KeyBindingHandler.keyBindings[0].isPressed())

            System.out.println("ping");

        if(KeyBindingHandler.keyBindings[1].isPressed())

            System.out.println("pong");

    }

 

}

 

 

 

The server doesn't like this line in the bindinghandler:

        keyBindings[0] = new KeyBinding("key.ping", Keyboard.KEY_O, "key.categories.movement");

 

I'm not trying to be rude it just comes out that way sometimes.

I'm here to try and learn as much as I can, won't you join me?

Link to comment
Share on other sites

Because you're still calling that class from common code.  Proxy that shit.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Yeah, key bindings are only supposed to be on the client -- this allows each player to have different key bindings if they really wanted. As explained above, use proxy to only do the key binding stuff on client side, then send packet to server to get it to respond in whatever way you want. I have a tutorial here (on the key binding part): http://jabelarminecraft.blogspot.com/p/minecraft-forge-1721710-keybinding.html

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

Okay now I'm kinda confused  :-\

 

Ive moved to the ClientProxy:

package robo51.newt.proxy;

 

import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;

import net.minecraft.client.settings.KeyBinding;

import net.minecraft.item.Item;

import net.minecraftforge.client.MinecraftForgeClient;

 

import org.lwjgl.input.Keyboard;

 

import robo51.newt.blocks.ModBlocks;

import robo51.newt.entity.EntityCyborg;

import robo51.newt.model.ModelCyborg;

import robo51.newt.renderer.ItemRenderNeuralCellBlock;

import robo51.newt.renderer.RenderCyborg;

import robo51.newt.renderer.RenderNeuralCellBlock;

import robo51.newt.tileentities.TileEntityNeuralCellBlock;

import cpw.mods.fml.client.registry.ClientRegistry;

import cpw.mods.fml.client.registry.RenderingRegistry;

 

public class ClientProxy extends CommonProxy{

 

public static KeyBinding[] keyBindings;

   

    public static void init() {

   

        keyBindings[0] = new KeyBinding("key.ping", Keyboard.KEY_O, "key.categories.movement");

        keyBindings[1] = new KeyBinding("key.pong", Keyboard.KEY_P, "key.categories.movement");

 

        for (int i = 0; i < keyBindings.length; ++i) {

            ClientRegistry.registerKeyBinding(keyBindings);

        }     

    }

 

    public void registerRenderThings() {

    //Neural Cell

    TileEntitySpecialRenderer render = new RenderNeuralCellBlock();

    ClientRegistry.bindTileEntitySpecialRenderer(TileEntityNeuralCellBlock.class, render);

    MinecraftForgeClient.registerItemRenderer(Item.getItemFromBlock(ModBlocks.neuralcellBlock), new ItemRenderNeuralCellBlock(render, new TileEntityNeuralCellBlock()));

   

    //Entities

    RenderingRegistry.registerEntityRenderingHandler(EntityCyborg.class, new RenderCyborg(new ModelCyborg(), 0.3F));

   

    }

   

    public void registerTileEntitySpecialRenderer() {

   

    }

}

 

 

 

And this is what in now in the KeyInputHandler:

package robo51.newt.handlers;

 

import net.minecraft.client.settings.KeyBinding;

import robo51.newt.proxy.ClientProxy;

import cpw.mods.fml.common.eventhandler.EventPriority;

import cpw.mods.fml.common.eventhandler.SubscribeEvent;

import cpw.mods.fml.common.gameevent.InputEvent.KeyInputEvent;

import cpw.mods.fml.relauncher.Side;

import cpw.mods.fml.relauncher.SideOnly;

 

public class KeyInputHandler {

 

@SideOnly(Side.CLIENT)

@SubscribeEvent(priority=EventPriority.NORMAL, receiveCanceled=true)

public void onEvent(KeyInputEvent event) {

    System.out.println("Key Input Event");

    KeyBinding[] keyBindings = ClientProxy.keyBindings;

 

    if (keyBindings[0].isPressed()) {

        System.out.println("Key binding ="+keyBindings[0].getKeyDescription());

        // do stuff for this key binding here

        // remember you may need to send packet to server

    }

 

    if (keyBindings[1].isPressed()) {

        System.out.println("Key binding ="+keyBindings[1].getKeyDescription());

        // do stuff for this key binding here

        // remember you may need to send packet to server

    }

 

}

 

 

 

}

 

 

 

And now the client errors the moment I press any key, I feel like its going to smack me in the face when I figure it out.

But I've got no idea now.

I'm not trying to be rude it just comes out that way sometimes.

I'm here to try and learn as much as I can, won't you join me?

Link to comment
Share on other sites

No, bad!

 

You should never directly reference your ClientProxy file.  That breaks the whole point of proxies.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Yeah, that's not the way to use proxies. In the approach that I use is that your main class should have methods that handle the FML "life cycle" events (what people typically call "pre-init", "init", "post-init"). Those should call the same method in the proxy instance. E.g. in my main class I'll have something like this:

 

 

@EventHandler

    // Do your mod setup. Build whatever data structures you care about. Register recipes."

    // Register network handlers

    public void fmlLifeCycleEvent(FMLInitializationEvent event)

    {

   

        // DEBUG

        System.out.println("init()");

       

        proxy.fmlLifeCycleEvent(event);

    }

 

 

 

Then your Common Proxy should have the same methods and should do whatever is actually common, for example in my CommonProxy:

 

public void fmlLifeCycleEvent(FMLInitializationEvent event)

    {

        // register custom event listeners

        registerEventListeners();

       

        // register recipes here to allow use of items from other mods

        registerRecipes();

       

        // register achievements here to allow use of items from other mods

        registerAchievements();

       

        // register gui handlers

        registerGuiHandlers();

}

 

 

 

Then in the ClientProxy which extends CommonProxy, call the super method for the common stuff then add the client-specific stuff:

 

    @Override

    public void fmlLifeCycleEvent(FMLInitializationEvent event)

    {

        // DEBUG

        System.out.println("on Client side");

 

        // do common stuff

        super.fmlLifeCycleEvent(event);

 

        // do client-specific stuff

        // registerClientPacketHandler();

        registerKeyBindings();

 

        // create sphere call list

        createSphereCallList();

       

        registerEntityRenderers();

        registerItemRenderers();

        registerBlockRenderers();

    }

 

 

 

So main class handles the init life cycle event by calling the proxy, and proxy will have appropriate methods based on side.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

Is:

  @EventHandler

    public void fmlLifeCycleEvent(FMLInitializationEvent event)

 

Similar to:

    @Mod.EventHandler

    public void init(FMLInitializationEvent event) {

 

Because this is what my main class looks like right now:

package robo51.newt;

 

import net.minecraft.client.Minecraft;

import net.minecraft.creativetab.CreativeTabs;

import net.minecraftforge.common.MinecraftForge;

import robo51.newt.blocks.ModBlocks;

import robo51.newt.entity.EntityCyborg;

import robo51.newt.gui.GuiWaterBar;

import robo51.newt.handlers.CraftingHandler;

import robo51.newt.handlers.EntityHandler;

import robo51.newt.handlers.FuelHandler;

import robo51.newt.handlers.GuiHandler;

import robo51.newt.handlers.KeyInputHandler;

import robo51.newt.handlers.RegisterEntityHandler;

import robo51.newt.handlers.WaterSealantHandler;

import robo51.newt.items.ModItems;

import robo51.newt.lib.Constants;

import robo51.newt.packets.PacketRegistry;

import robo51.newt.proxy.CommonProxy;

import robo51.newt.world.NewtWorldGenerator;

import cpw.mods.fml.common.FMLCommonHandler;

import cpw.mods.fml.common.Mod;

import cpw.mods.fml.common.Mod.Instance;

import cpw.mods.fml.common.SidedProxy;

import cpw.mods.fml.common.event.FMLInitializationEvent;

import cpw.mods.fml.common.event.FMLPostInitializationEvent;

import cpw.mods.fml.common.event.FMLPreInitializationEvent;

import cpw.mods.fml.common.network.NetworkRegistry;

import cpw.mods.fml.common.registry.GameRegistry;

 

@Mod(modid = Constants.MODID, name = Constants.MODNAME, version = Constants.VERSION)

 

public class Newt {

 

    @SidedProxy(clientSide = Constants.CLIENT_PROXY_CLASS, serverSide = Constants.SERVER_PROXY_CLASS)

    public static CommonProxy proxy;

 

    public static CreativeTabs tabNewt = new CreativeTabsNewt("NewtTab");

   

    @Instance(Constants.MODID)

    public static Newt instance;

   

    @Mod.EventHandler

    public void preInit(FMLPreInitializationEvent event) {

   

ModBlocks.init();

ModItems.init();

 

GameRegistry.registerFuelHandler(new FuelHandler());

GameRegistry.registerWorldGenerator(new NewtWorldGenerator(), 10);

 

EntityHandler.registerMonsters(EntityCyborg.class, "Cyborg");

 

PacketRegistry.init();

 

FMLCommonHandler.instance().bus().register(new KeyInputHandler());

 

proxy.registerRenderThings();

    }

 

    @Mod.EventHandler

    public void init(FMLInitializationEvent event) {

   

CraftingHandler.init();

        proxy.registerTileEntities();

        NetworkRegistry.INSTANCE.registerGuiHandler(this, new GuiHandler());

        MinecraftForge.EVENT_BUS.register(new RegisterEntityHandler());

        FMLCommonHandler.instance().bus().register(new WaterSealantHandler());

        FMLCommonHandler.instance().bus().register(new KeyInputHandler());

    }

   

    @Mod.EventHandler

    public void postInit(FMLPostInitializationEvent event) {

 

    if (FMLCommonHandler.instance().getEffectiveSide().isClient())

    MinecraftForge.EVENT_BUS.register(new GuiWaterBar(Minecraft.getMinecraft()));

    }

   

}

 

I'm not trying to be rude it just comes out that way sometimes.

I'm here to try and learn as much as I can, won't you join me?

Link to comment
Share on other sites

Yes, it doesn't actually matter what the name of the method is, or the name of the parameter either, rather the @EventHandler only looks at the parameter type passed. So a method that passes FMLInitializationEvent will be the same.

 

Okay, in the code you posted, you're registering the key handler in the main class. That should be in the client proxy lik,e we've been saying.

 

I personally would put all that other code into the CommonProxy and the KeyBinding stuff in the ClientProxy, although it is technically okay to have the common stuff in the main class method.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

I feel like I'm still missing something.

 

I've moved the register KeyInputHandler into the ClientProxy:

 

 

package robo51.newt.proxy;

 

import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;

import net.minecraft.client.settings.KeyBinding;

import net.minecraft.item.Item;

import net.minecraftforge.client.MinecraftForgeClient;

 

import org.lwjgl.input.Keyboard;

 

import robo51.newt.blocks.ModBlocks;

import robo51.newt.entity.EntityCyborg;

import robo51.newt.handlers.KeyInputHandler;

import robo51.newt.model.ModelCyborg;

import robo51.newt.renderer.ItemRenderNeuralCellBlock;

import robo51.newt.renderer.RenderCyborg;

import robo51.newt.renderer.RenderNeuralCellBlock;

import robo51.newt.tileentities.TileEntityNeuralCellBlock;

import cpw.mods.fml.client.registry.ClientRegistry;

import cpw.mods.fml.client.registry.RenderingRegistry;

import cpw.mods.fml.common.FMLCommonHandler;

import cpw.mods.fml.common.event.FMLInitializationEvent;

import cpw.mods.fml.common.event.FMLPreInitializationEvent;

 

public class ClientProxy extends CommonProxy {

 

public void FMLPreInitializationEvent(FMLPreInitializationEvent event) {

 

FMLCommonHandler.instance().bus().register(new KeyInputHandler());

 

}

 

public void FMLInitializationEvent(FMLInitializationEvent event) {

 

}

 

public static KeyBinding[] keyBindings;

   

    public static void init() {

   

        keyBindings[0] = new KeyBinding("key.ping", Keyboard.KEY_O, "key.categories.movement");

        keyBindings[1] = new KeyBinding("key.pong", Keyboard.KEY_P, "key.categories.movement");

 

        for (int i = 0; i < keyBindings.length; ++i) {

            ClientRegistry.registerKeyBinding(keyBindings);

        }     

    }

 

    public void registerRenderThings() {

    //Neural Cell

    TileEntitySpecialRenderer render = new RenderNeuralCellBlock();

    ClientRegistry.bindTileEntitySpecialRenderer(TileEntityNeuralCellBlock.class, render);

    MinecraftForgeClient.registerItemRenderer(Item.getItemFromBlock(ModBlocks.neuralcellBlock), new ItemRenderNeuralCellBlock(render, new TileEntityNeuralCellBlock()));

   

    //Entities

    RenderingRegistry.registerEntityRenderingHandler(EntityCyborg.class, new RenderCyborg(new ModelCyborg(), 0.3F));

   

    }

   

    public void registerTileEntitySpecialRenderer() {

   

    }

}

 

 

 

And still get the same error from the client upon test:

[16:09:37] [Client thread/ERROR] [FML]: Exception caught during firing event cpw.mods.fml.common.gameevent.InputEvent$KeyInputEvent@1835b24b:

java.lang.NullPointerException

at robo51.newt.handlers.KeyInputHandler.onEvent(KeyInputHandler.java:19) ~[KeyInputHandler.class:?]

at cpw.mods.fml.common.eventhandler.ASMEventHandler_6_KeyInputHandler_onEvent_KeyInputEvent.invoke(.dynamic) ~[?:?]

at cpw.mods.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:54) ~[ASMEventHandler.class:?]

at cpw.mods.fml.common.eventhandler.EventBus.post(EventBus.java:138) [EventBus.class:?]

at cpw.mods.fml.common.FMLCommonHandler.fireKeyInput(FMLCommonHandler.java:540) [FMLCommonHandler.class:?]

at net.minecraft.client.Minecraft.runTick(Minecraft.java:1953) [Minecraft.class:?]

at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:1028) [Minecraft.class:?]

at net.minecraft.client.Minecraft.run(Minecraft.java:951) [Minecraft.class:?]

at net.minecraft.client.main.Main.main(Main.java:164) [Main.class:?]

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_40]

at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_40]

at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_40]

at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_40]

at net.minecraft.launchwrapper.Launch.launch(Launch.java:135) [launchwrapper-1.11.jar:?]

at net.minecraft.launchwrapper.Launch.main(Launch.java:28) [launchwrapper-1.11.jar:?]

at net.minecraftforge.gradle.GradleStartCommon.launch(Unknown Source) [start/:?]

at GradleStart.main(Unknown Source) [start/:?]

 

 

Am I just repeating myself at this point?

I'm not trying to be rude it just comes out that way sometimes.

I'm here to try and learn as much as I can, won't you join me?

Link to comment
Share on other sites

This is the KeyInputHandler code:

package robo51.newt.handlers;

import net.minecraft.client.settings.KeyBinding;
import robo51.newt.proxy.ClientProxy;
import cpw.mods.fml.common.eventhandler.EventPriority;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.InputEvent.KeyInputEvent;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

public class KeyInputHandler {

@SideOnly(Side.CLIENT)
@SubscribeEvent(priority=EventPriority.NORMAL, receiveCanceled=true)
public void onEvent(KeyInputEvent event) {
    System.out.println("Key Input Event");
    KeyBinding[] keyBindings = ClientProxy.keyBindings; 

    if (keyBindings[0].isPressed()) { //line 19
        System.out.println("Key binding ="+keyBindings[0].getKeyDescription());
        // do stuff for this key binding here
        // remember you may need to send packet to server
    }

    if (keyBindings[1].isPressed()) {
        System.out.println("Key binding ="+keyBindings[1].getKeyDescription());
        // do stuff for this key binding here
        // remember you may need to send packet to server
    }

}



}

I'm not trying to be rude it just comes out that way sometimes.

I'm here to try and learn as much as I can, won't you join me?

Link to comment
Share on other sites

@EventHandler
public void preLoad(FMLPreInitializationEvent event)
{
	proxy.registerPre();
}

public class CommonProxy
{
@SideOnly(Side.CLIENT)
public KeyHandler keyHandler; //will exist only on client.jar

public void registerPre() {} // Empty, you will override it on clientProxy
}

public class ClientProxy extends CommonProxy
{
@Override
public void registerPre()
{
	keyHandler = new KeyHandler(); // this is keyHandler from superclass.
	FMLCommonHandler.instance().bus().register(keyHandler); //You register it only if you are running client.jar
}
}

 

The to reference it:

public void someMethod()
{
MainMod.proxy.keyHandler
}

On dedic.jar this will crash since CommonProxy doesn't have this field, but you don't use in on server, do you?

On client.jar you will get keyHandler from ClientProxy.

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

I've moved the register KeyInputHandler into the ClientProxy:

 

   public void FMLPreInitializationEvent(FMLPreInitializationEvent event) {
      
      FMLCommonHandler.instance().bus().register(new KeyInputHandler());
      
   }

 

...No, no you did not.  You referenced your KeyInputHandler in your main mod file.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

How would I go about properly registering the KeyInputHandler in the ClientProxy then?

I'm not trying to be rude it just comes out that way sometimes.

I'm here to try and learn as much as I can, won't you join me?

Link to comment
Share on other sites

Thank you for that. :)

 

I started from scratch and the keybinds work great now.

 

 

I move onto the packets and I'm stuck at what I actually send in the packet.

This is the KeyHandler:

 

package robo51.newt.handlers;

 

import robo51.newt.packets.PacketRegistry;

import robo51.newt.proxy.ClientProxy;

import net.minecraft.client.settings.KeyBinding;

import cpw.mods.fml.common.eventhandler.EventPriority;

import cpw.mods.fml.common.eventhandler.SubscribeEvent;

import cpw.mods.fml.common.gameevent.InputEvent.KeyInputEvent;

import cpw.mods.fml.relauncher.Side;

import cpw.mods.fml.relauncher.SideOnly;

 

public class KeyHandler {

 

private final boolean waterup = false;

 

@SideOnly(Side.CLIENT)

@SubscribeEvent(priority=EventPriority.NORMAL, receiveCanceled=true)

public void onEvent(KeyInputEvent event) {

    // make local copy of key binding array

    KeyBinding[] keyBindings = ClientProxy.keyBindings;

    // check each enumerated key binding type for pressed and take appropriate action

   

    if (keyBindings[0].isPressed()) { //WaterRise

        // DEBUG

        System.out.println("Key binding ="+keyBindings[0].getKeyDescription());

        waterup = true;

        PacketRegistry.network.sendToServer();

        // do stuff for this key binding here

        // remember you may need to send packet to server

    }

 

    if (keyBindings[1].isPressed()) {

        // DEBUG

        System.out.println("Key binding ="+keyBindings[1].getKeyDescription());

        // do stuff for this key binding here

        // remember you may need to send packet to server

    }

 

}

 

}

 

 

 

Now in the sendToServer(); bit what do I actually put in the parenthesis?

Is it something I choose or does it need to be something specific?

I'm not trying to be rude it just comes out that way sometimes.

I'm here to try and learn as much as I can, won't you join me?

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.