private static void processPacketHandlers() throws IOException, ClassNotFoundException { packetHandlers = new HashMap<Class<? extends Packet>, Class<? extends PacketCommand>>(); Set<String> annotationIndex = getAnnotationIndex(PACKET_HANDLER_PATH); for (String sClazz : annotationIndex) { Class<?> clazz = Class.forName(sClazz); Class<? extends PacketCommand> handler = (Class<? extends PacketCommand>) clazz; PacketHandler annotation = handler.getAnnotation(PacketHandler.class); Class<? extends Packet> value = annotation.value(); packetHandlers.put(value, handler); } }
@Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { if (ctx.channel().isActive()) { if (cause instanceof ReadTimeoutException) { ProxyServer.getInstance().getLogger().log(Level.WARNING, "{0} - read timed out", handler); } else if (cause instanceof BadPacketException) { ProxyServer.getInstance() .getLogger() .log(Level.WARNING, "{0} - bad packet ID, are mods in use!?", handler); } else if (cause instanceof IOException) { ProxyServer.getInstance() .getLogger() .log( Level.WARNING, "{0} - IOException: {1}", new Object[] {handler, cause.getMessage()}); } else { ProxyServer.getInstance() .getLogger() .log(Level.SEVERE, handler + " - encountered exception", cause); } if (handler != null) { try { handler.exception(cause); } catch (Exception ex) { ProxyServer.getInstance() .getLogger() .log(Level.SEVERE, handler + " - exception processing exception", ex); } } ctx.close(); } }
@Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { if (handler != null) { handler.disconnected(channel); if (!(handler instanceof InitialHandler || handler instanceof PingHandler)) { ProxyServer.getInstance().getLogger().log(Level.INFO, "{0} has disconnected", handler); } } }
/** * Delivers a packet that was received from the client. * * @param packet the wrapper around the JSON object containing the message */ public void recvRequest(Packet packet) { int suppliedId = packet.getPlayerId(); if (player == null && suppliedId != Packet.PLAYER_ID_NOT_LOGGED_IN) { // impersonation attempt throw new ClientModifiedException( "hacking attempt detected: not logged in, but supplied id = " + packet.getPlayerId()); } else if (player != null && packet.getPlayerId() != player.getId()) { // impersonation attempt throw new ClientModifiedException( "hacking attempt detected: wrong player id, is " + player.getId() + " but supplied " + packet.getPlayerId()); } // passed the hacking checks, find the handler String action = packet.getAction(); PacketHandler handler = (player == null) ? packetHandlersNoAuth.get(action) : packetHandlers.get(action); if (handler == null) { throw new ClientModifiedException("hacking attempt detected: action = " + action); } // finally, handle the packet handler.handlePacket(this, packet); }
@Override public void updateEntity() { if (this.worldObj.isRemote) { simpleAnimationIterate(); return; } if (this.worldObj.getWorldTime() % 20 == 7) { PacketDispatcher.sendPacketToAllAround( this.xCoord, this.yCoord, this.zCoord, 256, this.worldObj.provider.dimensionId, PacketHandler.getPacketFromNBT(this)); } this.ticks++; for (int i = this.efficiency + 1; i > 0; i--) { Recipe r = RefineryRecipes.findRefineryRecipe(this.src1, this.src2); if (r == null) { decreaseAnimation(); this.ticks = 0; return; } if (this.res != null && r.result.amount > (this.buf - this.res.amount)) { decreaseAnimation(); return; } if (r.delay > this.ticks) return; if (i == 1) this.ticks = 0; if (!PowerManager.useEnergyR(this.pp, r.energy, this.unbreaking)) { decreaseAnimation(); return; } increaseAnimation(); if (r.ingredient1.isFluidEqual(this.src1)) this.src1.amount -= r.ingredient1.amount; else this.src2.amount -= r.ingredient1.amount; if (r.ingredient2 != null) { if (r.ingredient2.isFluidEqual(this.src2)) this.src2.amount -= r.ingredient2.amount; else this.src1.amount -= r.ingredient2.amount; } if (this.src1 != null && this.src1.amount == 0) this.src1 = null; if (this.src2 != null && this.src2.amount == 0) this.src2 = null; if (this.res == null) this.res = r.result.copy(); else this.res.amount += r.result.amount; } }
/** * BLOCKING if queue between here and PacketHandler is full. * * @return zero (was queue size) */ private final int doReceive(UDPPacket packet) { if (!_keepRunning) return 0; if (_log.shouldLog(Log.INFO)) _log.info("Received: " + packet); RemoteHostId from = packet.getRemoteHost(); if (_transport.isInDropList(from)) { if (_log.shouldLog(Log.INFO)) _log.info("Ignoring packet from the drop-listed peer: " + from); _context.statManager().addRateData("udp.ignorePacketFromDroplist", packet.getLifetime()); packet.release(); return 0; } // drop anything apparently from our IP (any port) if (Arrays.equals(from.getIP(), _transport.getExternalIP()) && !_transport.allowLocal()) { if (_log.shouldLog(Log.WARN)) _log.warn("Dropping (spoofed?) packet from ourselves"); packet.release(); return 0; } /** * ** packet.enqueue(); boolean rejected = false; int queueSize = 0; long headPeriod = 0; * * <p>UDPPacket head = _inboundQueue.peek(); if (head != null) { headPeriod = * head.getLifetime(); if (headPeriod > MAX_QUEUE_PERIOD) { rejected = true; } } if (!rejected) * { ** */ try { _handler.queueReceived(packet); } catch (InterruptedException ie) { packet.release(); _keepRunning = false; } // return queueSize + 1; return 0; /** * ** } * * <p>// rejected packet.release(); _context.statManager().addRateData("udp.droppedInbound", * queueSize, headPeriod); if (_log.shouldLog(Log.WARN)) { queueSize = _inboundQueue.size(); * StringBuilder msg = new StringBuilder(); msg.append("Dropping inbound packet with "); * msg.append(queueSize); msg.append(" queued for "); msg.append(headPeriod); msg.append(" * packet handlers: ").append(_transport.getPacketHandlerStatus()); _log.warn(msg.toString()); } * return queueSize; ** */ }
public static void handle() { if (FMLCommonHandler.instance().getEffectiveSide() == Side.CLIENT) { ClientHelper.getPlayer() .addChatComponentMessage( new ChatComponentText("All player data for Progression was reset.")); } else { if (Options.editor) { Progression.instance .createWorldData(); // Recreate the world data, Wiping out any saved information for // players RemappingHandler.reloadServerData(); for (EntityPlayer player : PlayerHelper.getAllPlayers()) { RemappingHandler.onPlayerConnect((EntityPlayerMP) player); } PacketHandler.sendToEveryone(new PacketReset()); } } }
private void sendNowPacket() { try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(bos); dos.writeInt(this.xCoord); dos.writeInt(this.yCoord); dos.writeInt(this.zCoord); dos.writeByte(PacketHandler.StC_NOW); dos.writeFloat(this.animationSpeed); PacketDispatcher.sendPacketToAllAround( this.xCoord, this.yCoord, this.zCoord, 256, this.worldObj.provider.dimensionId, PacketHandler.composeTilePacket(bos)); } catch (Exception e) { e.printStackTrace(); } }
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { if (handler != null) { PacketWrapper packet = (PacketWrapper) msg; boolean sendPacket = true; try { if (packet.packet != null) { try { packet.packet.handle(handler); } catch (CancelSendSignal ex) { sendPacket = false; } } if (sendPacket) { handler.handle(packet); } } finally { packet.trySingleRelease(); } } }
@SideOnly(Side.CLIENT) @SubscribeEvent /** Grabs mouse scroll events for slope selection. */ public void onMouseEvent(MouseEvent event) { // We only want to process wheel events if (event.button < 0) { EntityPlayer entityPlayer = Minecraft.getMinecraft().thePlayer; if (entityPlayer != null && entityPlayer.isSneaking()) { ItemStack itemStack = entityPlayer.getHeldItem(); if (itemStack != null && itemStack.getItem() instanceof ItemBlock && BlockProperties.toBlock(itemStack).equals(BlockRegistry.blockCarpentersSlope)) { if (event.dwheel != 0) { PacketHandler.sendPacketToServer( new PacketSlopeSelect(entityPlayer.inventory.currentItem, event.dwheel > 0)); } event.setCanceled(true); } } } }