aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/dev/mayaqq/ygasi/gui/BranchGui.java
blob: eca0226cf1232a89cfe1fe4f05d4550f17d5ec8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package dev.mayaqq.ygasi.gui;

import dev.mayaqq.ygasi.util.AdvUtils;
import eu.pb4.sgui.api.elements.*;

import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.screen.ScreenHandlerType;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.stat.Stats;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;

import static dev.mayaqq.ygasi.registry.StatRegistry.SKILL_POINTS;

import dev.mayaqq.ygasi.registry.ConfigRegistry;
import net.minecraft.util.Identifier;

public class BranchGui {
    public static void gui(ServerPlayerEntity player) {
        Text cost = Text.of("§3" + ConfigRegistry.CONFIG.branchCost);
        try {
            SkillGui gui = new SkillGui(ScreenHandlerType.GENERIC_9X3, player, false) {};
            //the title of the gui
            gui.setTitle(Text.translatable("gui.ygasi.branch.title", player.getStatHandler().getStat(Stats.CUSTOM.getOrCreateStat(SKILL_POINTS))).formatted(Formatting.DARK_AQUA));

            //background items
            for (int x = 0; x <= 26; x++) {
                gui.setSlot(x, new GuiElementBuilder()
                        .setItem(Items.GRAY_STAINED_GLASS_PANE)
                        .setName(Text.of(" "))
                );
            }
            for (int x = 10; x <= 16; x++) {
                gui.setSlot(x, new GuiElementBuilder()
                        .setItem(Items.LIGHT_BLUE_STAINED_GLASS_PANE)
                        .setName(Text.of(" "))
                );
            }
            for (int x = 0; x <= 2; x++) {
                for (int y = 2; y <= 6; y+=2) {
                    gui.setSlot(x * 9 + y, new GuiElementBuilder()
                            .setItem(Items.LIGHT_BLUE_STAINED_GLASS_PANE)
                            .setName(Text.of(" "))
                    );
                }
            }

            //branch items
            //mercenary gui button
            if (!AdvUtils.getAdvancementProgress(player, "minecraft", "ygasi/mercenary")) {
                gui.setSlot(11, new GuiElementBuilder()
                        .setItem(Items.DIAMOND_SWORD)
                        .setCustomModelData(1)
                        .hideFlag(ItemStack.TooltipSection.MODIFIERS)
                        .addLoreLine(Text.translatable("gui.ygasi.branch.cost", cost))
                        .setName(Text.translatable("gui.ygasi.branch.mercenary.title").formatted(Formatting.BOLD))
                        .setCallback((index, clickType, actionType) -> save(player, "mercenary", "gui.ygasi.branch.mercenary.title"))
                );
            } else {
                gui.setSlot(11, new GuiElementBuilder()
                        .setItem(Items.DIAMOND_SWORD)
                        .hideFlag(ItemStack.TooltipSection.MODIFIERS)
                        .glow()
                        .setName(Text.translatable("gui.ygasi.branch.mercenary.title").formatted(Formatting.BOLD))
                        .setCallback((index, clickType, actionType) -> MercenaryGui.gui(player))
                );
            }

            //wizardry gui button
            if (!AdvUtils.getAdvancementProgress(player, "minecraft", "ygasi/wizardry")) {
                gui.setSlot(13, new GuiElementBuilder()
                        .setItem(Items.BLAZE_ROD)
                        .addLoreLine(Text.translatable("gui.ygasi.branch.cost", cost))
                        .setName(Text.translatable("gui.ygasi.branch.wizardry.title").formatted(Formatting.BOLD))
                        .setCallback((index, clickType, actionType) -> save(player, "wizardry", "gui.ygasi.branch.wizardry.title"))
                );
            } else {
                gui.setSlot(13, new GuiElementBuilder()
                        .setItem(Items.BLAZE_ROD)
                        .glow()
                        .setName(Text.translatable("gui.ygasi.branch.wizardry.title").formatted(Formatting.BOLD))
                        .setCallback((index, clickType, actionType) -> WizardryGui.gui(player))
                );
            }

            //druidry gui button
            if (!AdvUtils.getAdvancementProgress(player, "minecraft", "ygasi/druidry")) {
                gui.setSlot(15, new GuiElementBuilder()
                        .setItem(Items.OAK_SAPLING)
                        .addLoreLine(Text.translatable("gui.ygasi.branch.cost", cost))
                        .setName(Text.translatable("gui.ygasi.branch.druidry.title").formatted(Formatting.BOLD))
                        .setCallback((index, clickType, actionType) -> save(player, "druidry", "gui.ygasi.branch.druidry.title"))
                );
            } else {
                gui.setSlot(15, new GuiElementBuilder()
                        .setItem(Items.OAK_SAPLING)
                        .glow()
                        .setName(Text.translatable("gui.ygasi.branch.druidry.title").formatted(Formatting.BOLD))
                        .setCallback((index, clickType, actionType) -> DruidryGui.gui(player))
                );
            }

            //extra gui button
            if (!AdvUtils.getAdvancementProgress(player, "minecraft", "ygasi/extra")) {
                gui.setSlot(26, new GuiElementBuilder()
                        .setItem(Items.BOOK)
                        .addLoreLine(Text.translatable("gui.ygasi.branch.cost", Text.of("§3" + ConfigRegistry.CONFIG.branchCost / 2)))
                        .setName(Text.translatable("gui.ygasi.branch.extra.title").formatted(Formatting.BOLD))
                        .setCallback((index, clickType, actionType) -> save(player, "extra", "gui.ygasi.branch.extra.title"))
                );
            } else {
                gui.setSlot(26, new GuiElementBuilder()
                        .setItem(Items.BOOK)
                        .glow()
                        .setName(Text.translatable("gui.ygasi.branch.extra.title").formatted(Formatting.BOLD))
                        .setCallback((index, clickType, actionType) -> ExtraGui.gui(player))
                );
            }

            //info item button
            gui.setSlot(18, new GuiElementBuilder()
                    .setItem(Items.PAPER)
                    .setName(Text.translatable("gui.ygasi.branch.info.title"))
                    .setCallback((index, clickType, actionType) -> {
                        player.closeHandledScreen();
                        player.sendMessage(Text.translatable("gui.ygasi.branch.info.main"), false);
                    })
            );
            //reset item button
            gui.setSlot(8, new GuiElementBuilder()
                    .setItem(Items.BARRIER)
                    .setName(Text.translatable("gui.ygasi.branch.reset.title"))
                    .addLoreLine(Text.translatable("gui.ygasi.branch.reset.lore"))
                    .addLoreLine(Text.translatable("gui.ygasi.branch.reset.lore2"))
                    .setCallback((index, clickType, actionType) -> ResetGui.gui(player))
            );

            //grant the player the root advancement of ygasi
            AdvUtils.grantAdvancementCriterion(player, new Identifier("minecraft", "ygasi/root"), "opened_skill_menu");

            gui.open();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //this happens when you try to unlock a branch
    public static void save(ServerPlayerEntity player, String branch, String branchName) {
        boolean hasMercenary = AdvUtils.getAdvancementProgress(player, "minecraft", "ygasi/mercenary");
        boolean hasWizadry = AdvUtils.getAdvancementProgress(player, "minecraft", "ygasi/wizardry");
        boolean hasDrudiry = AdvUtils.getAdvancementProgress(player, "minecraft", "ygasi/druidry");
        //special category for extra branch because it's half the price
        if (branch.equals("extra")) {
            if (player.getStatHandler().getStat(Stats.CUSTOM.getOrCreateStat(SKILL_POINTS)) >= ConfigRegistry.CONFIG.branchCost / 2) {
                //remove the spent skill points
                player.getStatHandler().setStat(player, Stats.CUSTOM.getOrCreateStat(SKILL_POINTS), player.getStatHandler().getStat(Stats.CUSTOM.getOrCreateStat(SKILL_POINTS)) - ConfigRegistry.CONFIG.branchCost / 2);
                AdvUtils.grantAdvancementCriterion(player, new Identifier("minecraft", "ygasi/extra"), "unlocked_extra");
                player.sendMessage(Text.translatable("gui.ygasi.branch.unlock", Text.translatable(branchName)), false);
                ExtraGui.gui(player);
            } else {
                player.sendMessage(Text.translatable("gui.ygasi.branch.no.skill"), false);
            }
        //the rest of the branches
        } else {
            //same code as in extra
            if (player.getStatHandler().getStat(Stats.CUSTOM.getOrCreateStat(SKILL_POINTS)) >= ConfigRegistry.CONFIG.branchCost) {
                //grant the player the advancement of the branch they unlocked
                switch (branch) {
                    case "mercenary" -> {
                        if (hasWizadry || hasDrudiry) {
                            player.sendMessage(Text.translatable("gui.ygasi.branch.no.unlock"), true);
                        } else {
                            hasMercenary = true;
                            AdvUtils.grantAdvancementCriterion(player, new Identifier("minecraft", "ygasi/mercenary"), "unlocked_mercenary");
                            MercenaryGui.gui(player);
                            unlockSuccess(player, branchName);
                        }
                    }
                    case "wizardry" -> {
                        if (hasMercenary || hasDrudiry) {
                            player.sendMessage(Text.translatable("gui.ygasi.branch.no.unlock"), true);
                        } else {
                            hasWizadry = true;
                            AdvUtils.grantAdvancementCriterion(player, new Identifier("minecraft", "ygasi/wizardry"), "unlocked_wizardry");
                            WizardryGui.gui(player);
                            unlockSuccess(player, branchName);
                        }
                    }
                    case "druidry" -> {
                        if (hasMercenary || hasWizadry) {
                            player.sendMessage(Text.translatable("gui.ygasi.branch.no.unlock"), true);
                        } else {
                            hasDrudiry = true;
                            AdvUtils.grantAdvancementCriterion(player, new Identifier("minecraft", "ygasi/druidry"), "unlocked_druidry");
                            DruidryGui.gui(player);
                            unlockSuccess(player, branchName);
                        }
                    }
                }
                player.closeHandledScreen();
            //if the player doesn't have enough skill points
            } else {
                player.sendMessage(Text.translatable("gui.ygasi.branch.no.skill"), false);
                player.closeHandledScreen();
            }
        }
    }
    private static void unlockSuccess(ServerPlayerEntity player, String branchName) {
        player.sendMessage(Text.translatable("gui.ygasi.branch.unlock", Text.translatable(branchName)), false);
        //remove the spent skill points
        player.getStatHandler().setStat(player, Stats.CUSTOM.getOrCreateStat(SKILL_POINTS), player.getStatHandler().getStat(Stats.CUSTOM.getOrCreateStat(SKILL_POINTS)) - ConfigRegistry.CONFIG.branchCost);
    }
}