i was doing services for someone and never got to finishing this cause they were no longer interested. this is basically the original pi clan chat, but i went and cleaned up a few things and added features such as banning, kicking, ranks and coinshare. i only suggest using this if you're still using the original pi system. you'll still need to do things like the commands to ban/kick/setranks, implement coinshare and saving unless you dont want it to save
Code:
package server.virtual.clan;
import server.main.entity.player.Client;
import server.main.entity.player.PlayerHandler;
import server.main.item.GroundItem;
import server.tools.Misc;
import server.virtual.shop.ShopAssistant;
/**
* @author Sanity
* @revisedby Karma
*/
public class ClanChatHandler {
private static Clan[] clans = new Clan[100];
/**
* Finds a clan by a clanId
* @param clanId
* the clan id
* @return
* the clan
*/
public Clan getClan(int clanId) {
return clans[clanId];
}
/**
* Finds a clan by a specific name
* @param name
* the name
* @return
* the clan
*/
public Clan getClan(String name) {
for (int j = 0; j < clans.length; j++) {
if (clans[j].getClanName().equalsIgnoreCase(name))
return clans[j];
}
return null;
}
/**
* Finds an available clanId
* @return
* the available clanId
*/
private int findAvailableId() {
for (int j = 0; j < clans.length; j++) {
if (clans[j] == null || clans[j].getOwnerName() == "")
return j;
}
return -1;
}
/**
* Gets a player's rank in a certain clan
* @param client
* the player
* @param clanName
* the clan's name
* @return
* the rank of the player in the clan
*/
public ClanRank getPlayerRank(Client client, Clan clan) {
if (clan.getRanks().containsKey(client.playerName))
return clan.getRanks().get(client.playerName);
return ClanRank.NONE;
}
/**
* Kicks a player from a clan chat
* @param kicker
* the person distributing the kick
* @param target
* the name of the player to be kicked
*/
public void kickPlayer(Client kicker, String target) {
Clan clan = this.getClan(kicker.clanId);
if (kicker.playerName.equalsIgnoreCase(target))
return;
if (target.equalsIgnoreCase(clan.getOwnerName()))
return;
if (this.getPlayerRank(kicker, clan).ordinal() < clan.getWhoCanPunish().ordinal())
return;
for (int i = 0; i < PlayerHandler.players.length; i++) {
Client players = (Client) PlayerHandler.players[i];
if (players.clanId == kicker.clanId) {
if (players.playerName.equalsIgnoreCase(target))
this.leaveClan(players);
break;
}
}
}
/**
* Bans a player from a clan chat
* @param banner
* the person distributing the ban
* @param target
* the name of the player to be banned
*/
public void banPlayer(Client banner, String target) {
Clan clan = this.getClan(banner.clanId);
if (banner.playerName.equalsIgnoreCase(target))
return;
if (target.equalsIgnoreCase(clan.getOwnerName()))
return;
if (this.getPlayerRank(banner, clan).ordinal() < clan.getWhoCanPunish().ordinal())
return;
for (int i = 0; i < PlayerHandler.players.length; i++) {
Client players = (Client) PlayerHandler.players[i];
if (players.clanId == banner.clanId) {
if (players.playerName.equalsIgnoreCase(target)) {
clan.getBanList().add(target);
this.leaveClan(players);
}
break;
}
}
}
/**
* Joins a clan chat for the player
* @param c
* the player
* @param name
* the name of the clan chat to join
*/
public void joinClanChat(Client client, String name) {
if (!this.validName(name)) {
if (this.getClan(name).getBanList().contains(client.playerName)) {
client.sendMessage("You're currently banned from this clan.");
return;
}
this.addToClan(client, this.getClan(name).getClanId());
return;
}
makeClan(client, name);
}
/**
* Determines if a name is already in use by another clanchat
* @param name
* the name
* @return
* if the name is already used
*/
private boolean validName(String name) {
for (int j = 0; j < clans.length; j++) {
if (getClan(j) != null) {
if (getClan(j).getClanName().equalsIgnoreCase(name))
return false;
}
}
return true;
}
/**
* Makes a clan for the player
* @param c
* the player
* @param name
* the name of the clan chat to make
*/
public void makeClan(Client client, String name) {
if (findAvailableId() >= 0) {
if (validName(name)) {
client.clanId = findAvailableId();
clans[client.clanId] = new Clan(client, name, client.clanId);
getClan(client.clanId).getRanks().put(client.playerName, ClanRank.CLAN_OWNER);
addToClan(client, client.clanId);
} else {
client.sendMessage("A clan with this name already exists.");
}
} else {
client.sendMessage("Your clan chat request could not be completed.");
}
}
/**
* Adds a player to a clanchat
* @param client
* the player
* @param clanId
* the clanchat to add the player to
*/
private void addToClan(Client client, int clanId) {
Clan clan = getClan(clanId);
if (clan != null) {
if (clan.getMembers().length > 50) {
client.sendMessage("This clan chat is currently full.");
return;
}
for (int j = 0; j < clan.getMembers().length; j++) {
if (clan.getMembers()[j] <= 0) {
clan.getMembers()[j] = client.playerId;
client.clanId = clanId;
messageToClan(client.playerName + " has joined the channel.", clanId);
updateClanChat(clanId);
break;
}
}
}
}
/**
* Performs the leaving action of a clanchat for a player
* @param c
* the player
*/
public void leaveClan(Client client) {
if (client.clanId < 0) {
client.sendMessage("You are not in a clan.");
return;
}
Clan clan = getClan(client.clanId);
if (clan != null) {
if (client.playerName
.equalsIgnoreCase(clan.getOwnerName())) {
messageToClan("The clan has been deleted by the owner.", client.clanId);
destructClan(client.clanId);
return;
}
for (int j = 0; j < getClan(client.clanId).getMembers().length; j++) {
if (clan.getMembers()[j] == client.getId()) {
clan.getMembers()[j] = -1;
}
}
if (client != null) {
client.clanId = -1;
client.sendMessage("You have left the clan.");
client.getPA().clearClanChat();
}
updateClanChat(client.clanId);
} else {
client.clanId = -1;
client.sendMessage("You are not in a clan.");
}
}
/**
* Deletes a certain clan
* @param clanId
* the clan to delete
*/
private void destructClan(int clanId) {
Clan clan = getClan(clanId);
if (clanId < 0)
return;
for (int j = 0; j < getClan(clanId).getMembers().length; j++) {
if (clanId < 0)
continue;
if (clan.getMembers()[j] <= 0)
continue;
if (PlayerHandler.players[clan.getMembers()[j]] != null) {
Client client = (Client) PlayerHandler.players[clan.getMembers()[j]];
client.clanId = -1;
client.getPA().clearClanChat();
}
}
clan.setMembers(new int[50]);
clan.setOwnerName("");
clan.setClanName("");
}
/**
* Updates the clan chat interface for a clan
* @param clanId
* the clan id to update
*/
public void updateClanChat(int clanId) {
for (int j = 0; j < getClan(clanId).getMembers().length; j++) {
if (getClan(clanId).getMembers()[j] <= 0)
continue;
if (PlayerHandler.players[getClan(clanId).getMembers()[j]] != null) {
Client clanMembers = (Client) PlayerHandler.players[getClan(clanId).getMembers()[j]];
clanMembers.getPA().sendFrame126("Talking in: " + getClan(clanId).getClanName(), 18139);
clanMembers.getPA().sendFrame126("Owner: " + getClan(clanId).getOwnerName(), 18140);
int slotToFill = 18144;
for (int i = 0; i < getClan(clanId).getMembers().length; i++) {
if (getClan(clanId).getMembers()[i] > 0) {
if (PlayerHandler.players[getClan(clanId).getMembers()[i]] != null) {
clanMembers.getPA()
.sendFrame126(Misc.formatPlayerName(this.getPlayerRank((Client)PlayerHandler.players[getClan(clanId).getMembers()[i]], getClan(clanId)).name().toLowerCase().replaceAll("_", " ")) +
" - " + PlayerHandler.players[getClan(clanId).getMembers()[i]].playerName,
slotToFill);
slotToFill++;
}
}
}
for (int k = slotToFill; k < 18244; k++)
clanMembers.getPA().sendFrame126("", k);
}
}
}
/**
* Sends a general message to a certain clanchat
* @param message
* the message
* @param clanId
* the clanchat
*/
public void messageToClan(String message, int clanId) {
if (clanId < 0)
return;
for (int j = 0; j < getClan(clanId).getMembers().length; j++) {
if (getClan(clanId).getMembers()[j] < 0)
continue;
if (PlayerHandler.players[getClan(clanId).getMembers()[j]] != null) {
Client c = (Client) PlayerHandler.players[getClan(clanId).getMembers()[j]];
c.sendMessage("@[email protected]" + message);
}
}
}
/**
* Sends a player's message to a clanchat
* @param client
* the player sending the message
* @param message
* the message to be sent
*/
public void playerMessageToClan(Client client, String message) {
if (client.clanId < 0)
return;
if (this.getPlayerRank(client, getClan(client.clanId)).ordinal() < getClan(client.clanId).getWhoCanTalk().ordinal()) {
client.sendMessage("You don't meet the rights of talking in this clan chat.");
return;
}
for (int j = 0; j < getClan(client.clanId).getMembers().length; j++) {
if (getClan(client.clanId).getMembers()[j] <= 0)
continue;
if (PlayerHandler.players[getClan(client.clanId).getMembers()[j]] != null) {
Client clanMates = (Client) PlayerHandler.players[getClan(client.clanId).getMembers()[j]];
clanMates.sendClan(Misc.optimizeText(this.getPlayerRank(client, getClan(client.clanId)).name().toLowerCase().replaceAll("_", " ")) +" - "+ client.playerName, message,
getClan(client.clanId).getClanName(),
client.playerRights);
}
}
}
/**
* Gets the amount of clan mates by a certain tile
* @param clanId
* the clan we're working with
* @param x
* the x coordinate of the tile
* @param y
* the y coordinate of the tile
* @return
* the amount of clan mates by the tile
*/
private int getClanMatesNear(int clanId, int x, int y) {
int nearby = 0;
for (int j = 0; j < getClan(clanId).getMembers().length; j++) {
if (getClan(clanId).getMembers()[j] <= 0)
continue;
if (PlayerHandler.players[getClan(clanId).getMembers()[j]] != null) {
Client clanMates = (Client) PlayerHandler.players[getClan(clanId).getMembers()[j]];
if (clanMates.distanceToPoint(x, y) <= 16)
nearby++;
}
}
return nearby;
}
/**
* Handles coin sharing for npc drops
* @param client
* the player who obtained the drop
* @param item
* the item
*/
public boolean coinShare(Client client, GroundItem item) {
int members = this.getClanMatesNear(client.clanId, item.getItemX(), item.getItemY());
int value = (int) ShopAssistant.getItemShopValue(client, item.getItemId(), item.getItemAmount());
if (value < 10000000)
return false;
if (members <= 1)
return false;
if (client.clanId >= 0 && client.getCoinShareStatus()) {
for (int j = 0; j < getClan(client.clanId).getMembers().length; j++) {
if (getClan(client.clanId).getMembers()[j] <= 0)
continue;
if (PlayerHandler.players[getClan(client.clanId).getMembers()[j]] != null) {
Client clanMates = (Client) PlayerHandler.players[getClan(client.clanId).getMembers()[j]];
if (clanMates.distanceToPoint(item.getItemX(), item.getItemY()) <= 16)
clanMates.getItems().addItem(995, value/members);
}
}
}
return true;
}
}
Code:
package server.virtual.clan;
/**
* The types of clan ranking
* @author Final Project
*
*/
public enum ClanRank {
NONE,
FRIEND,
PRIVATE,
CORPORAL,
SERGEANT,
CAPTAIN,
LIEUTENANT,
GENERAL,
CLAN_OWNER;
}
Code:
package server.virtual.clan;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import server.main.entity.player.Client;
/**
* Represents a clan
* @author Final Project
*
*/
public class Clan {
private String clanName;
private String ownerName;
private int[] members = new int[50];
private boolean lootshareOn;
private int clanId;
public int getClanId() {
return this.clanId;
}
public void setClanId(int id) {
this.clanId = id;
}
public String getClanName() {
return this.clanName;
}
public void setClanName(String name) {
this.clanName = name;
}
public String getOwnerName() {
return this.ownerName;
}
public void setOwnerName(String name) {
this.ownerName = name;
}
public int[] getMembers() {
return members;
}
public void setMembers(int[] set) {
this.members = set;
}
public boolean getLootShareStatus() {
return this.lootshareOn;
}
public void setLootShareStatus(boolean status) {
this.lootshareOn = status;
}
public Clan(Client client, String name, int id) {
this.setOwnerName(client.playerName.toLowerCase());
this.setClanName(name);
this.setClanId(id);
}
private ArrayList<String> banList = new ArrayList<>();
public ArrayList<String> getBanList() {
return banList;
}
private Map<String, ClanRank> ranks = new HashMap<>();
public Map<String, ClanRank> getRanks() {
return ranks;
}
private ClanRank whoCanTalk = ClanRank.NONE;
public ClanRank getWhoCanTalk() {
return this.whoCanTalk;
}
public void setWhoCanTalk(ClanRank rank) {
this.whoCanTalk = rank;
}
private ClanRank whoCanPunish = ClanRank.CLAN_OWNER;
public ClanRank getWhoCanPunish() {
return this.whoCanPunish;
}
public void setWhoCanPunish(ClanRank rank) {
this.whoCanPunish = rank;
}
}