| 1 | package net.minecraft.client.tutorial;
|
| 2 |
|
| 3 | import net.fabricmc.api.EnvType;
|
| 4 | import net.fabricmc.api.Environment;
|
| 5 | import net.minecraft.client.Minecraft;
|
| 6 | import net.minecraft.client.gui.components.toasts.TutorialToast;
|
| 7 | import net.minecraft.client.player.LocalPlayer;
|
| 8 | import net.minecraft.core.Holder;
|
| 9 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 10 | import net.minecraft.network.chat.Component;
|
| 11 | import net.minecraft.stats.Stats;
|
| 12 | import net.minecraft.tags.ItemTags;
|
| 13 | import net.minecraft.tags.TagKey;
|
| 14 | import net.minecraft.world.item.Item;
|
| 15 | import net.minecraft.world.item.ItemStack;
|
| 16 | import org.jspecify.annotations.Nullable;
|
| 17 |
|
| 18 | @Environment(EnvType.CLIENT)
|
| 19 | public class CraftPlanksTutorialStep implements TutorialStepInstance {
|
| 20 | private static final int HINT_DELAY = 1200;
|
| 21 | private static final Component CRAFT_TITLE = Component.translatable("tutorial.craft_planks.title");
|
| 22 | private static final Component CRAFT_DESCRIPTION = Component.translatable("tutorial.craft_planks.description");
|
| 23 | private final Tutorial tutorial;
|
| 24 | private @Nullable TutorialToast toast;
|
| 25 | private int timeWaiting;
|
| 26 |
|
| 27 | public CraftPlanksTutorialStep(final Tutorial tutorial) {
|
| 28 | this.tutorial = tutorial;
|
| 29 | }
|
| 30 |
|
| 31 | @Override
|
| 32 | public void tick() {
|
| 33 | this.timeWaiting++;
|
| 34 | if (!this.tutorial.isSurvival()) {
|
| 35 | this.tutorial.setStep(TutorialSteps.NONE);
|
| 36 | } else {
|
| 37 | Minecraft minecraft = this.tutorial.getMinecraft();
|
| 38 | if (this.timeWaiting == 1) {
|
| 39 | LocalPlayer player = minecraft.player;
|
| 40 | if (player != null) {
|
| 41 | if (player.getInventory().contains(ItemTags.PLANKS)) {
|
| 42 | this.tutorial.setStep(TutorialSteps.NONE);
|
| 43 | return;
|
| 44 | }
|
| 45 |
|
| 46 | if (hasCraftedPlanksPreviously(player, ItemTags.PLANKS)) {
|
| 47 | this.tutorial.setStep(TutorialSteps.NONE);
|
| 48 | return;
|
| 49 | }
|
| 50 | }
|
| 51 | }
|
| 52 |
|
| 53 | if (this.timeWaiting >= 1200 && this.toast == null) {
|
| 54 | this.toast = new TutorialToast(minecraft.font, TutorialToast.Icons.WOODEN_PLANKS, CRAFT_TITLE, CRAFT_DESCRIPTION, false);
|
| 55 | minecraft.gui.toastManager().addToast(this.toast);
|
| 56 | }
|
| 57 | }
|
| 58 | }
|
| 59 |
|
| 60 | @Override
|
| 61 | public void clear() {
|
| 62 | if (this.toast != null) {
|
| 63 | this.toast.hide();
|
| 64 | this.toast = null;
|
| 65 | }
|
| 66 | }
|
| 67 |
|
| 68 | @Override
|
| 69 | public void onGetItem(final ItemStack itemStack) {
|
| 70 | if (itemStack.is(ItemTags.PLANKS)) {
|
| 71 | this.tutorial.setStep(TutorialSteps.NONE);
|
| 72 | }
|
| 73 | }
|
| 74 |
|
| 75 | public static boolean hasCraftedPlanksPreviously(final LocalPlayer player, final TagKey<Item> tag) {
|
| 76 | for (Holder<Item> item : BuiltInRegistries.ITEM.getTagOrEmpty(tag)) {
|
| 77 | if (player.getStats().getValue(Stats.ITEM_CRAFTED.get(item.value())) > 0) {
|
| 78 | return true;
|
| 79 | }
|
| 80 | }
|
| 81 |
|
| 82 | return false;
|
| 83 | }
|
| 84 | }
|
| 85 |
|