Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.20.1] backport translations feature from 1.21 #746

Draft
wants to merge 3 commits into
base: 1.20.1/dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.mojang.blaze3d.platform.InputConstants;
import dev.architectury.utils.Env;
import dev.ftb.mods.ftblibrary.icon.Icons;
import dev.ftb.mods.ftblibrary.util.TooltipList;
import dev.ftb.mods.ftblibrary.util.client.ClientUtils;
import dev.ftb.mods.ftbquests.FTBQuests;
import dev.ftb.mods.ftbquests.client.gui.CustomToast;
Expand All @@ -14,6 +15,7 @@
import dev.ftb.mods.ftbquests.quest.TeamData;
import dev.ftb.mods.ftbquests.quest.task.StructureTask;
import dev.ftb.mods.ftbquests.quest.theme.QuestTheme;
import dev.ftb.mods.ftbquests.quest.translation.TranslationKey;
import dev.ftb.mods.ftbquests.util.TextUtils;
import dev.ftb.mods.ftbteams.api.FTBTeamsAPI;
import dev.ftb.mods.ftbteams.api.client.KnownClientPlayer;
Expand Down Expand Up @@ -175,6 +177,12 @@ public boolean isPlayerOnTeam(Player player, TeamData teamData) {
.orElse(false);
}

@Override
public String getLocale() {
String locale = FTBQuestsClientConfig.EDITING_LOCALE.get();
return locale.isEmpty() ? Minecraft.getInstance().options.languageCode : locale;
}

@Override
public boolean moveChapterGroup(long id, boolean movingUp) {
if (super.moveChapterGroup(id, movingUp)) {
Expand Down Expand Up @@ -214,4 +222,14 @@ public static void openBookToQuestObject(long id) {
}
}
}

public static void addTranslationWarning(TooltipList list, TranslationKey key) {
list.add(Component.translatable("ftbquests.message.missing_xlate_1",
Component.translatable(key.getTranslationKey()),
ClientQuestFile.INSTANCE.getLocale())
.withStyle(ChatFormatting.GRAY, ChatFormatting.ITALIC)
);
list.add(Component.translatable("ftbquests.message.missing_xlate_2")
.withStyle(ChatFormatting.GRAY, ChatFormatting.ITALIC));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

import dev.ftb.mods.ftblibrary.config.ConfigGroup;
import dev.ftb.mods.ftblibrary.config.ui.EditConfigScreen;
import dev.ftb.mods.ftblibrary.snbt.config.BooleanValue;
import dev.ftb.mods.ftblibrary.snbt.config.EnumValue;
import dev.ftb.mods.ftblibrary.snbt.config.IntValue;
import dev.ftb.mods.ftblibrary.snbt.config.SNBTConfig;
import dev.ftb.mods.ftblibrary.snbt.config.*;
import dev.ftb.mods.ftblibrary.util.PanelPositioning;
import dev.ftb.mods.ftbquests.api.FTBQuestsAPI;
import dev.ftb.mods.ftbquests.client.config.LocaleValue;
import dev.ftb.mods.ftbquests.net.RequestTranslationTableMessage;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screens.Screen;

Expand All @@ -24,12 +23,22 @@ public interface FTBQuestsClientConfig {
IntValue PINNED_QUESTS_INSET_X = UI.addInt("pinned_quests_inset_x", 2);
IntValue PINNED_QUESTS_INSET_Y = UI.addInt("pinned_quests_inset_y", 2);

SNBTConfig XLATE = CONFIG.addGroup("xlate", 1);
StringValue EDITING_LOCALE = XLATE.add(new LocaleValue(XLATE,"editing_locale", ""));
BooleanValue HILITE_MISSING = XLATE.addBoolean("hilite_missing", true);

// TODO migrate chapter-pinned and pinned-quests data out of per-player team data into here

static void openSettings(Screen screen) {
String prevLocale = EDITING_LOCALE.get();

ConfigGroup group = new ConfigGroup("ftbquests", accepted -> {
if (accepted) {
saveConfig();
if (!prevLocale.equals(EDITING_LOCALE.get()) && ClientQuestFile.INSTANCE != null) {
new RequestTranslationTableMessage(ClientQuestFile.INSTANCE.getLocale()).sendToServer();
ClientQuestFile.INSTANCE.clearCachedData();
}
}
Minecraft.getInstance().setScreen(screen);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import dev.ftb.mods.ftbquests.events.ClearFileCacheEvent;
import dev.ftb.mods.ftbquests.item.FTBQuestsItems;
import dev.ftb.mods.ftbquests.item.LootCrateItem;
import dev.ftb.mods.ftbquests.net.RequestTranslationTableMessage;
import dev.ftb.mods.ftbquests.net.SubmitTaskMessage;
import dev.ftb.mods.ftbquests.quest.BaseQuestFile;
import dev.ftb.mods.ftbquests.quest.Quest;
Expand Down Expand Up @@ -201,6 +202,11 @@ private void onPlayerLogin(LocalPlayer localPlayer) {
FTBQuestsClient.rebuildCreativeTabs();
creativeTabRebuildPending = false;
}

String locale = FTBQuestsClientConfig.EDITING_LOCALE.get();
if (!locale.isEmpty() && !locale.equals(Minecraft.getInstance().options.languageCode)) {
new RequestTranslationTableMessage(locale).sendToServer();
}
}

private void onPlayerLogout(@Nullable LocalPlayer localPlayer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,20 @@ public static void claimReward(UUID teamId, UUID player, long rewardId) {
}
}

public static void createObject(long id, long parent, QuestObjectType type, CompoundTag nbt, @Nullable CompoundTag extra, UUID creator) {
QuestObjectBase object = ClientQuestFile.INSTANCE.create(id, type, parent, extra == null ? new CompoundTag() : extra);
public static void createObject(long id, long parent, QuestObjectType type, CompoundTag nbt, CompoundTag extra, UUID creator) {
ClientQuestFile file = ClientQuestFile.INSTANCE;

QuestObjectBase object = file.create(id, type, parent, extra);
object.readData(nbt);
file.getTranslationManager().processInitialTranslation(extra, object);
object.onCreated();
ClientQuestFile.INSTANCE.refreshIDMap();
file.refreshIDMap();
object.editedFromGUI();
FTBQuests.getRecipeModHelper().refreshRecipes(object);

LocalPlayer player = Minecraft.getInstance().player;
if (object instanceof QuestObject qo && player != null && creator.equals(player.getUUID())) {
ClientQuestFile.INSTANCE.getQuestScreen()
file.getQuestScreen()
.ifPresent(questScreen -> questScreen.open(qo, true));
}

Expand Down Expand Up @@ -95,6 +98,7 @@ public static void deleteObject(long id) {
ClientQuestFile.INSTANCE.refreshIDMap();
object.editedFromGUI();
FTBQuests.getRecipeModHelper().refreshRecipes(object);
ClientQuestFile.INSTANCE.getTranslationManager().removeAllTranslations(object);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@
import dev.ftb.mods.ftbquests.quest.loot.RewardTable;
import dev.ftb.mods.ftbquests.quest.reward.*;
import dev.ftb.mods.ftbquests.quest.task.*;
import dev.ftb.mods.ftbquests.quest.translation.TranslationKey;
import dev.ftb.mods.ftbquests.util.ConfigQuestObject;
import net.minecraft.client.Minecraft;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.StructureBlockEntity;
import net.minecraft.world.phys.BlockHitResult;

import java.util.function.Consumer;
import java.util.function.BiConsumer;

public class GuiProviders {
public static RewardType.GuiProvider defaultRewardGuiProvider(RewardType.Provider provider) {
Expand Down Expand Up @@ -64,7 +66,7 @@ public static TaskType.GuiProvider defaultTaskGuiProvider(TaskType.Provider prov
EditStringConfigOverlay<Long> overlay = new EditStringConfigOverlay<>(panel.getGui(), c, accepted -> {
if (accepted) {
slvTask.setValue(c.getValue());
callback.accept(task);
callback.accept(task, task.getType().makeExtraNBT());
}
panel.run();
}, task.getType().getDisplayName()).atMousePosition();
Expand All @@ -84,7 +86,7 @@ public static void setTaskGuiProviders() {
gui.run();
if (accepted) {
ItemTask itemTask = new ItemTask(0L, quest).setStackAndCount(c.getValue(), c.getValue().getCount());
callback.accept(itemTask);
callback.accept(itemTask, itemTask.getType().makeExtraNBT());
}
}).openGui();
});
Expand All @@ -97,7 +99,9 @@ public static void setTaskGuiProviders() {
if (accepted) {
CheckmarkTask checkmarkTask = new CheckmarkTask(0L, quest);
checkmarkTask.setRawTitle(c.getValue());
callback.accept(checkmarkTask);
CompoundTag extra = checkmarkTask.getType().makeExtraNBT();
quest.getQuestFile().getTranslationManager().addInitialTranslation(extra, quest.getQuestFile().getLocale(), TranslationKey.TITLE, c.getValue());
callback.accept(checkmarkTask, extra);
}
panel.run();
}, TaskTypes.CHECKMARK.getDisplayName()).atMousePosition();
Expand All @@ -113,7 +117,7 @@ public static void setTaskGuiProviders() {
gui.run();
if (accepted) {
FluidTask fluidTask = new FluidTask(0L, quest).setFluid(c.getValue().getFluid());
callback.accept(fluidTask);
callback.accept(fluidTask, fluidTask.getType().makeExtraNBT());
}
}).openGui();
});
Expand Down Expand Up @@ -142,7 +146,7 @@ public static void setTaskGuiProviders() {

if (blockEntity instanceof StructureBlockEntity structure) {
task.initFromStructure(structure);
callback.accept(task);
callback.accept(task, task.getType().makeExtraNBT());
return;
}
}
Expand All @@ -151,11 +155,11 @@ public static void setTaskGuiProviders() {
});
}

private static void openSetupGui(Runnable gui, Consumer<Task> callback, Task task) {
private static void openSetupGui(Runnable gui, BiConsumer<Task, CompoundTag> callback, Task task) {
ConfigGroup group = new ConfigGroup(FTBQuestsAPI.MOD_ID, accepted -> {
gui.run();
if (accepted) {
callback.accept(task);
callback.accept(task, new CompoundTag());
}
});
task.fillConfigGroup(task.createSubGroup(group));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package dev.ftb.mods.ftbquests.client.config;

import com.mojang.datafixers.util.Pair;
import dev.ftb.mods.ftblibrary.config.ConfigCallback;
import dev.ftb.mods.ftblibrary.config.ConfigValue;
import dev.ftb.mods.ftblibrary.icon.Color4I;
import dev.ftb.mods.ftblibrary.ui.Panel;
import dev.ftb.mods.ftblibrary.ui.SimpleTextButton;
import dev.ftb.mods.ftblibrary.ui.Widget;
import dev.ftb.mods.ftblibrary.ui.input.MouseButton;
import dev.ftb.mods.ftblibrary.ui.misc.AbstractButtonListScreen;
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.network.chat.Component;
import org.apache.commons.lang3.mutable.MutableInt;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

public class LocaleConfig extends ConfigValue<String> {
public static final Color4I COLOR = Color4I.rgb(0xFFAA49);

private final LocaleValue localeValue;

public LocaleConfig(LocaleValue localeValue) {
this.localeValue = localeValue;
}

@Override
public void onClicked(Widget clickedWidget, MouseButton button, ConfigCallback callback) {
var gui = new LocaleSelectorScreen(localeValue, callback);

gui.setTitle(Component.translatable("ftbquests.xlate.editing_locale"));
gui.showBottomPanel(false); // no need for accept/cancel buttons here
gui.setHasSearchBox(true);
gui.openGui();
}

@Override
public Color4I getColor(@Nullable String v) {
return COLOR;
}

@Override
public Component getStringForGUI(@Nullable String v) {
return v == null ? NULL_TEXT : Component.literal('"' + v + '"');
}

private class LocaleSelectorScreen extends AbstractButtonListScreen {
private final LocaleValue localeValue;
private final ConfigCallback callback;
private final List<Pair<String,Component>> entries = new ArrayList<>();
private final int widest;

public LocaleSelectorScreen(LocaleValue localeValue, ConfigCallback callback) {
this.localeValue = localeValue;
this.callback = callback;

MutableInt widestM = new MutableInt(0);

entries.add(new Pair<>("", Component.translatable("ftbquests.gui.use_default_lang").withStyle(ChatFormatting.ITALIC)));
Minecraft.getInstance().getLanguageManager().getLanguages().forEach((lang, info) -> {
Component c = Component.literal("[" + lang + "] ").withStyle(ChatFormatting.YELLOW)
.append(info.toComponent().copy().withStyle(ChatFormatting.WHITE));
entries.add(new Pair<>(lang, c));
widestM.setValue(Math.max(widestM.toInteger(), getTheme().getStringWidth(c)));
});

widest = widestM.intValue();
}

@Override
public boolean onInit() {
setWidth(widest + 25);
setHeight(getScreen().getGuiScaledHeight() * 4 / 5);
return true;
}

@Override
public void addButtons(Panel panel) {
entries.forEach(entry -> {
panel.add(new SimpleTextButton(panel, entry.getSecond(), Color4I.empty()) {
@Override
public void onClicked(MouseButton button) {
playClickSound();
boolean changed = setCurrentValue(entry.getFirst());
callback.save(changed);
}
});
});
}

@Override
protected void doCancel() {
callback.save(false);
}

@Override
protected void doAccept() {
callback.save(true);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package dev.ftb.mods.ftbquests.client.config;

import dev.ftb.mods.ftblibrary.config.ConfigGroup;
import dev.ftb.mods.ftblibrary.snbt.config.SNBTConfig;
import dev.ftb.mods.ftblibrary.snbt.config.StringValue;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;

public class LocaleValue extends StringValue {
public LocaleValue(SNBTConfig config, String value, String def) {
super(config, value, def);
}

@Override
@Environment(EnvType.CLIENT)
public void createClientConfig(ConfigGroup group) {
group.add(key, new LocaleConfig(this), get(), this::set, defaultValue);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public EditRewardTableScreen(Runnable parentScreen, RewardTable originalTable, C
this.callback = callback;

editedTable = QuestObjectBase.copy(originalTable, () -> new RewardTable(originalTable.id, originalTable.getFile()));
if (editedTable != null) {
editedTable.setRawTitle(originalTable.getRawTitle());
}

setBorder(1, 1, 1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
public class MultilineTextEditorScreen extends BaseScreen {
private static final Pattern STRIP_FORMATTING_PATTERN = Pattern.compile("(?i)&[0-9A-FK-OR]");
private static final int MAX_UNDO = 10;
protected static final String LINK_TEXT_TEMPLATE = "{ \"text\": \"%s\", \"underlined\": \"true\", \"clickEvent\": { \"action\": \"change_page\", \"value\": \"%016X\" } }";
protected static final String LINK_TEXT_TEMPLATE = "{ \"text\": \"%s\", \"underlined\": true, \"clickEvent\": { \"action\": \"change_page\", \"value\": \"%016X\" } }";

private final Component title;
private final ListConfig<String, StringConfig> config;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@
import dev.ftb.mods.ftbquests.quest.loot.LootCrate;
import dev.ftb.mods.ftbquests.quest.loot.RewardTable;
import dev.ftb.mods.ftbquests.quest.reward.RandomReward;
import dev.ftb.mods.ftbquests.quest.translation.TranslationKey;
import net.minecraft.ChatFormatting;
import net.minecraft.Util;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.world.item.Items;
import org.apache.commons.lang3.mutable.MutableInt;
Expand Down Expand Up @@ -122,14 +125,18 @@ protected void doCancel() {

@Override
protected void doAccept() {
Set<Long> toRemove = ClientQuestFile.INSTANCE.getRewardTables().stream().map(t -> t.id).collect(Collectors.toSet());
ClientQuestFile file = ClientQuestFile.INSTANCE;
Set<Long> toRemove = file.getRewardTables().stream().map(t -> t.id).collect(Collectors.toSet());

rewardTablesCopy.forEach(table -> {
if (table.id == 0) {
if (table.getId() == 0L) {
// newly-created
new CreateObjectMessage(table, null).sendToServer();
CompoundTag extra = Util.make(new CompoundTag(), tag -> file.getTranslationManager().addInitialTranslation(
tag, file.getLocale(), TranslationKey.TITLE, table.getRawTitle())
);
new CreateObjectMessage(table, extra).sendToServer();
}
toRemove.remove(table.id);
toRemove.remove(table.getId());
});

toRemove.forEach(id -> new DeleteObjectMessage(id).sendToServer());
Expand Down
Loading