Store the item loaders on the plugin instance

This commit is contained in:
Sn0wStorm 2020-11-08 01:50:17 +01:00
parent 74171f78c9
commit 3fa16414d0
7 changed files with 44 additions and 42 deletions

View File

@ -471,11 +471,11 @@ public class BIngredients {
List<Ingredient> ing = new ArrayList<>(size);
for (; size > 0; size--) {
ItemLoader itemLoader = new ItemLoader(dataVersion, in, in.readUTF());
if (!Ingredient.LOADERS.containsKey(itemLoader.getSaveID())) {
if (!P.p.ingredientLoaders.containsKey(itemLoader.getSaveID())) {
P.p.errorLog("Ingredient Loader not found: " + itemLoader.getSaveID());
break;
}
Ingredient loaded = Ingredient.LOADERS.get(itemLoader.getSaveID()).apply(itemLoader);
Ingredient loaded = P.p.ingredientLoaders.get(itemLoader.getSaveID()).apply(itemLoader);
int amount = in.readShort();
if (loaded != null) {
loaded.setAmount(amount);

View File

@ -9,8 +9,7 @@ import com.dre.brewery.integration.ChestShopListener;
import com.dre.brewery.integration.IntegrationListener;
import com.dre.brewery.integration.barrel.LogBlockBarrel;
import com.dre.brewery.listeners.*;
import com.dre.brewery.recipe.BCauldronRecipe;
import com.dre.brewery.recipe.BRecipe;
import com.dre.brewery.recipe.*;
import com.dre.brewery.utility.BUtil;
import com.dre.brewery.utility.LegacyUtil;
import org.apache.commons.lang.math.NumberUtils;
@ -28,6 +27,7 @@ import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.function.Function;
public class P extends JavaPlugin {
public static P p;
@ -47,6 +47,9 @@ public class P extends JavaPlugin {
public WorldListener worldListener;
public IntegrationListener integrationListener;
// Registrations
public Map<String, Function<ItemLoader, Ingredient>> ingredientLoaders = new HashMap<>();
// Language
public String language;
public LanguageReader languageReader;
@ -95,6 +98,13 @@ public class P extends JavaPlugin {
getServer().getPluginManager().disablePlugin(this);
return;
}
// Register Item Loaders
CustomItem.registerItemLoader(this);
SimpleItem.registerItemLoader(this);
PluginItem.registerItemLoader(this);
// Read data files
BData.readData();
// Setup Metrics
@ -240,6 +250,28 @@ public class P extends JavaPlugin {
}
}
/**
* For loading ingredients from ItemMeta.
* <p>Register a Static function that takes an ItemLoader, containing a DataInputStream.
* <p>Using the Stream it constructs a corresponding Ingredient for the chosen SaveID
*
* @param saveID The SaveID should be a small identifier like "AB"
* @param loadFct The Static Function that loads the Item, i.e.
* public static AItem loadFrom(ItemLoader loader)
*/
public void registerForItemLoader(String saveID, Function<ItemLoader, Ingredient> loadFct) {
ingredientLoaders.put(saveID, loadFct);
}
/**
* Unregister the ItemLoader
*
* @param saveID the chosen SaveID
*/
public void unRegisterItemLoader(String saveID) {
ingredientLoaders.remove(saveID);
}
public static P getInstance() {
return p;
}

View File

@ -75,11 +75,6 @@ public class BData {
}
}
// Register Item Loaders
CustomItem.registerItemLoader();
SimpleItem.registerItemLoader();
PluginItem.registerItemLoader();
// loading Ingredients into ingMap
// Only for Legacy Brews
Map<String, BIngredients> ingMap = new HashMap<>();

View File

@ -1,5 +1,6 @@
package com.dre.brewery.recipe;
import com.dre.brewery.P;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
@ -285,7 +286,7 @@ public class CustomItem extends RecipeItem implements Ingredient {
}
// Needs to be called at Server start
public static void registerItemLoader() {
Ingredient.registerForItemLoader("CI", CustomItem::loadFrom);
public static void registerItemLoader(P p) {
p.registerForItemLoader("CI", CustomItem::loadFrom);
}
}

View File

@ -4,9 +4,6 @@ import org.bukkit.inventory.ItemStack;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
/**
* Item used in a BIngredients, inside BCauldron or Brew,
@ -17,30 +14,6 @@ import java.util.function.Function;
*/
public interface Ingredient {
Map<String, Function<ItemLoader, Ingredient>> LOADERS = new HashMap<>();
/**
* Register a Static function as function that takes an ItemLoader, containing a DataInputStream.
* <p>Using the Stream it constructs a corresponding Ingredient for the chosen SaveID
*
* @param saveID The SaveID should be a small identifier like "AB"
* @param loadFct The Static Function that loads the Item, i.e.
* public static AItem loadFrom(ItemLoader loader)
*/
static void registerForItemLoader(String saveID, Function<ItemLoader, Ingredient> loadFct) {
LOADERS.put(saveID, loadFct);
}
/**
* Unregister the ItemLoader
*
* @param saveID the chosen SaveID
*/
static void unRegisterItemLoader(String saveID) {
LOADERS.remove(saveID);
}
/**
* Saves this Ingredient to the DataOutputStream.
* <p>The first data HAS to be storing the SaveID like:

View File

@ -1,5 +1,6 @@
package com.dre.brewery.recipe;
import com.dre.brewery.P;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
@ -164,8 +165,8 @@ public abstract class PluginItem extends RecipeItem implements Ingredient {
* Registers the chosen SaveID and the loading Method for loading from Brew or BCauldron.
* <p>Needs to be called at Server start.
*/
public static void registerItemLoader() {
Ingredient.registerForItemLoader("PI", PluginItem::loadFrom);
public static void registerItemLoader(P p) {
p.registerForItemLoader("PI", PluginItem::loadFrom);
}

View File

@ -144,8 +144,8 @@ public class SimpleItem extends RecipeItem implements Ingredient {
}
// Needs to be called at Server start
public static void registerItemLoader() {
Ingredient.registerForItemLoader("SI", SimpleItem::loadFrom);
public static void registerItemLoader(P p) {
p.registerForItemLoader("SI", SimpleItem::loadFrom);
}
}