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 5 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
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@

package plugily.projects.minigamesbox.classic.kits;

import org.bukkit.configuration.file.FileConfiguration;
import com.cryptomorin.xseries.XItemStack;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.ItemStack;
import plugily.projects.minigamesbox.classic.PluginMain;
import plugily.projects.minigamesbox.classic.kits.basekits.Kit;
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;

/**
* @author Tigerpanzer_02
Expand All @@ -37,7 +40,6 @@ public class KitRegistry {
private Kit defaultKit;
private final PluginMain plugin;

//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;
}
Expand All @@ -48,6 +50,7 @@ public KitRegistry(PluginMain plugin) {
* @param kit Kit to register
*/
public void registerKit(Kit kit) {
ConfigurationSection configurationSection = kit.getKitsConfig().createSection(kit.getKey());
Lagggpixel marked this conversation as resolved.
Show resolved Hide resolved
if(!plugin.getConfigPreferences().getOption("KITS")) {
plugin.getDebugger().debug("Kit " + kit.getKey() + " can't be added as kits are disabled");
return;
Expand All @@ -56,15 +59,59 @@ 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)) {

initializeKitConfig(kit);
kit.saveKitsConfig();
plugin.getDebugger().debug("Kit " + kit.getKey() + "'s config files have been initialized");

if(!configurationSection.getBoolean("enabled", false)) {
plugin.getDebugger().debug("Kit " + kit.getKey() + " is disabled by kits.yml");
return;
}

loadKitConfig(kit);
plugin.getDebugger().debug("Kit " + kit.getKey() + "'s config files have been loaded");

plugin.getDebugger().debug("Registered {0} kit", kit.getKey());
kits.add(kit);
}

public void initializeKitConfig(Kit kit) {
ConfigurationSection configurationSection = kit.getKitsConfig().getConfigurationSection(kit.getKey());
if (configurationSection == null) {
configurationSection = kit.getKitsConfig().createSection(kit.getKey());
}
configurationSection.set("name", kit.getName());
if (!configurationSection.contains("enabled")) {
configurationSection.set("enabled", true);
Lagggpixel marked this conversation as resolved.
Show resolved Hide resolved
}
int currentItem = 0;
if (!configurationSection.contains("items")) {
Lagggpixel marked this conversation as resolved.
Show resolved Hide resolved
ConfigurationSection finalConfigurationSection = configurationSection;
kit.getKitItems().forEach((item, indexes) -> {
ConfigurationSection itemConfigurationSection = finalConfigurationSection.createSection(String.valueOf(currentItem));
XItemStack.serialize(item, itemConfigurationSection.createSection("item"));
itemConfigurationSection.set("slots", indexes);
});
}
}

public void loadKitConfig(Kit kit) {
ConfigurationSection configurationSection = kit.getKitsConfig().getConfigurationSection(kit.getKey());
assert configurationSection != null;
kit.setName(configurationSection.getString("name"));

kit.getKitItems().clear();
HashMap<ItemStack, List<Integer>> kitItems = new HashMap<>();

Objects.requireNonNull(configurationSection.getConfigurationSection("items"), "Items for kit " + kit.getKey() + " is null").getKeys(false).forEach((k) -> {
Lagggpixel marked this conversation as resolved.
Show resolved Hide resolved
ItemStack item = XItemStack.deserialize(Objects.requireNonNull(configurationSection.getConfigurationSection("items." + k + ".item"), "An itemstack in " + kit.getKey() + " is null"));
List<Integer> indexes = configurationSection.getIntegerList("items." + k + ".slots");
kitItems.put(item, indexes);
});
kit.setKitItems(kitItems);
}

/**
* Return default game kit
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@

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

import org.bukkit.ChatColor;
import com.cryptomorin.xseries.XItemStack;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;
import plugily.projects.minigamesbox.classic.PluginMain;
import plugily.projects.minigamesbox.classic.utils.configuration.ConfigUtils;

import java.util.HashMap;
import java.util.List;

/**
Expand All @@ -37,14 +38,22 @@ public abstract class Kit {

private static final PluginMain plugin = JavaPlugin.getPlugin(PluginMain.class);

private final FileConfiguration kitsConfig = ConfigUtils.getConfig(plugin, "kits");
private FileConfiguration kitsConfig = ConfigUtils.getConfig(plugin, "kits");

private String name = "";

private String key = "";

private boolean unlockedOnDefault = false;
private String[] description = new String[0];

// Item index -5: Helmet
// Item index -4: Chestplate
// Item index -3: Leggings
// Item index -2: Boots
// Item index -1: Next available slot
private HashMap<ItemStack, List<Integer>> kitItems = new HashMap<>();

protected Kit() {
}

Expand All @@ -68,6 +77,22 @@ public void setUnlockedOnDefault(boolean unlockedOnDefault) {
this.unlockedOnDefault = unlockedOnDefault;
}

public void addKitItem(ItemStack item, List<Integer> slots) {
kitItems.put(item, slots);
}

public void addKitItem(ItemStack item, Integer slot) {
kitItems.put(item, List.of(slot));
}

public HashMap<ItemStack, List<Integer>> getKitItems() {
return kitItems;
}

public void setKitItems(HashMap<ItemStack, List<Integer>> kitItems) {
this.kitItems = kitItems;
}

/**
* @return main plugin
*/
Expand All @@ -82,6 +107,11 @@ public FileConfiguration getKitsConfig() {
return kitsConfig;
}

public void saveKitsConfig() {
ConfigUtils.saveConfig(plugin, kitsConfig, "kits");
kitsConfig = ConfigUtils.getConfig(plugin, "kits");
}

public String getName() {
return name;
}
Expand All @@ -96,6 +126,7 @@ public void setKey(String key) {
this.key = key;
}

public abstract void setupKitItems();

public String getKey() {
if (key.equalsIgnoreCase("")) {
Expand All @@ -122,8 +153,37 @@ public void setDescription(List<String> description) {

public abstract ItemStack getItemStack();

public abstract void giveKitItems(Player player);
public void giveKitItems(Player player) {
player.getInventory().clear();
kitItems.forEach((item, slots) -> {
for (Integer slot : slots) {
switch (slot) {
case (-5) -> {
Lagggpixel marked this conversation as resolved.
Show resolved Hide resolved
player.getInventory().setHelmet(item);
}
case (-4) -> {
player.getInventory().setChestplate(item);
}
case (-3) -> {
player.getInventory().setLeggings(item);
}
case (-2) -> {
player.getInventory().setBoots(item);
}
case (-1) -> {
player.getInventory().setItem(XItemStack.firstEmpty(player.getInventory(), 0), item);
}
default -> {
Lagggpixel marked this conversation as resolved.
Show resolved Hide resolved
player.getInventory().setItem(slot, item);
}
}
}
});
};

public abstract void reStock(Player player);

public String getKitConfigPath() {
return"kit." + key + ".";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.bukkit.inventory.ItemStack;
import plugily.projects.minigamesbox.classic.handlers.language.MessageBuilder;
import plugily.projects.minigamesbox.classic.kits.basekits.FreeKit;
import plugily.projects.minigamesbox.classic.utils.helper.ArmorHelper;
import plugily.projects.minigamesbox.classic.utils.helper.WeaponHelper;

import java.util.List;
Expand All @@ -50,11 +49,14 @@ public boolean isUnlockedByPlayer(Player player) {
}

@Override
public void giveKitItems(Player player) {
player.getInventory().addItem(WeaponHelper.getUnBreakingSword(WeaponHelper.ResourceType.WOOD, 10));
ArmorHelper.setArmor(player, ArmorHelper.ArmorType.LEATHER);
player.getInventory().addItem(new ItemStack(XMaterial.COOKED_PORKCHOP.parseMaterial(), 8));
public void setupKitItems() {
addKitItem(WeaponHelper.getUnBreakingSword(WeaponHelper.ResourceType.WOOD, 10), 0);
addKitItem(new ItemStack(XMaterial.COOKED_PORKCHOP.parseMaterial()), 8);

addKitItem(new ItemStack(Material.LEATHER_BOOTS), -2);
addKitItem(new ItemStack(Material.LEATHER_LEGGINGS), -3);
addKitItem(new ItemStack(Material.LEATHER_CHESTPLATE), -4);
addKitItem(new ItemStack(Material.LEATHER_HELMET), -5);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public boolean isUnlockedByPlayer(Player player) {
return true;
}

@Override
public void setupKitItems() {

}

@Override
public void giveKitItems(Player player) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,20 @@ public static void saveConfig(JavaPlugin plugin, FileConfiguration config, Strin
}
}

/**
* Sets the data into the config file if it is absent
*
* @param config FileConfiguration to use
* @param key Key of the data value to set
* @param data The data to be set
*
* @return Return value is true if the data value was not set and has now been set.
*/
public static boolean setIfAbsent(FileConfiguration config, String key, Object data) {
if (!config.isSet(key) || !config.contains(key) || config.get(key) == null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

These 3 condition literally doing the same check.

Copy link
Member Author

Choose a reason for hiding this comment

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

Redundancy in case one of them breaks for some random reason.

Copy link
Contributor

Choose a reason for hiding this comment

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

I doubt it, using the get method is completely sufficient. If you want a deep check, you can use the isSet method as this checks to copy the default value or not.

Copy link
Member

Choose a reason for hiding this comment

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

why we got this if its never used?

Copy link
Contributor

Choose a reason for hiding this comment

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

oh, I didn't even see it isn't used

config.set(key, data);
return true;
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,33 +36,33 @@ private ArmorHelper() {

public static void setArmor(Player player, ArmorType type) {
PlayerInventory inv = player.getInventory();
switch(type) {
case LEATHER:
switch (type) {
case LEATHER -> {
Lagggpixel marked this conversation as resolved.
Show resolved Hide resolved
inv.setBoots(new ItemStack(Material.LEATHER_BOOTS));
inv.setLeggings(new ItemStack(Material.LEATHER_LEGGINGS));
inv.setChestplate(new ItemStack(Material.LEATHER_CHESTPLATE));
inv.setHelmet(new ItemStack(Material.LEATHER_HELMET));
break;
case IRON:
}
case IRON -> {
inv.setBoots(new ItemStack(Material.IRON_BOOTS));
inv.setLeggings(new ItemStack(Material.IRON_LEGGINGS));
inv.setChestplate(new ItemStack(Material.IRON_CHESTPLATE));
inv.setHelmet(new ItemStack(Material.IRON_HELMET));
break;
case GOLD:
}
case GOLD -> {
inv.setBoots(XMaterial.GOLDEN_BOOTS.parseItem());
inv.setLeggings(XMaterial.GOLDEN_LEGGINGS.parseItem());
inv.setChestplate(XMaterial.GOLDEN_CHESTPLATE.parseItem());
inv.setHelmet(XMaterial.GOLDEN_HELMET.parseItem());
break;
case DIAMOND:
}
case DIAMOND -> {
inv.setBoots(new ItemStack(Material.DIAMOND_BOOTS));
inv.setLeggings(new ItemStack(Material.DIAMOND_LEGGINGS));
inv.setChestplate(new ItemStack(Material.DIAMOND_CHESTPLATE));
inv.setHelmet(new ItemStack(Material.DIAMOND_HELMET));
break;
default:
break; //o.o
}
default -> {
} //o.o
}
}

Expand Down