Move registry instance out of Main

This commit is contained in:
Aubri Seebauer committed 2023-05-02 16:00:10 -04:00
1 parent 1dd938da60
commit 89fc6d1769
6 files changed
+18 -9

No files matched your search

@@ -19,8 +19,6 @@ public class Main extends JavaPlugin {
public static Main instance; public static Main instance;
public static Economy econ; public static Economy econ;
public static AlathraWarLogger warLogger; public static AlathraWarLogger warLogger;
public static WarItemRegistry itemRegistry;
public static WarRecipeRegistry recipeRegistry;
static { static {
instance = null; instance = null;
@@ -101,9 +99,9 @@ public class Main extends JavaPlugin {
getServer().getPluginManager().registerEvents(new PlayerInteractListener(), this); getServer().getPluginManager().registerEvents(new PlayerInteractListener(), this);
//run first //run first
itemRegistry = new WarItemRegistry(); new WarItemRegistry();
//run second //run second
recipeRegistry = new WarRecipeRegistry(); new WarRecipeRegistry();
initData(); initData();
initAPIs(); initAPIs();
@@ -152,9 +152,9 @@ public class AdminCommands implements CommandExecutor {
//find item //find item
ItemStack stack = null; ItemStack stack = null;
if(args[1].contains(Main.getInstance().getName().toLowerCase())) { if(args[1].contains(Main.getInstance().getName().toLowerCase())) {
stack = Main.itemRegistry.getOrNullNamespace(args[1]); stack = WarItemRegistry.getInstance().getOrNullNamespace(args[1]);
} else { } else {
stack = Main.itemRegistry.getOrNull(args[1]); stack = WarItemRegistry.getInstance().getOrNull(args[1]);
} }
// item not real! // item not real!
if(stack == null) { if(stack == null) {
@@ -242,6 +242,6 @@ public class CommandHelper {
* @return list of all alathrawar items namespaced * @return list of all alathrawar items namespaced
*/ */
public static List<String> getWarItems() { public static List<String> getWarItems() {
return new ArrayList<>(WarItemRegistry.getItemRegistry().keySet()); return new ArrayList<>(WarItemRegistry.getInstance().getItemRegistry().keySet());
} }
} }
@@ -15,6 +15,7 @@ import java.util.stream.Stream;
public class WarItemRegistry { public class WarItemRegistry {
private static WarItemRegistry instance;
private static final HashMap<String, IWarItem> itemRegistry = new HashMap<>(); private static final HashMap<String, IWarItem> itemRegistry = new HashMap<>();
@@ -24,6 +25,8 @@ public class WarItemRegistry {
* loads all items into the plugin registry list * loads all items into the plugin registry list
*/ */
public WarItemRegistry() { public WarItemRegistry() {
instance = this;
Main.warLogger.log(Helper.chatLabel() + "Registering AlathraWar items."); Main.warLogger.log(Helper.chatLabel() + "Registering AlathraWar items.");
itemRegistry.put(registryName("ram"), new RamItem()); itemRegistry.put(registryName("ram"), new RamItem());
@@ -31,6 +34,10 @@ public class WarItemRegistry {
Main.warLogger.log(Helper.chatLabel() + "Registered AlathraWar items."); Main.warLogger.log(Helper.chatLabel() + "Registered AlathraWar items.");
} }
public static WarItemRegistry getInstance() {
return instance;
}
/** /**
* generate a registry name * generate a registry name
* @param item * @param item
@@ -11,12 +11,16 @@ import java.util.Set;
public class WarRecipeRegistry { public class WarRecipeRegistry {
private static WarRecipeRegistry instance;
private static final HashMap<String, Recipe> recipeRegistry = new HashMap<>(); private static final HashMap<String, Recipe> recipeRegistry = new HashMap<>();
public WarRecipeRegistry() { public WarRecipeRegistry() {
instance = this;
Main.warLogger.log(Helper.chatLabel() + "Registering AlathraWar recipes."); Main.warLogger.log(Helper.chatLabel() + "Registering AlathraWar recipes.");
register(WarItemRegistry.registryName("ram"), Main.itemRegistry.getWarItem("ram").getRecipes()); register(WarItemRegistry.registryName("ram"), WarItemRegistry.getInstance().getWarItem("ram").getRecipes());
Main.warLogger.log(Helper.chatLabel() + "Registered AlathraWar recipes."); Main.warLogger.log(Helper.chatLabel() + "Registered AlathraWar recipes.");
} }
@@ -48,7 +48,7 @@ public class PlayerInteractListener implements Listener {
if (clicked.getType().toString().contains("DOOR")) { // left click + any door types if (clicked.getType().toString().contains("DOOR")) { // left click + any door types
Door door = (Door) clicked.getBlockData(); Door door = (Door) clicked.getBlockData();
// lock door in the opposite position // lock door in the opposite position
if (item.equals(WarItemRegistry.getOrNull("ram"))) { if (item.equals(Main.itemRegistry.getOrNull("ram"))) {
boolean inSiegeOrRaid = false; boolean inSiegeOrRaid = false;
//siege check //siege check
for (Siege s : SiegeData.getSieges()) { for (Siege s : SiegeData.getSieges()) {