Skip to content

Commit

Permalink
Kit editing (#31)
Browse files Browse the repository at this point in the history
* feat: added setIfAbsent method to ConfigUtils

* Minor kit update.

* feat: kits config foundation

* Revert "feat: kits config foundation"

This reverts commit 90b140d.

* Java 8 compactible

* Resolved 1 review.

#31 (comment)

* Removed 1 unnecessary method.

* Moved initialize kit config to after checking if kit is enabled.

* Reformatted code

* Removed setting name to the config file

* Reformatted inventory section of configuration file for kits.yml

* Reformatted armour section of configuration file for kits.yml

* Added kit item handling, allowing items to be changed while giving kit items.

* Update .gitignore

* Updated snapshot version

* Removed Kit.restock() method.

* Removed Kit.restock() method.

* Fix for a NPE

* Fix for another NPE

* Fix for another another NPE

* Bumped snapshot version

* Update MiniGamesBox Classic/src/main/java/plugily/projects/minigamesbox/classic/kits/basekits/Kit.java

Co-authored-by: montlikadani <[email protected]>

* Update for level kits.

* Updated structure of kit loading and handling

* Fixed getting the itemstack on display for the kit menu

* Fixed default kit

* Fixed clone kit

* Merged suggestion on key_name.

Co-authored-by: montlikadani <[email protected]>

* Restructured code

* Updated KitMenuHandler.java

* Updated version

* Modified how KitRegistry.handleItem is used.

* Create HandleItem.java

* Modified handleitem interface

* Fixed optional configuration values

* Update MiniGamesBox Classic/src/main/java/plugily/projects/minigamesbox/classic/kits/KitRegistry.java

Co-authored-by: montlikadani <[email protected]>

---------

Co-authored-by: montlikadani <[email protected]>
Co-authored-by: Tigerpanzer_02 <[email protected]>
  • Loading branch information
3 people authored Oct 10, 2023
1 parent 7b70817 commit 71031a1
Show file tree
Hide file tree
Showing 13 changed files with 409 additions and 177 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,7 @@ run/
# gradle
.gradle
buildSrc/build
MiniGamesBox Classic/build
MiniGamesBox Database/build
MiniGamesBox Inventory/build
MiniGamesBox Utils/build
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
Expand Up @@ -66,7 +66,7 @@ public void createMenu(Player player) {

gui.addItem(new SimpleClickableItem(itemStack, event -> {
event.setCancelled(true);
if(!(event.isLeftClick() || event.isRightClick()) || !(event.getWhoClicked() instanceof Player) || !ItemUtils.isItemStackNamed(event.getCurrentItem())) {
if(!(event.isLeftClick() || event.isRightClick()) || !(event.getWhoClicked() instanceof Player)) {
return;
}
PluginArena arena = plugin.getArenaRegistry().getArena(player);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,24 @@

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.Optional;
import java.util.logging.Level;

/**
* @author Tigerpanzer_02
Expand All @@ -33,17 +44,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
*/
Expand All @@ -56,23 +69,172 @@ 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(List<String> optionalConfigurations) {

for (String key : kitsConfig.getKeys(false)) {
if (!Objects.equals(key, "Do-Not-Edit")) {
loadKitConfig(key, optionalConfigurations);
}
}
}

/**
* 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, List<String> optionalConfigurations) {
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"));

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 (configurationSection.getString("default_kit") != null) {
this.setDefaultKit(kit);
plugin.getDebugger().debug("Default kit set to " + kit.getKey());
}

if (optionalConfigurations != null) {
optionalConfigurations.forEach((k) -> {
if (configurationSection.contains(k)) {
kit.addOptionalConfiguration(k, configurationSection.get(k));
plugin.getDebugger().debug("Kit " + kit.getKey() + " has optional configuration " + k);
}
});
}

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;
Expand All @@ -97,4 +259,12 @@ public List<Kit> getKits() {
return kits;
}

public static HandleItem getHandleItem() {
return handleItem;
}

public static void setHandleItem(HandleItem handleItem) {
KitRegistry.handleItem = handleItem;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,26 @@

package plugily.projects.minigamesbox.classic.kits.basekits;

import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import plugily.projects.minigamesbox.classic.utils.helper.ItemBuilder;
import plugily.projects.minigamesbox.classic.kits.KitRegistry;

/**
* @author Tigerpanzer_02
* <p>
* Created at 21.09.2021
*/
public abstract class FreeKit extends Kit {
public class FreeKit extends Kit {

public abstract Material getMaterial();
public FreeKit(String key, String name, ItemStack itemStack) {
super(key, name, itemStack);
}

/**
* @return true
*/
@Override
public ItemStack getItemStack() {
return new ItemBuilder(getMaterial())
.name(getName())
.lore(getDescription())
.build();
public boolean isUnlockedByPlayer(Player p) {
return true;
}
}
Loading

0 comments on commit 71031a1

Please sign in to comment.