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

273 lines
8.5 KiB
Java
Raw Normal View History

package noppes.npcs.controllers.data;
2018-09-13 12:03:28 -04:00
import net.minecraft.block.Block;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.ShapedRecipes;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import noppes.npcs.NBTTags;
import noppes.npcs.NoppesUtilPlayer;
import noppes.npcs.NoppesUtilServer;
2022-11-15 15:25:53 -05:00
import noppes.npcs.api.handler.data.IRecipe;
2023-01-12 21:33:06 -05:00
import noppes.npcs.controllers.RecipeController;
import java.io.IOException;
import java.util.HashMap;
2018-09-13 12:03:28 -04:00
2022-01-10 15:21:48 -06:00
public class RecipeCarpentry extends ShapedRecipes implements IRecipe {
2025-03-13 17:14:45 -04:00
public int id = -1;
public String name = "";
2018-09-13 12:03:28 -04:00
public Availability availability = new Availability();
public boolean isGlobal = false;
public boolean ignoreDamage = false;
public boolean ignoreNBT = false;
2025-02-10 02:44:28 -05:00
2018-09-13 12:03:28 -04:00
2025-03-13 17:14:45 -04:00
public RecipeCarpentry(int width, int height, ItemStack[] recipe, ItemStack result) {
super(width, height, recipe, result);
2018-09-13 12:03:28 -04:00
}
2025-03-13 17:14:45 -04:00
public RecipeCarpentry(String name) {
super(0, 0, new ItemStack[0], null);
this.name = name;
}
public static RecipeCarpentry read(NBTTagCompound compound) {
RecipeCarpentry recipe = new RecipeCarpentry(compound.getInteger("Width"), compound.getInteger("Height"),
NBTTags.getItemStackArray(compound.getTagList("Materials", 10)), NoppesUtilServer.readItem(compound.getCompoundTag("Item")));
recipe.name = compound.getString("Name");
recipe.id = compound.getInteger("ID");
recipe.availability.readFromNBT(compound.getCompoundTag("Availability"));
recipe.ignoreDamage = compound.getBoolean("IgnoreDamage");
recipe.ignoreNBT = compound.getBoolean("IgnoreNBT");
recipe.isGlobal = compound.getBoolean("Global");
return recipe;
2018-09-13 12:03:28 -04:00
}
2025-02-10 02:44:28 -05:00
2025-03-13 17:14:45 -04:00
public NBTTagCompound writeNBT() {
NBTTagCompound compound = new NBTTagCompound();
compound.setInteger("ID", id);
compound.setInteger("Width", recipeWidth);
compound.setInteger("Height", recipeHeight);
if (getRecipeOutput() != null)
compound.setTag("Item", NoppesUtilServer.writeItem(getRecipeOutput(), new NBTTagCompound()));
compound.setTag("Materials", NBTTags.nbtItemStackArray(recipeItems));
compound.setTag("Availability", availability.writeToNBT(new NBTTagCompound()));
compound.setString("Name", name);
compound.setBoolean("Global", isGlobal);
compound.setBoolean("IgnoreDamage", ignoreDamage);
compound.setBoolean("IgnoreNBT", ignoreNBT);
return compound;
2018-09-13 12:03:28 -04:00
}
2025-03-13 17:14:45 -04:00
@Override
public boolean matches(InventoryCrafting par1InventoryCrafting, World world) {
for (int i = 0; i <= 4 - this.recipeWidth; ++i) {
for (int j = 0; j <= 4 - this.recipeHeight; ++j) {
2018-09-13 12:03:28 -04:00
if (this.checkMatch(par1InventoryCrafting, i, j, true))
return true;
if (this.checkMatch(par1InventoryCrafting, i, j, false))
return true;
}
}
return false;
2025-03-13 17:14:45 -04:00
}
2018-09-13 12:03:28 -04:00
/**
* Checks if the region of a crafting inventory is match for the recipe.
*/
2025-03-13 17:14:45 -04:00
private boolean checkMatch(InventoryCrafting par1InventoryCrafting, int par2, int par3, boolean par4) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
2018-09-13 12:03:28 -04:00
int var7 = i - par2;
int var8 = j - par3;
ItemStack var9 = null;
2025-03-13 17:14:45 -04:00
if (var7 >= 0 && var8 >= 0 && var7 < this.recipeWidth && var8 < this.recipeHeight) {
2018-09-13 12:03:28 -04:00
if (par4)
var9 = this.recipeItems[this.recipeWidth - var7 - 1 + var8 * this.recipeWidth];
else
var9 = this.recipeItems[var7 + var8 * this.recipeWidth];
}
ItemStack var10 = par1InventoryCrafting.getStackInRowAndColumn(i, j);
2025-03-13 17:14:45 -04:00
if ((var10 != null || var9 != null) && !NoppesUtilPlayer.compareItems(var9, var10, ignoreDamage, ignoreNBT)) {
return false;
2018-09-13 12:03:28 -04:00
}
}
}
return true;
}
2025-03-13 17:14:45 -04:00
@Override
public ItemStack getCraftingResult(InventoryCrafting var1) {
if (getRecipeOutput() == null)
return null;
return getRecipeOutput().copy();
}
2018-09-13 12:03:28 -04:00
2025-03-13 17:14:45 -04:00
@Override
public int getRecipeSize() {
return 16;
}
2018-09-13 12:03:28 -04:00
2025-03-13 17:14:45 -04:00
public static RecipeCarpentry saveRecipe(RecipeCarpentry recipe, ItemStack par1ItemStack, Object... par2ArrayOfObj) {
2018-09-13 12:03:28 -04:00
String var3 = "";
int var4 = 0;
int var5 = 0;
int var6 = 0;
int var9;
2025-03-13 17:14:45 -04:00
if (par2ArrayOfObj[var4] instanceof String[]) {
String[] var7 = (String[]) ((String[]) par2ArrayOfObj[var4++]);
2018-09-13 12:03:28 -04:00
String[] var8 = var7;
var9 = var7.length;
2025-03-13 17:14:45 -04:00
for (int var10 = 0; var10 < var9; ++var10) {
2018-09-13 12:03:28 -04:00
String var11 = var8[var10];
++var6;
var5 = var11.length();
var3 = var3 + var11;
}
2025-03-13 17:14:45 -04:00
} else {
while (par2ArrayOfObj[var4] instanceof String) {
String var13 = (String) par2ArrayOfObj[var4++];
2018-09-13 12:03:28 -04:00
++var6;
var5 = var13.length();
var3 = var3 + var13;
}
}
HashMap var14;
2025-03-13 17:14:45 -04:00
for (var14 = new HashMap(); var4 < par2ArrayOfObj.length; var4 += 2) {
Character var16 = (Character) par2ArrayOfObj[var4];
2018-09-13 12:03:28 -04:00
ItemStack var17 = null;
2025-03-13 17:14:45 -04:00
if (par2ArrayOfObj[var4 + 1] instanceof Item) {
var17 = new ItemStack((Item) par2ArrayOfObj[var4 + 1]);
} else if (par2ArrayOfObj[var4 + 1] instanceof Block) {
var17 = new ItemStack((Block) par2ArrayOfObj[var4 + 1], 1, -1);
} else if (par2ArrayOfObj[var4 + 1] instanceof ItemStack) {
var17 = (ItemStack) par2ArrayOfObj[var4 + 1];
2018-09-13 12:03:28 -04:00
}
var14.put(var16, var17);
}
ItemStack[] var15 = new ItemStack[var5 * var6];
2025-03-13 17:14:45 -04:00
for (var9 = 0; var9 < var5 * var6; ++var9) {
2018-09-13 12:03:28 -04:00
char var18 = var3.charAt(var9);
2025-03-13 17:14:45 -04:00
if (var14.containsKey(Character.valueOf(var18))) {
var15[var9] = ((ItemStack) var14.get(Character.valueOf(var18))).copy();
} else {
2018-09-13 12:03:28 -04:00
var15[var9] = null;
}
}
2025-02-10 02:44:28 -05:00
RecipeCarpentry newrecipe = new RecipeCarpentry(var5, var6, var15, par1ItemStack);
2018-09-13 12:03:28 -04:00
newrecipe.copy(recipe);
2025-03-13 17:14:45 -04:00
if (var5 == 4 || var6 == 4)
newrecipe.isGlobal = false;
2025-02-10 02:44:28 -05:00
2018-09-13 12:03:28 -04:00
return newrecipe;
}
2025-02-10 02:44:28 -05:00
2018-09-13 12:03:28 -04:00
public void copy(RecipeCarpentry recipe) {
2025-03-13 17:14:45 -04:00
this.id = recipe.id;
this.name = recipe.name;
this.availability = recipe.availability;
this.isGlobal = recipe.isGlobal;
this.ignoreDamage = recipe.ignoreDamage;
this.ignoreNBT = recipe.ignoreNBT;
2018-09-13 12:03:28 -04:00
}
2025-03-13 17:14:45 -04:00
public ItemStack getCraftingItem(int i) {
if (recipeItems == null || i >= recipeItems.length)
return null;
return recipeItems[i];
}
public void setCraftingItem(int i, ItemStack item) {
if (i < recipeItems.length)
recipeItems[i] = item;
}
public boolean isValid() {
if (recipeItems.length == 0 || getRecipeOutput() == null)
return false;
for (ItemStack item : recipeItems) {
if (item != null)
return true;
}
return false;
2018-09-13 12:03:28 -04:00
}
2022-01-10 15:21:48 -06:00
public String getName() {
return this.name;
}
public ItemStack getResult() {
return this.getRecipeOutput();
}
public boolean isGlobal() {
return this.isGlobal;
}
public void setIsGlobal(boolean bo) {
this.isGlobal = bo;
}
public boolean getIgnoreNBT() {
return this.ignoreNBT;
}
public void setIgnoreNBT(boolean bo) {
this.ignoreNBT = bo;
}
public boolean getIgnoreDamage() {
return this.ignoreDamage;
}
public void setIgnoreDamage(boolean bo) {
this.ignoreDamage = bo;
}
public int getWidth() {
return this.recipeWidth;
}
public int getHeight() {
return this.recipeHeight;
}
public void save() {
try {
2023-06-05 22:12:48 -04:00
RecipeController.Instance.saveRecipe(this.writeNBT());
2022-01-10 15:21:48 -06:00
} catch (IOException var2) {
}
}
public void delete() {
2023-06-05 22:12:48 -04:00
RecipeController.Instance.delete(this.id);
2022-01-10 15:21:48 -06:00
}
public ItemStack[] getRecipe() {
return this.recipeItems;
}
public int getId() {
return this.id;
}
2018-09-13 12:03:28 -04:00
}