Job Holders / Slaves (Something like Miscellanious)
Reputation and Thank's is appreciated as I'd love to build some reputation on here
Let me start by apologizing for the title, some of you might take it offending. Honestly the npc names are called slaves, and that's why I based the title off it, this way everybody will know exactly what this system is about.
Just like my previous releases, I will not provide a top to toe, fully programmed system, there will be bugs you have to fix yourself while using this. Ofcourse, anybody could do this in like 30 minutes, i'm just here to save you those precious 30 minutes which you could use to finish what this system needs.
I could of fixed and done what it needed, but I want you guys to put some work in this that way you'll learn how to do something similar the next time, and while you are figuring out how to do it, you will be studying the code which is another good thing.
Alright, let's get started, this is the point where i'm going to explain you exactly what to put where and for what reason.
1) You'd want to create a new class, I'd suggest creating it in it's own package, but that is up to you. once you've done that, you'd want to create a class inside it, name the class Worker and paste this inside it, the code is documented so you should be able to understand what each part does.
Code:
package server.game.players.workers;
import server.event.*;
import server.game.players.Client;
/**
* @author Stan : 15 / 08 / 2014
* Represents an abstract class for workers (slaves)
*/
public abstract class Workers {
/**
* The items the player belongs to
*/
private final Client player;
/**
* The item id being worked for.
*/
private final int id;
/**
* The amount being worked for.
*/
private final int amount;
/**
* The amount it costs.
*/
private final int price;
/**
* The amount of time it costs until it's finished.
*/
private final int time;
/**
* The flag which represents whether it has to process the payment
*/
private boolean check = false;
/**
* A protected void to handle the force messages of npcs
*/
protected abstract String randomMessage(String message);
/**
* A protected integer to hold the npcs npc id
*/
protected abstract int npcId(int id);
/**
* creates the worker
*
* @param player
* @param id
* @param amount
* @param price
*/
protected Workers(final Client player, final int id, final int amount, final int price, final int time, final boolean check) {
this.player = player;
this.id = id;
this.amount = amount;
this.price = price;
this.time = time;
this.check = check;
}
/**
* Gets the item id
* @return the item id
*/
private final int getId() {
return id;
}
/**
* Gets the amount
* @return the amount
*/
private final int getAmount() {
return amount;
}
/**
* Gets the price
* This is static because we need to call it from a different class.
* @return the price
*/
public static final int getPrice() {
return price;
}
/**
* Gets the amount of time is left
* @return the amount of time
*/
public final int getTime() {
return time;
}
/**
* Gets whether the check is true or false
* @return true or false
*/
public final boolean getCheck() {
return check;
}
/**
* handles the main timer class
*/
protected void handleTimer() {
CycleEventHandler.getSingleton().addEvent(player, new CycleEvent() {
@Override
public void execute(CycleEventContainer container) {
if(check == false) {
return;
} else {
if(getTime() > 0) {
time--;
} else if(getTime() == 0) {
handleQWorker();
check = false;
stop();
}
System.out.println(""+getTime());
}
}
@Override
public void stop() {
time = -1;
return;
}
}, 2);
}
/**
* Gives the player the item.
*/
public void handleQWorker() {
player.getItems().addItem(getId(), getAmount());
}
}
Now that we have implemented the main class, we still need to handle the the instanceable classes (SlowWorker, AverageWorker, QuickWorker)
Eclipse should auto generate an impl package in the new package you created, if this isn't the case, create your own impl package.
2) Let's add the instanceable classes, inside your impl package make 3 classes, name them SlowWorker, AverageWorker, QuickWorker, paste the following inside SlowWorker
Code:
package server.game.players.workers.impl;
import core.util.Misc;
import server.game.players.Client;
import server.game.players.workers.Workers;
public class SlowWorker extends Workers {
public SlowWorker(Client player, int id, int amount, int price, int time, boolean check) {
super(player, id, amount, price, time, check);
handleTimer();
}
@Override
protected String randomMessage(String message) {
int chatType = Misc.random(5);
if(chatType == 0) {
message = "I'm soo old, should of retired already..";
} else if(chatType == 1) {
message = "This is soo not worth what i'm being paid for...";
} else {
message = "Ohhh, my back is about to collapse..";
}
return message;
}
@Override
protected int npcId(int id) {
return id;
}
}
now paste the following in AverageWorker
Code:
package server.game.players.workers.impl;
import core.util.Misc;
import server.game.players.Client;
import server.game.players.workers.Workers;
public class AverageWorker extends Workers {
public AverageWorker(Client player, int id, int amount, int price, int time, boolean check) {
super(player, id, amount, price, time, check);
handleTimer();
}
@Override
protected String randomMessage(String message) {
int chatType = Misc.random(5);
if(chatType == 0) {
message = "Work work work work.";
} else if(chatType == 1) {
message = "Put what you want here.";
} else {
message = "Boss wants me to do work, and i'm going to do it..";
}
return message;
}
@Override
protected int npcId(int id) {
return id;
}
}
Now paste the following inside QuickWorker
Code:
package server.game.players.workers.impl;
import core.util.Misc;
import server.game.players.Client;
import server.game.players.workers.Workers;
public class QuickWorker extends Workers {
public QuickWorker(Client player, int id, int amount, int price, int time, boolean check) {
super(player, id, amount, price, time, check);
handleTimer();
}
@Override
protected String randomMessage(String message) {
int chatType = Misc.random(5);
if(chatType == 0) {
message = "Quick quick quick quick...";
} else if(chatType == 1) {
message = "The faster I work the fitter I get.";
} else {
message = "Let's get this done! the faster the more money.";
}
return message;
}
@Override
protected int npcId(int id) {
return id;
}
}
Now you've got it set up, I'll show you an example of how to execute this piece of code.
3) Open DialogueHandler class and paste the following dialogue inside it.
Code:
case 10005:
c.getDH().sendOption2("Armadyl Godsword", "Dragon Claws");
c.dialogueAction = 10005;
break;
case 10006:
c.getDH().sendNpcChat1("Alright, that will be "+Workers.getPrice(), c.talkingNpc, "Commissioner");
c.nextChat = 10007;
break;
case 10007:
c.getDH().sendOption2("Sure, here you go", "I can't afford that");
c.dialogueAction = 10007;
break;
Alright, this is also the part where you'd want to make a check whether the player has the price to pay, if not there is a different dialogue.
Now let's open Clickingbuttons class, this part will assign what to do when the dialogue buttons are clicked
find case 9157: and paste the following below it
Code:
if(c.dialogueAction == 10005) {
new SlowWorker(c, 11694, 1, 100, 10, false);
c.getDH().sendDialogues(10006, actionButtonId);
}
if(c.dialogueAction == 10007) {
new SlowWorker(c, 11694, 1, 100, 10, true);
c.getPA().removeAllWindows();
}
Alright, do you see how the first SlowWorker is set to false? This partially cancels the code to be executed so you can get the price of the Job and anything else you'd need from which should be instanced, if we didn't include that piece of code, your dialogue would display "This currently costs 0" as the price was never assigned towards the Job, now it is, and the job is cancelled at the same time.
The second SlowWorker is set to true as you can see, basically this one DOES execute the code as the parameter was set to true.
Heres a small explanation on how SlowWorker / AverageWorker / QuickWorker is instanced.
Code:
new Slow/Average/Quick-Worker(c, ITEMID, AMOUNT, PRICE, TIME, TRUE OR FALSE);
As explained before, use true when you're going to execute the code, use false when you just want to show the price in a dialogue, sendmessage or anything else you'd want to display it on.
now that that's done, you need to find how you want to assign the dialogue to a player, you could do this with NPCS, Objects or anything else that could assign an action, i'm just going to use a command. Heres a video on what exactly this does. Enjoy