mirror of
https://github.com/MightyPirates/OpenComputers.git
synced 2025-09-18 11:48:02 -04:00
Merge pull request #2075 from thiakil/WorldInventoryAnalytics-updates-pr
World Inventory Analytics Improvements closes #2771
This commit is contained in:
parent
aac94d7f22
commit
352aaefbc5
137
src/main/java/li/cil/oc/api/prefab/ItemStackArrayValue.java
Normal file
137
src/main/java/li/cil/oc/api/prefab/ItemStackArrayValue.java
Normal file
@ -0,0 +1,137 @@
|
|||||||
|
package li.cil.oc.api.prefab;
|
||||||
|
|
||||||
|
import li.cil.oc.api.machine.Arguments;
|
||||||
|
import li.cil.oc.api.machine.Callback;
|
||||||
|
import li.cil.oc.api.machine.Context;
|
||||||
|
import net.minecraft.item.ItemStack;
|
||||||
|
import net.minecraft.nbt.NBTBase;
|
||||||
|
import net.minecraft.nbt.NBTTagCompound;
|
||||||
|
import net.minecraft.nbt.NBTTagList;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.TreeMap;
|
||||||
|
|
||||||
|
public class ItemStackArrayValue extends AbstractValue {
|
||||||
|
|
||||||
|
private ItemStack[] array = null;
|
||||||
|
private int iteratorIndex;
|
||||||
|
|
||||||
|
private static final byte TAGLIST_ID = (new NBTTagList()).getId();
|
||||||
|
private static final byte COMPOUND_ID = (new NBTTagCompound()).getId();
|
||||||
|
private static final String ARRAY_KEY = "Array";
|
||||||
|
private static final String INDEX_KEY = "Index";
|
||||||
|
|
||||||
|
private static final HashMap<Object,Object> emptyMap = new HashMap<Object,Object>();
|
||||||
|
|
||||||
|
public ItemStackArrayValue(ItemStack[] arr){
|
||||||
|
if (arr != null){
|
||||||
|
this.array = new ItemStack[arr.length];
|
||||||
|
for (int i=0; i< arr.length; i++){
|
||||||
|
this.array[i] = arr[i] != null ? arr[i].copy() : null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.iteratorIndex = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ItemStackArrayValue(){
|
||||||
|
this(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object[] call(Context context, Arguments arguments) {
|
||||||
|
if (this.array == null)
|
||||||
|
return null;
|
||||||
|
if (this.iteratorIndex >= this.array.length)
|
||||||
|
return null;
|
||||||
|
int index = this.iteratorIndex++;
|
||||||
|
if (this.array[index] == null)//TODO 1.11 change to ItemStack.EMPTY?
|
||||||
|
return new Object[]{ emptyMap };
|
||||||
|
return new Object[]{ this.array[index] != null ? this.array[index] : emptyMap };
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object apply(Context context, Arguments arguments) {
|
||||||
|
if (arguments.count() == 0 || this.array == null)
|
||||||
|
return null;
|
||||||
|
if (arguments.isInteger(0)){//index access
|
||||||
|
int luaIndex = arguments.checkInteger(0);
|
||||||
|
if (luaIndex > this.array.length || luaIndex < 1){
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return this.array[luaIndex-1];
|
||||||
|
}
|
||||||
|
if (arguments.isString(0)){
|
||||||
|
String arg = arguments.checkString(0);
|
||||||
|
if (arg.equals("n")){
|
||||||
|
return this.array.length;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void load(NBTTagCompound nbt) {
|
||||||
|
if (nbt.hasKey(ARRAY_KEY, TAGLIST_ID)){
|
||||||
|
NBTTagList tagList = nbt.getTagList(ARRAY_KEY,COMPOUND_ID);
|
||||||
|
this.array = new ItemStack[tagList.tagCount()];
|
||||||
|
for (int i = 0; i < tagList.tagCount(); ++i){
|
||||||
|
NBTTagCompound el = tagList.getCompoundTagAt(i);
|
||||||
|
if (el.hasNoTags())
|
||||||
|
this.array[i] = null;//TODO 1.11 change to ItemStack.EMPTY?
|
||||||
|
else
|
||||||
|
this.array[i] = ItemStack.loadItemStackFromNBT(el);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this.array = null;
|
||||||
|
}
|
||||||
|
this.iteratorIndex = nbt.getInteger(INDEX_KEY);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void save(NBTTagCompound nbt) {
|
||||||
|
|
||||||
|
NBTTagCompound nullnbt = new NBTTagCompound();
|
||||||
|
|
||||||
|
if (this.array != null) {
|
||||||
|
NBTTagList nbttaglist = new NBTTagList();
|
||||||
|
for (ItemStack stack : this.array) {
|
||||||
|
if (stack != null) {
|
||||||
|
NBTTagCompound nbttagcompound = new NBTTagCompound();
|
||||||
|
stack.writeToNBT(nbttagcompound);
|
||||||
|
nbttaglist.appendTag(nbttagcompound);
|
||||||
|
} else {
|
||||||
|
nbttaglist.appendTag(nullnbt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
nbt.setTag(ARRAY_KEY, nbttaglist);
|
||||||
|
}
|
||||||
|
|
||||||
|
nbt.setInteger(INDEX_KEY, iteratorIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Callback(doc="function():nil -- Reset the iterator index so that the next call will return the first element.")
|
||||||
|
public Object[] reset(Context context, Arguments arguments) throws Exception {
|
||||||
|
this.iteratorIndex = 0;
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Callback(doc="function():number -- Returns the number of elements in the this.array.")
|
||||||
|
public Object[] count(Context context, Arguments arguments) throws Exception {
|
||||||
|
return new Object[] { this.array != null ? this.array.length : 0 };
|
||||||
|
}
|
||||||
|
|
||||||
|
@Callback(doc="function():table -- Returns ALL the stack in the this.array. Memory intensive.")
|
||||||
|
public Object[] getAll(Context context, Arguments arguments) throws Exception {
|
||||||
|
TreeMap<Integer,Object> map = new TreeMap<Integer,Object>();
|
||||||
|
for (int i=0; i<this.array.length; i++){
|
||||||
|
map.put(i, this.array[i] != null ? this.array[i] : emptyMap);
|
||||||
|
}
|
||||||
|
return new Object[] { map };
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString(){
|
||||||
|
return "{ItemStack Array}";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -4,11 +4,14 @@ import li.cil.oc.Settings
|
|||||||
import li.cil.oc.api.machine.Arguments
|
import li.cil.oc.api.machine.Arguments
|
||||||
import li.cil.oc.api.machine.Callback
|
import li.cil.oc.api.machine.Callback
|
||||||
import li.cil.oc.api.machine.Context
|
import li.cil.oc.api.machine.Context
|
||||||
|
import li.cil.oc.api.prefab.ItemStackArrayValue
|
||||||
import li.cil.oc.server.component.result
|
import li.cil.oc.server.component.result
|
||||||
import li.cil.oc.util.DatabaseAccess
|
import li.cil.oc.util.{BlockPosition, DatabaseAccess, InventoryUtils}
|
||||||
|
import li.cil.oc.util.ExtendedWorld._
|
||||||
import li.cil.oc.util.ExtendedArguments._
|
import li.cil.oc.util.ExtendedArguments._
|
||||||
import li.cil.oc.util.InventoryUtils
|
import li.cil.oc.util.InventoryUtils
|
||||||
import net.minecraft.inventory.IInventory
|
import net.minecraft.inventory.IInventory
|
||||||
|
import net.minecraft.block.Block
|
||||||
import net.minecraft.item.ItemStack
|
import net.minecraft.item.ItemStack
|
||||||
import net.minecraftforge.common.util.ForgeDirection
|
import net.minecraftforge.common.util.ForgeDirection
|
||||||
import net.minecraftforge.oredict.OreDictionary
|
import net.minecraftforge.oredict.OreDictionary
|
||||||
@ -76,6 +79,36 @@ trait WorldInventoryAnalytics extends WorldAware with SideRestricted with Networ
|
|||||||
}
|
}
|
||||||
else result(Unit, "not enabled in config")
|
else result(Unit, "not enabled in config")
|
||||||
|
|
||||||
|
@Callback(doc = """function(side:number):userdata -- Get a description of all stacks in the inventory on the specified side of the device.""")
|
||||||
|
def getAllStacks(context: Context, args: Arguments): Array[AnyRef] = if (Settings.get.allowItemStackInspection) {
|
||||||
|
val facing = checkSideForAction(args, 0)
|
||||||
|
withInventory(facing, inventory => {
|
||||||
|
val stacks = new Array[ItemStack](inventory.getSizeInventory)
|
||||||
|
for(i <- 0 until inventory.getSizeInventory){
|
||||||
|
stacks(i) = inventory.getStackInSlot(i)
|
||||||
|
}
|
||||||
|
result(new ItemStackArrayValue(stacks))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
else result(Unit, "not enabled in config")
|
||||||
|
|
||||||
|
@Callback(doc = """function(side:number):string -- Get the the name of the inventory on the specified side of the device.""")
|
||||||
|
def getInventoryName(context: Context, args: Arguments): Array[AnyRef] = if (Settings.get.allowItemStackInspection) {
|
||||||
|
val facing = checkSideForAction(args, 0)
|
||||||
|
def blockAt(position: BlockPosition): Option[Block] = position.world match {
|
||||||
|
case Some(world) if world.blockExists(position) => world.getBlock(position) match {
|
||||||
|
case block: Block => Some(block)
|
||||||
|
case _ => None
|
||||||
|
}
|
||||||
|
case _ => None
|
||||||
|
}
|
||||||
|
withInventory(facing, inventory => blockAt(position.offset(facing)) match {
|
||||||
|
case Some(block) => result(block.getUnlocalizedName)
|
||||||
|
case _ => result(Unit, "Unknown")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
else result(Unit, "not enabled in config")
|
||||||
|
|
||||||
@Callback(doc = """function(side:number, slot:number, dbAddress:string, dbSlot:number):boolean -- Store an item stack description in the specified slot of the database with the specified address.""")
|
@Callback(doc = """function(side:number, slot:number, dbAddress:string, dbSlot:number):boolean -- Store an item stack description in the specified slot of the database with the specified address.""")
|
||||||
def store(context: Context, args: Arguments): Array[AnyRef] = {
|
def store(context: Context, args: Arguments): Array[AnyRef] = {
|
||||||
val facing = checkSideForAction(args, 0)
|
val facing = checkSideForAction(args, 0)
|
||||||
|
@ -8,6 +8,7 @@ import java.io.DataOutputStream
|
|||||||
import li.cil.oc.OpenComputers
|
import li.cil.oc.OpenComputers
|
||||||
import li.cil.oc.api.Persistable
|
import li.cil.oc.api.Persistable
|
||||||
import li.cil.oc.api.machine.Value
|
import li.cil.oc.api.machine.Value
|
||||||
|
import li.cil.oc.server.driver.Registry
|
||||||
import li.cil.oc.server.machine.ArgumentsImpl
|
import li.cil.oc.server.machine.ArgumentsImpl
|
||||||
import li.cil.oc.util.ExtendedLuaState.extendLuaState
|
import li.cil.oc.util.ExtendedLuaState.extendLuaState
|
||||||
import net.minecraft.nbt.CompressedStreamTools
|
import net.minecraft.nbt.CompressedStreamTools
|
||||||
@ -56,7 +57,7 @@ class UserdataAPI(owner: NativeLuaArchitecture) extends NativeLuaAPI(owner) {
|
|||||||
lua.pushScalaFunction(lua => {
|
lua.pushScalaFunction(lua => {
|
||||||
val value = lua.toJavaObjectRaw(1).asInstanceOf[Value]
|
val value = lua.toJavaObjectRaw(1).asInstanceOf[Value]
|
||||||
val args = lua.toSimpleJavaObjects(2)
|
val args = lua.toSimpleJavaObjects(2)
|
||||||
owner.invoke(() => Array(value.apply(machine, new ArgumentsImpl(args))))
|
owner.invoke(() => Registry.convert(Array(value.apply(machine, new ArgumentsImpl(args)))))
|
||||||
})
|
})
|
||||||
lua.setField(-2, "apply")
|
lua.setField(-2, "apply")
|
||||||
|
|
||||||
@ -73,7 +74,7 @@ class UserdataAPI(owner: NativeLuaArchitecture) extends NativeLuaAPI(owner) {
|
|||||||
lua.pushScalaFunction(lua => {
|
lua.pushScalaFunction(lua => {
|
||||||
val value = lua.toJavaObjectRaw(1).asInstanceOf[Value]
|
val value = lua.toJavaObjectRaw(1).asInstanceOf[Value]
|
||||||
val args = lua.toSimpleJavaObjects(2)
|
val args = lua.toSimpleJavaObjects(2)
|
||||||
owner.invoke(() => value.call(machine, new ArgumentsImpl(args)))
|
owner.invoke(() => Registry.convert(value.call(machine, new ArgumentsImpl(args))))
|
||||||
})
|
})
|
||||||
lua.setField(-2, "call")
|
lua.setField(-2, "call")
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@ package li.cil.oc.server.machine.luaj
|
|||||||
|
|
||||||
import li.cil.oc.OpenComputers
|
import li.cil.oc.OpenComputers
|
||||||
import li.cil.oc.api.machine.Value
|
import li.cil.oc.api.machine.Value
|
||||||
|
import li.cil.oc.server.driver.Registry
|
||||||
import li.cil.oc.server.machine.ArgumentsImpl
|
import li.cil.oc.server.machine.ArgumentsImpl
|
||||||
import li.cil.oc.util.ScalaClosure._
|
import li.cil.oc.util.ScalaClosure._
|
||||||
import li.cil.repack.org.luaj.vm2.LuaValue
|
import li.cil.repack.org.luaj.vm2.LuaValue
|
||||||
@ -16,7 +17,7 @@ class UserdataAPI(owner: LuaJLuaArchitecture) extends LuaJAPI(owner) {
|
|||||||
userdata.set("apply", (args: Varargs) => {
|
userdata.set("apply", (args: Varargs) => {
|
||||||
val value = args.checkuserdata(1, classOf[Value]).asInstanceOf[Value]
|
val value = args.checkuserdata(1, classOf[Value]).asInstanceOf[Value]
|
||||||
val params = toSimpleJavaObjects(args, 2)
|
val params = toSimpleJavaObjects(args, 2)
|
||||||
owner.invoke(() => Array(value.apply(machine, new ArgumentsImpl(params))))
|
owner.invoke(() => Registry.convert(Array(value.apply(machine, new ArgumentsImpl(params)))))
|
||||||
})
|
})
|
||||||
|
|
||||||
userdata.set("unapply", (args: Varargs) => {
|
userdata.set("unapply", (args: Varargs) => {
|
||||||
@ -31,7 +32,7 @@ class UserdataAPI(owner: LuaJLuaArchitecture) extends LuaJAPI(owner) {
|
|||||||
userdata.set("call", (args: Varargs) => {
|
userdata.set("call", (args: Varargs) => {
|
||||||
val value = args.checkuserdata(1, classOf[Value]).asInstanceOf[Value]
|
val value = args.checkuserdata(1, classOf[Value]).asInstanceOf[Value]
|
||||||
val params = toSimpleJavaObjects(args, 2)
|
val params = toSimpleJavaObjects(args, 2)
|
||||||
owner.invoke(() => value.call(machine, new ArgumentsImpl(params)))
|
owner.invoke(() => Registry.convert(value.call(machine, new ArgumentsImpl(params))))
|
||||||
})
|
})
|
||||||
|
|
||||||
userdata.set("dispose", (args: Varargs) => {
|
userdata.set("dispose", (args: Varargs) => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user