本文整理匯總了Java中org.spongepowered.api.text.TranslatableText類的典型用法代碼示例。如果您正苦於以下問題:Java TranslatableText類的具體用法?Java TranslatableText怎麽用?Java TranslatableText使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
TranslatableText類屬於org.spongepowered.api.text包,在下文中一共展示了TranslatableText類的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: deserialize
import org.spongepowered.api.text.TranslatableText; //導入依賴的package包/類
@Override
public TranslatableText deserialize(JsonElement element, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
final JsonObject obj = element.getAsJsonObject();
final String name = obj.get(TRANSLATABLE).getAsString();
final Translation translation = this.translationManager.get(name);
final Object[] arguments;
if ((element = obj.get(TRANSLATABLE_ARGS)) != null) {
final Text[] with = context.deserialize(element, Text[].class);
arguments = new Object[with.length];
System.arraycopy(with, 0, arguments, 0, with.length);
} else {
arguments = new Object[0];
}
final TranslatableText.Builder builder = Text.builder(translation, arguments);
deserialize(obj, builder, context);
return builder.build();
}
示例2: deserialize
import org.spongepowered.api.text.TranslatableText; //導入依賴的package包/類
@Override
public Text deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
if (json.isJsonPrimitive()) {
return context.deserialize(json, LiteralText.class);
}
if (json.isJsonArray()) {
final Text.Builder builder = Text.builder();
builder.append(context.<Text[]>deserialize(json, Text[].class));
return builder.build();
}
final JsonObject obj = json.getAsJsonObject();
if (obj.has(TEXT)) {
return context.deserialize(json, LiteralText.class);
} else if (obj.has(TRANSLATABLE)) {
return context.deserialize(json, TranslatableText.class);
} else if (obj.has(SCORE_VALUE)) {
return context.deserialize(json, ScoreText.class);
} else if (obj.has(SELECTOR)) {
return context.deserialize(json, SelectorText.class);
} else {
throw new JsonParseException("Unknown text format: " + json.toString());
}
}
示例3: transformTranslationText
import org.spongepowered.api.text.TranslatableText; //導入依賴的package包/類
private static Text transformTranslationText(TranslatableText text, Locale locale) {
// This is bad, don't look
Translation translation = text.getTranslation();
ImmutableList<Object> arguments = text.getArguments();
Object[] markers = new Object[arguments.size()];
for (int i = 0; i < markers.length; i++) {
markers[i] = "$MARKER" + i + "$";
}
LanguagePackManager langMgr = ChatUILib.getInstance().getLanguageManager();
if (!langMgr.isDefault(locale)) {
translation = langMgr.forTranslation(translation);
}
String patched = translation.get(locale, markers);
List<Object> sections = Lists.newArrayList();
Matcher m = MARKER_PATTERN.matcher(patched);
int prevPos = 0;
while (m.find()) {
if (m.start() != prevPos) {
sections.add(patched.substring(prevPos, m.start()));
}
int index = Integer.parseInt(m.group(1));
sections.add(arguments.get(index));
prevPos = m.end();
}
if (prevPos != patched.length() || prevPos == 0) {
sections.add(patched.substring(prevPos));
}
Text.Builder builder = new Format(text).applyToBuilder(Text.builder());
for (Object val : sections) {
builder.append(Text.of(val));
}
builder.append(text.getChildren());
return builder.build();
}
示例4: applyTo
import org.spongepowered.api.text.TranslatableText; //導入依賴的package包/類
/**
* Registers the json text serializers for the specified gson builder.
*
* @param gsonBuilder The gson builder
* @param translationManager The translation manager
* @param networkingFormat Whether the text serializers will be used for networking
* @return The gson builder for chaining
*/
public static GsonBuilder applyTo(GsonBuilder gsonBuilder, TranslationManager translationManager,
boolean networkingFormat) {
gsonBuilder.registerTypeAdapter(Text.class, new JsonTextSerializer());
gsonBuilder.registerTypeAdapter(LiteralText.class, new JsonTextLiteralSerializer(networkingFormat));
gsonBuilder.registerTypeAdapter(ScoreText.class, new JsonTextScoreSerializer(networkingFormat));
gsonBuilder.registerTypeAdapter(SelectorText.class, new JsonTextSelectorSerializer());
gsonBuilder.registerTypeAdapter(TranslatableText.class, new JsonTextTranslatableSerializer(
translationManager, networkingFormat, networkingFormat));
return gsonBuilder;
}
示例5: serialize
import org.spongepowered.api.text.TranslatableText; //導入依賴的package包/類
@Override
public JsonElement serialize(Text src, Type typeOfSrc, JsonSerializationContext context) {
if (src instanceof LiteralText) {
return context.serialize(src, LiteralText.class);
} else if (src instanceof TranslatableText) {
return context.serialize(src, TranslatableText.class);
} else if (src instanceof ScoreText) {
return context.serialize(src, ScoreText.class);
} else if (src instanceof SelectorText) {
return context.serialize(src, SelectorText.class);
} else {
throw new IllegalStateException("Attempted to serialize an unsupported text type: " + src.getClass().getName());
}
}
示例6: apply
import org.spongepowered.api.text.TranslatableText; //導入依賴的package包/類
private static Object[] apply(int currLineLength, Text.Builder currLineBuilder, Stack<Format> formatStack, Text text, List<Text> output,
int maxWidth, Locale locale, TextUtils utils) {
if (text instanceof TranslatableText) {
text = transformTranslationText((TranslatableText) text, locale);
}
Format format = pushFormat(formatStack, text);
String plainText = text.toPlainSingle();
List<String> lines = LINE_SPLITTER.splitToList(plainText);
boolean first = true;
String next = null;
for (int i = 0; next != null || i < lines.size(); i++) {
String line;
if (next != null) {
line = next;
next = null;
i--;
} else {
line = lines.get(i);
}
if (!first) {
output.add(currLineBuilder.build());
currLineBuilder = Text.builder();
currLineLength = 0;
}
first = false;
if (line.isEmpty()) {
continue;
}
boolean isBold = format.format.getStyle().isBold().get();
int lineW = utils.getStringWidth(line, isBold);
if (currLineLength + lineW > maxWidth) {
String oldLine = line;
int trimPos = trimToMaxWidth(oldLine, isBold, maxWidth - currLineLength, utils);
line = oldLine.substring(0, trimPos);
if (currLineLength == 0 && line.isEmpty()) {
// Cannot fit this within the maxWidth
break; // give up
}
lineW = utils.getStringWidth(line, isBold);
next = oldLine.substring(trimPos);
}
currLineLength += lineW;
currLineBuilder.append(format.createText(line));
}
for (Text child : text.getChildren()) {
Object[] ret = apply(currLineLength, currLineBuilder, formatStack, child, output, maxWidth, locale, utils);
currLineLength = (Integer) ret[0];
currLineBuilder = (Text.Builder) ret[1];
}
formatStack.pop();
return new Object[] {currLineLength, currLineBuilder};
}
示例7: serializeValues
import org.spongepowered.api.text.TranslatableText; //導入依賴的package包/類
@Override
public void serializeValues(LanternItemStack object, SimpleValueContainer valueContainer, DataView dataView) {
final ItemTypeObjectSerializer serializer = this.itemTypeSerializers.get(object.getType());
if (serializer != null) {
serializer.serializeValues(object, valueContainer, dataView);
}
DataView displayView = null;
final Optional<Text> optDisplayName = valueContainer.remove(Keys.DISPLAY_NAME);
if (optDisplayName.isPresent()) {
displayView = getOrCreateView(dataView, DISPLAY);
final Text displayName = optDisplayName.get();
if (displayName instanceof TranslatableText) {
final TranslatableText name1 = (TranslatableText) displayName;
// We can only do this for translatable text without any formatting
if (name1.getArguments().isEmpty() && name1.getChildren().isEmpty() &&
name1.getStyle().isEmpty() && name1.getColor() == TextColors.NONE) {
displayView.set(LOCALIZED_NAME, name1.getTranslation().getId());
} else {
displayView.set(NAME, LanternTexts.toLegacy(displayName));
}
} else {
displayView.set(NAME, LanternTexts.toLegacy(displayName));
}
}
final Optional<List<Text>> optLore = valueContainer.remove(Keys.ITEM_LORE);
if (optLore.isPresent() && !optLore.get().isEmpty()) {
if (displayView == null) {
displayView = getOrCreateView(dataView, DISPLAY);
}
displayView.set(LORE, optLore.get().stream().map(LanternTexts::toLegacy).collect(Collectors.toList()));
}
if (valueContainer.remove(Keys.UNBREAKABLE).orElse(false)) {
dataView.set(UNBREAKABLE, (byte) 1);
}
final Optional<Set<BlockType>> optBlockTypes = valueContainer.remove(Keys.BREAKABLE_BLOCK_TYPES);
if (optBlockTypes.isPresent() && !optBlockTypes.get().isEmpty()) {
dataView.set(CAN_DESTROY, optBlockTypes.get().stream().map(CatalogType::getId).collect(Collectors.toSet()));
}
valueContainer.remove(Keys.ITEM_ENCHANTMENTS).ifPresent(list -> serializeEnchantments(dataView, ENCHANTMENTS, list));
valueContainer.remove(Keys.STORED_ENCHANTMENTS).ifPresent(list -> serializeEnchantments(dataView, STORED_ENCHANTMENTS, list));
super.serializeValues(object, valueContainer, dataView);
}
示例8: onDeath
import org.spongepowered.api.text.TranslatableText; //導入依賴的package包/類
@Listener
public void onDeath(DestructEntityEvent event) {
if (!(event.getTargetEntity() instanceof Player)) {
return;
}
Player p = (Player) event.getTargetEntity();
//Get the translation, because for some reason sponge hides it in a lot of children
Text orgmessage = event.getOriginalMessage();
while (!(orgmessage instanceof TranslatableText) && !orgmessage.getChildren().isEmpty()) {
for (Text child : orgmessage.getChildren()) {
if (child instanceof TranslatableText) {
break;
}
}
orgmessage = orgmessage.getChildren().get(0); //Safe because of while loop
}
if (orgmessage instanceof TranslatableText) {
//Id
TranslatableText org = (TranslatableText) orgmessage;
String id_mc = org.getTranslation().getId();
if (!id_mc.startsWith("death.")) {
return;
}
String id_uc = id_mc.toLowerCase().replaceFirst("death\\.", "deathmessage.message.");
//Item
ItemStack stack = p.getItemInHand(HandTypes.MAIN_HAND).orElse(ItemStack.of(ItemTypes.NONE, 1));
Text item = stack.get(Keys.DISPLAY_NAME).orElse(Text.of(stack.getTranslation().get()));
//Final message //TODO hover
Text message = Messages.getFormatted(id_uc, "%player%", VariableUtil.getNameSource(p), "%item%", item);
//Custom args
int i = 1;
for (Object ob : org.getArguments()) {
message = TextUtil.replace(message, "%arg" + i + "%", ob instanceof Text ? ((Text) ob) : Text.of(ob.toString()));
i++;
}
event.setMessage(message);
}
}