MinecraftFunctions.java 107 KB

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