Advertisement
justync7

TileCoprocAdvMap.java

Oct 8th, 2015
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 25.91 KB | None | 0 0
  1. package mods.immibis.ccperiphs.coproc;
  2.  
  3. import java.util.HashMap;
  4. import java.util.HashSet;
  5. import java.util.List;
  6. import java.util.Map;
  7. import java.util.Set;
  8.  
  9. import mods.immibis.ccperiphs.ImmibisPeripherals;
  10. import net.minecraft.block.Block;
  11. import net.minecraft.entity.Entity;
  12. import net.minecraft.entity.EntityLivingBase;
  13. import net.minecraft.entity.player.EntityPlayer;
  14. import net.minecraft.entity.player.EntityPlayerMP;
  15. import net.minecraft.nbt.*;
  16. import net.minecraft.server.MinecraftServer;
  17. import net.minecraft.tileentity.TileEntity;
  18. import net.minecraft.util.ChatComponentText;
  19. import net.minecraft.util.DamageSource;
  20. import net.minecraft.util.FoodStats;
  21. import net.minecraft.util.Vec3;
  22. import net.minecraft.world.EnumSkyBlock;
  23. import net.minecraft.world.WorldServer;
  24. import net.minecraftforge.common.DimensionManager;
  25. import net.minecraftforge.common.MinecraftForge;
  26. import net.minecraftforge.event.ServerChatEvent;
  27.  
  28. import com.google.common.collect.ImmutableMap;
  29.  
  30. import cpw.mods.fml.common.FMLCommonHandler;
  31. import cpw.mods.fml.common.eventhandler.SubscribeEvent;
  32. import cpw.mods.fml.common.gameevent.PlayerEvent;
  33. import cpw.mods.fml.relauncher.ReflectionHelper;
  34. import dan200.computercraft.api.lua.ILuaContext;
  35. import dan200.computercraft.api.lua.ILuaObject;
  36. import dan200.computercraft.api.lua.LuaException;
  37. import dan200.computercraft.api.peripheral.IComputerAccess;
  38.  
  39. public class TileCoprocAdvMap extends TileCoprocBase {
  40.    
  41.     // All NBT types:
  42.     // compound, list, byte, byteArray, double, float, int, intArray, long, short, string
  43.    
  44.     // converts NBT tags -> lua-readable objects
  45.     private static class LuaNBTObject {
  46.         public String type;
  47.         public Object object;
  48.        
  49.         public LuaNBTObject(NBTBase t) throws LuaException {
  50.             if(t instanceof NBTTagCompound) {
  51.                 object = new LuaNBTCompound((NBTTagCompound)t);
  52.                 type = "compound";
  53.             }
  54.             else if(t instanceof NBTTagList) {
  55.                 object = new LuaNBTList((NBTTagList)t);
  56.                 type = "list";
  57.             }
  58.             else if(t instanceof NBTTagByte) {
  59.                 object = ((NBTTagByte)t).func_150290_f();
  60.                 type = "byte";
  61.             }
  62.             else if(t instanceof NBTTagByteArray) {
  63.                 object = new LuaNBTArray(((NBTTagByteArray)t).func_150292_c());
  64.                 type = "byteArray";
  65.             }
  66.             else if(t instanceof NBTTagDouble) {
  67.                 object = ((NBTTagDouble)t).func_150286_g();
  68.                 type = "double";
  69.             }
  70.             else if(t instanceof NBTTagFloat) {
  71.                 object = ((NBTTagFloat)t).func_150288_h();
  72.                 type = "float";
  73.             }
  74.             else if(t instanceof NBTTagInt) {
  75.                 object = ((NBTTagInt)t).func_150287_d();
  76.                 type = "int";
  77.             }
  78.             else if(t instanceof NBTTagIntArray) {
  79.                 object = new LuaNBTArray(((NBTTagIntArray)t).func_150302_c());
  80.                 type = "intArray";
  81.             }
  82.             else if(t instanceof NBTTagLong) {
  83.                 long l = ((NBTTagLong)t).func_150291_c();
  84.                 long high = (l >> 32) & 0xFFFFFFFFL;
  85.                 long low = l & 0xFFFFFFFFL;
  86.                 // returns {high, low}, "long"
  87.                 object = ImmutableMap.<Integer, Double>builder().put(1, (double)high).put(2, (double)low).build();
  88.                 type = "long";
  89.             }
  90.             else if(t instanceof NBTTagShort) {
  91.                 object = ((NBTTagShort)t).func_150289_e();
  92.                 type = "short";
  93.             }
  94.             else if(t instanceof NBTTagString) {
  95.                 object = ((NBTTagString)t).func_150285_a_();
  96.                 type = "string";
  97.             }
  98.             else
  99.                 throw new LuaException("unknown nbt tag type: "+t.getClass().getSimpleName());
  100.         }
  101.     }
  102.    
  103.     // lua argument lists -> NBT tags
  104.     private static NBTBase convertArgumentsToTag(Object[] args) throws LuaException {
  105.         String type = (String)args[0];
  106.        
  107.        
  108.         // compound, list, byte, byteArray, double, float, int, intArray, long, short, string
  109.         if(type.equals("compound")) {
  110.             return new NBTTagCompound();
  111.            
  112.         } else if(type.equals("list")) {
  113.             return new NBTTagList();
  114.            
  115.         } else if(type.equals("byte")) {
  116.             if(args.length < 2 || !(args[1] instanceof Double))
  117.                 throw new LuaException("for byte: need a number");
  118.            
  119.             return new NBTTagByte((byte)(double)(Double)args[1]);
  120.            
  121.         } else if(type.equals("byteArray")) {
  122.             if(args.length < 2 || !(args[1] instanceof Double))
  123.                 throw new LuaException("for byteArray: need a number (size)");
  124.             try {
  125.                 return new NBTTagByteArray(new byte[(int)(double)(Double)args[1]]);
  126.             } catch(NegativeArraySizeException e) {
  127.                 throw new LuaException("size cannot be negative");
  128.             }
  129.            
  130.         } else if(type.equals("double")) {
  131.             if(args.length < 2 || !(args[1] instanceof Double))
  132.                 throw new LuaException("for double: need a number");
  133.            
  134.             return new NBTTagDouble((Double)args[1]);
  135.            
  136.         } else if(type.equals("float")) {
  137.             if(args.length < 2 || !(args[1] instanceof Double))
  138.                 throw new LuaException("for float: need a number");
  139.            
  140.             return new NBTTagFloat((float)(double)(Double)args[1]);
  141.            
  142.         } else if(type.equals("int")) {
  143.             if(args.length < 2 || !(args[1] instanceof Double))
  144.                 throw new LuaException("for int: need a number");
  145.            
  146.             return new NBTTagInt((int)(double)(Double)args[1]);
  147.            
  148.         } else if(type.equals("intArray")) {
  149.             if(args.length < 2 || !(args[1] instanceof Double))
  150.                 throw new LuaException("for intArray: need a number (size)");
  151.             try {
  152.                 return new NBTTagIntArray(new int[(int)(double)(Double)args[1]]);
  153.             } catch(NegativeArraySizeException e) {
  154.                 throw new LuaException("size cannot be negative");
  155.             }
  156.            
  157.         } else if(type.equals("long")) {
  158.             if(args.length < 3 || !(args[1] instanceof Double) || !(args[2] instanceof Double))
  159.                 throw new LuaException("for long: need two numbers, high 32 bits first, then low 32 bits");
  160.             long high = (long)(double)(Double)args[1];
  161.             long low = (long)(double)(Double)args[2];
  162.             return new NBTTagLong(low | (high << 32));
  163.        
  164.         } else if(type.equals("short")) {
  165.             if(args.length < 2 || !(args[1] instanceof Double))
  166.                 throw new LuaException("for short: need a number");
  167.            
  168.             return new NBTTagShort((short)(double)(Double)args[1]);
  169.        
  170.         } else if(type.equals("string")) {
  171.             if(args.length < 2 || !(args[1] instanceof String))
  172.                 throw new LuaException("for string: need a string");
  173.            
  174.             return new NBTTagString((String)args[1]);
  175.        
  176.         } else {
  177.             throw new LuaException("invalid NBT type: "+type+". valid types are: compound, list, byte, byteArray, double, float, int, intArray, long, short, string");
  178.         }
  179.     }
  180.    
  181.     private static class LuaNBTArray implements ILuaObject {
  182.         private final Object ar;
  183.         private final int len;
  184.         private final String type;
  185.        
  186.         public LuaNBTArray(byte[] ar) {
  187.             this.ar = ar;
  188.             len = ar.length;
  189.             type = "byteArray";
  190.         }
  191.        
  192.         public LuaNBTArray(int[] ar) {
  193.             this.ar = ar;
  194.             len = ar.length;
  195.             type = "intArray";
  196.         }
  197.        
  198.         @Override
  199.         public String[] getMethodNames() {
  200.             return new String[] {
  201.                 "getType",
  202.                 "getLength",
  203.                 "get",
  204.                 "set"
  205.             };
  206.         }
  207.        
  208.         @Override
  209.         public Object[] callMethod(ILuaContext ctx, int arg0, Object[] args) throws LuaException {
  210.             switch(arg0) {
  211.             case 0:
  212.                 return new Object[] {type};
  213.             case 1:
  214.                 return new Object[] {len};
  215.             case 2:
  216.                 checkArgs(args, Double.class);
  217.                 try {
  218.                     if(ar instanceof byte[])
  219.                         return new Object[] {((byte[])ar)[(int)(double)(Double)args[0]]};
  220.                     else
  221.                         return new Object[] {((int[])ar)[(int)(double)(Double)args[0]]};
  222.                 } catch(ArrayIndexOutOfBoundsException e) {
  223.                     throw new LuaException("index out of bounds");
  224.                 }
  225.             case 3:
  226.                 checkArgs(args, Double.class, Double.class);
  227.                 try {
  228.                     if(ar instanceof byte[])
  229.                         ((byte[])ar)[(int)(double)(Double)args[0]] = (byte)(double)(Double)args[1];
  230.                     else
  231.                         ((int[])ar)[(int)(double)(Double)args[0]] = (int)(double)(Double)args[1];
  232.                 } catch(ArrayIndexOutOfBoundsException e) {
  233.                     throw new LuaException("index out of bounds");
  234.                 }
  235.                 return null;
  236.             }
  237.             return null;
  238.         }
  239.     }
  240.    
  241.     private static class LuaNBTList implements ILuaObject {
  242.         private final NBTTagList tag;
  243.         public LuaNBTList(NBTTagList tag) {
  244.             this.tag = tag;
  245.         }
  246.        
  247.         @Override
  248.         public String[] getMethodNames() {
  249.             return new String[] {
  250.                 "getSize",
  251.                 "get",
  252.                 "add",
  253.                 "remove",
  254.                 "getType"
  255.             };
  256.         }
  257.        
  258.         private List<NBTBase> getList() {
  259.             return ReflectionHelper.<List<NBTBase>, NBTTagList>getPrivateValue(NBTTagList.class, tag, 0);
  260.         }
  261.        
  262.         @Override
  263.         public Object[] callMethod(ILuaContext ctx, int arg0, Object[] args) throws LuaException {
  264.             switch(arg0) {
  265.             case 0: // getSize() -> int
  266.                 return new Object[] {tag.tagCount()};
  267.             case 1:
  268.                 // get(int index) -> tag
  269.                 checkArgs(args, Double.class);
  270.                 try {
  271.                     LuaNBTObject o = new LuaNBTObject(getList().get((int)(double)(Double)args[0]));
  272.                     return new Object[] {o.type, o.object};
  273.                 } catch(IndexOutOfBoundsException e) {
  274.                     throw new LuaException("index out of bounds");
  275.                 }
  276.             case 2:
  277.                 // add(int index, string type, [variant value])
  278.                 {
  279.                     checkArgs(args, Double.class, String.class);
  280.                    
  281.                     int index = (int)(double)(Double)args[0];
  282.                    
  283.                     // strip first argument, convert remaining arguments to tag
  284.                     Object[] tagInfo = new Object[args.length - 1];
  285.                     System.arraycopy(args, 1, tagInfo, 0, tagInfo.length);
  286.                     try {
  287.                         getList().add(index, convertArgumentsToTag(tagInfo));
  288.                     } catch(IndexOutOfBoundsException e) {
  289.                         throw new LuaException("index out of bounds");
  290.                     }
  291.                 }
  292.                 return null;
  293.             case 3:
  294.                 // remove(int index)
  295.                 checkArgs(args, Double.class);
  296.                 try {
  297.                     getList().remove((int)(double)(Double)args[0]);
  298.                 } catch(IndexOutOfBoundsException e) {
  299.                     throw new LuaException("index out of bounds");
  300.                 }
  301.                 return null;
  302.             case 4:
  303.                 return new Object[] {"list"};
  304.             }
  305.             return null;
  306.         }
  307.     }
  308.    
  309.     private static class LuaNBTCompound implements ILuaObject {
  310.         private final NBTTagCompound tag;
  311.        
  312.         public LuaNBTCompound(NBTTagCompound tag) {
  313.             this.tag = tag;
  314.         }
  315.        
  316.         private Map<String, NBTBase> getMap() {
  317.             return ReflectionHelper.<Map<String, NBTBase>, NBTTagCompound>getPrivateValue(NBTTagCompound.class, tag, 0);
  318.         }
  319.        
  320.         @Override
  321.         public Object[] callMethod(ILuaContext ctx, int arg0, Object[] args) throws LuaException {
  322.             switch(arg0) {
  323.             case 0:
  324.                 return new Object[] {"compound"};
  325.             case 1:
  326.                 {
  327.                     checkArgs(args, String.class);
  328.                     String key = (String)args[0];
  329.                     NBTBase t = tag.getTag(key);
  330.                    
  331.                     LuaNBTObject o = new LuaNBTObject(t);
  332.                     return new Object[] {o.type, o.object};
  333.                 }
  334.             case 2:
  335.                 // hasKey(string key) -> boolean
  336.                 checkArgs(args, String.class);
  337.                 return new Object[] {tag.hasKey((String)args[0])};
  338.             case 3:
  339.                 // getKeys() -> array of keys
  340.                 {
  341.                     Map<Integer, String> rv = new HashMap<Integer, String>();
  342.                     int k = 1;
  343.                     for(String key : (Set<String>)getMap().keySet())
  344.                         rv.put(k++, key);
  345.                     return new Object[] {rv};
  346.                 }
  347.             case 4:
  348.                 // setValue(string key, string type, [variant value])
  349.                 {
  350.                     checkArgs(args, String.class, String.class);
  351.                    
  352.                     String key = (String)args[0];
  353.                    
  354.                     // strip first argument, convert remaining arguments to tag
  355.                     Object[] tagInfo = new Object[args.length - 1];
  356.                     System.arraycopy(args, 1, tagInfo, 0, tagInfo.length);
  357.                     tag.setTag(key, convertArgumentsToTag(tagInfo));
  358.                 }
  359.                 return null;
  360.             case 5:
  361.                 // remove(string key)
  362.                 checkArgs(args, String.class);
  363.                 tag.removeTag((String)args[0]);
  364.                 return null;
  365.             }
  366.             return null;
  367.         }
  368.        
  369.         @Override
  370.         public String[] getMethodNames() {
  371.             return new String[] {
  372.                 "getType",
  373.                 "getValue",
  374.                 "hasKey",
  375.                 "getKeys",
  376.                 "setValue",
  377.                 "remove"
  378.             };
  379.         }
  380.        
  381.        
  382.     }
  383.    
  384.     private static class LuaTileEntity implements ILuaObject {
  385.  
  386.         private final TileEntity te;
  387.        
  388.         public LuaTileEntity(TileEntity te) {
  389.             this.te = te;
  390.         }
  391.        
  392.         private NBTTagCompound curNBT;
  393.        
  394.         @Override
  395.         public Object[] callMethod(ILuaContext ctx, int arg0, Object[] arg1) throws LuaException {
  396.             switch(arg0) {
  397.             case 0:
  398.                 return new Object[] {te.getClass().getName()};
  399.             case 1:
  400.                 curNBT = new NBTTagCompound();
  401.                 te.writeToNBT(curNBT);
  402.                 return null;
  403.             case 2:
  404.                 if(curNBT == null)
  405.                     throw new LuaException("no NBT loaded");
  406.                 return new Object[] {new LuaNBTCompound(curNBT)};
  407.             case 3:
  408.                 if(curNBT == null)
  409.                     throw new LuaException("no NBT loaded");
  410.                 te.readFromNBT(curNBT);
  411.                 te.getWorldObj().markBlockForUpdate(te.xCoord, te.yCoord, te.zCoord);
  412.                 return null;
  413.             }
  414.             return null;
  415.         }
  416.  
  417.         @Override
  418.         public String[] getMethodNames() {
  419.             return new String[] {
  420.                 "getClass",
  421.                 "readNBT",
  422.                 "getNBT",
  423.                 "writeNBT"
  424.             };
  425.         }
  426.        
  427.     }
  428.    
  429.     private static class LuaEntity implements ILuaObject {
  430.        
  431.         private final Entity e;
  432.        
  433.         public LuaEntity(Entity e) {
  434.             this.e = e;
  435.         }
  436.  
  437.         @Override
  438.         public Object[] callMethod(ILuaContext ctx, int arg0, Object[] args) throws LuaException {
  439.             switch(arg0) {
  440.             case 0: // getPosition() -> x, y, z
  441.                 return new Object[] {e.posX, e.posY, e.posZ};
  442.            
  443.             case 1: // setPosition(x, y, z)
  444.                 checkArgs(args, Double.class, Double.class, Double.class);
  445.                 {
  446.                     double x = (Double)args[0];
  447.                     double y = (Double)args[1];
  448.                     double z = (Double)args[2];
  449.                     if(e instanceof EntityLivingBase)
  450.                         ((EntityLivingBase)e).setPositionAndUpdate(x, y, z);
  451.                     else
  452.                         e.setPosition(x, y, z);
  453.                 }
  454.                 return null;
  455.                
  456.             case 2: // getWorldID() -> dimID
  457.                 return new Object[] {e.worldObj.provider.dimensionId};
  458.                
  459.             case 3: // getLooking() -> x, y, z
  460.                 Vec3 v = e.getLookVec();
  461.                 return new Object[] {v.xCoord, v.yCoord, v.zCoord};
  462.             }
  463.             return null;
  464.         }
  465.  
  466.         @Override
  467.         public String[] getMethodNames() {
  468.             return new String[] {
  469.                 "getPosition",
  470.                 "setPosition",
  471.                 "getWorldID",
  472.                 "getLooking"
  473.             };
  474.         }
  475.        
  476.     }
  477.    
  478.     private static class LuaEntityPlayer implements ILuaObject {
  479.  
  480.         private final EntityPlayer pl;
  481.        
  482.         public LuaEntityPlayer(EntityPlayer pl) {
  483.             this.pl = pl;
  484.         }
  485.        
  486.         @Override
  487.         public Object[] callMethod(ILuaContext ctx, int arg0, Object[] args) throws LuaException {
  488.             switch(arg0) {
  489.             case 0: // getUsername() -> string
  490.                 return new Object[] {pl.getGameProfile().getName()};
  491.             case 1: // asEntity() -> entity
  492.                 return new Object[] {new LuaEntity(pl)};
  493.             case 2: // sendChat(string)
  494.                 checkArgs(args, String.class);
  495.                 pl.addChatMessage(new ChatComponentText((String)args[0]));
  496.                 return null;
  497.             case 3: // getHealth() -> float
  498.                 return new Object[] {pl.getHealth()};
  499.             case 4: // getHunger() -> int
  500.                 return new Object[] {pl.getFoodStats().getFoodLevel()};
  501.             case 5: // getFoodSaturation() -> int
  502.                 return new Object[] {pl.getFoodStats().getSaturationLevel()};
  503.             case 6: // setHunger(int amt)
  504.                 checkArgs(args, Double.class);
  505.                 ReflectionHelper.setPrivateValue(FoodStats.class, pl.getFoodStats(), (int)(double)(Double)args[0], 0);
  506.                 return null;
  507.             case 7: // setFoodSaturation(int amt)
  508.                 checkArgs(args, Double.class);
  509.                 ReflectionHelper.setPrivateValue(FoodStats.class, pl.getFoodStats(), (float)(double)(Double)args[0], 1);
  510.                 return null;
  511.             case 8: // heal(int amt)
  512.                 checkArgs(args, Double.class);
  513.                 pl.heal((int)(double)(Double)args[0]);
  514.                 return null;
  515.             case 9: // damage(int amt) -> boolean success
  516.                 checkArgs(args, Double.class);
  517.                 return new Object[] {pl.attackEntityFrom(DamageSource.magic, (int)(double)(Double)args[0])};
  518.             case 10: // getGamemode() -> int mode
  519.                 if(pl instanceof EntityPlayerMP)
  520.                     return new Object[] {((EntityPlayerMP)pl).theItemInWorldManager.getGameType().getID()};
  521.                 else
  522.                     return null;
  523.             }
  524.             return null;
  525.         }
  526.  
  527.         @Override
  528.         public String[] getMethodNames() {
  529.             return new String[] {
  530.                 "getUsername",
  531.                 "asEntity",
  532.                 "sendChat",
  533.                 "getHealth",
  534.                 "getHunger",
  535.                 "getFoodSaturation",
  536.                 "setHunger",
  537.                 "setFoodSaturation",
  538.                 "heal",
  539.                 "damage",
  540.                 "getGamemode"
  541.             };
  542.         }
  543.        
  544.     }
  545.    
  546.     private static class LuaWorld implements ILuaObject {
  547.        
  548.         private final WorldServer w;
  549.        
  550.         public LuaWorld(WorldServer world) {
  551.             this.w = world;
  552.         }
  553.        
  554.         @Override
  555.         public String[] getMethodNames() {
  556.             return new String[] {
  557.                 "getBiome",
  558.                 "getBlockID",
  559.                 "getMetadata",
  560.                 "getBlockLight",
  561.                 "getSkyLight",
  562.                 "playSound",
  563.                 "explode",
  564.                 "getClosestPlayer",
  565.                 "isChunkLoaded",
  566.                 "setBlock",
  567.                 "setBlockWithoutNotify",
  568.                 "getTime",
  569.                 "setTime",
  570.                 "getTileEntity"
  571.             };
  572.         }
  573.        
  574.         @Override
  575.         public Object[] callMethod(ILuaContext ctx, int arg0, Object[] args) throws LuaException {
  576.             switch(arg0) {
  577.            
  578.             case 0: // getBiome(int x, int z) -> string biomeName
  579.                 checkArgs(args, Double.class, Double.class);
  580.                 return new Object[] {w.getBiomeGenForCoords((int)(double)(Double)args[0], (int)(double)(Double)args[1]).biomeName};
  581.                
  582.             case 1: // getBlockID(int x, int y, int z) -> int ID
  583.                 checkArgs(args, Double.class, Double.class, Double.class);
  584.                 return new Object[] {Block.getIdFromBlock(w.getBlock((int)(double)(Double)args[0], (int)(double)(Double)args[1], (int)(double)(Double)args[2]))};
  585.            
  586.             case 2: // getMetadata(int x, int y, int z) -> int meta
  587.                 checkArgs(args, Double.class, Double.class, Double.class);
  588.                 return new Object[] {w.getBlockMetadata((int)(double)(Double)args[0], (int)(double)(Double)args[1], (int)(double)(Double)args[2])};
  589.            
  590.             case 3: // getBlockLight(int x, int y, int z) -> int light
  591.                 checkArgs(args, Double.class, Double.class, Double.class);
  592.                 return new Object[] {w.getSkyBlockTypeBrightness(EnumSkyBlock.Block, (int)(double)(Double)args[0], (int)(double)(Double)args[1], (int)(double)(Double)args[2])};
  593.            
  594.             case 4: // getSkyLight(int x, int y, int z) -> int light
  595.                 checkArgs(args, Double.class, Double.class, Double.class);
  596.                 return new Object[] {w.getSkyBlockTypeBrightness(EnumSkyBlock.Sky, (int)(double)(Double)args[0], (int)(double)(Double)args[1], (int)(double)(Double)args[2])};
  597.            
  598.             case 5: // playSound(string sound, double x, double y, double z, float volume, float pitch)
  599.                 checkArgs(args, String.class, Double.class, Double.class, Double.class, Double.class, Double.class);
  600.                 w.playSoundEffect((Double)args[1], (Double)args[2], (Double)args[3], (String)args[0], (float)(double)(Double)args[4], (float)(double)(Double)args[5]);
  601.                 return null;
  602.            
  603.             case 6: // explode(double x, double y, double z, float power, boolean fire, boolean blocks)
  604.                 checkArgs(args, Double.class, Double.class, Double.class, Double.class, Boolean.class, Boolean.class);
  605.                 w.newExplosion(null, (Double)args[0], (Double)args[1], (Double)args[2], (float)(double)(Double)args[3], (Boolean)args[4], (Boolean)args[5]);
  606.                 return null;
  607.            
  608.             case 7: // getClosestPlayer(double x, double y, double z, [double maxDist]) -> player
  609.                 checkArgs(args, 3, Double.class, Double.class, Double.class, Double.class);
  610.                 EntityPlayer pl = w.getClosestPlayer((Double)args[0], (Double)args[1], (Double)args[2], args.length >= 4 && args[3] != null ? (Double)args[3] : 0);
  611.                 if(pl != null)
  612.                     return new Object[] {new LuaEntityPlayer(pl)};
  613.                 else
  614.                     return null;
  615.                
  616.             case 8: // isChunkLoaded(int x, int z) -> boolean
  617.                 checkArgs(args, Double.class, Double.class);
  618.                 {
  619.                     int x = (int)(double)(Double)args[0];
  620.                     int z = (int)(double)(Double)args[1];
  621.                     return new Object[] {w.blockExists(x<<4, 64, z<<4)};
  622.                 }
  623.            
  624.             case 9: // setBlock(int x, int y, int z, int ID, int meta)
  625.                 checkArgs(args, Double.class, Double.class, Double.class, Double.class, Double.class);
  626.                 int blockID = (int)(double)(Double)args[3];
  627.                 if(Block.getBlockById(blockID) == null)
  628.                     throw new LuaException("invalid block ID");
  629.                 w.setBlock(
  630.                     (int)(double)(Double)args[0],
  631.                     (int)(double)(Double)args[1],
  632.                     (int)(double)(Double)args[2],
  633.                     Block.getBlockById(blockID),
  634.                     (int)(double)(Double)args[4] & 15,
  635.                     3);
  636.                 return null;
  637.            
  638.             case 10: // setBlockWithoutNotify(int x, int y, int z, int ID, int meta)
  639.                 checkArgs(args, Double.class, Double.class, Double.class, Double.class, Double.class);
  640.                 blockID = (int)(double)(Double)args[3];
  641.                 if(Block.getBlockById(blockID) == null)
  642.                     throw new LuaException("invalid block ID");
  643.                 w.setBlock(
  644.                     (int)(double)(Double)args[0],
  645.                     (int)(double)(Double)args[1],
  646.                     (int)(double)(Double)args[2],
  647.                     Block.getBlockById(blockID),
  648.                     (int)(double)(Double)args[4] & 15,
  649.                     2);
  650.                 return null;
  651.            
  652.             case 11: // getTime() -> int
  653.                 return new Object[] {w.getWorldTime()};
  654.            
  655.             case 12: // setTime(long time)
  656.                 checkArgs(args, Double.class);
  657.                 w.setWorldTime((long)(double)(Double)args[0]);
  658.                 return null;
  659.            
  660.             case 13: // getTileEntity(int x, int y, int z) -> tile entity
  661.                 checkArgs(args, Double.class, Double.class, Double.class);
  662.                 {
  663.                     TileEntity te = w.getTileEntity((int)(double)(Double)args[0], (int)(double)(Double)args[1], (int)(double)(Double)args[2]);
  664.                     if(te == null)
  665.                         return null;
  666.                     else
  667.                         return new Object[] {new LuaTileEntity(te)};
  668.                 }
  669.             }
  670.             return null;
  671.         }
  672.     }
  673.  
  674.     @SuppressWarnings("unchecked")
  675.     @Override
  676.     public Object[] callMethod(IComputerAccess computer, ILuaContext ctx, int method, Object[] args) throws LuaException {
  677.         if(!ImmibisPeripherals.allowAdventureMapInterface)
  678.             throw new LuaException("Peripheral disabled in config");
  679.        
  680.         switch(method) {
  681.         case 0:
  682.             // getLoadedWorlds() -> list of dim IDs
  683.             {
  684.                 int k = 1;
  685.                 Map<Integer, Integer> rv = new HashMap<Integer, Integer>();
  686.                 for(WorldServer ws : MinecraftServer.getServer().worldServers)
  687.                     rv.put(k++, ws.provider.dimensionId);
  688.                 return new Object[] {rv};
  689.             }
  690.            
  691.         case 1:
  692.             {
  693.                 // getWorld(int dimID) -> world
  694.                 checkArgs(args, Double.class);
  695.                 int dim = (int)(double)(Double)args[0];
  696.                 WorldServer ws = DimensionManager.getWorld(dim);
  697.                 if(ws == null)
  698.                     return null;
  699.                 else
  700.                     return new Object[] {new LuaWorld(ws)};
  701.             }
  702.        
  703.         case 2:
  704.             {
  705.                 // getOrLoadWorld(int dimID) -> world
  706.                 checkArgs(args, Double.class);
  707.                 int dim = (int)(double)(Double)args[0];
  708.                 WorldServer ws = MinecraftServer.getServer().worldServerForDimension(dim);
  709.                 if(ws == null)
  710.                     return null;
  711.                 else
  712.                     return new Object[] {new LuaWorld(ws)};
  713.             }
  714.            
  715.         case 3:
  716.             // getPeripheralPos() -> x, y, z
  717.             return new Object[] {xCoord, yCoord, zCoord};
  718.        
  719.         case 4:
  720.             // getPeripheralWorldID() -> dimID
  721.             return new Object[] {worldObj.provider.dimensionId};
  722.            
  723.         case 5:
  724.             {
  725.                 // getPlayerByName(string username) -> player/nil
  726.                 checkArgs(args, String.class);
  727.                 for(EntityPlayer pl : (List<EntityPlayer>)MinecraftServer.getServer().getConfigurationManager().playerEntityList)
  728.                     if(pl.getGameProfile().getName().equals((String)args[0]))
  729.                         return new Object[] {new LuaEntityPlayer(pl)};
  730.                 return new Object[0];
  731.             }
  732.            
  733.         case 6:
  734.             // getPlayerUsernames() -> array of usernames
  735.             {
  736.                 Map<Integer, String> rvmap = new HashMap<Integer, String>();
  737.                 int k = 1;
  738.                 for(EntityPlayer pl2 : (List<EntityPlayer>)MinecraftServer.getServer().getConfigurationManager().playerEntityList)
  739.                     rvmap.put(k++, pl2.getGameProfile().getName());
  740.                
  741.                 return new Object[] {rvmap};
  742.             }
  743.            
  744.         case 7:
  745.             {
  746.                 // getRegisteredWorlds() -> list of dim IDs.
  747.                 int k = 1;
  748.                 Map<Integer, Integer> rv = new HashMap<Integer, Integer>();
  749.                 for(int i : ((Map<Integer, Integer>)ReflectionHelper.getPrivateValue(DimensionManager.class, null, "dimensions")).keySet())
  750.                     rv.put(k++, i);
  751.                 return new Object[] {rv};
  752.             }
  753.         }
  754.        
  755.         return null;
  756.     }
  757.  
  758.     private static Set<TileCoprocAdvMap> tiles = new HashSet<TileCoprocAdvMap>();
  759.     @Override
  760.     public void onChunkUnload() {
  761.         tiles.remove(this);
  762.         super.onChunkUnload();
  763.     }
  764.    
  765.     @Override
  766.     public void invalidate() {
  767.         tiles.remove(this);
  768.         super.invalidate();
  769.     }
  770.    
  771.     @Override
  772.     public void validate() {
  773.         tiles.add(this);
  774.         super.validate();
  775.     }
  776.  
  777.     @Override
  778.     public String[] getMethodNames() {
  779.         return new String[] {
  780.             "getLoadedWorlds",
  781.             "getWorld",
  782.             "getOrLoadWorld",
  783.             "getPeripheralPos",
  784.             "getPeripheralWorldID",
  785.             "getPlayerByName",
  786.             "getPlayerUsernames",
  787.             "getRegisteredWorlds",
  788.         };
  789.     }
  790.  
  791.     @Override
  792.     public String getType() {
  793.         return "adventure map interface";
  794.     }
  795.  
  796.     public void onChatEvent(EntityPlayer entityPlayer, String message) {
  797.         queueEvent("chat_message", entityPlayer.getGameProfile().getName(), message, String.valueOf(entityPlayer.getGameProfile().getId()));
  798.     }
  799.    
  800.     private void queueEvent(String evt, Object... args) {
  801.         if(!ImmibisPeripherals.allowAdventureMapInterface)
  802.             return;
  803.         for(IComputerAccess c : computers)
  804.             c.queueEvent(evt, args);
  805.     }
  806.    
  807.     public static class EventHandler {
  808.         @SubscribeEvent
  809.         public void onPlayerRespawn(PlayerEvent.PlayerRespawnEvent evt) {
  810.             EntityPlayer player = evt.player;
  811.             for(TileCoprocAdvMap t : tiles)
  812.                 t.queueEvent("player_respawn", player.getGameProfile().getName(), String.valueOf(player.getGameProfile().getId()));
  813.         }
  814.        
  815.         @SubscribeEvent
  816.         public void onPlayerLogout(PlayerEvent.PlayerLoggedOutEvent evt) {
  817.             EntityPlayer player = evt.player;
  818.             for(TileCoprocAdvMap t : tiles)
  819.                 t.queueEvent("player_logout", player.getGameProfile().getName(), String.valueOf(player.getGameProfile().getId()));
  820.         }
  821.        
  822.         @SubscribeEvent
  823.         public void onPlayerLogin(PlayerEvent.PlayerLoggedInEvent evt) {
  824.             EntityPlayer player = evt.player;
  825.             for(TileCoprocAdvMap t : tiles)
  826.                 t.queueEvent("player_login", player.getGameProfile().getName(), String.valueOf(player.getGameProfile().getId()));
  827.         }
  828.        
  829.         @SubscribeEvent
  830.         public void onPlayerChangedDimension(PlayerEvent.PlayerChangedDimensionEvent evt) {
  831.             EntityPlayer player = evt.player;
  832.             for(TileCoprocAdvMap t : tiles)
  833.                 t.queueEvent("player_change_world", player.getGameProfile().getName(), String.valueOf(player.getGameProfile().getId()));
  834.         }
  835.        
  836.         @SubscribeEvent
  837.         public void onChat(ServerChatEvent evt) {
  838.             for(TileCoprocAdvMap t : tiles)
  839.                 t.onChatEvent(evt.player, evt.message);
  840.         }
  841.     }
  842.    
  843.     static {
  844.         FMLCommonHandler.instance().bus().register(new EventHandler());
  845.         MinecraftForge.EVENT_BUS.register(new EventHandler());
  846.     }
  847.  
  848. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement