下面列出了怎么用net.minecraft.util.ChatStyle的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void handleChat(String s) {
if (s.toLowerCase().contains("reconnecting hyperium connection")) return;
Hyperium.LOGGER.debug("Chat: {}", s);
s = s.replace("&", C.COLOR_CODE_SYMBOL);
IChatComponent chatComponent = new ChatComponentText("");
Arrays.stream(s.split(" ")).forEach(s1 -> {
ChatComponentText iChatComponents = new ChatComponentText(s1 + " ");
if (s1.contains(".") && !s1.startsWith(".") && !s1.endsWith(".")) {
ChatStyle chatStyle = new ChatStyle();
chatStyle.setChatClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, s1.startsWith("http") ? s1 : "http://" + s1));
iChatComponents.setChatStyle(chatStyle);
}
chatComponent.appendSibling(iChatComponents);
});
GeneralChatHandler.instance().sendMessage(chatComponent);
}
@Inject(method = "actionPerformed", at = @At("HEAD"))
private void actionPerformed(GuiButton button, CallbackInfo callbackInfo) {
switch(button.id) {
case 0:
if(!signCommand1.getText().isEmpty())
tileSign.signText[0].setChatStyle(new ChatStyle().setChatClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, signCommand1.getText())));
if(!signCommand2.getText().isEmpty())
tileSign.signText[1].setChatStyle(new ChatStyle().setChatClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, signCommand2.getText())));
if(!signCommand3.getText().isEmpty())
tileSign.signText[2].setChatStyle(new ChatStyle().setChatClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, signCommand3.getText())));
if(!signCommand4.getText().isEmpty())
tileSign.signText[3].setChatStyle(new ChatStyle().setChatClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, signCommand4.getText())));
break;
case 1:
enabled = !enabled;
toggleButton.displayString = enabled ? "Disable Formatting codes" : "Enable Formatting codes";
break;
}
}
@Inject(method = "actionPerformed", at = @At("HEAD"))
private void actionPerformed(GuiButton button, CallbackInfo callbackInfo) {
switch(button.id) {
case 0:
if(!signCommand1.getText().isEmpty())
tileSign.signText[0].setChatStyle(new ChatStyle().setChatClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, signCommand1.getText())));
if(!signCommand2.getText().isEmpty())
tileSign.signText[1].setChatStyle(new ChatStyle().setChatClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, signCommand2.getText())));
if(!signCommand3.getText().isEmpty())
tileSign.signText[2].setChatStyle(new ChatStyle().setChatClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, signCommand3.getText())));
if(!signCommand4.getText().isEmpty())
tileSign.signText[3].setChatStyle(new ChatStyle().setChatClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, signCommand4.getText())));
break;
case 1:
enabled = !enabled;
toggleButton.displayString = enabled ? "Disable Formatting codes" : "Enable Formatting codes";
break;
}
}
public String getFormattingCode(ChatStyle parentStyle) {
if (cachedState != null) return cachedState;
if (parent.isEmpty()) {
return parentStyle != null ? parentStyle.getFormattingCode() : "";
} else {
StringBuilder stringbuilder = new StringBuilder();
if (parent.getColor() != null) stringbuilder.append(parent.getColor());
if (parent.getBold()) stringbuilder.append(EnumChatFormatting.BOLD);
if (parent.getItalic()) stringbuilder.append(EnumChatFormatting.ITALIC);
if (parent.getUnderlined()) stringbuilder.append(EnumChatFormatting.UNDERLINE);
if (parent.getObfuscated()) stringbuilder.append(EnumChatFormatting.OBFUSCATED);
if (parent.getStrikethrough()) stringbuilder.append(EnumChatFormatting.STRIKETHROUGH);
String s = stringbuilder.toString();
cachedState = s;
return s;
}
}
@Inject(method = "handleComponentHover", at = @At("HEAD"))
private void handleHoverOverComponent(IChatComponent component, int x, int y, final CallbackInfo callbackInfo) {
if (component == null || component.getChatStyle().getChatClickEvent() == null || !LiquidBounce.moduleManager.getModule(ComponentOnHover.class).getState())
return;
final ChatStyle chatStyle = component.getChatStyle();
final ClickEvent clickEvent = chatStyle.getChatClickEvent();
final HoverEvent hoverEvent = chatStyle.getChatHoverEvent();
drawHoveringText(Collections.singletonList("§c§l" + clickEvent.getAction().getCanonicalName().toUpperCase() + ": §a" + clickEvent.getValue()), x, y - (hoverEvent != null ? 17 : 0));
}
@Inject(method = "handleComponentHover", at = @At("HEAD"))
private void handleHoverOverComponent(IChatComponent component, int x, int y, final CallbackInfo callbackInfo) {
if (component == null || component.getChatStyle().getChatClickEvent() == null || !LiquidBounce.moduleManager.getModule(ComponentOnHover.class).getState())
return;
final ChatStyle chatStyle = component.getChatStyle();
final ClickEvent clickEvent = chatStyle.getChatClickEvent();
final HoverEvent hoverEvent = chatStyle.getChatHoverEvent();
drawHoveringText(Collections.singletonList("§c§l" + clickEvent.getAction().getCanonicalName().toUpperCase() + ": §a" + clickEvent.getValue()), x, y - (hoverEvent != null ? 17 : 0));
}
public WrapperChatStyle(ChatStyle var1) {
this.real = var1;
}
public ChatStyle unwrap() {
return this.real;
}
public HyperiumChatStyle(ChatStyle parent) {
this.parent = parent;
}
@InvokeEvent(priority = Priority.HIGH) // We use the high priority to grab things first
public void onChatReceive(ServerChatEvent event) {
// Strip the message of any colors for improved detectability
String unformattedText = ChatColor.stripColor(event.getChat().getUnformattedText());
// The formatted message for a few of the custom toggles
String formattedText = event.getChat().getFormattedText();
try {
// Loop through all the toggles
for (ToggleBase type : mod.getToggleHandler().getToggles().values()) {
// The chat its looking for shouldn't be toggled, move to next one!
if (type.isEnabled()) continue;
// We don't want an issue with one toggle bringing
// the whole toggle system crashing down in flames.
try {
// The text we want to input into the shouldToggle method.
String input = type.useFormattedMessage() ? formattedText : unformattedText;
// If the toggle should toggle the specified message and
// the toggle is not enabled (this message is turned off)
// don't send the message to the player & stop looping
if (type.shouldToggle(input)) {
if (type instanceof TypeMessageSeparator) {
// Attempt to keep the formatting
ChatStyle style = event.getChat().getChatStyle();
String edited = ((TypeMessageSeparator) type).editMessage(formattedText);
// Don't bother sending the message if its empty
if (!input.equals(edited) && edited.isEmpty()) {
event.setCancelled(true);
} else {
event.setChat(new ChatComponentText(edited).setChatStyle(style));
}
} else {
event.setCancelled(true);
}
break;
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
@Shadow
public abstract ChatStyle getChatStyle();
@Inject(method = "setChatStyle", at = @At("HEAD"), cancellable = true)
private void setChatStyle(ChatStyle style, CallbackInfoReturnable<IChatComponent> ci) {
hyperiumChatComponentStyle.invalidateCache();
}
/**
* @author Sk1er
*/
@Inject(method = "setColor", at = @At("HEAD"))
private void setColor(EnumChatFormatting color, CallbackInfoReturnable<ChatStyle> info) {
hyperiumChatStyle.resetCache();
}
/**
* @author Sk1er
*/
@Inject(method = "setBold", at = @At("HEAD"))
private void setBold(Boolean boldIn, CallbackInfoReturnable<ChatStyle> callbackInfoReturnable) {
hyperiumChatStyle.resetCache();
}
/**
* @author Sk1er
*/
@Inject(method = "setItalic", at = @At("HEAD"))
private void setItalic(Boolean boldIn, CallbackInfoReturnable<ChatStyle> callbackInfoReturnable) {
hyperiumChatStyle.resetCache();
}
/**
* @author Sk1er
*/
@Inject(method = "setStrikethrough", at = @At("HEAD"))
private void setStrikethrough(Boolean boldIn, CallbackInfoReturnable<ChatStyle> callbackInfoReturnable) {
hyperiumChatStyle.resetCache();
}
/**
* @author Sk1er
*/
@Inject(method = "setUnderlined", at = @At("HEAD"))
private void setUnderlined(Boolean boldIn, CallbackInfoReturnable<ChatStyle> callbackInfoReturnable) {
hyperiumChatStyle.resetCache();
}
/**
* @author Sk1er
*/
@Inject(method = "setObfuscated", at = @At("HEAD"))
private void setObfuscated(Boolean boldIn, CallbackInfoReturnable<ChatStyle> callbackInfoReturnable) {
hyperiumChatStyle.resetCache();
}
/**
* @author Sk1er
*/
@Inject(method = "setChatClickEvent", at = @At("HEAD"))
private void setChatClickEvent(ClickEvent boldIn, CallbackInfoReturnable<ChatStyle> callbackInfoReturnable) {
hyperiumChatStyle.resetCache();
}
/**
* @author Sk1er
*/
@Inject(method = "setChatHoverEvent", at = @At("HEAD"))
private void setChatHoverEvent(HoverEvent boldIn, CallbackInfoReturnable<ChatStyle> callbackInfoReturnable) {
hyperiumChatStyle.resetCache();
}
/**
* @author Sk1er
*/
@Inject(method = "setInsertion", at = @At("HEAD"))
private void setChatHoverEvent(String boldIn, CallbackInfoReturnable<ChatStyle> callbackInfoReturnable) {
hyperiumChatStyle.resetCache();
}
/**
* @author Sk1er
*/
@Inject(method = "setParentStyle", at = @At("HEAD"))
private void setChatHoverEvent(ChatStyle boldIn, CallbackInfoReturnable<ChatStyle> callbackInfoReturnable) {
hyperiumChatStyle.resetCache();
}
@Override
public IChatComponent toChatMessage() {
return LocalManager.get("mytown.format.rank", name).setChatStyle(new ChatStyle().setColor(type.color));
}
public static void jamOpenCommand(String playername, String[] args, WCommandSender listener, boolean jam) {
RedstoneEtherServer ether = RedstoneEther.server();
if (args.length == 0) {
listener.chatT("wrcbe_core.param.invalidno");
return;
}
if ((args.length == 1 && ServerUtils.getPlayer(playername) == null)) {
listener.chatT("wrcbe_core.param.jam.noplayer");
return;
}
String range = args[args.length - 1];
String jamPlayer = args.length == 1 ? playername : args[0];
int startfreq;
int endfreq;
if (range.equals("all")) {
startfreq = 1;
endfreq = RedstoneEther.numfreqs;
} else if (range.equals("default")) {
startfreq = ether.getLastSharedFrequency() + 1;
endfreq = RedstoneEther.numfreqs;
} else {
int[] freqrange = RedstoneEther.parseFrequencyRange(range);
startfreq = freqrange[0];
endfreq = freqrange[1];
}
if (startfreq < 1 || endfreq > RedstoneEther.numfreqs || endfreq < startfreq) {
listener.chatT("wrcbe_core.param.invalidfreqrange");
return;
}
ether.setFrequencyRangeCommand(jamPlayer, startfreq, endfreq, jam);
int publicend = ether.getLastPublicFrequency();
EntityPlayer player = ServerUtils.getPlayer(jamPlayer);
String paramName = jam ? "jam" : "open";
ChatStyle playerStyle = new ChatStyle().setColor(EnumChatFormatting.YELLOW);
if (startfreq == endfreq) {
if (startfreq <= publicend) {
listener.chatT("wrcbe_core.param.jam.errpublic");
return;
}
listener.chatOpsT("wrcbe_core.param."+paramName+".opjammed", playername, jamPlayer, startfreq);
if (player != null)
player.addChatComponentMessage(new ChatComponentTranslation("wrcbe_core.param."+paramName+".jammed", startfreq).setChatStyle(playerStyle));
} else {
if (startfreq <= publicend && endfreq <= publicend) {
listener.chatT("wrcbe_core.param.jam.errpublic");
return;
}
if (startfreq <= publicend)
startfreq = publicend + 1;
listener.chatOpsT("wrcbe_core.param."+paramName+".opjammed2", playername, jamPlayer, startfreq + "-" + endfreq);
if (player != null)
player.addChatComponentMessage(new ChatComponentTranslation("wrcbe_core.param."+paramName+".jammed2", startfreq + "-" + endfreq).setChatStyle(playerStyle));
}
}