SoundCommands.java 1.7 KB

12345678910111213141516171819202122232425262728293031
  1. package me.km.snuviscript.commands;
  2. import me.hammerle.snuviscript.code.ScriptManager;
  3. import me.km.utils.Location;
  4. import me.km.utils.Mapper;
  5. import net.minecraft.entity.player.ServerPlayerEntity;
  6. import net.minecraft.network.play.server.SPlaySoundEffectPacket;
  7. import net.minecraft.util.SoundCategory;
  8. import net.minecraft.util.SoundEvent;
  9. import net.minecraft.world.server.ServerWorld;
  10. public class SoundCommands {
  11. public static void registerFunctions(ScriptManager sm) {
  12. sm.registerFunction("sound.get", (sc, in) -> Mapper.getSound(in[0].getString(sc)));
  13. sm.registerFunction("sound.getcategory", (sc, in) -> Mapper.getSoundCategory(in[0].getString(sc)));
  14. sm.registerConsumer("sound.spawn", (sc, in) -> {
  15. Location l = (Location) in[0].get(sc);
  16. ServerWorld sw = (ServerWorld) l.getWorld();
  17. float volume = in.length >= 4 ? in[3].getFloat(sc) : 1.0f;
  18. float pitch = in.length >= 5 ? in[4].getFloat(sc) : (sw.rand.nextFloat() * 0.1f + 0.9f);
  19. sw.playSound(null, l.getX(), l.getY(), l.getZ(), (SoundEvent) in[1].get(sc), (SoundCategory) in[2].get(sc), volume, pitch);
  20. });
  21. sm.registerConsumer("sound.spawnforplayer", (sc, in) -> {
  22. ServerPlayerEntity p = (ServerPlayerEntity) in[0].get(sc);
  23. float volume = in.length >= 4 ? in[3].getFloat(sc) : 1.0f;
  24. float pitch = in.length >= 5 ? in[4].getFloat(sc) : (p.world.rand.nextFloat() * 0.1f + 0.9f);
  25. p.connection.sendPacket(new SPlaySoundEffectPacket((SoundEvent) in[1].get(sc),
  26. (SoundCategory) in[2].get(sc), p.getPosX(), p.getPosY(), p.getPosZ(), volume, pitch));
  27. });
  28. }
  29. }