My favorites | Sign in
Logo
                
Search
for
Updated Sep 04, 2009 by joebingham07
Labels: Featured, Phase-Implementation
CodeSamples  
Snippets of code to get you started with TibiaAPI.

Table of Contents

Note

Most of these samples assume you have imported Tibia and Tibia.Objects:

C#

using Tibia;
using Tibia.Objects;

VB.Net

Imports Tibia
Imports Tibia.Objects

Get a client object to work with

C#

Client client = Util.ClientChooser.ShowBox();
if (client == null)
{
	MessageBox.Show("No active client.");
	Application.Exit();
}

VB.Net

Dim client as Client = ClientChooser.ShowBox()
If client Is Nothing Then
	MsgBox("No active client.")
	Application.[Exit]()
End If

Using the hook proxy

VB.Net

Imports Tibia.Objects
Imports Tibia.Packets
Imports Tibia.Util

Public Class MainForm
    Private client As Client
    Private proxy As HookProxy

    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        client = ClientChooser.ShowBox()
        proxy = New HookProxy(client)

        AddHandler proxy.ReceivedCreatureSpeechIncomingPacket, AddressOf ReceivedCreatureSpeechIncomingPacket
    End Sub

    Private Function ReceivedCreatureSpeechIncomingPacket(ByVal p As IncomingPacket)
        Dim packet As Incoming.CreatureSpeechPacket = p
        MessageBox.Show(packet.SenderName & ": " & packet.Message)
        Return True
    End Function
End Class

Getting player information from the Tibia website

C#

// Set the textbox uxText to equal the profession of the player Bubble
// This method is asynchronous, that is why we have to supply a 
// callback to get the data. It does not block your GUI if the 
// request take a while.
private void uxButton_Click(object sender, EventArgs e)
{
	Website.LookupPlayer("Bubble", delegate(Website.CharInfo i)
	{
		uxText.Invoke(new EventHandler(delegate
		{
			uxText.Text = i.Profession;
		}));
	});
}

Parsing commands from the player

C#

// Put this after you start the proxy
client.Proxy.ReceivedPlayerSpeechOutgoingPacket += new Tibia.Util.Proxy.OutgoingPacketListener(Proxy_ReceivedPlayerSpeechOutgoingPacket);
// This simple example assumes you have a textbox named uxText, 
// to which it will append the command your client sent
private bool Proxy_ReceivedPlayerSpeechOutgoingPacket(Tibia.Packets.OutgoingPacket packet)
{
    Tibia.Packets.Outgoing.PlayerSpeechPacket p = (Tibia.Packets.Outgoing.PlayerSpeechPacket)packet;
    
    if (p.Message.StartsWith("/"))
    {
        uxText.Invoke(new EventHandler(delegate
        {
            uxText.AppendText("Command: " + p.Message.Substring(1));
            uxText.AppendText(Environment.NewLine);

            // Scroll to the bottom of the text box
            uxText.SelectionStart = uxText.Text.Length;
            uxText.ScrollToCaret();
        }));

        // Don't send the command to the server
        return false;
    }

    return true;
}

Using the proxy

C#

// Start the proxy and attach event handlers
// Put this in Form_Load
client.StartProxy();
client.Proxy.SplitPacketFromServer += Proxy_SplitPacketFromServer;
client.Proxy.SplitPacketFromClient += Proxy_SplitPacketFromClient;


// These sample event handlers are other methods, outside Form_Load
// LogPacket assumes you have a textbox called uxLog
void Proxy_SplitPacketFromClient(byte type, byte[] data)
{
	LogPacket(data, "CLIENT");
}

void Proxy_SplitPacketFromServer(byte type, byte[] data)
{
	LogPacket(data, "SERVER");
}

private void LogPacket(byte[] packet, string from)
{
	uxPackets.Invoke(new EventHandler(delegate
	{
		string s = "from " + from + Environment.NewLine;
		s += Packet.ByteArrayToHexString(packet) + Environment.NewLine + Environment.NewLine;
		uxLog.Text += s;
	}));
}

Perform name spy or level spy

C#

// Show names on other floors
client.Map.ShowNames(true);

// Move the screen to the floor below
client.Map.ShowFloor(-1, true);

VB.Net

' Show names on other floors
client.Map.ShowNames(true)

' Move the screen to the floor below
client.Map.ShowFloor(-1, true)

Setup a global mouse hook

The following code will show you how to set up a global mouse hook and capture when the ButtonDown event is raised

C#

// Enable mouse hook and set the button down event to a function

if (!MouseHook.Enabled) {
    MouseHook.Enable();
    MouseHook.ButtonDown = GlobalMouseDown;
}

// The function to handle the button down event
public bool GlobalMouseDown(System.Windows.Forms.MouseButtons button)
{
    if (button == Windows.Forms.MouseButtons.Left) {
        MessageBox.Show("Left mouse button was clicked down.");
    }
    
    return true;
}

VB.Net

' Enable mouse hook and set the button down event to a function
If Not MouseHook.Enabled Then
    MouseHook.Enable()
    MouseHook.ButtonDown = AddressOf GlobalMouseDown
End If

' The function to handle the button down event
Public Function GlobalMouseDown(ByVal button As System.Windows.Forms.MouseButtons)
    If button = Windows.Forms.MouseButtons.Left Then
        MessageBox.Show("Left mouse button was clicked down.")
    End If

    Return True
End Function

Setup a global keyboard hook

Put the following in a button's click event. This example captures the hotkey Ctrl + Alt + A in the Tibia client only (doesn't let it go to the client). All other keypresses, client or otherwise, are ignored.

C#

if (!KeyboardHook.Enabled)
{
    KeyboardHook.Enable();
    KeyboardHook.Add(Keys.A, new KeyboardHook.KeyPressed(delegate()
    {
        if (client.IsActive)
        {
            if (KeyboardHook.Control && KeyboardHook.Alt)
            {
                string text = "You want to capture the hotkey " +
                Tibia.KeyboardHook.KeyToString(Keys.A) + " in Tibia!";
                MessageBox.Show(text);
            	return false;
      	     }
        }
      	return true;
    }));
}
else
{
	KeyboardHook.Disable();
}

Connect to an OT Server

C#

client.Login.SetOT("radonia.net", 7171);

VB.Net

client.Login.SetOT("radonia.net", 7171)

Eat a mushroom in your inventory

C#

client.Inventory.UseItem(Tibia.Constants.Items.Food.WhiteMushroom.Id);

VB.Net

client.Inventory.UseItem(Tibia.Constants.Items.Food.WhiteMushroom.Id)

Attack the first rat found in the battelist

C#

client.BattleList.GetCreatures().Where(
	creature => creature.Name.Equals("rat", StringComparison.CurrentCultureIgnoreCase).Attack();

VB.Net

client.BattleList.GetCreatures().Where(Function(creature) creature.Name.Equals("rat", StringComparison.CurrentCultureIgnoreCase).Attack()

Change your players outfit type and addon

C#

Player player = client.getPlayer();
player.OutfitType = Tibia.Constants.OutfitType.BeggarMale;
player.Addon = Tibia.Constants.OutfitAddon.Both;

VB.Net

Dim player As Player = client.getPlayer()
player.OutfitType = Tibia.Constants.OutfitType.BeggarMale
player.Addon = Tibia.Constants.OutfitAddon.Both

Replace all the trees with small fir trees

C#

client.Map.replaceTrees();

VB.Net

client.Map.replaceTrees()

Make a simple lighthack

C#

Player player=client.GetPlayer();
player.Light = Tibia.Constants.LightSize.Full;
player.LightColor = Tibia.Constants.LightColor.White;

Draw players hitpoints in percents above his name

C#

Player player = client.GetPlayer();
client.Screen.DrawCreatureText(player.Id, new Location(0, -10, 0), Color.Green, 
Tibia.Constants.ClientFont.NormalBorder, player.HPBar.ToString() + "%");

Comment by MoneyMakingAccount2, Apr 21, 2008

Good good. !!!

Comment by theafien, Apr 22, 2008

Good work.. ^^

Comment by klusbert, Apr 27, 2008

How to do lighthack, keep updating diffrent kind of small cheat. Realy like this projekt, best ever! Love u man!

Comment by Bnc321123, Jun 18, 2008

I cant get the dll imported into C#.net, the example projects dont open properly :/

Any help?

Comment by OudMartijn, Jan 04, 2009

im a terrible noob, but can someone also show how to do this in C++?

Comment by HaCkeR_o...@yahOo.coM.Br, Jan 07, 2009

you can rewrite the entire api to the native C++ =)

i'm tring to do this .. but .. it's hard :( .. i have emulated all of C# functions used in the library but now .. i need to correct 1198 errors :(

Comment by lu.4...@hotmail.com, Apr 23, 2009

ihul ;D

Comment by Imperionsupport, Dec 21 (5 days ago)

Please update the examples!


Sign in to add a comment
Hosted by Google Code