Java源码示例:net.minecraft.client.network.play.ClientPlayNetHandler
示例1
@SubscribeEvent
public void onClientPayload(NetworkEvent.ServerCustomPayloadEvent event) {
if (event instanceof NetworkEvent.ServerCustomPayloadLoginEvent) {
return;
}
PacketCustom packet = new PacketCustom(event.getPayload());
NetworkEvent.Context ctx = event.getSource().get();
INetHandler netHandler = ctx.getNetworkManager().getNetHandler();
ctx.setPacketHandled(true);
if (netHandler instanceof ClientPlayNetHandler) {
ClientPlayNetHandler nh = (ClientPlayNetHandler) netHandler;
ctx.enqueueWork(() -> packetHandler.handlePacket(packet, Minecraft.getInstance(), nh));
}
}
示例2
public static boolean interceptChatMessage(String message) {
if (message.startsWith("/bbor:")) {
ClientPlayNetHandler connection = Minecraft.getInstance().getConnection();
if (connection != null) {
CommandDispatcher<ISuggestionProvider> commandDispatcher = connection.func_195515_i();
CommandSource commandSource = Minecraft.getInstance().player.getCommandSource();
try {
commandDispatcher.execute(message.substring(1), commandSource);
} catch (CommandSyntaxException exception) {
commandSource.sendErrorMessage(TextComponentUtils.toTextComponent(exception.getRawMessage()));
if (exception.getInput() != null && exception.getCursor() >= 0) {
ITextComponent suggestion = new StringTextComponent("")
.applyTextStyle(TextFormatting.GRAY)
.applyTextStyle(style -> style.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, message)));
int textLength = Math.min(exception.getInput().length(), exception.getCursor());
if (textLength > 10) {
suggestion.appendText("...");
}
suggestion.appendText(exception.getInput().substring(Math.max(0, textLength - 10), textLength));
if (textLength < exception.getInput().length()) {
suggestion.appendSibling(new StringTextComponent(exception.getInput().substring(textLength))
.applyTextStyles(TextFormatting.RED, TextFormatting.UNDERLINE));
}
suggestion.appendSibling(new TranslationTextComponent("command.context.here")
.applyTextStyles(TextFormatting.RED, TextFormatting.ITALIC));
commandSource.sendErrorMessage(suggestion);
}
}
}
return true;
}
return false;
}
示例3
@Redirect(method = "processPacket", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/network/play/IClientPlayNetHandler;handleCustomPayload(Lnet/minecraft/network/play/server/SCustomPayloadPlayPacket;)V"))
private void processPacket(IClientPlayNetHandler netHandlerPlayClient, SCustomPayloadPlayPacket packet) {
String channelName = channel.toString();
if (channelName.startsWith("bbor:")) {
PacketBuffer data = null;
try {
data = packet.getBufferData();
PayloadReader reader = new PayloadReader(data);
switch (channelName) {
case InitializeClient.NAME: {
EventBus.publish(InitializeClient.getEvent(reader));
((ClientPlayNetHandler) netHandlerPlayClient).sendPacket(SubscribeToServer.getPayload().build());
break;
}
case AddBoundingBox.NAME:
case AddBoundingBox.LEGACY: {
EventBus.publish(AddBoundingBox.getEvent(reader, channelName));
break;
}
}
} finally {
if (data != null)
data.release();
}
} else {
netHandlerPlayClient.handleCustomPayload(packet);
}
}
示例4
private CommandDispatcher<ISuggestionProvider> getPlayerCommandDispatcher() {
return Optional.ofNullable(this.minecraft.player)
.map(player -> player.connection)
.map(ClientPlayNetHandler::getCommandDispatcher)
.orElse(null);
}
示例5
@Inject(method = "processPacket", at = @At("RETURN"))
private void processPacket(IClientPlayNetHandler netHandlerPlayClient, CallbackInfo ci) {
TypeHelper.doIfType(netHandlerPlayClient, ClientPlayNetHandler.class, handler ->
ClientInterop.registerClientCommands(handler.func_195515_i()));
}