Hashmap kits

This commit is contained in:
officereso 2020-06-07 21:52:32 -05:00
parent f181630635
commit 3804b500f5
3 changed files with 146 additions and 78 deletions

View File

@ -15,6 +15,7 @@ public class Kit extends JavaPlugin {
private List<ItemStack> itemStackList;
private int cost;
private int viewPosition;
private Integer invPosition;
/**
@ -25,86 +26,90 @@ public class Kit extends JavaPlugin {
* Must be between 0 and 26.
*
*/
public Kit(String name, List<ItemStack> items, int cost, int viewPosition){
public Kit(String name, List<ItemStack> items, int cost, int viewPosition, Integer invPosition){
this.name = name;
this.itemStackList = items;
this.cost = cost;
this.viewPosition = viewPosition;
this.invPosition = invPosition;
}
/**
* This constructor takes a string and parses it into
* a list of ItemStacks.
*
* @param name Kit name
* @param items String from config formated like
* "DIAMOND_SWORD 1, DIAMOND_SHOVEL 1"
* @param cost The amount in XP that the kit object costs.
* @param viewPosition Where the kit will show up in the kit selection inventory.
* Must be between 0 and 26.
*/
@Deprecated
public Kit(String name, String items, int cost, int viewPosition){
this.name = name;
List<String[]> list = new ArrayList<String[]>();
try {
for (String item : items.split(", ")) {
list.add(item.split(" "));
}
for (String[] item : list){
itemStackList.add(new ItemStack(Material.getMaterial(item[0]), Integer.parseInt(item[1])));
if (!(item[2].equals("NONE"))){ // If the enchantment is not set to none
}
}
this.cost = cost;
this.viewPosition = viewPosition;
}
catch(NullPointerException e){
getLogger().severe("Malformed config for kit "+name+". \n" +
"Items should be ITEM_NAME #ammount, ITEM_NAME #ammount");
}
}
public Kit(String name, int cost, int viewPosition, String... args){
this.name = name;
this.cost = cost;
this.viewPosition = viewPosition;
}
public void addKitToInventory(@NotNull PlayerInventory playerInventory){
for (ItemStack itemStack : itemStackList){
playerInventory.addItem(itemStack);
}
}
public String getKitName(){
return name;
}
public List<ItemStack> getItems(){
return itemStackList;
}
public int getCost() {
return cost;
}
public int getViewPosition() {
return viewPosition;
}
@Override
public String toString() {
return "Kit{" +
"name='" + name + '\'' +
", itemStackList=" + itemStackList +
", cost=" + cost +
", viewPosition=" + viewPosition +
"} " + super.toString();
}
//
//
// /**
// * This constructor takes a string and parses it into
// * a list of ItemStacks.
// *
// * @param name Kit name
// * @param items String from config formated like
// * "DIAMOND_SWORD 1, DIAMOND_SHOVEL 1"
// * @param cost The amount in XP that the kit object costs.
// * @param viewPosition Where the kit will show up in the kit selection inventory.
// * Must be between 0 and 26.
// */
// @Deprecated
// public Kit(String name, String items, int cost, int viewPosition){
// this.name = name;
// List<String[]> list = new ArrayList<String[]>();
// try {
// for (String item : items.split(", ")) {
// list.add(item.split(" "));
// }
//
// for (String[] item : list){
// itemStackList.add(new ItemStack(Material.getMaterial(item[0]), Integer.parseInt(item[1])));
// if (!(item[2].equals("NONE"))){ // If the enchantment is not set to none
//
// }
// }
//
// this.cost = cost;
// this.viewPosition = viewPosition;
// }
// catch(NullPointerException e){
// getLogger().severe("Malformed config for kit "+name+". \n" +
// "Items should be ITEM_NAME #ammount, ITEM_NAME #ammount");
// }
// }
//
// public Kit(String name, int cost, int viewPosition){
// this.name = name;
// this.cost = cost;
// this.viewPosition = viewPosition;
//
//
// }
//
//
//
// public void addKitToInventory(@NotNull PlayerInventory playerInventory){
// for (ItemStack itemStack : itemStackList){
// playerInventory.addItem(itemStack);
// }
// }
//
// public String getKitName(){
// return name;
// }
//
// public List<ItemStack> getItems(){
// return itemStackList;
// }
//
// public int getCost() {
// return cost;
// }
//
// public int getViewPosition() {
// return viewPosition;
// }
//
// @Override
// public String toString() {
// return "Kit{" +
// "name='" + name + '\'' +
// ", itemStackList=" + itemStackList +
// ", cost=" + cost +
// ", viewPosition=" + viewPosition +
// "} " + super.toString();
// }
}

View File

@ -5,6 +5,6 @@ import org.bukkit.Material;
public class main {
public static void main(String[] args) {
String test = null;
}
}

View File

@ -1,14 +1,26 @@
package io.github.officereso;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
public class pvp extends JavaPlugin implements Listener {
private final FileConfiguration config = this.getConfig();
@Override
public void onEnable() {
HashMap<String, Kit> kits = configToKit();
this.saveDefaultConfig();
}
@ -17,4 +29,55 @@ public class pvp extends JavaPlugin implements Listener {
public void onDisable() {
getLogger().info("onDisable is called!");
}
private HashMap<String, Kit> configToKit() {
HashMap<String, Kit> kits = new HashMap<String, Kit>();
final @NotNull Set<String> keys = config.getConfigurationSection("kits.sword1.items.1").getKeys(false);
for (String kit : config.getConfigurationSection("kits").getKeys(false)){
String name = config.getString("kits."+kit+".name");
Integer cost = (Integer) config.get("kits."+kit+".cost");
Integer position = (Integer) config.get("kits."+kit+".position");
Integer inventoryPos = (Integer) config.get("kits."+kit+".inventoryPos");
if (name == null){
getLogger().severe("Kit " + name + " has invalid name");
return null;
} // Checks for bad names.
if (cost == null){
getLogger().severe("Kit " + name + " has invalid cost");
return null;
} // Checks for bad cost.
if (position == null){
getLogger().severe("Kit " + name + " has invalid position");
return null;
} // Checks for bad position.
List<ItemStack> itemStackList = new ArrayList<ItemStack>();
for (String item : config.getConfigurationSection("kits."+kit+".items").getKeys(false)){
ItemStack itemStack = new ItemStack(
Material.valueOf(config.getString("kits."+kit+".items."+item+".material")), // Material located in config
(config.getInt("kits."+kit+".items."+item+".amount"))); // Amount of the item
String enchantments = config.getString("kits."+kit+".items."+item+".enchantments");
if (enchantments == null){
itemStackList.add(itemStack);
} // If no enchantments create add itemStack to itemStackList
List<String[]> list = new ArrayList<String[]>();
for (String enchantment : enchantments.split(", ")) {
list.add(enchantment.split(" "));
}
ItemMeta itemMeta = itemStack.getItemMeta();
for (String[] enchantmentData : list){
itemMeta.addEnchant(Enchantment.getByKey(NamespacedKey.minecraft(enchantmentData[0].toLowerCase())), Integer.parseInt(enchantmentData[1]), true);
}
}
Kit kitClass = new Kit(name, itemStackList, cost, position, inventoryPos);
kits.put(name, kitClass);
}
return kits;
}
}