MinecraftFunctions.java 112 KB

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