MinecraftFunctions.java 118 KB

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