mirror of
https://github.com/MightyPirates/OpenComputers.git
synced 2025-09-25 22:14:09 -04:00
Merge pull request #15 from repo-alt/feature/PatternEditing
AE Pattern editing
This commit is contained in:
commit
36d8bcb936
@ -0,0 +1,47 @@
|
||||
package li.cil.oc.integration.appeng;
|
||||
|
||||
import appeng.helpers.PatternHelper;
|
||||
import li.cil.oc.api.driver.Converter;
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public final class ConverterPattern implements Converter {
|
||||
@Override
|
||||
public void convert(final Object value, final Map<Object, Object> output) {
|
||||
if (value instanceof ItemStack) {
|
||||
ItemStack is = (ItemStack)value;
|
||||
try
|
||||
{
|
||||
PatternHelper p = new PatternHelper( is, null );
|
||||
Map[] inputs = new Map[p.getInputs().length];
|
||||
for (int i = 0; i < p.getInputs().length; ++i)
|
||||
{
|
||||
inputs[i] = new HashMap<Object, Object>();
|
||||
if (p.getInputs()[i] == null)
|
||||
continue;
|
||||
ItemStack input = p.getInputs()[i].getItemStack();
|
||||
inputs[i].put("name", input.getItem().getItemStackDisplayName(input));
|
||||
inputs[i].put("count", input.stackSize);
|
||||
}
|
||||
output.put("inputs", inputs);
|
||||
Map[] results = new Map[p.getInputs().length];
|
||||
for (int i = 0; i < p.getOutputs().length; ++i)
|
||||
{
|
||||
results[i] = new HashMap<Object, Object>();
|
||||
if (p.getOutputs()[i] == null)
|
||||
continue;
|
||||
ItemStack result = p.getOutputs()[i].getItemStack();
|
||||
results[i].put("name", result.getItem().getItemStackDisplayName(result));
|
||||
results[i].put("count", result.stackSize);
|
||||
}
|
||||
output.put("outputs", results);
|
||||
output.put("isCraftable", p.isCraftable());
|
||||
}
|
||||
catch( final Throwable ignored)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -14,6 +14,7 @@ import li.cil.oc.integration.ManagedTileEntityEnvironment
|
||||
import li.cil.oc.util.ExtendedArguments._
|
||||
import li.cil.oc.util.ResultWrapper._
|
||||
import net.minecraft.item.ItemStack
|
||||
import net.minecraft.nbt.{NBTTagCompound, NBTTagList}
|
||||
import net.minecraft.world.World
|
||||
import net.minecraftforge.common.util.ForgeDirection
|
||||
|
||||
@ -40,7 +41,13 @@ object DriverBlockInterface extends DriverSidedTileEntity {
|
||||
def setInterfaceConfiguration(context: Context, args: Arguments): Array[AnyRef] = {
|
||||
val config = tileEntity.getInventoryByName("config")
|
||||
val slot = if (args.isString(0)) 0 else args.optSlot(config, 0, 0)
|
||||
val stack = if (args.count > 1) {
|
||||
config.setInventorySlotContents(slot, getStack(args))
|
||||
context.pause(0.5)
|
||||
result(true)
|
||||
}
|
||||
|
||||
private def getStack(args: Arguments) =
|
||||
if (args.count > 1) {
|
||||
val (address, entry, size) =
|
||||
if (args.isString(0)) (args.checkString(0), args.checkInteger(1), args.optInteger(2, 1))
|
||||
else (args.checkString(1), args.checkInteger(2), args.optInteger(3, 1))
|
||||
@ -60,10 +67,49 @@ object DriverBlockInterface extends DriverSidedTileEntity {
|
||||
}
|
||||
}
|
||||
else null
|
||||
config.setInventorySlotContents(slot, stack)
|
||||
context.pause(0.5)
|
||||
|
||||
@Callback(doc = "function([slot:number]):table -- Get the given pattern in the interface.")
|
||||
def getInterfacePattern(context: Context, args: Arguments): Array[AnyRef] = {
|
||||
val inv = tileEntity.getInventoryByName("patterns")
|
||||
val slot = args.optSlot(inv, 0, 0)
|
||||
val stack = inv.getStackInSlot(slot)
|
||||
result(stack)
|
||||
}
|
||||
|
||||
@Callback(doc = "function(slot:number, database:address, entry:number, size:number, index:number):boolean -- Set the pattern input at the given index.")
|
||||
def setInterfacePatternInput(context: Context, args: Arguments): Array[AnyRef] = {
|
||||
setPatternSlot(context, args, "in")
|
||||
result(true)
|
||||
}
|
||||
@Callback(doc = "function(slot:number, database:address, entry:number, size:number, index:number):boolean -- Set the pattern output at the given index.")
|
||||
def setInterfacePatternOutput(context: Context, args: Arguments): Array[AnyRef] = {
|
||||
setPatternSlot(context, args, "out")
|
||||
result(true)
|
||||
}
|
||||
|
||||
private def setPatternSlot(context: Context, args: Arguments, tag: String) = {
|
||||
val inv = tileEntity.getInventoryByName("patterns")
|
||||
val slot = if (args.isString(0)) 0 else args.optSlot(inv, 0, 0)
|
||||
val stack = getStack(args)
|
||||
var index = args.checkInteger(4)
|
||||
if (index < 1 || index > 512)
|
||||
throw new IllegalArgumentException("Invalid index!")
|
||||
index -= 1
|
||||
val pattern = inv.getStackInSlot(slot)
|
||||
val encodedValue = pattern.getTagCompound
|
||||
if (encodedValue == null)
|
||||
throw new IllegalArgumentException("No pattern here!")
|
||||
val inTag = encodedValue.getTagList(tag, 10)
|
||||
while (inTag.tagCount() <= index)
|
||||
inTag.appendTag(new NBTTagCompound())
|
||||
val nbt = new NBTTagCompound()
|
||||
stack.writeToNBT(nbt)
|
||||
inTag.func_150304_a(index, nbt)
|
||||
encodedValue.setTag(tag, inTag)
|
||||
pattern.setTagCompound(encodedValue)
|
||||
inv.setInventorySlotContents(slot, pattern)
|
||||
context.pause(0.1)
|
||||
}
|
||||
}
|
||||
|
||||
object Provider extends EnvironmentProvider {
|
||||
|
@ -23,6 +23,7 @@ object ModAppEng extends ModProxy {
|
||||
Driver.add(DriverBlockInterface)
|
||||
|
||||
Driver.add(new ConverterCellInventory)
|
||||
Driver.add(new ConverterPattern)
|
||||
|
||||
Driver.add(DriverController.Provider)
|
||||
Driver.add(DriverExportBus.Provider)
|
||||
|
Loading…
x
Reference in New Issue
Block a user