diff --git a/.gitignore b/.gitignore index 776998c18..22222fc39 100644 --- a/.gitignore +++ b/.gitignore @@ -26,21 +26,3 @@ run # Changelog backup /changelog.bak - -screenshots/ - -saves/ - -usernamecache.json - -options.txt - -logs/ - -doc/ - -crash-reports/ - -config/ - -asm/ diff --git a/src/main/java/com/hbm/config/CustomMachineConfigJSON.java b/src/main/java/com/hbm/config/CustomMachineConfigJSON.java new file mode 100644 index 000000000..750fb8733 --- /dev/null +++ b/src/main/java/com/hbm/config/CustomMachineConfigJSON.java @@ -0,0 +1,197 @@ +package com.hbm.config; + +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import com.google.gson.Gson; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import com.google.gson.stream.JsonWriter; +import com.hbm.config.CustomMachineConfigJSON.MachineConfiguration.ComponentDefinition; +import com.hbm.main.MainRegistry; + +import net.minecraft.block.Block; + +public class CustomMachineConfigJSON { + + public static final Gson gson = new Gson(); + public static HashMap customMachines = new HashMap(); + + public static void initialize() { + File folder = MainRegistry.configHbmDir; + + File config = new File(folder.getAbsolutePath() + File.separatorChar + "hbmCustomMachines.json"); + + if(!config.exists()) { + writeDefault(config); + } + + readConfig(config); + } + + public static void writeDefault(File config) { + + try { + JsonWriter writer = new JsonWriter(new FileWriter(config)); + writer.setIndent(" "); + writer.beginObject(); + writer.name("machines").beginArray(); + + writer.beginObject(); + writer.name("recipeKey").value("paperPress"); + writer.name("unlocalizedName").value("paperPress"); + writer.name("localizedName").value("Paper Press"); + writer.name("fluidInCount").value(1); + writer.name("fluidInCap").value(1_000); + writer.name("itemInCount").value(1); + writer.name("fluidOutCount").value(0); + writer.name("fluidOutCap").value(0); + writer.name("itemOutCount").value(1); + writer.name("generatorMode").value(false); + writer.name("recipeSpeedMult").value(1.0D); + writer.name("recipeConsumptionMult").value(1.0D); + writer.name("maxPower").value(10_000L); + + writer.name("components").beginArray(); + + for(int x = -1; x <= 1; x++) { + for(int y = -1; y <= 1; y++) { + for(int z = 0; z <= 2; z++) { + if(!(x == 0 && y == 0 && z == 1) && !(x == 0 && z == 0)) { + writer.beginObject().setIndent(""); + writer.name("block").value(y == 0 ? "hbm:tile.cm_sheet" : "hbm:tile.cm_block"); + writer.name("x").value(x); + writer.name("y").value(y); + writer.name("z").value(z); + writer.name("metas").beginArray(); + writer.value(0); + writer.endArray(); + writer.endObject().setIndent(" "); + } + } + } + } + + writer.beginObject().setIndent(""); + writer.name("block").value("hbm:tile.cm_port"); + writer.name("x").value(0); + writer.name("y").value(-1); + writer.name("z").value(0); + writer.name("metas").beginArray(); + writer.value(0); + writer.endArray(); + writer.endObject().setIndent(" "); + + writer.beginObject().setIndent(""); + writer.name("block").value("hbm:tile.cm_port"); + writer.name("x").value(0); + writer.name("y").value(1); + writer.name("z").value(0); + writer.name("metas").beginArray(); + writer.value(0); + writer.endArray(); + writer.endObject().setIndent(" "); + + writer.endArray(); + writer.endObject(); + + writer.endArray(); + writer.endObject(); + writer.close(); + } catch(IOException e) { + e.printStackTrace(); + } + } + + public static void readConfig(File config) { + + try { + JsonObject json = gson.fromJson(new FileReader(config), JsonObject.class); + JsonArray machines = json.get("machines").getAsJsonArray(); + + for(int i = 0; i < machines.size(); i++) { + JsonObject machineObject = machines.get(i).getAsJsonObject(); + + MachineConfiguration configuration = new MachineConfiguration(); + configuration.recipeKey = machineObject.get("recipeKey").getAsString(); + configuration.unlocalizedName = machineObject.get("unlocalizedName").getAsString(); + configuration.localizedName = machineObject.get("localizedName").getAsString(); + configuration.fluidInCount = machineObject.get("fluidInCount").getAsInt(); + configuration.fluidInCap = machineObject.get("fluidInCap").getAsInt(); + configuration.itemInCount = machineObject.get("itemInCount").getAsInt(); + configuration.fluidOutCount = machineObject.get("fluidOutCount").getAsInt(); + configuration.fluidOutCap = machineObject.get("fluidOutCap").getAsInt(); + configuration.itemOutCount = machineObject.get("itemOutCount").getAsInt(); + configuration.generatorMode = machineObject.get("generatorMode").getAsBoolean(); + configuration.recipeSpeedMult = machineObject.get("recipeSpeedMult").getAsDouble(); + configuration.recipeConsumptionMult = machineObject.get("recipeConsumptionMult").getAsDouble(); + configuration.maxPower = machineObject.get("maxPower").getAsLong(); + + JsonArray components = machineObject.get("components").getAsJsonArray(); + configuration.components = new ArrayList(); + + for(int j = 0; j < components.size(); j++) { + JsonObject compObject = components.get(j).getAsJsonObject(); + ComponentDefinition compDef = new ComponentDefinition(); + compDef.block = (Block) Block.blockRegistry.getObject(compObject.get("block").getAsString()); + compDef.x = compObject.get("x").getAsInt(); + compDef.y = compObject.get("y").getAsInt(); + compDef.z = compObject.get("z").getAsInt(); + compDef.allowedMetas = new HashSet(); + JsonArray metas = compObject.get("metas").getAsJsonArray(); + for(int k = 0; k < metas.size(); k++) { + compDef.allowedMetas.add(metas.get(k).getAsInt()); + } + + configuration.components.add(compDef); + } + + customMachines.put(configuration.unlocalizedName, configuration); + } + + } catch(Exception ex) { + ex.printStackTrace(); + } + } + + public static class MachineConfiguration { + + /** The name of the recipe set that this machine can handle */ + public String recipeKey; + /** The internal name of this machine */ + public String unlocalizedName; + /** The display name of this machine */ + public String localizedName; + + public int fluidInCount; + public int fluidInCap; + public int itemInCount; + public int fluidOutCount; + public int fluidOutCap; + public int itemOutCount; + /** Whether inputs should be used up when the process begins */ + public boolean generatorMode; + + public double recipeSpeedMult = 1D; + public double recipeConsumptionMult = 1D; + public long maxPower; + + /** Definitions of blocks that this machine is composed of */ + public List components; + + public static class ComponentDefinition { + public Block block; + public Set allowedMetas; + public int x; + public int y; + public int z; + } + } +} diff --git a/src/main/java/com/hbm/tileentity/machine/TileEntityCustomMachine.java b/src/main/java/com/hbm/tileentity/machine/TileEntityCustomMachine.java index 5133d5d8b..700da5e04 100644 --- a/src/main/java/com/hbm/tileentity/machine/TileEntityCustomMachine.java +++ b/src/main/java/com/hbm/tileentity/machine/TileEntityCustomMachine.java @@ -22,6 +22,7 @@ import com.hbm.util.Compat; import com.hbm.util.fauxpointtwelve.BlockPos; import com.hbm.util.fauxpointtwelve.DirPos; +import api.hbm.energy.IEnergyUser; import api.hbm.fluid.IFluidStandardTransceiver; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; @@ -35,7 +36,7 @@ import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; import net.minecraftforge.common.util.ForgeDirection; -public class TileEntityCustomMachine extends TileEntityMachineBase implements IFluidStandardTransceiver, IGUIProvider { +public class TileEntityCustomMachine extends TileEntityMachineBase implements IFluidStandardTransceiver, IEnergyUser, IGUIProvider { public String machineType; public MachineConfiguration config; @@ -110,6 +111,13 @@ public class TileEntityCustomMachine extends TileEntityMachineBase implements IF for(FluidTank tank : this.inputTanks) { this.trySubscribe(tank.getTankType(), worldObj, pos.getX(), pos.getY(), pos.getZ(), pos.getDir()); } + if(!config.generatorMode) this.trySubscribe(worldObj, pos.getX(), pos.getY(), pos.getZ(), pos.getDir()); + } + } + + if(config.generatorMode && power > 0) { + for(DirPos pos : this.connectionPos) { + this.sendPower(worldObj, pos.getX(), pos.getY(), pos.getZ(), pos.getDir()); } } @@ -118,7 +126,7 @@ public class TileEntityCustomMachine extends TileEntityMachineBase implements IF if(config.generatorMode) { if(this.cachedRecipe == null) { CustomMachineRecipe recipe = this.getMatchingRecipe(); - if(this.hasRequiredQuantities(recipe) && this.hasSpace(recipe)) { + if(recipe != null && this.hasRequiredQuantities(recipe) && this.hasSpace(recipe)) { this.cachedRecipe = recipe; this.useUpInput(recipe); } @@ -384,6 +392,11 @@ public class TileEntityCustomMachine extends TileEntityMachineBase implements IF this.matcher.readFromNBT(nbt); } + + int index = nbt.getInteger("cachedIndex"); + if(index != -1) { + this.cachedRecipe = CustomMachineRecipes.recipes.get(this.machineType).get(index); + } } @Override @@ -402,6 +415,13 @@ public class TileEntityCustomMachine extends TileEntityMachineBase implements IF for(int i = 0; i < outputTanks.length; i++) outputTanks[i].writeToNBT(nbt, "o" + i); this.matcher.writeToNBT(nbt); + + if(this.cachedRecipe != null) { + int index = CustomMachineRecipes.recipes.get(this.machineType).indexOf(this.cachedRecipe); + nbt.setInteger("cachedIndex", index); + } else { + nbt.setInteger("cachedIndex", -1); + } } @Override @@ -437,4 +457,42 @@ public class TileEntityCustomMachine extends TileEntityMachineBase implements IF if(this.config == null) return null; return new GUIMachineCustom(player.inventory, this); } + + @Override + public long getPower() { + return this.power; + } + + @Override + public long getMaxPower() { + return this.config != null ? this.getMaxPower() : 1; + } + + @Override + public void setPower(long power) { + this.power = power; + } + + @Override + public long transferPower(long power) { + if(this.config != null && this.config.generatorMode) return power; + + this.setPower(this.getPower() + power); + + if(this.getPower() > this.getMaxPower()) { + + long overshoot = this.getPower() - this.getMaxPower(); + this.setPower(this.getMaxPower()); + return overshoot; + } + + return 0; + } + + @Override + public long getTransferWeight() { + if(this.config != null && this.config.generatorMode) return 0; + + return Math.max(getMaxPower() - getPower(), 0); + } }