implemented control over energy requirements, for #1664
This commit is contained in:
parent
d2b1e5b8f7
commit
5e3b0126ac
9 changed files with 60 additions and 25 deletions
|
@ -269,4 +269,18 @@ public abstract class Schematic {
|
||||||
public BuildingPermission getBuildingPermission () {
|
public BuildingPermission getBuildingPermission () {
|
||||||
return BuildingPermission.ALL;
|
return BuildingPermission.ALL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the amount of energy required to build this slot, depends on the
|
||||||
|
* stacks selected for the build.
|
||||||
|
*/
|
||||||
|
public double getEnergyRequirement(LinkedList<ItemStack> stacksUsed) {
|
||||||
|
double result = 0;
|
||||||
|
|
||||||
|
for (ItemStack s : stacksUsed) {
|
||||||
|
result += s.stackSize * SchematicRegistry.BUILD_ENERGY;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,6 +22,9 @@ import buildcraft.api.core.JavaTools;
|
||||||
|
|
||||||
public class SchematicRegistry {
|
public class SchematicRegistry {
|
||||||
|
|
||||||
|
public static double BREAK_ENERGY = 10;
|
||||||
|
public static final double BUILD_ENERGY = 20;
|
||||||
|
|
||||||
private static class SchematicConstructor {
|
private static class SchematicConstructor {
|
||||||
Class <? extends SchematicEntity> clas;
|
Class <? extends SchematicEntity> clas;
|
||||||
Object [] params;
|
Object [] params;
|
||||||
|
|
|
@ -14,6 +14,7 @@ import java.util.LinkedList;
|
||||||
import net.minecraft.inventory.IInventory;
|
import net.minecraft.inventory.IInventory;
|
||||||
import net.minecraft.nbt.NBTTagCompound;
|
import net.minecraft.nbt.NBTTagCompound;
|
||||||
import buildcraft.api.blueprints.ITileBuilder;
|
import buildcraft.api.blueprints.ITileBuilder;
|
||||||
|
import buildcraft.api.blueprints.SchematicRegistry;
|
||||||
import buildcraft.api.core.NetworkData;
|
import buildcraft.api.core.NetworkData;
|
||||||
import buildcraft.api.core.SafeTimeTracker;
|
import buildcraft.api.core.SafeTimeTracker;
|
||||||
import buildcraft.api.mj.MjBattery;
|
import buildcraft.api.mj.MjBattery;
|
||||||
|
@ -27,15 +28,12 @@ import buildcraft.core.network.RPCSide;
|
||||||
|
|
||||||
public abstract class TileAbstractBuilder extends TileBuildCraft implements ITileBuilder, IInventory, IBoxProvider {
|
public abstract class TileAbstractBuilder extends TileBuildCraft implements ITileBuilder, IInventory, IBoxProvider {
|
||||||
|
|
||||||
public static double BREAK_ENERGY = 10;
|
|
||||||
public static final double BUILD_ENERGY = 20;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Computes the maximum amount of energy required to build a full chest,
|
* Computes the maximum amount of energy required to build a full chest,
|
||||||
* plus a safeguard. That's a nice way to evaluate maximum amount of energy
|
* plus a safeguard. That's a nice way to evaluate maximum amount of energy
|
||||||
* that need to be in a builder.
|
* that need to be in a builder.
|
||||||
*/
|
*/
|
||||||
private static final double FULL_CHEST_ENERGY = 9 * 3 * 64 * BUILD_ENERGY + 1000;
|
private static final double FULL_CHEST_ENERGY = 9 * 3 * 64 * SchematicRegistry.BUILD_ENERGY + 1000;
|
||||||
|
|
||||||
@MjBattery(maxReceivedPerCycle = 100, maxCapacity = FULL_CHEST_ENERGY, minimumConsumption = 1)
|
@MjBattery(maxReceivedPerCycle = 100, maxCapacity = FULL_CHEST_ENERGY, minimumConsumption = 1)
|
||||||
protected double mjStored = 0;
|
protected double mjStored = 0;
|
||||||
|
|
|
@ -20,6 +20,7 @@ import net.minecraft.world.World;
|
||||||
import net.minecraftforge.common.util.Constants;
|
import net.minecraftforge.common.util.Constants;
|
||||||
import buildcraft.BuildCraftBuilders;
|
import buildcraft.BuildCraftBuilders;
|
||||||
import buildcraft.api.blueprints.IBuilderContext;
|
import buildcraft.api.blueprints.IBuilderContext;
|
||||||
|
import buildcraft.api.blueprints.SchematicRegistry;
|
||||||
import buildcraft.api.core.IAreaProvider;
|
import buildcraft.api.core.IAreaProvider;
|
||||||
import buildcraft.api.core.Position;
|
import buildcraft.api.core.Position;
|
||||||
import buildcraft.builders.BuildingItem;
|
import buildcraft.builders.BuildingItem;
|
||||||
|
@ -149,10 +150,10 @@ public abstract class BptBuilderBase implements IAreaProvider {
|
||||||
.getBlockHardness(context.world(), slot.x, slot.y,
|
.getBlockHardness(context.world(), slot.x, slot.y,
|
||||||
slot.z)+1;
|
slot.z)+1;
|
||||||
|
|
||||||
if (builder.energyAvailable() < hardness * TileAbstractBuilder.BREAK_ENERGY) {
|
if (builder.energyAvailable() < hardness * SchematicRegistry.BREAK_ENERGY) {
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
builder.consumeEnergy(hardness * TileAbstractBuilder.BREAK_ENERGY);
|
builder.consumeEnergy(hardness * SchematicRegistry.BREAK_ENERGY);
|
||||||
|
|
||||||
for (int i = 0; i < hardness; ++i) {
|
for (int i = 0; i < hardness; ++i) {
|
||||||
slot.addStackConsumed(new ItemStack(BuildCraftBuilders.buildToolBlock));
|
slot.addStackConsumed(new ItemStack(BuildCraftBuilders.buildToolBlock));
|
||||||
|
|
|
@ -314,15 +314,12 @@ public class BptBuilderBlueprint extends BptBuilderBase {
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean checkRequirements(TileAbstractBuilder builder, Schematic slot) {
|
public boolean checkRequirements(TileAbstractBuilder builder, Schematic slot) {
|
||||||
double energyRequired = 0;
|
|
||||||
|
|
||||||
LinkedList<ItemStack> tmpReq = new LinkedList<ItemStack>();
|
LinkedList<ItemStack> tmpReq = new LinkedList<ItemStack>();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
for (ItemStack stk : slot.getRequirements(context)) {
|
for (ItemStack stk : slot.getRequirements(context)) {
|
||||||
if (stk != null) {
|
if (stk != null) {
|
||||||
tmpReq.add(stk.copy());
|
tmpReq.add(stk.copy());
|
||||||
energyRequired += stk.stackSize * TileAbstractBuilder.BUILD_ENERGY;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
|
@ -331,25 +328,31 @@ public class BptBuilderBlueprint extends BptBuilderBase {
|
||||||
BCLog.logger.throwing("BptBuilderBlueprint", "checkRequirements", t);
|
BCLog.logger.throwing("BptBuilderBlueprint", "checkRequirements", t);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (builder.energyAvailable() < energyRequired) {
|
LinkedList <ItemStack> stacksUsed = new LinkedList<ItemStack>();
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (context.world().getWorldInfo().getGameType() == GameType.CREATIVE) {
|
if (context.world().getWorldInfo().getGameType() == GameType.CREATIVE) {
|
||||||
return true;
|
for (ItemStack s : tmpReq) {
|
||||||
|
stacksUsed.add(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (builder.energyAvailable() < slot.getEnergyRequirement (stacksUsed)) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (ItemStack reqStk : tmpReq) {
|
for (ItemStack reqStk : tmpReq) {
|
||||||
for (IInvSlot slotInv : InventoryIterator.getIterable(new InventoryCopy(builder), ForgeDirection.UNKNOWN)) {
|
for (IInvSlot slotInv : InventoryIterator.getIterable(new InventoryCopy(builder), ForgeDirection.UNKNOWN)) {
|
||||||
if (!builder.isBuildingMaterialSlot(slotInv.getIndex())) {
|
if (!builder.isBuildingMaterialSlot(slotInv.getIndex())) {
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
ItemStack invStk = slotInv.getStackInSlot();
|
ItemStack invStk = slotInv.getStackInSlot();
|
||||||
|
|
||||||
if (invStk != null && invStk.stackSize > 0 && StackHelper.isCraftingEquivalent(reqStk, invStk, true)) {
|
if (invStk != null && invStk.stackSize > 0 && StackHelper.isCraftingEquivalent(reqStk, invStk, true)) {
|
||||||
try {
|
try {
|
||||||
slot.useItem(context, reqStk, slotInv);
|
stacksUsed.add(slot.useItem(context, reqStk, slotInv));
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
// Defensive code against errors in implementers
|
// Defensive code against errors in implementers
|
||||||
t.printStackTrace();
|
t.printStackTrace();
|
||||||
|
@ -367,19 +370,20 @@ public class BptBuilderBlueprint extends BptBuilderBase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
if (builder.energyAvailable() < slot.getEnergyRequirement (stacksUsed)) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void useRequirements(TileAbstractBuilder builder, BuildingSlot slot) {
|
public void useRequirements(TileAbstractBuilder builder, BuildingSlot slot) {
|
||||||
LinkedList<ItemStack> tmpReq = new LinkedList<ItemStack>();
|
LinkedList<ItemStack> tmpReq = new LinkedList<ItemStack>();
|
||||||
|
|
||||||
double energyRequired = 0;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
for (ItemStack stk : slot.getRequirements(context)) {
|
for (ItemStack stk : slot.getRequirements(context)) {
|
||||||
if (stk != null) {
|
if (stk != null) {
|
||||||
tmpReq.add(stk.copy());
|
tmpReq.add(stk.copy());
|
||||||
energyRequired += stk.stackSize * TileAbstractBuilder.BUILD_ENERGY;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
|
@ -389,13 +393,13 @@ public class BptBuilderBlueprint extends BptBuilderBase {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
builder.consumeEnergy(energyRequired);
|
|
||||||
|
|
||||||
if (context.world ().getWorldInfo().getGameType() == GameType.CREATIVE) {
|
if (context.world ().getWorldInfo().getGameType() == GameType.CREATIVE) {
|
||||||
for (ItemStack s : slot.getRequirements(context)) {
|
for (ItemStack s : tmpReq) {
|
||||||
slot.addStackConsumed(s);
|
slot.addStackConsumed(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
builder.consumeEnergy(slot.getEnergyRequirement());
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -408,7 +412,7 @@ public class BptBuilderBlueprint extends BptBuilderBase {
|
||||||
|
|
||||||
for (IInvSlot slotInv : InventoryIterator.getIterable(builder, ForgeDirection.UNKNOWN)) {
|
for (IInvSlot slotInv : InventoryIterator.getIterable(builder, ForgeDirection.UNKNOWN)) {
|
||||||
if (!builder.isBuildingMaterialSlot(slotInv.getIndex())) {
|
if (!builder.isBuildingMaterialSlot(slotInv.getIndex())) {
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
ItemStack invStk = slotInv.getStackInSlot();
|
ItemStack invStk = slotInv.getStackInSlot();
|
||||||
|
@ -438,6 +442,8 @@ public class BptBuilderBlueprint extends BptBuilderBase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
builder.consumeEnergy(slot.getEnergyRequirement ());
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,6 +14,7 @@ import net.minecraft.item.ItemStack;
|
||||||
import net.minecraft.world.World;
|
import net.minecraft.world.World;
|
||||||
import net.minecraftforge.common.util.ForgeDirection;
|
import net.minecraftforge.common.util.ForgeDirection;
|
||||||
import buildcraft.api.blueprints.SchematicBlockBase;
|
import buildcraft.api.blueprints.SchematicBlockBase;
|
||||||
|
import buildcraft.api.blueprints.SchematicRegistry;
|
||||||
import buildcraft.api.core.BuildCraftAPI;
|
import buildcraft.api.core.BuildCraftAPI;
|
||||||
import buildcraft.api.core.IInvSlot;
|
import buildcraft.api.core.IInvSlot;
|
||||||
import buildcraft.builders.TileAbstractBuilder;
|
import buildcraft.builders.TileAbstractBuilder;
|
||||||
|
@ -170,8 +171,8 @@ public class BptBuilderTemplate extends BptBuilderBase {
|
||||||
iterator.remove();
|
iterator.remove();
|
||||||
builtLocations.add(new BlockIndex(slot.x, slot.y, slot.z));
|
builtLocations.add(new BlockIndex(slot.x, slot.y, slot.z));
|
||||||
} else {
|
} else {
|
||||||
if (builder.energyAvailable() > TileAbstractBuilder.BUILD_ENERGY && firstSlotToConsume != null) {
|
if (builder.energyAvailable() > SchematicRegistry.BUILD_ENERGY && firstSlotToConsume != null) {
|
||||||
builder.consumeEnergy(TileAbstractBuilder.BUILD_ENERGY);
|
builder.consumeEnergy(SchematicRegistry.BUILD_ENERGY);
|
||||||
|
|
||||||
slot.addStackConsumed(firstSlotToConsume
|
slot.addStackConsumed(firstSlotToConsume
|
||||||
.decreaseStackInSlot());
|
.decreaseStackInSlot());
|
||||||
|
|
|
@ -58,4 +58,6 @@ public abstract class BuildingSlot {
|
||||||
public abstract void writeToNBT (NBTTagCompound nbt, MappingRegistry registry);
|
public abstract void writeToNBT (NBTTagCompound nbt, MappingRegistry registry);
|
||||||
|
|
||||||
public abstract void readFromNBT (NBTTagCompound nbt, MappingRegistry registry);
|
public abstract void readFromNBT (NBTTagCompound nbt, MappingRegistry registry);
|
||||||
|
|
||||||
|
public abstract double getEnergyRequirement();
|
||||||
}
|
}
|
||||||
|
|
|
@ -161,4 +161,9 @@ public class BuildingSlotBlock extends BuildingSlot {
|
||||||
return getSchematic ().getStacksToDisplay (stackConsumed);
|
return getSchematic ().getStacksToDisplay (stackConsumed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double getEnergyRequirement() {
|
||||||
|
return schematic.getEnergyRequirement(stackConsumed);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -78,4 +78,9 @@ public class BuildingSlotEntity extends BuildingSlot {
|
||||||
schematic = (SchematicEntity) SchematicFactory
|
schematic = (SchematicEntity) SchematicFactory
|
||||||
.createSchematicFromWorldNBT(nbt.getCompoundTag("schematic"), registry);
|
.createSchematicFromWorldNBT(nbt.getCompoundTag("schematic"), registry);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double getEnergyRequirement() {
|
||||||
|
return schematic.getEnergyRequirement(stackConsumed);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue