/[projects]/miscJava/bukkit-minecraft-plugins/HoerupUtils/src/dk/thoerup/bukkit/hoeruputils/secretdoor/Door.java
ViewVC logotype

Contents of /miscJava/bukkit-minecraft-plugins/HoerupUtils/src/dk/thoerup/bukkit/hoeruputils/secretdoor/Door.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1534 - (show annotations) (download)
Mon Jun 27 19:43:33 2011 UTC (12 years, 10 months ago) by torben
File size: 3512 byte(s)
Add guards against overlapping secretdoors
1 package dk.thoerup.bukkit.hoeruputils.secretdoor;
2
3 import java.util.Map;
4
5 import org.bukkit.Location;
6 import org.bukkit.Server;
7 import org.bukkit.World;
8 import org.bukkit.block.Block;
9
10
11 class Door {
12 final static int EAST = 0;
13 final static int WEST = 1;
14 final static int NORTH = 2;
15 final static int SOUTH = 3;
16
17 Location leftUpper;
18
19
20 Location blocks[][]; // [width][height] - [0][0] == leftUpper
21
22 int material;
23
24 int direction;
25
26 int width;
27 int height;
28
29
30 public Door(Block sign, int width, int height, DoorStorage store) throws ConflictingDoorException{
31 leftUpper = sign.getLocation().clone();
32
33 this.width = width;
34 this.height = height;
35
36 blocks = new Location[width][height];
37
38 direction = sign.getData() - 2;
39
40 switch ( sign.getData() ) {
41 case 2: //facing east
42 leftUpper.setZ( leftUpper.getZ() + 1 );
43 break;
44 case 3: //facing west
45 leftUpper.setZ( leftUpper.getZ() - 1 );
46 break;
47 case 4: //facing north
48 leftUpper.setX( leftUpper.getX() + 1);
49 break;
50 case 5: //facing south
51 leftUpper.setX( leftUpper.getX() - 1);
52 break;
53 }
54
55
56
57 material = leftUpper.getBlock().getTypeId();
58
59 loadBlocks();
60
61
62 for (int w=0; w<width; w++) {
63 for (int h=0; h<height; h++) {
64 Door d = store.findDoor( blocks[w][h] );
65 if (d != null) {
66 throw new ConflictingDoorException();
67 }
68 }
69 }
70
71 }
72
73 public Door(String input, Server server) {
74 String parts[] = input.split(":");
75
76 World w = server.getWorld( parts[0] );
77
78 leftUpper = new Location(w, Integer.parseInt(parts[1]), Integer.parseInt(parts[2]), Integer.parseInt(parts[3]) );
79 direction = Integer.parseInt( parts[4] );
80 material = Integer.parseInt( parts[5] );
81
82 width = Integer.parseInt( parts[6] );
83 height = Integer.parseInt( parts[7] );
84
85 loadBlocks();
86 }
87
88 private void loadBlocks() {
89
90 for (int w=0; w<width; w++) {
91 for (int h=0; h<height; h++) {
92 int x = leftUpper.getBlockX();
93 int y = leftUpper.getBlockY();
94 int z = leftUpper.getBlockZ();
95 switch(direction) {
96 case EAST:
97 x -= w;
98 break;
99 case WEST:
100 x += w;
101 break;
102 case NORTH:
103 z += w;
104 break;
105 case SOUTH:
106 z -= w;
107 break;
108 default:
109 System.out.println("[SecretDoor] this should never happen");
110 }
111 y -= h;
112
113 //System.out.println("Add block: " + x + ":" + y + ":" + z);
114
115 blocks[w][h] = new Location( leftUpper.getWorld(), x, y, z);
116
117 }
118 }
119
120 }
121
122 public String toCsv() {
123 StringBuilder sb = new StringBuilder();
124 sb.append( leftUpper.getWorld().getName() );
125
126 sb.append( ":" + leftUpper.getBlockX() );
127 sb.append( ":" + leftUpper.getBlockY() );
128 sb.append( ":" + leftUpper.getBlockZ() );
129
130 sb.append( ":" + direction );
131
132 sb.append( ":" + material );
133
134 sb.append( ":" + width );
135 sb.append( ":" + height );
136
137 return sb.toString();
138 }
139
140
141 public void registerMap(Map<Location,Door> map) {
142 for (int w=0; w<width; w++) {
143 for (int h=0; h<height; h++) {
144 map.put(blocks[w][h], this);
145 }
146 }
147 }
148
149 public void unregisterMap(Map<Location,Door> map) {
150 for (int w=0; w<width; w++) {
151 for (int h=0; h<height; h++) {
152 map.remove( blocks[w][h] );
153 }
154 }
155
156 }
157
158 public void open(World world) {
159 for (int w=0; w<width; w++) {
160 for (int h=0; h<height; h++) {
161 blocks[w][h].getBlock().setTypeId(0);
162 }
163 }
164
165 }
166
167 public void close(World world) {
168 for (int w=0; w<width; w++) {
169 for (int h=0; h<height; h++) {
170 blocks[w][h].getBlock().setTypeId( material );
171 }
172 }
173 }
174
175 }

  ViewVC Help
Powered by ViewVC 1.1.20