MinecraftFunctions.java 115 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676
  1. package me.km.snuviscript;
  2. import com.mojang.authlib.GameProfile;
  3. import com.mojang.brigadier.StringReader;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.util.ArrayList;
  8. import java.util.Date;
  9. import java.util.GregorianCalendar;
  10. import java.util.List;
  11. import java.util.UUID;
  12. import java.util.function.Consumer;
  13. import me.hammerle.snuviscript.code.Script;
  14. import me.hammerle.snuviscript.code.ScriptManager;
  15. import me.hammerle.snuviscript.code.SnuviUtils;
  16. import me.hammerle.snuviscript.inputprovider.InputProvider;
  17. import me.km.Server;
  18. import me.km.blockprotections.IBlockProtection;
  19. import me.km.databank.DataBank;
  20. import me.km.utils.Location;
  21. import me.km.utils.Utils;
  22. import me.km.world.WorldManager;
  23. import me.km.entities.EntityHuman;
  24. import me.km.entities.EntityItemProjectile;
  25. import me.km.entities.ModEntities;
  26. import me.km.inventory.InventoryUtils;
  27. import me.km.utils.ItemStackUtils;
  28. import me.km.utils.ReflectionUtils;
  29. import net.minecraft.enchantment.Enchantment;
  30. import net.minecraft.enchantment.EnchantmentHelper;
  31. import net.minecraft.entity.Entity;
  32. import net.minecraft.inventory.IInventory;
  33. import net.minecraft.item.ItemStack;
  34. import net.minecraft.network.ThreadQuickExitException;
  35. import net.minecraft.potion.PotionUtils;
  36. import net.minecraft.tileentity.TileEntity;
  37. import net.minecraft.util.DamageSource;
  38. import net.minecraft.util.SoundCategory;
  39. import net.minecraft.util.math.BlockPos;
  40. import net.minecraft.util.math.Vec3d;
  41. import net.minecraft.world.World;
  42. import me.km.events.CustomEventCaller;
  43. import me.km.events.PlayerMoveData;
  44. import me.km.inventory.CustomContainer;
  45. import me.km.inventory.ModInventory;
  46. import me.km.networking.ModPacketHandler;
  47. import me.km.overrides.ModEntityPlayerMP;
  48. import me.km.permissions.PermissionManager;
  49. import me.km.playerbank.IPlayerBank;
  50. import me.km.plots.PlotMap.Plot;
  51. import me.km.plots.WorldPlotMap;
  52. import me.km.scheduler.SnuviScheduler;
  53. import me.km.utils.Mapper;
  54. import me.km.utils.TableUtils;
  55. import net.minecraft.block.Block;
  56. import net.minecraft.block.BlockState;
  57. import net.minecraft.block.ChestBlock;
  58. import net.minecraft.block.CropsBlock;
  59. import net.minecraft.block.DoorBlock;
  60. import net.minecraft.command.ICommandSource;
  61. import net.minecraft.command.arguments.BlockStateParser;
  62. import net.minecraft.entity.AgeableEntity;
  63. import net.minecraft.entity.EntityType;
  64. import net.minecraft.entity.LivingEntity;
  65. import net.minecraft.entity.MobEntity;
  66. import net.minecraft.entity.SharedMonsterAttributes;
  67. import net.minecraft.entity.SpawnReason;
  68. import net.minecraft.entity.ai.attributes.AttributeModifier.Operation;
  69. import net.minecraft.entity.effect.LightningBoltEntity;
  70. import net.minecraft.entity.item.EnderPearlEntity;
  71. import net.minecraft.entity.item.ExperienceBottleEntity;
  72. import net.minecraft.entity.item.ItemEntity;
  73. import net.minecraft.entity.item.ItemFrameEntity;
  74. import net.minecraft.entity.monster.CreeperEntity;
  75. import net.minecraft.entity.player.PlayerEntity;
  76. import net.minecraft.entity.player.ServerPlayerEntity;
  77. import net.minecraft.entity.projectile.AbstractArrowEntity;
  78. import net.minecraft.entity.projectile.ArrowEntity;
  79. import net.minecraft.entity.projectile.DamagingProjectileEntity;
  80. import net.minecraft.entity.projectile.DragonFireballEntity;
  81. import net.minecraft.entity.projectile.EggEntity;
  82. import net.minecraft.entity.projectile.FireballEntity;
  83. import net.minecraft.entity.projectile.PotionEntity;
  84. import net.minecraft.entity.projectile.SmallFireballEntity;
  85. import net.minecraft.entity.projectile.SnowballEntity;
  86. import net.minecraft.entity.projectile.SpectralArrowEntity;
  87. import net.minecraft.entity.projectile.WitherSkullEntity;
  88. import net.minecraft.inventory.EnderChestInventory;
  89. import net.minecraft.inventory.EquipmentSlotType;
  90. import net.minecraft.inventory.container.ChestContainer;
  91. import net.minecraft.inventory.container.SimpleNamedContainerProvider;
  92. import net.minecraft.item.ArmorItem;
  93. import net.minecraft.item.Item;
  94. import net.minecraft.item.Items;
  95. import net.minecraft.nbt.CompoundNBT;
  96. import net.minecraft.nbt.JsonToNBT;
  97. import net.minecraft.nbt.NBTUtil;
  98. import net.minecraft.network.play.client.CChatMessagePacket;
  99. import net.minecraft.network.play.client.CClientStatusPacket;
  100. import net.minecraft.network.play.server.SEntityVelocityPacket;
  101. import net.minecraft.network.play.server.SSpawnPositionPacket;
  102. import net.minecraft.network.play.server.STitlePacket;
  103. import net.minecraft.network.play.server.SUpdateTileEntityPacket;
  104. import net.minecraft.particles.BlockParticleData;
  105. import net.minecraft.particles.IParticleData;
  106. import net.minecraft.particles.ItemParticleData;
  107. import net.minecraft.particles.ParticleType;
  108. import net.minecraft.particles.ParticleTypes;
  109. import net.minecraft.particles.RedstoneParticleData;
  110. import net.minecraft.potion.Effect;
  111. import net.minecraft.potion.EffectInstance;
  112. import net.minecraft.server.MinecraftServer;
  113. import net.minecraft.server.management.PlayerList;
  114. import net.minecraft.server.management.ProfileBanEntry;
  115. import net.minecraft.state.IProperty;
  116. import net.minecraft.state.properties.ChestType;
  117. import net.minecraft.tags.BlockTags;
  118. import net.minecraft.tags.ItemTags;
  119. import net.minecraft.tags.Tag;
  120. import net.minecraft.tileentity.ChestTileEntity;
  121. import net.minecraft.tileentity.MobSpawnerTileEntity;
  122. import net.minecraft.tileentity.SignTileEntity;
  123. import net.minecraft.tileentity.SkullTileEntity;
  124. import net.minecraft.util.Direction;
  125. import net.minecraft.util.NonNullList;
  126. import net.minecraft.util.ResourceLocation;
  127. import net.minecraft.util.SoundEvent;
  128. import net.minecraft.util.math.BlockRayTraceResult;
  129. import net.minecraft.util.math.MathHelper;
  130. import net.minecraft.util.math.RayTraceContext;
  131. import net.minecraft.util.math.RayTraceResult;
  132. import net.minecraft.util.text.ITextComponent;
  133. import net.minecraft.util.text.StringTextComponent;
  134. import net.minecraft.util.text.Style;
  135. import net.minecraft.util.text.TextFormatting;
  136. import net.minecraft.util.text.event.ClickEvent;
  137. import net.minecraft.util.text.event.HoverEvent;
  138. import net.minecraft.world.Difficulty;
  139. import net.minecraft.world.GameRules;
  140. import net.minecraft.world.GameType;
  141. import net.minecraft.world.IWorld;
  142. import net.minecraft.world.server.ServerWorld;
  143. import net.minecraft.world.storage.WorldInfo;
  144. public class MinecraftFunctions
  145. {
  146. public static int inventoryIds = 0;
  147. @SuppressWarnings("")
  148. public static void registerFunctions(ScriptManager sm, Scripts scripts,
  149. PermissionManager perms, SnuviScheduler scheduler, MinecraftServer server,
  150. IPlayerBank playerBank, CustomEventCaller cec,
  151. IScriptBank scriptBank, DataBank dataBank, IBlockProtection blockProtection,
  152. WorldPlotMap plots)
  153. {
  154. // ---------------------------------------------------------------------
  155. // Command-library
  156. // ---------------------------------------------------------------------
  157. sm.registerFunction("command.add", (sc, in) ->
  158. {
  159. scripts.registerScriptCommand(in[0].getString(sc));
  160. return Void.TYPE;
  161. });
  162. sm.registerFunction("command.remove", (sc, in) ->
  163. {
  164. scripts.unregisterScriptCommand(in[0].getString(sc));
  165. return Void.TYPE;
  166. });
  167. sm.registerFunction("command.exists", (sc, in) -> scripts.isRegisteredScriptCommand(in[0].getString(sc)));
  168. sm.registerFunction("command.clear", (sc, in) ->
  169. {
  170. scripts.clearScriptCommands();
  171. return Void.TYPE;
  172. });
  173. // ---------------------------------------------------------------------
  174. // Permission-library
  175. // ---------------------------------------------------------------------
  176. sm.registerFunction("perm.clear", (sc, in) ->
  177. {
  178. perms.clear();
  179. return Void.TYPE;
  180. });
  181. sm.registerFunction("perm.registergroup", (sc, in) ->
  182. {
  183. perms.registerGroupPermission(in[0].getInt(sc), in[1].getString(sc));
  184. return Void.TYPE;
  185. });
  186. sm.registerFunction("perm.registerplayer", (sc, in) ->
  187. {
  188. perms.registerPlayerGroup(getUUID(in[0].get(sc)), in[1].getInt(sc));
  189. return Void.TYPE;
  190. });
  191. sm.registerFunction("perm.unregisterplayer", (sc, in) -> perms.unregisterPlayer(getUUID(in[0].get(sc)), in[1].getInt(sc)));
  192. sm.registerFunction("perm.has", (sc, in) -> perms.hasPermission((Entity) in[0].get(sc), in[1].getString(sc)));
  193. // ---------------------------------------------------------------------
  194. // Title-library
  195. // ---------------------------------------------------------------------
  196. sm.registerFunction("table.getstart", (sc, in) -> TableUtils.getTableStart(in[0].getInt(sc), in[1].getInt(sc), in[2].getString(sc)));
  197. sm.registerFunction("table.getmiddle", (sc, in) -> TableUtils.getTableMiddle(in[0].getInt(sc), in[1].getInt(sc), in[2].getString(sc)));
  198. sm.registerFunction("table.getend", (sc, in) -> TableUtils.getTableEnd(in[0].getInt(sc), in[1].getInt(sc), in[2].getString(sc)));
  199. sm.registerFunction("table.get", (sc, in) ->
  200. {
  201. String[] columns = new String[in.length - 2];
  202. for(int i = 0; i < columns.length; i++)
  203. {
  204. columns[i] = in[i + 2].getString(sc);
  205. }
  206. return TableUtils.getTable(in[0].getInt(sc), in[1].getString(sc), columns);
  207. });
  208. // ---------------------------------------------------------------------
  209. // table library
  210. // ---------------------------------------------------------------------
  211. sm.registerFunction("title.settime", (sc, in) ->
  212. {
  213. ((ServerPlayerEntity) in[0].get(sc)).connection.sendPacket(new STitlePacket(in[1].getInt(sc), in[2].getInt(sc), in[3].getInt(sc)));
  214. return Void.TYPE;
  215. });
  216. sm.registerFunction("title.clear", (sc, in) ->
  217. {
  218. ServerPlayerEntity p = (ServerPlayerEntity) in[0].get(sc);
  219. p.connection.sendPacket(new STitlePacket(STitlePacket.Type.CLEAR, null));
  220. p.connection.sendPacket(new STitlePacket(STitlePacket.Type.RESET, null));
  221. return Void.TYPE;
  222. });
  223. sm.registerFunction("title.reset", (sc, in) ->
  224. {
  225. ((ServerPlayerEntity) in[0].get(sc)).connection.sendPacket(new STitlePacket(STitlePacket.Type.RESET, null));
  226. return Void.TYPE;
  227. });
  228. sm.registerFunction("title.send", (sc, in) ->
  229. {
  230. ITextComponent text = new StringTextComponent(SnuviUtils.connect(sc, in, 1));
  231. ((ServerPlayerEntity) in[0].get(sc)).connection.sendPacket(new STitlePacket(STitlePacket.Type.TITLE, text));
  232. return Void.TYPE;
  233. });
  234. sm.registerFunction("title.setsub", (sc, in) ->
  235. {
  236. ITextComponent text = new StringTextComponent(SnuviUtils.connect(sc, in, 1));
  237. ((ServerPlayerEntity) in[0].get(sc)).connection.sendPacket(new STitlePacket(STitlePacket.Type.SUBTITLE, text));
  238. return Void.TYPE;
  239. });
  240. // ---------------------------------------------------------------------
  241. // Player-library
  242. // ---------------------------------------------------------------------
  243. sm.registerFunction("player.getitemamount", (sc, in) -> (double) InventoryUtils.searchInventoryFor(((PlayerEntity) in[0].get(sc)).inventory, (ItemStack) in[2].get(sc), in[1].getBoolean(sc)));
  244. sm.registerFunction("player.removeitem", (sc, in) ->
  245. {
  246. ItemStack stack = ((ItemStack) in[1].get(sc)).copy();
  247. stack.setCount(InventoryUtils.removeFromInventory(((PlayerEntity) in[0].get(sc)).inventory, stack));
  248. return stack;
  249. });
  250. sm.registerFunction("player.giveitem", (sc, in) ->
  251. {
  252. ItemStack stack = ((ItemStack) in[1].get(sc)).copy();
  253. stack.setCount(InventoryUtils.addToInventory(((PlayerEntity) in[0].get(sc)).inventory, stack));
  254. return stack;
  255. });
  256. sm.registerFunction("player.shootprojectile", (sc, in) -> launchProjectile((PlayerEntity) in[0].get(sc),
  257. getClass(in[1].getString(sc)), in[2].getDouble(sc), in.length >= 4 ? in[3].get(sc) : null));
  258. sm.registerFunction("player.respawn", (sc, in) ->
  259. {
  260. final ServerPlayerEntity p = ((ServerPlayerEntity) in[0].get(sc));
  261. scheduler.scheduleTask(() ->
  262. {
  263. try
  264. {
  265. p.connection.processClientStatus(new CClientStatusPacket(CClientStatusPacket.State.PERFORM_RESPAWN));
  266. }
  267. catch(ThreadQuickExitException ex)
  268. {
  269. // Minecraft needs this for canceling and queueing into main thread
  270. }
  271. });
  272. return Void.TYPE;
  273. });
  274. sm.registerFunction("player.clearinventory", (sc, in) ->
  275. {
  276. ((PlayerEntity) in[0].get(sc)).inventory.clear();
  277. return Void.TYPE;
  278. });
  279. sm.registerFunction("player.inventorytolist", (sc, in) -> ((PlayerEntity) in[1].get(sc)).inventory.mainInventory);
  280. sm.registerFunction("player.getnearest", (sc, in) ->
  281. {
  282. Location l = (Location) in[0].get(sc);
  283. return l.getWorld().getClosestPlayer(l.getX(), l.getY(), l.getZ(), -1, p -> true);
  284. });
  285. sm.registerFunction("player.say", (sc, in) ->
  286. {
  287. try
  288. {
  289. ((ServerPlayerEntity) in[0].get(sc)).connection.processChatMessage(new CChatMessagePacket(SnuviUtils.connect(sc, in, 1)));
  290. }
  291. catch(ThreadQuickExitException ex)
  292. {
  293. // Minecraft needs this for canceling and queueing into main thread
  294. }
  295. return Void.TYPE;
  296. });
  297. sm.registerFunction("player.speak", (sc, in) ->
  298. {
  299. sendMessageToGroup(server, scripts, perms, in[0].get(sc), sc, concat(sc, 2, "§7[§r" + in[1].getString(sc) + "§7]§r ", in));
  300. return Void.TYPE;
  301. });
  302. sm.registerFunction("player.setcompass", (sc, in) ->
  303. {
  304. ((ServerPlayerEntity) in[0].get(sc)).connection.sendPacket(new SSpawnPositionPacket(((Location) in[1].get(sc)).getBlockPos()));
  305. return Void.TYPE;
  306. });
  307. sm.registerFunction("player.gethunger", (sc, in) -> (double) ((PlayerEntity) in[0].get(sc)).getFoodStats().getFoodLevel());
  308. sm.registerFunction("player.sethunger", (sc, in) ->
  309. {
  310. ((PlayerEntity) in[0].get(sc)).getFoodStats().setFoodLevel(in[1].getInt(sc));
  311. return Void.TYPE;
  312. });
  313. sm.registerFunction("player.getsaturation", (sc, in) -> (double) ((PlayerEntity) in[0].get(sc)).getFoodStats().getSaturationLevel());
  314. sm.registerFunction("player.setsaturation", (sc, in) ->
  315. {
  316. ReflectionUtils.setSaturation(((PlayerEntity) in[0].get(sc)).getFoodStats(), in[1].getFloat(sc));
  317. return Void.TYPE;
  318. });
  319. sm.registerFunction("player.getname", (sc, in) ->
  320. {
  321. Object o = in[0].get(sc);
  322. if(o instanceof PlayerEntity)
  323. {
  324. return ((PlayerEntity) o).getName().getFormattedText();
  325. }
  326. return server.getPlayerProfileCache().getProfileByUUID(getUUID(o.toString())).getName();
  327. });
  328. sm.registerFunction("player.getuuid", (sc, in) ->
  329. {
  330. Object o = in[0].get(sc);
  331. if(o instanceof PlayerEntity)
  332. {
  333. return ((PlayerEntity) o).getUniqueID();
  334. }
  335. return playerBank.getUUID(o.toString());
  336. });
  337. sm.registerFunction("player.getid", (sc, in) -> (double) getId(playerBank, in[0].get(sc)));
  338. sm.registerFunction("player.get", (sc, in) -> server.getPlayerList().getPlayerByUUID(getUUID(in[0].get(sc))));
  339. sm.registerFunction("player.getuuidfromid", (sc, in) -> playerBank.getUUIDfromID(in[0].getInt(sc)));
  340. sm.registerFunction("player.getnamefromid", (sc, in) -> playerBank.getNamefromID(in[0].getInt(sc)));
  341. sm.registerFunction("player.getip", (sc, in) -> ((ServerPlayerEntity) in[0].get(sc)).connection.netManager.getRemoteAddress().toString());
  342. sm.registerFunction("player.iscreative", (sc, in) -> ((PlayerEntity) in[0].get(sc)).isCreative());
  343. sm.registerFunction("player.isspectator", (sc, in) -> ((PlayerEntity) in[0].get(sc)).isSpectator());
  344. sm.registerFunction("player.issurvival", (sc, in) ->
  345. {
  346. PlayerEntity p = (PlayerEntity) in[0].get(sc);
  347. return !p.isCreative() && !p.isSpectator();
  348. });
  349. sm.registerFunction("player.isadventure", (sc, in) -> !((PlayerEntity) in[0].get(sc)).abilities.allowEdit);
  350. sm.registerFunction("player.setfly", (sc, in) ->
  351. {
  352. PlayerEntity p = ((PlayerEntity) in[0].get(sc));
  353. boolean b = in[1].getBoolean(sc);
  354. p.abilities.allowFlying = b;
  355. p.abilities.isFlying = b;
  356. p.sendPlayerAbilities();
  357. return Void.TYPE;
  358. });
  359. sm.registerFunction("player.hasfly", (sc, in) -> ((PlayerEntity) in[0].get(sc)).abilities.allowFlying);
  360. sm.registerFunction("player.isflying", (sc, in) -> ((PlayerEntity) in[0].get(sc)).abilities.isFlying);
  361. sm.registerFunction("player.setgamemode", (sc, in) ->
  362. {
  363. PlayerEntity p = (PlayerEntity) in[0].get(sc);
  364. switch(in[1].get(sc).toString())
  365. {
  366. case "survival":
  367. case "s":
  368. case "0":
  369. p.setGameType(GameType.SURVIVAL);
  370. break;
  371. case "creative":
  372. case "c":
  373. case "1":
  374. p.setGameType(GameType.CREATIVE);
  375. break;
  376. case "adventure":
  377. case "a":
  378. case "2":
  379. p.setGameType(GameType.ADVENTURE);
  380. break;
  381. case "spectator":
  382. case "w":
  383. case "3":
  384. p.setGameType(GameType.SPECTATOR);
  385. break;
  386. default:
  387. p.setGameType(GameType.CREATIVE);
  388. break;
  389. }
  390. return Void.TYPE;
  391. });
  392. sm.registerFunction("player.getlastdamager", (sc, in) ->
  393. {
  394. DamageSource ds = ((PlayerEntity) in[0].get(sc)).getLastDamageSource();
  395. if(ds == null)
  396. {
  397. return null;
  398. }
  399. return ds.getImmediateSource();
  400. });
  401. sm.registerFunction("player.dropinventory", (sc, in) ->
  402. {
  403. ((PlayerEntity) in[0].get(sc)).inventory.dropAllItems();
  404. return Void.TYPE;
  405. });
  406. sm.registerFunction("player.gettarget", (sc, in) ->
  407. {
  408. PlayerEntity p = (PlayerEntity) in[0].get(sc);
  409. double radius = in[1].getDouble(sc);
  410. if(radius > 128.0)
  411. {
  412. radius = 128.0;
  413. }
  414. RayTraceContext.BlockMode bm = RayTraceContext.BlockMode.OUTLINE;
  415. if(in.length >= 3 && in[2].getBoolean(sc))
  416. {
  417. bm = RayTraceContext.BlockMode.COLLIDER;
  418. }
  419. RayTraceContext.FluidMode fm = RayTraceContext.FluidMode.NONE;
  420. if(in.length >= 4 && in[3].getBoolean(sc))
  421. {
  422. fm = RayTraceContext.FluidMode.ANY;
  423. }
  424. Vec3d start = p.getEyePosition(0.0f);
  425. Vec3d unit = p.getLook(0.0f);
  426. Vec3d end = start.add(unit.x * radius, unit.y * radius, unit.z * radius);
  427. BlockRayTraceResult result = p.world.rayTraceBlocks(new RayTraceContext(start, end, bm, fm, p));
  428. if(result.getType() == RayTraceResult.Type.BLOCK)
  429. {
  430. return new Location(p.world, result.getPos());
  431. }
  432. return new Location(p.world, end);
  433. });
  434. sm.registerFunction("player.gettargetentity", (sc, in) -> Utils.getTargetedEntity((PlayerEntity) in[0].get(sc), in[1].getDouble(sc), getClass(in[2].getString(sc))));
  435. sm.registerFunction("player.hasscript", (sc, in) -> scripts.isRegistered((PlayerEntity) in[0].get(sc)));
  436. sm.registerAlias("player.hasscript", "player.hasquest");
  437. sm.registerFunction("player.action", (sc, in) ->
  438. {
  439. StringTextComponent text = new StringTextComponent(SnuviUtils.connect(sc, in, 1));
  440. doForGroup(server, scripts, perms, in[0].get(sc), sc, p -> ((ServerPlayerEntity) p).sendStatusMessage(text, true));
  441. return Void.TYPE;
  442. });
  443. sm.registerFunction("player.disconnect", (sc, in) ->
  444. {
  445. ((ServerPlayerEntity) in[0].get(sc)).connection.disconnect(new StringTextComponent(in[1].getString(sc)));
  446. return Void.TYPE;
  447. });
  448. sm.registerFunction("player.kick", (sc, in) -> scripts.unregisterPlayer(sc, (PlayerEntity) in[0].get(sc)));
  449. sm.registerFunction("player.getspawn", (sc, in) ->
  450. {
  451. ServerWorld ws = (ServerWorld) in[1].get(sc);
  452. return new Location(ws, ((PlayerEntity) in[0].get(sc)).getBedLocation(ws.getDimension().getType()));
  453. });
  454. sm.registerAlias("player.getspawn", "player.getbedspawn");
  455. sm.registerFunction("player.setspawn", (sc, in) ->
  456. {
  457. Location l = (Location) in[1].get(sc);
  458. ((ServerPlayerEntity) in[0].get(sc)).setSpawnPoint(l.getBlockPos(), true, l.getWorld().getDimension().getType());
  459. return Void.TYPE;
  460. });
  461. sm.registerAlias("player.setspawn", "player.setbedspawn");
  462. sm.registerFunction("player.damageitem", (sc, in) ->
  463. {
  464. PlayerEntity p =(PlayerEntity) in[0].get(sc);
  465. p.getHeldItemMainhand().damageItem(in[1].getInt(sc), p, (c) -> {});
  466. return Void.TYPE;
  467. });
  468. sm.registerFunction("player.openenderchest", (sc, in) ->
  469. {
  470. PlayerEntity p1 = (PlayerEntity) in[0].get(sc);
  471. PlayerEntity p2 = (PlayerEntity) in[1].get(sc);
  472. EnderChestInventory inv = p2.getInventoryEnderChest();
  473. p1.openContainer(new SimpleNamedContainerProvider((id, pInv, p) ->
  474. {
  475. return ChestContainer.createGeneric9X3(id, pInv, inv);
  476. }, new StringTextComponent(in[2].getString(sc))));
  477. return Void.TYPE;
  478. });
  479. sm.registerFunction("player.getlevel", (sc, in) -> (double) ((PlayerEntity) in[0].get(sc)).experienceLevel);
  480. sm.registerFunction("player.getexp", (sc, in) -> (double) ((PlayerEntity) in[0].get(sc)).experience);
  481. sm.registerFunction("player.addlevel", (sc, in) ->
  482. {
  483. PlayerEntity p = (PlayerEntity) in[0].get(sc);
  484. p.addExperienceLevel(in[1].getInt(sc));
  485. return Void.TYPE;
  486. });
  487. sm.registerFunction("player.setexp", (sc, in) ->
  488. {
  489. PlayerEntity p = (PlayerEntity) in[0].get(sc);
  490. float f = in[1].getFloat(sc);
  491. if(f < 0.0f)
  492. {
  493. f = 0.0f;
  494. }
  495. else if(f > 1.0f)
  496. {
  497. f = 1.0f;
  498. }
  499. p.giveExperiencePoints((int) (f * p.xpBarCap()));
  500. return Void.TYPE;
  501. });
  502. sm.registerFunction("player.gethead", (sc, in) ->
  503. {
  504. ItemStack stack = new ItemStack(Items.PLAYER_HEAD);
  505. CompoundNBT com = stack.getOrCreateTag();
  506. GameProfile gp = new GameProfile(getUUID(in[0].get(sc)), in[1].getString(sc));
  507. gp = SkullTileEntity.updateGameProfile(gp);
  508. com.put("SkullOwner", NBTUtil.writeGameProfile(new CompoundNBT(), gp));
  509. return stack;
  510. });
  511. sm.registerFunction("player.setspeed", (sc, in) ->
  512. {
  513. PlayerEntity p = (PlayerEntity) in[0].get(sc);
  514. float f = in[1].getFloat(sc);
  515. ReflectionUtils.setFlySpeed(p.abilities, f);
  516. ReflectionUtils.setWalkSpeed(p.abilities, f * 2);
  517. p.sendPlayerAbilities();
  518. return true;
  519. });
  520. sm.registerFunction("player.near", (sc, in) -> Utils.getPlayers((Entity) in[0].get(sc), in[1].getDouble(sc)));
  521. sm.registerFunction("player.getinvslot", (sc, in) -> ((PlayerEntity) in[0].get(sc)).inventory.mainInventory.get(in[1].getInt(sc)));
  522. sm.registerFunction("player.setinvslot", (sc, in) ->
  523. {
  524. ((PlayerEntity) in[0].get(sc)).inventory.mainInventory.set(in[1].getInt(sc), ((ItemStack) in[2].get(sc)).copy());
  525. return Void.TYPE;
  526. });
  527. sm.registerFunction("player.getenderslot", (sc, in) -> ((PlayerEntity) in[0].get(sc)).getInventoryEnderChest().getStackInSlot(in[1].getInt(sc)));
  528. sm.registerFunction("player.setenderslot", (sc, in) ->
  529. {
  530. ((PlayerEntity) in[0].get(sc)).getInventoryEnderChest().setInventorySlotContents(in[1].getInt(sc), ((ItemStack) in[2].get(sc)).copy());
  531. return Void.TYPE;
  532. });
  533. // ---------------------------------------------------------------------
  534. // Players-library
  535. // ---------------------------------------------------------------------
  536. sm.registerFunction("players.getamount", (sc, in) -> (double) server.getCurrentPlayerCount());
  537. sm.registerFunction("players.tolist", (sc, in) -> new ArrayList(server.getPlayerList().getPlayers()));
  538. sm.registerFunction("players.toworldlist", (sc, in) -> new ArrayList(((World) in[0].get(sc)).getPlayers()));
  539. sm.registerFunction("players.near", (sc, in) ->
  540. {
  541. Location l = (Location) in[0].get(sc);
  542. return Utils.getPlayers(l.getWorld(), l.getX(), l.getY(), l.getZ(), in[1].getDouble(sc));
  543. });
  544. // ---------------------------------------------------------------------
  545. // World-library
  546. // ---------------------------------------------------------------------
  547. sm.registerAlias("players.toworldlist", "world.getplayers");
  548. sm.registerFunction("world.register", (sc, in) ->
  549. {
  550. boolean b = WorldManager.register(in[0].getString(sc));
  551. if(b)
  552. {
  553. ModPacketHandler.syncDimensions();
  554. }
  555. return b;
  556. });
  557. sm.registerFunction("world.unregister", (sc, in) ->
  558. {
  559. boolean b = WorldManager.unregister(server, in[0].getString(sc));
  560. if(b)
  561. {
  562. ModPacketHandler.syncDimensions();
  563. }
  564. return b;
  565. });
  566. sm.registerFunction("world.get", (sc, in) ->
  567. {
  568. return WorldManager.get(server, in[0].getString(sc));
  569. });
  570. sm.registerFunction("world.getname", (sc, in) ->
  571. {
  572. return WorldManager.getName((World) in[0].get(sc));
  573. });
  574. sm.registerFunction("world.setdiffi", (sc, in) ->
  575. {
  576. Difficulty diffi = Difficulty.valueOf(in[1].getString(sc).toUpperCase());
  577. ((World) in[0].get(sc)).getWorldInfo().setDifficulty(diffi);
  578. return Void.TYPE;
  579. });
  580. sm.registerFunction("world.setgamerule", (sc, in) ->
  581. {
  582. Object o = ((World) in[0].get(sc)).getGameRules().get(Mapper.getGameRule(in[1].getString(sc)));
  583. if(o instanceof GameRules.BooleanValue)
  584. {
  585. ((GameRules.BooleanValue) o).set(in[2].getBoolean(sc), server);
  586. }
  587. if(o instanceof GameRules.IntegerValue)
  588. {
  589. ReflectionUtils.setIntegerValue((GameRules.IntegerValue) o, in[2].getInt(sc));
  590. }
  591. return Void.TYPE;
  592. });
  593. sm.registerFunction("world.getgamerule", (sc, in) ->
  594. {
  595. Object o = ((World) in[0].get(sc)).getGameRules().get(Mapper.getGameRule(in[1].getString(sc)));
  596. if(o instanceof GameRules.BooleanValue)
  597. {
  598. return ((GameRules.BooleanValue) o).get();
  599. }
  600. if(o instanceof GameRules.IntegerValue)
  601. {
  602. return (double) ((GameRules.IntegerValue) o).get();
  603. }
  604. return null;
  605. });
  606. sm.registerFunction("world.setspawn", (sc, in) ->
  607. {
  608. Location l = ((Location) in[0].get(sc));
  609. l.getWorld().getDimension().setSpawnPoint(l.getBlockPos());
  610. return Void.TYPE;
  611. });
  612. sm.registerFunction("world.getspawn", (sc, in) ->
  613. {
  614. World w = (World) in[0].get(sc);
  615. return new Location(w, w.getSpawnPoint());
  616. });
  617. sm.registerFunction("world.getall", (sc, in) ->
  618. {
  619. ArrayList<World> worlds = new ArrayList<>();
  620. for(World w : server.getWorlds())
  621. {
  622. worlds.add(w);
  623. }
  624. return worlds;
  625. });
  626. sm.registerFunction("world.settime", (sc, in) ->
  627. {
  628. ((World) in[0].get(sc)).setDayTime(in[1].getLong(sc));
  629. return Void.TYPE;
  630. });
  631. sm.registerFunction("world.gettime", (sc, in) -> (double) ((World) in[0].get(sc)).getDayTime());
  632. sm.registerFunction("world.hasstorm", (sc, in) -> ((World) in[0].get(sc)).isRaining());
  633. sm.registerFunction("world.clearweather", (sc, in) ->
  634. {
  635. WorldInfo wi = ((World) in[0].get(sc)).getWorldInfo();
  636. wi.setClearWeatherTime(in[1].getInt(sc));
  637. wi.setRainTime(0);
  638. wi.setThunderTime(0);
  639. wi.setRaining(false);
  640. wi.setThundering(false);
  641. return Void.TYPE;
  642. });
  643. sm.registerFunction("world.setrain", (sc, in) ->
  644. {
  645. WorldInfo wi = ((World) in[0].get(sc)).getWorldInfo();
  646. wi.setClearWeatherTime(0);
  647. int i = in[1].getInt(sc);
  648. wi.setRainTime(i);
  649. wi.setThunderTime(i);
  650. wi.setRaining(true);
  651. wi.setThundering(false);
  652. return Void.TYPE;
  653. });
  654. sm.registerFunction("world.setthunder", (sc, in) ->
  655. {
  656. WorldInfo wi = ((World) in[0].get(sc)).getWorldInfo();
  657. wi.setClearWeatherTime(0);
  658. int i = in[1].getInt(sc);
  659. wi.setRainTime(i);
  660. wi.setThunderTime(i);
  661. wi.setRaining(true);
  662. wi.setThundering(true);
  663. return Void.TYPE;
  664. });
  665. // ---------------------------------------------------------------------
  666. // Item-library
  667. // ---------------------------------------------------------------------
  668. sm.registerFunction("item.drop", (sc, in) ->
  669. {
  670. Location l = (Location) in[0].get(sc);
  671. World w = l.getWorld().getWorld();
  672. BlockPos pos = l.getBlockPos();
  673. ItemStack stack = ((ItemStack) in[1].get(sc)).copy();
  674. int amount = stack.getCount();
  675. while(amount > stack.getMaxStackSize())
  676. {
  677. stack.setCount(stack.getMaxStackSize());
  678. amount -= stack.getMaxStackSize();
  679. Block.spawnAsEntity(w, pos, stack.copy());
  680. }
  681. if(amount > 0)
  682. {
  683. stack.setCount(amount);
  684. Block.spawnAsEntity(w, pos, stack);
  685. }
  686. return Void.TYPE;
  687. });
  688. sm.registerFunction("item.gettag", (sc, in) -> ItemTags.getCollection().get(new ResourceLocation(in[0].getString(sc))));
  689. sm.registerFunction("item.hastag", (sc, in) -> ((Tag<Item>) in[0].get(sc)).contains(((ItemStack) in[1].get(sc)).getItem()));
  690. sm.registerFunction("item.get", (sc, in) -> ((ItemStack) in[0].get(sc)).getItem());
  691. sm.registerFunction("item.gettype", (sc, in) -> ((ItemStack) in[0].get(sc)).getItem().getRegistryName().toString());
  692. sm.registerFunction("item.getamount", (sc, in) -> (double) ((ItemStack) in[0].get(sc)).getCount());
  693. sm.registerFunction("item.setamount", (sc, in) ->
  694. {
  695. ((ItemStack) in[0].get(sc)).setCount(in[1].getInt(sc));
  696. return Void.TYPE;
  697. });
  698. sm.registerFunction("item.getname", (sc, in) -> ((ItemStack) in[0].get(sc)).getDisplayName());
  699. sm.registerFunction("item.setname", (sc, in) ->
  700. {
  701. ((ItemStack) in[0].get(sc)).setDisplayName(new StringTextComponent(SnuviUtils.connect(sc, in, 1)));
  702. return Void.TYPE;
  703. });
  704. sm.registerFunction("item.getlore", (sc, in) ->
  705. {
  706. return ItemStackUtils.getLore((ItemStack) in[0].get(sc));
  707. });
  708. sm.registerFunction("item.setlore", (sc, in) ->
  709. {
  710. ItemStackUtils.setLore((ItemStack) in[0].get(sc), (List<Object>) in[1].get(sc));
  711. return Void.TYPE;
  712. });
  713. sm.registerFunction("item.addlore", (sc, in) ->
  714. {
  715. ItemStackUtils.addLore((ItemStack) in[0].get(sc), SnuviUtils.connect(sc, in, 2), in[1].getInt(sc));
  716. return Void.TYPE;
  717. });
  718. sm.registerFunction("item.addenchant", (sc, in) ->
  719. {
  720. ItemStack stack = (ItemStack) in[0].get(sc);
  721. Enchantment ench = Mapper.getEnchantment(in[1].getString(sc));
  722. int i = in[2].getInt(sc);
  723. stack.addEnchantment(ench, i);
  724. return true;
  725. });
  726. sm.registerFunction("item.getenchantlevel", (sc, in) -> (double) EnchantmentHelper.getEnchantmentLevel(Mapper.getEnchantment(in[1].getString(sc)), (ItemStack) in[0].get(sc)));
  727. sm.registerFunction("item.setcooldown", (sc, in) ->
  728. {
  729. ((PlayerEntity) in[0].get(sc)).getCooldownTracker().setCooldown(((ItemStack) in[1].get(sc)).getItem(), in[2].getInt(sc));
  730. return Void.TYPE;
  731. });
  732. sm.registerFunction("item.addpotion", (sc, in) ->
  733. {
  734. ItemStack stack = (ItemStack) in[0].get(sc);
  735. List<EffectInstance> list = PotionUtils.getEffectsFromStack(stack);
  736. list.add(new EffectInstance(Mapper.getPotion(in[1].getString(sc)), in[2].getInt(sc), in[3].getInt(sc)));
  737. PotionUtils.appendEffects(stack, list);
  738. return Void.TYPE;
  739. });
  740. sm.registerFunction("item.addflag", (sc, in) ->
  741. {
  742. ItemStackUtils.addItemFlag((ItemStack) in[0].get(sc), ItemStackUtils.ItemFlag.valueOf(in[1].getString(sc).toUpperCase()));
  743. return Void.TYPE;
  744. });
  745. sm.registerFunction("item.addattribute", (sc, in) ->
  746. {
  747. ItemStackUtils.addAttribute((ItemStack) in[0].get(sc),
  748. ItemStackUtils.Attribute.valueOf(in[1].getString(sc).toUpperCase()),
  749. EquipmentSlotType.valueOf(in[2].getString(sc).toUpperCase()),
  750. in[3].getDouble(sc),
  751. Operation.valueOf(in[4].getString(sc).toUpperCase()));
  752. return Void.TYPE;
  753. });
  754. sm.registerFunction("item.setmagic", (sc, in) ->
  755. {
  756. ItemStack stack = (ItemStack) in[0].get(sc);
  757. int value = in[1].getInt(sc);
  758. if(stack.getItem() instanceof ArmorItem)
  759. {
  760. CompoundNBT com = stack.getTag();
  761. if(com == null)
  762. {
  763. com = new CompoundNBT();
  764. stack.setTag(com);
  765. }
  766. com.putInt("magic", value);
  767. }
  768. else
  769. {
  770. throw new IllegalArgumentException("magic resistance is only for armor");
  771. }
  772. return Void.TYPE;
  773. });
  774. sm.registerFunction("item.clone", (sc, in) -> ((ItemStack) in[0].get(sc)).copy());
  775. // ---------------------------------------------------------------------
  776. // Location-library
  777. // ---------------------------------------------------------------------
  778. sm.registerFunction("loc.new", (sc, in) ->
  779. {
  780. if(in.length >= 6)
  781. {
  782. return new Location((World) in[0].get(sc),
  783. in[1].getDouble(sc), in[2].getDouble(sc), in[3].getDouble(sc),
  784. in[4].getFloat(sc), in[5].getFloat(sc));
  785. }
  786. return new Location((World) in[0].get(sc), in[1].getDouble(sc), in[2].getDouble(sc), in[3].getDouble(sc), 0, 0);
  787. });
  788. sm.registerFunction("loc.getx", (sc, in) -> ((Location) in[0].get(sc)).getX());
  789. sm.registerFunction("loc.gety", (sc, in) -> ((Location) in[0].get(sc)).getY());
  790. sm.registerFunction("loc.getz", (sc, in) -> ((Location) in[0].get(sc)).getZ());
  791. sm.registerFunction("loc.setx", (sc, in) ->
  792. {
  793. ((Location) in[0].get(sc)).setX(in[1].getDouble(sc));
  794. return Void.TYPE;
  795. });
  796. sm.registerFunction("loc.sety", (sc, in) ->
  797. {
  798. ((Location) in[0].get(sc)).setY(in[1].getDouble(sc));
  799. return Void.TYPE;
  800. });
  801. sm.registerFunction("loc.setz", (sc, in) ->
  802. {
  803. ((Location) in[0].get(sc)).setZ(in[1].getDouble(sc));
  804. return Void.TYPE;
  805. });
  806. sm.registerFunction("loc.addx", (sc, in) ->
  807. {
  808. ((Location) in[0].get(sc)).addX(in[1].getDouble(sc));
  809. return Void.TYPE;
  810. });
  811. sm.registerFunction("loc.addy", (sc, in) ->
  812. {
  813. ((Location) in[0].get(sc)).addY(in[1].getDouble(sc));
  814. return Void.TYPE;
  815. });
  816. sm.registerFunction("loc.addz", (sc, in) ->
  817. {
  818. ((Location) in[0].get(sc)).addZ(in[1].getDouble(sc));
  819. return Void.TYPE;
  820. });
  821. sm.registerFunction("loc.setyaw", (sc, in) ->
  822. {
  823. ((Location) in[0].get(sc)).setYaw(in[1].getFloat(sc));
  824. return Void.TYPE;
  825. });
  826. sm.registerFunction("loc.getyaw", (sc, in) -> (double) ((Location) in[0].get(sc)).getYaw());
  827. sm.registerFunction("loc.setpitch", (sc, in) ->
  828. {
  829. ((Location) in[0].get(sc)).setPitch(in[1].getFloat(sc));
  830. return Void.TYPE;
  831. });
  832. sm.registerFunction("loc.getpitch", (sc, in) -> (double) ((Location) in[0].get(sc)).getPitch());
  833. sm.registerFunction("loc.getworld", (sc, in) -> ((Location) in[0].get(sc)).getWorld());
  834. sm.registerFunction("loc.distance", (sc, in) -> ((Location) in[0].get(sc)).getPos().distanceTo(((Location) in[1].get(sc)).getPos()));
  835. sm.registerFunction("loc.mod", (sc, in) -> ((Location) in[0].get(sc)).copyAdd(in[1].getDouble(sc), in[2].getDouble(sc), in[3].getDouble(sc)));
  836. sm.registerFunction("loc.getcoord", (sc, in) ->
  837. {
  838. Location l = (Location) in[0].get(sc);
  839. switch(in[1].getString(sc))
  840. {
  841. case "x":
  842. return l.getX();
  843. case "y":
  844. return l.getY();
  845. case "z":
  846. return l.getZ();
  847. case "bx":
  848. return (double) MathHelper.floor(l.getX());
  849. case "by":
  850. return (double) MathHelper.floor(l.getY());
  851. case "bz":
  852. return (double) MathHelper.floor(l.getZ());
  853. case "w":
  854. return WorldManager.getName(l.getWorld());
  855. default:
  856. return null;
  857. }
  858. });
  859. sm.registerFunction("loc.isbetween", (sc, in) ->
  860. {
  861. Location l1 = (Location) in[0].get(sc);
  862. Location l2 = (Location) in[1].get(sc);
  863. Location l3 = (Location) in[2].get(sc);
  864. return l1.getX() >= Math.min(l2.getX(), l3.getX()) && l1.getX() <= Math.max(l2.getX(), l3.getX()) &&
  865. l1.getY() >= Math.min(l2.getY(), l3.getY()) && l1.getY() <= Math.max(l2.getY(), l3.getY()) &&
  866. l1.getZ() >= Math.min(l2.getZ(), l3.getZ()) && l1.getZ() <= Math.max(l2.getZ(), l3.getZ());
  867. });
  868. sm.registerFunction("loc.sort", (sc, in) ->
  869. {
  870. Location l1 = (Location) in[0].get(sc);
  871. Location l2 = (Location) in[1].get(sc);
  872. if(l1.getX() > l2.getX())
  873. {
  874. double tmp = l1.getX();
  875. l1.setX(l2.getX());
  876. l2.setX(tmp);
  877. }
  878. if(l1.getY() > l2.getY())
  879. {
  880. double tmp = l1.getY();
  881. l1.setY(l2.getY());
  882. l2.setY(tmp);
  883. }
  884. if(l1.getZ() > l2.getZ())
  885. {
  886. double tmp = l1.getZ();
  887. l1.setZ(l2.getZ());
  888. l2.setZ(tmp);
  889. }
  890. return Void.TYPE;
  891. });
  892. // ---------------------------------------------------------------------
  893. // Block-library
  894. // ---------------------------------------------------------------------
  895. sm.registerFunction("block.gettag", (sc, in) -> BlockTags.getCollection().get(new ResourceLocation(in[0].getString(sc))));
  896. sm.registerFunction("block.hastag", (sc, in) -> ((Tag<Block>) in[0].get(sc)).contains((Block) in[1].get(sc)));
  897. sm.registerFunction("block.gettype", (sc, in) ->
  898. {
  899. Location l = (Location) in[0].get(sc);
  900. return l.getWorld().getBlockState(l.getBlockPos()).getBlock().getRegistryName().toString();
  901. });
  902. sm.registerFunction("block.get", (sc, in) ->
  903. {
  904. Location l = (Location) in[0].get(sc);
  905. return l.getWorld().getBlockState(l.getBlockPos()).getBlock();
  906. });
  907. sm.registerFunction("block.getproperty", (sc, in) -> Mapper.getProperty(in[0].getString(sc)));
  908. sm.registerFunction("block.getstate", (sc, in) ->
  909. {
  910. Location l = (Location) in[0].get(sc);
  911. IProperty prop = (IProperty) in[1].get(sc);
  912. BlockState state = l.getWorld().getBlockState(l.getBlockPos());
  913. if(state.has(prop))
  914. {
  915. Object o = l.getWorld().getBlockState(l.getBlockPos()).get(prop);
  916. if(o instanceof Number)
  917. {
  918. return ((Number) o).doubleValue();
  919. }
  920. else if(o instanceof Boolean)
  921. {
  922. return o;
  923. }
  924. return o.toString();
  925. }
  926. return null;
  927. });
  928. sm.registerFunction("block.clone", (sc, in) ->
  929. {
  930. Location l0 = (Location) in[0].get(sc);
  931. Location l1 = (Location) in[1].get(sc);
  932. IWorld w0 = l0.getWorld();
  933. BlockPos pos0 = l0.getBlockPos();
  934. BlockState state = w0.getBlockState(pos0);
  935. TileEntity tileEnt0 = w0.getTileEntity(pos0);
  936. IWorld w1 = l1.getWorld();
  937. BlockPos pos1 = l1.getBlockPos();
  938. w1.setBlockState(pos1, state, 2);
  939. TileEntity tileEnt1 = w1.getTileEntity(pos1);
  940. if(tileEnt0 != null && tileEnt1 != null)
  941. {
  942. CompoundNBT nbt = tileEnt0.write(new CompoundNBT());
  943. nbt.putInt("x", pos1.getX());
  944. nbt.putInt("y", pos1.getY());
  945. nbt.putInt("z", pos1.getZ());
  946. tileEnt1.read(nbt);
  947. tileEnt1.markDirty();
  948. }
  949. return Void.TYPE;
  950. });
  951. sm.registerFunction("block.set", (sc, in) ->
  952. {
  953. Location l = (Location) in[0].get(sc);
  954. BlockStateParser parser = new BlockStateParser(new StringReader(in[1].getString(sc)), true);
  955. BlockState state = parser.parse(true).getState();
  956. l.getWorld().setBlockState(l.getBlockPos(), state, 2);
  957. return Void.TYPE;
  958. });
  959. sm.registerFunction("block.setsign", (sc, in) ->
  960. {
  961. Location l = (Location) in[0].get(sc);
  962. SignTileEntity sign = (SignTileEntity) l.getWorld().getTileEntity(l.getBlockPos());
  963. sign.signText[in[1].getInt(sc)] = new StringTextComponent(SnuviUtils.connect(sc, in, 2));
  964. SUpdateTileEntityPacket packet = sign.getUpdatePacket();
  965. World w = sign.getWorld();
  966. if(w != null)
  967. {
  968. w.getPlayers().stream().filter(p -> p instanceof ServerPlayerEntity)
  969. .forEach(p -> ((ServerPlayerEntity) p).connection.sendPacket(packet));
  970. }
  971. return Void.TYPE;
  972. });
  973. sm.registerFunction("block.getsign", (sc, in) ->
  974. {
  975. Location l = (Location) in[0].get(sc);
  976. SignTileEntity sign = (SignTileEntity) l.getWorld().getTileEntity(l.getBlockPos());
  977. return sign.signText[in[1].getInt(sc)].getString();
  978. });
  979. sm.registerFunction("block.setdoorstatus", (sc, in) ->
  980. {
  981. Location l = (Location) in[0].get(sc);
  982. BlockPos pos = l.getBlockPos();
  983. ((DoorBlock) l.getWorld().getBlockState(pos).getBlock()).toggleDoor(l.getWorld().getWorld(), pos, in[1].getBoolean(sc));
  984. return Void.TYPE;
  985. });
  986. sm.registerFunction("block.getdoorstatus", (sc, in) ->
  987. {
  988. Location l = (Location) in[0].get(sc);
  989. return l.getBlockState().get(DoorBlock.OPEN);
  990. });
  991. sm.registerFunction("block.isdoor", (sc, in) ->
  992. {
  993. Location l = (Location) in[0].get(sc);
  994. return l.getWorld().getBlockState(l.getBlockPos()).getBlock() instanceof DoorBlock;
  995. });
  996. sm.registerFunction("block.issolid", (sc, in) ->
  997. {
  998. BlockState state = getBlockState((Location) in[0].get(sc));
  999. return state.isSolid();
  1000. });
  1001. sm.registerFunction("block.tostack", (sc, in) ->
  1002. {
  1003. Location l = (Location) in[0].get(sc);
  1004. return new ItemStack(l.getBlockState().getBlock().asItem());
  1005. });
  1006. sm.registerFunction("block.getitemamount", (sc, in) ->
  1007. {
  1008. Location l = (Location) in[0].get(sc);
  1009. TileEntity te = l.getWorld().getTileEntity(l.getBlockPos());
  1010. if(te == null || !(te instanceof ChestTileEntity))
  1011. {
  1012. return 0.0d;
  1013. }
  1014. return (double) InventoryUtils.searchInventoryFor((ChestTileEntity) te, (ItemStack) in[2].get(sc), in[1].getBoolean(sc));
  1015. });
  1016. sm.registerFunction("block.getsecchest", (sc, in) ->
  1017. {
  1018. Location l = (Location) in[0].get(sc);
  1019. BlockPos pos = l.getBlockPos();
  1020. BlockState state = l.getWorld().getBlockState(pos);
  1021. ChestType chesttype = state.get(ChestBlock.TYPE);
  1022. if(chesttype == ChestType.SINGLE)
  1023. {
  1024. return null;
  1025. }
  1026. Direction dir = ChestBlock.getDirectionToAttached(state);
  1027. return l.copyAdd(dir.getXOffset(), dir.getYOffset(), dir.getZOffset());
  1028. });
  1029. sm.registerFunction("block.additem", (sc, in) ->
  1030. {
  1031. Location l = (Location) in[0].get(sc);
  1032. ItemStack stack = ((ItemStack) in[1].get(sc));
  1033. TileEntity te = l.getWorld().getTileEntity(l.getBlockPos());
  1034. if(te == null || !(te instanceof ChestTileEntity))
  1035. {
  1036. return stack;
  1037. }
  1038. stack.setCount(InventoryUtils.addToInventory((ChestTileEntity) te, stack));
  1039. return stack;
  1040. });
  1041. sm.registerFunction("block.subitem", (sc, in) ->
  1042. {
  1043. Location l = (Location) in[0].get(sc);
  1044. ItemStack stack = ((ItemStack) in[1].get(sc));
  1045. TileEntity te = l.getWorld().getTileEntity(l.getBlockPos());
  1046. if(te == null || !(te instanceof ChestTileEntity))
  1047. {
  1048. return stack;
  1049. }
  1050. stack.setCount(InventoryUtils.removeFromInventory((ChestTileEntity) te, stack));
  1051. return stack;
  1052. });
  1053. sm.registerFunction("block.grow", (sc, in) ->
  1054. {
  1055. Location l1 = (Location) in[0].get(sc);
  1056. World w = l1.getWorld().getWorld();
  1057. BlockPos pos1 = l1.getBlockPos();
  1058. BlockPos pos2 = ((Location) in[1].get(sc)).getBlockPos();
  1059. int x = Math.min(pos1.getX(), pos2.getX());
  1060. int endX = Math.max(pos1.getX(), pos2.getX());
  1061. int y = Math.min(pos1.getY(), pos2.getY());
  1062. int endY = Math.max(pos1.getY(), pos2.getY());
  1063. int z = Math.min(pos1.getZ(), pos2.getZ());
  1064. int endZ = Math.max(pos1.getZ(), pos2.getZ());
  1065. if(endX - x > 50 || endY - y > 50 || endZ - z > 50)
  1066. {
  1067. throw new IllegalArgumentException("uhh, that area seems way to big for growing plants");
  1068. }
  1069. BlockPos relative;
  1070. BlockState state;
  1071. for(; x <= endX; x++)
  1072. {
  1073. for(; y <= endY; y++)
  1074. {
  1075. for(; z <= endZ; z++)
  1076. {
  1077. relative = new BlockPos(x, y, z);
  1078. state = w.getBlockState(relative);
  1079. if(state.getBlock() instanceof CropsBlock)
  1080. {
  1081. w.setBlockState(relative, state.with(CropsBlock.AGE, 7));
  1082. }
  1083. }
  1084. }
  1085. }
  1086. return Void.TYPE;
  1087. });
  1088. sm.registerFunction("block.setspawnertype", (sc, in) ->
  1089. {
  1090. Location l = (Location) in[0].get(sc);
  1091. MobSpawnerTileEntity spawner = (MobSpawnerTileEntity) l.getWorld().getTileEntity(l.getBlockPos());
  1092. spawner.getSpawnerBaseLogic().setEntityType(EntityType.byKey(in[1].getString(sc)).get());
  1093. return Void.TYPE;
  1094. });
  1095. // ---------------------------------------------------------------------
  1096. // Event-library
  1097. // ---------------------------------------------------------------------
  1098. sm.registerFunction("event.addmovedata", (sc, in) ->
  1099. {
  1100. UUID uuid = in.length >= 5 ? getUUID(in[4].get(sc)) : null;
  1101. PlayerMoveData pmd = new PlayerMoveData(sc, (Location) in[0].get(sc), (Location) in[1].get(sc), in[2].getInt(sc), in[3].getInt(sc), uuid);
  1102. return (double) cec.registerMoveData(pmd);
  1103. });
  1104. sm.registerFunction("event.removemovedata", (sc, in) ->
  1105. {
  1106. cec.removeMoveData(in[0].getInt(sc));
  1107. return Void.TYPE;
  1108. });
  1109. // ---------------------------------------------------------------------
  1110. // damage stuff
  1111. // ---------------------------------------------------------------------
  1112. sm.registerFunction("damage.ismagic", (sc, in) ->
  1113. {
  1114. DamageSource ds = ((DamageSource) in[0].get(sc));
  1115. return ds.isMagicDamage() && !ds.isDamageAbsolute();
  1116. });
  1117. sm.registerFunction("damage.isphysical", (sc, in) ->
  1118. {
  1119. DamageSource ds = ((DamageSource) in[0].get(sc));
  1120. return (!ds.isMagicDamage() || ds.isDamageAbsolute()) && !ds.isUnblockable();
  1121. });
  1122. sm.registerFunction("damage.gettype", (sc, in) -> ((DamageSource) in[0].get(sc)).getDamageType());
  1123. // ---------------------------------------------------------------------
  1124. // entity commands
  1125. // ---------------------------------------------------------------------
  1126. sm.registerFunction("entity.getlook", (sc, in) ->
  1127. {
  1128. Object[] o = new Object[3];
  1129. Vec3d v = ((Entity) in[0].get(sc)).getLookVec();
  1130. o[0] = v.x;
  1131. o[1] = v.y;
  1132. o[2] = v.z;
  1133. return o;
  1134. });
  1135. sm.registerFunction("entity.getlocation", (sc, in) -> new Location((Entity) in[0].get(sc)));
  1136. sm.registerFunction("entity.damage", (sc, in) ->
  1137. {
  1138. // entity.damage(entity, damagefloat, DamageSource);
  1139. if(in.length >= 3)
  1140. {
  1141. ((LivingEntity) in[0].get(sc)).attackEntityFrom((DamageSource) in[2].get(sc), in[1].getFloat(sc));
  1142. return Void.TYPE;
  1143. }
  1144. ((LivingEntity) in[0].get(sc)).attackEntityFrom(DamageSource.GENERIC, in[1].getFloat(sc));
  1145. return Void.TYPE;
  1146. });
  1147. sm.registerFunction("entity.fromsource", (sc, in) ->
  1148. {
  1149. DamageSource ds = (DamageSource) in[0].get(sc);
  1150. Entity ent = ds.getTrueSource();
  1151. if(ent == null)
  1152. {
  1153. return ds.getImmediateSource();
  1154. }
  1155. return ent;
  1156. });
  1157. sm.registerFunction("entity.getdamagesource", (sc, in) ->
  1158. {
  1159. Object o = in[0].get(sc);
  1160. if(o instanceof LivingEntity)
  1161. {
  1162. LivingEntity ent = (LivingEntity) o;
  1163. DamageSource ds;
  1164. if(ent instanceof PlayerEntity)
  1165. {
  1166. ds = DamageSource.causePlayerDamage((PlayerEntity) ent);
  1167. }
  1168. else
  1169. {
  1170. ds = DamageSource.causeMobDamage(ent);
  1171. }
  1172. if(in[1].getBoolean(sc))
  1173. {
  1174. ds.setDamageAllowedInCreativeMode();
  1175. }
  1176. if(in[2].getBoolean(sc))
  1177. {
  1178. ds.setDamageBypassesArmor();
  1179. }
  1180. if(in[3].getBoolean(sc))
  1181. {
  1182. ds.setDamageIsAbsolute();
  1183. }
  1184. if(in[4].getBoolean(sc))
  1185. {
  1186. ds.setExplosion();
  1187. }
  1188. if(in[5].getBoolean(sc))
  1189. {
  1190. ds.setFireDamage();
  1191. }
  1192. if(in[6].getBoolean(sc))
  1193. {
  1194. ds.setMagicDamage();
  1195. }
  1196. if(in[7].getBoolean(sc))
  1197. {
  1198. ds.setProjectile();
  1199. }
  1200. return ds;
  1201. }
  1202. switch(o.toString())
  1203. {
  1204. case "IN_FIRE": return DamageSource.IN_FIRE;
  1205. case "LIGHTNING_BOLT": return DamageSource.LIGHTNING_BOLT;
  1206. case "ON_FIRE": return DamageSource.ON_FIRE;
  1207. case "LAVA": return DamageSource.LAVA;
  1208. case "HOT_FLOOR": return DamageSource.HOT_FLOOR;
  1209. case "IN_WALL": return DamageSource.IN_WALL;
  1210. case "CRAMMING": return DamageSource.CRAMMING;
  1211. case "DROWN": return DamageSource.DROWN;
  1212. case "STARVE": return DamageSource.STARVE;
  1213. case "CACTUS": return DamageSource.CACTUS;
  1214. case "FALL": return DamageSource.FALL;
  1215. case "FLY_INTO_WALL": return DamageSource.FLY_INTO_WALL;
  1216. case "OUT_OF_WORLD": return DamageSource.OUT_OF_WORLD;
  1217. case "GENERIC": return DamageSource.GENERIC;
  1218. case "MAGIC": return DamageSource.MAGIC;
  1219. case "WITHER": return DamageSource.WITHER;
  1220. case "ANVIL": return DamageSource.ANVIL;
  1221. case "FALLING_BLOCK": return DamageSource.FALLING_BLOCK;
  1222. case "DRAGON_BREATH": return DamageSource.DRAGON_BREATH;
  1223. case "FIREWORKS": return DamageSource.FIREWORKS;
  1224. case "THORNS": return DamageSource.causeThornsDamage((Entity) in[1].get(sc));
  1225. }
  1226. return DamageSource.GENERIC;
  1227. });
  1228. sm.registerFunction("entity.gethealth", (sc, in) -> (double) ((LivingEntity) in[0].get(sc)).getHealth());
  1229. sm.registerFunction("entity.sethealth", (sc, in) ->
  1230. {
  1231. ((LivingEntity) in[0].get(sc)).setHealth(in[1].getFloat(sc));
  1232. return Void.TYPE;
  1233. });
  1234. sm.registerFunction("entity.setname", (sc, in) ->
  1235. {
  1236. Entity ent = (Entity) in[0].get(sc);
  1237. ent.setCustomName(new StringTextComponent(in[1].getString(sc)));
  1238. if(in.length >= 3)
  1239. {
  1240. ent.setCustomNameVisible(in[2].getBoolean(sc));
  1241. return Void.TYPE;
  1242. }
  1243. ent.setCustomNameVisible(false);
  1244. return Void.TYPE;
  1245. });
  1246. sm.registerFunction("entity.getname", (sc, in) -> ((Entity) in[0].get(sc)).getDisplayName().getFormattedText());
  1247. sm.registerFunction("entity.throw", (sc, in) ->
  1248. {
  1249. Entity ent = (Entity) in[0].get(sc);
  1250. ent.setMotion(in[1].getDouble(sc), in[2].getDouble(sc), in[3].getDouble(sc));
  1251. if(ent instanceof ServerPlayerEntity)
  1252. {
  1253. ServerPlayerEntity p = (ServerPlayerEntity) ent;
  1254. SEntityVelocityPacket packet = new SEntityVelocityPacket(p);
  1255. p.connection.sendPacket(packet);
  1256. }
  1257. return Void.TYPE;
  1258. });
  1259. sm.registerFunction("entity.teleport", (sc, in) ->
  1260. {
  1261. Entity ent = (Entity) in[0].get(sc);
  1262. Location l = (Location) in[1].get(sc);
  1263. if(l.getWorld() == null)
  1264. {
  1265. throw new IllegalArgumentException("world must not be null");
  1266. }
  1267. if(ent instanceof ServerPlayerEntity)
  1268. {
  1269. ServerPlayerEntity p = (ServerPlayerEntity) ent;
  1270. p.stopRiding();
  1271. if(p.isSleeping())
  1272. {
  1273. p.wakeUpPlayer(true, true, false);
  1274. }
  1275. float yaw = l.getYaw() != 0.0f ? l.getYaw() : ent.rotationYaw;
  1276. float pitch = l.getPitch() != 0.0f ? l.getPitch() : ent.rotationPitch;
  1277. p.teleport((ServerWorld) l.getWorld(), l.getX(), l.getY(), l.getZ(), yaw, pitch);
  1278. }
  1279. else
  1280. {
  1281. if(ent.world != l.getWorld())
  1282. {
  1283. ServerWorld ws = (ServerWorld) l.getWorld();
  1284. ent.changeDimension(ws.getDimension().getType());
  1285. }
  1286. if(l.getYaw() != 0 && l.getPitch() != 0)
  1287. {
  1288. ent.setLocationAndAngles(l.getX(), l.getY(), l.getZ(), l.getYaw(), l.getPitch());
  1289. }
  1290. else
  1291. {
  1292. ent.setLocationAndAngles(l.getX(), l.getY(), l.getZ(), ent.rotationYaw, ent.rotationPitch);
  1293. }
  1294. }
  1295. return Void.TYPE;
  1296. });
  1297. sm.registerFunction("entity.setequip", (sc, in) ->
  1298. {
  1299. LivingEntity liv = (LivingEntity) in[0].get(sc);
  1300. ItemStack stack = ((ItemStack) in[2].get(sc)).copy();
  1301. switch(in[1].getString(sc))
  1302. {
  1303. case "hand":
  1304. liv.setItemStackToSlot(EquipmentSlotType.MAINHAND, stack);
  1305. return Void.TYPE;
  1306. case "head":
  1307. liv.setItemStackToSlot(EquipmentSlotType.HEAD, stack);
  1308. return Void.TYPE;
  1309. case "chest":
  1310. liv.setItemStackToSlot(EquipmentSlotType.CHEST, stack);
  1311. return Void.TYPE;
  1312. case "legs":
  1313. liv.setItemStackToSlot(EquipmentSlotType.LEGS, stack);
  1314. return Void.TYPE;
  1315. case "feet":
  1316. liv.setItemStackToSlot(EquipmentSlotType.FEET, stack);
  1317. return Void.TYPE;
  1318. case "offhand":
  1319. liv.setItemStackToSlot(EquipmentSlotType.OFFHAND, stack);
  1320. }
  1321. return Void.TYPE;
  1322. });
  1323. sm.registerFunction("entity.getequip", (sc, in) ->
  1324. {
  1325. LivingEntity liv = (LivingEntity) in[0].get(sc);
  1326. switch(in[1].getString(sc))
  1327. {
  1328. case "hand":
  1329. return liv.getItemStackFromSlot(EquipmentSlotType.MAINHAND);
  1330. case "head":
  1331. return liv.getItemStackFromSlot(EquipmentSlotType.HEAD);
  1332. case "chest":
  1333. return liv.getItemStackFromSlot(EquipmentSlotType.CHEST);
  1334. case "legs":
  1335. return liv.getItemStackFromSlot(EquipmentSlotType.LEGS);
  1336. case "feet":
  1337. return liv.getItemStackFromSlot(EquipmentSlotType.FEET);
  1338. case "offhand":
  1339. return liv.getItemStackFromSlot(EquipmentSlotType.OFFHAND);
  1340. }
  1341. return ItemStack.EMPTY;
  1342. });
  1343. sm.registerFunction("entity.removeall", (sc, in) ->
  1344. {
  1345. Class<? extends Entity> c = (Class<? extends Entity>) getClass(in[0].getString(sc));
  1346. if(c == Entity.class)
  1347. {
  1348. return Void.TYPE;
  1349. }
  1350. Location l = (Location) in[1].get(sc);
  1351. Utils.getEntities(l.getWorld(), l.getX(), l.getY(), l.getZ(), in[2].getDouble(sc), c).stream().forEach(ent ->
  1352. {
  1353. ent.remove();
  1354. });
  1355. return Void.TYPE;
  1356. });
  1357. sm.registerFunction("entity.remove", (sc, in) ->
  1358. {
  1359. ((Entity) in[0].get(sc)).remove();
  1360. return Void.TYPE;
  1361. });
  1362. sm.registerFunction("entity.setinvulnerable", (sc, in) ->
  1363. {
  1364. ((Entity) in[0].get(sc)).setInvulnerable(in[1].getBoolean(sc));
  1365. return Void.TYPE;
  1366. });
  1367. sm.registerFunction("entity.setsilent", (sc, in) ->
  1368. {
  1369. ((Entity) in[0].get(sc)).setSilent(in[1].getBoolean(sc));
  1370. return Void.TYPE;
  1371. });
  1372. sm.registerFunction("entity.setinvisible", (sc, in) ->
  1373. {
  1374. ((Entity) in[0].get(sc)).setInvisible(in[1].getBoolean(sc));
  1375. return Void.TYPE;
  1376. });
  1377. sm.registerFunction("entity.ride", (sc, in) ->
  1378. {
  1379. ((Entity) in[0].get(sc)).startRiding(((Entity) in[1].get(sc)));
  1380. return Void.TYPE;
  1381. });
  1382. sm.registerFunction("entity.addeffect", (sc, in) ->
  1383. {
  1384. LivingEntity base = (LivingEntity) in[0].get(sc);
  1385. Effect potion = Mapper.getPotion(in[1].getString(sc));
  1386. if(potion == null) // doing this only to prevent EffectInstance doing shit
  1387. {
  1388. throw new IllegalArgumentException("potion does not exist");
  1389. }
  1390. if(base.isPotionActive(potion))
  1391. {
  1392. base.removePotionEffect(potion);
  1393. }
  1394. base.addPotionEffect(new EffectInstance(potion, in[2].getInt(sc), in[3].getInt(sc)));
  1395. return Void.TYPE;
  1396. });
  1397. sm.registerFunction("entity.cleareffects", (sc, in) ->
  1398. {
  1399. ((LivingEntity) in[0].get(sc)).clearActivePotions();
  1400. return Void.TYPE;
  1401. });
  1402. sm.registerFunction("entity.haseffect", (sc, in) -> ((LivingEntity) in[0].get(sc)).isPotionActive(Mapper.getPotion(in[1].getString(sc))));
  1403. sm.registerFunction("entity.explode", (sc, in) ->
  1404. {
  1405. ((CreeperEntity) in[0].get(sc)).ignite();
  1406. return Void.TYPE;
  1407. });
  1408. sm.registerFunction("entity.spawnitemframe", (sc, in) ->
  1409. {
  1410. Location l = ((Location) in[0].get(sc));
  1411. ItemFrameEntity frame = new ItemFrameEntity(l.getWorld().getWorld(), l.getBlockPos(), Direction.byName(in[1].getString(sc)));
  1412. frame.setDisplayedItem(((ItemStack) in[2].get(sc))); // copy happens in internals
  1413. l.getWorld().addEntity(frame);
  1414. return Void.TYPE;
  1415. });
  1416. sm.registerFunction("entity.getitemframe", (sc, in) -> ((ItemFrameEntity) in[0].get(sc)).getDisplayedItem());
  1417. sm.registerFunction("entity.get", (sc, in) ->
  1418. {
  1419. Location l = (Location) in[0].get(sc);
  1420. return Utils.getEntity(l.getWorld(), l.getX(), l.getY(), l.getZ(), in[1].getDouble(sc), getClass(in[2].getString(sc)));
  1421. });
  1422. sm.registerFunction("entity.getpotiontype", (sc, in) -> PotionUtils.getPotionFromItem(((PotionEntity) in[0].get(sc)).getItem()).getRegistryName().toString());
  1423. sm.registerFunction("entity.setgravity", (sc, in) ->
  1424. {
  1425. ((Entity) in[0].get(sc)).setNoGravity(!in[1].getBoolean(sc));
  1426. return Void.TYPE;
  1427. });
  1428. sm.registerFunction("entity.iswet", (sc, in) -> ((Entity) in[0].get(sc)).isWet());
  1429. sm.registerFunction("entity.setpickupdelay", (sc, in) ->
  1430. {
  1431. ((ItemEntity) in[0].get(sc)).setPickupDelay(in[1].getInt(sc));
  1432. return Void.TYPE;
  1433. });
  1434. sm.registerFunction("entity.setage", (sc, in) ->
  1435. {
  1436. ReflectionUtils.setAge((ItemEntity) in[0].get(sc), in[1].getInt(sc));
  1437. return Void.TYPE;
  1438. });
  1439. sm.registerFunction("entity.spawn", (sc, in) ->
  1440. {
  1441. ResourceLocation rl = new ResourceLocation(in[0].getString(sc));
  1442. Location l = (Location) in[1].get(sc);
  1443. ServerWorld sw = (ServerWorld) l.getWorld();
  1444. CompoundNBT compoundnbt = in.length >= 3 ? JsonToNBT.getTagFromJson(in[2].getString(sc)) : new CompoundNBT();
  1445. compoundnbt.putString("id", rl.toString());
  1446. if(EntityType.getKey(EntityType.LIGHTNING_BOLT).equals(rl))
  1447. {
  1448. LightningBoltEntity ent = new LightningBoltEntity(sw, l.getX(), l.getY(), l.getZ(), false);
  1449. sw.addLightningBolt(ent);
  1450. return ent;
  1451. }
  1452. Entity entity = EntityType.func_220335_a(compoundnbt, sw, (ent) ->
  1453. {
  1454. ent.setLocationAndAngles(l.getX(), l.getY(), l.getZ(), ent.rotationYaw, ent.rotationPitch);
  1455. return sw.summonEntity(ent) ? ent : null;
  1456. });
  1457. if(entity != null && entity instanceof MobEntity)
  1458. {
  1459. ((MobEntity) entity).onInitialSpawn(sw, sw.getDifficultyForLocation(new BlockPos(entity)), SpawnReason.COMMAND, null, null);
  1460. }
  1461. return entity;
  1462. });
  1463. sm.registerFunction("entity.near", (sc, in) -> Utils.getLiving((Entity) in[0].get(sc), in[1].getDouble(sc)));
  1464. sm.registerFunction("entity.setspeed", (sc, in) ->
  1465. {
  1466. ((LivingEntity) in[0].get(sc)).getAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).setBaseValue(in[1].getDouble(sc));
  1467. return Void.TYPE;
  1468. });
  1469. sm.registerFunction("entity.setgrowingage", (sc, in) ->
  1470. {
  1471. ((AgeableEntity) in[0].get(sc)).setGrowingAge(in[1].getInt(sc));
  1472. return Void.TYPE;
  1473. });
  1474. sm.registerFunction("entity.gettype", (sc, in) -> ((Entity) in[0].get(sc)).getType().getRegistryName().getPath());
  1475. // ---------------------------------------------------------------------
  1476. // human commands
  1477. // ---------------------------------------------------------------------
  1478. sm.registerFunction("human.spawn", (sc, in) ->
  1479. {
  1480. Location l = (Location) in[0].get(sc);
  1481. World w = l.getWorld().getWorld();
  1482. EntityHuman h = ModEntities.HUMAN.create(w);
  1483. h.setPosition(l.getX(), l.getY(), l.getZ());
  1484. w.addEntity(h);
  1485. return h;
  1486. });
  1487. sm.registerFunction("human.setskin", (sc, in) ->
  1488. {
  1489. ((EntityHuman) in[0].get(sc)).setSkinName(in[1].getString(sc));
  1490. return Void.TYPE;
  1491. });
  1492. sm.registerFunction("human.setscale", (sc, in) ->
  1493. {
  1494. ((EntityHuman) in[0].get(sc)).setScale(in[1].getFloat(sc));
  1495. return Void.TYPE;
  1496. });
  1497. sm.registerFunction("human.setslim", (sc, in) ->
  1498. {
  1499. ((EntityHuman) in[0].get(sc)).setSlim(in[1].getBoolean(sc));
  1500. return Void.TYPE;
  1501. });
  1502. // ---------------------------------------------------------------------
  1503. // GMap-library
  1504. // ---------------------------------------------------------------------
  1505. sm.registerFunction("gmap.removeall", (sc, in) ->
  1506. {
  1507. final String map = in[0].getString(sc);
  1508. scheduler.getWorker().add(() ->
  1509. {
  1510. try
  1511. {
  1512. scriptBank.removeMap(map);
  1513. }
  1514. catch(Exception ex)
  1515. {
  1516. scheduler.scheduleTask(() ->
  1517. {
  1518. sc.getScriptManager().getLogger().print("Worker error", ex, null, sc.getName(), sc, -1);
  1519. });
  1520. }
  1521. });
  1522. return Void.TYPE;
  1523. });
  1524. sm.registerFunction("gmap.add", (sc, in) ->
  1525. {
  1526. final String map = in[0].getString(sc);
  1527. final String key = in[1].getString(sc);
  1528. final String value = in[2].getString(sc);
  1529. scheduler.getWorker().add(() ->
  1530. {
  1531. try
  1532. {
  1533. scriptBank.addMapElement(map, key, value);
  1534. }
  1535. catch(Exception ex)
  1536. {
  1537. scheduler.scheduleTask(() ->
  1538. {
  1539. sc.getScriptManager().getLogger().print("Worker error", ex, null, sc.getName(), sc, -1);
  1540. });
  1541. }
  1542. });
  1543. return Void.TYPE;
  1544. });
  1545. sm.registerFunction("gmap.remove", (sc, in) ->
  1546. {
  1547. final String map = in[0].getString(sc);
  1548. final String key = in[1].getString(sc);
  1549. scheduler.getWorker().add(() ->
  1550. {
  1551. try
  1552. {
  1553. scriptBank.removeMapElement(map, key);
  1554. }
  1555. catch(Exception ex)
  1556. {
  1557. scheduler.scheduleTask(() ->
  1558. {
  1559. sc.getScriptManager().getLogger().print("Worker error", ex, null, sc.getName(), sc, -1);
  1560. });
  1561. }
  1562. });
  1563. return Void.TYPE;
  1564. });
  1565. sm.registerFunction("gmap.get", (sc, in) -> scriptBank.getMapValue(in[0].getString(sc), in[1].getString(sc)));
  1566. sm.registerFunction("gmap.getordefault", (sc, in) ->
  1567. {
  1568. Object o = scriptBank.getMapValue(in[0].getString(sc), in[1].getString(sc));
  1569. if(o == null)
  1570. {
  1571. return in[2].get(sc);
  1572. }
  1573. return o;
  1574. });
  1575. // ---------------------------------------------------------------------
  1576. // GDMap-library
  1577. // ---------------------------------------------------------------------
  1578. sm.registerFunction("gdmap.removeall", (sc, in) ->
  1579. {
  1580. if(in.length >= 2)
  1581. {
  1582. final String map = in[0].getString(sc);
  1583. final String key = in[1].getString(sc);
  1584. scheduler.getWorker().add(() ->
  1585. {
  1586. try
  1587. {
  1588. scriptBank.removeDualMapElement(map, key);
  1589. }
  1590. catch(Exception ex)
  1591. {
  1592. scheduler.scheduleTask(() ->
  1593. {
  1594. sc.getScriptManager().getLogger().print("Worker error", ex, null, sc.getName(), sc, -1);
  1595. });
  1596. }
  1597. });
  1598. }
  1599. else
  1600. {
  1601. final String map = in[0].getString(sc);
  1602. scheduler.getWorker().add(() ->
  1603. {
  1604. try
  1605. {
  1606. scriptBank.removeDualMap(map);
  1607. }
  1608. catch(Exception ex)
  1609. {
  1610. scheduler.scheduleTask(() ->
  1611. {
  1612. sc.getScriptManager().getLogger().print("Worker error", ex, null, sc.getName(), sc, -1);
  1613. });
  1614. }
  1615. });
  1616. }
  1617. return Void.TYPE;
  1618. });
  1619. sm.registerFunction("gdmap.add", (sc, in) ->
  1620. {
  1621. final String map = in[0].getString(sc);
  1622. final String key1 = in[1].getString(sc);
  1623. final String key2 = in[2].getString(sc);
  1624. final String value = in[3].getString(sc);
  1625. scheduler.getWorker().add(() ->
  1626. {
  1627. try
  1628. {
  1629. scriptBank.addDualMapElement(map, key1, key2, value);
  1630. }
  1631. catch(Exception ex)
  1632. {
  1633. scheduler.scheduleTask(() ->
  1634. {
  1635. sc.getScriptManager().getLogger().print("Worker error", ex, null, sc.getName(), sc, -1);
  1636. });
  1637. }
  1638. });
  1639. return Void.TYPE;
  1640. });
  1641. sm.registerFunction("gdmap.remove", (sc, in) ->
  1642. {
  1643. final String map = in[0].getString(sc);
  1644. final String key1 = in[1].getString(sc);
  1645. final String key2 = in[2].getString(sc);
  1646. scheduler.getWorker().add(() ->
  1647. {
  1648. try
  1649. {
  1650. scriptBank.removeDualMapElement(map, key1, key2);
  1651. }
  1652. catch(Exception ex)
  1653. {
  1654. scheduler.scheduleTask(() ->
  1655. {
  1656. sc.getScriptManager().getLogger().print("Worker error", ex, null, sc.getName(), sc, -1);
  1657. });
  1658. }
  1659. });
  1660. return Void.TYPE;
  1661. });
  1662. sm.registerFunction("gdmap.get", (sc, in) ->
  1663. {
  1664. return scriptBank.getDualMapValue(in[0].getString(sc), in[1].getString(sc), in[2].getString(sc));
  1665. });
  1666. sm.registerFunction("gdmap.getordefault", (sc, in) ->
  1667. {
  1668. Object o = scriptBank.getDualMapValue(in[0].getString(sc), in[1].getString(sc), in[2].getString(sc));
  1669. if(o == null)
  1670. {
  1671. return in[3].get(sc);
  1672. }
  1673. return o;
  1674. });
  1675. // ---------------------------------------------------------------------
  1676. // databank library
  1677. // ---------------------------------------------------------------------
  1678. sm.registerFunction("databank.prepare", (sc, in) ->
  1679. {
  1680. PreparedStatement p = dataBank.prepareUnsafeStatement(in[0].getString(sc));
  1681. if(in.length <= 1 || in[1].getBoolean(sc))
  1682. {
  1683. sc.addCloseable(p);
  1684. }
  1685. return p;
  1686. });
  1687. sm.registerFunction("databank.setint", (sc, in) ->
  1688. {
  1689. ((PreparedStatement) in[0].get(sc)).setInt(in[1].getInt(sc), in[2].getInt(sc));
  1690. return Void.TYPE;
  1691. });
  1692. sm.registerFunction("databank.setlong", (sc, in) ->
  1693. {
  1694. ((PreparedStatement) in[0].get(sc)).setLong(in[1].getInt(sc), in[2].getLong(sc));
  1695. return Void.TYPE;
  1696. });
  1697. sm.registerFunction("databank.setdouble", (sc, in) ->
  1698. {
  1699. ((PreparedStatement) in[0].get(sc)).setDouble(in[1].getInt(sc), in[2].getDouble(sc));
  1700. return Void.TYPE;
  1701. });
  1702. sm.registerFunction("databank.setstring", (sc, in) ->
  1703. {
  1704. ((PreparedStatement) in[0].get(sc)).setString(in[1].getInt(sc), in[2].getString(sc));
  1705. return Void.TYPE;
  1706. });
  1707. sm.registerFunction("databank.setbool", (sc, in) ->
  1708. {
  1709. ((PreparedStatement) in[0].get(sc)).setBoolean(in[1].getInt(sc), in[2].getBoolean(sc));
  1710. return Void.TYPE;
  1711. });
  1712. sm.registerFunction("databank.getint", (sc, in) -> (double) ((ResultSet) in[0].get(sc)).getInt(in[1].getInt(sc)));
  1713. sm.registerFunction("databank.getlong", (sc, in) -> (double) ((ResultSet) in[0].get(sc)).getLong(in[1].getInt(sc)));
  1714. sm.registerFunction("databank.getdouble", (sc, in) -> ((ResultSet) in[0].get(sc)).getDouble(in[1].getInt(sc)));
  1715. sm.registerFunction("databank.getstring", (sc, in) -> ((ResultSet) in[0].get(sc)).getString(in[1].getInt(sc)));
  1716. sm.registerFunction("databank.getbool", (sc, in) -> ((ResultSet) in[0].get(sc)).getBoolean(in[1].getInt(sc)));
  1717. sm.registerFunction("databank.execute", (sc, in) -> ((PreparedStatement) in[0].get(sc)).executeQuery());
  1718. sm.registerFunction("databank.workerexecute", (sc, in) ->
  1719. {
  1720. final PreparedStatement p = (PreparedStatement) in[0].get(sc);
  1721. scheduler.getWorker().add(() ->
  1722. {
  1723. try
  1724. {
  1725. p.execute();
  1726. }
  1727. catch(SQLException ex)
  1728. {
  1729. scheduler.scheduleTask(() ->
  1730. {
  1731. sc.getScriptManager().getLogger().print("Worker error", ex, null, sc.getName(), sc, -1);
  1732. });
  1733. }
  1734. sc.removeCloseable(p);
  1735. try
  1736. {
  1737. p.close();
  1738. }
  1739. catch(SQLException ex)
  1740. {
  1741. scheduler.scheduleTask(() ->
  1742. {
  1743. sc.getScriptManager().getLogger().print("Worker error", ex, null, sc.getName(), sc, -1);
  1744. });
  1745. }
  1746. });
  1747. return Void.TYPE;
  1748. });
  1749. sm.registerFunction("databank.next", (sc, in) -> ((ResultSet) in[0].get(sc)).next());
  1750. sm.registerFunction("databank.close", (sc, in) ->
  1751. {
  1752. AutoCloseable auto = (AutoCloseable) in[0].get(sc);
  1753. auto.close();
  1754. sc.removeCloseable(auto);
  1755. return Void.TYPE;
  1756. });
  1757. // ---------------------------------------------------------------------
  1758. // Plot-library
  1759. // ---------------------------------------------------------------------
  1760. sm.registerFunction("plot.get", (sc, in) ->
  1761. {
  1762. Location l = (Location) in[0].get(sc);
  1763. return plots.getPlots(l.getWorld(), l.getBlockPos());
  1764. });
  1765. sm.registerFunction("plot.check", (sc, in) ->
  1766. {
  1767. Location l = (Location) in[0].get(sc);
  1768. ModEntityPlayerMP p = (ModEntityPlayerMP) in[1].get(sc);
  1769. int flags = in[2].getInt(sc);
  1770. boolean empty = in[3].getBoolean(sc);
  1771. return plots.canDoSomething(l.getWorld(), l.getBlockPos(), p, flags, empty);
  1772. });
  1773. sm.registerFunction("plot.setflags", (sc, in) ->
  1774. {
  1775. Plot p = (Plot) in[0].get(sc);
  1776. p.setFlag(in[1].getInt(sc), in[2].getBoolean(sc));
  1777. return Void.TYPE;
  1778. });
  1779. sm.registerFunction("plot.hasflags", (sc, in) -> ((Plot) in[0].get(sc)).hasFlags(in[1].getInt(sc)));
  1780. sm.registerFunction("plot.getflags", (sc, in) -> (double) ((Plot) in[0].get(sc)).getFlags());
  1781. sm.registerFunction("plot.getowners", (sc, in) -> ((Plot) in[0].get(sc)).getOwners());
  1782. sm.registerFunction("plot.add", (sc, in) ->
  1783. {
  1784. Location l1 = (Location) in[0].get(sc);
  1785. Location l2 = (Location) in[1].get(sc);
  1786. if(l1.getWorld() != l2.getWorld())
  1787. {
  1788. throw new IllegalArgumentException("worlds not equal for locations");
  1789. }
  1790. return plots.add(l1.getWorld(), l1.getBlockPos(), l2.getBlockPos());
  1791. });
  1792. sm.registerFunction("plot.remove", (sc, in) ->
  1793. {
  1794. plots.remove((IWorld) in[1].get(sc), (Plot) in[0].get(sc));
  1795. return Void.TYPE;
  1796. });
  1797. sm.registerFunction("plot.getname", (sc, in) -> ((Plot) in[0].get(sc)).getName());
  1798. sm.registerFunction("plot.setname", (sc, in) ->
  1799. {
  1800. ((Plot) in[0].get(sc)).setName(in[1].getString(sc));
  1801. return Void.TYPE;
  1802. });
  1803. sm.registerFunction("plot.getid", (sc, in) -> (double) ((Plot) in[0].get(sc)).getId());
  1804. sm.registerFunction("plot.iterator", (sc, in) ->
  1805. {
  1806. IWorld word = (IWorld) in[0].get(sc);
  1807. if(in.length >= 2)
  1808. {
  1809. plots.getIterator(word, getUUID(in[1].get(sc)));
  1810. }
  1811. return plots.getIterator(word);
  1812. });
  1813. sm.registerFunction("plot.intersecting", (sc, in) ->
  1814. plots.getIntersectingPlots((IWorld) in[0].get(sc), in[1].getInt(sc),
  1815. in[2].getInt(sc), in[3].getInt(sc), in[4].getInt(sc),
  1816. in[5].getInt(sc), in[6].getInt(sc)));
  1817. sm.registerFunction("plot.getminx", (sc, in) -> (double) ((Plot) in[0].get(sc)).getMinX());
  1818. sm.registerFunction("plot.getminy", (sc, in) -> (double) ((Plot) in[0].get(sc)).getMinY());
  1819. sm.registerFunction("plot.getminz", (sc, in) -> (double) ((Plot) in[0].get(sc)).getMinZ());
  1820. sm.registerFunction("plot.getmaxx", (sc, in) -> (double) ((Plot) in[0].get(sc)).getMaxX());
  1821. sm.registerFunction("plot.getmaxy", (sc, in) -> (double) ((Plot) in[0].get(sc)).getMaxY());
  1822. sm.registerFunction("plot.getmaxz", (sc, in) -> (double) ((Plot) in[0].get(sc)).getMaxZ());
  1823. // ---------------------------------------------------------------------
  1824. // block protection library
  1825. // ---------------------------------------------------------------------
  1826. sm.registerFunction("protect.hasaccess", (sc, in) ->
  1827. {
  1828. Location l = (Location) in[0].get(sc);
  1829. BlockPos pos = l.getBlockPos();
  1830. return blockProtection.hasAccess(pos, l.getWorld(), (ModEntityPlayerMP) in[1].get(sc));
  1831. });
  1832. // ---------------------------------------------------------------------
  1833. // Script-library
  1834. // ---------------------------------------------------------------------
  1835. sm.registerFunction("script.playerstolist", (sc, in) -> new ArrayList<>(scripts.getPlayerList(sc.getId())));
  1836. sm.registerFunction("script.getplayeramount", (sc, in) -> (double) scripts.getPlayerList(sc.getId()).size());
  1837. sm.registerFunction("script.start", (sc, in) ->
  1838. {
  1839. Object o = in[0].get(sc);
  1840. if(o instanceof PlayerEntity)
  1841. {
  1842. String[] names = new String[in.length - 1];
  1843. for(int i = 1; i < in.length; i++)
  1844. {
  1845. names[i - 1] = in[i].getString(sc);
  1846. }
  1847. return scripts.startPlayerScript((PlayerEntity) in[0].get(sc), names);
  1848. }
  1849. String[] names = new String[in.length];
  1850. names[0] = o.toString();
  1851. for(int i = 1; i < in.length; i++)
  1852. {
  1853. names[i] = in[i].getString(sc);
  1854. }
  1855. scripts.startScript(names);
  1856. return true;
  1857. });
  1858. sm.registerFunction("script.join", (sc, in) -> scripts.registerPlayer((Script) in[0].get(sc), (PlayerEntity) in[1].get(sc)));
  1859. sm.registerFunction("script.kick", (sc, in) -> scripts.unregisterPlayer(sc, (PlayerEntity) in[0].get(sc)));
  1860. sm.registerFunction("script.getleader", (sc, in) ->
  1861. {
  1862. List<UUID> players = scripts.getPlayerList(sc.getId());
  1863. if(players.isEmpty())
  1864. {
  1865. return null;
  1866. }
  1867. return server.getPlayerList().getPlayerByUUID(players.get(0));
  1868. });
  1869. // ---------------------------------------------------------------------
  1870. // Scoreboard-library
  1871. // ---------------------------------------------------------------------
  1872. sm.registerFunction("sb.add", (sc, in) ->
  1873. {
  1874. int id = in[1].getInt(sc);
  1875. String message = SnuviUtils.connect(sc, in, 2);
  1876. doForGroup(server, scripts, perms, in[0].get(sc), sc, p -> ((ModEntityPlayerMP) p).getScoreboard().addText(id, message));
  1877. return Void.TYPE;
  1878. });
  1879. sm.registerFunction("sb.remove", (sc, in) ->
  1880. {
  1881. int id = in[1].getInt(sc);
  1882. doForGroup(server, scripts, perms, in[0].get(sc), sc, p -> ((ModEntityPlayerMP) p).getScoreboard().removeText(id));
  1883. return Void.TYPE;
  1884. });
  1885. sm.registerFunction("sb.reset", (sc, in) ->
  1886. {
  1887. doForGroup(server, scripts, perms, in[0].get(sc), sc, p -> ((ModEntityPlayerMP) p).getScoreboard().clear((ModEntityPlayerMP) p));
  1888. return Void.TYPE;
  1889. });
  1890. // ---------------------------------------------------------------------
  1891. // Display-library
  1892. // ---------------------------------------------------------------------
  1893. sm.registerFunction("display.add", (sc, in) ->
  1894. {
  1895. byte id = in[1].getByte(sc);
  1896. String message = SnuviUtils.connect(sc, in, 2);
  1897. doForGroup(server, scripts, perms, in[0].get(sc), sc, p -> ModPacketHandler.sendToDisplay((ServerPlayerEntity) p, (byte) 1, id, message));
  1898. return Void.TYPE;
  1899. });
  1900. sm.registerFunction("display.remove", (sc, in) ->
  1901. {
  1902. byte id = in[1].getByte(sc);
  1903. doForGroup(server, scripts, perms, in[0].get(sc), sc, p -> ModPacketHandler.sendToDisplay((ServerPlayerEntity) p, (byte) 2, id, ""));
  1904. return Void.TYPE;
  1905. });
  1906. sm.registerFunction("display.reset", (sc, in) ->
  1907. {
  1908. doForGroup(server, scripts, perms, in[0].get(sc), sc, p -> ModPacketHandler.sendToDisplay((ServerPlayerEntity) p, (byte) 3, (byte) -1, ""));
  1909. return Void.TYPE;
  1910. });
  1911. // ---------------------------------------------------------------------
  1912. // status - library
  1913. // ---------------------------------------------------------------------
  1914. sm.registerFunction("status.add", (sc, in) ->
  1915. {
  1916. byte index = in[1].getByte(sc);
  1917. String message = SnuviUtils.connect(sc, in, 2);
  1918. doForGroup(server, scripts, perms, in[0].get(sc), sc, p -> ModPacketHandler.addStatus((ServerPlayerEntity) p, index, message));
  1919. return Void.TYPE;
  1920. });
  1921. sm.registerFunction("status.addtimed", (sc, in) ->
  1922. {
  1923. byte index = in[1].getByte(sc);
  1924. int time = in[2].getInt(sc);
  1925. String message = SnuviUtils.connect(sc, in, 3);
  1926. doForGroup(server, scripts, perms, in[0].get(sc), sc, p -> ModPacketHandler.addTimedStatus((ServerPlayerEntity) p, index, message, time));
  1927. return Void.TYPE;
  1928. });
  1929. sm.registerFunction("status.remove", (sc, in) ->
  1930. {
  1931. byte index = in[1].getByte(sc);
  1932. doForGroup(server, scripts, perms, in[0].get(sc), sc, p -> ModPacketHandler.removeStatus((ServerPlayerEntity) p, index));
  1933. return Void.TYPE;
  1934. });
  1935. sm.registerFunction("status.reset", (sc, in) ->
  1936. {
  1937. doForGroup(server, scripts, perms, in[0].get(sc), sc, p -> ModPacketHandler.clearStatus((ServerPlayerEntity) p));
  1938. return Void.TYPE;
  1939. });
  1940. // ---------------------------------------------------------------------
  1941. // ItemStack-Display-library
  1942. // ---------------------------------------------------------------------
  1943. sm.registerFunction("stacks.set", (sc, in) ->
  1944. {
  1945. byte index = in[1].getByte(sc);
  1946. if(index < 0 || index >= 9)
  1947. {
  1948. throw new IllegalArgumentException("index must be beetween 0 and 8, given: " + index);
  1949. }
  1950. int iconIndex = in[2].getInt(sc);
  1951. int count = in[3].getInt(sc);
  1952. doForGroup(server, scripts, perms, in[0].get(sc), sc, p -> ModPacketHandler.setItemStackIndex((ServerPlayerEntity) p, index, iconIndex, count));
  1953. return Void.TYPE;
  1954. });
  1955. sm.registerFunction("stacks.clearindex", (sc, in) ->
  1956. {
  1957. byte index = in[1].getByte(sc);
  1958. if(index < 0 || index >= 9)
  1959. {
  1960. throw new IllegalArgumentException("index must be beetween 0 and 8, given: " + index);
  1961. }
  1962. doForGroup(server, scripts, perms, in[0].get(sc), sc, p -> ModPacketHandler.setItemStackIndex((ServerPlayerEntity) p, index, 0, 0));
  1963. return Void.TYPE;
  1964. });
  1965. sm.registerFunction("stacks.clear", (sc, in) ->
  1966. {
  1967. doForGroup(server, scripts, perms, in[0].get(sc), sc, p -> ModPacketHandler.clearItemStacks((ServerPlayerEntity) p));
  1968. return Void.TYPE;
  1969. });
  1970. sm.registerFunction("stacks.setactive", (sc, in) ->
  1971. {
  1972. boolean active = in[1].getBoolean(sc);
  1973. doForGroup(server, scripts, perms, in[0].get(sc), sc, p -> ModPacketHandler.setItemStackActive((ServerPlayerEntity) p, active));
  1974. return Void.TYPE;
  1975. });
  1976. // ---------------------------------------------------------------------
  1977. // Head-library
  1978. // ---------------------------------------------------------------------
  1979. sm.registerFunction("head.add", (sc, in) ->
  1980. {
  1981. byte id = in[1].getByte(sc);
  1982. String name = in[2].getString(sc);
  1983. int x = in[3].getInt(sc);
  1984. int y = in[4].getInt(sc);
  1985. byte scale = in[5].getByte(sc);
  1986. doForGroup(server, scripts, perms, in[0].get(sc), sc, p -> ModPacketHandler.sendToHead((ServerPlayerEntity) p, (byte) 1, id, name, x, y, scale));
  1987. return Void.TYPE;
  1988. });
  1989. sm.registerFunction("head.remove", (sc, in) ->
  1990. {
  1991. byte id = in[1].getByte(sc);
  1992. doForGroup(server, scripts, perms, in[0].get(sc), sc, p -> ModPacketHandler.sendToHead((ServerPlayerEntity) p, (byte) 2, id, "", -1, -1, (byte) -1));
  1993. return Void.TYPE;
  1994. });
  1995. sm.registerFunction("head.reset", (sc, in) ->
  1996. {
  1997. doForGroup(server, scripts, perms, in[0].get(sc), sc, p -> ModPacketHandler.sendToHead((ServerPlayerEntity) p, (byte) 3, (byte) -1, "", -1, -1, (byte) -1));
  1998. return Void.TYPE;
  1999. });
  2000. // ---------------------------------------------------------------------
  2001. // particle library
  2002. // ---------------------------------------------------------------------
  2003. sm.registerFunction("particle.get", (sc, in) ->
  2004. {
  2005. IParticleData data = Mapper.getParticle(in[0].getString(sc));
  2006. if(data == ParticleTypes.BLOCK || data == ParticleTypes.FALLING_DUST)
  2007. {
  2008. data = new BlockParticleData((ParticleType<BlockParticleData>) data, Mapper.getBlock(in[1].getString(sc)).getDefaultState());
  2009. }
  2010. else if(data == ParticleTypes.DUST)
  2011. {
  2012. data = new RedstoneParticleData(in[1].getFloat(sc), in[2].getFloat(sc), in[3].getFloat(sc), in[4].getFloat(sc));
  2013. }
  2014. else if(data == ParticleTypes.ITEM)
  2015. {
  2016. data = new ItemParticleData((ParticleType<ItemParticleData>) data, new ItemStack(Mapper.getItem(in[1].getString(sc))));
  2017. }
  2018. return data;
  2019. });
  2020. sm.registerFunction("particle.spawn", (sc, in) ->
  2021. {
  2022. Location l = ((Location) in[0].get(sc));
  2023. IParticleData data = ((IParticleData) in[1].get(sc));
  2024. int count = in.length >= 3 ? in[2].getInt(sc) : 1;
  2025. double speed = in.length >= 4 ? in[3].getDouble(sc) : 0.0;
  2026. double offX = in.length >= 5 ? in[4].getDouble(sc) : 0.0;
  2027. double offY = in.length >= 6 ? in[5].getDouble(sc) : 0.0;
  2028. double offZ = in.length >= 7 ? in[6].getDouble(sc) : 0.0;
  2029. ((ServerWorld) l.getWorld()).spawnParticle(data, l.getX(), l.getY(), l.getZ(), count, offX, offY, offZ, speed);
  2030. return Void.TYPE;
  2031. });
  2032. sm.registerFunction("particle.spawncircle", (sc, in) ->
  2033. {
  2034. Location l = ((Location) in[0].get(sc));
  2035. IParticleData data = ((IParticleData) in[1].get(sc));
  2036. int instances = in[2].getInt(sc);
  2037. double radius = in[3].getDouble(sc);
  2038. int count = in.length >= 5 ? in[4].getInt(sc) : 1;
  2039. double speed = in.length >= 6 ? in[5].getDouble(sc) : 0.0;
  2040. double offX = in.length >= 7 ? in[6].getDouble(sc) : 0.0;
  2041. double offY = in.length >= 8 ? in[7].getDouble(sc) : 0.0;
  2042. double offZ = in.length >= 9 ? in[8].getDouble(sc) : 0.0;
  2043. double x = l.getX();
  2044. double y = l.getY();
  2045. double z = l.getZ();
  2046. ServerWorld sw = (ServerWorld) l.getWorld();
  2047. double angle = 2 * Math.PI / instances;
  2048. for(int i = 0; i < instances; i++)
  2049. {
  2050. sw.spawnParticle(data, x + Math.cos(i * angle) * radius, y, z + Math.sin(i * angle) * radius, count, offX, offY, offZ, speed);
  2051. }
  2052. return Void.TYPE;
  2053. });
  2054. sm.registerFunction("particle.spawnline", (sc, in) ->
  2055. {
  2056. Location l = ((Location) in[0].get(sc));
  2057. IParticleData data = ((IParticleData) in[1].get(sc));
  2058. int instances = in[2].getInt(sc);
  2059. double stepX = in[3].getDouble(sc);
  2060. double stepY = in[4].getDouble(sc);
  2061. double stepZ = in[5].getDouble(sc);
  2062. int count = in.length >= 7 ? in[6].getInt(sc) : 1;
  2063. double speed = in.length >= 8 ? in[7].getDouble(sc) : 0.0;
  2064. double offX = in.length >= 9 ? in[8].getDouble(sc) : 0.0;
  2065. double offY = in.length >= 10 ? in[9].getDouble(sc) : 0.0;
  2066. double offZ = in.length >= 11 ? in[10].getDouble(sc) : 0.0;
  2067. double x = l.getX();
  2068. double y = l.getY();
  2069. double z = l.getZ();
  2070. ServerWorld sw = (ServerWorld) l.getWorld();
  2071. for(int i = 0; i < instances; i++)
  2072. {
  2073. sw.spawnParticle(data, x + i * stepX, y + i * stepY, z + i * stepZ, count, offX, offY, offZ, speed);
  2074. }
  2075. return Void.TYPE;
  2076. });
  2077. // ---------------------------------------------------------------------
  2078. // particle library
  2079. // ---------------------------------------------------------------------
  2080. sm.registerFunction("sound.get", (sc, in) -> Mapper.getSound(in[0].getString(sc)));
  2081. sm.registerFunction("sound.spawn", (sc, in) ->
  2082. {
  2083. Location l = (Location) in[0].get(sc);
  2084. ServerWorld sw = (ServerWorld) l.getWorld();
  2085. float volume = in.length >= 3 ? in[2].getFloat(sc) : 1.0f;
  2086. float pitch = in.length >= 4 ? in[3].getFloat(sc) : (sw.rand.nextFloat() * 0.1f + 0.9f);
  2087. sw.playSound(null, l.getX(), l.getY(), l.getZ(), (SoundEvent) in[1].get(sc), SoundCategory.MASTER, volume, pitch);
  2088. return Void.TYPE;
  2089. });
  2090. sm.registerFunction("sound.spawnforplayer", (sc, in) ->
  2091. {
  2092. ServerPlayerEntity p = (ServerPlayerEntity) in[0].get(sc);
  2093. float volume = in.length >= 3 ? in[2].getFloat(sc) : 1.0f;
  2094. float pitch = in.length >= 4 ? in[3].getFloat(sc) : (p.world.rand.nextFloat() * 0.1f + 0.9f);
  2095. p.playSound((SoundEvent) in[1].get(sc), volume, pitch);
  2096. return Void.TYPE;
  2097. });
  2098. // ---------------------------------------------------------------------
  2099. // Inventory-library
  2100. // ---------------------------------------------------------------------
  2101. sm.registerFunction("inv.new", (sc, in) -> new ModInventory(in[0].getString(sc)));
  2102. sm.registerFunction("inv.getid", (sc, in) -> (double) ((ModInventory) in[0].get(sc)).getModId());
  2103. sm.registerFunction("inv.loadblock", (sc, in) ->
  2104. {
  2105. Location l = (Location) in[0].get(sc);
  2106. ChestTileEntity chest = (ChestTileEntity) l.getWorld().getTileEntity(l.getBlockPos());
  2107. int size = chest.getSizeInventory();
  2108. if(size % 9 != 0)
  2109. {
  2110. size /= 9;
  2111. size++;
  2112. size *= 9;
  2113. }
  2114. ModInventory inv = new ModInventory(size);
  2115. for(int i = 0; i < chest.getSizeInventory(); i++)
  2116. {
  2117. inv.setInventorySlotContents(i, chest.getStackInSlot(i).copy());
  2118. }
  2119. return inv;
  2120. });
  2121. sm.registerFunction("inv.setitem", (sc, in) ->
  2122. {
  2123. ((IInventory) in[0].get(sc)).setInventorySlotContents(in[1].getInt(sc), (ItemStack) in[2].get(sc));
  2124. return Void.TYPE;
  2125. });
  2126. sm.registerFunction("inv.getitem", (sc, in) -> ((IInventory) in[0].get(sc)).getStackInSlot(in[1].getInt(sc)));
  2127. sm.registerFunction("inv.open", (sc, in) ->
  2128. {
  2129. CustomContainer.openForPlayer((ServerPlayerEntity) in[1].get(sc), (ModInventory) in[0].get(sc), in[2].getString(sc), sc);
  2130. return Void.TYPE;
  2131. });
  2132. sm.registerFunction("inv.close", (sc, in) ->
  2133. {
  2134. ((PlayerEntity) in[0].get(sc)).closeScreen();
  2135. return Void.TYPE;
  2136. });
  2137. sm.registerFunction("inv.update", (sc, in) ->
  2138. {
  2139. ServerPlayerEntity p = (ServerPlayerEntity) in[0].get(sc);
  2140. NonNullList<ItemStack> list = NonNullList.<ItemStack>create();
  2141. int size = p.openContainer.inventorySlots.size();
  2142. for(int j = 0; j < size; j++)
  2143. {
  2144. ItemStack itemstack = p.openContainer.inventorySlots.get(j).getStack();
  2145. list.add(itemstack.isEmpty() ? ItemStack.EMPTY : itemstack);
  2146. }
  2147. p.sendAllContents(p.openContainer, list);
  2148. return Void.TYPE;
  2149. });
  2150. // ---------------------------------------------------------------------
  2151. // Read-library
  2152. // ---------------------------------------------------------------------
  2153. sm.registerFunction("read.player", (sc, in) -> Utils.getPlayerByName(server, in[0].getString(sc)));
  2154. sm.registerFunction("read.location", (sc, in) -> new Location(server, in[0].getString(sc)));
  2155. sm.registerFunction("read.item", (sc, in) ->
  2156. {
  2157. String s = in[0].getString(sc);
  2158. if(s.startsWith("{"))
  2159. {
  2160. String left = SnuviUtils.connect(sc, in, 1);
  2161. return ItemStackUtils.getStackFromNbtString(s + left);
  2162. }
  2163. Item item = Mapper.getItem(s);
  2164. int amount = in.length >= 2 ? in[1].getInt(sc) : 1;
  2165. ItemStack stack = new ItemStack(item, amount);
  2166. if(in.length >= 3)
  2167. {
  2168. stack.setDisplayName(new StringTextComponent(in[2].getString(sc)));
  2169. }
  2170. if(in.length >= 4)
  2171. {
  2172. for(int i = 3; i < in.length; i++)
  2173. {
  2174. ItemStackUtils.addLore(stack, in[i].getString(sc));
  2175. }
  2176. }
  2177. return stack;
  2178. });
  2179. sm.registerFunction("read.spawnmob", (sc, in) ->
  2180. {
  2181. Location l = (Location) in[0].get(sc);
  2182. ServerWorld sw = (ServerWorld) l.getWorld();
  2183. CompoundNBT compoundnbt = JsonToNBT.getTagFromJson(SnuviUtils.connect(sc, in, 1));
  2184. Entity entity = EntityType.func_220335_a(compoundnbt, sw, (ent) ->
  2185. {
  2186. ent.setLocationAndAngles(l.getX(), l.getY(), l.getZ(), ent.rotationYaw, ent.rotationPitch);
  2187. return sw.summonEntity(ent) ? ent : null;
  2188. });
  2189. return entity;
  2190. });
  2191. // ---------------------------------------------------------------------
  2192. // Text-library
  2193. // ---------------------------------------------------------------------
  2194. sm.registerFunction("text.location", (sc, in) -> ((Location) in[0].get(sc)).toString());
  2195. sm.registerFunction("text.locationblock", (sc, in) -> ((Location) in[0].get(sc)).toBlockString());
  2196. sm.registerFunction("text.item", (sc, in) -> ItemStackUtils.getNbtString((ItemStack) in[0].get(sc)));
  2197. sm.registerFunction("text.click", (sc, in) ->
  2198. {
  2199. Object message = in[0].get(sc);
  2200. ITextComponent text;
  2201. if(message instanceof ITextComponent)
  2202. {
  2203. text = (ITextComponent) message;
  2204. }
  2205. else
  2206. {
  2207. text = new StringTextComponent(String.valueOf(message));
  2208. }
  2209. Style style = text.getStyle();
  2210. style.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, in[1].getString(sc)));
  2211. return text;
  2212. });
  2213. sm.registerFunction("text.hover", (sc, in) ->
  2214. {
  2215. Object message = in[0].get(sc);
  2216. ITextComponent text;
  2217. if(message instanceof ITextComponent)
  2218. {
  2219. text = (ITextComponent) message;
  2220. }
  2221. else
  2222. {
  2223. text = new StringTextComponent(String.valueOf(message));
  2224. }
  2225. Style style = text.getStyle();
  2226. style.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new StringTextComponent(in[1].getString(sc))));
  2227. return text;
  2228. });
  2229. sm.registerFunction("text.link", (sc, in) ->
  2230. {
  2231. StringTextComponent text = new StringTextComponent(in[0].getString(sc));
  2232. Style style = text.getStyle();
  2233. style.setColor(TextFormatting.RED);
  2234. style.setBold(true);
  2235. style.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, in[1].getString(sc)));
  2236. return text;
  2237. });
  2238. sm.registerFunction("text.copytext", (sc, in) ->
  2239. {
  2240. String s = in[1].getString(sc);
  2241. //s = s.replace('{', 'Ɛ');
  2242. //s = s.replace('}', 'Ƒ');
  2243. //s = s.replace('[', 'ƒ');
  2244. //s = s.replace(']', 'Ɠ');
  2245. //s = s.replace('(', 'ƕ');
  2246. //s = s.replace(')', 'Ɩ');
  2247. s = s.replace(" ", "%20");
  2248. //s = s.replace('\'', 'Ɩ');
  2249. //s = s.replace('"', 'Ɩ');
  2250. //s = s.replace("\\", "");
  2251. StringTextComponent text = new StringTextComponent(in[0].getString(sc));
  2252. Style style = text.getStyle();
  2253. style.setColor(TextFormatting.RED);
  2254. style.setBold(true);
  2255. style.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, "http://minecraft.hammerle.me/showtext.php/?text=" + s));
  2256. return text;
  2257. });
  2258. sm.registerFunction("text.entity", (sc, in) ->
  2259. {
  2260. CompoundNBT tag = new CompoundNBT();
  2261. ((Entity) in[0].get(sc)).writeWithoutTypeId(tag);
  2262. return tag.toString();
  2263. });
  2264. // ---------------------------------------------------------------------
  2265. // worker library
  2266. // ---------------------------------------------------------------------
  2267. sm.registerFunction("worker.haswork", (sc, in) -> scheduler.getWorker().hasWork());
  2268. // ---------------------------------------------------------------------
  2269. // ban library
  2270. // ---------------------------------------------------------------------
  2271. sm.registerFunction("ban.kick", (sc, in) ->
  2272. {
  2273. ((ServerPlayerEntity) in[0].get(sc)).connection.disconnect(new StringTextComponent(in[1].getString(sc)));
  2274. return Void.TYPE;
  2275. });
  2276. sm.registerFunction("ban.add", (sc, in) ->
  2277. {
  2278. GameProfile gp = server.getPlayerProfileCache().getProfileByUUID(getUUID(in[0].get(sc)));
  2279. String reason = in[1].getString(sc);
  2280. String banner = in[2].getString(sc);
  2281. ProfileBanEntry entry;
  2282. if(in.length >= 4)
  2283. {
  2284. GregorianCalendar calender = (GregorianCalendar) in[3].get(sc);
  2285. Date d = new Date(calender.getTimeInMillis());
  2286. entry = new ProfileBanEntry(gp, null, banner, d, reason);
  2287. }
  2288. else
  2289. {
  2290. entry = new ProfileBanEntry(gp, null, banner, null, reason);
  2291. }
  2292. server.getPlayerList().getBannedPlayers().addEntry(entry);
  2293. return Void.TYPE;
  2294. });
  2295. sm.registerFunction("ban.remove", (sc, in) ->
  2296. {
  2297. GameProfile gp = server.getPlayerProfileCache().getProfileByUUID(getUUID(in[0].get(sc)));
  2298. server.getPlayerList().getBannedPlayers().removeEntry(gp);
  2299. return Void.TYPE;
  2300. });
  2301. // ---------------------------------------------------------------------
  2302. // player data
  2303. // ---------------------------------------------------------------------
  2304. sm.registerFunction("data.set", (sc, in) ->
  2305. {
  2306. ((ModEntityPlayerMP) in[0].get(sc)).setVar(in[1].getString(sc), in[2].get(sc));
  2307. return Void.TYPE;
  2308. });
  2309. sm.registerFunction("data.settimer", (sc, in) ->
  2310. {
  2311. ((ModEntityPlayerMP) in[0].get(sc)).setTimer(in[1].getString(sc), in[2].getInt(sc));
  2312. return Void.TYPE;
  2313. });
  2314. sm.registerFunction("data.get", (sc, in) ->
  2315. {
  2316. return ((ModEntityPlayerMP) in[0].get(sc)).getVar(in[1].getString(sc));
  2317. });
  2318. sm.registerFunction("data.gettimer", (sc, in) ->
  2319. {
  2320. return (double) ((ModEntityPlayerMP) in[0].get(sc)).getTimer(in[1].getString(sc));
  2321. });
  2322. sm.registerFunction("data.clear", (sc, in) ->
  2323. {
  2324. ((ModEntityPlayerMP) in[0].get(sc)).clearData();
  2325. return Void.TYPE;
  2326. });
  2327. // ---------------------------------------------------------------------
  2328. // Ohne library
  2329. // ---------------------------------------------------------------------
  2330. sm.registerFunction("getglobalvar", (sc, in) ->
  2331. {
  2332. Object o = in[0].get(sc);
  2333. if(in.length == 2)
  2334. {
  2335. return scriptBank.getVar(in[1].getString(sc), getId(playerBank, o));
  2336. }
  2337. return scriptBank.getVar(in[1].getString(sc), getId(playerBank, o), in[2].get(sc));
  2338. });
  2339. sm.registerAlias("getglobalvar", "ggv");
  2340. sm.registerFunction("setglobalvar", (sc, in) ->
  2341. {
  2342. final String value = in[2].getString(sc);
  2343. final String var = in[1].getString(sc);
  2344. final int id = getId(playerBank, in[0].get(sc));
  2345. scheduler.getWorker().add(() ->
  2346. {
  2347. try
  2348. {
  2349. scriptBank.setVar(value, var, id);
  2350. }
  2351. catch(Exception ex)
  2352. {
  2353. scheduler.scheduleTask(() ->
  2354. {
  2355. sc.getScriptManager().getLogger().print("Worker error", ex, null, sc.getName(), sc, -1);
  2356. });
  2357. }
  2358. });
  2359. return Void.TYPE;
  2360. });
  2361. sm.registerAlias("setglobalvar", "sgv");
  2362. sm.registerFunction("delglobalvar", (sc, in) ->
  2363. {
  2364. final String var = in[1].getString(sc);
  2365. final int id = getId(playerBank, in[0].get(sc));
  2366. scheduler.getWorker().add(() ->
  2367. {
  2368. try
  2369. {
  2370. scriptBank.deleteVar(var, id);
  2371. }
  2372. catch(Exception ex)
  2373. {
  2374. scheduler.scheduleTask(() ->
  2375. {
  2376. sc.getScriptManager().getLogger().print("Worker error", ex, null, sc.getName(), sc, -1);
  2377. });
  2378. }
  2379. });
  2380. return Void.TYPE;
  2381. });
  2382. sm.registerAlias("delglobalvar", "dgv");
  2383. sm.registerFunction("msg", (sc, in) ->
  2384. {
  2385. sendMessageToGroup(server, scripts, perms, in[0].get(sc), sc, concat(sc, 1, "", in));
  2386. return Void.TYPE;
  2387. });
  2388. sm.registerFunction("removeformat", (sc, in) -> SnuviUtils.connect(sc, in, 0).replaceAll("§.", ""));
  2389. sm.registerFunction("command", (sc, in) ->
  2390. {
  2391. final String s = SnuviUtils.connect(sc, in, 0);
  2392. scheduler.scheduleTask(() -> Server.executeCommand(s));
  2393. return Void.TYPE;
  2394. });
  2395. }
  2396. private static Class getClass(String s) throws ClassNotFoundException
  2397. {
  2398. return Class.forName(s);
  2399. }
  2400. private static BlockState getBlockState(Location l)
  2401. {
  2402. return l.getWorld().getBlockState(l.getBlockPos());
  2403. }
  2404. private final static UUID SERVER_UUID = new UUID(0, 0);
  2405. private static UUID getUUID(Object o)
  2406. {
  2407. if(o instanceof PlayerEntity)
  2408. {
  2409. return ((PlayerEntity) o).getUniqueID();
  2410. }
  2411. else if(o instanceof UUID)
  2412. {
  2413. return (UUID) o;
  2414. }
  2415. else if("SERVER".equals(o))
  2416. {
  2417. return SERVER_UUID;
  2418. }
  2419. return UUID.fromString(o.toString());
  2420. }
  2421. private static int getId(IPlayerBank bank, Object o)
  2422. {
  2423. if(o instanceof ModEntityPlayerMP)
  2424. {
  2425. return ((ModEntityPlayerMP) o).getId();
  2426. }
  2427. else if(o instanceof Double)
  2428. {
  2429. return ((Double) o).intValue();
  2430. }
  2431. UUID uuid = getUUID(o);
  2432. return bank.getId(uuid);
  2433. }
  2434. public static void doForGroup(MinecraftServer server, Scripts scripts, PermissionManager perms, Object group, Script sc, Consumer<ICommandSource> c)
  2435. {
  2436. if(group instanceof String)
  2437. {
  2438. switch(group.toString().toLowerCase())
  2439. {
  2440. case "all":
  2441. PlayerList list = server.getPlayerList();
  2442. if(list != null)
  2443. {
  2444. scripts.getPlayerList(sc.getId()).forEach(uuid ->
  2445. {
  2446. ServerPlayerEntity p = list.getPlayerByUUID(uuid);
  2447. if(p != null)
  2448. {
  2449. c.accept(p);
  2450. }
  2451. });
  2452. }
  2453. break;
  2454. case "online":
  2455. if(server.getPlayerList() != null)
  2456. {
  2457. server.getPlayerList().getPlayers().forEach(p -> c.accept(p));
  2458. }
  2459. break;
  2460. case "dev":
  2461. if(server.getPlayerList() != null)
  2462. {
  2463. server.getPlayerList().getPlayers().forEach(p ->
  2464. {
  2465. if(perms.hasPermission(p, "script.error"))
  2466. {
  2467. c.accept(p);
  2468. }
  2469. });
  2470. }
  2471. break;
  2472. case "server":
  2473. c.accept(server);
  2474. break;
  2475. default:
  2476. if(server.getPlayerList() != null)
  2477. {
  2478. c.accept(Utils.getPlayerByName(server, group.toString()));
  2479. }
  2480. break;
  2481. }
  2482. return;
  2483. }
  2484. c.accept((PlayerEntity) group);
  2485. }
  2486. private static void sendMessageToGroup(MinecraftServer server, Scripts scripts, PermissionManager perms, Object group, Script sc, ITextComponent text)
  2487. {
  2488. doForGroup(server, scripts, perms, group, sc, p -> p.sendMessage(text));
  2489. }
  2490. private static ITextComponent concat(Script sc, int start, String pre, InputProvider... ob) throws Exception
  2491. {
  2492. StringTextComponent text = new StringTextComponent(pre);
  2493. Object o;
  2494. for(int i = start; i < ob.length; i++)
  2495. {
  2496. o = ob[i].get(sc);
  2497. if(o instanceof ITextComponent)
  2498. {
  2499. text.appendSibling((ITextComponent) o);
  2500. }
  2501. else
  2502. {
  2503. text.appendText(String.valueOf(o));
  2504. }
  2505. }
  2506. return text;
  2507. }
  2508. private static <T> T launchProjectile(PlayerEntity p, Class<? extends T> projectile, double scale, Object data)
  2509. {
  2510. World w = p.world;
  2511. Entity launch = null;
  2512. if(EntityItemProjectile.class == projectile)
  2513. {
  2514. if(data == null)
  2515. {
  2516. throw new NullPointerException("Data musn't be null for EntityItemProjectile");
  2517. }
  2518. ItemStack stack = (ItemStack) data;
  2519. if(stack.isEmpty())
  2520. {
  2521. throw new IllegalArgumentException("Empty ItemStack not allowed here");
  2522. }
  2523. launch = new EntityItemProjectile(p, stack.copy());
  2524. ((EntityItemProjectile) launch).setHeadingFromThrower(p, p.rotationPitch, p.rotationYaw, 0.0f, 1.5f, 1.0f);
  2525. }
  2526. else if(SnowballEntity.class == projectile)
  2527. {
  2528. launch = new SnowballEntity(w, p);
  2529. ((SnowballEntity) launch).shoot(p, p.rotationPitch, p.rotationYaw, 0.0f, 1.5f, 1.0f);
  2530. }
  2531. else if(EggEntity.class == projectile)
  2532. {
  2533. launch = new EggEntity(w, p);
  2534. ((EggEntity) launch).shoot(p, p.rotationPitch, p.rotationYaw, 0.0f, 1.5f, 1.0f);
  2535. }
  2536. else if(EnderPearlEntity.class == projectile)
  2537. {
  2538. launch = new EnderPearlEntity(w, p);
  2539. ((EnderPearlEntity) launch).shoot(p, p.rotationPitch, p.rotationYaw, 0.0f, 1.5f, 1.0f);
  2540. }
  2541. else if(PotionEntity.class == projectile)
  2542. {
  2543. launch = new PotionEntity(w, p);
  2544. ((PotionEntity) launch).setItem((ItemStack) data);
  2545. ((PotionEntity) launch).shoot(p, p.rotationPitch, p.rotationYaw, -20.0f, 0.5f, 1.0f);
  2546. }
  2547. else if(ExperienceBottleEntity.class == projectile)
  2548. {
  2549. launch = new ExperienceBottleEntity(w, p);
  2550. ((ExperienceBottleEntity) launch).shoot(p, p.rotationPitch, p.rotationYaw, -20.0f, 0.7f, 1.0f);
  2551. }
  2552. else if(AbstractArrowEntity.class.isAssignableFrom(projectile))
  2553. {
  2554. if(SpectralArrowEntity.class == projectile)
  2555. {
  2556. launch = new SpectralArrowEntity(w, p);
  2557. }
  2558. else
  2559. {
  2560. launch = new ArrowEntity(w, p);
  2561. if(data != null)
  2562. {
  2563. ((ArrowEntity) launch).setPotionEffect((ItemStack) data);
  2564. }
  2565. }
  2566. ((AbstractArrowEntity) launch).shoot(p, p.rotationPitch, p.rotationYaw, 0.0F, 3.0F, 1.0F);
  2567. }
  2568. else if(DamagingProjectileEntity.class.isAssignableFrom(projectile))
  2569. {
  2570. Vec3d v = p.getLookVec().scale(10);
  2571. if(SmallFireballEntity.class == projectile)
  2572. {
  2573. launch = new SmallFireballEntity(w, p, v.x, v.y, v.z);
  2574. }
  2575. else if(WitherSkullEntity.class == projectile)
  2576. {
  2577. launch = new WitherSkullEntity(w, p, v.x, v.y, v.z);
  2578. }
  2579. else if(DragonFireballEntity.class == projectile)
  2580. {
  2581. launch = new DragonFireballEntity(w, p, v.x, v.y, v.z);
  2582. }
  2583. else
  2584. {
  2585. launch = new FireballEntity(w, p, v.x, v.y, v.z);
  2586. }
  2587. }
  2588. else
  2589. {
  2590. return null;
  2591. }
  2592. launch.setMotion(launch.getMotion().scale(scale));
  2593. w.addEntity(launch);
  2594. return (T) launch;
  2595. }
  2596. }