This commit is contained in:
gnembon committed 2019-06-29 03:29:29 -03:00
1 parent e6158c1aab
commit b79b942819
3 files changed
+20 -6

No files matched your search

@@ -873,6 +873,15 @@ public class CarpetExpression
public void API_InventoryManipulation() public void API_InventoryManipulation()
{ {
this.expr.addLazyFunction("inventory_size", -1, (c, t, lv) -> {
CarpetContext cc = (CarpetContext) c;
NBTSerializableValue.InventoryLocator inventoryLocator = NBTSerializableValue.locateInventory(cc, lv, 0);
if (inventoryLocator == null)
return (_c, _t) -> Value.NULL;
Value res = new NumericValue(inventoryLocator.inventory.getInvSize());
return (_c, _t) -> res;
});
//inventory_get(<b, e>, <n>) -> item_triple //inventory_get(<b, e>, <n>) -> item_triple
this.expr.addLazyFunction("inventory_get", -1, (c, t, lv) -> { this.expr.addLazyFunction("inventory_get", -1, (c, t, lv) -> {
CarpetContext cc = (CarpetContext) c; CarpetContext cc = (CarpetContext) c;
@@ -890,8 +899,9 @@ public class CarpetExpression
return (_c, _t) -> res; return (_c, _t) -> res;
} }
int slot = (int)NumericValue.asNumber(lv.get(inventoryLocator.offset).evalValue(c)).getLong(); int slot = (int)NumericValue.asNumber(lv.get(inventoryLocator.offset).evalValue(c)).getLong();
if (slot < 0 || slot > inventoryLocator.inventory.getInvSize()) slot = NBTSerializableValue.validateSlot(slot, inventoryLocator.inventory);
throw new InternalExpressionException("Inventory has "+inventoryLocator.inventory.getInvSize()+" slots available"); if (slot == inventoryLocator.inventory.getInvSize())
return (_c, _t) -> Value.NULL;
Value res = ListValue.fromItemStack(inventoryLocator.inventory.getInvStack(slot)); Value res = ListValue.fromItemStack(inventoryLocator.inventory.getInvStack(slot));
return (_c, _t) -> res; return (_c, _t) -> res;
}); });
@@ -905,8 +915,9 @@ public class CarpetExpression
if (lv.size() == inventoryLocator.offset) if (lv.size() == inventoryLocator.offset)
throw new InternalExpressionException("slot number is required for inventory_drop"); throw new InternalExpressionException("slot number is required for inventory_drop");
int slot = (int)NumericValue.asNumber(lv.get(inventoryLocator.offset).evalValue(c)).getLong(); int slot = (int)NumericValue.asNumber(lv.get(inventoryLocator.offset).evalValue(c)).getLong();
if (slot < 0 || slot > inventoryLocator.inventory.getInvSize()) slot = NBTSerializableValue.validateSlot(slot, inventoryLocator.inventory);
throw new InternalExpressionException("Inventory has "+inventoryLocator.inventory.getInvSize()+" slots available"); if (slot == inventoryLocator.inventory.getInvSize())
return (_c, _t) -> Value.NULL;
int amount = 0; int amount = 0;
if (lv.size() > inventoryLocator.offset+1) if (lv.size() > inventoryLocator.offset+1)
amount = (int)NumericValue.asNumber(lv.get(inventoryLocator.offset+1).evalValue(c)).getLong(); amount = (int)NumericValue.asNumber(lv.get(inventoryLocator.offset+1).evalValue(c)).getLong();
@@ -989,6 +1000,8 @@ public class CarpetExpression
throw new InternalExpressionException("inventory_set requires at least slot number and new stack size, and optional new item"); throw new InternalExpressionException("inventory_set requires at least slot number and new stack size, and optional new item");
int slot = (int) NumericValue.asNumber(lv.get(inventoryLocator.offset+0).evalValue(c)).getLong(); int slot = (int) NumericValue.asNumber(lv.get(inventoryLocator.offset+0).evalValue(c)).getLong();
slot = NBTSerializableValue.validateSlot(slot, inventoryLocator.inventory); slot = NBTSerializableValue.validateSlot(slot, inventoryLocator.inventory);
if (slot == inventoryLocator.inventory.getInvSize())
return (_c, _t) -> Value.NULL;
int count = (int) NumericValue.asNumber(lv.get(inventoryLocator.offset+1).evalValue(c)).getLong(); int count = (int) NumericValue.asNumber(lv.get(inventoryLocator.offset+1).evalValue(c)).getLong();
if (count == 0) if (count == 0)
{ {
+2 -1
View File
@@ -2242,7 +2242,7 @@ public class Expression implements Cloneable
* number('2')+number('2') =&gt; 4 * number('2')+number('2') =&gt; 4
* </pre> * </pre>
* *
* <h3><code>str(expr, params? ... )</code></h3> * <h3><code>str(expr, params? ... ), str(expr, param_list)</code></h3>
* *
* <p>Returns a formatted string representing expression. * <p>Returns a formatted string representing expression.
* Accepts formatting style accepted by <code>String.format</code>. * Accepts formatting style accepted by <code>String.format</code>.
@@ -2265,6 +2265,7 @@ public class Expression implements Cloneable
* str('3bar') =&gt; null * str('3bar') =&gt; null
* str(2)+str(2) =&gt; 22 * str(2)+str(2) =&gt; 22
* str('pi: %.2f',pi) =&gt; 'pi: 3.14' * str('pi: %.2f',pi) =&gt; 'pi: 3.14'
* str('player at: %d %d %d',pos(player())) =&gt; 'player at: 567, -2423, 124'
* </pre> * </pre>
* *
* <hr> * <hr>
@@ -119,7 +119,7 @@ public class NBTSerializableValue extends Value
if (slot < 0) if (slot < 0)
slot = invSize + slot; slot = invSize + slot;
if (slot < 0 || slot >= invSize) if (slot < 0 || slot >= invSize)
throw new InternalExpressionException("Slot for inventory should be between 0 and "+(invSize-1)); return inv.getInvSize(); // outside of inventory
return slot; return slot;
} }