Code:
package server.model.players.skills;
import java.util.HashMap;
import java.util.Map;
import server.World;
import server.model.players.Client;
import server.tick.Tick;
import server.util.Misc;
public class Mining {
private Client c;
private int cycle = 0;
public Mining(Client c) {
this.c = c;
}
public final int[] VALID_PICKAXES = {1265, 1267, 1269, 1273, 1271, 1275, 15259};
private final int[] RANDOM_GEMS = {1623,1621,1619,1617,1631};
private enum Pickaxe {
Bronze(1265, 1, 625, 1.0),
Iron(1267, 1, 626, 1.05),
Steel(1269, 6, 627, 1.1),
Mithril(1273, 21, 628, 1.2),
Adamant(1271, 31, 629, 1.25),
Rune(1275, 41, 624, 1.3),
Dragon(15259, 61, 12188, 1.50);
private int id, req, anim;
private double speed;
private Pickaxe(int id, int req, int anim, double speed) {
this.id = id;
this.req = req;
this.anim = anim;
this.speed = speed;
}
public int getId() {
return id;
}
public int getReq(){
return req;
}
public int getAnim(){
return anim;
}
public double getSpeed() {
return speed;
}
private static final Map<Integer, Pickaxe> p = new HashMap<Integer, Pickaxe>();
public static Pickaxe forId(int id) {
return p.get(id);
}
static {
for (Pickaxe paxe : Pickaxe.values()) {
p.put(paxe.getId(), paxe);
}
}
}
private enum Ores {
Copper(31081, 1, 175, 436, 3),
Tin(31077, 1, 175, 438, 3),
Iron(31073, 15, 350, 440, 4),
Coal(31068, 30, 500, 453, 5),
Gold(31065, 40, 650, 444, 5),
Mithril(31086, 50, 800, 447, 7),
Adamantite(31083, 70, 950, 449, 10),
Adamantite2(31085, 70, 950, 449, 10),
Runite(14859, 85, 1250, 451, 15);
private int objid, itemid, req, xp, ticks;
private Ores(int objid, int req, int xp, int itemid, int ticks) {
this.objid = objid;
this.req = req;
this.xp = xp;
this.itemid = itemid;
this.ticks = ticks;
}
public int getObjId() {
return objid;
}
public int getLevelReq(){
return req;
}
public int getXpAmount(){
return xp;
}
public int getItemId(){
return itemid;
}
public int getTicks() {
return ticks;
}
private static final Map<Integer, Ores> miningores = new HashMap<Integer, Ores>();
public static Ores forId(int id) {
return miningores.get(id);
}
static {
for (Ores ore : Ores.values()) {
miningores.put(ore.getObjId(), ore);
}
}
}
public boolean canUsePickaxe(Client c, int itemid) {
Pickaxe p = Pickaxe.forId(itemid);
return c.playerLevel[c.playerMining] >= p.getReq();
}
public boolean weaponIsPickaxe(Client c) {
for (int i : VALID_PICKAXES) {
return c.playerEquipment[c.playerWeapon] == i;
}
return false;
}
public boolean playerHasPickaxeInInventory(Client c) {
for (int i : VALID_PICKAXES) {
return c.getItems().playerHasItem(i);
}
return false;
}
public boolean playerHasPickaxe(Client c) {
return weaponIsPickaxe(c) || playerHasPickaxeInInventory(c);
}
public int getPick(Client c) {
for (int j = VALID_PICKAXES.length - 1; j >= 0; j--) {
if (c.playerEquipment[c.playerWeapon] == VALID_PICKAXES[j]) {
return VALID_PICKAXES[j];
}
}
for (int i = 0; i < c.playerItems.length; i++) {
for (int j = VALID_PICKAXES.length - 1; j >= 0; j--) {
if (c.playerItems[i] == VALID_PICKAXES[j] + 1) {
return VALID_PICKAXES[j];
}
}
}
return - 1;
}
public boolean canMineOre(Client c, int objid) {
Ores m = Ores.forId(objid);
return c.playerLevel[c.playerMining] >= m.getLevelReq();
}
public int getExperience(Client c, int objid) {
Ores m = Ores.forId(objid);
return m.getXpAmount();
}
public int getItem(Client c, int objid) {
Ores m = Ores.forId(objid);
return m.getItemId();
}
public int getAnimation(Client c, int itemid) {
Pickaxe p = Pickaxe.forId(itemid);
return p.getAnim();
}
public boolean useInventory(Client c) {
return weaponIsPickaxe(c) ? false : true;
}
public int getGoodAmountOfTicks(Client c, int objid) {
Ores m = Ores.forId(objid);
return (m.getTicks() + Misc.random(m.getTicks()));
}
public void startMining(final Client c, final int objid) {
final Ores m = Ores.forId(objid);
if (m != null) {
if (canMineOre(c, objid)) {
final Pickaxe p = Pickaxe.forId(getPick(c));
if (canUsePickaxe(c, getPick(c))) {
c.startAnimation(p.getAnim());
World.getWorld().submit(new Tick(2) {
@Override
public void execute() {
cycle++;
if (Misc.random(25) == 17) {
c.getItems().addItem(RANDOM_GEMS[(int)(RANDOM_GEMS.length * Math.random())], 1);
c.sendMessage("You find a gem!");
}
if (cycle == m.getTicks()) {
c.getItems().addItem(m.getItemId(), 1);
c.getPA().addSkillXP(m.getXpAmount(), c.playerMining);
c.getPA().refreshSkill(c.playerMining);
c.sendMessage("You mine a ore.");
c.getPA().resetAnimation();
this.stop();
}
c.getPA().resetAnimation();
this.stop();
}
});
} else {
c.sendMessage("You must have a mining level of "+p.getReq()+" to use this pickaxe.");
}
} else {
c.sendMessage("You need a mining level of "+m.getLevelReq()+" to mine this ore.");
}
}
}
}