diff --git a/src/main/java/com/hbm/handler/nei/SatelliteHandler.java b/src/main/java/com/hbm/handler/nei/SatelliteHandler.java new file mode 100644 index 0000000000..77e0c3cb47 --- /dev/null +++ b/src/main/java/com/hbm/handler/nei/SatelliteHandler.java @@ -0,0 +1,189 @@ +package com.hbm.handler.nei; + +import codechicken.nei.NEIServerUtils; +import codechicken.nei.PositionedStack; +import codechicken.nei.recipe.TemplateRecipeHandler; +import com.hbm.blocks.ModBlocks; +import com.hbm.handler.imc.ICompatNHNEI; +import com.hbm.itempool.ItemPool; +import com.hbm.itempool.ItemPoolsSatellite; +import com.hbm.items.ModItems; +import com.hbm.lib.RefStrings; +import com.hbm.saveddata.satellites.SatelliteMiner; +import com.hbm.util.ItemStackUtil; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.util.EnumChatFormatting; +import net.minecraft.util.MathHelper; +import net.minecraft.util.WeightedRandomChestContent; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; + +import static codechicken.lib.gui.GuiDraw.drawTexturedModalRect; + +public class SatelliteHandler extends TemplateRecipeHandler implements ICompatNHNEI { + @Override + public ItemStack[] getMachinesForRecipe() { + return new ItemStack[] { + new ItemStack(ModBlocks.sat_dock) + }; + } + + @Override + public String getRecipeID() { + return "ntmSatellite"; + } + + @Override + public String getRecipeName() { + return "Satellite"; + } + + @Override + public String getGuiTexture() { + return RefStrings.MODID + ":textures/gui/nei/gui_nei_anvil.png"; + } + + @Override + public void loadCraftingRecipes(String outputId, Object... results) { + if(outputId.equals("ntmSatellite")) { + for(Item satelliteItem : new Item[]{ModItems.sat_miner, ModItems.sat_lunar_miner}) { + String poolName = SatelliteMiner.getCargoForItem(satelliteItem); + if(poolName == null) { + continue; + } + this.addRecipeToList(satelliteItem, ItemPool.getPool(poolName)); + } + } else { + super.loadCraftingRecipes(outputId, results); + } + } + + @Override + public void loadCraftingRecipes(ItemStack result) { + for(Item satelliteItem : new Item[]{ModItems.sat_miner, ModItems.sat_lunar_miner}) { + String poolName = SatelliteMiner.getCargoForItem(satelliteItem); + if(poolName == null) { + continue; + } + WeightedRandomChestContent[] pool = ItemPool.getPool(poolName); + for(WeightedRandomChestContent poolEntry : pool) { + if(NEIServerUtils.areStacksSameTypeCrafting(poolEntry.theItemId, result)) { + this.addRecipeToList(satelliteItem, pool); + break; + } + } + } + } + + @Override + public void loadUsageRecipes(String inputId, Object... ingredients) { + if(inputId.equals("ntmSatellite")) { + loadCraftingRecipes("ntmSatellite"); + } else { + super.loadUsageRecipes(inputId, ingredients); + } + } + + @Override + public void loadUsageRecipes(ItemStack ingredient) { + if(ingredient.getItem() == ModItems.sat_miner) { + this.addRecipeToList(ModItems.sat_miner, ItemPool.getPool(ItemPoolsSatellite.POOL_SAT_MINER)); + } else if(ingredient.getItem() == ModItems.sat_lunar_miner) { + this.addRecipeToList(ModItems.sat_lunar_miner, ItemPool.getPool(ItemPoolsSatellite.POOL_SAT_LUNAR)); + } + } + + + private void addRecipeToList(Item poolItem, WeightedRandomChestContent[] poolEntries) { + List outs = new ArrayList<>(); + int weight = Arrays.stream(poolEntries).mapToInt(poolEntry -> poolEntry.itemWeight).sum(); + + for(WeightedRandomChestContent poolEntry : poolEntries) { + ItemStack stack = poolEntry.theItemId.copy(); + + float chance = 100F * poolEntry.itemWeight / weight; + ItemStackUtil.addTooltipToStack(stack, EnumChatFormatting.RED + "" + ((int)(chance * 10F) / 10F) + "%"); + + outs.add(stack); + } + + this.arecipes.add(new RecipeSet(new ItemStack(poolItem), outs)); + } + + @Override + public void drawBackground(int recipe) { + super.drawBackground(recipe); + + drawTexturedModalRect(11, 23, 113, 105, 18, 18); //in + drawTexturedModalRect(47, 5, 5, 87, 108, 54); //out + drawTexturedModalRect(29, 14, 131, 96, 18, 36); //operation + } + + public class RecipeSet extends TemplateRecipeHandler.CachedRecipe { + List input = new ArrayList<>(); + List output = new ArrayList<>(); + PositionedStack satelliteDock; + + public RecipeSet(Object in, List out) { + //not the prettiest of solutions but certainly the most pleasant to work with + int inLine = 1; + int outLine = 1; + int inOX = 0; + int inOY = 0; + int outOX = 0; + int outOY = 0; + int anvX = 0; + int anvY = 31; + + outLine = 6; + inOX = 12; + inOY = 24; + outOX = 48; + outOY = 6; + anvX = 30; + + this.input.add(new PositionedStack(in, inOX, inOY)); + + int overflowCount = out.size() / 18; + for(int i = 0; i < Math.min(out.size(), 18); i++) { + ItemStack[] stacks = new ItemStack[overflowCount + 1]; + for(int j = 0; j < overflowCount + 1 && j * 18 + i < out.size(); j++) { + stacks[j] = out.get(j * 18 + i); + } + this.output.add(new PositionedStack(stacks, outOX + 18 * (i % outLine), outOY + 18 * (i / outLine))); + } + + this.satelliteDock = new PositionedStack(new ItemStack(ModBlocks.sat_dock), anvX, anvY); + } + + @Override + public List getIngredients() { + return getCycledIngredients(cycleticks / 20, input); + } + + @Override + public PositionedStack getResult() { + return output.get(0); + } + + @Override + public List getOtherStacks() { + ArrayList stacks = new ArrayList<>(output); + stacks.add(satelliteDock); + return getCycledIngredients(cycleticks / 20, stacks); + } + } + + private static HashMap getRecipeMap() { + HashMap recipeMap = new HashMap<>(); + ItemStack minerStack = new ItemStack(ModItems.sat_miner); + ItemStack lunarMinerStack = new ItemStack(ModItems.sat_lunar_miner); + Arrays.stream(ItemPool.getPool(ItemPoolsSatellite.POOL_SAT_MINER)).forEach(poolEntry -> recipeMap.put(minerStack, poolEntry.theItemId)); + Arrays.stream(ItemPool.getPool(ItemPoolsSatellite.POOL_SAT_LUNAR)).forEach(poolEntry -> recipeMap.put(lunarMinerStack, poolEntry.theItemId)); + return recipeMap; + } +} diff --git a/src/main/java/com/hbm/main/NEIRegistry.java b/src/main/java/com/hbm/main/NEIRegistry.java index 79c495534e..12d7101c71 100644 --- a/src/main/java/com/hbm/main/NEIRegistry.java +++ b/src/main/java/com/hbm/main/NEIRegistry.java @@ -43,6 +43,7 @@ public static List listAllHandlers() { handlers.add(new CrucibleCastingHandler()); handlers.add(new ToolingHandler()); handlers.add(new ConstructionHandler()); + handlers.add(new SatelliteHandler()); //universal boyes handlers.add(new ZirnoxRecipeHandler()); diff --git a/src/main/java/com/hbm/saveddata/satellites/SatelliteMiner.java b/src/main/java/com/hbm/saveddata/satellites/SatelliteMiner.java index 8481a54e90..daca3b22db 100644 --- a/src/main/java/com/hbm/saveddata/satellites/SatelliteMiner.java +++ b/src/main/java/com/hbm/saveddata/satellites/SatelliteMiner.java @@ -2,6 +2,7 @@ import com.hbm.itempool.ItemPoolsSatellite; import com.hbm.util.WeightedRandomObject; +import net.minecraft.item.Item; import net.minecraft.nbt.NBTTagCompound; import java.util.HashMap; @@ -13,15 +14,15 @@ public class SatelliteMiner extends Satellite { private static final HashMap, String> CARGO = new HashMap<>(); public long lastOp; - + public SatelliteMiner() { this.satIface = Interfaces.NONE; } - + public void writeToNBT(NBTTagCompound nbt) { nbt.setLong("lastOp", lastOp); } - + public void readFromNBT(NBTTagCompound nbt) { lastOp = nbt.getLong("lastOp"); } @@ -42,6 +43,16 @@ public String getCargo() { return CARGO.get(getClass()); } + /** + * Gets the cargo key for the satellite item. If the item is not a miner satellite null is returned. + * @param satelliteItem - Satellite item + * @return - Returns {@link com.hbm.itempool.ItemPool} key or null if the item is not a mining satellite. + */ + public static String getCargoForItem(Item satelliteItem) { + Class satelliteClass = itemToClass.getOrDefault(satelliteItem, null); + return satelliteClass != null ? CARGO.getOrDefault(satelliteClass, null) : null; + } + static { registerCargo(SatelliteMiner.class, ItemPoolsSatellite.POOL_SAT_MINER); }