Skip to content

Commit

Permalink
Add support for custom alloy recipes.
Browse files Browse the repository at this point in the history
Fix retriever render issues.
  • Loading branch information
darkevilmac committed Sep 9, 2017
1 parent e978a24 commit 040ee21
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ public TileRetrieverRender() {
public void render(TileRetriever te, double x, double y, double z, float partialTicks, int destroyStage, float alpha) {
super.render(te, x, y, z, partialTicks, destroyStage, alpha);
IBlockState blockState = te.getWorld().getBlockState(te.getPos());
if (blockState.getBlock() != TeckleObjects.blockSortingMachine || !te.isLit() && !te.isJammed())
if (blockState.getBlock() != TeckleObjects.blockRetriever || !te.isLit() && !te.isJammed())
return;

EnumFacing pointTo = blockState.getValue(BlockSortingMachine.FACING);
EnumFacing pointTo = blockState.getValue(BlockRetriever.FACING);
if (mouthOverlay == null || blinkenLightsOverlay == null) {
mouthOverlay = new ModelMachineOverlay(this.getClass(), "teckle:blocks/retrievermouth", false);
blinkenLightsOverlay = new ModelMachineOverlay(this.getClass(), "teckle:blocks/retrieverblinkenlights", true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

public class TeckleConfiguration extends ConcreteConfig {

public File configFolder;

@ConfigValue(type = Property.Type.INTEGER, category = "oregen")
public int nikoliteCount = 8, nikoliteSize = 8, nikoliteMinHeight = 0, nikoliteMaxHeight = 16;

Expand All @@ -26,5 +28,6 @@ public class TeckleConfiguration extends ConcreteConfig {

protected TeckleConfiguration(File configFile, String modID) {
super(configFile, modID);
this.configFolder = configFile.getParentFile();
}
}
12 changes: 11 additions & 1 deletion src/main/java/com/elytradev/teckle/common/TeckleMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.registry.GameRegistry;

import java.io.File;

import static com.elytradev.teckle.common.TeckleMod.*;

@Mod(modid = MOD_ID, name = MOD_NAME, version = MOD_VER, guiFactory = GUI_FACTORY)
Expand Down Expand Up @@ -67,8 +69,16 @@ public class TeckleMod {
public void onPreInit(FMLPreInitializationEvent e) {
PROXY.registerHandlers();
LOG = new TeckleLog(e.getModLog());
CONFIG = new TeckleConfiguration(e.getSuggestedConfigurationFile(), MOD_ID);

//Move config file if it exists.
File teckleFolder = new File(e.getModConfigurationDirectory(), "teckle");
teckleFolder.mkdirs();
if (e.getSuggestedConfigurationFile().exists()) {
e.getSuggestedConfigurationFile().renameTo(new File(teckleFolder, "teckle.cfg"));
}
CONFIG = new TeckleConfiguration(new File(teckleFolder, "teckle.cfg"), MOD_ID);
CONFIG.loadConfig();

MinecraftForge.EVENT_BUS.register(OBJECTS);
OBJECTS.preInit(e);
CapabilityWorldNetworkTile.register();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,21 @@
import com.elytradev.teckle.common.TeckleObjects;
import com.elytradev.teckle.common.item.ItemIngot;
import com.elytradev.teckle.common.item.ItemSiliconWafer;
import com.google.common.base.Charsets;
import com.google.common.collect.Lists;
import com.google.common.io.Resources;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.FurnaceRecipes;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.Tuple;
import net.minecraftforge.oredict.OreDictionary;

import java.io.File;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -98,12 +108,98 @@ public void init() {
// Adds all the vanilla recipes to the alloy furnace.
if (TeckleMod.CONFIG.importFurnaceRecipes)
recipes.addAll(FurnaceRecipes.instance().getSmeltingList().entrySet().stream().map(this::convertFurnaceRecipe).collect(Collectors.toList()));

File recipeFolder = new File(TeckleMod.CONFIG.configFolder, "alloyrecipes");
recipeFolder.mkdirs();

for (File file : recipeFolder.listFiles()) {
if (file.isFile() && !file.isHidden() && !file.getName().startsWith("_")
&& file.getName().toLowerCase().endsWith(".json")) {
RecipeData data = null;
try {
data = new Gson().fromJson(Resources.toString(file.toURI().toURL(), Charsets.UTF_8), RecipeData.class);
} catch (Exception e) {
TeckleMod.LOG.error("Failed to load alloy recipe. File {}", file.toString());
}
if (data != null) {
List<Tuple<Object, Integer>> inputs = Lists.newArrayList();
boolean failed = false;
for (int i = 0; i < data.inputs.length; i++) {
String input = data.inputs[i];
int inputMeta = i < data.inputsMeta.length ? data.inputsMeta[i] : 0;
int inputCount = i < data.inputsCount.length ? data.inputsCount[i] : 1;
if (input.contains(":")) {
// Normal registry name.
Item item = Item.REGISTRY.getObject(new ResourceLocation(input));
inputs.add(new Tuple<>(new ItemStack(item, inputCount, inputMeta), null));
} else if (OreDictionary.doesOreNameExist(input)) {
// Oredict name.
inputs.add(new Tuple<>(input, inputCount));
} else {
TeckleMod.LOG.error("Failed to load alloy recipe, invalid input data." +
" Name:{}, Meta:{}, Count:{}", input, inputMeta, inputCount);
failed = true;
break;
}
}
if (failed)
continue;

ItemStack output = null;
if (data.output.contains(":")) {
// Normal registry name.
Item item = Item.REGISTRY.getObject(new ResourceLocation(data.output));
output = new ItemStack(item, data.outputCount, data.outputMeta);
} else {
TeckleMod.LOG.error("Failed to load alloy recipe, invalid output data." +
" Name:{}, Meta:{}, Count:{}", data.output, data.outputMeta, data.outputCount);
continue;
}

Tuple<Object, Integer>[] inputsArray = new Tuple[inputs.size()];
inputsArray = inputs.toArray(inputsArray);
AlloyRecipe loadedRecipe = new AlloyRecipe(output, inputsArray);
recipes.add(loadedRecipe);
}
}
}
plantExampleRecipe(recipeFolder);
}


private void plantExampleRecipe(File alloyRecipeFolder) {
// Writes a small demo recipe with an underscore in the name so it doesn't load.
// TODO: HJSON support
RecipeData testData = new RecipeData();
testData.inputs = new String[]{"ingotIron", "ingotGold"};
testData.inputsCount = new int[]{1, 1};
testData.inputsMeta = new int[]{0, 0};
testData.output = Items.BLAZE_ROD.getRegistryName().toString();
try {
File demoRecipe = new File(alloyRecipeFolder, "_demorecipe.json");
if (!demoRecipe.exists()) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
Files.write(demoRecipe.toPath(), gson.toJson(testData, RecipeData.class).getBytes());
}
} catch (Exception e) {
// Not that big of a problem...
}
}

private AlloyRecipe convertFurnaceRecipe(Map.Entry<ItemStack, ItemStack> furnaceRecipe) {
return new AlloyRecipe(furnaceRecipe.getValue(), new Tuple<>(furnaceRecipe.getKey(), null));
}

public class RecipeData {
private String output = "";
private int outputMeta = 0;
private int outputCount = 1;

private String[] inputs;
private int[] inputsMeta;
private int[] inputsCount;
}

public void clear() {
recipes.clear();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ public void setOtherTile(NetworkTileRetrieverBase otherTile) {
this.otherTile = otherTile;
}

public void setTriggered() {
if (getWorld().isBlockLoaded(getPos())) {
((TileRetriever) getTileEntity()).setTriggered();
}
}

public NetworkTileRetrieverOutput getOutputTile() {
if (otherTile == null)
findOtherTile();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ public ItemStack acceptTraveller(WorldNetworkTraveller traveller) {
new ItemStack(traveller.data.getCompoundTag("stack")), additionalData, false, false);
if (!remainder.isEmpty())
remainder = bufferData.getHandler().insertItem(remainder, false);
setTriggered();
return remainder;
} else {
return new ItemStack(traveller.data.getCompoundTag("stack"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,6 @@ public EnumFacing getFacing() {
public void readFromNBT(NBTTagCompound tag) {
super.readFromNBT(tag);
this.cachedFace = EnumFacing.values()[tag.getInteger("cachedFace")];
this.outputTile.setColour(tag.getInteger("colour") < 0 ? null
: EnumDyeColor.byMetadata(tag.getInteger("colour")));

if (FMLCommonHandler.instance().getEffectiveSide().isServer()) {
int dimID = tag.getInteger("databaseID");
Expand All @@ -158,6 +156,10 @@ public void readFromNBT(NBTTagCompound tag) {
loadNetworkTile(tag, "outputTileID", getFacing(), NetworkTileRetrieverOutput.class);
}
}

if (this.outputTile != null)
this.outputTile.setColour(tag.getInteger("colour") < 0 ? null
: EnumDyeColor.byMetadata(tag.getInteger("colour")));
}

protected boolean loadNetworkTile(NBTTagCompound tag, String tileIDKey, EnumFacing tileFace, Class<? extends WorldNetworkTile> tileType) {
Expand Down

0 comments on commit 040ee21

Please sign in to comment.