Allows the server to have over 1000 perfectly working doors.
I have now converted the code into what most people use (PI)
This will make all single doors on your server work to get DoubleDoors working
do this snippet afterwards - DoubleDoors
Video of this:
http://www.youtube.com/watch?v=O4CjQRLIlKE
First you need the door file (5kb) contains all single doors
doors.rar
This goes into your Data folder make sure you the doors.txt file in this and not the doors.rar
More doors can also be added into this if you follow the format correctly (some doors never are opened like rfd and so on, so this leaves an option to add more in a super easy format)
-id-x-y-f-z-t
Key:
- = space
id = the door Id
x = the doors x co ordinate
y = the doors y co ordinate
f = the doors current facing direction 0 = westside, 1 = northside, 2 = east side, 3 = southside
z = the doors height level
t = the doors type (normal doors are type 0 and some diagonal doors are type 9)
Spaces must be in front of every number, It will work out how to handle the door
if you added it correctly.
Then you’ll need the door class, it should be placed so its like src/server/model/players/objects/Doors.java
package server.model.players.objects;
import java.util.ArrayList;
import java.util.List;
import java.io.*;
import java.util.Scanner;
import server.Server;
import server.world.ObjectHandler;
/**
*
* @author Killamess
* Basic door manipulation
*
*/
public class Doors {
private static Doors singleton = null;
private List<Doors> doors = new ArrayList<Doors>();
private File doorFile;
public static Doors getSingleton() {
if (singleton == null) {
singleton = new Doors("./Data/doors.txt");
}
return singleton;
}
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 boolean handleDoor(int id, int x, int y, int z) {
Doors d = getDoor(id, x, y, z);
if (d == null) {
return false;
}
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);
Server.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);
Server.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;
}
}
Server.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;
}
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,
};
}
Then in your ClickObject packet you will see something like this
case FIRST_CLICK:
c.objectX = c.getInStream().readSignedWordBigEndianA();
c.objectId = c.getInStream().readUnsignedWord();
c.objectY = c.getInStream().readUnsignedWordA();
under that add this
if(c.goodDistance(c.getX(), c.getY(), c.objectX, c.objectY, 1)) {
if (Doors.getSingleton().handleDoor(c.objectId, c.objectX, c.objectY, c.heightLevel)) {
return;
}
}
import Doors, this goes with the rest of the imports in ClickObject.
import server.model.players.objects.Doors;
Also in your objectHandler at the placeObject method you’ll need to add this above the other code (inside the placeObject method).
[CODE] ArrayList toremove = new ArrayList();
for (Objects s : globalObjects) {
if (s.getObjectX() == o.getObjectX() && s.getObjectY() == o.getObjectY()) {
toremove.add(s);
}
}
for (Objects s : toremove) {
if (globalObjects.contains(s)) {
globalObjects.remove(s);
}
}
globalObjects.add(o);
[/CODE]
This will stop the server making lots of objects and instead it will search for already existing objects, delete them then add your object.
Also you need to add this to your Server.java file find the main method and in it, it will have something like this
EventManager.initialize();
under ^^^ that add this
Doors.getSingleton().load();
Also you will need to import Doors, this goes at the top of your Server.java file with the other imports.
import server.model.players.objects.Doors;
If its loaded right it should say this when you start up the server
Gates will not work with this.