summaryrefslogtreewikicommitdiff
path: root/src/main/java/dev/tylerm/khs/configuration/Items.java
blob: af8216d678930f81dc8a767c608acf4409b26107 (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
package dev.tylerm.khs.configuration;

import com.cryptomorin.xseries.XItemStack;
import dev.tylerm.khs.Main;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;

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

public class Items {

    public static List<ItemStack> HIDER_ITEMS, SEEKER_ITEMS;
    public static ItemStack 
        HIDER_HELM, SEEKER_HELM,
        HIDER_CHEST, SEEKER_CHEST,
        HIDER_LEGS, SEEKER_LEGS,
        HIDER_BOOTS, SEEKER_BOOTS;

    public static List<PotionEffect> HIDER_EFFECTS, SEEKER_EFFECTS;

    public static void loadItems() {

        ConfigManager manager = ConfigManager.create("items.yml");

        SEEKER_ITEMS = new ArrayList<>();
        SEEKER_HELM = null;
        SEEKER_CHEST = null;
        SEEKER_LEGS = null;
        SEEKER_BOOTS = null;

        ConfigurationSection SeekerItems = manager.getConfigurationSection("items.seeker");

        for (int i = 0; i < 9; i++) {
            ConfigurationSection section = SeekerItems.getConfigurationSection(String.valueOf(i));
            if (section == null) {
                SEEKER_ITEMS.add(null);
                continue;
            }
            ItemStack item = createItem(section);
            SEEKER_ITEMS.add(item);
        }

        ConfigurationSection SeekerHelmet = SeekerItems.getConfigurationSection("helmet");
        if (SeekerHelmet != null) {
            ItemStack item = createItem(SeekerHelmet);
            if (item != null) {
                SEEKER_HELM = item;
            }
        }

        ConfigurationSection SeekerChestplate = SeekerItems.getConfigurationSection("chestplate");
        if (SeekerChestplate != null) {
            ItemStack item = createItem(SeekerChestplate);
            if (item != null) {
                SEEKER_CHEST = item;
            }
        }

        ConfigurationSection SeekerLeggings = SeekerItems.getConfigurationSection("leggings");
        if (SeekerLeggings != null) {
            ItemStack item = createItem(SeekerLeggings);
            if (item != null) {
                SEEKER_LEGS = item;
            }
        }

        ConfigurationSection SeekerBoots = SeekerItems.getConfigurationSection("boots");
        if (SeekerBoots != null) {
            ItemStack item = createItem(SeekerBoots);
            if (item != null) {
                SEEKER_BOOTS = item;
            }
        }

        HIDER_ITEMS = new ArrayList<>();
        HIDER_HELM = null;
        HIDER_CHEST = null;
        HIDER_LEGS = null;
        HIDER_BOOTS = null;

        ConfigurationSection HiderItems = manager.getConfigurationSection("items.hider");

        for (int i = 0; i < 9; i++) {
            ConfigurationSection section = HiderItems.getConfigurationSection(String.valueOf(i));
            if (section == null) {
                HIDER_ITEMS.add(null);
                continue;
            }
            ItemStack item = createItem(section);
            HIDER_ITEMS.add(item);
        }

        ConfigurationSection HiderHelmet = HiderItems.getConfigurationSection("helmet");
        if (HiderHelmet != null) {
            ItemStack item = createItem(HiderHelmet);
            if (item != null) {
                HIDER_HELM = item;
            }
        }

        ConfigurationSection HiderChestplate = HiderItems.getConfigurationSection("chestplate");
        if (HiderChestplate != null) {
            ItemStack item = createItem(HiderChestplate);
            if (item != null) {
                HIDER_CHEST = item;
            }
        }

        ConfigurationSection HiderLeggings = HiderItems.getConfigurationSection("leggings");
        if (HiderLeggings != null) {
            ItemStack item = createItem(HiderLeggings);
            if (item != null) {
                HIDER_LEGS = item;
            }
        }

        ConfigurationSection HiderBoots = HiderItems.getConfigurationSection("boots");
        if (HiderBoots != null) {
            ItemStack item = createItem(HiderBoots);
            if (item != null) {
                HIDER_BOOTS = item;
            }
        }

        SEEKER_EFFECTS = new ArrayList<>();
        ConfigurationSection SeekerEffects = manager.getConfigurationSection("effects.seeker");

        int i = 1;
        while (true) {
            ConfigurationSection section = SeekerEffects.getConfigurationSection(String.valueOf(i));
            if (section == null) break;
            PotionEffect effect = getPotionEffect(section);
            if (effect != null) SEEKER_EFFECTS.add(effect);
            i++;
        }

        HIDER_EFFECTS = new ArrayList<>();
        ConfigurationSection HiderEffects = manager.getConfigurationSection("effects.hider");
        i = 1;
        while (true) {
            ConfigurationSection section = HiderEffects.getConfigurationSection(String.valueOf(i));
            if (section == null) break;
            PotionEffect effect = getPotionEffect(section);
            if (effect != null) HIDER_EFFECTS.add(effect);
            i++;
        }
    }

    private static ItemStack createItem(ConfigurationSection item) {
        ConfigurationSection config = new YamlConfiguration().createSection("temp");
        String material = item.getString("material").toUpperCase();
        boolean splash = false;
        if (!Main.getInstance().supports(9)) {
            if (material.contains("POTION")) {
                config.set("level", 1);
            }
            if (material.equalsIgnoreCase("SPLASH_POTION") || material.equalsIgnoreCase("LINGERING_POTION")) {
                material = "POTION";
                splash = true;
            }
        }
        config.set("name", item.getString("name"));
        config.set("material", material);
        config.set("enchants", item.getConfigurationSection("enchantments"));
        config.set("unbreakable", item.getBoolean("unbreakable"));
        if (Main.getInstance().supports(14)) {
            if (item.contains("model-data")) {
                config.set("model-data", item.getInt("model-data"));
            }
        }
        if (item.isSet("lore"))
            config.set("lore", item.getStringList("lore"));
        if (material.equalsIgnoreCase("POTION") || material.equalsIgnoreCase("SPLASH_POTION") || material.equalsIgnoreCase("LINGERING_POTION"))
            config.set("base-effect", String.format("%s,%s,%s", item.getString("type"), false, splash));
        ItemStack stack = XItemStack.deserialize(config);
        int amt = item.getInt("amount");
        if (amt < 1) amt = 1;
        stack.setAmount(amt);
        if (stack.getData().getItemType() == Material.AIR) return null;
        return stack;
    }

    private static PotionEffect getPotionEffect(ConfigurationSection item) {
        String type = item.getString("type");
        if (type == null) return null;
        if (PotionEffectType.getByName(type.toUpperCase()) == null) return null;
        return new PotionEffect(
                PotionEffectType.getByName(type.toUpperCase()),
                item.getInt("duration"),
                item.getInt("amplifier"),
                item.getBoolean("ambient"),
                item.getBoolean("particles")
        );
    }

    public static boolean matchItem(ItemStack stack){
        for(ItemStack check : HIDER_ITEMS)
            if(equals(stack,check)) return true;
        for(ItemStack check : SEEKER_ITEMS)
            if(equals(stack,check)) return true;
        return false;
    }

    private static boolean equals(ItemStack a, ItemStack b) {
        if (a == null || b == null) {
            return false;
        } else if (a == b) {
            return true;
        } else {
            return a.getType() == b.getType() && a.hasItemMeta() == b.hasItemMeta() && (!a.hasItemMeta() || Bukkit.getItemFactory().equals(a.getItemMeta(), b.getItemMeta()));
        }
    }

}