Skip to content
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

Merged
merged 41 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
d4620b2
feat: added setIfAbsent method to ConfigUtils
Lagggpixel Aug 9, 2023
fc71112
Minor kit update.
Lagggpixel Aug 12, 2023
259eeb6
Merge branch 'Plugily-Projects:master' into dev_kitEdit
Lagggpixel Aug 12, 2023
2cb9b1c
Merge branch 'development' into dev_kitEdit
Lagggpixel Aug 13, 2023
90b140d
feat: kits config foundation
Lagggpixel Aug 13, 2023
f5a3f57
Revert "feat: kits config foundation"
Lagggpixel Aug 14, 2023
6a740a0
Java 8 compactible
Lagggpixel Aug 14, 2023
2d92e26
Resolved 1 review.
Lagggpixel Aug 15, 2023
1eb5a69
Removed 1 unnecessary method.
Lagggpixel Aug 16, 2023
227ea09
Moved initialize kit config to after checking if kit is enabled.
Lagggpixel Aug 16, 2023
9f2b5c7
Reformatted code
Lagggpixel Aug 16, 2023
31da022
Removed setting name to the config file
Lagggpixel Aug 16, 2023
ec6ef46
Reformatted inventory section of configuration file for kits.yml
Lagggpixel Aug 16, 2023
b465798
Reformatted armour section of configuration file for kits.yml
Lagggpixel Aug 17, 2023
e1c3892
Added kit item handling, allowing items to be changed while giving ki…
Lagggpixel Aug 26, 2023
f167035
Update .gitignore
Lagggpixel Aug 26, 2023
4572f9b
Updated snapshot version
Lagggpixel Aug 26, 2023
9245a0d
Removed Kit.restock() method.
Lagggpixel Aug 26, 2023
85b5fbc
Removed Kit.restock() method.
Lagggpixel Aug 26, 2023
00fd8f2
Fix for a NPE
Lagggpixel Aug 26, 2023
8ca692e
Fix for another NPE
Lagggpixel Aug 26, 2023
4149775
Fix for another another NPE
Lagggpixel Aug 26, 2023
2bcf610
Bumped snapshot version
Lagggpixel Aug 26, 2023
22582e4
Update MiniGamesBox Classic/src/main/java/plugily/projects/minigamesb…
Lagggpixel Aug 26, 2023
2ce165b
Update for level kits.
Lagggpixel Sep 18, 2023
c272aeb
Updated structure of kit loading and handling
Lagggpixel Sep 21, 2023
fed38b9
Fixed getting the itemstack on display for the kit menu
Lagggpixel Sep 21, 2023
bc3e5a0
Fixed default kit
Lagggpixel Sep 22, 2023
d960b92
Fixed clone kit
Lagggpixel Sep 22, 2023
6bdc46d
Merge branch 'development' into dev_kitEdit
Lagggpixel Sep 22, 2023
bb51e6e
Merged suggestion on key_name.
Lagggpixel Sep 22, 2023
90335d4
Restructured code
Lagggpixel Sep 23, 2023
804fcab
Merge branch 'dev_kitEdit' of https://github.com/Lagggpixel/MiniGames…
Lagggpixel Sep 23, 2023
f4fc006
Updated KitMenuHandler.java
Lagggpixel Sep 23, 2023
0cd16ec
Updated version
Lagggpixel Sep 23, 2023
34641b3
Modified how KitRegistry.handleItem is used.
Lagggpixel Sep 27, 2023
5dc2315
Create HandleItem.java
Lagggpixel Sep 27, 2023
5f42f21
Modified handleitem interface
Lagggpixel Sep 27, 2023
94a3a35
Fixed optional configuration values
Lagggpixel Sep 27, 2023
b41b95b
Update MiniGamesBox Classic/src/main/java/plugily/projects/minigamesb…
Lagggpixel Sep 27, 2023
82fc8dc
Merge branch 'development' into dev_kitEdit
Tigerpanzer02 Oct 10, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,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
Expand All @@ -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
*/
Expand All @@ -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")));
Copy link
Contributor

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 found

Copy link
Member Author

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 🤣 .


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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not print a warning because of this missing unlockedOnDefault property, I would just specify a default value for this boolean. If this property missing from the config, it will return the default value, pretty useless and dumb users might also ask "what is this error" as usual

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unlockOnDefault is a value set for all default kits.yml

Copy link
Member Author

Choose a reason for hiding this comment

The 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;
Expand All @@ -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;
}

}
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