RuneScape 2 Development > Snippets

Post your code snippets here! [Updated 08/09/2012]

<<< (122/172) >>>

RiiPiiNFtW:
Wrote this together for a simullation I am working on for school and added it to my server. Its works well. Instructions on how to use are in the WriteTextDocument class. It is usable on any server. You might need to change the packaging at the top though.

ReadFile.java


--- Code: Java ---package org.regicide.util.write; import java.io.BufferedReader;import java.io.FileReader;import java.io.IOException; /** * ReadFile.java *  * @author RiiPiiNFtW  * @MSN [email protected] */ public class ReadFile {                private String path;                public ReadFile(String file_path) {                path = file_path;        }        public String[] OpenFile() throws IOException {                                FileReader fr = new FileReader(path);                BufferedReader textReader = new BufferedReader(fr);                                int numberOfLines = 3;                String[] textData = new String[numberOfLines];                                int i;                for (i = 0; i < numberOfLines; i++) {                        textData[i] = textReader.readLine();                }                                textReader.close();                return textData;        }        int readLines () throws IOException {                                FileReader file_to_read = new FileReader(path);                BufferedReader br = new BufferedReader(file_to_read);                                int numberOfLines = 0;                                while ((br.readLine()) != null) {                        numberOfLines++;                }                br.close();                                return numberOfLines;        }} 
WriteTextDocument.java


--- Code: Java ---package org.regicide.util.write; import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter; /** * WriteTextDocument.java *  * @author RiiPiiNFtW  * @MSN [email protected] */ /* * @How to Use: * Declare this in the used Class: static String file_name = "SET_PATH"; *  * Declare this to grab statically: * private final static WriteTextDocument data = new WriteTextDocument(file_name, true); *  * Use this to write: * data.writeToFile("String Here"); */ public class WriteTextDocument {        private String path;        private boolean append_to_file = false;                public WriteTextDocument(String file_path) {                path = file_path;        }        public WriteTextDocument(String file_path, boolean append_value) {                path = file_path;                append_to_file = append_value;        }        public void writeToFile(String textline) throws IOException {                FileWriter write = new FileWriter(path, append_to_file);                PrintWriter print_line = new PrintWriter(write);                                print_line.printf("%s" + "%n", textline);                print_line.close();        }} 

RandQm:

--- Quote from: enzo42 on February 14, 2012, 09:41:49 AM ---Delta snippet's i made:

--- End quote ---

I like the part where it's 95% similar to what I released as tutorial a while back.

Anyways, thought about sharing this, coming from my survival project.

--- Code: Java ---package org.content.engine.survivalActions; import org.content.engine.Client;import org.content.engine.PlayerHandler;import org.content.engine.Server;import org.content.events.Task;import org.content.util.Misc; public class BananaTree {                /**         * @Ultimate_Survival_Project         * @Author_RandQm         * @Date: 16 February 2012         */         private boolean succesAttempt = true;        public int cX, cY;                private void attemptingTimer (final int playerId, final int xSpot, final int ySpot) {                final Client c = (Client) PlayerHandler.players[playerId];                        Server.getTaskScheduler().schedule(new Task(3, false) {                                @Override                                protected void execute() {                                        if (c.disconnected) {                                                stop();                                                return;                                        }                                        if (succesAttempt == false) {                                                c.sM("but the tree seems to be empty.");                                                                                        stop();                                                return;                                        }                                        if (succesAttempt == true && c.freeSlots() == 2) {                                                c.sM("You didn't have enough space in your inventory for the 3 banana's you found.");                                                c.sM("1 banana glides out of your backpack and dissapears in the sand.");                                                c.addItem(1963, 1);                                                c.addItem(1963, 1);                                                cX = xSpot;                                                cY = ySpot;                                                stop();                                                return;                                        }                                        if (succesAttempt == true && c.freeSlots() == 1) {                                                c.sM("You didn't have enough space in your inventory for the 3 banana's you found.");                                                c.sM("2 banana's glide out of your backpack and dissapear in the sand.");                                                c.addItem(1963, 1);                                                cX = xSpot;                                                cY = ySpot;                                                stop();                                                return;                                        }                                        if (succesAttempt == true && c.freeSlots() == 0) {                                                c.sM("You didn't have enough space in your inventory for the 3 banana's you found.");                                                c.sM("The banana's glide out of your backpack and dissapear in the sand.");                                                cX = xSpot;                                                cY = ySpot;                                                stop();                                                return;                                        }                                        if (succesAttempt == true && c.freeSlots() >= 3) {                                                c.sM("You managed to pick 3 banana's!");                                                c.addItem(1963, 1);                                                c.addItem(1963, 1);                                                c.addItem(1963, 1);                                                cX = xSpot;                                                cY = ySpot;                                                stop();                                                return;                                        }                                }                        });        }         private void getBananas (final int playerId, int xSpot, int ySpot) {                final Client c = (Client) PlayerHandler.players[playerId];                if (!c.playerHasItem(5605)) {                        c.sM("You need a knife to cut off the banana's.");                }                if (cX == xSpot && cY == ySpot) {                        c.sM("You have already searched for banana's in this tree.");                }                else if (c.playerHasItem(5605)) {                        c.sM("You search for some banana's...");                        if (Misc.random(2) == 1) {                                succesAttempt = false;                                attemptingTimer(playerId, xSpot, ySpot);                        } else {                                succesAttempt = true;                                attemptingTimer(playerId, xSpot, ySpot);                        }                }        }                public void searchBananas(final int playerId, int treeID, int xSpot, int ySpot) {                        if ((treeID == 2073 || treeID == 2074 || treeID == 2075 || treeID == 2076 || treeID == 2077 || treeID == 2078                                         || treeID == 4749 || treeID == 4750 || treeID == 4751 || treeID == 4752 || treeID == 4753 || treeID == 4754                                         || treeID == 6578 || treeID == 6579) && cX != xSpot && cY != ySpot) {                                getBananas(playerId, xSpot, ySpot);                        }                }} 
In your object clicking packet:

--- Code: Java ---Server.BananaTree.searchBananas(c.playerId, objectID, objectX, objectY); 
don't forget to declare BananaTree in your server class.

masan05:
My first one is how to make people drop stuff on death.


This is just a easy tut on how when people die, they drop stuff. But they might drop 1item they might drop 12 items. its all random. Please make a backup of all files incase i mess up your files. I kept trying to find how to make it so when people die they drop items. I finally just spent a few minutes thinking and came up with this for you guys, I as of a now do not use this, but hopefully this helps you :).

This is my second tut!

difficulty is 1, 10;

Ok, go to player.java and add this:


--- Code: ---public int totaldropped = 1;
public int totaldropped2 = 3;
--- End code ---

then search for:


--- Code: ---droploot
--- End code ---

until you see something like this:

--- Code: ---public void dropLoot() {
--- End code ---

Then Replace that entire void with this.

--- Code: ---public void dropLoot() {
final Player p2 = World.getInstance().getPlayerList().get(this.giveDrop);
this.sm("Hurry and get your loot!");
int itemid;
int price2 = Misc.random(125000000);
if(this.realsafezone()) {
return;
}
List<Item> itemsInHand = new ArrayList<Item>();
for(int i = 0; i < Inventory.SIZE; i++) {
Item item = this.getInventory().getContainer().get(i);
if(item != null) {
if (totaldropped2 == 3) {
itemsInHand.remove(item);
World.getInstance().getItemManager().createDropGroundItem(p2, this.getLocation(), item);
this.getActionSender().sendCreateObject(400, 0, this.getLocation().getX(), this.getLocation().getY(), 0, 10);
totaldropped2--;
} else
if (Misc.random(5) >= 3) {
if (totaldropped2 != 0) {
itemsInHand.remove(item);
World.getInstance().getItemManager().createDropGroundItem(p2, this.getLocation(), item);
this.getActionSender().sendCreateObject(400, 0, this.getLocation().getX(), this.getLocation().getY(), 0, 10);
totaldropped2--;
} else {
itemsInHand.add(item);
}
} else {
itemsInHand.add(item);
}
}
}
for(int i = 0; i < Equipment.SIZE; i++) {
Item item = this.getEquipment().getContainer().get(i);
if(item != null) {
if (Misc.random(5) >= 3) {
if (totaldropped != 0) {
itemsInHand.remove(item);
World.getInstance().getItemManager().createDropGroundItem(p2, this.getLocation(), item);
totaldropped--;
} else {
itemsInHand.add(item);
}

} else {
itemsInHand.add(item);
}
}
}
this.getInventory().reset();
this.getEquipment().reset();
int keep = 10;
if(this.PROTECTITEM == true) {
keep = 13;
}
if(this.getHeadIcons().isSkulled()) {
keep = 38;
}
if(keep > 0) {
Collections.sort(itemsInHand, new Comparator<Item>() {
@Override
public int compare(Item arg0, Item arg1) {
                    int a0 = arg0.getDefinition().getPrice().getNormalPrice();
                    int a1 = arg1.getDefinition().getPrice().getNormalPrice();
return a1 - a0;
}
});
List<Item> toRemove = new ArrayList<Item>();
for(int i = 0; i < itemsInHand.size(); i++) {
Item item = itemsInHand.get(i);
if(item.getDefinition().isStackable() || item.getDefinition().isNoted()) {
continue;
}
if(keep > 0) {
toRemove.add(item);
keep--;
} else {
break;
}
}
for(Item i : toRemove) {
itemsInHand.remove(i);
this.getInventory().addItem(i.getId(), 1);
}
}
for(Item i : itemsInHand) {
//World.getInstance().getItemManager().createDropGroundItem(this.getKiller(), this.getLocation(), i);
}
totaldropped = 1;
totaldropped2 = 3;
}
--- End code ---
Now you might have to do this.
Go into DeathEvent.java
look for:

--- Code: ---else if(entity instanceof Player) {
--- End code ---
it should look like this:

--- Code: ---if (entity.isDead() == false) {
entity.teleport(Entity.DEFAULT_LOCATION);
return;
}
--- End code ---
Under that add:

--- Code: --- entity.dropLoot();
--- End code ---

Save Compile and run.

My second one is a report system.


Ok this is a report system. Just need to know basic knowledge of how to copy and paste.

Lets start in player.java

First off int these:

--- Code: ---public int playerreported1 = 0;
public int playerreported2 = 0;
--- End code ---

then search for:

--- Code: ---tick()
--- End code ---

Under the

--- Code: ---try {
--- End code ---

Add this


--- Code: --- if (playerreported1 > 0) {
playerreported1--;
}
if (playerreported2 > 0) {
playerreported2--;
}
--- End code ---

Ok now your done in that file, now go into ChatPacketHandler.java

under the ints and the:


--- Code: ---XStreamPlayerLoader.punish.writeTo(player.getUsername()+" = "+text, "./data/text/chatlog");
--- End code ---

add this under that:


--- Code: ---if (player.playerreported1 >= 1) {
XStreamPlayerLoader.punish.writeTo(player.getUsername()+" = "+text, "./data/text/reportlog1");
}
if (player.playerreported2 >= 1) {
if (text.contains("fudge") || text.contains("f") && text.contains("u") && text.contains("c") && text.contains("k") || text.contains("ass")
|| text.contains("damn") || text.contains("shit") || text.contains("sh!t") || text.contains("com") || text.contains("0") || text.contains("()") || text.contains("fag")) {
XStreamPlayerLoader.punish.writeTo(player.getUsername()+" = "+text, "./data/text/reportlog2");
}
}
--- End code ---
Where it says:
--- Code: ---text.contains
--- End code ---
, if they say whats in there and are currently reported it will add it to the file we will make. if you want to add more to that just add this:

--- Code: --- || text.contains("Whatyouwant")
--- End code ---

We are now done in that file.

Now go into commands.java or command.java and add these commands:


--- Code: ---if(cmd[0].equals("::reportother")) {
    Player other = World.getInstance().getPlayerList().get(World.getInstance().getIdFromName(command.substring((command.indexOf(" ") + 1))));
if (other != null && other.playerreported1 == 0) {
player.getActionSender().sendMessage("Report receieved.");
other.playerreported1 = 50;
}else{
player.getActionSender().sendMessage("Report failed because either you have reported within 30 seconds.");
player.getActionSender().sendMessage("or that person has already been report since the last 30 seconds.");
}
}
if(cmd[0].equals("::reportoffensive")) {
    Player other = World.getInstance().getPlayerList().get(World.getInstance().getIdFromName(command.substring((command.indexOf(" ") + 1))));
if (other != null && other.playerreported2 == 0) {
player.getActionSender().sendMessage("Report receieved.");
other.playerreported2 = 200;
}else{
player.getActionSender().sendMessage("Report failed because either you have reported within 2 minutes.");
player.getActionSender().sendMessage("or that person has already been report since the last 2 minutes.");
}
}
--- End code ---

Now your almost done.
Now go to your main file for source and click on:
Data (file) >> Text (file)

And add these text documents names with nothing in them:
reportlog1

and

reportlog2

reportlog1 is for the other report the reportlog2 is where the offensive language will go.

Compile Run Save ...enjoy :)

arham 4:
In my process to make music in my rsps, I put in Music.java an enum and it lists all the cities and there ID list. So, it makes it easier as you can refer it as InVarrock.

My incomplete Enum list:


--- Code: Java ---   public static enum CITY_MUSIC {      VARROCK(169),      WATERFALL(125),      FALLADOR(15),          LUMBRIDGE(2),          START(1),          DRAYNOR(3),          STRANGE(7),          CAMELOT(8),          RIMMINGTON(12),          CW_WIN(16),          FLOWER_MUSIC(18),          FLOWER_MUSIC2(20),          FLOWER_MUSIC3(21),          GOBLIN(23),          KBD_ATTACK_2(25),          SARIM(35),          KHARID(36),          CLAN_WARS(47),          WILDERNESS(48),          FALADOR_CROSS(49),          KHARID2(50),          FAIRY(52),          TO_VARROCK(54),          ESSENSE(57),          JUNGLE(58),          CANAFIS(61),          TUTORIAL(62),          SWAMP(64),          DESSERT(69),          FALADOR2(72),          LOST(73),          CATHERBY(74),          LUMBRIDGE2(76),          FLOWER_MUSIC4(77),          DRAYNOR_MANOR(87),          KARAMJA(92),                          //F2P Side          SAWMILL(93),          EDGEVILLE(96),          WILDERNESS2(103),          CAMELOT2(104),          VARROCKR(106),                        //Varrock Center          UP(126),          AROUND(107),                          //Of FALLY and RIMMINGTON          lANDV(111),                           //Lumbridge - Varrock          ARDOUGNE(116),          FAIRY2(118),          FISHING(119),                         //At CATHERBY          WILDERNESS3(121),          MININGINK(123);                       //The Mine in AL-KHARID 
Will add the full when I am done with it.

TIP: If anyone will use this, you can refer this like:


--- Code: Java ---public boolean isInvarrock()        {    if (isInArea(3158,3469, 3258, 3258)) return true;    return false;} 
for Varrock under


--- Code: Java ---public boolean HasItemAmount(int itemID, int itemAmount) { 

Ryley:
What you said made no sense, like this?

--- Code: Java ---     private CITY_MUSIC play;     public boolean isInvarrock() {        return (isInArea(3158,3469, 3258, 3258));    }     if (isInVarrock()) {        playSound(play.VARROCK);    } else {        stopSound();    }     

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version
Powered by SMFPacks SEO Pro Mod |