| 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.multiplayer.ClientLevel;
|
| 8 | import net.minecraft.client.player.LocalPlayer;
|
| 9 | import net.minecraft.core.Holder;
|
| 10 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 11 | import net.minecraft.network.chat.Component;
|
| 12 | import net.minecraft.stats.Stats;
|
| 13 | import net.minecraft.tags.BlockTags;
|
| 14 | import net.minecraft.tags.ItemTags;
|
| 15 | import net.minecraft.world.item.ItemStack;
|
| 16 | import net.minecraft.world.level.block.Block;
|
| 17 | import net.minecraft.world.level.block.state.BlockState;
|
| 18 | import net.minecraft.world.phys.BlockHitResult;
|
| 19 | import net.minecraft.world.phys.HitResult;
|
| 20 | import net.minecraft.world.phys.HitResult.Type;
|
| 21 | import org.jspecify.annotations.Nullable;
|
| 22 |
|
| 23 | @Environment(EnvType.CLIENT)
|
| 24 | public class FindTreeTutorialStepInstance implements TutorialStepInstance {
|
| 25 | private static final int HINT_DELAY = 6000;
|
| 26 | private static final Component TITLE = Component.translatable("tutorial.find_tree.title");
|
| 27 | private static final Component DESCRIPTION = Component.translatable("tutorial.find_tree.description");
|
| 28 | private final Tutorial tutorial;
|
| 29 | private @Nullable TutorialToast toast;
|
| 30 | private int timeWaiting;
|
| 31 |
|
| 32 | public FindTreeTutorialStepInstance(final Tutorial tutorial) {
|
| 33 | this.tutorial = tutorial;
|
| 34 | }
|
| 35 |
|
| 36 | @Override
|
| 37 | public void tick() {
|
| 38 | this.timeWaiting++;
|
| 39 | if (!this.tutorial.isSurvival()) {
|
| 40 | this.tutorial.setStep(TutorialSteps.NONE);
|
| 41 | } else {
|
| 42 | Minecraft minecraft = this.tutorial.getMinecraft();
|
| 43 | if (this.timeWaiting == 1) {
|
| 44 | LocalPlayer player = minecraft.player;
|
| 45 | if (player != null && (hasCollectedTreeItems(player) || hasPunchedTreesPreviously(player))) {
|
| 46 | this.tutorial.setStep(TutorialSteps.CRAFT_PLANKS);
|
| 47 | return;
|
| 48 | }
|
| 49 | }
|
| 50 |
|
| 51 | if (this.timeWaiting >= 6000 && this.toast == null) {
|
| 52 | this.toast = new TutorialToast(minecraft.font, TutorialToast.Icons.TREE, TITLE, DESCRIPTION, false);
|
| 53 | minecraft.gui.toastManager().addToast(this.toast);
|
| 54 | }
|
| 55 | }
|
| 56 | }
|
| 57 |
|
| 58 | @Override
|
| 59 | public void clear() {
|
| 60 | if (this.toast != null) {
|
| 61 | this.toast.hide();
|
| 62 | this.toast = null;
|
| 63 | }
|
| 64 | }
|
| 65 |
|
| 66 | @Override
|
| 67 | public void onLookAt(final ClientLevel level, final HitResult hit) {
|
| 68 | if (hit.getType() == Type.BLOCK) {
|
| 69 | BlockState state = level.getBlockState(((BlockHitResult)hit).getBlockPos());
|
| 70 | if (state.is(BlockTags.COMPLETES_FIND_TREE_TUTORIAL)) {
|
| 71 | this.tutorial.setStep(TutorialSteps.PUNCH_TREE);
|
| 72 | }
|
| 73 | }
|
| 74 | }
|
| 75 |
|
| 76 | @Override
|
| 77 | public void onGetItem(final ItemStack itemStack) {
|
| 78 | if (itemStack.is(ItemTags.COMPLETES_FIND_TREE_TUTORIAL)) {
|
| 79 | this.tutorial.setStep(TutorialSteps.CRAFT_PLANKS);
|
| 80 | }
|
| 81 | }
|
| 82 |
|
| 83 | private static boolean hasCollectedTreeItems(final LocalPlayer player) {
|
| 84 | return player.getInventory().hasAnyMatching(item -> item.is(ItemTags.COMPLETES_FIND_TREE_TUTORIAL));
|
| 85 | }
|
| 86 |
|
| 87 | public static boolean hasPunchedTreesPreviously(final LocalPlayer player) {
|
| 88 | for (Holder<Block> holder : BuiltInRegistries.BLOCK.getTagOrEmpty(BlockTags.COMPLETES_FIND_TREE_TUTORIAL)) {
|
| 89 | Block block = holder.value();
|
| 90 | if (player.getStats().getValue(Stats.BLOCK_MINED.get(block)) > 0) {
|
| 91 | return true;
|
| 92 | }
|
| 93 | }
|
| 94 |
|
| 95 | return false;
|
| 96 | }
|
| 97 | }
|
| 98 |
|