aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/dev/mayaqq/ygasi/gui/BranchGui.java
blob: 3c8e4865d719a6bf67b016e68701fdac39e092d1 (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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package dev.mayaqq.ygasi.gui;

import dev.mayaqq.ygasi.Ygasi;
import dev.mayaqq.ygasi.gui.common.SkillGui;
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.sound.SoundCategory;
import net.minecraft.sound.SoundEvent;
import net.minecraft.sound.SoundEvents;
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;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

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", Text.of("§3" + player.getStatHandler().getStat(Stats.CUSTOM.getOrCreateStat(SKILL_POINTS)))));

            //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, "ygasi", "mercenary/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/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))
                );
            }

            for (int i = 13; i <=15; i += 2) {
                gui.setSlot(i, new GuiElementBuilder()
                        .setItem(Items.BARRIER)
                        .setName(Text.translatable("gui.ygasi.branch.wip").formatted(Formatting.RED))
                );
            }
            gui.setSlot(26, new GuiElementBuilder()
                    .setItem(Items.BARRIER)
                    .setName(Text.translatable("gui.ygasi.branch.wip").formatted(Formatting.RED))
            );

            /*
            //wizardry gui button
            if (!AdvUtils.getAdvancementProgress(player, "ygasi", "wizardry/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.TNT)
                    .setName(Text.translatable("gui.ygasi.branch.reset.title"))
                    .addLoreLine(Text.translatable("gui.ygasi.branch.reset.lore"))
                    .addLoreLine(Text.translatable("gui.ygasi.branch.reset.lore2"))
                    .addLoreLine(Text.translatable("gui.ygasi.branch.reset.lore3", Text.of("§8" + ConfigRegistry.CONFIG.resetCost)))
                    .setCallback((index, clickType, actionType) -> ResetGui.gui(player))
            );

            //config button for admins
            if (player.hasPermissionLevel(4)) {
                gui.setSlot(0, new GuiElementBuilder()
                        .setItem(Items.COMPARATOR)
                        .setName(Text.translatable("gui.ygasi.branch.config.title"))
                        .addLoreLine(Text.translatable("gui.ygasi.branch.config.lore"))
                        .setCallback((index, clickType, actionType) -> {
                            ConfigGui.gui(player, true);
                            player.playSound(SoundEvent.of(Ygasi.click), SoundCategory.PLAYERS, 1.0F, 1.0F);
                        })
                );
            }

            //grant the player the root advancement of ygasi
            AdvUtils.grantAdvancementCriterion(player, new Identifier("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, "ygasi", "mercenary/mercenary");
        boolean hasWizadry = AdvUtils.getAdvancementProgress(player, "ygasi", "wizardry/wizardry");
        boolean hasDrudiry = AdvUtils.getAdvancementProgress(player, "ygasi", "druidry/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("ygasi", "extra/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 {
            if (player.getStatHandler().getStat(Stats.CUSTOM.getOrCreateStat(SKILL_POINTS)) >= ConfigRegistry.CONFIG.branchCost) {
                //grant the player the advancement of the branch they unlocked
                final Map<String, Class<?>> BRANCH_TO_GUI = new HashMap<>() {{
                    put("mercenary/mercenary", MercenaryGui.class);
                    put("wizardry/wizardry", WizardryGui.class);
                    put("druidry/druidry", DruidryGui.class);
                }};

                Identifier advancementId = new Identifier("ygasi", branch);
                Class<?> guiClass = BRANCH_TO_GUI.get(branch);
                if (hasMercenary || hasWizadry || hasDrudiry) {
                    player.sendMessage(Text.translatable("gui.ygasi.branch.no.unlock"), true);
                    player.playSound(SoundEvents.BLOCK_ANVIL_BREAK, SoundCategory.PLAYERS, 1.0F, 1.0F);
                    player.closeHandledScreen();
                } else {
                    AdvUtils.grantAdvancementCriterion(player, advancementId, "unlocked_" + branch.split("/")[0]);
                    unlockSuccess(player, branchName);
                    try {
                        Method guiMethod = guiClass.getMethod("gui", ServerPlayerEntity.class);
                        guiMethod.invoke(null, player);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            //if the player doesn't have enough skill points
            } else {
                player.sendMessage(Text.translatable("gui.ygasi.branch.no.skill"), true);
                player.playSound(SoundEvents.BLOCK_ANVIL_BREAK, SoundCategory.PLAYERS, 1.0F, 1.0F);
                player.closeHandledScreen();
            }
        }
    }
    private static void unlockSuccess(ServerPlayerEntity player, String branchName) {
        player.playSound(SoundEvents.BLOCK_ENCHANTMENT_TABLE_USE, SoundCategory.PLAYERS, 1.0F, 1.0F);
        player.sendMessage(Text.translatable("gui.ygasi.branch.unlock", Text.translatable(branchName)), true);
        //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);
    }
}