Skip to content

Commit 352aaef

Browse files
committed
Merge pull request #2075 from thiakil/WorldInventoryAnalytics-updates-pr
World Inventory Analytics Improvements closes #2771
1 parent aac94d7 commit 352aaef

File tree

4 files changed

+177
-5
lines changed

4 files changed

+177
-5
lines changed
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
package li.cil.oc.api.prefab;
2+
3+
import li.cil.oc.api.machine.Arguments;
4+
import li.cil.oc.api.machine.Callback;
5+
import li.cil.oc.api.machine.Context;
6+
import net.minecraft.item.ItemStack;
7+
import net.minecraft.nbt.NBTBase;
8+
import net.minecraft.nbt.NBTTagCompound;
9+
import net.minecraft.nbt.NBTTagList;
10+
11+
import java.util.HashMap;
12+
import java.util.TreeMap;
13+
14+
public class ItemStackArrayValue extends AbstractValue {
15+
16+
private ItemStack[] array = null;
17+
private int iteratorIndex;
18+
19+
private static final byte TAGLIST_ID = (new NBTTagList()).getId();
20+
private static final byte COMPOUND_ID = (new NBTTagCompound()).getId();
21+
private static final String ARRAY_KEY = "Array";
22+
private static final String INDEX_KEY = "Index";
23+
24+
private static final HashMap<Object,Object> emptyMap = new HashMap<Object,Object>();
25+
26+
public ItemStackArrayValue(ItemStack[] arr){
27+
if (arr != null){
28+
this.array = new ItemStack[arr.length];
29+
for (int i=0; i< arr.length; i++){
30+
this.array[i] = arr[i] != null ? arr[i].copy() : null;
31+
}
32+
}
33+
this.iteratorIndex = 0;
34+
}
35+
36+
public ItemStackArrayValue(){
37+
this(null);
38+
}
39+
40+
@Override
41+
public Object[] call(Context context, Arguments arguments) {
42+
if (this.array == null)
43+
return null;
44+
if (this.iteratorIndex >= this.array.length)
45+
return null;
46+
int index = this.iteratorIndex++;
47+
if (this.array[index] == null)//TODO 1.11 change to ItemStack.EMPTY?
48+
return new Object[]{ emptyMap };
49+
return new Object[]{ this.array[index] != null ? this.array[index] : emptyMap };
50+
}
51+
52+
@Override
53+
public Object apply(Context context, Arguments arguments) {
54+
if (arguments.count() == 0 || this.array == null)
55+
return null;
56+
if (arguments.isInteger(0)){//index access
57+
int luaIndex = arguments.checkInteger(0);
58+
if (luaIndex > this.array.length || luaIndex < 1){
59+
return null;
60+
}
61+
return this.array[luaIndex-1];
62+
}
63+
if (arguments.isString(0)){
64+
String arg = arguments.checkString(0);
65+
if (arg.equals("n")){
66+
return this.array.length;
67+
}
68+
}
69+
return null;
70+
}
71+
72+
@Override
73+
public void load(NBTTagCompound nbt) {
74+
if (nbt.hasKey(ARRAY_KEY, TAGLIST_ID)){
75+
NBTTagList tagList = nbt.getTagList(ARRAY_KEY,COMPOUND_ID);
76+
this.array = new ItemStack[tagList.tagCount()];
77+
for (int i = 0; i < tagList.tagCount(); ++i){
78+
NBTTagCompound el = tagList.getCompoundTagAt(i);
79+
if (el.hasNoTags())
80+
this.array[i] = null;//TODO 1.11 change to ItemStack.EMPTY?
81+
else
82+
this.array[i] = ItemStack.loadItemStackFromNBT(el);
83+
}
84+
} else {
85+
this.array = null;
86+
}
87+
this.iteratorIndex = nbt.getInteger(INDEX_KEY);
88+
}
89+
90+
@Override
91+
public void save(NBTTagCompound nbt) {
92+
93+
NBTTagCompound nullnbt = new NBTTagCompound();
94+
95+
if (this.array != null) {
96+
NBTTagList nbttaglist = new NBTTagList();
97+
for (ItemStack stack : this.array) {
98+
if (stack != null) {
99+
NBTTagCompound nbttagcompound = new NBTTagCompound();
100+
stack.writeToNBT(nbttagcompound);
101+
nbttaglist.appendTag(nbttagcompound);
102+
} else {
103+
nbttaglist.appendTag(nullnbt);
104+
}
105+
}
106+
107+
nbt.setTag(ARRAY_KEY, nbttaglist);
108+
}
109+
110+
nbt.setInteger(INDEX_KEY, iteratorIndex);
111+
}
112+
113+
@Callback(doc="function():nil -- Reset the iterator index so that the next call will return the first element.")
114+
public Object[] reset(Context context, Arguments arguments) throws Exception {
115+
this.iteratorIndex = 0;
116+
return null;
117+
}
118+
119+
@Callback(doc="function():number -- Returns the number of elements in the this.array.")
120+
public Object[] count(Context context, Arguments arguments) throws Exception {
121+
return new Object[] { this.array != null ? this.array.length : 0 };
122+
}
123+
124+
@Callback(doc="function():table -- Returns ALL the stack in the this.array. Memory intensive.")
125+
public Object[] getAll(Context context, Arguments arguments) throws Exception {
126+
TreeMap<Integer,Object> map = new TreeMap<Integer,Object>();
127+
for (int i=0; i<this.array.length; i++){
128+
map.put(i, this.array[i] != null ? this.array[i] : emptyMap);
129+
}
130+
return new Object[] { map };
131+
}
132+
133+
public String toString(){
134+
return "{ItemStack Array}";
135+
}
136+
}
137+

src/main/scala/li/cil/oc/server/component/traits/WorldInventoryAnalytics.scala

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ import li.cil.oc.Settings
44
import li.cil.oc.api.machine.Arguments
55
import li.cil.oc.api.machine.Callback
66
import li.cil.oc.api.machine.Context
7+
import li.cil.oc.api.prefab.ItemStackArrayValue
78
import li.cil.oc.server.component.result
8-
import li.cil.oc.util.DatabaseAccess
9+
import li.cil.oc.util.{BlockPosition, DatabaseAccess, InventoryUtils}
10+
import li.cil.oc.util.ExtendedWorld._
911
import li.cil.oc.util.ExtendedArguments._
1012
import li.cil.oc.util.InventoryUtils
1113
import net.minecraft.inventory.IInventory
14+
import net.minecraft.block.Block
1215
import net.minecraft.item.ItemStack
1316
import net.minecraftforge.common.util.ForgeDirection
1417
import net.minecraftforge.oredict.OreDictionary
@@ -76,6 +79,36 @@ trait WorldInventoryAnalytics extends WorldAware with SideRestricted with Networ
7679
}
7780
else result(Unit, "not enabled in config")
7881

82+
@Callback(doc = """function(side:number):userdata -- Get a description of all stacks in the inventory on the specified side of the device.""")
83+
def getAllStacks(context: Context, args: Arguments): Array[AnyRef] = if (Settings.get.allowItemStackInspection) {
84+
val facing = checkSideForAction(args, 0)
85+
withInventory(facing, inventory => {
86+
val stacks = new Array[ItemStack](inventory.getSizeInventory)
87+
for(i <- 0 until inventory.getSizeInventory){
88+
stacks(i) = inventory.getStackInSlot(i)
89+
}
90+
result(new ItemStackArrayValue(stacks))
91+
})
92+
}
93+
else result(Unit, "not enabled in config")
94+
95+
@Callback(doc = """function(side:number):string -- Get the the name of the inventory on the specified side of the device.""")
96+
def getInventoryName(context: Context, args: Arguments): Array[AnyRef] = if (Settings.get.allowItemStackInspection) {
97+
val facing = checkSideForAction(args, 0)
98+
def blockAt(position: BlockPosition): Option[Block] = position.world match {
99+
case Some(world) if world.blockExists(position) => world.getBlock(position) match {
100+
case block: Block => Some(block)
101+
case _ => None
102+
}
103+
case _ => None
104+
}
105+
withInventory(facing, inventory => blockAt(position.offset(facing)) match {
106+
case Some(block) => result(block.getUnlocalizedName)
107+
case _ => result(Unit, "Unknown")
108+
})
109+
}
110+
else result(Unit, "not enabled in config")
111+
79112
@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.""")
80113
def store(context: Context, args: Arguments): Array[AnyRef] = {
81114
val facing = checkSideForAction(args, 0)

src/main/scala/li/cil/oc/server/machine/luac/UserdataAPI.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import java.io.DataOutputStream
88
import li.cil.oc.OpenComputers
99
import li.cil.oc.api.Persistable
1010
import li.cil.oc.api.machine.Value
11+
import li.cil.oc.server.driver.Registry
1112
import li.cil.oc.server.machine.ArgumentsImpl
1213
import li.cil.oc.util.ExtendedLuaState.extendLuaState
1314
import net.minecraft.nbt.CompressedStreamTools
@@ -56,7 +57,7 @@ class UserdataAPI(owner: NativeLuaArchitecture) extends NativeLuaAPI(owner) {
5657
lua.pushScalaFunction(lua => {
5758
val value = lua.toJavaObjectRaw(1).asInstanceOf[Value]
5859
val args = lua.toSimpleJavaObjects(2)
59-
owner.invoke(() => Array(value.apply(machine, new ArgumentsImpl(args))))
60+
owner.invoke(() => Registry.convert(Array(value.apply(machine, new ArgumentsImpl(args)))))
6061
})
6162
lua.setField(-2, "apply")
6263

@@ -73,7 +74,7 @@ class UserdataAPI(owner: NativeLuaArchitecture) extends NativeLuaAPI(owner) {
7374
lua.pushScalaFunction(lua => {
7475
val value = lua.toJavaObjectRaw(1).asInstanceOf[Value]
7576
val args = lua.toSimpleJavaObjects(2)
76-
owner.invoke(() => value.call(machine, new ArgumentsImpl(args)))
77+
owner.invoke(() => Registry.convert(value.call(machine, new ArgumentsImpl(args))))
7778
})
7879
lua.setField(-2, "call")
7980

src/main/scala/li/cil/oc/server/machine/luaj/UserdataAPI.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package li.cil.oc.server.machine.luaj
22

33
import li.cil.oc.OpenComputers
44
import li.cil.oc.api.machine.Value
5+
import li.cil.oc.server.driver.Registry
56
import li.cil.oc.server.machine.ArgumentsImpl
67
import li.cil.oc.util.ScalaClosure._
78
import li.cil.repack.org.luaj.vm2.LuaValue
@@ -16,7 +17,7 @@ class UserdataAPI(owner: LuaJLuaArchitecture) extends LuaJAPI(owner) {
1617
userdata.set("apply", (args: Varargs) => {
1718
val value = args.checkuserdata(1, classOf[Value]).asInstanceOf[Value]
1819
val params = toSimpleJavaObjects(args, 2)
19-
owner.invoke(() => Array(value.apply(machine, new ArgumentsImpl(params))))
20+
owner.invoke(() => Registry.convert(Array(value.apply(machine, new ArgumentsImpl(params)))))
2021
})
2122

2223
userdata.set("unapply", (args: Varargs) => {
@@ -31,7 +32,7 @@ class UserdataAPI(owner: LuaJLuaArchitecture) extends LuaJAPI(owner) {
3132
userdata.set("call", (args: Varargs) => {
3233
val value = args.checkuserdata(1, classOf[Value]).asInstanceOf[Value]
3334
val params = toSimpleJavaObjects(args, 2)
34-
owner.invoke(() => value.call(machine, new ArgumentsImpl(params)))
35+
owner.invoke(() => Registry.convert(value.call(machine, new ArgumentsImpl(params))))
3536
})
3637

3738
userdata.set("dispose", (args: Varargs) => {

0 commit comments

Comments
 (0)