Files
CustomNPC-Plus/src/main/java/noppes/npcs/controllers/RecipeController.java
T

377 lines
13 KiB
Java
Raw Normal View History

2018-09-13 12:03:28 -04:00
package noppes.npcs.controllers;
2025-01-30 09:26:10 -05:00
import kamkeel.npcs.controllers.SyncController;
2018-09-13 12:03:28 -04:00
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.CraftingManager;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import noppes.npcs.CustomNpcs;
2022-11-15 15:25:53 -05:00
import noppes.npcs.api.handler.IRecipeHandler;
2025-02-09 20:49:45 -05:00
import noppes.npcs.api.handler.data.IAnvilRecipe;
2022-11-15 15:25:53 -05:00
import noppes.npcs.api.handler.data.IRecipe;
2025-07-09 15:40:31 -04:00
import noppes.npcs.controllers.data.*;
2023-01-12 21:33:06 -05:00
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;
2018-09-13 12:03:28 -04:00
2022-01-10 15:21:48 -06:00
public class RecipeController implements IRecipeHandler {
2025-03-12 20:12:01 -04:00
private static Collection<RecipeCarpentry> prevRecipes;
2025-02-09 20:49:45 -05:00
public HashMap<Integer, RecipeCarpentry> globalRecipes = new HashMap<Integer, RecipeCarpentry>();
2025-03-12 20:12:01 -04:00
public HashMap<Integer, RecipeCarpentry> carpentryRecipes = new HashMap<Integer, RecipeCarpentry>();
2025-02-09 20:49:45 -05:00
public HashMap<Integer, RecipeAnvil> anvilRecipes = new HashMap<Integer, RecipeAnvil>();
2025-07-09 15:40:31 -04:00
public HashMap<Integer, RecipeScript> carpentryScripts = new HashMap<>();
public HashMap<Integer, RecipeScript> anvilScripts = new HashMap<>();
2025-02-09 20:49:45 -05:00
public static RecipeController Instance;
2018-09-13 12:03:28 -04:00
2025-03-12 20:12:01 -04:00
public static final int version = 1;
public int nextId = 1;
2025-02-09 20:49:45 -05:00
public int nextAnvilId = 1;
2025-03-12 20:12:01 -04:00
public static HashMap<Integer, RecipeCarpentry> syncRecipes = new HashMap<Integer, RecipeCarpentry>();
2025-02-09 20:49:45 -05:00
public static HashMap<Integer, RecipeAnvil> syncAnvilRecipes = new HashMap<Integer, RecipeAnvil>();
2025-03-12 20:12:01 -04:00
public RecipeController() {
Instance = this;
}
public void load() {
loadCategories();
reloadGlobalRecipes(globalRecipes);
}
public static void reloadGlobalRecipes(HashMap<Integer, RecipeCarpentry> globalRecipes) {
List list = CraftingManager.getInstance().getRecipeList();
if (prevRecipes != null) {
list.removeAll(prevRecipes);
}
prevRecipes = new HashSet<RecipeCarpentry>();
for (RecipeCarpentry recipe : globalRecipes.values()) {
if (recipe.isValid())
prevRecipes.add(recipe);
}
list.addAll(prevRecipes);
}
private void loadCategories() {
File saveDir = CustomNpcs.getWorldSaveDirectory();
try {
File file = new File(saveDir, "recipes.dat");
if (file.exists()) {
loadCategories(file);
} else {
globalRecipes.clear();
carpentryRecipes.clear();
2025-02-09 20:49:45 -05:00
anvilRecipes.clear();
2025-03-12 20:12:01 -04:00
loadDefaultRecipes(-1);
}
} catch (Exception e) {
e.printStackTrace();
try {
File file = new File(saveDir, "recipes.dat_old");
if (file.exists()) {
loadCategories(file);
}
} catch (Exception ee) {
e.printStackTrace();
}
}
}
private void loadDefaultRecipes(int i) {
if (i == version)
return;
RecipesDefault.loadDefaultRecipes(i);
saveCategories();
}
private void loadCategories(File file) throws Exception {
2018-09-13 12:03:28 -04:00
NBTTagCompound nbttagcompound1 = CompressedStreamTools.readCompressed(new FileInputStream(file));
nextId = nbttagcompound1.getInteger("LastId");
2025-02-09 20:49:45 -05:00
nextAnvilId = nbttagcompound1.getInteger("LastAnvilId");
2018-09-13 12:03:28 -04:00
NBTTagList list = nbttagcompound1.getTagList("Data", 10);
2025-03-12 20:12:01 -04:00
HashMap<Integer, RecipeCarpentry> globalRecipes = new HashMap<Integer, RecipeCarpentry>();
HashMap<Integer, RecipeCarpentry> carpentryRecipes = new HashMap<Integer, RecipeCarpentry>();
HashMap<Integer, RecipeAnvil> anvilRecipes = new HashMap<Integer, RecipeAnvil>();
if (list != null) {
for (int i = 0; i < list.tagCount(); i++) {
2025-02-09 20:49:45 -05:00
NBTTagCompound compound = list.getCompoundTagAt(i);
2025-03-12 20:12:01 -04:00
if (compound.hasKey("IsAnvil")) {
2025-07-09 15:40:31 -04:00
RecipeAnvil anvil = new RecipeAnvil();
anvil.readNBT(compound);
2025-02-09 20:49:45 -05:00
anvilRecipes.put(anvil.id, anvil);
2025-03-12 20:12:01 -04:00
if (anvil.id > nextAnvilId)
2025-02-09 20:49:45 -05:00
nextAnvilId = anvil.id;
} else {
2025-07-09 15:40:31 -04:00
RecipeCarpentry recipe = RecipeCarpentry.create(compound);
recipe.readNBT(compound);
2025-03-12 20:12:01 -04:00
if (recipe.isGlobal)
globalRecipes.put(recipe.id, recipe);
2025-02-09 20:49:45 -05:00
else
2025-03-12 20:12:01 -04:00
carpentryRecipes.put(recipe.id, recipe);
if (recipe.id > nextId)
2025-02-09 20:49:45 -05:00
nextId = recipe.id;
}
2018-09-13 12:03:28 -04:00
}
}
2025-02-09 20:49:45 -05:00
this.carpentryRecipes = carpentryRecipes;
2018-09-13 12:03:28 -04:00
this.globalRecipes = globalRecipes;
2025-02-09 20:49:45 -05:00
this.anvilRecipes = anvilRecipes;
2025-03-12 20:12:01 -04:00
loadDefaultRecipes(nbttagcompound1.getInteger("Version"));
}
private void saveCategories() {
try {
File saveDir = CustomNpcs.getWorldSaveDirectory();
NBTTagList list = new NBTTagList();
for (RecipeCarpentry recipe : globalRecipes.values()) {
2025-07-09 15:40:31 -04:00
list.appendTag(recipe.writeNBT(true));
2025-03-12 20:12:01 -04:00
}
for (RecipeCarpentry recipe : carpentryRecipes.values()) {
2025-07-09 15:40:31 -04:00
list.appendTag(recipe.writeNBT(true));
2025-03-12 20:12:01 -04:00
}
for (RecipeAnvil recipe : anvilRecipes.values()) {
2025-07-09 15:40:31 -04:00
list.appendTag(recipe.writeNBT(true));
2025-02-09 20:49:45 -05:00
}
2025-03-12 20:12:01 -04:00
NBTTagCompound nbttagcompound = new NBTTagCompound();
nbttagcompound.setTag("Data", list);
nbttagcompound.setInteger("LastId", nextId);
2025-02-09 20:49:45 -05:00
nbttagcompound.setInteger("LastAnvilId", nextAnvilId);
2025-03-12 20:12:01 -04:00
nbttagcompound.setInteger("Version", version);
2018-09-13 12:03:28 -04:00
File file = new File(saveDir, "recipes.dat_new");
File file1 = new File(saveDir, "recipes.dat_old");
File file2 = new File(saveDir, "recipes.dat");
CompressedStreamTools.writeCompressed(nbttagcompound, new FileOutputStream(file));
2025-03-12 20:12:01 -04:00
if (file1.exists()) {
2018-09-13 12:03:28 -04:00
file1.delete();
}
file2.renameTo(file1);
2025-03-12 20:12:01 -04:00
if (file2.exists()) {
2018-09-13 12:03:28 -04:00
file2.delete();
}
file.renameTo(file2);
2025-03-12 20:12:01 -04:00
if (file.exists()) {
2018-09-13 12:03:28 -04:00
file.delete();
}
2025-03-12 20:12:01 -04:00
} catch (Exception e) {
e.printStackTrace();
}
2018-09-13 12:03:28 -04:00
}
2025-03-12 20:12:01 -04:00
public RecipeCarpentry findMatchingRecipe(InventoryCrafting par1InventoryCrafting) {
for (RecipeCarpentry recipe : carpentryRecipes.values()) {
if (recipe.isValid() && recipe.matches(par1InventoryCrafting, null))
return recipe;
}
return null;
}
public RecipeCarpentry getRecipe(int id) {
if (globalRecipes.containsKey(id))
return globalRecipes.get(id);
if (carpentryRecipes.containsKey(id))
return carpentryRecipes.get(id);
return null;
}
2018-09-13 12:03:28 -04:00
2025-02-09 20:49:45 -05:00
public RecipeAnvil getAnvilRecipe(int id) {
2025-03-12 20:12:01 -04:00
if (anvilRecipes.containsKey(id))
2025-02-09 20:49:45 -05:00
return anvilRecipes.get(id);
return null;
}
2025-03-12 20:12:01 -04:00
public RecipeCarpentry saveRecipe(NBTTagCompound compound) throws IOException {
2025-07-09 15:40:31 -04:00
RecipeCarpentry recipe = RecipeCarpentry.create(compound);
recipe.readNBT(compound);
2025-03-12 20:12:01 -04:00
RecipeCarpentry current = getRecipe(recipe.id);
if (current != null && !current.name.equals(recipe.name)) {
while (containsRecipeName(recipe.name))
recipe.name += "_";
}
if (recipe.id == -1) {
recipe.id = getUniqueId();
while (containsRecipeName(recipe.name))
recipe.name += "_";
}
if (recipe.isGlobal) {
carpentryRecipes.remove(recipe.id);
globalRecipes.put(recipe.id, recipe);
} else {
globalRecipes.remove(recipe.id);
carpentryRecipes.put(recipe.id, recipe);
}
saveCategories();
reloadGlobalRecipes(globalRecipes);
return recipe;
}
2018-09-13 12:03:28 -04:00
2025-02-09 20:49:45 -05:00
public RecipeAnvil saveAnvilRecipe(NBTTagCompound compound) throws IOException {
2025-07-09 15:40:31 -04:00
RecipeAnvil recipe = new RecipeAnvil();
recipe.readNBT(compound);
2025-02-09 20:49:45 -05:00
RecipeAnvil current = getAnvilRecipe(recipe.id);
2025-03-12 20:12:01 -04:00
if (current != null && !current.name.equals(recipe.name)) {
while (containsAnvilRecipeName(recipe.name))
2025-02-09 20:49:45 -05:00
recipe.name += "_";
}
2025-03-12 20:12:01 -04:00
if (recipe.id == -1) {
2025-02-09 20:49:45 -05:00
recipe.id = getUniqueAnvilId();
2025-03-12 20:12:01 -04:00
while (containsAnvilRecipeName(recipe.name))
2025-02-09 20:49:45 -05:00
recipe.name += "_";
}
anvilRecipes.put(recipe.id, recipe);
saveCategories();
return recipe;
}
2025-03-12 20:12:01 -04:00
private int getUniqueId() {
return nextId++;
}
2025-02-09 20:49:45 -05:00
private int getUniqueAnvilId() {
return nextAnvilId++;
}
2025-03-12 20:12:01 -04:00
private boolean containsRecipeName(String name) {
name = name.toLowerCase();
for (RecipeCarpentry recipe : globalRecipes.values()) {
if (recipe.name.toLowerCase().equals(name))
return true;
}
for (RecipeCarpentry recipe : carpentryRecipes.values()) {
if (recipe.name.toLowerCase().equals(name))
return true;
}
return false;
}
2018-09-13 12:03:28 -04:00
2025-02-09 20:49:45 -05:00
private boolean containsAnvilRecipeName(String name) {
name = name.toLowerCase();
2025-03-12 20:12:01 -04:00
for (RecipeAnvil recipe : anvilRecipes.values()) {
if (recipe.name.toLowerCase().equals(name))
2025-02-09 20:49:45 -05:00
return true;
}
return false;
}
2025-03-12 20:12:01 -04:00
public RecipeCarpentry delete(int id) {
RecipeCarpentry recipe = getRecipe(id);
if (recipe == null)
return null;
2025-03-12 20:12:01 -04:00
RecipeCarpentry globalRecipe = globalRecipes.remove(recipe.id);
if (globalRecipe != null)
2025-01-30 09:26:10 -05:00
SyncController.syncAllWorkbenchRecipes();
2025-01-30 09:22:43 -05:00
RecipeCarpentry carpentry = carpentryRecipes.remove(recipe.id);
2025-07-09 15:40:31 -04:00
if (carpentry != null){
carpentryScripts.remove(recipe.id);
2025-02-09 20:49:45 -05:00
SyncController.syncAllCarpentryRecipes();
2025-07-09 15:40:31 -04:00
}
2025-01-30 09:22:43 -05:00
2025-03-12 20:12:01 -04:00
saveCategories();
reloadGlobalRecipes(globalRecipes);
return recipe;
}
2025-02-09 20:49:45 -05:00
public RecipeAnvil deleteAnvil(int id) {
RecipeAnvil recipe = getAnvilRecipe(id);
2025-03-12 20:12:01 -04:00
if (recipe == null)
2025-02-09 20:49:45 -05:00
return null;
RecipeAnvil anvilRecipe = anvilRecipes.remove(recipe.id);
2025-03-12 20:12:01 -04:00
if (anvilRecipe != null)
2025-02-10 02:44:28 -05:00
SyncController.syncAllAnvilRecipes();
2025-02-09 20:49:45 -05:00
saveCategories();
return recipe;
}
2025-03-12 20:12:01 -04:00
public void addRecipe(RecipeCarpentry recipeCarpentry) {
recipeCarpentry.id = getUniqueId();
if (!recipeCarpentry.isGlobal)
RecipeController.Instance.carpentryRecipes.put(recipeCarpentry.id, recipeCarpentry);
else {
RecipeController.Instance.globalRecipes.put(recipeCarpentry.id, recipeCarpentry);
}
}
2022-01-10 15:21:48 -06:00
2025-02-09 20:49:45 -05:00
public void addAnvilRecipe(RecipeAnvil recipeAnvil) {
recipeAnvil.id = getUniqueAnvilId();
anvilRecipes.put(recipeAnvil.id, recipeAnvil);
}
@Override
2025-03-12 20:12:01 -04:00
public List<IRecipe> getGlobalList() {
return new ArrayList(this.globalRecipes.values());
}
2022-01-10 15:21:48 -06:00
2025-02-09 20:49:45 -05:00
@Override
2025-03-12 20:12:01 -04:00
public List<IRecipe> getCarpentryList() {
return new ArrayList(this.carpentryRecipes.values());
}
2022-01-10 15:21:48 -06:00
2025-02-09 20:49:45 -05:00
@Override
public List<IAnvilRecipe> getAnvilList() {
List<IAnvilRecipe> list = new ArrayList<>();
list.addAll(anvilRecipes.values());
return list;
}
@Override
2025-03-12 20:12:01 -04:00
public void addRecipe(String name, boolean global, ItemStack result, Object... objects) {
RecipeCarpentry recipe = new RecipeCarpentry(name);
recipe.isGlobal = global;
recipe = RecipeCarpentry.saveRecipe(recipe, result, objects);
try {
2025-07-09 15:40:31 -04:00
this.saveRecipe(recipe.writeNBT(true));
2025-03-12 20:12:01 -04:00
} catch (Exception var7) {
var7.printStackTrace();
}
}
2022-01-10 15:21:48 -06:00
2025-02-09 20:49:45 -05:00
@Override
2025-03-12 20:12:01 -04:00
public void addRecipe(String name, boolean global, ItemStack result, int width, int height, ItemStack... objects) {
ArrayList<ItemStack> list = new ArrayList<>();
int var9 = objects.length;
for (int var10 = 0; var10 < var9; ++var10) {
ItemStack item = objects[var10];
if (item.stackSize > 0) {
list.add(item);
}
}
RecipeCarpentry recipe = new RecipeCarpentry(width, height, list.toArray(new ItemStack[0]), result);
recipe.isGlobal = global;
recipe.name = name;
try {
2025-07-09 15:40:31 -04:00
this.saveRecipe(recipe.writeNBT(true));
2025-03-12 20:12:01 -04:00
} catch (IOException var12) {
var12.printStackTrace();
}
}
2025-02-09 20:49:45 -05:00
@Override
public void addAnvilRecipe(String name, boolean global, ItemStack itemToRepair, ItemStack repairMaterial, int xpCost, float repairPercentage) {
RecipeAnvil recipe = new RecipeAnvil(name, itemToRepair, repairMaterial, xpCost, repairPercentage);
try {
2025-07-09 15:40:31 -04:00
this.saveAnvilRecipe(recipe.writeNBT(true));
2025-02-09 20:49:45 -05:00
} catch (Exception e) {
e.printStackTrace();
}
}
2018-09-13 12:03:28 -04:00
}