Well I figured since I'm going to take a break from rsps's for a while might as well release this for all the people who are struggling trying to add content to their hyperion sources. I did rip the interfaces and the loading of the interfaces from some other client but I did create all of the code server-sided and as well as some of the code client-sided. This tutorial will be spoonfed.
CLIENT-SIDED:
[SPOIL]First thing we need to do is add the interfaces so download: https://dl.dropboxusercontent.com/u/...lan%20chat.zip and put that in your cache sprites or if you want pack them into your cache.
Next thing is in RSInterface add these methods
Code:
public static void clanChatSetup(TextDrawingArea[] tda) {
RSInterface rsi = addInterface(18300);
rsi.totalChildren(25);
int count = 0;
/* Background */
addSprite(18301, 1, "/Clan Chat/sprite");
rsi.child(count++, 18301, 14, 18);
/* Close button */
addButton(18302, 0, "/Clan Chat/close", "Close");
interfaceCache[18302].atActionType = 3;
rsi.child(count++, 18302, 475, 26);
/* Clan Setup title */
addText(18303, "Clan Setup", tda, 2, 0xFF981F, true, true);
rsi.child(count++, 18303, 256, 26);
/* Setup buttons */
String[] titles = { "Clan name:", "Who can enter chat?",
"Who can talk on chat?", "Who can kick on chat?",
"Who can ban on chat?" };
String[] defaults = { "Not set", "Anyone", "Anyone", "Only me",
"Only me" };
String[] whoCan = { "Anyone", "Recruit", "Corporal", "Sergeant",
"Lieutenant", "Captain", "General", "Only Me" };
for (int index = 0, id = 18304, y = 50; index < titles.length; index++, id += 3, y += 40) {
addButton(id, 2, "/Clan Chat/sprite", "");
interfaceCache[id].atActionType = 0;
if (index > 0) {
interfaceCache[id].actions = whoCan;
} else {
interfaceCache[id].actions = new String[] { "Change title",
"Delete clan" };
;
}
addText(id + 1, titles[index], tda, 0, 0xFF981F, true, true);
addText(id + 2, defaults[index], tda, 1, 0xFFFFFF, true, true);
rsi.child(count++, id, 25, y);
rsi.child(count++, id + 1, 100, y + 4);
rsi.child(count++, id + 2, 100, y + 17);
}
/* Table */
addSprite(18319, 5, "/Clan Chat/sprite");
rsi.child(count++, 18319, 197, 70);
/* Labels */
int id = 18320;
int y = 74;
addText(id, "Clan Members", tda, 2, 0xFF981F, false, true);
rsi.child(count++, id++, 202, y);
addText(id, "Banned Members", tda, 2, 0xFF981F, false, true);
rsi.child(count++, id++, 339, y);
/* Ranked members list */
RSInterface list = addInterface(id++);
int lines = 100;
list.totalChildren(lines);
String[] ranks = { "Demote", "Recruit", "Corporal", "Sergeant",
"Lieutenant", "Captain", "General", "Owner" };
list.childY[0] = 2;
// System.out.println(id);
for (int index = id; index < id + lines; index++) {
addText(index, "", tda, 1, 0xffffff, false, true);
interfaceCache[index].actions = ranks;
list.children[index - id] = index;
list.childX[index - id] = 2;
list.childY[index - id] = (index - id > 0 ? list.childY[index - id
- 1] + 14 : 0);
}
id += lines;
list.width = 119;
list.height = 210;
list.scrollMax = (lines * 14) + 2;
rsi.child(count++, list.id, 199, 92);
/* Banned members list */
list = addInterface(id++);
list.totalChildren(lines);
list.childY[0] = 2;
// System.out.println(id);
for (int index = id; index < id + lines; index++) {
addText(index, "", tda, 1, 0xffffff, false, true);
interfaceCache[index].actions = new String[] { "Unban" };
list.children[index - id] = index;
list.childX[index - id] = 0;
list.childY[index - id] = (index - id > 0 ? list.childY[index - id
- 1] + 14 : 0);
}
id += lines;
list.width = 119;
list.height = 210;
list.scrollMax = (lines * 14) + 2;
rsi.child(count++, list.id, 339, 92);
/* Table info text */
y = 47;
addText(id, "You can manage both ranked and banned members here.", tda,
0, 0xFF981F, true, true);
rsi.child(count++, id++, 337, y);
addText(id, "Right click on a name to edit the member.", tda, 0,
0xFF981F, true, true);
rsi.child(count++, id++, 337, y + 11);
/* Add ranked member button */
y = 75;
/* Hovers */
int[] clanSetup = { 18302, 18304, 18307, 18310, 18313, 18316 };
String[] names = { "close", "sprite", "sprite", "sprite", "sprite",
"sprite" };
int[] ids = { 1, 3, 3, 3, 3, 3, 1, 1 };
for (int index = 0; index < clanSetup.length; index++) {
rsi = interfaceCache[clanSetup[index]];
rsi.disabledHover = imageLoader(ids[index],
"/Clan Chat/" + names[index]);
}
}
public static void clanChatTab(TextDrawingArea[] tda) {
RSInterface tab = addTabInterface(18128);
addHoverButton(18129, "/Clan Chat/SPRITE", 6, 72, 32, "Join Chat", 550, 18130, 5);
addHoveredButton(18130, "/Clan Chat/SPRITE", 7, 72, 32, 18131);
addHoverButton(18132, "/Clan Chat/SPRITE", 6, 72, 32, "Edit Clan", -1, 18133, 5);
addHoveredButton(18133, "/Clan Chat/SPRITE", 7, 72, 32, 18134);
addText(18135, "Join Chat", tda, 0, 0xff9b00, true, true);
addText(18136, "Clan Setup", tda, 0, 0xff9b00, true, true);
addSprite(18137, 37, "/Clan Chat/SPRITE");
addText(18138, "Clan Chat", tda, 1, 0xff9b00, true, true);
addText(18139, "Talking in: Not in chat", tda, 0, 0xff9b00, false, true);
addText(18140, "Owner: None", tda, 0, 0xff9b00, false, true);
addSprite(16126, 4, "/Clan Chat/SPRITE");
tab.totalChildren(13);
tab.child(0, 16126, 0, 221);
tab.child(1, 16126, 0, 59);
tab.child(2, 18137, 0, 62);
tab.child(3, 18143, 0, 62);
tab.child(4, 18129, 15, 226);
tab.child(5, 18130, 15, 226);
tab.child(6, 18132, 103, 226);
tab.child(7, 18133, 103, 226);
tab.child(8, 18135, 51, 237);
tab.child(9, 18136, 139, 237);
tab.child(10, 18138, 95, 1);
tab.child(11, 18139, 10, 23);
tab.child(12, 18140, 25, 38);
/* Text area */
RSInterface list = addTabInterface(18143);
list.totalChildren(100);
for(int i = 18144; i <= 18244; i++) {
addText(i, "", tda, 0, 0xffffff, false, true);
}
for(int id = 18144, i = 0; id <= 18243 && i <= 99; id++, i++) {
interfaceCache[id].actions = new String[]{ "Edit Rank", "Kick", "Ban" };
list.children[i] = id; list.childX[i] = 5;
for(int id2 = 18144, i2 = 1; id2 <= 18243 && i2<= 99; id2++, i2++) {
list.childY[0] = 2;
list.childY[i2] = list.childY[i2 - 1] + 14;
}
}
list.height = 158; list.width = 174;
list.scrollMax = 1405;
}
And load them up with all your other custom interfaces in the client.
Now open up Client.java and add this method
Code:
public boolean inClan(String name) {
Predicate<String> a = (String s) -> s.toLowerCase().contains(name.toLowerCase());
return Arrays.stream(clanList).filter(Objects::nonNull).anyMatch(a);
}
Add this string array
private String[] clanList = new String[100];
look up case 126:
under
Code:
if (text.startsWith("www.")) {
launchURL(text);
pktType = -1;
return true;
}
SERVER-SIDED
[SPOIL]A lot of classes first one is Clan
Code:
package org.hyperion.rs2.content.clan;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Predicate;
import org.hyperion.rs2.Misc.SystemClock;
import org.hyperion.rs2.model.Player;
import org.hyperion.rs2.model.World;
/**
* A clan
*
* @author Dan0194
*/
public final class Clan {
/**
* The players clan list
*/
public ClanList<Player> players = new ClanList<Player>(18144, 18244, ClanConstants.MAX_PLAYERS) {
/**
*
* Makes it update for everyone in the clan
*
* (non-Javadoc)
* @see org.hyperion.rs2.content.clan.ClanList#update(org.hyperion.rs2.model.Player)
*/
@Override
public void update(Player owner) {
getList().stream().forEach((Player p) -> super.update(p));
}
/**
* (non-Javadoc)
* @see org.hyperion.rs2.content.clan.ClanList#onUpdate(org.hyperion.rs2.model.Player, java.util.Optional, int)
*/
@Override
public void onUpdate(Player owner, Optional<Player> value, int slot) {
owner.getActionSender().sendString(slot, value.isPresent() ? "<clan=" + getClanPlayerByName(value.get().getName()).get().getRank().getPosition() + "> " + value.get().getName() : "");
}
};
/**
* The banned players clan list
*/
private ClanList<String> bannedPlayers = new ClanList<String>(18424, 18503, ClanConstants.MAX_PLAYERS_IN_CLAN) {
/**
* (non-Javadoc)
* @see org.hyperion.rs2.content.clan.ClanList#onUpdate(org.hyperion.rs2.model.Player, java.util.Optional, int)
*/
@Override
public void onUpdate(Player owner, Optional<String> value, int slot) {
owner.getActionSender().sendString(slot, value.isPresent() ? "<clan=" + getClanPlayerByName(value.get()).get().getRank().getPosition() + "> " + value.get() : "");
}
};
private ClanList<ClanPlayer> clanPlayers = new ClanList<ClanPlayer>(18323, 18422, ClanConstants.MAX_PLAYERS_IN_CLAN) {
/**
* (non-Javadoc)
* @see org.hyperion.rs2.content.clan.ClanList#onUpdate(org.hyperion.rs2.model.Player, java.util.Optional, int)
*/
@Override
public void onUpdate(Player owner, Optional<ClanPlayer> value, int slot) {
owner.getActionSender().sendString(slot, value.isPresent() ? "<clan=" + getClanPlayerByName(value.get().getName()).get().getRank().getPosition() + "> " + value.get().getName() : "");
}
};
/**
* The map of all the kicked players in the clan chat
*/
private Map<String, SystemClock> kickedPlayers = new HashMap<>(ClanConstants.MAX_PLAYERS_IN_CLAN);
/**
* The rank able enter the chat
*/
private ClanRank enterRank = ClanRank.NORMAL;
/**
* The rank able to talk in the chat
*/
private ClanRank talkRank = ClanRank.NORMAL;
/**
* The rank able to kick people in the chat
*/
private ClanRank kickRank = ClanRank.OWNER;
/**
* The rank able to ban people in the chat
*/
private ClanRank banRank = ClanRank.OWNER;
/**
* The owner of the clan
*/
private String owner;
/**
* The title of the clan
*/
private String title;
/**
* If the clan is on loot share
*/
private boolean lootshare;
/**
* @param owner The owner of the clan
* @param title The title of the clan
*/
public Clan(String owner, String title) {
this.title = title;
this.owner = owner;
}
/**
* Gets an optional of a clan player depending on the name
*
* @return an optional with the value of clan player or an optional that is empty
*/
public Optional<ClanPlayer> getClanPlayerByName(String name) {
Predicate<ClanPlayer> player = (ClanPlayer c) -> c.getName().equals(name);
return clanPlayers.getList().stream().filter(player).findFirst();
}
/**
* Gets an optional of a player in a clan chat depending on name
*
* @return an optional with the value of the player or an optional that is empty
*/
public Optional<Player> getPlayerByName(String name) {
Predicate<Player> player = (Player p) -> p.getName().equals(name);
return players.getList().stream().filter(player).findFirst();
}
/**
* Kicks a player out of the clan chat
*
* @param player The player kicking the player out of the chat
* @param kickedPlayer The player being kicked out of the chat
*/
public void kick(Player player, Player kickedPlayer) {
/** Gets the clan player depending on the name */
ClanPlayer clanPlayer = getClanPlayerByName(player.getName()).get();
/** If the player is too low to kick people out of the clan chat */
if(clanPlayer.getRank().getPosition() < kickRank.getPosition()) {
player.getActionSender().sendMessage("Your too low of a rank to kick someone out.");
return;
}
/** If the player they are trying to kick out is themselves*/
if(kickedPlayer.equals(player)) {
player.getActionSender().sendMessage("You can't kick yourself out of the chat.");
return;
}
/** If the player is trying to kick out the creator of the clan chat */
if(kickedPlayer.getName().equals(owner)) {
player.getActionSender().sendMessage("You can't kick the creator of the clan.");
return;
}
/** Kicks them out of the clan chat */
eject(kickedPlayer, "You have been kicked out of the chat.");
kickedPlayer.getClanManager().clear();
SystemClock clock = new SystemClock(ClanConstants.KICK_TIMER * 1000);
kickedPlayers.put(kickedPlayer.getName(), clock);
clock.reset();
}
/**
* Bans a player from the clan
*
* @param the player The player banning the other player
* @param bannedPlayer The player being banned
*/
public void ban(Player player, Player bannedPlayer) {
/** Gets the clan player */
ClanPlayer clanPlayer = this.getClanPlayerByName(player.getName()).get();
/** If the player is too low to ban someone from the chat */
if(clanPlayer.getRank().getPosition() < banRank.getPosition()) {
player.getActionSender().sendMessage("Your rank is too low to ban someone.");
return;
}
/** If the player is trying to ban themselves */
if(player.equals(bannedPlayer)) {
player.getActionSender().sendMessage("You can't ban yourself.");
return;
}
/** If the player is trying to ban the creator of the chat */
if(bannedPlayer.getName().equals(owner)) {
player.getActionSender().sendMessage("You can't ban the creator of the clan.");
return;
}
/** Bans the player from the clan chat*/
bannedPlayers.getList().add(bannedPlayer.getName());
eject(bannedPlayer, "You have been banned from the chat.");
bannedPlayer.getClanManager().clear();
}
/**
* Unbans a player from the clan
*
* @param owner The creator unbanning the player from the chat
* @param bannedPlayer The name of the player being unbanned
*/
public void unban(Player owner, String bannedPlayer) {
bannedPlayers.getList().remove(bannedPlayer);
bannedPlayers.update(owner);
}
/**
* Sends message to entire clan chat
*
* @param player The player sending the message
* @param message being sent to entire clan chat
*/
public void message(Player player, String message) {
/** Gets the clan player from the player's name */
ClanPlayer rank = getClanPlayerByName(player.getName()).get();
/** If the player is too low rank to talk in the chat */
if(rank.getRank().getPosition() < talkRank.getPosition()) {
player.getActionSender().sendMessage("Your rank is too low to talk in the chat.");
return;
}
/** Sends the message to everyone in the chat */
players.getList().stream().filter(Objects::nonNull).forEach((Player p) -> p.getActionSender().sendClanMessage(player.getName(), message, title, player.getRights().toInteger()));
}
/**
* Ranks up a player in the clan
*
* @param owner The owner who is ranking the player
* @param clanPlayer the player in the clan who is being ranked
* @param rank The new rank
*/
public void rank(Player owner, ClanPlayer clanPlayer, ClanRank rank) {
/** If the owner is trying to rank himself */
if(this.owner.equals(clanPlayer.getName())) {
owner.getActionSender().sendMessage("You cannot set a rank for yourself!");
return;
}
/** If the owner is trying to demote an admin */
if(clanPlayer.getRank().equals(ClanRank.ADMIN)) {
owner.getActionSender().sendMessage("You cannot demote an admin.");
return;
}
/** If the rank is the same as the Clan player's rank */
if(rank.equals(clanPlayer.getRank())) {
owner.getActionSender().sendMessage("That player is already that rank.");
return;
}
/** Makes sure your not setting the rank to a friend */
rank = rank.equals(ClanRank.FRIEND) ? ClanRank.NORMAL : rank;
/** Sets the rank and updates all the players in the clan */
clanPlayer.setRank(rank);
clanPlayers.update(owner);
/** Gets an optional of the clan player */
Optional<Player> rankedPlayer = getPlayerByName(clanPlayer.getName());
/** If the clan player is in the chat update the entire chat */
if(rankedPlayer.isPresent()) {
players.update();
}
}
/**
* Deletes the entire clan
*
* @param owner The owner deleting the clan
*/
public void delete(Player owner) {
/** Creates a new list of the entire clan to be looped through */
new ArrayList<Player>(players.getList()).stream().forEach(new Consumer<Player>() {
@Override
public void accept(Player t) {
/** The message the player will be sent */
String message = t.equals(owner) ? "Deleting clan..." : "The clan you were in has been deleted.";
/** Ejects the player out of the clan */
eject(t, message);
t.getClanManager().clear();
}
});
/** Removes the clan from the global list of all clans */
World.getWorld().getClans().remove(this);
owner.getActionSender().sendMessage("You delete the clan.");
owner.getClanManager().clear();
}
/**
* Ejects a player out of the clan
*
* @param player The player being ejected out of clan
* @param message The message that will be sent to the player
*/
public void eject(Player player, String message) {
player.getActionSender().sendMessage(message);
players.getList().remove(player);
player.getClanManager().setCurrentClan(null);
players.update();
}
/**
* Sets the new enter rank
*
* @param enterRank to the enterRank
*/
public void setEnterRank(ClanRank enterRank) {
this.enterRank = enterRank;
}
/**
* Sets the new talk rank
*
* @param talkRank to the talkRank
*/
public void setTalkRank(ClanRank talkRank) {
this.talkRank = talkRank;
}
/**
* Sets the new kick rank
*
* @param kickRank to the kickRank
*/
public void setKickRank(ClanRank kickRank) {
this.kickRank = kickRank;
}
/**
* Sets the new ban rank
*
* @param banRank to banRank
*/
public void setBanRank(ClanRank banRank) {
this.banRank = banRank;
}
/**
* Gets the owner of the clan
*
* @return owner
*/
public String getOwner() {
return owner;
}
/**
* Gets the title of the clan
*
* @return title
*/
public String getTitle() {
return title;
}
/**
* Gets if the clan is on lootshare
*
* @return lootshare
*/
public boolean onLootshare() {
return lootshare;
}
/**
* Sets the new title of the clan
*
* @param title to the title
*/
public void setTitle(String title) {
this.title = title;
}
/**
* Sets if the clan is on lootshare
*
* @param lootshare to lootshare
*/
public void setLootshare(boolean lootshare) {
this.lootshare = lootshare;
}
/**
* Gets the clan list of all players
*
* @return clanPlayers
*/
public ClanList<ClanPlayer> getClanPlayers() {
return clanPlayers;
}
/**
* Gets the players in the chat
*
* @return players
*/
public ClanList<Player> getPlayers() {
return players;
}
/**
* Gets the banned players list
*
* @return bannedPlayers
*/
public ClanList<String> getBannedPlayers() {
return bannedPlayers;
}
/**
* Gets the enter rank
*
* @return enterRank
*/
public ClanRank getEnterRank() {
return enterRank;
}
/**
* Gets the talk rank
*
* @return talkRank
*/
public ClanRank getTalkRank() {
return talkRank;
}
/**
* Gets the kick rank
*
* @return kickRank
*/
public ClanRank getKickRank() {
return kickRank;
}
/**
* Gets the ban rank
*
* @return banRank
*/
public ClanRank getBanRank() {
return banRank;
}
/**
* Gets the kicked players
*
* @return kickedPlayers
*/
public Map<String, SystemClock> getKickedPlayers() {
return kickedPlayers;
}
}
Code:
package org.hyperion.rs2.content.clan;
import java.util.Optional;
import java.util.stream.IntStream;
import org.hyperion.rs2.Misc.SystemClock;
import org.hyperion.rs2.model.Player;
import org.hyperion.rs2.model.World;
import org.hyperion.rs2.packet.ActionPacketHandler;
import org.hyperion.rs2.packet.ActionPacketHandler.Type;
/**
* Clan Manager for the player
*
* @author Dan0194
* TODO add ability to be a friend in clan
*/
public final class ClanManager {
/**
* The current clan the player is in
*/
private Clan currentClan;
/**
* The player
*/
private Player player;
/**
* @param player to the player
*/
public ClanManager(Player player) {
this.player = player;
}
/**
* The setup screen for the clan
*/
public void initSetup() {
/** Sends the clan setup interface*/
player.getActionSender().sendInterface(ClanConstants.CLAN_SETUP);
/** Updates the setup interface*/
updateSetup();
}
/**
* Updates the setup screen
*/
public void updateSetup() {
/** If the player is viewing the clan set up screen */
if(player.getInterfaceState().getCurrentInterface() == ClanConstants.CLAN_SETUP) {
/** Makes an optional of the players clan */
Optional<Clan> clan = World.getWorld().getClanByOwner(player.getName());
/** If player has a clan then update the interface */
if(clan.isPresent()) {
player.getActionSender().sendString(18306, clan.get().getTitle());
player.getActionSender().sendString(18309, clan.get().getEnterRank().getText());
player.getActionSender().sendString(18312, clan.get().getTalkRank().getText());
player.getActionSender().sendString(18315, clan.get().getKickRank().getText());
player.getActionSender().sendString(18318, clan.get().getBanRank().getText());
clan.get().getBannedPlayers().update(player);
clan.get().getClanPlayers().update(player);
}
}
}
/**
* Setting a new title of the clan
*/
public void setTitle() {
/** Creates new enter name chatbox */
ActionPacketHandler.submit(player, Type.ENTER_NAME, new ActionPacketHandler() {
@Override
public void execute(Object object) {
/** The title of the clan */
String title = (String) object;
/** An optional if a clan already exists for the player */
Optional<Clan> clan = World.getWorld().getClanByOwner(player.getName());
/** If a clan does not exist */
if(!clan.isPresent()) {
/** Creates a new clan */
Clan newClan = new Clan(player.getName(), title);
newClan.getClanPlayers().getList().add(new ClanPlayer(player.getName(), ClanRank.OWNER));
World.getWorld().getClans().add(newClan);
player.getActionSender().sendMessage("You have just created a new clan.");
} else {
/** Sets new title for clan */
clan.get().setTitle(title);
}
/** Sends title to clan edit screen */
player.getActionSender().sendString(18306, title);
}
});
}
/**
* Makes player join a clan in the game
*/
public void join() {
/** Creates enter name text box */
ActionPacketHandler.submit(player, Type.ENTER_NAME, new ActionPacketHandler() {
@Override
public void execute(Object object) {
/** Gets the input */
String name = (String) object;
player.getActionSender().sendMessage("Joining clan...");
/** Makes an optional on the input name's clan */
Optional<Clan> clan = World.getWorld().getClanByOwner(name);
/** If that player does not have a clan */
if(!clan.isPresent()) {
player.getActionSender().sendMessage("This clan does not exist.");
return;
}
/** Joins the clan */
join(clan.get());
}
});
}
/**
*
* Joins a specific clan in the game
*
* @param clan The clan the player will be attempting to join
*/
public void join(Clan clan) {
/** If the clan is full of players */
if(clan.getPlayers().getList().size() >= ClanConstants.MAX_PLAYERS) {
player.getActionSender().sendMessage("The clan you are trying to enter is full.");
return;
}
/** If the player is banned from the clan */
if(clan.getBannedPlayers().getList().contains(player.getName())) {
player.getActionSender().sendMessage("You have been banned from this clan chat.");
return;
}
/** Makes an optional if the player is kicked */
Optional<SystemClock> kickedPlayer = Optional.ofNullable(clan.getKickedPlayers().get(player.getName()));
/** If the player is kicked and the timer has not finished */
if(kickedPlayer.isPresent() && !kickedPlayer.get().isFinished()) {
player.getActionSender().sendMessage("You were kicked out of the chat. Please wait a while before joining again.");
clan.getKickedPlayers().remove(player.getName());
return;
}
/** Gets an Optional of the clan player from the players name */
Optional<ClanPlayer> clanPlayer = clan.getClanPlayerByName(player.getName());
/** If the player has never joined the clan before */
if(!clanPlayer.isPresent()) {
/** If player is an admin */
if(player.getRights().toInteger() >= 2) {
/** Sets rights to admin rank */
clan.getClanPlayers().getList().add(new ClanPlayer(player.getName(), ClanRank.ADMIN));
} else {
/** Sets rights to normal rank */
clan.getClanPlayers().getList().add(new ClanPlayer(player.getName(), ClanRank.NORMAL));
}
}
/** Gets the new clan player */
ClanPlayer player = clan.getClanPlayerByName(this.player.getName()).get();
/** If the players rank is too low to enter the chat */
if(player.getRank().getPosition() < clan.getEnterRank().getPosition()) {
this.player.getActionSender().sendMessage("Your rank is too low to enter this clan.");
return;
}
/** Updates the interfaces strings*/
this.player.getActionSender().sendString(18135, "Leave Chat");
this.player.getActionSender().sendString(18139, "Talking in: " + clan.getTitle());
this.player.getActionSender().sendString(18140, "Owner: " + clan.getOwner());
this.player.getActionSender().sendMessage("You have joined " + clan.getOwner() + "'s chat / You are talking in " + clan.getTitle());
/** Adds the player to the clan list */
clan.getPlayers().getList().add(this.player);
/** Sets the player's current clan to this one */
currentClan = clan;
/** Updates the clan roster */
clan.getPlayers().update();
}
/**
* Clears the line ids of the clan chat
*/
public void clear() {
IntStream.range(18144, 18244).forEach(i -> player.getActionSender().sendString(i, ""));
IntStream.range(18323, 18422).forEach(i -> player.getActionSender().sendString(i, ""));
IntStream.range(18424, 18503).forEach(i -> player.getActionSender().sendString(i, ""));
player.getActionSender().sendString(18139, "Talking in");
player.getActionSender().sendString(18140, "Owner:");
player.getActionSender().sendString(18135, "Join Chat");
player.getActionSender().sendString(18306, "Not set");
player.getActionSender().sendString(18309, "Anyone");
player.getActionSender().sendString(18312, "Anyone");
player.getActionSender().sendString(18315, "Only me");
player.getActionSender().sendString(18318, "Only me");
}
/**
* Gets the clan the player is in
*
* @return clan
*/
public Clan getCurrentClan() {
return currentClan;
}
/**
* Sets the current clan the player is in
*
* @param currentClan to the currentClan
*/
public void setCurrentClan(Clan currentClan) {
this.currentClan = currentClan;
}
}
ClanList class
Code:
package org.hyperion.rs2.content.clan;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.IntConsumer;
import java.util.stream.IntStream;
import org.hyperion.rs2.model.Player;
/**
* A clan collection
*
* @author Dan0194
*/
public abstract class ClanList<T> {
/**
* The list containing the data
*/
private final List<T> list;
/**
* The first line id in the list
*/
private int first;
/**
* The second line id in the list
*/
private int second;
/**
* @param first to the first
* @param second to the second
*/
public ClanList(int first, int second, int size) {
this.list = new ArrayList<T>(size);
this.first = first;
this.second = second;
}
/**
* Called when there is an update
*
* @param owner The owner recieving the update
* @param value The optional value given on update
* @param slot The slot is the line id of the update
*/
public abstract void onUpdate(Player owner, Optional<T> value, int slot);
/**
* Sends an update that has no owner value
*/
public void update() {
update(null);
}
/**
* Starts the updating of the list
*
* @param owner The person recieving the update
*/
public void update(Player owner) {
/** Loops through all the line ids */
IntStream.range(first, second).forEach(new IntConsumer() {
@Override
public void accept(int value) {
/** The slot of the line id */
int slot = value - first;
/** The Optional value */
Optional<T> name = slot < list.size() ? Optional.of(list.get(slot)) : Optional.empty();
/** Calls the update method */
onUpdate(owner, name, value);
}
});
}
/**
* Gets the list
*
* @return list
*/
public List<T> getList() {
return list;
}
}
ClanConstants class
Code:
package org.hyperion.rs2.content.clan;
/**
* All of the clan constants
*
* @author Dan0194
*/
public final class ClanConstants {
/**
* The private constructor trying to keep it from being initiliazed
*
* @throws an AssertionError if the class is initliazed
*/
private ClanConstants() {
throw new AssertionError();
}
/**
* The help clan
*/
public static final Clan HELP_CLAN = new Clan("", "Help Chat");
/**
* The clan setup interface id
*/
public static final int CLAN_SETUP = 18300;
/**
* The maximum amount of players allowed in a clan
*/
public static final int MAX_PLAYERS_IN_CLAN = 100;
/**
* The maximum amount of players in a clan chat at once
*/
public static final int MAX_PLAYERS = 50;
/**
* The kick timer in seconds
*/
public static final int KICK_TIMER = 600;
/**
* The path to the XML file that holds all data on the clans
*/
public static final String CLAN_PATH = "./data/clans.xml";
}
ClanPlayer class
Code:
package org.hyperion.rs2.content.clan;
/**
* A player in a clan
*
* @author Dan0194
*/
public final class ClanPlayer {
/**
* The name of the player
*/
private String name;
/**
* The player's rank in the clan
*/
private ClanRank rank;
/**
* @param name to the name
* @param rank to the rank
*/
public ClanPlayer(String name, ClanRank rank) {
this.name = name;
this.rank = rank;
}
/**
* Gets the name of the player
*
* @return name
*/
public String getName() {
return name;
}
/**
* Gets the rank of the player
*
* @return rank
*/
public ClanRank getRank() {
return rank;
}
/**
* Sets a new rank for the player
*
* @param rank to the rank
*/
public void setRank(ClanRank rank) {
this.rank = rank;
}
}
Clan Rank enum
Code:
package org.hyperion.rs2.content.clan;
import java.util.Arrays;
import java.util.Optional;
import java.util.function.Predicate;
/**
* All of the clan ranks
*
* @author Dan0194
*/
public enum ClanRank {
/**
* A normal rank in the clan
*/
NORMAL(-1, "Anyone"),
/**
* A friend rank in the clan
*/
FRIEND(0, "Friends"),
/**
* A recruit rank in the clan
*/
RECRUIT(1, "Recruits"),
/**
* A corporal rank in the clan
*/
CORPORAL(2, "Corporals"),
/**
* A sergeant rank in the clan
*/
SERGEANT(3, "Sergeants"),
/**
* A lieutenant rank in the clan
*/
LIEUTENANT(4, "Lieutenants"),
/**
* A captain in the clan
*/
CAPTAIN(5, "Captains"),
/**
* A general in the clan
*/
GENERAL(6, "Generals"),
/**
* An owner in the clan
*/
OWNER(7, "Only me"),
/**
* An admin in the game
*/
ADMIN(8, "Administrator");
/**
* The position of the rank
*/
private int position;
/**
* The text of the position
*/
private String text;
/**
* @param the position to the position
*/
ClanRank(int position, String text) {
this.position = position;
this.text = text;
}
/**
* Gets a clan rank on what position it is in
*
* @return A optional with a value of a clan rank or an empty optional
*/
public static Optional<ClanRank> forPosition(int position) {
Predicate<ClanRank> rank = (ClanRank c) -> c.getPosition() == position;
return Arrays.stream(values()).filter(rank).findFirst();
}
/**
* Gets the position of the rank
*
* @return position
*/
public int getPosition() {
return position;
}
/**
* Gets the position text
*
* @return text
*/
public String getText() {
return text;
}
}
For the Clan loading I decided to use xml however if you wish to change you may do so freely
ClanLoader
Code:
package org.hyperion.rs2.content.clan.xml;
import java.util.Arrays;
import org.hyperion.rs2.content.clan.Clan;
import org.hyperion.rs2.content.clan.ClanPlayer;
import org.hyperion.rs2.content.clan.ClanRank;
import org.hyperion.rs2.model.World;
import org.hyperion.rs2.util.xml.XMLHandler;
import com.thoughtworks.xstream.XStream;
/**
* Loads all clans from the XML file
*
* @author Dan0194
*/
public final class ClanLoader extends XMLHandler<ClanXML> {
/**
* (non-Javadoc)
* @see org.hyperion.rs2.util.xml.XMLHandler#getKey()
*/
@Override
public String getKey() {
return "clan";
}
/**
* (non-Javadoc)
* @see org.hyperion.rs2.util.xml.XMLHandler#getClassName()
*/
@Override
public Class<?> getClassName() {
return ClanXML.class;
}
/**
* (non-Javadoc)
* @see org.hyperion.rs2.util.xml.XMLHandler#getPath()
*/
@Override
public String getPath() {
return "clans.xml";
}
/**
* (non-Javadoc)
* @see org.hyperion.rs2.util.xml.XMLHandler#load(com.thoughtworks.xstream.XStream)
*/
@Override
public void load(XStream xstream) {
xstream.addImplicitCollection(ClanXML.class, "clanPlayer");
}
/**
* (non-Javadoc)
* @see org.hyperion.rs2.util.xml.XMLHandler#bind(java.lang.Object, int)
*/
@Override
public void bind(ClanXML t, int i) {
/** Creates a new clan based on the XML data*/
Clan clan = new Clan(t.getOwner(), t.getTitle());
/** Sets all the privalages ranks */
clan.setEnterRank(ClanRank.valueOf(t.getEnterRank()));
clan.setTalkRank(ClanRank.valueOf(t.getTalkRank()));
clan.setKickRank(ClanRank.valueOf(t.getKickRank()));
clan.setBanRank(ClanRank.valueOf(t.getBanRank()));
/** Adds all the clan players and banned players to the chat */
Arrays.stream(t.getClanPlayer()).forEach((String[] s) -> clan.getClanPlayers().getList().add(new ClanPlayer(s[0], ClanRank.valueOf(s[1]))));
Arrays.stream(t.getBannedPlayers()).forEach((String s) -> clan.getBannedPlayers().getList().add(s));
/** Adds the chat to global chats in the game */
World.getWorld().getClans().add(clan);
}
}
ClanXML
Code:
package org.hyperion.rs2.content.clan.xml;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
/**
* All of the clan data in the XML file
*
* @author Dan0194
*/
public final class ClanXML {
/**
* The owner of the clan
*/
private String owner;
/**
* The title of the clan
*/
private String title;
/**
* The ability to enter in the clan rank
*/
private String enterRank;
/**
* The ability to talk in the clan rank
*/
private String talkRank;
/**
* The ability to kick in the clan rank
*/
private String kickRank;
/**
* The ability to ban in the clan rank
*/
private String banRank;
/**
* All of the players in the clan
*/
@XStreamImplicit
private String[][] clanPlayer;
/**
* All of the banned players in the clan
*/
private String[] bannedPlayers;
/**
* Gets the owner of the clan
*
* @return owner
*/
public String getOwner() {
return owner;
}
/**
* Gets the title of the clan
*
* @return title
*/
public String getTitle() {
return title;
}
/**
* Gets the enter rank of the clan
*
* @return enterRank
*/
public String getEnterRank() {
return enterRank;
}
/**
* Gets the talk rank of the clan
*
* @return talkRank
*/
public String getTalkRank() {
return talkRank;
}
/**
* Gets the kick rank of the clan
*
* @return kickRank
*/
public String getKickRank() {
return kickRank;
}
/**
* Gets the ban rank of the clan
*
* @return banRank
*/
public String getBanRank() {
return banRank;
}
/**
* Gets the players in the clan
*
* @return clanPlayer
*/
public String[][] getClanPlayer() {
return clanPlayer;
}
/**
* Gets the banned players in the clan
*
* @return bannedPlayers
*/
public String[] getBannedPlayers() {
return bannedPlayers;
}
}
Clan Writer Event
Code:
package org.hyperion.rs2.content.clan.xml;
import java.util.function.Consumer;
import org.hyperion.rs2.content.clan.Clan;
import org.hyperion.rs2.content.clan.ClanConstants;
import org.hyperion.rs2.content.clan.ClanPlayer;
import org.hyperion.rs2.model.World;
import org.hyperion.rs2.util.xml.writer.XMLWriterEvent;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
/**
* Writes all the clan data to the XML file
*
* @author Dan0194
*/
public final class ClanWriterEvent extends XMLWriterEvent {
/**
* @param clan to the clan
*/
public ClanWriterEvent() {
super(ClanConstants.CLAN_PATH);
}
/**
* (non-Javadoc)
* @see org.hyperion.rs2.util.xml.writer.XMLWriterEvent#write(org.w3c.dom.Document)
*/
@Override
public boolean write(Document doc) {
/** If there are no clans */
if(World.getWorld().getClans().isEmpty()) {
return false;
}
/** Creates the root element list */
Element list = doc.createElement("list");
doc.appendChild(list);
World.getWorld().getClans().stream().forEach(new Consumer<Clan>() {
@Override
public void accept(Clan clan) {
/** Creates the clan root */
Element clanz = doc.createElement("clan");
list.appendChild(clanz);
/** Writes the owner of the clan data to file */
Element firstname = doc.createElement("owner");
firstname.appendChild(doc.createTextNode(clan.getOwner()));
clanz.appendChild(firstname);
/** Wrties the title of the clan to file */
Element title = doc.createElement("title");
title.appendChild(doc.createTextNode(clan.getTitle()));
clanz.appendChild(title);
/** Writes the enter rank of the clan to the file */
Element enterRank = doc.createElement("enterRank");
enterRank.appendChild(doc.createTextNode(clan.getEnterRank().toString()));
clanz.appendChild(enterRank);
/** Writes the talk rank of the clan to the file */
Element talkRank = doc.createElement("talkRank");
talkRank.appendChild(doc.createTextNode(clan.getTalkRank().toString()));
clanz.appendChild(talkRank);
/** Writes the kick rank of the clan to the file */
Element kickRank = doc.createElement("kickRank");
kickRank.appendChild(doc.createTextNode(clan.getKickRank().toString()));
clanz.appendChild(kickRank);
/** Writes the bank rank of the clan to the file */
Element banRank = doc.createElement("banRank");
banRank.appendChild(doc.createTextNode(clan.getBanRank().toString()));
clanz.appendChild(banRank);
/** Writes all of the players in the clan to the file */
clan.getClanPlayers().getList().stream().forEach(new Consumer<ClanPlayer>() {
@Override
public void accept(ClanPlayer t) {
Element clanPlayer = doc.createElement("clanPlayer");
Element clanPlayerName = doc.createElement("string");
Element clanPlayerRank = doc.createElement("string");
clanPlayerName.appendChild(doc.createTextNode(t.getName()));
clanPlayerRank.appendChild(doc.createTextNode("" + t.getRank().toString()));
clanPlayer.appendChild(clanPlayerName);
clanPlayer.appendChild(clanPlayerRank);
clanz.appendChild(clanPlayer);
}
});
/** Creates banned players element */
Element bannedPlayers = doc.createElement("bannedPlayers");
/** Writes all the banned players to the file */
clan.getBannedPlayers().getList().stream().forEach(new Consumer<String>() {
@Override
public void accept(String t) {
Element bannedPlayer = doc.createElement("string");
bannedPlayer.appendChild(doc.createTextNode(t));
bannedPlayers.appendChild(bannedPlayer);
}
});
clanz.appendChild(bannedPlayers);
}
});
return true;
}
}
If you need XMLWriterEvent here it is
Code:
package org.hyperion.rs2.util.xml.writer;
import java.io.File;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
/**
* @author Dan0914
*
* Event that handles writing data to XML file
*/
public abstract class XMLWriterEvent {
/**
* The logger
*/
private static final Logger LOGGER = Logger.getLogger(XMLWriterEvent.class.getName());
/**
* The path where the XML file is
*/
private String path;
/**
* @param path to the path
*/
public XMLWriterEvent(String path) {
this.path = path;
}
/**
* Starts up the writing event for a specific event
*
* @param event The XML writing event that will start
*/
public static void init(XMLWriterEvent event) {
event.write();
}
/**
*
* Writing all the data to the XML file
*
* @param doc The XML document that will be wrote too
*/
public abstract boolean write(Document doc);
/**
* Begins the writing process to the XML file
*/
public void write() {
try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();
if(!write(doc)) {
return;
}
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(path));
transformer.transform(source, result);
LOGGER.info("Data has been wrote to " + path);
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (TransformerException tfe) {
tfe.printStackTrace();
}
}
}
Now to add the new component packet handler
Code:
package org.hyperion.rs2.packet;
import java.util.Optional;
import java.util.logging.Logger;
import org.hyperion.rs2.content.clan.Clan;
import org.hyperion.rs2.content.clan.ClanPlayer;
import org.hyperion.rs2.content.clan.ClanRank;
import org.hyperion.rs2.model.Player;
import org.hyperion.rs2.model.World;
import org.hyperion.rs2.net.Packet;
/**
* Handles all component buttons actions
*
* @author Dan0194
*/
public final class ComponentPacketHandler implements PacketHandler {
/**
* The logger
*/
private static final Logger LOGGER = Logger.getLogger(ComponentPacketHandler.class.getName());
/**
* (non-Javadoc)
* @see org.hyperion.rs2.packet.PacketHandler#handle(org.hyperion.rs2.model.Player, org.hyperion.rs2.net.Packet)
*/
@Override
public void handle(Player player, Packet packet) {
int button = packet.getShort();
int component = packet.getShort();
LOGGER.info("The button id " + button + " The component " + component);
if (button >= 18144 && button <= 18244) {
Clan clan = player.getClanManager().getCurrentClan();
Player clanPlayer = clan.getPlayers().getList().get(button - 18144);
if(component == 0) {
clan.kick(player, clanPlayer);
} else {
clan.ban(player, clanPlayer);
}
}
if(button >= 18323 && button <= 18423) {
Optional<Clan> currentClan = World.getWorld().getClanByOwner(player.getName());
if(currentClan.isPresent()) {
ClanPlayer clanPlayer = currentClan.get().getClanPlayers().getList().get(button - 18323);
Optional<ClanRank> newRank = component == 0 ? ClanRank.forPosition(clanPlayer.getRank().getPosition() - 1)
: ClanRank.forPosition(component);
if(newRank.isPresent()) {
currentClan.get().rank(player, clanPlayer, newRank.get());
return;
}
player.getActionSender().sendMessage("That player cannot be set to that rank.");
return;
}
}
if(button >= 18424 && button <= 18523) {
Optional<Clan> currentClan = World.getWorld().getClanByOwner(player.getName());
if(currentClan.isPresent()) {
String bannedPlayer = currentClan.get().getBannedPlayers().getList().get(button - 18424);
currentClan.get().unban(player, bannedPlayer);
return;
}
}
switch(button) {
/** The clan set title button*/
case 18304:
if(component == 0) {
player.getClanManager().setTitle();
} else {
Optional<Clan> clan = World.getWorld().getClanByOwner(player.getName());
if(clan.isPresent()) {
clan.get().delete(player);
return;
}
player.getActionSender().sendMessage("You don't have a clan.");
}
break;
case 18307:
Optional<Clan> clan = World.getWorld().getClanByOwner(player.getName());
if(clan.isPresent()) {
int position = component;
Optional<ClanRank> rank = ClanRank.forPosition(position == 0 ? -1 : position);
if(rank.isPresent()) {
clan.get().setEnterRank(rank.get());
player.getClanManager().updateSetup();
}
return;
}
player.getActionSender().sendMessage("You don't have a clan.");
break;
case 18310:
clan = World.getWorld().getClanByOwner(player.getName());
if(clan.isPresent()) {
int position = component;
Optional<ClanRank> rank = ClanRank.forPosition(position == 0 ? -1 : position);
if(rank.isPresent()) {
clan.get().setTalkRank(rank.get());
player.getClanManager().updateSetup();
}
return;
}
player.getActionSender().sendMessage("You don't have a clan.");
break;
case 18313:
clan = World.getWorld().getClanByOwner(player.getName());
if(clan.isPresent()) {
int position = component;
Optional<ClanRank> rank = ClanRank.forPosition(position == 0 ? -1 : position);
if(rank.isPresent()) {
clan.get().setKickRank(rank.get());
player.getClanManager().updateSetup();
}
return;
}
player.getActionSender().sendMessage("You don't have a clan.");
break;
case 18316:
clan = World.getWorld().getClanByOwner(player.getName());
if(clan.isPresent()) {
int position = component;
Optional<ClanRank> rank = ClanRank.forPosition(position == 0 ? -1 : position);
if(rank.isPresent()) {
clan.get().setBanRank(rank.get());
player.getClanManager().updateSetup();
}
return;
}
player.getActionSender().sendMessage("You don't have a clan.");
break;
}
}
}
/**
* Clan chat
*
* @param name
* @param message
* @param clan
* @param rights
*/
public ActionSender sendClanMessage(String name, String message, String clan, int rights) {
if (rights >= 3) {
rights--;
}
PacketBuilder builder = new PacketBuilder(217, Type.VARIABLE_SHORT);
builder.putRS2String(name);
builder.putRS2String(message);
builder.putRS2String(clan);
builder.putShort(rights);
player.write(builder.toPacket());
return this;
}
Now finally in your World class add this stuff in it's appropriate places
Code:
/**
* All of the clans
*/
public List<Clan> clans = new ArrayList<>();
/**
* Gets a clan by the owner's name
*
* @return an optional containing either the clan or empty
*/
public Optional<Clan> getClanByOwner(String owner) {
Predicate<Clan> player = (Clan c) -> c.getOwner().equals(owner);
return clans.stream().filter(player).findFirst();
}
/**
* Gets the all of the clans in the world
*
* @return clans
*/
public List<Clan> getClans() {
return clans;
}
VERY IMPORTANT WHEREVER YOUR HYPERION HANDLES SERVER SHUTDOWNS ADD: