Jump to content

[1.7.10]Keybind making entity spawn?


Stefinus321

Recommended Posts

So I got a working keybind, but i can't spawn any entities in it. What now? I got the idea of sending a packet, but i don't know what i need to send with it nor how i then actually need to make the entity spawn (I'm quite new to packets).

 

KeyHandler (it works, but what now?)

 

package com.stefinus.Main.network;

 

import org.lwjgl.input.Keyboard;

import org.lwjgl.input.Mouse;

 

import com.stefinus.Main.SMainRegistry;

import com.stefinus.entity.EntityBullet;

import com.stefinus.items.GunStats;

import com.stefinus.lib.SRefStrings;

 

import cpw.mods.fml.client.FMLClientHandler;

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

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;

import net.minecraft.client.Minecraft;

import net.minecraft.client.settings.KeyBinding;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.item.ItemStack;

 

 

public class StefGunsKeyHandler

{

public static boolean pressed;

/** Key index for easy handling */

public static final int SHOOT = 0;

/** Key descriptions; use a language file to localize the description later */

private static final String[] desc = {"key.sg.buttonfire"};

/** Default key values */

private static final int[] keyValues = {Keyboard.KEY_M};

private final KeyBinding[] keys;

 

public StefGunsKeyHandler()

{

keys = new KeyBinding[desc.length];

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

{

keys = new KeyBinding(desc, keyValues, "key.sg.buttonfire");

ClientRegistry.registerKeyBinding(keys);

}

}

/**

* KeyInputEvent is in the FML package, so we must register to the FML event bus

*/

 

@SubscribeEvent

public void onKeyInput(KeyInputEvent event)

{

 

if (keys[sHOOT].isPressed())

{

 

}

 

}

}

 

 

 

PacketPipeline class (everything ok?)

 

 

package com.stefinus.Main.network;

 

import io.netty.buffer.ByteBuf;

import io.netty.buffer.Unpooled;

import io.netty.channel.ChannelHandler;

import io.netty.channel.ChannelHandlerContext;

import io.netty.handler.codec.MessageToMessageCodec;

 

import java.util.Collections;

import java.util.Comparator;

import java.util.EnumMap;

import java.util.LinkedList;

import java.util.List;

 

import net.minecraft.client.Minecraft;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.network.INetHandler;

import net.minecraft.network.NetHandlerPlayServer;

 

import com.stefinus.lib.SRefStrings;

 

import cpw.mods.fml.common.FMLCommonHandler;

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

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

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

import cpw.mods.fml.common.network.internal.FMLProxyPacket;

import cpw.mods.fml.relauncher.Side;

 

@ChannelHandler.Sharable

public class SGPacketPipeline extends MessageToMessageCodec<FMLProxyPacket, SGAbstractPacket> {

 

private EnumMap<Side, FMLEmbeddedChannel> channels;

private LinkedList<Class<? extends SGAbstractPacket>> packets = new LinkedList<Class<? extends SGAbstractPacket>>();

private boolean isPostInitialized = false;

 

public boolean registerPacket(Class<? extends SGAbstractPacket> clazz)

{

if(this.packets.size() > 256)

{

System.err.println("Maximum amount of packets reached!");

return false;

}

 

if(this.packets.contains(clazz))

{

System.err.println("This packet has already been registered!");

return false;

}

 

if(this.isPostInitialized)

{

System.err.println("Packet registered too late!");

return false;

}

 

this.packets.add(clazz);

return true;

}

 

public void initialize()

{

this.channels = NetworkRegistry.INSTANCE.newChannel(SRefStrings.PACKET_CHANNEL_NAME, this);

 

registerPackets();

}

 

public void postInitialize()

{

if(isPostInitialized)

return;

 

 

isPostInitialized = true;

 

Collections.sort(this.packets, new Comparator<Class<? extends SGAbstractPacket>>()

{

@Override

public int compare(Class<? extends SGAbstractPacket> o1, Class<? extends SGAbstractPacket> o2)

{

int com = String.CASE_INSENSITIVE_ORDER.compare(o1.getCanonicalName(), o2.getCanonicalName());

if(com == 0)

com = o1.getCanonicalName().compareTo(o2.getCanonicalName());

 

return com;

}

});

 

}

 

public void registerPackets()

{

registerPacket(ShootPackage.class);

}

@Override

protected void encode(ChannelHandlerContext ctx, SGAbstractPacket msg, List<Object> out) throws Exception

{

ByteBuf buffer = Unpooled.buffer();

 

Class<? extends SGAbstractPacket> clazz = msg.getClass();

if(!this.packets.contains(clazz))

throw new NullPointerException("This packet has never been registered" + clazz.getCanonicalName());

 

 

 

 

byte discriminator = (byte) this.packets.indexOf(clazz);

buffer.writeByte(discriminator);

msg.encodeInto(ctx, buffer);

 

FMLProxyPacket proxyPacket = new FMLProxyPacket(buffer.copy(), ctx.channel().attr(NetworkRegistry.FML_CHANNEL).get());

out.add(proxyPacket);

}

 

@Override

protected void decode(ChannelHandlerContext ctx, FMLProxyPacket msg, List<Object> out) throws Exception

{

ByteBuf payload = msg.payload();

byte discriminator = payload.readByte();

 

Class<? extends SGAbstractPacket> clazz = this.packets.get(discriminator);

if(clazz == null)

throw new NullPointerException("This packet has never been registered" + clazz.getCanonicalName());

 

SGAbstractPacket abstractPacket = clazz.newInstance();

abstractPacket.decodeInto(ctx, payload.slice());

 

EntityPlayer player;

switch (FMLCommonHandler.instance().getEffectiveSide())

{

case CLIENT:

player = Minecraft.getMinecraft().thePlayer;

abstractPacket.handleClientSide(player);

break;

case SERVER:

INetHandler iNetHandler = ctx.channel().attr(NetworkRegistry.NET_HANDLER).get();

player = ((NetHandlerPlayServer) iNetHandler).playerEntity;

abstractPacket.handleServerSide(player);

break;

default:

}

 

out.add(abstractPacket);

}

 

public void sendToServer(SGAbstractPacket message)

{

this.channels.get(Side.CLIENT).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.TOSERVER);

this.channels.get(Side.CLIENT).writeAndFlush(message);

}

}

 

 

 

 

The actual packet (what do i need to write where?)

 

 

package com.stefinus.Main.network;

 

import com.stefinus.Main.SMainRegistry;

import com.stefinus.entity.EntityBullet;

import com.stefinus.items.GunStats;

 

import cpw.mods.fml.common.network.internal.FMLNetworkHandler;

import io.netty.buffer.ByteBuf;

import io.netty.channel.ChannelHandlerContext;

import net.minecraft.client.Minecraft;

import net.minecraft.client.gui.inventory.GuiContainerCreative;

import net.minecraft.client.gui.inventory.GuiInventory;

import net.minecraft.client.renderer.entity.RenderManager;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.entity.projectile.EntitySnowball;

import net.minecraft.item.ItemStack;

import net.minecraft.world.World;

 

public class ShootPackage extends SGAbstractPacket

{

private byte shooting;

private float damage;

 

 

 

public ShootPackage(){}

 

public ShootPackage(byte shooting, float damage, int FireMax)

{

 

}

 

@Override

public void encodeInto(ChannelHandlerContext ctx, ByteBuf buffer)

{

 

}

 

@Override

public void decodeInto(ChannelHandlerContext ctx, ByteBuf buffer)

{

 

}

 

@Override

public void handleClientSide(EntityPlayer player)

{

 

}

 

@Override

public void handleServerSide(EntityPlayer player)

{

 

}

 

}

 

 

 

Link to comment
Share on other sites

Ok i changed to the Simple Network Wrapper (way easier than the PacketPipelin one). And now, say i wan't to spawn a snowball, that what i tried did not work.

 

the onMessage (the package is send to the server, ofcourse.

 

 

        World world = ctx.getServerHandler().playerEntity.worldObj;

        EntitySnowball ball = new EntitySnowball(world);

       

        if(!world.isRemote)// just to make sure...

        {

        ctx.getServerHandler().playerEntity.worldObj.spawnEntityInWorld(ball);

                //...and see proof of it in game

                        ctx.getServerHandler().playerEntity.addChatComponentMessage(new ChatComponentText("World is NOT remote"));

        }

       

        // test i did

            ctx.getServerHandler().playerEntity.addChatComponentMessage(new ChatComponentText("Package recieved, guess it's working"));

            return null;

 

 

I guess this is not the way to do it.

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.