InventoryCommands.java 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package me.km.snuviscript.commands;
  2. import me.hammerle.snuviscript.code.ScriptManager;
  3. import me.km.inventory.CustomContainer;
  4. import me.km.inventory.ModInventory;
  5. import me.km.utils.Location;
  6. import net.minecraft.entity.player.PlayerEntity;
  7. import net.minecraft.entity.player.ServerPlayerEntity;
  8. import net.minecraft.inventory.IInventory;
  9. import net.minecraft.item.ItemStack;
  10. import net.minecraft.tileentity.ChestTileEntity;
  11. public class InventoryCommands {
  12. public static void registerFunctions(ScriptManager sm) {
  13. sm.registerFunction("inv.new", (sc, in) -> new ModInventory(in[0].getString(sc)));
  14. sm.registerFunction("inv.getid",
  15. (sc, in) -> (double) ((ModInventory) in[0].get(sc)).getModId());
  16. sm.registerFunction("inv.loadchest", (sc, in) -> {
  17. Location l = (Location) in[0].get(sc);
  18. ChestTileEntity chest = (ChestTileEntity) l.getWorld().getTileEntity(l.getBlockPos());
  19. int size = chest.getSizeInventory();
  20. if(size % 9 != 0) {
  21. size /= 9;
  22. size++;
  23. size *= 9;
  24. }
  25. ModInventory inv = new ModInventory(size);
  26. for(int i = 0; i < chest.getSizeInventory(); i++) {
  27. inv.setInventorySlotContents(i, chest.getStackInSlot(i).copy());
  28. }
  29. return inv;
  30. });
  31. sm.registerConsumer("inv.setitem", (sc, in) -> {
  32. ((IInventory) in[0].get(sc)).setInventorySlotContents(in[1].getInt(sc),
  33. (ItemStack) in[2].get(sc));
  34. });
  35. sm.registerFunction("inv.getitem",
  36. (sc, in) -> ((IInventory) in[0].get(sc)).getStackInSlot(in[1].getInt(sc)));
  37. sm.registerFunction("inv.getsize",
  38. (sc, in) -> (double) ((IInventory) in[0].get(sc)).getSizeInventory());
  39. sm.registerConsumer("inv.open", (sc, in) -> {
  40. CustomContainer.openForPlayer((ServerPlayerEntity) in[1].get(sc),
  41. (ModInventory) in[0].get(sc), in[2].getString(sc), sc);
  42. });
  43. sm.registerConsumer("inv.close", (sc, in) -> {
  44. ((PlayerEntity) in[0].get(sc)).closeScreen();
  45. });
  46. sm.registerConsumer("inv.update", (sc, in) -> {
  47. ServerPlayerEntity p = (ServerPlayerEntity) in[0].get(sc);
  48. p.sendAllContents(p.openContainer, p.openContainer.getInventory());
  49. });
  50. }
  51. }