下面列出了怎么用net.minecraft.util.text.event.HoverEvent的API类实例代码及写法,或者点击链接到github查看源代码。
private void printPortalToChat(Vec3d portal) {
final TextComponentString portalTextComponent = new TextComponentString("Portal found!");
String overworld = "";
String nether = "";
if (Minecraft.getMinecraft().player.dimension == 0) { // overworld
overworld = String.format("Overworld: X: %s, Y: %s, Z: %s", (int) portal.x, (int) portal.y, (int) portal.z);
nether = String.format("Nether: X: %s, Y: %s, Z: %s", (int) portal.x / 8, (int) portal.y, (int) portal.z / 8);
} else if (Minecraft.getMinecraft().player.dimension == -1) { // nether
overworld = String.format("Overworld: X: %s, Y: %s, Z: %s", (int) portal.x * 8, (int) portal.y, (int) portal.z * 8);
nether = String.format("Nether: X: %s, Y: %s, Z: %s", (int) portal.x, (int) portal.y, (int) portal.z);
}
int playerDistance = (int) Minecraft.getMinecraft().player.getDistance(portal.x, portal.y, portal.z);
String distance = ChatFormatting.GRAY + "" + playerDistance + "m away";
String hoverText = overworld + "\n" + nether + "\n" + distance;
portalTextComponent.setStyle(new Style().setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new TextComponentString(hoverText))));
Seppuku.INSTANCE.logcChat(portalTextComponent);
}
@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);
}
@Override
public void exec(String input) {
if (!this.clamp(input, 1, 1)) {
this.printUsage();
return;
}
final int size = Seppuku.INSTANCE.getCommandManager().getCommandList().size();
final TextComponentString msg = new TextComponentString("\2477Commands [" + size + "]\247f ");
for (int i = 0; i < size; i++) {
final Command cmd = Seppuku.INSTANCE.getCommandManager().getCommandList().get(i);
msg.appendSibling(new TextComponentString("\247a" + cmd.getDisplayName() + "\2477" + ((i == size - 1) ? "" : ", "))
.setStyle(new Style()
.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new TextComponentString("\2476" + cmd.getDesc() + "\247f")
.appendSibling(new TextComponentString("\n" + cmd.getUsage()))))));
}
Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(msg);
}
private void printEndPortalToChat(Vec3d portal) {
final TextComponentString portalTextComponent = new TextComponentString("End Portal found!");
String coords = String.format("X: %s, Y: %s, Z: %s", (int) portal.x, (int) portal.y, (int) portal.z);
int playerDistance = (int) Minecraft.getMinecraft().player.getDistance(portal.x, portal.y, portal.z);
String distance = ChatFormatting.GRAY + "" + playerDistance + "m away";
String hoverText = coords + "\n" + distance;
portalTextComponent.setStyle(new Style().setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new TextComponentString(hoverText))));
Seppuku.INSTANCE.logcChat(portalTextComponent);
}
@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);
}
}
}
/**
* Called serverside to obtain text displayed in GUI
* each element of list is displayed on new line
* to use translation, use TextComponentTranslation
*/
protected void addDisplayText(List<ITextComponent> textList) {
if (!isStructureFormed()) {
ITextComponent tooltip = new TextComponentTranslation("gregtech.multiblock.invalid_structure.tooltip");
tooltip.setStyle(new Style().setColor(TextFormatting.GRAY));
textList.add(new TextComponentTranslation("gregtech.multiblock.invalid_structure")
.setStyle(new Style().setColor(TextFormatting.RED)
.setHoverEvent(new HoverEvent(Action.SHOW_TEXT, tooltip))));
}
}
public static ITextComponent withHoverTextTranslate(ITextComponent textComponent, String hoverTranslation) {
Style style = textComponent.getStyle();
ITextComponent translation = new TextComponentTranslation(hoverTranslation);
translation.getStyle().setColor(TextFormatting.GRAY);
style.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, translation));
return textComponent;
}
@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;
}
}
@Override
public void debugActionBlockClicked(ItemStack stack, EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) {
if (world.isRemote) {
return;
}
// Start with the position of the block that was clicked on. '7' is gray. Btw, the chat font is not fixed width. :(
StringBuilder outputRaw = new StringBuilder(String.format("`7%1$4d,%2$4d,%3$4d`r ", pos.getX(), pos.getY(), pos.getZ()));
// Check if the clicked on block has the IGrowable interface.
final IGrowable crop = WorldHelper.getBlock(world, pos, IGrowable.class).orElse(null);
if (crop == null) {
// If it does not, add a nicely formatted report, then skip onward.
outputRaw.append(chatNotIG);
} else {
// Otherwise run the tests and record the results.
IBlockState state = world.getBlockState(pos);
outputRaw.append(crop.canGrow(world, pos, state, false) ? chatTrue : chatFalse); // canGrow
outputRaw.append(crop.canUseBonemeal(world, world.rand, pos, state) ? chatTrue : chatFalse); // canUseBonemeal
crop.grow(world, world.rand, pos, state); // grow
// It's also helpful to also make clear what block was being tested.
outputRaw.append("`3"); // '3' is dark aqua.
outputRaw.append(crop.toString().replaceFirst("Block", ""));
outputRaw.append("`r");
}
// Ellipsis are added as a clue that there's more text.
outputRaw.append(" `8[...]`r"); // '8' is dark gray.
// Create a hover box with explanatory information.
TextComponentString hoverComponent = new TextComponentString(MessageUtil.colorize(chatInfo));
HoverEvent hoverEvent = new HoverEvent(HoverEvent.Action.SHOW_TEXT, hoverComponent);
// Turn the output String into a chat message.
TextComponentString outputComponent = new TextComponentString(MessageUtil.colorize(outputRaw.toString()));
// Add the hover box to the chat message.
outputComponent.getStyle().setHoverEvent(hoverEvent);
// Now send the completed chat message.
player.sendMessage(outputComponent);
}