Thread: [Asteria] Sounds & Songs

Page 1 of 2 12 LastLast
Results 1 to 10 of 17
  1. #1 [Asteria] Sounds & Songs 
    Registered Member mr giggles's Avatar
    Join Date
    Jul 2010
    Posts
    197
    Thanks given
    49
    Thanks received
    69
    Rep Power
    18
    This is a little something I've been messing with. This is my first time writing a sound handling system for an RSPS, it isn't perfect, but it's a good base to work off of.

    I used http://www.rune-server.org/runescape...sic-areas.html for the locations of the songs and for the IDs I also used a 666 revision list as I couldn't find a good 317 list. So some of the ids will be off and some are missing, they'll have to be fixed manually. For the SoundHandling class I used a sounds list that I found somewhere on here. I didn't add everything in, but that can once again be easily added onto.

    Also, if you plan to add this, make sure you have http://www.rune-server.org/runescape...nds-music.html in your client.

    1. First create the package world.sound;
    2. Add the following java files inside there:

      Sound.java
      Spoiler for Sound:

      Code:
      package server.world.sound;
      
      /**
       * Parent class for all sound and song handling, more can be added onto in the future
       * @author Mr Giggles
       */
      public abstract class Sound {
      	
      	protected int id, type, delay, volume;
      
      	public int getId() {
      		return id;
      	}
      
      	public int getType() {
      		return type;
      	}
      
      	public int getDelay() {
      		return delay;
      	}
      	
      	public int getVolume() {
      		return volume;
      	}
      	
      }


      SoundHandling.java
      Spoiler for SoundHandling:

      Code:
      package server.world.sound;
      
      /**
       * Handles different types of sounds
       * @author Mr Giggles
       */
      
      //-------------------------------------------------------------------------------
      //Data is stored in different enums for organization reasons, to access them use
      //player.playSound(new SoundHandler( arg );
      //Example: player.playSound(new SoundHandler(getSkill.WOODCUTTING_CUTTING.getSoundId()));
      //-------------------------------------------------------------------------------
      public class SoundHandler extends Sound {
      	
      	public SoundHandler(int id, int type, int delay, int volume) {
      		this.id = id;
      		this.type = type;
      		this.delay = delay;
      		this.volume = volume;
      	}
      	
      	public SoundHandler(int id) {
      		this.id = id;
      		this.type = 0;
      		this.delay = 0;
      		this.volume = 100;
      	}
      
      	public enum getMisc {
      		EATFOOD(317),
      		DRINK(334),
      		LEVEL(67),
      		PICKUP_ITEM(358),
      		DROP_ITEM(376),
      		DROP_MONEY(465),
      		DUAL_WIN(77),
      		DUAL_LOSE(76),
      		GENIE(430),
      		SPADE(232)
      		;
      		//Add more
      		
      		private int soundId;
      
      		getMisc(int soundId) {
      			this.soundId = soundId;
      		}
      		
      		public int getSoundId() {
      			return soundId;
      		}
      		
      	}
      	
      	public enum getNpc {
      		
      		CHICKEN_ATTACK(26),
      		CHICKEN_BLOCK(24),
      		CHICKEN_DIE(25),
      		MAN_GETTING_HIT(72),
      		WOMAN_GETTING_HIT(73),
      		MAN_DIE(70),
      		WOMAN_DIE(71)
      		;
      		//Add more
      		
      		private int soundId;
      		
      		getNpc(int soundId) {
      			this.soundId = soundId;
      		}
      		
      		public int getSoundId() {
      			return soundId;
      		}
      		
      	}
      	
      	public enum getSkill {
      		
      		MINING_ORE_EMPTY(431),
      		MINING_ORE(432),
      		RUNECRAFTING(481),
      		CRAFTING_GEM(464),
      		THEIVING_STUN(458),
      		SMITHING(468),
      		SMELTING(469),
      		WOODCUTTING_START(471),
      		WOODCUTTING_CUTTING(472),
      		WOODCUTTING_TREE_FALL(473),
      		BURY_BONES(380),
      		FISHING(378),//377?
      		MAKE_POTION(373),//or 281?
      		PICK_LOCK(324),
      		COOKING(357),
      		FLETCHING(375)
      		;
      		//Add more
      		
      		private int soundId;
      		
      		getSkill(int soundId) {
      			this.soundId = soundId;
      		}
      
      		public int getSoundId() {
      			return soundId;
      		}
      		
      	}
      	
      	public enum getCombat {
      		
      		PUNCH_AND_KICK(417),
      		GETTING_HIT(69)
      		;
      		//Add more
      		
      		private int soundId;
      		
      		getCombat(int soundId) {
      			this.soundId = soundId;
      		}
      		
      		public int getSoundId() {
      			return soundId;
      		}
      		
      	}
      	
      	public enum getMagic {
      		
      		HOME_TELEPORT_1(2500),
      		HOME_TELEPORT_2(2501),
      		HOME_TELEPORT_3(2502),
      		HOME_TELEPORT_4(2503),
      		TELEPORT(202),
      		HIGH_ALCH(223),
      		LOW_ALCH(224)
      		;
      		//Add more
      		
      		private int soundId;
      		
      		getMagic(int soundId) {
      			this.soundId = soundId;
      		}
      		
      		public int getSoundId() {
      			return soundId;
      		}
      		
      	}
      	
      	public enum getPrayer {
      		
      		FILL_PRAYER(442),
      		PRAYER_OFF(435),
      		NO_PRAYER_LEFT(437),
      		FAIL_PRAYER(447), //Level too low to use
      		PROTECT_MELEE(433)
      		;
      		//Add more
      		
      		private int soundId;
      		
      		getPrayer(int soundId) {
      			this.soundId = soundId;
      		}
      		
      		public int getSoundId() {
      			return soundId;
      		}
      		
      	}
      
      }


      SongHandling.java (I've rewritten this class to not use an enum. You'll need to load the data through a json file, download songData.json - Pastebin.com and put it in ./data/songs/songData.json)
      Spoiler for SongHandler:

      Code:
      package server.world.sound;
      
      import java.io.File;
      import java.io.FileNotFoundException;
      import java.io.FileReader;
      import java.util.HashMap;
      import java.util.Iterator;
      
      import com.google.gson.Gson;
      import com.google.gson.GsonBuilder;
      import com.google.gson.JsonArray;
      import com.google.gson.JsonElement;
      import com.google.gson.JsonObject;
      import com.google.gson.JsonParser;
      
      import server.world.entity.player.Player;
      import server.world.map.Position;
      
      /**
       * Handles all song data based on Players location
       * @author Mr Giggles
       */
      public class SongHandler extends Sound {
      	
      	/**
      	 * Get a song based on location
      	 * @param player
      	 * @param position
      	 */
      	public SongHandler(Player player, Position position) {
      		this.id = getSong(player, position);
      	}
      	
      	/**
      	 * Send a song
      	 * @param id
      	 */
      	public SongHandler(int id) {
      		this.id = id;
      	}
      			
      	private final static int TOTAL_SONGS = 422;
      	private static HashMap<Integer, SongData> songMap = new HashMap<Integer, SongData>();	
      	private static SongLocation[] songLocation = new SongLocation[TOTAL_SONGS];
      	
      	private final static File fileDir() throws FileNotFoundException {
      		if (new File("./data/songs/songData.json").exists())
      			return new File("./data/songs/songData.json");
      		
      		throw new FileNotFoundException("./data/songs/songData.json not found");
      	}
      	
      	/**
      	 * Gets the song id based on the position given
      	 * @param player
      	 * @param position
      	 * @return
      	 */
      	public int getSong(Player player, Position position) {
      		double x = (position.getX() / 64), y = (position.getY() / 64);
      		SongData song = getSongData(x, y);
      		
      		if (song.getName().equals("n/a") || song.getId() == 0) {
      			player.getPacketBuilder().debugMessage("No valid song found for area: " + x + ", " + y);
      			return song.getId();
      		}
      		return song.getId();
      	}
      	
      	public SongData getSongData(double x, double y) {
      		for (SongLocation locData : songLocation)
      			if (x >= locData.getX1() && x <= locData.getX2() && y >= locData.getY1() && y <= locData.getY2()) {
      				return songMap.get(locData.getIndex());
      			}
      		return new SongData("n/a", 0);
      	}
      	
      	public static void setSong(SongLocation coord, SongData song) {
      		songMap.put(coord.getIndex(), song);
      	}
      	
      	public static void readFile() {
      		try {
      			final JsonElement jelement = new JsonParser().parse(new FileReader(fileDir()));
      			final Gson builder = new GsonBuilder().create();
      		    final JsonObject reader = jelement.getAsJsonObject();
      			
      	        JsonArray slideContent = (JsonArray) reader.get("songData");
      	        Iterator i = slideContent.iterator();
      	        int count = 0;
      
      	        while (i.hasNext()) {
      	        	JsonObject slide = (JsonObject) i.next();
      				songLocation[count] = new SongLocation(count, builder.fromJson(slide.get("region-parameters").getAsJsonArray(), double[].class)); //song location
      				setSong(songLocation[count], new SongData(slide.get("song-name").getAsString(), slide.get("song-id").getAsInt()));
      				count++;
      			}
      	        
      	        System.out.println(count + " songs loaded into map.");
      		} catch (Exception e) {
      			System.out.println("Error loading songData");
      			e.printStackTrace();
      			System.exit(0);
      		}
      	}
      }


      SongLocation.java
      Spoiler for SongLocation:

      Code:
      package server.world.sound;
      
      public class SongLocation extends Sound {
      
      	private int index;
      	private double regionXone, regionYone, regionXtwo, regionYtwo;
      	
      	public SongLocation(int index, double ... coords) {
      		this.index = index;
      		this.regionXone = coords[0];
      		this.regionYone = coords[1];
      		this.regionXtwo = coords[2];
      		this.regionYtwo = coords[3];
      	}
      	
      	public int getIndex() {
      		return index;
      	}
      	
      	public double getX1() {
      		return regionXone;
      	}
      	
      	public double getX2() {
      		return regionXtwo;
      	}
      	
      	public double getY1() {
      		return regionYone;
      	}
      	
      	public double getY2() {
      		return regionYtwo;
      	}
      
      }


      SongData.java
      Spoiler for SongData:
      Code:
      package server.world.sound;
      
      public class SongData extends Sound {
      
      	private String songName;
      	private int songId;
      	
      	public SongData(String songName, int songId) {
      		this.songName = songName;
      		this.songId = songId;
      	}
      	
      	public String getName() {
      		return songName;
      	}
      	
      	public int getId() {
      		return songId;
      	}
      
      }

    3. In Main.java add this under the new DoorHandler(); and its logger.
      Code:
                  SongHandler.readFile();
                  logger.info("The song system is now running...");
    4. Now in your Player.java file add the two methods:
      Code:
          public void playSound(SoundHandler sound) {
          	if (isPlayer()) {
                  Player player = (Player) this;
                  player.getPacketBuilder().sendSound(sound.getId(), sound.getType(), sound.getDelay(), sound.getVolume());
          	}
          }
          
          public void playSong(SongHandler sound) {
          	if (isPlayer()) {
                  Player player = (Player) this;
                  player.getPacketBuilder().sendSong(sound.getId());
          	}
          }
    5. Now in your PacketEncoder.java file, find sendSound, replace that and add the other methods below:
      Code:
          public PacketEncoder sendSound(int id, int type, int delay, int volume) {
              PacketBuffer.WriteBuffer out = PacketBuffer.newWriteBuffer(8);
              out.writeHeader(174);
              out.writeShort(id);
              out.writeByte(type);
              out.writeShort(delay);
              out.writeShort(volume);
              player.getSession().encode(out);
              return this;
          }
      
          public PacketEncoder sendSong(int id) {
              PacketBuffer.WriteBuffer out = PacketBuffer.newWriteBuffer(2);
              out.writeHeader(74);
              out.writeShort(id);
              player.getSession().encode(out);
              return this;
          }
          
          public PacketEncoder sendQuickSong(int id, int delay) {
              PacketBuffer.WriteBuffer out = PacketBuffer.newWriteBuffer(8);
              out.writeHeader(121);
              out.writeShort(id);
              out.writeShort(delay);
              player.getSession().encode(out);
              return this;
          }
    6. Here is a simple debug method if you want it, add it below sendMessage:
      Code:
          public PacketEncoder debugMessage(String message) {
          	if (Config.DEBUG_MODE && player.getStaffRights() >= 1) {
      	        PacketBuffer.WriteBuffer out = PacketBuffer.newWriteBuffer(message.length() + 3);
      	        out.writeVariablePacketHeader(253);
      	        out.writeString("[Debug] " + message);
      	        out.finishVariablePacketHeader();
      	        player.getSession().encode(out);
      	        return this;
          	}
          	return null;
          }
    7. Here are two commands:
      Code:
              if (cmd[0].equals("sound")) {
                  int id = Integer.parseInt(cmd[1]);
                  int type = Integer.parseInt(cmd[2]);
                  int delay = Integer.parseInt(cmd[3]);
                  int volume = Integer.parseInt(cmd[4]);
                  player.playSound(new SoundHandler(id, type, delay, volume));
                  return;
              }
              
              if (cmd[0].equals("song")) {
                  int id = Integer.parseInt(cmd[1]);
                  player.playSong(new SongHandler(id));
                  return;
              }
    8. For example to add a sound effect I'll supply you with a basic one, go to your DecodeDropItemPacket.java file, and under "if (player.getInventory().getContainer().contains(ite m)) {" add:
      Code:
       	if (item == 995) {
              	player.playSound(new SoundHandler(getMisc.DROP_MONEY.getSoundId()));
              } else {
                  	player.playSound(new SoundHandler(getMisc.DROP_ITEM.getSoundId()));
              }
    9. For the songs, just go to your MovementQueue.java and add in the playSong method there.
      e.g. player.playSong(new SongHandler(player, player.getPosition()));


    Once again, this is mainly just to be used as a base, and you'll have to add any other things. Enjoy
    Last edited by mr giggles; 09-07-2014 at 02:22 AM. Reason: updating classes
    Reply With Quote  
     

  2. #2  


    Major's Avatar
    Join Date
    Jan 2011
    Posts
    2,997
    Thanks given
    1,293
    Thanks received
    3,556
    Rep Power
    5000
    Why are you adding the methods in Entity instead of Player? Npcs etc can't play songs so it doesn't make sense to put them there. Also, as you say, load this from json or some other format - this is far too much data to put in a class.
    Reply With Quote  
     

  3. Thankful users:


  4. #3  
    Registered Member mr giggles's Avatar
    Join Date
    Jul 2010
    Posts
    197
    Thanks given
    49
    Thanks received
    69
    Rep Power
    18
    Quote Originally Posted by Major View Post
    Why are you adding the methods in Entity instead of Player? Npcs etc can't play songs so it doesn't make sense to put them there. Also, as you say, load this from json or some other format - this is far too much data to put in a class.
    Entity - Player.. Yea, some reason I overlooked that. I'll probably convert the data into a Json later and update it to load through a HashMap, when I first started it for some reason I didn't realize how much data it would be.
    Reply With Quote  
     

  5. #4  
    Registered Member
    Stanaveli's Avatar
    Join Date
    Aug 2014
    Posts
    1,490
    Thanks given
    184
    Thanks received
    653
    Rep Power
    1338
    I'm happy that more and more people are starting to use Asteria..

    Goodjob on this one !
    Keep your head up.



    Reply With Quote  
     

  6. Thankful user:


  7. #5  
    Registered Member
    Join Date
    Oct 2013
    Posts
    775
    Thanks given
    48
    Thanks received
    104
    Rep Power
    14
    i have mine loaded through xml, nice though.
    Reply With Quote  
     

  8. #6  
    Banned
    Join Date
    Mar 2014
    Posts
    628
    Thanks given
    109
    Thanks received
    181
    Rep Power
    0
    I've got sounds being handled through JSON for Asteria. Good job, though!
    Reply With Quote  
     

  9. #7  
    Banned [Asteria] Sounds &amp; Songs Market Banned


    Join Date
    Jan 2011
    Age
    26
    Posts
    3,112
    Thanks given
    1,198
    Thanks received
    1,479
    Rep Power
    0
    PLEASE load that data through JSON or some other external file, as major stated all of that data really shouldn't be loaded in an enum
    Reply With Quote  
     

  10. #8  
    Banned
    Join Date
    Jul 2013
    Posts
    383
    Thanks given
    108
    Thanks received
    25
    Rep Power
    0
    Yes, if you make it load the data through JSON, it would be cool

    Thanks for this, though.
    Reply With Quote  
     

  11. #9  
    Registered Member mr giggles's Avatar
    Join Date
    Jul 2010
    Posts
    197
    Thanks given
    49
    Thanks received
    69
    Rep Power
    18
    I've converted it to a json for anyone that wants it.
    songData.json - Pastebin.com
    Reply With Quote  
     

  12. #10  
    Banned [Asteria] Sounds &amp; Songs Market Banned


    Join Date
    Jan 2011
    Age
    26
    Posts
    3,112
    Thanks given
    1,198
    Thanks received
    1,479
    Rep Power
    0
    Quote Originally Posted by mr giggles View Post
    I've converted it to a json for anyone that wants it.
    songData.json - Pastebin.com
    what are "region parameters" ? can't you just use the region id lol
    Reply With Quote  
     

Page 1 of 2 12 LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Best Dj Songs Of All Time!
    By Faris in forum Music
    Replies: 20
    Last Post: 04-27-2008, 04:14 PM
  2. Replies: 44
    Last Post: 04-13-2008, 09:46 PM
  3. Sound Effects Needed
    By Palidino in forum RS2 Server
    Replies: 6
    Last Post: 10-17-2007, 04:39 AM
  4. beta project-rs the sound server beta
    By laurens in forum RS2 Server
    Replies: 18
    Last Post: 09-29-2007, 06:59 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •