Skip to content

Commit

Permalink
Added satellite NEI handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Toshayo committed Mar 3, 2025
1 parent d13d067 commit 228fbf5
Show file tree
Hide file tree
Showing 3 changed files with 204 additions and 3 deletions.
189 changes: 189 additions & 0 deletions src/main/java/com/hbm/handler/nei/SatelliteHandler.java
Original file line number Diff line number Diff line change
@@ -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<ItemStack> 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<PositionedStack> input = new ArrayList<>();
List<PositionedStack> output = new ArrayList<>();
PositionedStack satelliteDock;

public RecipeSet(Object in, List<ItemStack> 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<PositionedStack> getIngredients() {
return getCycledIngredients(cycleticks / 20, input);
}

@Override
public PositionedStack getResult() {
return output.get(0);
}

@Override
public List<PositionedStack> getOtherStacks() {
ArrayList<PositionedStack> stacks = new ArrayList<>(output);
stacks.add(satelliteDock);
return getCycledIngredients(cycleticks / 20, stacks);
}
}

private static HashMap<ItemStack, ItemStack> getRecipeMap() {
HashMap<ItemStack, ItemStack> 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;
}
}
1 change: 1 addition & 0 deletions src/main/java/com/hbm/main/NEIRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public static List<TemplateRecipeHandler> listAllHandlers() {
handlers.add(new CrucibleCastingHandler());
handlers.add(new ToolingHandler());
handlers.add(new ConstructionHandler());
handlers.add(new SatelliteHandler());

//universal boyes
handlers.add(new ZirnoxRecipeHandler());
Expand Down
17 changes: 14 additions & 3 deletions src/main/java/com/hbm/saveddata/satellites/SatelliteMiner.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -13,15 +14,15 @@ public class SatelliteMiner extends Satellite {
private static final HashMap<Class<? extends SatelliteMiner>, 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");
}
Expand All @@ -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<? extends Satellite> satelliteClass = itemToClass.getOrDefault(satelliteItem, null);
return satelliteClass != null ? CARGO.getOrDefault(satelliteClass, null) : null;
}

static {
registerCargo(SatelliteMiner.class, ItemPoolsSatellite.POOL_SAT_MINER);
}
Expand Down

0 comments on commit 228fbf5

Please sign in to comment.