Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[Guide]Adding a Sprite Changer NPC

Joined
Aug 16, 2006
Messages
1,251
Reaction score
199
Hello I'm here today to tell you how to set up a spriting npc in your server!

Basically it will look something like this:
-DefaulT - [Guide]Adding a Sprite Changer NPC - RaGEZONE Forums

-DefaulT - [Guide]Adding a Sprite Changer NPC - RaGEZONE Forums

-DefaulT - [Guide]Adding a Sprite Changer NPC - RaGEZONE Forums


If you use the source provided on this forum, you prob already have the code for the spriter so dont worry its easy to add!

But here is the basics of the code;
In Server>org.pokenet.server.backend.entity>NonPlayChar.java

Your going to find something like this:
Code:
/* If this NPC is a sprite selection npc */
			if(m_name.equalsIgnoreCase("Spriter")) {
				p.setSpriting(true);
				TcpProtocolHandler.writeMessage(p.getTcpSession(), new SpriteSelectMessage());
				return;
			}

Essentially it checks, if the name equals "Spriter" and if it does, then it knows to show the SpriteSelectMessage.

The sprite select message code is located at Client>org.pokenet.client.ui.frames>SpriteChooserDialog.java

and looks something like this;
Code:
package org.pokenet.client.ui.frames;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import mdes.slick.sui.Button;
import mdes.slick.sui.Frame;
import mdes.slick.sui.Label;
import mdes.slick.sui.event.ActionEvent;
import mdes.slick.sui.event.ActionListener;

import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.gui.GUIContext;
import org.pokenet.client.GameClient;
import org.pokenet.client.backend.FileLoader;
import org.pokenet.client.ui.base.ConfirmationDialog;
import org.pokenet.client.ui.base.ListBox;

public class SpriteChooserDialog extends Frame {
	protected ListBox m_spriteList;
	protected Label m_spriteDisplay;
	protected String m_mustLoadSprite;
	private List<String> m_sprites;
	private InputStream m_stream;
	private String m_respath;

	public SpriteChooserDialog() {
		getContentPane().setX(getContentPane().getX() - 1);
		getContentPane().setY(getContentPane().getY() + 1);
		m_sprites = new ArrayList<String>();
		m_respath = System.getProperty("res.path");
		if(m_respath==null)
			m_respath="";
		for (int i = 1; i <= 218; i++) {
			m_sprites.add(String.valueOf(i));
		}
		/*
		 * Handle blocked sprites
		 */
		
		try {
			InputStream in;
			in = FileLoader.loadFile(m_respath+"res/characters/sprites.txt");
			Scanner s = new Scanner(in);
			while(s.hasNextLine()) {
				m_sprites.remove(s.nextLine());
			}
			s.close();
			
			m_spriteDisplay = new Label();
			m_spriteDisplay.setSize(124, 204);
			m_spriteDisplay.setLocation(105, 20);
			getContentPane().add(m_spriteDisplay);

			m_spriteList = new ListBox(m_sprites, false) {
				@Override
				protected void itemClicked(String itemName, int idx) {
					super.itemClicked(itemName, idx);
					m_mustLoadSprite = m_respath+"res/characters/" + itemName + ".png";
				}
			};
			m_spriteList.setSize(105, 317);
			getContentPane().add(m_spriteList);

			setTitle("Please choose your character..");
			getCloseButton().setVisible(false);
			setSize(265, 340);
			setResizable(false);
			setDraggable(false);
			setVisible(true);
			initUse();
		} catch (FileNotFoundException e) {
			//No sprites to handle. 
		}
		
	}

	public void initUse() {
		final SpriteChooserDialog thisDialog = this;

		Button use = new Button("Use new sprite!");
		use.pack();
		use.setLocation(130, 245);
		getContentPane().add(use);

		Button cancel = new Button("Cancel");
		cancel.pack();
		cancel.setLocation(130, 280);
		getContentPane().add(cancel);

		cancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				GameClient.getInstance().getDisplay().remove(thisDialog);
			}
		});

		use.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				GameClient.getInstance().getDisplay().remove(thisDialog);

				final ConfirmationDialog confirm = new ConfirmationDialog("Are you sure you want to change sprites?\nIt'll cost you P500!");
				confirm.addYesListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						confirm.setVisible(false);
						GameClient.getInstance().getDisplay().remove(confirm);

						GameClient.getInstance().getPacketGenerator().writeTcpMessage(
								"S" + m_spriteList.getSelectedName());
					}
				});
				confirm.addNoListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						confirm.setVisible(false);
						GameClient.getInstance().getDisplay().remove(confirm);
					}
				});

				GameClient.getInstance().getDisplay().add(confirm);
			}
		});
	}

	public int getChoice() {
		return m_spriteList.getSelectedIndex();
	}

	@Override
	public void render(GUIContext container, Graphics g) {
		super.render(container, g);
		if (m_mustLoadSprite != null) {
			try {
				m_stream = FileLoader.loadFile(m_mustLoadSprite);
				m_spriteDisplay.setImage(new Image(m_stream, m_mustLoadSprite, false));
			} catch (SlickException e) {
				e.printStackTrace();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
			m_mustLoadSprite = null;
		}
	}
}

Here you can change the way the gui looks and the text that will be displayed on the gui.

If you look at Server>org.pokenet.server.network>TcpProtocalHandler.java

You will see;
Code:
else if(p.isSpriting()) {
					//Sprite changing
					int sprite = Integer.parseInt(message.substring(1));
					/* Ensure the user buys a visible sprite */
					if(sprite > 0 && !GameServer.getServiceManager().
							getSpriteList().getUnbuyableSprites().contains(sprite)) {
						if(p.getMoney() >= 500) {
							p.setMoney(p.getMoney() - 500);
							p.updateClientMoney();
							p.setSprite(sprite);
							p.setSpriting(false);
						}
					}
				}

Here u can change how much he will charge the user to change the sprite.


Now that the mechanics are explained, lets actually add this npc!

So open the map editor to the map that you want to add it to, im just going to use the Pallet town since its the easiest, now choose a spot you want to put the npc, hover over the spot and get the coordinates for it which are in the bottom left corner of the map editor.

Im going to use coordinates 9,9

Now open your Server>Res>Npc folder, and open the text file coordinating to your map. (mine was 3.1 for pallet town.)

In the first line of the text file add something like this:
Code:
[npc]
Spriter
down
164
9
9
NULL
0
-1
6
false
false
false
[/npc]

However, change the numbers 9 9 to be your coordinates you chose on your map. and 164 to what ever you want him to look like.

There you go. Easy peasy, enjoy!
 
Back
Top