Jump to content

[1.7] Command execution with keyboard key


HyperKut

Recommended Posts

Hello,


first of all I want to say that it's been a short time since I started the development of mod on Minecraft, and that I still have a hard time grasping some things.

I wanted to create a mod, when we press the key "B", it executes the command "/shop".


This mod works, but it only works in Singleplayer. When I put it on my server, the server crashes with the error "java.lang.NoClassDefFoundError: net / minecraft / client / entity / EntityClientPlayerMP", and when I go on the server with the mod only on my game, when I press the key nothing happens.

 

If anyone could help me, it would be really great especially since I do not understand much. Thank you in advance !

 

Base

 

package com.mod.divisionwar;

(lot of imports)

@Mod(modid = Base.MODID, version = Base.VERSION)
public class Base
{
    public static final String MODID = "divisionwarmod";
    public static final String VERSION = "1.0";
    
    @Mod.Instance(MODID)
    public static Base instance;
    
    @SidedProxy(clientSide = "proxy.ClientProxy", serverSide = "proxy.CommonProxy")
    public static CommonProxy proxy;
    
    public static final PacketPipeline packetPipeline = new PacketPipeline();
    
    @EventHandler
    public void load(FMLInitializationEvent event){
    	packetPipeline.initialize();
    	proxy.registerProxies();
        ItemMod.init();
        ItemMod.register();
    }
    
    @EventHandler
    public void postLoad(FMLPostInitializationEvent event){
    	packetPipeline.postInitialize();
    }

    

}

 

KeyHandler

 

package handler;

(lot of imports)

public class KeyHandler {
	
	public static final int BOUTIQUE_KEY = 0;
	
	private static String[] keyDesc = {"Shop"};
	private static final int[] keyValues = {Keyboard.KEY_B};
	private final KeyBinding[] keys;
	
	public KeyHandler(){
		keys = new KeyBinding[keyValues.length];
		for(int i = 0; i < keyValues.length; i++){
			keys[i] = new KeyBinding(keyDesc[i], keyValues[i], "DivisionWar - Controls");
			ClientRegistry.registerKeyBinding(keys[i]);
		}
	}
	
	@SubscribeEvent
	public void onKeyInput(InputEvent.KeyInputEvent event){
		if(!FMLClientHandler.instance().isGUIOpen(GuiChat.class)){
			int key = Keyboard.getEventKey();
			boolean isDown = Keyboard.getEventKeyState();
			
			// Boutique Key
			if(isDown && key == keyValues[SHOP_KEY]){
				// Send packet to open gui
				Base.packetPipeline.sendToServer(new OpenGuiPacket(References.GUI_SHOP));
			}
		}
	}

}

 

OpenGuiPacket

 

package packet;

(lot of imports)

public class OpenGuiPacket extends AbstractPacket{
	
	private byte id;
	
	public OpenGuiPacket(){}
	
	public OpenGuiPacket(byte id){
		this.id = id;
	}
	
	@Override
	public void encodeInto(ChannelHandlerContext ctx, ByteBuf buffer) {
		buffer.writeByte(id);
	}
	
	@Override
	public void decodeInto(ChannelHandlerContext ctx, ByteBuf buffer) {
		id = buffer.readByte();
	}
	
	@Override
	public void handleClientSide(EntityPlayer player) {

	}
	
	@Override
	public void handleServerSide(EntityPlayer player) {
		Minecraft.getMinecraft().thePlayer.sendChatMessage("/shop");
	}

}

 

PacketPipeline

 

package packet;

(lot of imports)

@ChannelHandler.Sharable
public class PacketPipeline extends MessageToMessageCodec<FMLProxyPacket, AbstractPacket> {
	
	private EnumMap<Side, FMLEmbeddedChannel> channels;
	private LinkedList<Class<? extends AbstractPacket>> packets = new LinkedList<Class<? extends AbstractPacket>>();
	private boolean isPostInitialized = false;
	
	public boolean registerPacket(Class<? extends AbstractPacket> 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(References.PACKET_CHANNEL, this);
		
		registerPackets();
	}
	
	public void postInitialize(){
		if(isPostInitialized)
			return;
		
		isPostInitialized = true;
		Collections.sort(this.packets, new Comparator<Class<? extends AbstractPacket>>() {
			@Override
			public int compare(Class<? extends AbstractPacket> o1, Class<? extends AbstractPacket> 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(OpenGuiPacket.class);
	}
	
	@Override
	protected void encode(ChannelHandlerContext ctx, AbstractPacket msg, List<Object> out) throws Exception {
		ByteBuf buffer = Unpooled.buffer();
		
		Class<? extends AbstractPacket> 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 AbstractPacket> clazz = this.packets.get(discriminator);
		if(clazz == null)
			throw new NullPointerException("This packet has never been registered:" + clazz.getCanonicalName());
		
		AbstractPacket 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(AbstractPacket message){
		this.channels.get(Side.CLIENT).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.TOSERVER);
		this.channels.get(Side.CLIENT).writeAndFlush(message);
	}

}

 

Edited by HyperKut
Link to comment
Share on other sites

  • Guest locked this topic
Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

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