-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Kit editing #31
Kit editing #31
Changes from 38 commits
d4620b2
fc71112
259eeb6
2cb9b1c
90b140d
f5a3f57
6a740a0
2d92e26
1eb5a69
227ea09
9f2b5c7
31da022
ec6ef46
b465798
e1c3892
f167035
4572f9b
9245a0d
85b5fbc
00fd8f2
8ca692e
4149775
2bcf610
22582e4
2ce165b
c272aeb
fed38b9
bc3e5a0
d960b92
6bdc46d
bb51e6e
90335d4
804fcab
f4fc006
0cd16ec
34641b3
5dc2315
5f42f21
94a3a35
b41b95b
82fc8dc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package plugily.projects.minigamesbox.classic.kits; | ||
|
||
import org.bukkit.entity.Player; | ||
import org.bukkit.inventory.ItemStack; | ||
|
||
public interface HandleItem { | ||
ItemStack apply(Player player, ItemStack itemStack); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,13 +18,23 @@ | |
|
||
package plugily.projects.minigamesbox.classic.kits; | ||
|
||
import com.cryptomorin.xseries.XItemStack; | ||
import org.bukkit.Material; | ||
import org.bukkit.configuration.ConfigurationSection; | ||
import org.bukkit.configuration.file.FileConfiguration; | ||
import org.bukkit.inventory.ItemStack; | ||
import plugily.projects.minigamesbox.classic.PluginMain; | ||
import plugily.projects.minigamesbox.classic.kits.basekits.FreeKit; | ||
import plugily.projects.minigamesbox.classic.kits.basekits.Kit; | ||
import plugily.projects.minigamesbox.classic.kits.basekits.LevelKit; | ||
import plugily.projects.minigamesbox.classic.kits.basekits.PremiumKit; | ||
import plugily.projects.minigamesbox.classic.kits.free.EmptyKit; | ||
import plugily.projects.minigamesbox.classic.utils.configuration.ConfigUtils; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.logging.Level; | ||
|
||
/** | ||
* @author Tigerpanzer_02 | ||
|
@@ -33,17 +43,19 @@ | |
*/ | ||
public class KitRegistry { | ||
|
||
private final List<Kit> kits = new java.util.ArrayList<>(); | ||
public final List<Kit> kits = new java.util.ArrayList<>(); | ||
private Kit defaultKit; | ||
private final PluginMain plugin; | ||
public final PluginMain plugin; | ||
public FileConfiguration kitsConfig; | ||
private static HandleItem handleItem; | ||
|
||
//todo default kits - kit loading - possibility to edit kits with files - patreon will be ingame gui - kits.yml | ||
public KitRegistry(PluginMain plugin) { | ||
this.plugin = plugin; | ||
kitsConfig = ConfigUtils.getConfig(plugin, "kits"); | ||
} | ||
|
||
/** | ||
* Method for registering new kit | ||
* Method for registering clone and empty kit | ||
* | ||
* @param kit Kit to register | ||
*/ | ||
|
@@ -56,23 +68,162 @@ public void registerKit(Kit kit) { | |
plugin.getDebugger().debug("Kit " + kit.getKey() + " can't be added as its already registered"); | ||
return; | ||
} | ||
FileConfiguration config = ConfigUtils.getConfig(plugin, "kits"); | ||
if(!config.getBoolean("Enabled-Game-Kits." + kit.getKey(), false)) { | ||
|
||
ConfigurationSection configurationSection = kit.getKitConfigSection(); | ||
if(configurationSection != null && !configurationSection.getBoolean("enabled", false)) { | ||
plugin.getDebugger().debug("Kit " + kit.getKey() + " is disabled by kits.yml"); | ||
return; | ||
} | ||
|
||
plugin.getDebugger().debug("Registered {0} kit", kit.getKey()); | ||
kits.add(kit); | ||
} | ||
|
||
/** | ||
* Registers the kits by loading their configurations. | ||
*/ | ||
public void registerKits() { | ||
|
||
for (String key : kitsConfig.getKeys(false)) { | ||
if (!Objects.equals(key, "Do-Not-Edit")) { | ||
loadKitConfig(key); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Loads the configuration for a kit. | ||
* | ||
* @param kit_key the key of the kit to load the configuration for | ||
*/ | ||
public void loadKitConfig(String kit_key) { | ||
ConfigurationSection configurationSection = kitsConfig.getConfigurationSection(kit_key); | ||
|
||
if (configurationSection == null) { | ||
plugin.getDebugger().debug(Level.SEVERE, "Kit " + kit_key + " does not have any configuration."); | ||
plugin.getDebugger().debug(Level.SEVERE, "Kit" + kit_key + " will not be loaded."); | ||
return; | ||
} | ||
|
||
String kit_name = configurationSection.getString("name", kit_key); | ||
|
||
if (configurationSection.getConfigurationSection("display_item") == null) { | ||
configurationSection.set("display_item", XItemStack.serialize(new ItemStack(Material.BEDROCK))); | ||
} | ||
|
||
ItemStack itemStack = XItemStack.deserialize(Objects.requireNonNull(configurationSection.getConfigurationSection("display_item"))); | ||
|
||
if(!configurationSection.getBoolean("enabled", false)) { | ||
plugin.getDebugger().debug("Kit " + kit_key + " is disabled by kits.yml"); | ||
return; | ||
} | ||
|
||
String kitType = configurationSection.getString("kit_type"); | ||
|
||
if (kitType == null) { | ||
plugin.getDebugger().debug(Level.SEVERE, "Kit " + kit_key + " kit_type is null."); | ||
plugin.getDebugger().debug(Level.SEVERE, "Kit" + kit_key + " will not be loaded."); | ||
return; | ||
} | ||
|
||
Kit kit; | ||
|
||
switch (kitType) { | ||
case "free": { | ||
kit = new FreeKit(kit_key, kit_name, itemStack); | ||
break; | ||
} | ||
case "level": { | ||
kit = new LevelKit(kit_key, kit_name, itemStack); | ||
((LevelKit) kit).setLevel(configurationSection.getInt("required-level")); | ||
break; | ||
} | ||
case "premium": { | ||
kit = new PremiumKit(kit_key, kit_name, itemStack); | ||
break; | ||
} | ||
default: { | ||
plugin.getDebugger().debug(Level.SEVERE, "Kit " + kit_key + " kit_type is not recognised."); | ||
plugin.getDebugger().debug(Level.SEVERE, "Kit" + kit_key + " will not be loaded."); | ||
return; | ||
} | ||
} | ||
|
||
if (configurationSection.getString("unlockedOnDefault") == null) { | ||
plugin.getDebugger().debug(Level.SEVERE, "Kit " + kit_key + " does not have an unlockedOnDefault configuration."); | ||
plugin.getDebugger().debug(Level.SEVERE, "Kit" + kit_key + " will not be loaded."); | ||
return; | ||
} | ||
kit.setUnlockedOnDefault(configurationSection.getBoolean("unlockedOnDefault")); | ||
Comment on lines
+153
to
+158
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would not print a warning because of this missing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unlockOnDefault is a value set for all default kits.yml There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So if you set up the kits properly, it should print anything in console. |
||
|
||
HashMap<ItemStack, Integer> kitItems = new HashMap<>(); | ||
|
||
ConfigurationSection inventoryConfigurationSection = configurationSection.getConfigurationSection("inventory"); | ||
if (inventoryConfigurationSection != null) { | ||
inventoryConfigurationSection.getKeys(false).forEach((k) -> { | ||
|
||
ConfigurationSection itemConfigurationSection = inventoryConfigurationSection.getConfigurationSection(k); | ||
assert itemConfigurationSection != null; | ||
|
||
ConfigurationSection itemStackConfigurationSection = itemConfigurationSection.getConfigurationSection("item"); | ||
assert itemStackConfigurationSection != null; | ||
ItemStack item = XItemStack.deserialize(itemStackConfigurationSection); | ||
Integer slot = itemConfigurationSection.getInt("slot"); | ||
|
||
kitItems.put(item, slot); | ||
}); | ||
kit.setKitItems(kitItems); | ||
} | ||
else { | ||
plugin.getDebugger().debug(Level.SEVERE, "The kit " + kit.getKey() + " does not have an inventory configuration section."); | ||
plugin.getDebugger().debug(Level.SEVERE, "The kit " + kit.getKey() + " will not give any inventory items."); | ||
} | ||
|
||
|
||
ConfigurationSection armourConfigurationSection = configurationSection.getConfigurationSection("armour"); | ||
if (armourConfigurationSection != null) { | ||
|
||
ConfigurationSection helmetConfigurationSection = armourConfigurationSection.getConfigurationSection("helmet"); | ||
if (helmetConfigurationSection != null) { | ||
kit.setKitHelmet(XItemStack.deserialize(helmetConfigurationSection)); | ||
} | ||
|
||
ConfigurationSection chestplateConfigurationSection = armourConfigurationSection.getConfigurationSection("chestplate"); | ||
if (chestplateConfigurationSection != null) { | ||
kit.setKitChestplate(XItemStack.deserialize(chestplateConfigurationSection)); | ||
} | ||
|
||
ConfigurationSection leggingsConfigurationSection = armourConfigurationSection.getConfigurationSection("leggings"); | ||
if (leggingsConfigurationSection != null) { | ||
kit.setKitLeggings(XItemStack.deserialize(leggingsConfigurationSection)); | ||
} | ||
|
||
ConfigurationSection bootsConfigurationSection = armourConfigurationSection.getConfigurationSection("boots"); | ||
if (bootsConfigurationSection != null) { | ||
kit.setKitBoots(XItemStack.deserialize(bootsConfigurationSection)); | ||
} | ||
} | ||
else { | ||
plugin.getDebugger().debug(Level.SEVERE, "The kit " + kit.getKey() + " does not have an armour configuration section."); | ||
plugin.getDebugger().debug(Level.SEVERE, "The kit " + kit.getKey() + " will not give any armour items."); | ||
} | ||
|
||
if (!Objects.equals(configurationSection.getString("default_kit"), null)) { | ||
Lagggpixel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.setDefaultKit(kit); | ||
} | ||
|
||
plugin.getDebugger().debug("Kit " + kit.getKey() + " loaded."); | ||
kits.add(kit); | ||
} | ||
|
||
/** | ||
* Return default game kit | ||
* | ||
* @return default game kit | ||
*/ | ||
public Kit getDefaultKit() { | ||
if(defaultKit == null) { | ||
setDefaultKit(new EmptyKit()); | ||
setDefaultKit(new EmptyKit("default", "default")); | ||
} | ||
plugin.getDebugger().debug("getDefaultKit is {0}", defaultKit.getName()); | ||
return defaultKit; | ||
|
@@ -97,4 +248,12 @@ public List<Kit> getKits() { | |
return kits; | ||
} | ||
|
||
public static HandleItem getHandleItem() { | ||
return handleItem; | ||
} | ||
|
||
public static void setHandleItem(HandleItem handleItem) { | ||
KitRegistry.handleItem = handleItem; | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Useless
Objects.requireNonNull
, intellij suggests using it, but the above code set this section if not foundThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just want it there so I don't get yellow code 🤣 .