mirror of
https://github.com/darksaid98/AlathranWars.git
synced 2026-12-01 14:38:55 +00:00
Redo the item registry system for the mod
This commit is contained in:
1 parent
c2b5629df5
commit
1dd938da60
10 files changed
+212
-152
No files matched your search
@@ -1,7 +1,8 @@
|
||||
package me.ShermansWorld.AlathraWar;
|
||||
|
||||
import me.ShermansWorld.AlathraWar.commands.*;
|
||||
import me.ShermansWorld.AlathraWar.items.WarItems;
|
||||
import me.ShermansWorld.AlathraWar.items.WarItemRegistry;
|
||||
import me.ShermansWorld.AlathraWar.items.WarRecipeRegistry;
|
||||
import me.ShermansWorld.AlathraWar.listeners.*;
|
||||
import org.bukkit.plugin.RegisteredServiceProvider;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
@@ -18,7 +19,8 @@ public class Main extends JavaPlugin {
|
||||
public static Main instance;
|
||||
public static Economy econ;
|
||||
public static AlathraWarLogger warLogger;
|
||||
public static WarItems itemRegistry;
|
||||
public static WarItemRegistry itemRegistry;
|
||||
public static WarRecipeRegistry recipeRegistry;
|
||||
|
||||
static {
|
||||
instance = null;
|
||||
@@ -98,7 +100,10 @@ public class Main extends JavaPlugin {
|
||||
getServer().getPluginManager().registerEvents(new BlockBreakListener(), (Plugin) this);
|
||||
getServer().getPluginManager().registerEvents(new PlayerInteractListener(), this);
|
||||
|
||||
itemRegistry = new WarItems();
|
||||
//run first
|
||||
itemRegistry = new WarItemRegistry();
|
||||
//run second
|
||||
recipeRegistry = new WarRecipeRegistry();
|
||||
|
||||
initData();
|
||||
initAPIs();
|
||||
|
||||
@@ -12,7 +12,7 @@ import me.ShermansWorld.AlathraWar.data.RaidData;
|
||||
import me.ShermansWorld.AlathraWar.data.RaidPhase;
|
||||
import me.ShermansWorld.AlathraWar.data.SiegeData;
|
||||
import me.ShermansWorld.AlathraWar.data.WarData;
|
||||
import me.ShermansWorld.AlathraWar.items.WarItems;
|
||||
import me.ShermansWorld.AlathraWar.items.WarItemRegistry;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.boss.KeyedBossBar;
|
||||
@@ -152,9 +152,9 @@ public class AdminCommands implements CommandExecutor {
|
||||
//find item
|
||||
ItemStack stack = null;
|
||||
if(args[1].contains(Main.getInstance().getName().toLowerCase())) {
|
||||
stack = WarItems.getOrNullNamespace(args[1]);
|
||||
stack = Main.itemRegistry.getOrNullNamespace(args[1]);
|
||||
} else {
|
||||
stack = WarItems.getOrNull(args[1]);
|
||||
stack = Main.itemRegistry.getOrNull(args[1]);
|
||||
}
|
||||
// item not real!
|
||||
if(stack == null) {
|
||||
|
||||
@@ -10,7 +10,7 @@ import me.ShermansWorld.AlathraWar.data.RaidData;
|
||||
import me.ShermansWorld.AlathraWar.data.RaidPhase;
|
||||
import me.ShermansWorld.AlathraWar.data.SiegeData;
|
||||
import me.ShermansWorld.AlathraWar.data.WarData;
|
||||
import me.ShermansWorld.AlathraWar.items.WarItems;
|
||||
import me.ShermansWorld.AlathraWar.items.WarItemRegistry;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Statistic;
|
||||
import org.bukkit.command.CommandSender;
|
||||
@@ -242,6 +242,6 @@ public class CommandHelper {
|
||||
* @return list of all alathrawar items namespaced
|
||||
*/
|
||||
public static List<String> getWarItems() {
|
||||
return new ArrayList<>(WarItems.getItemRegistry().keySet());
|
||||
return new ArrayList<>(WarItemRegistry.getItemRegistry().keySet());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package me.ShermansWorld.AlathraWar.items;
|
||||
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.Recipe;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public interface IWarItem {
|
||||
|
||||
public ItemStack getItemStack();
|
||||
|
||||
public Set<Recipe> getRecipes();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package me.ShermansWorld.AlathraWar.items;
|
||||
|
||||
import me.ShermansWorld.AlathraWar.Helper;
|
||||
import me.ShermansWorld.AlathraWar.Main;
|
||||
import me.ShermansWorld.AlathraWar.items.item.RamItem;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class WarItemRegistry {
|
||||
|
||||
|
||||
private static final HashMap<String, IWarItem> itemRegistry = new HashMap<>();
|
||||
|
||||
public static final String namespace = Main.getInstance().getName().toLowerCase();
|
||||
|
||||
/**
|
||||
* loads all items into the plugin registry list
|
||||
*/
|
||||
public WarItemRegistry() {
|
||||
Main.warLogger.log(Helper.chatLabel() + "Registering AlathraWar items.");
|
||||
|
||||
itemRegistry.put(registryName("ram"), new RamItem());
|
||||
|
||||
Main.warLogger.log(Helper.chatLabel() + "Registered AlathraWar items.");
|
||||
}
|
||||
|
||||
/**
|
||||
* generate a registry name
|
||||
* @param item
|
||||
* @return
|
||||
*/
|
||||
public static String registryName(String item) {
|
||||
return namespace + ":" + item;
|
||||
}
|
||||
|
||||
public Map<String, ItemStack> getItemRegistry() {
|
||||
|
||||
return itemRegistry.entrySet()
|
||||
.stream()
|
||||
.filter(v -> v.getValue() != null)
|
||||
.collect(Collectors.toMap(
|
||||
Map.Entry::getKey,
|
||||
v -> v.getValue().getItemStack()));
|
||||
}
|
||||
|
||||
/**
|
||||
* returns the associated itemstack with the item key, use for finding without namespace
|
||||
* @param registryName
|
||||
* @return
|
||||
*/
|
||||
public ItemStack getOrNull(String registryName) {
|
||||
return itemRegistry.get(registryName(registryName)).getItemStack().clone();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* returns the associated itemstack with the item key, use for finding with namespace
|
||||
* @param registryName
|
||||
* @return
|
||||
*/
|
||||
public ItemStack getOrNullNamespace(String registryName) {
|
||||
return itemRegistry.get(registryName).getItemStack().clone();
|
||||
}
|
||||
|
||||
/**
|
||||
* returns the associated IWarItem with the item key, use for finding with namespace
|
||||
* @param registryName
|
||||
* @return
|
||||
*/
|
||||
public IWarItem getWarItem(String registryName) {
|
||||
return itemRegistry.get(registryName);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,78 +0,0 @@
|
||||
package me.ShermansWorld.AlathraWar.items;
|
||||
|
||||
import me.ShermansWorld.AlathraWar.Helper;
|
||||
import me.ShermansWorld.AlathraWar.Main;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class WarItems {
|
||||
|
||||
|
||||
private static final HashMap<String, ItemStack> itemRegistry = new HashMap<>();
|
||||
|
||||
public static final String namespace = Main.getInstance().getName().toLowerCase();
|
||||
|
||||
/**
|
||||
* loads all items into the plugin registry list
|
||||
*/
|
||||
public WarItems() {
|
||||
Main.warLogger.log(Helper.chatLabel() + "Registering AlathraWar items.");
|
||||
|
||||
itemRegistry.put(registryName("ram"), batteringRam());
|
||||
|
||||
Main.warLogger.log(Helper.chatLabel() + "Registered AlathraWar items.");
|
||||
}
|
||||
|
||||
/**
|
||||
* generate a registry name
|
||||
* @param item
|
||||
* @return
|
||||
*/
|
||||
public static String registryName(String item) {
|
||||
return namespace + ":" + item;
|
||||
}
|
||||
|
||||
public static HashMap<String, ItemStack> getItemRegistry() {
|
||||
return itemRegistry;
|
||||
}
|
||||
|
||||
/**
|
||||
* returns the associated itemstack with the item key, use for finding without namespace
|
||||
* @param registryName
|
||||
* @return
|
||||
*/
|
||||
public static ItemStack getOrNull(String registryName) {
|
||||
return itemRegistry.get(registryName(registryName)).clone();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* returns the associated itemstack with the item key, use for finding with namespace
|
||||
* @param registryName
|
||||
* @return
|
||||
*/
|
||||
public static ItemStack getOrNullNamespace(String registryName) {
|
||||
return itemRegistry.get(registryName).clone();
|
||||
}
|
||||
|
||||
|
||||
public ItemStack batteringRam() {
|
||||
|
||||
ItemStack ram = new ItemStack(Material.WOODEN_HOE);
|
||||
ItemMeta meta = ram.getItemMeta();
|
||||
|
||||
meta.setDisplayName("Door Ram");
|
||||
ArrayList<String> lore = new ArrayList<String>();
|
||||
meta.setLore(lore);
|
||||
meta.setCustomModelData(14700);
|
||||
|
||||
ram.setItemMeta(meta);
|
||||
|
||||
return ram;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package me.ShermansWorld.AlathraWar.items;
|
||||
|
||||
import me.ShermansWorld.AlathraWar.AlathraWarLogger;
|
||||
import me.ShermansWorld.AlathraWar.Helper;
|
||||
import me.ShermansWorld.AlathraWar.Main;
|
||||
import me.ShermansWorld.AlathraWar.items.item.RamItem;
|
||||
import org.bukkit.inventory.Recipe;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Set;
|
||||
|
||||
public class WarRecipeRegistry {
|
||||
|
||||
private static final HashMap<String, Recipe> recipeRegistry = new HashMap<>();
|
||||
|
||||
public WarRecipeRegistry() {
|
||||
Main.warLogger.log(Helper.chatLabel() + "Registering AlathraWar recipes.");
|
||||
|
||||
register(WarItemRegistry.registryName("ram"), Main.itemRegistry.getWarItem("ram").getRecipes());
|
||||
|
||||
Main.warLogger.log(Helper.chatLabel() + "Registered AlathraWar recipes.");
|
||||
}
|
||||
|
||||
public void register(String name, Set<Recipe> recipes) {
|
||||
for (Recipe recipe : recipes) {
|
||||
Main.getInstance().getServer().addRecipe(recipe);
|
||||
recipeRegistry.put(name, recipe);
|
||||
}
|
||||
}
|
||||
|
||||
public HashMap<String, Recipe> getRecipeRegistry() {
|
||||
return recipeRegistry;
|
||||
}
|
||||
|
||||
/**
|
||||
* returns the associated itemstack with the item key, use for finding without namespace
|
||||
* @param registryName recipe name without namespace
|
||||
* @return recipe
|
||||
*/
|
||||
public Recipe getOrNull(String registryName) {
|
||||
return recipeRegistry.get(WarItemRegistry.registryName(registryName));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* returns the associated itemstack with the item key, use for finding with namespace
|
||||
* @param registryName registry name including namespace
|
||||
* @return recipe
|
||||
*/
|
||||
public Recipe getOrNullNamespace(String registryName) {
|
||||
return recipeRegistry.get(registryName);
|
||||
}
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
package me.ShermansWorld.AlathraWar.items;
|
||||
|
||||
import me.ShermansWorld.AlathraWar.AlathraWarLogger;
|
||||
import me.ShermansWorld.AlathraWar.Helper;
|
||||
import me.ShermansWorld.AlathraWar.Main;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.Recipe;
|
||||
import org.bukkit.inventory.ShapedRecipe;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class WarRecipes {
|
||||
|
||||
private static final HashMap<String, Recipe> recipeRegistry = new HashMap<>();
|
||||
|
||||
public WarRecipes() {
|
||||
Main.warLogger.log(Helper.chatLabel() + "Registering AlathraWar recipes.");
|
||||
|
||||
recipeRegistry.put(WarItems.registryName("ram"), ramRecipe());
|
||||
|
||||
Main.warLogger.log(Helper.chatLabel() + "Registered AlathraWar recipes.");
|
||||
}
|
||||
|
||||
|
||||
public static HashMap<String, Recipe> getRecipeRegistry() {
|
||||
return recipeRegistry;
|
||||
}
|
||||
|
||||
/**
|
||||
* returns the associated itemstack with the item key, use for finding without namespace
|
||||
* @param registryName recipe name without namespace
|
||||
* @return recipe
|
||||
*/
|
||||
public static Recipe getOrNull(String registryName) {
|
||||
return recipeRegistry.get(WarItems.registryName(registryName));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* returns the associated itemstack with the item key, use for finding with namespace
|
||||
* @param registryName registry name including namespace
|
||||
* @return recipe
|
||||
*/
|
||||
public static Recipe getOrNullNamespace(String registryName) {
|
||||
return recipeRegistry.get(registryName);
|
||||
}
|
||||
|
||||
public Recipe ramRecipe() {
|
||||
ItemStack output = WarItems.getOrNull("ram");
|
||||
NamespacedKey key = new NamespacedKey((Plugin) Main.getInstance(), "ram");
|
||||
ShapedRecipe recipe = new ShapedRecipe(key, output);
|
||||
recipe.shape(new String[] { "$@$", "B##"});
|
||||
recipe.setIngredient('@', Material.STICK);
|
||||
recipe.setIngredient('$', Material.IRON_INGOT);
|
||||
recipe.setIngredient('B', Material.IRON_BLOCK);
|
||||
recipe.setIngredient('%', Material.OAK_LOG);
|
||||
return recipe;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package me.ShermansWorld.AlathraWar.items.item;
|
||||
|
||||
import me.ShermansWorld.AlathraWar.Main;
|
||||
import me.ShermansWorld.AlathraWar.items.IWarItem;
|
||||
import me.ShermansWorld.AlathraWar.items.WarItemRegistry;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.Tag;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.Recipe;
|
||||
import org.bukkit.inventory.RecipeChoice;
|
||||
import org.bukkit.inventory.ShapedRecipe;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Set;
|
||||
|
||||
public class RamItem implements IWarItem {
|
||||
|
||||
public ItemStack getItemStack() {
|
||||
|
||||
ItemStack ram = new ItemStack(Material.WOODEN_HOE);
|
||||
ItemMeta meta = ram.getItemMeta();
|
||||
|
||||
meta.setDisplayName("Door Ram");
|
||||
ArrayList<String> lore = new ArrayList<String>();
|
||||
meta.setLore(lore);
|
||||
meta.setCustomModelData(14700);
|
||||
|
||||
ram.setItemMeta(meta);
|
||||
|
||||
return ram;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Recipe> getRecipes() {
|
||||
ItemStack output = Main.itemRegistry.getOrNull("ram");
|
||||
NamespacedKey key = new NamespacedKey(Main.getInstance(), "ram");
|
||||
ShapedRecipe recipe = new ShapedRecipe(key, output);
|
||||
recipe.shape("$@$", "B##");
|
||||
recipe.setIngredient('@', Material.STICK);
|
||||
recipe.setIngredient('$', Material.IRON_INGOT);
|
||||
recipe.setIngredient('B', Material.IRON_BLOCK);
|
||||
recipe.setIngredient('%', new RecipeChoice.MaterialChoice(Tag.LOGS));
|
||||
return Set.of(recipe);
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,7 @@ import me.ShermansWorld.AlathraWar.Raid;
|
||||
import me.ShermansWorld.AlathraWar.Siege;
|
||||
import me.ShermansWorld.AlathraWar.data.RaidData;
|
||||
import me.ShermansWorld.AlathraWar.data.SiegeData;
|
||||
import me.ShermansWorld.AlathraWar.items.WarItems;
|
||||
import me.ShermansWorld.AlathraWar.items.WarItemRegistry;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.data.Bisected;
|
||||
@@ -48,7 +48,7 @@ public class PlayerInteractListener implements Listener {
|
||||
if (clicked.getType().toString().contains("DOOR")) { // left click + any door types
|
||||
Door door = (Door) clicked.getBlockData();
|
||||
// lock door in the opposite position
|
||||
if (item.equals(WarItems.getOrNull("ram"))) {
|
||||
if (item.equals(WarItemRegistry.getOrNull("ram"))) {
|
||||
boolean inSiegeOrRaid = false;
|
||||
//siege check
|
||||
for (Siege s : SiegeData.getSieges()) {
|
||||
@@ -101,7 +101,7 @@ public class PlayerInteractListener implements Listener {
|
||||
@EventHandler(priority = EventPriority.HIGHEST)
|
||||
public void onPlayerRClick(final PlayerItemDamageEvent event) {
|
||||
ItemStack item = event.getItem();
|
||||
if (item.equals(WarItems.getOrNull("ram"))) {
|
||||
if (item.equals(WarItemRegistry.getOrNull("ram"))) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
@@ -131,7 +131,7 @@ public class PlayerInteractListener implements Listener {
|
||||
}
|
||||
}
|
||||
|
||||
if (item.equals(WarItems.getOrNull("ram"))) {
|
||||
if (item.equals(WarItemRegistry.getOrNull("ram"))) {
|
||||
if (clicked != null) {
|
||||
if (clicked.getType().name().equals("GRASS_BLOCK")
|
||||
|| clicked.getType().name().equals("DIRT")
|
||||
|
||||
Reference in new issue
Block a user