xgive, giveall, xtake, takeall
So, first, before you do ANYTHING here, backup your server source.
There are two ways you can do this, with the Perfect Compiler:
type bac and hit the [Enter] key.
Or...
C&P your source folder, rename it so it says "BACKUP" on the end of the name
That's like the most important tut in the world!
Anyways, now we can get started.
Purpose: Allows you to manage items that other people have.
Knowledge required: None.
File(s) Edited: Client.java
Difficulty: 2/10 (just know where functions and commands should go and how to c&p)
First, let me explain what these 4 commands do: These are admin only commands that will allow you to give anyone any items, as well as search for any item that anyone has, and "take" it (it just removes it). Like if anyone has glowing dagger (747), you can remove it instantly with ::xtake or ::takeall after you've completed the tut. (the take and give commands won't work for equip.)
::xgive just gives a player an item. If it's GP, it'll give 1 mil. You can just trade the player, but if you just want to force the player to take something, you can use this!
::giveall Gives every player an item, and I mean every player online. If you have any players you want to give something to and you miss them or they log, tough luck. This command can be pretty useful though.
::xtake Instantly peruses through a player's inventory and bank. If it finds the item you want to take, BAM! It's gone. Useful if you find a player has an item you don't want him to have. oh yes, if it "takes" any stackable item, it will take all of it.
::takeall if you think about it, takeall could be kind of like a cleaning method! you could clear all players' banks' and inventories' of any admin items without searching to see if a player has an admin only item or not.
End of descriptions, it's time for the work.
I've used some custom methods for my commands. Oh, remember that you can access these commands anytime with
Code:
customCommand("COMMANDNAMEHERE");
Now let's add in 2 functions i will be using for these 4 commands.
Search: public int freeBankSlots
Code:
public int freeBankSlots() {
int freeS = 0;
for (int i = 0; i < playerBankSize; i++) {
if (bankItems[i] <= 0) {
freeS++;
}
}
return freeS;
}
under that, add:
Code:
public int getFreeBankSlot() { // made by Escape
for (int i = 0; i < playerBankSize; i++) {
if (bankItems[i] <= 0) {
return i;
}
}
return -1;
}
Search: public int GetItemSlot
Code:
public int GetItemSlot(int ItemID) {
for (int i = 0; i < playerItems.length; i++) {
if ((playerItems[i] - 1) == ItemID) {
return i;
}
}
return -1;
}
Under that, add:
Code:
public int GetBankSlot(int ItemID) { // made by Escape
for (int i = 0; i < playerBankSize; i++) {
if ((bankItems[i] - 1) == ItemID) {
return i;
}
}
return -1;
}
There ya go, you did something!
Now for the commands, we can just c&p all 4 commands into there.
Search: command.startsWith("empty")
Code:
else if (command.startsWith("empty"))
{
removeAllItems();
}
Under that, add ALL of this:
Code:
if (command.startsWith("xgive") && playerRights >= 2) // this will give to a player (playerName > playerID)
{
try{
String otherPName = command.substring(12); // Playername
int gItemID = Integer.parseInt(command.substring(6,11)); // give item IDN (#####)
int otherPIndex = server.playerHandler.getPlayerID(otherPName);
if(otherPIndex != -1 && server.playerHandler.players[otherPIndex] != null && otherPName != playerName)
{
client p = (client) server.playerHandler.players[otherPIndex];
if (gItemID >= 0) {
if (p.freeSlots() > 0)
{
if (gItemID == 995)
p.addItem(gItemID,1000000);
else
p.addItem(gItemID,1);
sendMessage("You give the item "+GetItemName(gItemID)+" ("+gItemID+") to "+p.playerName);
p.sendMessage("You recieve the item "+GetItemName(gItemID)+" ("+gItemID+") from "+playerName);
}
else if (p.freeBankSlots() > 0)
{
if (gItemID == 995)
p.bankItem(gItemID,getFreeBankSlot(),1000000);
else
p.bankItem(gItemID,getFreeBankSlot(),1);
sendMessage("You give the item "+GetItemName(gItemID)+" ("+gItemID+") to "+p.playerName);
p.sendMessage("You recieve this item in your bank "+GetItemName(gItemID)+" ("+gItemID+") from "+playerName);
}
else {
sendMessage("Unable to send item. "+otherPName+"'s inventory and bank is full"); }
}
}
else {
sendMessage("Error giving item to player. Player is not online or doesn't exist.");
}
}
catch(Exception e) {
sendMessage("Syntax error! Try ::xgive (itemID: #####) (playername)");
}
}
if (command.startsWith("giveall") && playerRights >= 2) // this will gave an item to ALL players (except yourself)
{
try{
int pid2 = 0;
int gItemID = Integer.parseInt(command.substring(8,13)); // give item IDN (#####)
PlayerHandler.messageToAll = playerName + " gave the item "+GetItemName(gItemID)+" ("+gItemID+") to everyone";
for (Player pid : server.playerHandler.players) { //loop so it effects all players
if(pid != null && pid2 != playerId)
{
client p = (client) pid;
if (gItemID >= 0) {
if (p.freeSlots() > 0)
{
if (gItemID == 995)
p.addItem(gItemID,1000000);
else
p.addItem(gItemID,1);
p.sendMessage("You recieve the item "+GetItemName(gItemID)+" ("+gItemID+") from "+playerName);
}
else if (p.freeBankSlots() > 0)
{
if (gItemID == 995)
p.bankItem(gItemID,getFreeBankSlot(),1000000);
else
p.bankItem(gItemID,getFreeBankSlot(),1);
p.sendMessage("You recieve this item in your bank "+GetItemName(gItemID)+" ("+gItemID+") from "+playerName);
}
}
}
pid2 ++;
}
}
catch(Exception e) {
sendMessage("Syntax error! Try ::giveall (itemID: #####)");
}
}
if (command.startsWith("xtake") && playerRights >= 2) // this will take a player's item from inventory, equipment, or bank (playerName > playerID)
{
try{
String otherPName = command.substring(12); // Playername
int gItemID = Integer.parseInt(command.substring(6,11)); // give item IDN (#####)
int otherPIndex = PlayerHandler.getPlayerID(otherPName);
if(otherPIndex != -1 && server.playerHandler.players[otherPIndex] != null && otherPName != playerName)
{
client p = (client) server.playerHandler.players[otherPIndex];
if (gItemID >= 0) {
if (playerHasItem(gItemID))
{
for (int i = 0; i < playerItems.length; i++) {
if (i == GetItemSlot(gItemID))
p.deleteItem(gItemID,i,maxItemAmount);
}
sendMessage("You take all item(s) of "+GetItemName(gItemID)+" ("+gItemID+") from "+p.playerName);
p.sendMessage("All item(s) of this "+GetItemName(gItemID)+" ("+gItemID+") was taken by "+playerName);
}
else if (hasItemAny(gItemID, 1))
{
for (int i = 0; i < playerBankSize; i++) {
if (i == GetBankSlot(gItemID))
{
bankItems[i] = 0;
bankItemsN[i] = 0;
}
}
resetBank();
sendMessage("You take all item(s) of "+GetItemName(gItemID)+" ("+gItemID+") from "+p.playerName+"'s bank");
p.sendMessage("all item(s) of this from your bank "+GetItemName(gItemID)+" ("+gItemID+") was taken by "+playerName);
}
else {
sendMessage("Unable to take item(s). "+otherPName+"'s inventory and bank doesn't contain that item!"); }
}
}
else {
sendMessage("Error taking item(s) from player. Player is not online or doesn't exist.");
}
}
catch(Exception e) {
sendMessage("Syntax error! Try ::xtake (itemID: #####) (playername)");
}
}
if (command.startsWith("takeall") && playerRights >= 2) // this will gave an item to ALL players (except yourself)
{
try{
int pid2 = 0;
int gItemID = Integer.parseInt(command.substring(8,13)); // give item IDN (#####)
PlayerHandler.messageToAll = playerName + " took all item "+GetItemName(gItemID)+" ("+gItemID+") from everyone";
for (Player pid : server.playerHandler.players) { //loop so it effects all players
if(pid != null && pid2 != playerId)
{
client p = (client) pid;
if (gItemID >= 0) {
if (playerHasItem(gItemID))
{
for (int i = 0; i < playerItems.length; i++) {
if (i == GetItemSlot(gItemID))
p.deleteItem(gItemID,i,maxItemAmount);
}
p.sendMessage("All item(s) of this "+GetItemName(gItemID)+" ("+gItemID+") was taken by "+playerName);
}
else if (hasItemAny(gItemID, 1))
{
for (int i = 0; i < playerBankSize; i++) {
if (i == GetBankSlot(gItemID))
{
bankItems[i] = 0;
bankItemsN[i] = 0;
}
}
resetBank();
p.sendMessage("All item(s) of this from your bank "+GetItemName(gItemID)+" ("+gItemID+") was taken by "+playerName);
}
}
}
pid2 ++;
}
}
catch(Exception e) {
sendMessage("Syntax error! Try ::takeall (itemID: #####)");
}
}
Enjoy! post errors here.