[FEATURE] Data will now use json (#417)

* [FEATURE] Item data is now loaded from json

* [TASK] Renamed the old method into it's new use

* [TASK] Removed unused code

* [FEATURE] Added some more json's

* [CLEANUP] Code cleanup

* [FEATURE] NPC drops are now in json

* [CLEANUP] Removed testing class
This commit is contained in:
Sandro Coutinho
2020-08-13 17:50:04 +01:00
committed by GitHub
parent 09343c6a8c
commit 8a6cf6f1ae
33 changed files with 513556 additions and 22340 deletions
@@ -1,279 +1,317 @@
package com.rebotted.game.globalworldobjects;
import java.util.ArrayList;
import java.util.List;
import java.io.*;
import java.util.Scanner;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.rebotted.GameEngine;
import com.rebotted.game.objects.Objects;
import com.rebotted.game.players.Player;
import com.rebotted.util.DoorData;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.*;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Doors {
private static Doors singleton = null;
private static Doors singleton = null;
private List<Doors> doors = new ArrayList<Doors>();
private final List<Doors> doors = new ArrayList<>();
private File doorFile;
public static Doors getSingleton() {
if (singleton == null) {
singleton = new Doors(System.getProperty("user.dir") + "/data/doors.txt");
}
return singleton;
}
private File doorFile;
private Doors(String file){
doorFile = new File(file);
}
private Doors(int door, int x, int y, int z, int face, int type, int open) {
this.doorId = door;
this.originalId = door;
this.doorX = x;
this.doorY = y;
this.originalX = x;
this.originalY = y;
this.doorZ = z;
this.originalFace = face;
this.currentFace = face;
this.type = type;
this.open = open;
}
private Doors getDoor(int id, int x, int y, int z) {
for (Doors d : doors) {
if (d.doorId == id) {
if (d.doorX == x && d.doorY == y && d.doorZ == z) {
return d;
}
}
}
return null;
}
public static Doors getSingleton() {
if (singleton == null) {
singleton = new Doors(System.getProperty("user.dir") + "/data/doors.json");
}
return singleton;
}
public boolean handleDoor(Player player, int id, int x, int y, int z) {
Doors d = getDoor(id, x, y, z);
private Doors(String file) {
doorFile = new File(file);
}
if (d == null) {
//System.out.println("D: " + id + " null debug x: " + x + " y: " + y + ".");
return DoubleDoors.getSingleton().handleDoor(player, id, x, y, z);
}
private Doors(int door, int x, int y, int z, int face, int type, int open) {
this.doorId = door;
this.originalId = door;
this.doorX = x;
this.doorY = y;
this.originalX = x;
this.originalY = y;
this.doorZ = z;
this.originalFace = face;
this.currentFace = face;
this.type = type;
this.open = open;
}
//todo: improvment: if player manage to get to door then open the door.
if(player.distanceToPoint(x, y) > 1) {
//System.out.println("Door (single): " + id + " not in distance debug at x: " + x + " y: " + y + ".");
return false;
}
private Doors getDoor(int id, int x, int y, int z) {
for (Doors d : doors) {
if (d.doorId == id) {
if (d.doorX == x && d.doorY == y && d.doorZ == z) {
return d;
}
}
}
return null;
}
//Remove clipping for old door (gets added back in placeObject)
//Region.removeClipping(x, y, z);
public boolean handleDoor(Player player, int id, int x, int y, int z) {
Doors d = getDoor(id, x, y, z);
int xAdjustment = 0, yAdjustment = 0;
if (d.type == 0) {
if (d.open == 0) {
if (d.originalFace == 0 && d.currentFace == 0) {
xAdjustment = -1;
} else if (d.originalFace == 1 && d.currentFace == 1) {
yAdjustment = 1;
} else if (d.originalFace == 2 && d.currentFace == 2) {
xAdjustment = 1;
} else if (d.originalFace == 3 && d.currentFace == 3) {
yAdjustment = -1;
}
} else if (d.open == 1) {
if (d.originalFace == 0 && d.currentFace == 0) {
yAdjustment = 1;
} else if (d.originalFace == 1 && d.currentFace == 1) {
xAdjustment = 1;
} else if (d.originalFace == 2 && d.currentFace == 2) {
yAdjustment = -1;
} else if (d.originalFace == 3 && d.currentFace == 3) {
xAdjustment = -1;
}
}
} else if (d.type == 9) {
if (d.open == 0) {
if (d.originalFace == 0 && d.currentFace == 0) {
xAdjustment = 1;
} else if (d.originalFace == 1 && d.currentFace == 1) {
xAdjustment = 1;
} else if (d.originalFace == 2 && d.currentFace == 2) {
xAdjustment = -1;
} else if (d.originalFace == 3 && d.currentFace == 3) {
xAdjustment = -1;
}
} else if (d.open == 1) {
if (d.originalFace == 0 && d.currentFace == 0) {
xAdjustment = 1;
} else if (d.originalFace == 1 && d.currentFace == 1) {
xAdjustment = 1;
} else if (d.originalFace == 2 && d.currentFace == 2) {
xAdjustment = -1;
} else if (d.originalFace == 3 && d.currentFace == 3) {
xAdjustment = -1;
}
}
}
if (xAdjustment != 0 || yAdjustment != 0) {
Objects o = new Objects(-1, d.doorX, d.doorY, d.doorZ, 0, d.type, 0);
GameEngine.objectHandler.placeObject(o);
}
if (d.doorX == d.originalX && d.doorY == d.originalY) {
d.doorX += xAdjustment;
d.doorY += yAdjustment;
} else {
Objects o = new Objects(-1, d.doorX, d.doorY, d.doorZ, 0, d.type, 0);
GameEngine.objectHandler.placeObject(o);
d.doorX = d.originalX;
d.doorY = d.originalY;
}
if (d.doorId == d.originalId) {
if (d.open == 0) {
d.doorId += 1;
} else if (d.open == 1) {
d.doorId -= 1;
}
} else if (d.doorId != d.originalId) {
if (d.open == 0) {
d.doorId -= 1;
} else if (d.open == 1) {
d.doorId += 1;
}
}
GameEngine.objectHandler.placeObject(new Objects(d.doorId, d.doorX, d.doorY, d.doorZ, getNextFace(d), d.type, 0));
return true;
}
private int getNextFace(Doors d) {
int f = d.originalFace;
if (d.type == 0) {
if (d.open == 0) {
if (d.originalFace == 0 && d.currentFace == 0) {
f = 1;
} else if (d.originalFace == 1 && d.currentFace == 1) {
f = 2;
} else if (d.originalFace == 2 && d.currentFace == 2) {
f = 3;
} else if (d.originalFace == 3 && d.currentFace == 3) {
f = 0;
} else if (d.originalFace != d.currentFace){
f = d.originalFace;
}
} else if (d.open == 1) {
if (d.originalFace == 0 && d.currentFace == 0) {
f = 3;
} else if (d.originalFace == 1 && d.currentFace == 1) {
f = 0;
} else if (d.originalFace == 2 && d.currentFace == 2) {
f = 1;
} else if (d.originalFace == 3 && d.currentFace == 3) {
f = 2;
} else if (d.originalFace != d.currentFace){
f = d.originalFace;
}
}
} else if (d.type == 9) {
if (d.open == 0) {
if (d.originalFace == 0 && d.currentFace == 0) {
f = 3;
} else if (d.originalFace == 1 && d.currentFace == 1) {
f = 2;
} else if (d.originalFace == 2 && d.currentFace == 2) {
f = 1;
} else if (d.originalFace == 3 && d.currentFace == 3) {
f = 0;
} else if (d.originalFace != d.currentFace){
f = d.originalFace;
}
} else if (d.open == 1) {
if (d.originalFace == 0 && d.currentFace == 0) {
f = 3;
} else if (d.originalFace == 1 && d.currentFace == 1) {
f = 0;
} else if (d.originalFace == 2 && d.currentFace == 2) {
f = 1;
} else if (d.originalFace == 3 && d.currentFace == 3) {
f = 2;
} else if (d.originalFace != d.currentFace){
f = d.originalFace;
}
}
}
d.currentFace = f;
return f;
}
public void load() {
//long start = System.currentTimeMillis();
try {
singleton.processLineByLine();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//System.out.println("Loaded "+ doors.size() +" doors in "+ (System.currentTimeMillis() - start) +"ms.");
}
private final void processLineByLine() throws FileNotFoundException {
Scanner scanner = new Scanner(new FileReader(doorFile));
try {
while(scanner.hasNextLine()) {
processLine(scanner.nextLine());
}
} finally {
scanner.close();
}
}
protected void processLine(String line){
Scanner scanner = new Scanner(line);
scanner.useDelimiter(" ");
try {
while(scanner.hasNextLine()) {
int id = Integer.parseInt(scanner.next());
int x = Integer.parseInt(scanner.next());
int y = Integer.parseInt(scanner.next());
int f = Integer.parseInt(scanner.next());
int z = Integer.parseInt(scanner.next());
int t = Integer.parseInt(scanner.next());
doors.add(new Doors(id,x,y,z,f,t,alreadyOpen(id)?1:0));
}
} finally {
scanner.close();
}
}
private boolean alreadyOpen(int id) {
for (int i = 0; i < openDoors.length; i++) {
if (openDoors[i] == id) {
return true;
}
}
return false;
}
if (d == null) {
//System.out.println("D: " + id + " null debug x: " + x + " y: " + y + ".");
return DoubleDoors.getSingleton().handleDoor(player, id, x, y, z);
}
//todo: improvment: if player manage to get to door then open the door.
if (player.distanceToPoint(x, y) > 1) {
//System.out.println("Door (single): " + id + " not in distance debug at x: " + x + " y: " + y + ".");
return false;
}
//Remove clipping for old door (gets added back in placeObject)
//Region.removeClipping(x, y, z);
int xAdjustment = 0, yAdjustment = 0;
if (d.type == 0) {
if (d.open == 0) {
if (d.originalFace == 0 && d.currentFace == 0) {
xAdjustment = -1;
} else if (d.originalFace == 1 && d.currentFace == 1) {
yAdjustment = 1;
} else if (d.originalFace == 2 && d.currentFace == 2) {
xAdjustment = 1;
} else if (d.originalFace == 3 && d.currentFace == 3) {
yAdjustment = -1;
}
} else if (d.open == 1) {
if (d.originalFace == 0 && d.currentFace == 0) {
yAdjustment = 1;
} else if (d.originalFace == 1 && d.currentFace == 1) {
xAdjustment = 1;
} else if (d.originalFace == 2 && d.currentFace == 2) {
yAdjustment = -1;
} else if (d.originalFace == 3 && d.currentFace == 3) {
xAdjustment = -1;
}
}
} else if (d.type == 9) {
if (d.open == 0) {
if (d.originalFace == 0 && d.currentFace == 0) {
xAdjustment = 1;
} else if (d.originalFace == 1 && d.currentFace == 1) {
xAdjustment = 1;
} else if (d.originalFace == 2 && d.currentFace == 2) {
xAdjustment = -1;
} else if (d.originalFace == 3 && d.currentFace == 3) {
xAdjustment = -1;
}
} else if (d.open == 1) {
if (d.originalFace == 0 && d.currentFace == 0) {
xAdjustment = 1;
} else if (d.originalFace == 1 && d.currentFace == 1) {
xAdjustment = 1;
} else if (d.originalFace == 2 && d.currentFace == 2) {
xAdjustment = -1;
} else if (d.originalFace == 3 && d.currentFace == 3) {
xAdjustment = -1;
}
}
}
if (xAdjustment != 0 || yAdjustment != 0) {
Objects o = new Objects(-1, d.doorX, d.doorY, d.doorZ, 0, d.type, 0);
GameEngine.objectHandler.placeObject(o);
}
if (d.doorX == d.originalX && d.doorY == d.originalY) {
d.doorX += xAdjustment;
d.doorY += yAdjustment;
} else {
Objects o = new Objects(-1, d.doorX, d.doorY, d.doorZ, 0, d.type, 0);
GameEngine.objectHandler.placeObject(o);
d.doorX = d.originalX;
d.doorY = d.originalY;
}
if (d.doorId == d.originalId) {
if (d.open == 0) {
d.doorId += 1;
} else if (d.open == 1) {
d.doorId -= 1;
}
} else if (d.doorId != d.originalId) {
if (d.open == 0) {
d.doorId -= 1;
} else if (d.open == 1) {
d.doorId += 1;
}
}
GameEngine.objectHandler.placeObject(new Objects(d.doorId, d.doorX, d.doorY, d.doorZ, getNextFace(d), d.type, 0));
return true;
}
private int getNextFace(Doors d) {
int f = d.originalFace;
if (d.type == 0) {
if (d.open == 0) {
if (d.originalFace == 0 && d.currentFace == 0) {
f = 1;
} else if (d.originalFace == 1 && d.currentFace == 1) {
f = 2;
} else if (d.originalFace == 2 && d.currentFace == 2) {
f = 3;
} else if (d.originalFace == 3 && d.currentFace == 3) {
f = 0;
} else if (d.originalFace != d.currentFace) {
f = d.originalFace;
}
} else if (d.open == 1) {
if (d.originalFace == 0 && d.currentFace == 0) {
f = 3;
} else if (d.originalFace == 1 && d.currentFace == 1) {
f = 0;
} else if (d.originalFace == 2 && d.currentFace == 2) {
f = 1;
} else if (d.originalFace == 3 && d.currentFace == 3) {
f = 2;
} else if (d.originalFace != d.currentFace) {
f = d.originalFace;
}
}
} else if (d.type == 9) {
if (d.open == 0) {
if (d.originalFace == 0 && d.currentFace == 0) {
f = 3;
} else if (d.originalFace == 1 && d.currentFace == 1) {
f = 2;
} else if (d.originalFace == 2 && d.currentFace == 2) {
f = 1;
} else if (d.originalFace == 3 && d.currentFace == 3) {
f = 0;
} else if (d.originalFace != d.currentFace) {
f = d.originalFace;
}
} else if (d.open == 1) {
if (d.originalFace == 0 && d.currentFace == 0) {
f = 3;
} else if (d.originalFace == 1 && d.currentFace == 1) {
f = 0;
} else if (d.originalFace == 2 && d.currentFace == 2) {
f = 1;
} else if (d.originalFace == 3 && d.currentFace == 3) {
f = 2;
} else if (d.originalFace != d.currentFace) {
f = d.originalFace;
}
}
}
d.currentFace = f;
return f;
}
public void load() {
Gson gson = new Gson();
//long start = System.currentTimeMillis();
try {
Type collectionType = new TypeToken<DoorData[]>() {
}.getType();
DoorData[] doorData = gson.fromJson(new FileReader(doorFile), collectionType);
for (DoorData data : doorData) {
for (DoorData.Location location : data.getLocations()) {
doors.add(new Doors(data.getId(), location.getX(), location.getY(), location.getHeight(), data.getFace(), data.getType(), alreadyOpen(data.getId()) ? 1 : 0));
}
}
//singleton.writeJsonDump();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//System.out.println("Loaded "+ doors.size() +" doors in "+ (System.currentTimeMillis() - start) +" ms.");
}
private void writeJsonDump() throws FileNotFoundException {
try (Scanner scanner = new Scanner(new FileReader(doorFile))) {
while (scanner.hasNextLine()) {
processLine(scanner.nextLine());
}
}
}
protected void processLine(String line) {
JSONArray array = new JSONArray();
Scanner scanner = new Scanner(line);
scanner.useDelimiter(" ");
try {
while (scanner.hasNextLine()) {
int id = Integer.parseInt(scanner.next());
int x = Integer.parseInt(scanner.next());
int y = Integer.parseInt(scanner.next());
int face = Integer.parseInt(scanner.next());
int height = Integer.parseInt(scanner.next());
int type = Integer.parseInt(scanner.next());
JSONObject object = new JSONObject();
object.put("id", id);
JSONArray jsonArray = new JSONArray();
JSONObject object1 = new JSONObject();
object1.put("x", x);
object1.put("y", y);
object1.put("height", height);
jsonArray.put(0, object1);
object.put("location", jsonArray);
object.put("face", face);
object.put("type", type);
array.put(object);
}
} finally {
scanner.close();
try {
FileWriter fileWriter = new FileWriter("doors-dump.json");
fileWriter.write(array.toString());
System.out.println(array.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
}
private boolean alreadyOpen(int id) {
for (int openDoor : OPEN_DOORS) {
if (openDoor == id) {
return true;
}
}
return false;
}
private int doorId;
private int originalId;
private int doorX;
private int doorY;
private int originalX;
private int originalY;
private int doorZ;
private int originalFace;
private int currentFace;
private int type;
private int open;
private static final int[] OPEN_DOORS = {
1504, 1514, 1517, 1520, 1531,
1534, 2033, 2035, 2037, 2998,
3271, 4468, 4697, 6101, 6103,
6105, 6107, 6109, 6111, 6113,
6115, 6976, 6978, 8696, 8819,
10261, 10263, 10265, 11708, 11710,
11712, 11715, 11994, 12445, 13002,
};
private int doorId;
private int originalId;
private int doorX;
private int doorY;
private int originalX;
private int originalY;
private int doorZ;
private int originalFace;
private int currentFace;
private int type;
private int open;
private static int[] openDoors = {
1504, 1514, 1517, 1520, 1531,
1534, 2033, 2035, 2037, 2998,
3271, 4468, 4697, 6101,6103,
6105, 6107, 6109, 6111, 6113,
6115, 6976, 6978, 8696, 8819,
10261, 10263,10265,11708,11710,
11712,11715,11994,12445, 13002,
};
}
@@ -1,393 +1,429 @@
package com.rebotted.game.globalworldobjects;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.rebotted.GameEngine;
import com.rebotted.game.objects.Objects;
import com.rebotted.game.players.Player;
import com.rebotted.util.DoorData;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.*;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
*
* @author Killamess
*
*/
public class DoubleDoors {
private static DoubleDoors singleton = null;
private List<DoubleDoors> doors = new ArrayList<DoubleDoors>();
private static DoubleDoors singleton = null;
private File doorFile;
public static DoubleDoors getSingleton() {
if (singleton == null) {
singleton = new DoubleDoors("./data/doubledoors.txt");
}
return singleton;
}
private final List<DoubleDoors> doors = new ArrayList<>();
private DoubleDoors(String file){
doorFile = new File(file);
}
private DoubleDoors getDoor(int id, int x, int y, int z) {
for (DoubleDoors d : doors) {
if (d.doorId == id) {
if (d.x == x && d.y == y && d.z == z) {
return d;
}
}
}
return null;
}
public boolean handleDoor(Player player, int id, int x, int y, int z) {
DoubleDoors doorClicked = getDoor(id, x, y, z);
private File doorFile;
if (doorClicked == null) {
//System.out.println("Door: " + id + " null debug at x: " + x + " y: " + y + ".");
//GameEngine.objectHandler.placeObject(new Objects(-1, x, y, z, 0, 0, 0));
return true;
}
if (doorClicked.doorId > 15000) {
// System.out.println("Door: " + id + " clicking debug x: " + x + " y: " + y + ".");
return true; //nearly all of these are not opened
}
public static DoubleDoors getSingleton() {
if (singleton == null) {
singleton = new DoubleDoors(System.getProperty("user.dir") + "/data/doubledoors.json");
}
return singleton;
}
//Region.removeClipping(x, y, z);
private DoubleDoors(String file) {
doorFile = new File(file);
}
if (doorClicked.open == 0) {
if (doorClicked.originalFace == 0) {
DoubleDoors lowerDoor = getDoor(id - 3, x, y -1, z);
DoubleDoors upperDoor = getDoor(id + 3, x, y +1, z);
if (lowerDoor != null) {
changeLeftDoor(lowerDoor);
changeRightDoor(doorClicked);
} else if (upperDoor != null) {
changeLeftDoor(doorClicked);
changeRightDoor(upperDoor);
}
} else if (doorClicked.originalFace == 1) {
DoubleDoors westDoor = getDoor(id - 3, x -1, y, z);
DoubleDoors eastDoor = getDoor(id + 3, x +1, y, z);
if (westDoor != null) {
changeLeftDoor(westDoor);
changeRightDoor(doorClicked);
} else if (eastDoor != null) {
changeLeftDoor(doorClicked);
changeRightDoor(eastDoor);
}
} else if (doorClicked.originalFace == 2) {
DoubleDoors lowerDoor = getDoor(id - 3, x, y +1, z);
DoubleDoors upperDoor = getDoor(id + 3, x, y -1, z);
if (lowerDoor != null) {
changeLeftDoor(lowerDoor);
changeRightDoor(doorClicked);
} else if (upperDoor != null) {
changeLeftDoor(doorClicked);
changeRightDoor(upperDoor);
}
} else if (doorClicked.originalFace == 3) {
DoubleDoors westDoor = getDoor(id + 3, x -1, y, z);
DoubleDoors eastDoor = getDoor(id - 3, x +1, y, z);
if (westDoor != null) {
changeLeftDoor(westDoor);
changeRightDoor(doorClicked);
} else if (eastDoor != null) {
changeLeftDoor(doorClicked);
changeRightDoor(eastDoor);
}
}
} else if (doorClicked.open == 1) {
if (doorClicked.originalFace == 0) {
DoubleDoors westDoor = getDoor(id - 3, x -1, y, z);
DoubleDoors upperDoor = getDoor(id + 3, x +1, y, z);
if (westDoor != null) {
changeLeftDoor(westDoor);
changeRightDoor(doorClicked);
} else if (upperDoor != null) {
changeLeftDoor(doorClicked);
changeRightDoor(upperDoor);
}
} else if (doorClicked.originalFace == 1) {
DoubleDoors northDoor = getDoor(id - 3, x, y + 1, z);
DoubleDoors southDoor = getDoor(id + 3, x, y -1, z);
if (northDoor != null) {
changeLeftDoor(northDoor);
changeRightDoor(doorClicked);
} else if (southDoor != null) {
changeLeftDoor(doorClicked);
changeRightDoor(southDoor);
}
} else if (doorClicked.originalFace == 2) {
DoubleDoors westDoor = getDoor(id - 3, x -1, y, z);
DoubleDoors eastDoor = getDoor(id + 3, x, y -1, z);
if (westDoor != null) {
changeLeftDoor(westDoor);
changeRightDoor(doorClicked);
} else if (eastDoor != null) {
changeLeftDoor(doorClicked);
changeRightDoor(eastDoor);
}
} else if (doorClicked.originalFace == 3) {
DoubleDoors northDoor = getDoor(id - 3, x, y + 1, z);
DoubleDoors southDoor = getDoor(id + 3, x, y -1, z);
if (northDoor != null) {
changeLeftDoor(northDoor);
changeRightDoor(doorClicked);
} else if (southDoor != null) {
changeLeftDoor(doorClicked);
changeRightDoor(southDoor);
}
}
}
return true;
}
private DoubleDoors getDoor(int id, int x, int y, int z) {
for (DoubleDoors d : doors) {
if (d.doorId == id) {
if (d.x == x && d.y == y && d.z == z) {
return d;
}
}
}
return null;
}
public void changeLeftDoor(DoubleDoors d) {
int xAdjustment = 0, yAdjustment = 0;
if (d.open == 0) {
if (d.originalFace == 0 && d.currentFace == 0) {
xAdjustment = -1;
} else if (d.originalFace == 1 && d.currentFace == 1) {
yAdjustment = 1;
} else if (d.originalFace == 2 && d.currentFace == 2) {
xAdjustment = +1;
} else if (d.originalFace == 3 && d.currentFace == 3) {
yAdjustment = -1;
}
} else if (d.open == 1) {
if (d.originalFace == 0 && d.currentFace == 0) {
yAdjustment = -1;
} else if (d.originalFace == 1 && d.currentFace == 1) {
xAdjustment = -1;
} else if (d.originalFace == 2 && d.currentFace == 2) {
xAdjustment = -1;
} else if (d.originalFace == 3 && d.currentFace == 3) {
xAdjustment = -1;
}
}
if (xAdjustment != 0 || yAdjustment != 0) {
GameEngine.objectHandler.placeObject(new Objects(-1, d.x, d.y, d.z, 0, 0, 0));
}
if (d.x == d.originalX && d.y == d.originalY) {
d.x += xAdjustment;
d.y += yAdjustment;
} else {
GameEngine.objectHandler.placeObject(new Objects(-1, d.x, d.y, d.z, 0, 0, 0));
d.x = d.originalX;
d.y = d.originalY;
}
if (d.doorId == d.originalId) {
if (d.open == 0) {
d.doorId += 1;
} else if (d.open == 1) {
d.doorId -= 1;
}
} else if (d.doorId != d.originalId) {
if (d.open == 0) {
d.doorId = d.originalId;
} else if (d.open == 1) {
d.doorId = d.originalId;
}
}
GameEngine.objectHandler.placeObject(new Objects(d.doorId, d.x, d.y, d.z, getNextLeftFace(d), 0, 0));
}
private int getNextLeftFace(DoubleDoors d) {
int f = d.originalFace;
public boolean handleDoor(Player player, int id, int x, int y, int z) {
DoubleDoors doorClicked = getDoor(id, x, y, z);
if (d.open == 0) {
if (d.originalFace == 0 && d.currentFace == 0) {
f = 3;
} else if (d.originalFace == 1 && d.currentFace == 1) {
f = 0;
} else if (d.originalFace == 2 && d.currentFace == 2) {
f = 1;
} else if (d.originalFace == 3 && d.currentFace == 3) {
f = 0;
} else if (d.originalFace != d.currentFace){
f = d.originalFace;
}
} else if (d.open == 1) {
if (d.originalFace == 0 && d.currentFace == 0) {
f = 1;
} else if (d.originalFace == 1 && d.currentFace == 1) {
f = 2;
} else if (d.originalFace == 2 && d.currentFace == 2) {
f = 1;
} else if (d.originalFace == 3 && d.currentFace == 3) {
f = 2;
} else if (d.originalFace != d.currentFace){
f = d.originalFace;
}
}
d.currentFace = f;
return f;
}
public void changeRightDoor(DoubleDoors d) {
int xAdjustment = 0, yAdjustment = 0;
if (d.open == 0) {
if (d.originalFace == 0 && d.currentFace == 0) {
xAdjustment = -1;
} else if (d.originalFace == 1 && d.currentFace == 1) {
yAdjustment = 1;
} else if (d.originalFace == 2 && d.currentFace == 2) {
xAdjustment = +1;
} else if (d.originalFace == 3 && d.currentFace == 3) {
yAdjustment = -1;
}
} else if (d.open == 1) {
if (d.originalFace == 0 && d.currentFace == 0) {
xAdjustment = 1;
} else if (d.originalFace == 1 && d.currentFace == 1) {
xAdjustment = -1;
} else if (d.originalFace == 2 && d.currentFace == 2) {
yAdjustment = -1;
} else if (d.originalFace == 3 && d.currentFace == 3) {
xAdjustment = -1;
}
}
if (xAdjustment != 0 || yAdjustment != 0) {
GameEngine.objectHandler.placeObject(new Objects(-1, d.x, d.y, d.z, 0, 0, 0));
}
if (d.x == d.originalX && d.y == d.originalY) {
d.x += xAdjustment;
d.y += yAdjustment;
} else {
GameEngine.objectHandler.placeObject(new Objects(-1, d.x, d.y, d.z, 0, 0, 0));
d.x = d.originalX;
d.y = d.originalY;
}
if (d.doorId == d.originalId) {
if (d.open == 0) {
d.doorId += 1;
} else if (d.open == 1) {
d.doorId -= 1;
}
} else if (d.doorId != d.originalId) {
if (d.open == 0) {
d.doorId = d.originalId;
} else if (d.open == 1) {
d.doorId = d.originalId;
}
}
GameEngine.objectHandler.placeObject(new Objects(d.doorId, d.x, d.y, d.z, getNextRightFace(d), 0, 0));
}
private int getNextRightFace(DoubleDoors d) {
int f = d.originalFace;
if (doorClicked == null) {
//System.out.println("Door: " + id + " null debug at x: " + x + " y: " + y + ".");
//GameEngine.objectHandler.placeObject(new Objects(-1, x, y, z, 0, 0, 0));
return true;
}
if (doorClicked.doorId > 15000) {
// System.out.println("Door: " + id + " clicking debug x: " + x + " y: " + y + ".");
return true; //nearly all of these are not opened
}
if (d.open == 0) {
if (d.originalFace == 0 && d.currentFace == 0) {
f = 1;
} else if (d.originalFace == 1 && d.currentFace == 1) {
f = 2;
} else if (d.originalFace == 2 && d.currentFace == 2) {
f = 3;
} else if (d.originalFace == 3 && d.currentFace == 3) {
f = 2;
} else if (d.originalFace != d.currentFace){
f = d.originalFace;
//Region.removeClipping(x, y, z);
if (doorClicked.open == 0) {
if (doorClicked.originalFace == 0) {
DoubleDoors lowerDoor = getDoor(id - 3, x, y - 1, z);
DoubleDoors upperDoor = getDoor(id + 3, x, y + 1, z);
if (lowerDoor != null) {
changeLeftDoor(lowerDoor);
changeRightDoor(doorClicked);
} else if (upperDoor != null) {
changeLeftDoor(doorClicked);
changeRightDoor(upperDoor);
}
} else if (doorClicked.originalFace == 1) {
DoubleDoors westDoor = getDoor(id - 3, x - 1, y, z);
DoubleDoors eastDoor = getDoor(id + 3, x + 1, y, z);
if (westDoor != null) {
changeLeftDoor(westDoor);
changeRightDoor(doorClicked);
} else if (eastDoor != null) {
changeLeftDoor(doorClicked);
changeRightDoor(eastDoor);
}
} else if (doorClicked.originalFace == 2) {
DoubleDoors lowerDoor = getDoor(id - 3, x, y + 1, z);
DoubleDoors upperDoor = getDoor(id + 3, x, y - 1, z);
if (lowerDoor != null) {
changeLeftDoor(lowerDoor);
changeRightDoor(doorClicked);
} else if (upperDoor != null) {
changeLeftDoor(doorClicked);
changeRightDoor(upperDoor);
}
} else if (doorClicked.originalFace == 3) {
DoubleDoors westDoor = getDoor(id + 3, x - 1, y, z);
DoubleDoors eastDoor = getDoor(id - 3, x + 1, y, z);
if (westDoor != null) {
changeLeftDoor(westDoor);
changeRightDoor(doorClicked);
} else if (eastDoor != null) {
changeLeftDoor(doorClicked);
changeRightDoor(eastDoor);
}
}
} else if (doorClicked.open == 1) {
if (doorClicked.originalFace == 0) {
DoubleDoors westDoor = getDoor(id - 3, x - 1, y, z);
DoubleDoors upperDoor = getDoor(id + 3, x + 1, y, z);
if (westDoor != null) {
changeLeftDoor(westDoor);
changeRightDoor(doorClicked);
} else if (upperDoor != null) {
changeLeftDoor(doorClicked);
changeRightDoor(upperDoor);
}
} else if (doorClicked.originalFace == 1) {
DoubleDoors northDoor = getDoor(id - 3, x, y + 1, z);
DoubleDoors southDoor = getDoor(id + 3, x, y - 1, z);
if (northDoor != null) {
changeLeftDoor(northDoor);
changeRightDoor(doorClicked);
} else if (southDoor != null) {
changeLeftDoor(doorClicked);
changeRightDoor(southDoor);
}
} else if (doorClicked.originalFace == 2) {
DoubleDoors westDoor = getDoor(id - 3, x - 1, y, z);
DoubleDoors eastDoor = getDoor(id + 3, x, y - 1, z);
if (westDoor != null) {
changeLeftDoor(westDoor);
changeRightDoor(doorClicked);
} else if (eastDoor != null) {
changeLeftDoor(doorClicked);
changeRightDoor(eastDoor);
}
} else if (doorClicked.originalFace == 3) {
DoubleDoors northDoor = getDoor(id - 3, x, y + 1, z);
DoubleDoors southDoor = getDoor(id + 3, x, y - 1, z);
if (northDoor != null) {
changeLeftDoor(northDoor);
changeRightDoor(doorClicked);
} else if (southDoor != null) {
changeLeftDoor(doorClicked);
changeRightDoor(southDoor);
}
}
}
return true;
}
public void changeLeftDoor(DoubleDoors d) {
int xAdjustment = 0, yAdjustment = 0;
if (d.open == 0) {
if (d.originalFace == 0 && d.currentFace == 0) {
xAdjustment = -1;
} else if (d.originalFace == 1 && d.currentFace == 1) {
yAdjustment = 1;
} else if (d.originalFace == 2 && d.currentFace == 2) {
xAdjustment = +1;
} else if (d.originalFace == 3 && d.currentFace == 3) {
yAdjustment = -1;
}
} else if (d.open == 1) {
if (d.originalFace == 0 && d.currentFace == 0) {
yAdjustment = -1;
} else if (d.originalFace == 1 && d.currentFace == 1) {
xAdjustment = -1;
} else if (d.originalFace == 2 && d.currentFace == 2) {
xAdjustment = -1;
} else if (d.originalFace == 3 && d.currentFace == 3) {
xAdjustment = -1;
}
}
if (xAdjustment != 0 || yAdjustment != 0) {
GameEngine.objectHandler.placeObject(new Objects(-1, d.x, d.y, d.z, 0, 0, 0));
}
if (d.x == d.originalX && d.y == d.originalY) {
d.x += xAdjustment;
d.y += yAdjustment;
} else {
GameEngine.objectHandler.placeObject(new Objects(-1, d.x, d.y, d.z, 0, 0, 0));
d.x = d.originalX;
d.y = d.originalY;
}
if (d.doorId == d.originalId) {
if (d.open == 0) {
d.doorId += 1;
} else if (d.open == 1) {
d.doorId -= 1;
}
} else if (d.doorId != d.originalId) {
if (d.open == 0) {
d.doorId = d.originalId;
} else if (d.open == 1) {
d.doorId = d.originalId;
}
}
GameEngine.objectHandler.placeObject(new Objects(d.doorId, d.x, d.y, d.z, getNextLeftFace(d), 0, 0));
}
private int getNextLeftFace(DoubleDoors d) {
int f = d.originalFace;
if (d.open == 0) {
if (d.originalFace == 0 && d.currentFace == 0) {
f = 3;
} else if (d.originalFace == 1 && d.currentFace == 1) {
f = 0;
} else if (d.originalFace == 2 && d.currentFace == 2) {
f = 1;
} else if (d.originalFace == 3 && d.currentFace == 3) {
f = 0;
} else if (d.originalFace != d.currentFace) {
f = d.originalFace;
}
} else if (d.open == 1) {
if (d.originalFace == 0 && d.currentFace == 0) {
f = 1;
} else if (d.originalFace == 1 && d.currentFace == 1) {
f = 2;
} else if (d.originalFace == 2 && d.currentFace == 2) {
f = 1;
} else if (d.originalFace == 3 && d.currentFace == 3) {
f = 2;
} else if (d.originalFace != d.currentFace) {
f = d.originalFace;
}
}
d.currentFace = f;
return f;
}
public void changeRightDoor(DoubleDoors d) {
int xAdjustment = 0, yAdjustment = 0;
if (d.open == 0) {
if (d.originalFace == 0 && d.currentFace == 0) {
xAdjustment = -1;
} else if (d.originalFace == 1 && d.currentFace == 1) {
yAdjustment = 1;
} else if (d.originalFace == 2 && d.currentFace == 2) {
xAdjustment = +1;
} else if (d.originalFace == 3 && d.currentFace == 3) {
yAdjustment = -1;
}
} else if (d.open == 1) {
if (d.originalFace == 0 && d.currentFace == 0) {
xAdjustment = 1;
} else if (d.originalFace == 1 && d.currentFace == 1) {
xAdjustment = -1;
} else if (d.originalFace == 2 && d.currentFace == 2) {
yAdjustment = -1;
} else if (d.originalFace == 3 && d.currentFace == 3) {
xAdjustment = -1;
}
}
if (xAdjustment != 0 || yAdjustment != 0) {
GameEngine.objectHandler.placeObject(new Objects(-1, d.x, d.y, d.z, 0, 0, 0));
}
if (d.x == d.originalX && d.y == d.originalY) {
d.x += xAdjustment;
d.y += yAdjustment;
} else {
GameEngine.objectHandler.placeObject(new Objects(-1, d.x, d.y, d.z, 0, 0, 0));
d.x = d.originalX;
d.y = d.originalY;
}
if (d.doorId == d.originalId) {
if (d.open == 0) {
d.doorId += 1;
} else if (d.open == 1) {
d.doorId -= 1;
}
} else if (d.doorId != d.originalId) {
if (d.open == 0) {
d.doorId = d.originalId;
} else if (d.open == 1) {
d.doorId = d.originalId;
}
}
GameEngine.objectHandler.placeObject(new Objects(d.doorId, d.x, d.y, d.z, getNextRightFace(d), 0, 0));
}
private int getNextRightFace(DoubleDoors d) {
int f = d.originalFace;
if (d.open == 0) {
if (d.originalFace == 0 && d.currentFace == 0) {
f = 1;
} else if (d.originalFace == 1 && d.currentFace == 1) {
f = 2;
} else if (d.originalFace == 2 && d.currentFace == 2) {
f = 3;
} else if (d.originalFace == 3 && d.currentFace == 3) {
f = 2;
} else if (d.originalFace != d.currentFace) {
f = d.originalFace;
}
} else if (d.open == 1) {
if (d.originalFace == 0 && d.currentFace == 0) {
f = 3;
} else if (d.originalFace == 1 && d.currentFace == 1) {
f = 0;
} else if (d.originalFace == 2 && d.currentFace == 2) {
f = 1;
} else if (d.originalFace == 3 && d.currentFace == 3) {
f = 2;
} else if (d.originalFace != d.currentFace) {
f = d.originalFace;
}
}
d.currentFace = f;
return f;
}
private int doorId;
private int originalId;
private int open;
private int x;
private int y;
private int z;
private int originalX;
private int originalY;
private int currentFace;
private int originalFace;
public DoubleDoors(int id, int x, int y, int z, int f, int open) {
this.doorId = id;
this.originalId = id;
this.open = open;
this.x = x;
this.originalX = x;
this.y = y;
this.z = z;
this.originalY = y;
this.currentFace = f;
this.originalFace = f;
}
public boolean isOpenDoor(int id) {
for (int openDoor : OPEN_DOORS) {
if (id == openDoor || id + 3 == openDoor) {
return true;
}
}
return false;
}
//Have not found any others yet. Maybe only 1 type of double
//doors exist to operate.
private static final int[] OPEN_DOORS = {
1520, 1517
};
public void load() {
Gson gson = new Gson();
long start = System.currentTimeMillis();
try {
Type collectionType = new TypeToken<DoorData[]>() {
}.getType();
DoorData[] doorData = gson.fromJson(new FileReader(doorFile), collectionType);
for (DoorData data : doorData) {
for (DoorData.Location location : data.getLocations()) {
doors.add(new DoubleDoors(data.getId(), location.getX(), location.getY(), location.getHeight(), data.getFace(), isOpenDoor(data.getId()) ? 1 : 0));
}
}
//singleton.writeJsonDump();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
System.out.println("Loaded "+ doors.size() +" Double doors in "+ (System.currentTimeMillis() - start) +"ms.");
}
private void writeJsonDump() throws FileNotFoundException {
try (Scanner scanner = new Scanner(new FileReader(doorFile))) {
while (scanner.hasNextLine()) {
processLine(scanner.nextLine());
}
}
}
protected void processLine(String line) {
JSONArray array = new JSONArray();
Scanner scanner = new Scanner(line);
scanner.useDelimiter(" ");
try {
while (scanner.hasNextLine()) {
int id = Integer.parseInt(scanner.next());
int x = Integer.parseInt(scanner.next());
int y = Integer.parseInt(scanner.next());
int f = Integer.parseInt(scanner.next());
int z = Integer.parseInt(scanner.next());
JSONObject object = new JSONObject();
object.put("id", id);
JSONArray jsonArray = new JSONArray();
JSONObject object1 = new JSONObject();
object1.put("x", x);
object1.put("y", y);
object1.put("height", z);
jsonArray.put(0, object1);
object.put("locations", jsonArray);
object.put("face", f);
array.put(object);
}
} finally {
scanner.close();
try {
FileWriter fileWriter = new FileWriter("doubledoors-dump.json");
fileWriter.write(array.toString());
System.out.println(array.toString());
} catch (IOException e) {
e.printStackTrace();
}
} else if (d.open == 1) {
if (d.originalFace == 0 && d.currentFace == 0) {
f = 3;
} else if (d.originalFace == 1 && d.currentFace == 1) {
f = 0;
} else if (d.originalFace == 2 && d.currentFace == 2) {
f = 1;
} else if (d.originalFace == 3 && d.currentFace == 3) {
f = 2;
} else if (d.originalFace != d.currentFace){
f = d.originalFace;
}
}
d.currentFace = f;
return f;
}
private int doorId;
private int originalId;
private int open;
private int x;
private int y;
private int z;
private int originalX;
private int originalY;
private int currentFace;
private int originalFace;
public DoubleDoors(int id, int x, int y, int z, int f, int open) {
this.doorId = id;
this.originalId = id;
this.open = open;
this.x = x;
this.originalX = x;
this.y = y;
this.z = z;
this.originalY = y;
this.currentFace = f;
this.originalFace = f;
}
public boolean isOpenDoor(int id){
for (int i = 0; i < openDoors.length; i++) {
if (id == openDoors[i] || id + 3 == openDoors[i]) {
return true;
}
}
return false;
}
//Have not found any others yet. Maybe only 1 type of double
//doors exist to operate.
private static int[] openDoors = {
1520, 1517
};
public void load() {
//long start = System.currentTimeMillis();
try {
singleton.processLineByLine();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//System.out.println("Loaded "+ doors.size() +" Double doors in "+ (System.currentTimeMillis() - start) +"ms.");
}
private final void processLineByLine() throws FileNotFoundException {
Scanner scanner = new Scanner(new FileReader(doorFile));
try {
while(scanner.hasNextLine()) {
processLine(scanner.nextLine());
}
} finally {
scanner.close();
}
}
protected void processLine(String line){
Scanner scanner = new Scanner(line);
scanner.useDelimiter(" ");
try {
while(scanner.hasNextLine()) {
int id = Integer.parseInt(scanner.next());
int x = Integer.parseInt(scanner.next());
int y = Integer.parseInt(scanner.next());
int f = Integer.parseInt(scanner.next());
int z = Integer.parseInt(scanner.next());
doors.add(new DoubleDoors(id, x, y, z, f, isOpenDoor(id) ? 1 : 0));
}
} finally {
scanner.close();
}
}
}
}
}
File diff suppressed because it is too large Load Diff
@@ -2,6 +2,8 @@ package com.rebotted.game.npcs.drops;
import com.rebotted.util.Misc;
import java.util.Arrays;
public class ItemDrop {
public int item_id, chance;
public int[] amounts;
@@ -29,4 +31,13 @@ public class ItemDrop {
public int getAmount(){
return Misc.random(this.amounts[0], this.amounts[1]);
}
@Override
public String toString() {
return "ItemDrop{" +
"item_id=" + item_id +
", chance=" + chance +
", amounts=" + Arrays.toString(amounts) +
'}';
}
}
File diff suppressed because it is too large Load Diff
@@ -1,250 +1,97 @@
package com.rebotted.game.npcs.drops;
import com.google.gson.Gson;
import com.rebotted.GameEngine;
import com.rebotted.game.items.ItemList;
import com.rebotted.util.Misc;
import com.rebotted.util.NpcDrop;
import java.io.FileNotFoundException;
import java.io.FileReader;
/**
* Npc Drops Handler
* @author Andrew (Mr Extremez)
*
* @author Andrew (Mr Extremez), SandroC
*/
public class NPCDropsHandler {
private static NpcDrop[] npcDrops;
public static int // found on http://runescape.wikia.com/wiki/Drop_rate
ALWAYS = 0,
COINSRATE = 3,
COMMON = 32,
UNCOMMON = 64,
RARE = 256,
VERY_RARE = 512;
/*public static int // found on http://runescape.wikia.com/wiki/Drop rate
ALWAYS = 0,
COINSRATE = 3,
COMMON = 32,
UNCOMMON = 64,
RARE = 256,
VERY_RARE = 512;*/
/**
* Handles the npc drops for the npc names.
*
* @param NPCId
* @return
*/
public static final ItemDrop[] NPC_DROPS(String npc, int NPCId) {
if (npc.equals("man") || npc.equals("woman") || npc.equals("drunken_man")) {
return NPCDrops.man;
} else if (npc.equals("skeletal_wyvern")) {
return NPCDrops.wyvern;
} else if (npc.equals("dark_beast")) {
return NPCDrops.darkbeast;
} else if (npc.equals("shade")) {
return NPCDrops.shade;
} else if (npc.equals("watchman")) {
return NPCDrops.watchman;
} else if (npc.equals("river_troll")) {
return NPCDrops.rivertroll;
} else if (npc.equals("cave_crawler")) {
return NPCDrops.cavecrawler;
} else if (npc.equals("thief")) {
return NPCDrops.thief;
} else if (npc.equals("tzhaar-xil") || npc.equals("Tzhaar-Xil")) {
return NPCDrops.tzhaarxil;
} else if (npc.equals("tzhaar-ket") || npc.equals("Tzhaar-Ket")) {
return NPCDrops.tzhaarket;
} else if (npc.equals("tzhaar-hur") || npc.equals("Tzhaar-Hur")) {
return NPCDrops.tzhaarhur;
} else if (npc.equals("tzhaar-mej") || npc.equals("Tzhaar-Mej")) {
return NPCDrops.tzhaarmej;
} else if (npc.equals("tree_spirit")) {
return NPCDrops.treespirit;
} else if (npc.equals("unicorn")) {
return NPCDrops.unicorn;
} else if (npc.equals("evil_chicken")) {
return NPCDrops.evilchicken;
} else if (npc.equals("white_knight")) {
return NPCDrops.whiteknight;
} else if (npc.equals("black_knight")) {
return NPCDrops.blackknight;
} else if (npc.equals("bear")) {
return NPCDrops.bear;
} else if (npc.equals("jogre")) {
return NPCDrops.jogre;
} else if (npc.equals("ogre")) {
return NPCDrops.ogre;
} else if (npc.equals("chaos_druid")) {
return NPCDrops.chaosdruid;
} else if (npc.equals("jailer")) {
return NPCDrops.jailer;
} else if (npc.equals("fire_giant") || npc.equals("Fire_giant")) {
return NPCDrops.firegiant;
} else if (npc.equals("basilisk")) {
return NPCDrops.basilisk;
} else if (npc.equals("baby_blue_dragon") || npc.equals("baby_red_dragon") || npc.equals("baby_dragon")) {
return NPCDrops.babybluedragon;
} else if (npc.equals("red_dragon")) {
return NPCDrops.reddragon;
} else if (npc.equals("elf_warrior")) {
return NPCDrops.elfwarrior;
} else if (npc.equals("dagannoth")) {
return NPCDrops.dagannoth;
} else if (npc.equals("giant_mole")) {
return NPCDrops.giantmole;
} else if (npc.equals("dagannoth_supreme")) {
return NPCDrops.dagannothsupereme;
} else if (npc.equals("chaos_elemental")) {
return NPCDrops.chaoselemental;
} else if (npc.equals("dagannoth_prime")) {
return NPCDrops.dagannothprime;
} else if (npc.equals("dagannoth_rex")) {
return NPCDrops.daggannothrex;
} else if (npc.equals("monkey_guard")) {
return NPCDrops.monkeyguard;
} else if (npc.equals("monk")) {
return NPCDrops.monk;
} else if (npc.equals("abyssal_demon")) {
return NPCDrops.abyssaldemon;
} else if (npc.equals("pyrefiend")) {
return NPCDrops.pyrefiend;
} else if (npc.equals("aberrant_spectre")
|| npc.equals("aberrant_specter")
|| npc.equals("aberant_specter")) {
return NPCDrops.abberantspectre;
} else if (npc.equals("earth_warrior")) {
return NPCDrops.earthwarrior;
} else if (npc.equals("gargoyle")) {
return NPCDrops.gargoyle;
} else if (npc.equals("dust_devil") || npc.equals("dustdevil")) {
return NPCDrops.dustdevil;
} else if (npc.equals("cockatrice")) {
return NPCDrops.cockatrice;
} else if (npc.equals("infernal_mage")) {
return NPCDrops.infernalmage;
} else if (npc.equals("nechryael")) {
return NPCDrops.nechryael;
} else if (npc.equals("bloodveld")) {
return NPCDrops.bloodveld;
} else if (npc.equals("turoth")) {
return NPCDrops.turoth;
} else if (npc.equals("banshee")) {
return NPCDrops.banshee;
} else if (npc.equals("crawling_hand")) {
return NPCDrops.crawlinghand;
} else if (npc.equals("highwayman")) {
return NPCDrops.highwayman;
} else if (npc.equals("wild_dog") || npc.equals("battle_mage")) {
return NPCDrops.alwaysbones;
} else if (npc.equals("kalphite_queen")) {
return NPCDrops.kalphitequeen;
} else if (npc.equals("kalphite_worker")) {
return NPCDrops.kalphiteworker;
} else if (npc.equals("kalphite_soldier")) {
return NPCDrops.kalphitesoldier;
} else if (npc.equals("kalphite_guardian")) {
return NPCDrops.kalphiteguardian;
} else if (npc.equals("bat") || npc.equals("giant_bat")) {
return NPCDrops.bat;
} else if (npc.equals("bronze_dragon")) {
return NPCDrops.bronzedragon;
} else if (npc.equals("black_dragon")) {
return NPCDrops.blackdragon;
} else if (npc.equals("iron_dragon")) {
return NPCDrops.irondragon;
} else if (npc.equals("steel_dragon")) {
return NPCDrops.steeldragon;
} else if (npc.equals("moss_giant")) {
return NPCDrops.mossgiant;
} else if (npc.equals("greater_demon")) {
return NPCDrops.greaterdemon;
} else if (npc.equals("black_demon")) {
return NPCDrops.blackdemon;
} else if (npc.equals("dwarf")) {
return NPCDrops.dwarf;
} else if (npc.equals("jelly")) {
return NPCDrops.jelly;
} else if (npc.equals("rock_crab")) {
return NPCDrops.rockcrab;
} else if (npc.equals("rockslug")) {
return NPCDrops.rockslug;
} else if (npc.equals("king_black_dragon")) {
return NPCDrops.kingblackdragon;
} else if (npc.equals("green_dragon")) {
return NPCDrops.greendragon;
} else if (npc.equals("blue_dragon")) {
return NPCDrops.bluedragon;
} else if (npc.equals("goblin")) {
return NPCDrops.goblin;
} else if (npc.equals("lesser_demon") || npc.equals("Lesser_demon")
|| npc.equals("lesserdemon")) {
return NPCDrops.lesserdemon;
} else if (npc.equals("guard") || npc.equals("jail_guard")) {
return NPCDrops.guard;
} else if (npc.equals("al-kharid_warrior")) {
return NPCDrops.alkharidwarrior;
} else if (npc.equals("ice_warrior")) {
return NPCDrops.icewarrior;
} else if (npc.equals("kurask")) {
return NPCDrops.kurask;
} else if (npc.equals("ice_giant")) {
return NPCDrops.icegiant;
} else if (npc.equals("hobgoblin")) {
return NPCDrops.hobgoblin;
} else if (npc.equals("pirate")) {
return NPCDrops.pirate;
} else if (npc.equals("zombie")) {
return NPCDrops.zombie;
} else if (npc.equals("skeleton")) {
return NPCDrops.skeleton;
} else if (npc.equals("deadly_red_spider")) {
return NPCDrops.deadlyredspider;
} else if (npc.equals("rat")) {
return NPCDrops.rat;
} else if (npc.equals("imp")) {
return NPCDrops.imp;
} else if (npc.equals("cow") || npc.equals("cow_calf")) {
return NPCDrops.cow;
} else if (npc.equals("chicken") || npc.equals("rooster")) {
return NPCDrops.chicken;
} else if (npc.equals("hill_giant")) {
return NPCDrops.hillgiant;
} else if (npc.equals("giant_rat")) {
return NPCDrops.giantrat;
} else if (npc.equals("dark_wizard")) {
return NPCDrops.darkwizard;
} else {
return NPCDrops.DEFAULT;
}
}
public static void loadItemDropData() {
try {
npcDrops = new Gson().fromJson(new FileReader("./data/cfg/npcdrops.json"), NpcDrop[].class);
} catch (FileNotFoundException fileex) {
Misc.println("npcdrops.json: file not found.");
}
}
/**
* Gets the item name
*
* @param ItemID
* @return
*/
public static int i(String ItemName) {
return getItemId(ItemName);
}
/**
* Handles the npc drops for the npc id.
*
* @param npcId id of the npc
*
* @return Items dropped by that npc
*/
public static ItemDrop[] getNpcDrops(String npc, int npcId) {
for (NpcDrop npcDrop : npcDrops) {
if (npcDrop.getId() == npcId) {
return npcDrop.getItems();
}
}
/**
* Item name main method
*
* @param itemName
* @return
*/
public static int getItemId(String itemName) {
for (ItemList i : GameEngine.itemHandler.itemList) {
if (i != null) {
if (i.itemName.equalsIgnoreCase(itemName)) {
return i.itemId;
}
}
}
return -1;
}
return new ItemDrop[]{
new ItemDrop(i("bones"), 1, 0),
new ItemDrop(995, new int[]{ 1, 10 }, 3),
new ItemDrop(2677, 1, 512) };
}
/**
* Misc.random in shorter form
*
* @param random
* @return
*/
public static int r(int random) {
return Misc.random(random);
}
/**
* short version of getItemId
*
* @param itemName name of the item
*
* @return itemId
*/
public static int i(String itemName) {
return getItemId(itemName);
}
/**
* Item id main method
*
* @param itemName name of the item
*
* @return itemId
*/
public static int getItemId(String itemName) {
for (ItemList i : GameEngine.itemHandler.itemList) {
if (i != null) {
if (i.itemName.equalsIgnoreCase(itemName)) {
return i.itemId;
}
}
}
return -1;
}
/**
* Misc.random in shorter form
*
* @param max max number
*
* @return random number from 0->max
*/
public static int r(int max) {
return Misc.random(max);
}
}
@@ -1,249 +1,294 @@
package com.rebotted.game.shops;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.rebotted.game.players.Client;
import com.rebotted.game.players.Player;
import com.rebotted.game.players.PlayerHandler;
import com.rebotted.util.Misc;
import com.rebotted.util.ShopData;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.*;
import java.lang.reflect.Type;
public class ShopHandler {
public static int MAX_SHOPS = 800;
public static int MAX_SHOP_ITEMS = 40;
public static int SHOW_DELAY = 1; // Restock 1 item every tick
public static int SPECIAL_DELAY = 60; // Remove overstocked items after 60 ticks
public static int totalshops = 0;
public static int[][] shopItems = new int[MAX_SHOPS][MAX_SHOP_ITEMS];
public static int[][] shopItemsN = new int[MAX_SHOPS][MAX_SHOP_ITEMS];
public static int[][] shopItemsDelay = new int[MAX_SHOPS][MAX_SHOP_ITEMS];
public static int[][] shopItemsSN = new int[MAX_SHOPS][MAX_SHOP_ITEMS];
public static int[] shopItemsStandard = new int[MAX_SHOPS];
public static String[] shopName = new String[MAX_SHOPS];
public static int[] shopSModifier = new int[MAX_SHOPS];
public static int[] shopBModifier = new int[MAX_SHOPS];
public static long[][] shopItemsRestock = new long[MAX_SHOPS][MAX_SHOP_ITEMS];
public static int MAX_SHOPS = 800;
public ShopHandler() {
for (int i = 0; i < MAX_SHOPS; i++) {
for (int j = 0; j < MAX_SHOP_ITEMS; j++) {
ResetItem(i, j);
shopItemsSN[i][j] = 0;
public static int MAX_SHOP_ITEMS = 40;
public static int SHOW_DELAY = 1; // Restock 1 item every tick
public static int SPECIAL_DELAY = 60; // Remove overstocked items after 60 ticks
public static int totalshops = 0;
public static int[][] shopItems = new int[MAX_SHOPS][MAX_SHOP_ITEMS];
public static int[][] shopItemsN = new int[MAX_SHOPS][MAX_SHOP_ITEMS];
public static int[][] shopItemsDelay = new int[MAX_SHOPS][MAX_SHOP_ITEMS];
public static int[][] shopItemsSN = new int[MAX_SHOPS][MAX_SHOP_ITEMS];
public static int[] shopItemsStandard = new int[MAX_SHOPS];
public static String[] shopName = new String[MAX_SHOPS];
public static int[] shopSModifier = new int[MAX_SHOPS];
public static int[] shopBModifier = new int[MAX_SHOPS];
public static long[][] shopItemsRestock = new long[MAX_SHOPS][MAX_SHOP_ITEMS];
public ShopHandler() {
for (int i = 0; i < MAX_SHOPS; i++) {
for (int j = 0; j < MAX_SHOP_ITEMS; j++) {
ResetItem(i, j);
shopItemsSN[i][j] = 0;
}
shopItemsStandard[i] = 0;
shopSModifier[i] = 0;
shopBModifier[i] = 0;
shopName[i] = "";
}
totalshops = 0;
//writeShops("shops.cfg");
loadShops();
}
public static int restockTimeItem(int itemId) {
switch (itemId) {
default:
return 1000;
}
}
public void process() {
boolean DidUpdate = false;
for (int i = 1; i <= totalshops; i++) {
if (shopBModifier[i] == 0 || shopSModifier[i] == 0) {
continue;
}
shopItemsStandard[i] = 0;
shopSModifier[i] = 0;
shopBModifier[i] = 0;
shopName[i] = "";
}
totalshops = 0;
loadshops("shops.cfg");
}
public static int restockTimeItem(int itemId) {
switch(itemId) {
default:
return 1000;
}
for (int j = 0; j < MAX_SHOP_ITEMS; j++) {
if (shopItems[i][j] > 0) {
if (shopItemsDelay[i][j] >= SHOW_DELAY) {
if (j <= shopItemsStandard[i] && shopItemsN[i][j] <= shopItemsSN[i][j]) {
if (shopItemsN[i][j] < shopItemsSN[i][j] && System.currentTimeMillis() - shopItemsRestock[i][j] > restockTimeItem(shopItems[i][j])) {
shopItemsN[i][j] += 1;
shopItemsDelay[i][j] = 1;
shopItemsDelay[i][j] = 0;
DidUpdate = true;
shopItemsRestock[i][j] = System.currentTimeMillis();
}
} else if (shopItemsDelay[i][j] >= SPECIAL_DELAY) {
DiscountItem(i, j);
shopItemsDelay[i][j] = 0;
DidUpdate = true;
}
refreshshop(i);
}
shopItemsDelay[i][j]++;
}
}
if (DidUpdate) {
for (int k = 1; k < PlayerHandler.players.length; k++) {
if (PlayerHandler.players[k] != null) {
if (PlayerHandler.players[k].isShopping && PlayerHandler.players[k].shopId == i) {
PlayerHandler.players[k].updateShop = true;
PlayerHandler.players[k].updateShop(i);
}
}
}
DidUpdate = false;
}
}
}
}
public void process() {
boolean DidUpdate = false;
for (int i = 1; i <= totalshops; i++) {
if (shopBModifier[i] == 0 || shopSModifier[i] == 0) continue;
for (int j = 0; j < MAX_SHOP_ITEMS; j++) {
if (shopItems[i][j] > 0) {
if (shopItemsDelay[i][j] >= SHOW_DELAY) {
if (j <= shopItemsStandard[i] && shopItemsN[i][j] <= shopItemsSN[i][j]) {
if (shopItemsN[i][j] < shopItemsSN[i][j] && System.currentTimeMillis() - shopItemsRestock[i][j] > restockTimeItem(shopItems[i][j])) {
shopItemsN[i][j] += 1;
shopItemsDelay[i][j] = 1;
shopItemsDelay[i][j] = 0;
DidUpdate = true;
shopItemsRestock[i][j] = System.currentTimeMillis();
}
} else if (shopItemsDelay[i][j] >= SPECIAL_DELAY) {
DiscountItem(i, j);
shopItemsDelay[i][j] = 0;
DidUpdate = true;
}
refreshshop(i);
}
shopItemsDelay[i][j]++;
}
}
if (DidUpdate) {
for (int k = 1; k < PlayerHandler.players.length; k++) {
if (PlayerHandler.players[k] != null) {
if (PlayerHandler.players[k].isShopping && PlayerHandler.players[k].shopId == i) {
PlayerHandler.players[k].updateShop = true;
PlayerHandler.players[k].updateShop(i);
}
}
}
DidUpdate = false;
}
}
}
private void DiscountItem(int shopID, int ArrayID) {
shopItemsN[shopID][ArrayID] -= 1;
if (shopItemsN[shopID][ArrayID] <= 0) {
shopItemsN[shopID][ArrayID] = 0;
if (shopItemsStandard[shopID] <= ArrayID)
private void DiscountItem(int shopID, int ArrayID) {
shopItemsN[shopID][ArrayID] -= 1;
if (shopItemsN[shopID][ArrayID] <= 0) {
shopItemsN[shopID][ArrayID] = 0;
if (shopItemsStandard[shopID] <= ArrayID) {
ResetItem(shopID, ArrayID);
}
}
private static void ResetItem(int shopID, int ArrayID) {
if (shopItemsStandard[shopID] > ArrayID) return;
shopItems[shopID][ArrayID] = 0;
shopItemsN[shopID][ArrayID] = 0;
shopItemsDelay[shopID][ArrayID] = 0;
}
public boolean loadshops(String FileName) {
String line = "";
String token = "";
String token2 = "";
String[] token3 = new String[(MAX_SHOP_ITEMS * 2)];
boolean EndOfFile = false;
BufferedReader characterfile = null;
try {
characterfile = new BufferedReader(new FileReader("./data/cfg/" + FileName));
} catch (FileNotFoundException fileex) {
Misc.println(FileName + ": file not found.");
return false;
}
try {
line = characterfile.readLine();
} catch (IOException ioexception) {
Misc.println(FileName + ": error loading file.");
}
while (EndOfFile == false && line != null) {
line = line.trim();
int spot = line.indexOf("=");
if (spot > -1) {
token = line.substring(0, spot);
token = token.trim();
token2 = line.substring(spot + 1);
token3 = token2.trim().split("\t+");
if (token.equals("shop")) {
int shopID = Integer.parseInt(token3[0]);
shopName[shopID] = token3[1].replaceAll("_", " ");
shopSModifier[shopID] = Integer.parseInt(token3[2]);
shopBModifier[shopID] = Integer.parseInt(token3[3]);
for (int i = 0; i < ((token3.length - 4) / 2); i++) {
int itemID = Integer.parseInt(token3[(4 + (i * 2))]);
int itemAmount = Integer.parseInt(token3[(5 + (i * 2))]);
if (itemID > 0) {
shopItems[shopID][i] = itemID + 1;
shopItemsN[shopID][i] = itemAmount;
shopItemsSN[shopID][i] = itemAmount;
shopItemsStandard[shopID]++;
} else {
break;
}
}
totalshops++;
}
} else {
if (line.equalsIgnoreCase("[ENDOFSHOPLIST]")) {
try {
characterfile.close();
} catch (IOException ioexception) {
}
}
}
try {
line = characterfile.readLine();
} catch (IOException ioexception1) {
EndOfFile = true;
}
}
private static void ResetItem(int shopID, int ArrayID) {
if (shopItemsStandard[shopID] > ArrayID) {
return;
}
shopItems[shopID][ArrayID] = 0;
shopItemsN[shopID][ArrayID] = 0;
shopItemsDelay[shopID][ArrayID] = 0;
}
public void loadShops() {
Gson gson = new Gson();
try {
Type collectionType = new TypeToken<ShopData[]>() {
}.getType();
ShopData[] data = gson.fromJson(new FileReader("./data/cfg/shops.json"), collectionType);
for (ShopData shop : data) {
int shopID = shop.getId();
shopName[shopID] = shop.getName();
shopSModifier[shopID] = shop.getSellModifier();
shopBModifier[shopID] = shop.getBuyModifier();
for (int i = 0; i < shop.getItems().length; i++) {
if (shop.getItems()[i].getItemId() > 0) {
shopItems[shopID][i] = shop.getItems()[i].getItemId() + 1;
shopItemsN[shopID][i] = shop.getItems()[i].getItemAmount();
shopItemsSN[shopID][i] = shop.getItems()[i].getItemAmount();
shopItemsStandard[shopID]++;
} else {
break;
}
}
totalshops++;
}
} catch (FileNotFoundException fileex) {
Misc.println("shops.json: file not found.");
}
}
public boolean writeShops(String FileName) {
String line = "";
String token = "";
String token2 = "";
String[] token3 = new String[(MAX_SHOP_ITEMS * 2)];
boolean EndOfFile = false;
BufferedReader characterfile = null;
JSONArray array = new JSONArray();
try {
characterfile = new BufferedReader(new FileReader("./data/cfg/" + FileName));
} catch (FileNotFoundException fileex) {
Misc.println(FileName + ": file not found.");
return false;
}
try {
line = characterfile.readLine();
} catch (IOException ioexception) {
Misc.println(FileName + ": error loading file.");
}
while (EndOfFile == false && line != null) {
line = line.trim();
int spot = line.indexOf("=");
if (spot > -1) {
token = line.substring(0, spot);
token = token.trim();
token2 = line.substring(spot + 1);
token3 = token2.trim().split("\t+");
if (token.equals("shop")) {
JSONObject object = new JSONObject();
object.put("id", Integer.parseInt(token3[0]));
object.put("name", token3[1].replace("_", " "));
object.put("sellModifier", Integer.parseInt(token3[2]));
object.put("buyModifier", Integer.parseInt(token3[3]));
JSONArray array1 = new JSONArray();
for (int i = 0; i < ((token3.length - 4) / 2); i++) {
JSONObject object1 = new JSONObject();
object1.put("itemId", Integer.parseInt(token3[(4 + (i * 2))]));
object1.put("itemAmount", Integer.parseInt(token3[(5 + (i * 2))]));
array1.put(array1.length(), object1);
}
object.put("items", array1);
array.put(object);
}
} else {
if (line.equalsIgnoreCase("[ENDOFSHOPLIST]")) {
try {
characterfile.close();
} catch (IOException ioexception) {
}
}
}
try {
line = characterfile.readLine();
} catch (IOException ioexception1) {
EndOfFile = true;
}
}
try {
characterfile.close();
FileWriter fileWriter = new FileWriter("shops-dump.json");
fileWriter.write(array.toString());
} catch (IOException ioexception) {
}
return false;
}
public static void createPlayerShop(Client player) {
int id = getEmptyshop();
player.shopId = id;
shopSModifier[id] = 0;
shopBModifier[id] = 0;
shopName[id] = player.properName + "'s Store";
for (int i = 0; i < MAX_SHOP_ITEMS; i++) {
shopItems[id][i] = player.bankItems[i];
shopItemsN[id][i] = player.bankItemsN[i];
shopItemsSN[id][i] = 0;
shopItemsDelay[id][i] = 0;
}
totalshops++;
}
private static int getEmptyshop() {
for (int i = 0; i < MAX_SHOPS; i++) {
if (shopName[i].equals("")) {
return i;
}
}
try {
characterfile.close();
} catch (IOException ioexception) {
}
return false;
}
}
return -1;
}
public static void createPlayerShop(Client player){
int id = getEmptyshop();
player.shopId = id;
shopSModifier[id] = 0;
shopBModifier[id] = 0;
shopName[id] = player.properName + "'s Store";
for (int i = 0; i < MAX_SHOP_ITEMS; i++){
shopItems[id][i] = player.bankItems[i];
shopItemsN[id][i] = player.bankItemsN[i];
shopItemsSN[id][i] = 0;
shopItemsDelay[id][i] = 0;
}
totalshops++;
}
public static void refreshshop(int shop_id) {
// We don't want to remove items that should be kept in stock
for (int j = shopItemsStandard[shop_id]; j < MAX_SHOP_ITEMS; j++) {
if (shopItemsN[shop_id][j] <= 0) {
ResetItem(shop_id, j);
int next = j + 1;
if (next < MAX_SHOP_ITEMS && shopItemsN[shop_id][next] > 0) {
shopItems[shop_id][j] = shopItems[shop_id][next];
shopItemsN[shop_id][j] = shopItemsN[shop_id][next];
shopItemsDelay[shop_id][j] = shopItemsDelay[shop_id][next];
ResetItem(shop_id, next);
}
}
}
}
private static int getEmptyshop(){
for (int i = 0; i < MAX_SHOPS; i++) {
if (shopName[i].equals("")) return i;
}
return -1;
}
public static int getStock(int shop_id, int item_id) {
item_id++;
for (int j = 0; j < MAX_SHOP_ITEMS; j++) {
if (shopItems[shop_id][j] == item_id) {
return shopItemsN[shop_id][j];
}
}
return -1;
}
public static void refreshshop(int shop_id){
// We don't want to remove items that should be kept in stock
for (int j = shopItemsStandard[shop_id]; j < MAX_SHOP_ITEMS; j++) {
if (shopItemsN[shop_id][j] <= 0) {
ResetItem(shop_id, j);
int next = j + 1;
if (next < MAX_SHOP_ITEMS && shopItemsN[shop_id][next] > 0) {
shopItems[shop_id][j] = shopItems[shop_id][next];
shopItemsN[shop_id][j] = shopItemsN[shop_id][next];
shopItemsDelay[shop_id][j] = shopItemsDelay[shop_id][next];
ResetItem(shop_id, next);
}
}
}
}
public static void buyItem(int shop_id, int item_id, int amount) {
item_id++;
for (int j = 0; j < MAX_SHOP_ITEMS; j++) {
if (shopItems[shop_id][j] == item_id) {
shopItemsN[shop_id][j] -= amount;
}
}
refreshshop(shop_id);
}
public static int getStock(int shop_id, int item_id){
item_id++;
for (int j = 0; j < MAX_SHOP_ITEMS; j++) {
if (shopItems[shop_id][j] == item_id) {
return shopItemsN[shop_id][j];
}
}
return -1;
}
public static void buyItem(int shop_id, int item_id, int amount){
item_id++;
for (int j = 0; j < MAX_SHOP_ITEMS; j++) {
if (shopItems[shop_id][j] == item_id) {
shopItemsN[shop_id][j] -= amount;
}
}
refreshshop(shop_id);
}
public static boolean playerOwnsStore(int shop_id, Player player){
return shopSModifier[shop_id] == 0 && shopBModifier[shop_id] == 0 && shopName[shop_id].equalsIgnoreCase(player.properName + "'s Store");
}
public static boolean playerOwnsStore(int shop_id, Player player) {
return shopSModifier[shop_id] == 0 && shopBModifier[shop_id] == 0 && shopName[shop_id].equalsIgnoreCase(player.properName + "'s Store");
}
}