下面列出了net.minecraft.util.text.event.HoverEvent#net.minecraft.util.text.event.ClickEvent 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void exec(String input) {
if (!this.clamp(input, 1, 1)) {
this.printUsage();
return;
}
final int size = Seppuku.INSTANCE.getModuleManager().getModuleList().size();
final TextComponentString msg = new TextComponentString("\2477Modules [" + size + "]\247f ");
final CommandsModule commandsModule = (CommandsModule) Seppuku.INSTANCE.getModuleManager().find(CommandsModule.class);
for (int i = 0; i < size; i++) {
final Module mod = Seppuku.INSTANCE.getModuleManager().getModuleList().get(i);
if (mod != null) {
msg.appendSibling(new TextComponentString((mod.isEnabled() ? "\247a" : "\247c") + mod.getDisplayName() + "\2477" + ((i == size - 1) ? "" : ", "))
.setStyle(new Style()
.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new TextComponentString("\2476" + (mod.getDesc() == null ? "There is no description for this module" : mod.getDesc()) + "\247f").appendSibling(new TextComponentString((mod.toUsageTextComponent() == null ? "" : "\n" + mod.toUsageTextComponent().getText()) + "\247f"))))
.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, commandsModule.getPrefix().getValue() + "toggle" + " " + mod.getDisplayName()))));
}
}
Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(msg);
}
@SideOnly(Side.CLIENT)
private boolean handleCustomComponentClick(ITextComponent textComponent) {
Style style = textComponent.getStyle();
if (style.getClickEvent() != null) {
ClickEvent clickEvent = style.getClickEvent();
String componentText = clickEvent.getValue();
if (clickEvent.getAction() == Action.OPEN_URL && componentText.startsWith("@!")) {
String rawText = componentText.substring(2);
ClickData clickData = new ClickData(Mouse.getEventButton(), isShiftDown(), isCtrlDown());
writeClientAction(1, buf -> {
clickData.writeToBuf(buf);
buf.writeString(rawText);
});
return true;
}
}
return false;
}
@Listener
public void onUpdate(EventPlayerUpdate event) {
if (event.getStage() == EventStageable.EventStage.PRE) {
if (this.firstStart != null && this.firstEnd != null && this.secondStart != null && this.secondEnd != null) {
final double[] start = new double[]{this.secondStart.x, this.secondStart.z, this.secondEnd.x, this.secondEnd.z};
final double[] end = new double[]{this.firstStart.x, this.firstStart.z, this.firstEnd.x, this.firstEnd.z};
final double[] intersection = MathUtil.calcIntersection(start, end);
if (Double.isNaN(intersection[0]) || Double.isNaN(intersection[1]) || Double.isInfinite(intersection[0]) || Double.isInfinite(intersection[1])) {
Seppuku.INSTANCE.errorChat("Error lines are parallel");
Seppuku.INSTANCE.getEventManager().removeEventListener(this);
return;
}
final double dist = Minecraft.getMinecraft().player.getDistance(intersection[0], Minecraft.getMinecraft().player.posY, intersection[1]);
final TextComponentString component = new TextComponentString("Stronghold found " + ChatFormatting.GRAY + (int) dist + "m away");
final String coords = String.format("X: %s, Y: ?, Z: %s\nClick to add a Waypoint", (int) intersection[0], (int) intersection[1]);
final CommandsModule cmds = (CommandsModule) Seppuku.INSTANCE.getModuleManager().find(CommandsModule.class);
if (cmds != null) {
component.setStyle(new Style().setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new TextComponentString(coords))).setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, cmds.prefix.getValue() + "Waypoints add Stronghold " + intersection[0] + " " + Minecraft.getMinecraft().player.posY + " " + intersection[1])));
}
Seppuku.INSTANCE.logcChat(component);
Seppuku.INSTANCE.getNotificationManager().addNotification("", "Stronghold found " + ChatFormatting.GRAY + (int) dist + "m away");
this.firstStart = null;
this.firstEnd = null;
this.secondStart = null;
this.secondEnd = null;
Seppuku.INSTANCE.getEventManager().removeEventListener(this);
}
}
}
@Override
public boolean processInteract(EntityPlayer player, EnumHand hand) {
boolean ret = super.processInteract(player, hand);
if (player.world.isRemote && hand == EnumHand.MAIN_HAND) {
player.sendMessage(new TextComponentString(
"If you like these penguins, you'll like the full version more!"));
player.sendMessage(new TextComponentString("Click [here] to download it on CurseForge.")
.setStyle(new Style().setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL,
"https://minecraft.curseforge.com/projects/penguins"))));
}
return ret;
}
/**
* Executes the click event specified by the given chat component
*/
protected boolean handleComponentClick(ITextComponent component) {
ClickEvent clickevent = component.getStyle().getClickEvent();
if (clickevent == null) {
return false;
} else if (clickevent.getAction() == ClickEvent.Action.CHANGE_PAGE) {
String s = clickevent.getValue();
try {
int i = Integer.parseInt(s) - 1;
if (i >= 0 && i < this.bookTotalPages && i != this.currPage) {
this.currPage = i;
this.updateButtons();
return true;
}
} catch (Throwable var5) {
;
}
return false;
} else {
boolean flag = super.handleComponentClick(component);
if (flag && clickevent.getAction() == ClickEvent.Action.RUN_COMMAND) {
this.mc.displayGuiScreen((GuiScreen)null);
}
return flag;
}
}
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;
}
public static ITextComponent withButton(ITextComponent textComponent, String componentData) {
Style style = textComponent.getStyle();
style.setClickEvent(new ClickEvent(Action.OPEN_URL, "@!" + componentData));
style.setColor(TextFormatting.YELLOW);
return textComponent;
}
@Override
public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException {
if (sender instanceof EntityPlayerMP) {
EntityPlayerMP player = (EntityPlayerMP) sender;
ItemStack stackInHand = player.inventory.getCurrentItem();
if (stackInHand.isEmpty()) {
throw new CommandException("gregtech.command.util.hand.no_item");
}
String registryName = stackInHand.getItem().getRegistryName().toString();
ClickEvent itemNameEvent = new ClickEvent(Action.OPEN_URL, registryName);
player.sendMessage(new TextComponentTranslation("gregtech.command.util.hand.item_id", registryName, stackInHand.getItemDamage())
.setStyle(new Style().setClickEvent(itemNameEvent)));
IElectricItem electricItem = stackInHand.getCapability(GregtechCapabilities.CAPABILITY_ELECTRIC_ITEM, null);
IFluidHandlerItem fluidHandlerItem = stackInHand.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null);
if (electricItem != null) {
player.sendMessage(new TextComponentTranslation("gregtech.command.util.hand.electric",
electricItem.getCharge(),
electricItem.getMaxCharge(),
electricItem.getTier(),
Boolean.toString(electricItem.canProvideChargeExternally())));
}
if (fluidHandlerItem != null) {
for (IFluidTankProperties properties : fluidHandlerItem.getTankProperties()) {
FluidStack contents = properties.getContents();
String fluidName = contents == null ? "empty" : contents.getFluid().getName();
ClickEvent fluidClickEvent = new ClickEvent(Action.OPEN_URL, fluidName);
player.sendMessage(new TextComponentTranslation("gregtech.command.util.hand.fluid",
fluidName,
contents == null ? 0 : contents.amount,
properties.getCapacity(),
Boolean.toString(properties.canFill()), Boolean.toString(properties.canDrain()))
.setStyle(new Style().setClickEvent(fluidClickEvent)));
}
}
if (stackInHand.getItem() instanceof MetaItem) {
MetaItem<?> metaItem = (MetaItem<?>) stackInHand.getItem();
MetaValueItem metaValueItem = metaItem.getItem(stackInHand);
if (metaValueItem == null) {
if (metaItem instanceof MaterialMetaItem) {
Material material = ((MaterialMetaItem) metaItem).getMaterial(stackInHand);
OrePrefix orePrefix = ((MaterialMetaItem) metaItem).getOrePrefix(stackInHand);
String oreDictName = new UnificationEntry(orePrefix, material).toString();
player.sendMessage(new TextComponentTranslation("gregtech.command.util.hand.material_meta_item", orePrefix, material)
.setStyle(new Style().setClickEvent(new ClickEvent(Action.OPEN_URL, oreDictName))));
}
} else {
if (metaValueItem instanceof ToolMetaItem.MetaToolValueItem) {
IToolStats toolStats = ((MetaToolValueItem) metaValueItem).getToolStats();
player.sendMessage(new TextComponentTranslation("gregtech.command.util.hand.tool_stats", toolStats.getClass().getName()));
}
ClickEvent metaItemEvent = new ClickEvent(Action.OPEN_URL, metaValueItem.unlocalizedName);
player.sendMessage(new TextComponentTranslation("gregtech.command.util.hand.meta_item", metaValueItem.unlocalizedName, metaValueItem)
.setStyle(new Style().setClickEvent(metaItemEvent)));
}
}
} else {
throw new CommandException("gregtech.command.util.hand.not_a_player");
}
}
private StringMessage(String message, boolean keepNewlines) {
this.message = message;
if (message == null) {
output = new ITextComponent[]{currentChatComponent};
return;
}
list.add(currentChatComponent);
Matcher matcher = INCREMENTAL_PATTERN.matcher(message);
String match = null;
while (matcher.find()) {
int groupId = 0;
while ((match = matcher.group(++groupId)) == null) {
// NOOP
}
appendNewComponent(matcher.start(groupId));
switch (groupId) {
case 1:
TextFormatting format = formatMap.get(match.toLowerCase(java.util.Locale.ENGLISH).charAt(1));
if (format == TextFormatting.RESET) {
modifier = new Style();
} else if (format.isFancyStyling()) {
switch (format) {
case BOLD:
modifier.setBold(Boolean.TRUE);
break;
case ITALIC:
modifier.setItalic(Boolean.TRUE);
break;
case STRIKETHROUGH:
modifier.setStrikethrough(Boolean.TRUE);
break;
case UNDERLINE:
modifier.setUnderlined(Boolean.TRUE);
break;
case OBFUSCATED:
modifier.setObfuscated(Boolean.TRUE);
break;
default:
throw new AssertionError("Unexpected message format");
}
} else { // Color resets formatting
modifier = new Style().setColor(format);
}
break;
case 2:
if (keepNewlines) {
currentChatComponent.appendSibling(new TextComponentString("\n"));
} else {
currentChatComponent = null;
}
break;
case 3:
if (!(match.startsWith("http://") || match.startsWith("https://"))) {
match = "http://" + match;
}
modifier.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, match));
appendNewComponent(matcher.end(groupId));
modifier.setClickEvent(null);
}
currentIndex = matcher.end(groupId);
}
if (currentIndex < message.length()) {
appendNewComponent(message.length());
}
output = list.toArray(new ITextComponent[list.size()]);
}
@SubscribeEvent
@SideOnly(Side.CLIENT)
public static void onTick(TickEvent.ClientTickEvent event) {
if (!ConfigValues.versionCheckerEnabled) return;
EntityPlayer player = Minecraft.getMinecraft().player;
if (doneChecking && event.phase == TickEvent.Phase.END && player != null && !triedToWarnPlayer) {
ITextComponent component = new TextComponentString("[").setStyle(new Style().setColor(TextFormatting.GREEN))
.appendSibling(new TextComponentTranslation("wizardry.misc.update_link").setStyle(new Style().setColor(TextFormatting.GRAY)))
.appendSibling(new TextComponentString("]").setStyle(new Style().setColor(TextFormatting.GREEN)));
component.getStyle()
.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new TextComponentString(updateMessage)))
.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, "https://minecraft.curseforge.com/projects/wizardry-mod/files"));
if (onlineVersion != null && !onlineVersion.isEmpty()) {
String clientBuild = Wizardry.VERSION;
if (Utils.compareVersions(onlineVersion, clientBuild) > 0) {
ArrayList<String> messages = new ArrayList<>();
String base = "wizardry.update";
int n = 0;
while (LibrarianLib.PROXY.canTranslate(base + n))
messages.add(base + n++);
if (!messages.isEmpty())
player.sendMessage(new TextComponentTranslation(messages.get(RandUtil.nextInt(messages.size() - 1))).setStyle(new Style().setColor(TextFormatting.YELLOW)));
player.sendMessage(new TextComponentTranslation("wizardry.misc.update_checker0")
.setStyle(new Style().setColor(TextFormatting.GREEN)));
player.sendMessage(new TextComponentTranslation("wizardry.misc.update_checker1")
.setStyle(new Style().setColor(TextFormatting.GREEN))
.appendText(" ")
.appendSibling(new TextComponentString(clientBuild).setStyle(new Style().setColor(TextFormatting.RED))));
player.sendMessage(new TextComponentTranslation("wizardry.misc.update_checker2")
.setStyle(new Style().setColor(TextFormatting.GREEN))
.appendText(" ")
.appendSibling(new TextComponentString(onlineVersion).setStyle(new Style().setColor(TextFormatting.YELLOW))));
if (updateMessage != null && !updateMessage.isEmpty())
player.sendMessage(component);
} else if (updateMessage != null && !updateMessage.isEmpty())
player.sendMessage(component);
}
triedToWarnPlayer = true;
}
}