Advanced Lending System 1.0
Description: I see people are interested in loaning money and most of the systems released are crap so I decided to make my own system.
Difficulty: 3
Assumed Knowledge: Java (I'd hope you know Java...)
Tested Server: wL
Files/Classes Modified: Depends.
Procedure
Here is the class... basically make a new instance for each player, call the methods in your own context.
Code:
/**
* @(#)Lending.java
*
*
* @author Boomer
* @version 1.00 2008/5/8
*/
import java.io.*;
public class Lending {
public boolean checkAccount()
{
if (c == null)
return false;
try
{
File file = new File("./Lending/" + c.playerName + ".txt");
if (!file.createNewFile())
return file.length() == 0;
}
catch (IOException e)
{
}
return false;
}
public boolean documentLoan(String toPlayer, int amount)
{
try
{
BufferedWriter write = new BufferedWriter(new FileWriter("./Lending/" + c.playerName + ".txt"));
write.write(toPlayer + " " + amount);
write.flush();
return true;
}
catch (IOException e)
{
}
return false;
}
public void giveLoan(String toPlayer, int amount)
{
if (c.playerHasItemAmount(995, amount))
{
for (int i = 0; i < server.playerHandler.maxPlayers; i++)
{
client player = (client)server.playerHandler.players[i];
if (player != null && player.playerName.equalsIgnoreCase(toPlayer))
{
if (checkAccount())
{
if (player.Lending.documentLoan(c.playerName, amount))
{
c.deleteItem(995, c.GetItemSlot(995), amount);
player.addItem(995, amount);
}
else
{
c.sendMessage("Loan was unsuccessful, contact an admin please! (ERROR CODE: 101)");
return;
}
}
else
{
c.sendMessage("You cannot give out any loans until you pay off your own loans!");
return;
}
}
}
}
else
{
c.sendMessage("You don't have " + amount + " GP!");
}
}
public boolean owesLoan(String player, int amount)
{
try
{
BufferedReader read = new BufferedReader(new FileReader("./Lending/" + c.playerName + ".txt"));
String line;
while ((line = read.readLine()) != null)
{
if (line.equalsIgnoreCase(player + " " + amount))
return true;
}
}
catch (IOException e)
{
}
return false;
}
public void payLoan(String toPlayer, int amount)
{
if (c.playerHasItemAmount(995, amount))
{
for (int i = 0; i < server.playerHandler.maxPlayers; i++)
{
client player = (client)server.playerHandler.players[i];
if (player != null && player.playerName.equalsIgnoreCase(toPlayer))
{
if (!owesLoan(toPlayer, amount))
{
c.sendMessage("You don't owe " + toPlayer + " " + amount + " GP! [NEEDS TO BE EXACT!]");
return;
}
if (removeLoan(toPlayer, amount))
{
c.deleteItem(995, c.GetItemSlot(995), amount);
player.addItem(995, amount);
}
else
{
c.sendMessage("Loan was unsuccessful, contact an admin please! (ERROR CODE: 201)");
return;
}
}
}
}
}
public void myLoans()
{
try
{
BufferedReader read = new BufferedReader(new FileReader("./Lending/" + c.playerName + ".txt"));
String line;
while ((line = read.readLine()) != null)
{
if (line.length() >= 3)
{
c.sendMessage(line);
}
}
}
catch (IOException e)
{
}
}
public boolean removeLoan(String player, int amount)
{
try
{
BufferedReader read = new BufferedReader(new FileReader("./Lending/" + c.playerName + ".txt"));
String line;
String[] tempLines = new String[getLines("./Lending/" + c.playerName + ".txt")];
int lineOn = 0;
boolean found = false;
while ((line = read.readLine()) != null)
{
if (!line.equalsIgnoreCase(player + " " + amount) || found == true)
{
tempLines[lineOn] = line;
lineOn++;
}
else found = true;
}
new File("./Lending/" + c.playerName + ".txt").delete();
return createNewFile(tempLines);
}
catch (IOException e)
{
}
return false;
}
public boolean createNewFile(String[] tempLines)
{
try
{
BufferedWriter write = new BufferedWriter(new FileWriter("./Lending/" + c.playerName + ".txt"));
if (tempLines == null)
return false;
for (int i = 0; i < tempLines.length; i++)
{
if (tempLines[i] != null)
{
write.write(tempLines[i]);
write.newLine();
}
}
write.flush();
return true;
}
catch (IOException e)
{
}
return false;
}
public int getLines(String fileName)
{
int lines = 0;
String line;
try
{
BufferedReader read = new BufferedReader(new FileReader(fileName));
while ((line = read.readLine()) != null)
{
lines++;
}
}
catch (IOException e)
{
}
return lines;
}
private client c;
public Lending(client c)
{
this.c = c;
}
}
MAKE A FOLDER NAMED 'Lending'!!!
My suggested way to use that code is to do the following...
In your buyItem method, at the top add
Code:
if (Lending.checkAccount()) {
sendMessage("You cannot buy items until you pay off your loans!");
return;
}
Next in your tradeItem method add...
Code:
if (Lending.checkAccount()) {
sendMessage("You cannot trade items until you pay off your loans!");
return;
}
Also this may help with integrating it..
Code:
public Lending Lending = new Lending(this);
Basically that just doesn't allow them to give off currency until they pay off their loans...
This is a very simple system, in the future I hope to advance the saving system so they can pay off different amounts of cash then the exact amount...
Feel free to add on your own mods..
Should all work with a little effort.
Features:
- Saving Loans
- Unable to Give Off Currency until Pays Loans
- Adds and Removes loans to the files
- Able to Display all loans
I would have preferred to use a SQL database but I know many people dislike them... it would have been easier.
Credits: Myself