Advertisement
Guest User

asdfasdfasdf

a guest
Oct 15th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.04 KB | None | 0 0
  1. package openperipheral.addons.sensors;
  2.  
  3. import com.google.common.base.Preconditions;
  4. import com.google.common.collect.Lists;
  5. import com.google.common.collect.Maps;
  6. import com.mojang.authlib.GameProfile;
  7. import java.util.LinkedHashMap;
  8. import java.util.List;
  9. import java.util.Map;
  10. import java.util.UUID;
  11. import net.minecraft.block.Block;
  12. import net.minecraft.entity.Entity;
  13. import net.minecraft.entity.EntityLiving;
  14. import net.minecraft.entity.item.EntityItem;
  15. import net.minecraft.entity.item.EntityItemFrame;
  16. import net.minecraft.entity.item.EntityMinecart;
  17. import net.minecraft.entity.item.EntityPainting;
  18. import net.minecraft.entity.player.EntityPlayer;
  19. import net.minecraft.util.AxisAlignedBB;
  20. import net.minecraft.util.MathHelper;
  21. import net.minecraft.util.Vec3;
  22. import net.minecraft.world.World;
  23. import openmods.utils.ColorUtils;
  24. import openmods.utils.ColorUtils.RGB;
  25. import openmods.utils.WorldUtils;
  26. import openperipheral.addons.OpcAccess;
  27. import openperipheral.api.adapter.IPeripheralAdapter;
  28. import openperipheral.api.adapter.method.Arg;
  29. import openperipheral.api.adapter.method.ReturnType;
  30. import openperipheral.api.adapter.method.ScriptCallable;
  31. import openperipheral.api.architecture.FeatureGroup;
  32. import openperipheral.api.meta.IMetaProviderProxy;
  33.  
  34. @FeatureGroup("openperipheral-sensor")
  35. public class AdapterSensor implements IPeripheralAdapter {
  36.  
  37. private static final String DONT_EVER_CHANGE_THIS_TEXT_OTHERWISE_YOU_WILL_RUIN_EVERYTHING = "Entity not found";
  38.  
  39. public static enum SupportedEntityTypes {
  40. MOB(EntityLiving.class),
  41. MINECART(EntityMinecart.class),
  42. ITEM(EntityItem.class),
  43. ITEM_FRAME(EntityItemFrame.class),
  44. PAINTING(EntityPainting.class);
  45.  
  46. public final Class<? extends Entity> cls;
  47.  
  48. private SupportedEntityTypes(Class<? extends Entity> cls) {
  49. this.cls = cls;
  50. }
  51. }
  52.  
  53. // LRU cache with a maximum of 1000 entries; maps packed-RGB int -> color-bitmask int
  54. @SuppressWarnings("serial")
  55. private static final LinkedHashMap<Integer, Integer> COLOR_CACHE = new LinkedHashMap<Integer, Integer>(16, 0.75f, true) {
  56. @Override
  57. protected boolean removeEldestEntry(Map.Entry<Integer, Integer> eldest) {
  58. return size() > 1000;
  59. }
  60. };
  61.  
  62. @Override
  63. public Class<?> getTargetClass() {
  64. return ISensorEnvironment.class;
  65. }
  66.  
  67. @Override
  68. public String getSourceId() {
  69. return "openperipheral_sensor";
  70. }
  71.  
  72. private static AxisAlignedBB getBoundingBox(Vec3 location, double range) {
  73. return AxisAlignedBB.getBoundingBox(
  74. location.xCoord, location.yCoord, location.zCoord,
  75. location.xCoord + 1, location.yCoord + 1, location.zCoord + 1)
  76. .expand(range, range, range);
  77. }
  78.  
  79. private static AxisAlignedBB getBoundingBox(ISensorEnvironment env) {
  80. return getBoundingBox(env.getLocation(), env.getSensorRange());
  81. }
  82.  
  83. private static List<Integer> listEntityIds(ISensorEnvironment env, Class<? extends Entity> entityClass) {
  84. List<Integer> ids = Lists.newArrayList();
  85.  
  86. final AxisAlignedBB aabb = getBoundingBox(env);
  87. for (Entity entity : WorldUtils.getEntitiesWithinAABB(env.getWorld(), entityClass, aabb))
  88. ids.add(entity.getEntityId());
  89.  
  90. return ids;
  91. }
  92.  
  93. private static IMetaProviderProxy getEntityInfoById(ISensorEnvironment sensor, int mobId, Class<? extends Entity> cls) {
  94. Entity mob = sensor.getWorld().getEntityByID(mobId);
  95. Preconditions.checkArgument(cls.isInstance(mob), DONT_EVER_CHANGE_THIS_TEXT_OTHERWISE_YOU_WILL_RUIN_EVERYTHING);
  96. return getEntityInfo(sensor, mob);
  97. }
  98.  
  99. private static IMetaProviderProxy getPlayerInfo(ISensorEnvironment sensor, String username) {
  100. EntityPlayer player = sensor.getWorld().getPlayerEntityByName(username);
  101. return getEntityInfo(sensor, player);
  102. }
  103.  
  104. private static IMetaProviderProxy getPlayerInfo(ISensorEnvironment sensor, UUID uuid) {
  105. EntityPlayer player = sensor.getWorld().func_152378_a(uuid);
  106. return getEntityInfo(sensor, player);
  107. }
  108.  
  109. private static IMetaProviderProxy getEntityInfo(ISensorEnvironment sensor, Entity mob) {
  110. Preconditions.checkNotNull(mob, DONT_EVER_CHANGE_THIS_TEXT_OTHERWISE_YOU_WILL_RUIN_EVERYTHING);
  111. final AxisAlignedBB aabb = getBoundingBox(sensor);
  112.  
  113. Preconditions.checkArgument(mob.boundingBox.intersectsWith(aabb), DONT_EVER_CHANGE_THIS_TEXT_OTHERWISE_YOU_WILL_RUIN_EVERYTHING);
  114. final Vec3 sensorPos = sensor.getLocation();
  115. return OpcAccess.entityMetaBuilder.createProxy(mob, sensorPos);
  116. }
  117.  
  118. private static boolean checkRange(int x, int y, int z, int rangeSq) {
  119. final int distSq = x * x + y * y + z * z;
  120. return (distSq == 0 || distSq > rangeSq);
  121. }
  122.  
  123. private static String getBlockType(World world, Block block, int xPos, int yPos, int zPos) {
  124. if (block == null || world.isAirBlock(xPos, yPos, zPos)) return "AIR";
  125. else if (block.getMaterial().isLiquid()) return "LIQUID";
  126. else if (block.getMaterial().isSolid()) return "SOLID";
  127. else return "UNKNOWN";
  128. }
  129.  
  130. private static int getBlockColorBitmask(World world, Block block, int xPos, int yPos, int zPos) {
  131. if (block == null) {
  132. return ColorUtils.ColorMeta.BLACK.bitmask; // Default black -- same as air.
  133. } else {
  134. int color = block.getMapColor(block.getDamageValue(world, xPos, yPos, zPos)).colorValue;
  135. Integer cached = COLOR_CACHE.get(color);
  136. if (cached != null) return cached;
  137.  
  138. RGB rgb = new RGB(color);
  139. Integer nearestColorBitmask = ColorUtils.findNearestColor(rgb, 255).bitmask;
  140. COLOR_CACHE.put(color, nearestColorBitmask);
  141. return nearestColorBitmask;
  142. }
  143. }
  144.  
  145. private static Map<String, Object> describeBlock(World world, int sx, int sy, int sz, int dx, int dy, int dz) {
  146. final int bx = sx + dx;
  147. final int by = sy + dy;
  148. final int bz = sz + dz;
  149.  
  150. if (!world.blockExists(bx, by, bz)) return null;
  151.  
  152. final Block block = world.getBlock(bx, by, bz);
  153. final String type = getBlockType(world, block, bx, by, bz);
  154. final int color = getBlockColorBitmask(world, block, bx, by, bz);
  155.  
  156. Map<String, Object> tmp = Maps.newHashMap();
  157. tmp.put("x", dx);
  158. tmp.put("y", dy);
  159. tmp.put("z", dz);
  160. tmp.put("type", type);
  161. tmp.put("color", color);
  162. return tmp;
  163. }
  164.  
  165. @ScriptCallable(returnTypes = ReturnType.OBJECT, description = "Get the ids of all the mobs in range. Deprecated, please use getEntityIds('mob')")
  166. public List<Integer> getMobIds(ISensorEnvironment env) {
  167. return listEntityIds(env, EntityLiving.class);
  168. }
  169.  
  170. @ScriptCallable(returnTypes = ReturnType.OBJECT, description = "Get full details of a particular mob if it's in range. Deprecated, please use getEntityData(id, 'mob')")
  171. public IMetaProviderProxy getMobData(ISensorEnvironment sensor,
  172. @Arg(name = "mobId", description = "The id retrieved from getMobIds()") int id) {
  173. return getEntityInfoById(sensor, id, EntityLiving.class);
  174. }
  175.  
  176. @ScriptCallable(returnTypes = ReturnType.OBJECT, description = "Get the ids of all the minecarts in range. Deprecated, please use getEntityIds('minecart')")
  177. public List<Integer> getMinecartIds(ISensorEnvironment env) {
  178. return listEntityIds(env, EntityMinecart.class);
  179. }
  180.  
  181. @ScriptCallable(returnTypes = ReturnType.OBJECT, description = "Get full details of a particular minecart if it's in range. Deprecated, please use getEntityIds(id, 'minecraft')")
  182. public IMetaProviderProxy getMinecartData(ISensorEnvironment sensor,
  183. @Arg(name = "minecartId", description = "The id retrieved from getMinecartIds()") int id) {
  184. return getEntityInfoById(sensor, id, EntityMinecart.class);
  185. }
  186.  
  187. @ScriptCallable(returnTypes = ReturnType.OBJECT, description = "Get the ids of all entities of single type in range")
  188. public List<Integer> getEntityIds(ISensorEnvironment env, @Arg(name = "type") SupportedEntityTypes type) {
  189. return listEntityIds(env, type.cls);
  190. }
  191.  
  192. @ScriptCallable(returnTypes = ReturnType.OBJECT, description = "Get full details of a particular entity if it's in range")
  193. public IMetaProviderProxy getEntityData(ISensorEnvironment sensor,
  194. @Arg(name = "id", description = "The id retrieved from getEntityIds()") int id,
  195. @Arg(name = "type") SupportedEntityTypes type) {
  196. return getEntityInfoById(sensor, id, type.cls);
  197. }
  198.  
  199. @ScriptCallable(returnTypes = ReturnType.OBJECT, description = "Get the usernames of all the players in range")
  200. public List<GameProfile> getPlayers(ISensorEnvironment env) {
  201. List<EntityPlayer> players = WorldUtils.getEntitiesWithinAABB(env.getWorld(), EntityPlayer.class, getBoundingBox(env));
  202.  
  203. List<GameProfile> names = Lists.newArrayList();
  204. for (EntityPlayer player : players)
  205. names.add(player.getGameProfile());
  206.  
  207. return names;
  208. }
  209.  
  210. @ScriptCallable(returnTypes = ReturnType.OBJECT, description = "Get full details of a particular player if they're in range")
  211. public IMetaProviderProxy getPlayerByName(ISensorEnvironment env,
  212. @Arg(name = "username", description = "The players username") String username) {
  213. return getPlayerInfo(env, username);
  214. }
  215.  
  216. @ScriptCallable(returnTypes = ReturnType.OBJECT, description = "Get full details of a particular player if they're in range")
  217. public IMetaProviderProxy getPlayerByUUID(ISensorEnvironment env,
  218. @Arg(name = "uuid", description = "The players uuid") UUID uuid) {
  219. return getPlayerInfo(env, uuid);
  220. }
  221.  
  222. @ScriptCallable(returnTypes = ReturnType.TABLE, description = "Get a table of information about the surrounding area. Includes map color and whether each block is UNKNOWN, AIR, LIQUID, or SOLID.")
  223. public List<Map<String, Object>> sonicScan(ISensorEnvironment env) {
  224. int range = 1 + env.getSensorRange() / 2;
  225. List<Map<String, Object>> results = Lists.newArrayList();
  226. Vec3 sensorPos = env.getLocation();
  227. int sx = MathHelper.floor_double(sensorPos.xCoord);
  228. int sy = MathHelper.floor_double(sensorPos.yCoord);
  229. int sz = MathHelper.floor_double(sensorPos.zCoord);
  230.  
  231. final World world = env.getWorld();
  232.  
  233. final int rangeSq = range * range;
  234.  
  235. for (int dx = -range; dx <= range; dx++) {
  236. for (int dy = -range; dy <= range; dy++) {
  237. for (int dz = -range; dz <= range; dz++) {
  238. if (checkRange(dx, dy, dz, rangeSq)) {
  239. Map<String, Object> result = describeBlock(world, sx, sy, sz, dx, dy, dz);
  240. if (result != null) results.add(result);
  241. }
  242. }
  243. }
  244. }
  245.  
  246. return results;
  247. }
  248.  
  249. @ScriptCallable(returnTypes = ReturnType.TABLE, description = "Get a table of information about a single block in the surrounding area. Includes map color and whether each block is UNKNOWN, AIR, LIQUID, or SOLID.")
  250. public Map<String, Object> sonicScanTarget(ISensorEnvironment env,
  251. @Arg(name = "xOffset", description = "The target's offset from the sensor on the X-Axis.") int dx,
  252. @Arg(name = "yOffset", description = "The target's offset from the sensor on the Y-Axis.") int dy,
  253. @Arg(name = "zOffset", description = "The target's offset from the sensor on the Z-Axis.") int dz) {
  254. int range = 1 + env.getSensorRange() / 2;
  255. int rangeSq = range * range;
  256. if (checkRange(dx, dy, dz, rangeSq)) return null;
  257.  
  258. Vec3 sensorPos = env.getLocation();
  259. int sx = MathHelper.floor_double(sensorPos.xCoord);
  260. int sy = MathHelper.floor_double(sensorPos.yCoord);
  261. int sz = MathHelper.floor_double(sensorPos.zCoord);
  262.  
  263. return describeBlock(env.getWorld(), sx, sy, sz, dx, dy, dz);
  264. }
  265.  
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement