MinecraftFunctions.java 125 KB

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