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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1708 - (show annotations) (download)
Sun Mar 4 20:21:03 2012 UTC (12 years, 2 months ago) by torben
File size: 5150 byte(s)
target release 1.2 and make secretdoor work properly
1 package dk.thoerup.bukkit.hoeruputils.secretdoor;
2
3 import java.util.Map;
4
5 import org.bukkit.Material;
6 import org.bukkit.Location;
7 import org.bukkit.Server;
8 import org.bukkit.World;
9 import org.bukkit.block.Block;
10
11
12 class Door {
13 final static int EAST = 0;
14 final static int WEST = 1;
15 final static int NORTH = 2;
16 final static int SOUTH = 3;
17
18 private Location leftUpper;
19
20
21 private Location blocks[][]; // [width][height] - [0][0] == leftUpper
22 private int material[][];
23 private byte data[][];
24
25 private int direction;
26
27 private int width;
28 private int height;
29
30 private String owner;
31
32 private boolean isPrivate;
33
34
35 public Door(Block sign, int width, int height, DoorStorage store, String owner, boolean isPrivate) throws ConflictingDoorException{
36 leftUpper = sign.getLocation().clone();
37
38 this.width = width;
39 this.height = height;
40
41 this.owner = owner;
42
43 this.isPrivate = isPrivate;
44
45
46 direction = sign.getData() - 2;
47
48 switch ( sign.getData() ) {
49 case 2: //facing east
50 leftUpper.setZ( leftUpper.getZ() + 1 );
51 break;
52 case 3: //facing west
53 leftUpper.setZ( leftUpper.getZ() - 1 );
54 break;
55 case 4: //facing north
56 leftUpper.setX( leftUpper.getX() + 1);
57 break;
58 case 5: //facing south
59 leftUpper.setX( leftUpper.getX() - 1);
60 break;
61 }
62
63
64
65 //material = leftUpper.getBlock().getTypeId();
66
67 loadBlocks();
68
69
70 for (int w=0; w<width; w++) {
71 for (int h=0; h<height; h++) {
72 Door d = store.findDoor( blocks[w][h] );
73 if (d != null) {
74 throw new ConflictingDoorException();
75 }
76 }
77 }
78
79 }
80
81 /*
82 @Deprecated
83 public Door(String input, Server server) {
84 String parts[] = input.split(":");
85
86 World w = server.getWorld( parts[0] );
87
88 leftUpper = new Location(w, Integer.parseInt(parts[1]), Integer.parseInt(parts[2]), Integer.parseInt(parts[3]) );
89 direction = Integer.parseInt( parts[4] );
90 //material = Integer.parseInt( parts[5] );
91
92 width = Integer.parseInt( parts[6] );
93 height = Integer.parseInt( parts[7] );
94 owner = parts[8];
95
96
97 loadBlocks();
98 }*/
99
100 public Door(Location loc, int direction, int width, int height, String owner, boolean isPrivate) {
101 leftUpper = loc;
102 this.direction = direction;
103 this.width = width;
104 this.height = height;
105 this.owner = owner;
106 this.isPrivate = isPrivate;
107
108 loadBlocks();
109 }
110
111
112
113 public void powerChange(World world, int oldCurrent, int newCurrent ) {
114 if (isPowered() == true) {
115 open();
116 } else {
117 close();
118 }
119 }
120
121 public boolean isPowered() {
122 for (int w=0; w<width; w++) {
123 for (int h=0; h<height; h++) {
124 Block b = blocks[w][h].getBlock();
125
126 if ( b.isBlockPowered() || b.isBlockIndirectlyPowered() )
127 return true;
128 }
129 }
130
131 return false;
132 }
133
134 private void loadBlocks() {
135 blocks = new Location[width][height];
136 material = new int[width][height];
137 data = new byte[width][height];
138
139 for (int w=0; w<width; w++) {
140 for (int h=0; h<height; h++) {
141 int x = leftUpper.getBlockX();
142 int y = leftUpper.getBlockY();
143 int z = leftUpper.getBlockZ();
144 switch(direction) {
145 case EAST:
146 x -= w;
147 break;
148 case WEST:
149 x += w;
150 break;
151 case NORTH:
152 z += w;
153 break;
154 case SOUTH:
155 z -= w;
156 break;
157 default:
158 System.out.println("[SecretDoor] this should never happen");
159 }
160 y -= h;
161
162 //System.out.println("Add block: " + x + ":" + y + ":" + z);
163
164 blocks[w][h] = new Location( leftUpper.getWorld(), x, y, z);
165
166 material[w][h] = blocks[w][h].getBlock().getTypeId();
167 data[w][h] = blocks[w][h].getBlock().getData();
168
169 }
170 }
171
172 }
173
174 /* public String toCsv() {
175 StringBuilder sb = new StringBuilder();
176 sb.append( leftUpper.getWorld().getName() );
177
178 sb.append( ":" + leftUpper.getBlockX() );
179 sb.append( ":" + leftUpper.getBlockY() );
180 sb.append( ":" + leftUpper.getBlockZ() );
181
182 sb.append( ":" + direction );
183
184 sb.append( ":" + material[0][0] );
185
186 sb.append( ":" + width );
187 sb.append( ":" + height );
188 sb.append( ":" + owner);
189
190 return sb.toString();
191 }*/
192
193
194 public void registerMap(Map<Location,Door> map) {
195 for (int w=0; w<width; w++) {
196 for (int h=0; h<height; h++) {
197 map.put(blocks[w][h], this);
198 }
199 }
200 }
201
202 public void unregisterMap(Map<Location,Door> map) {
203 for (int w=0; w<width; w++) {
204 for (int h=0; h<height; h++) {
205 map.remove( blocks[w][h] );
206 }
207 }
208
209 }
210
211 public void open() {
212 for (int w=0; w<width; w++) {
213 for (int h=0; h<height; h++) {
214 //blocks[w][h].getBlock().setTypeId( 0, false);
215 blocks[w][h].getBlock().setType( Material.AIR );
216 }
217 }
218
219 }
220
221 public void close() {
222 for (int w=0; w<width; w++) {
223 for (int h=0; h<height; h++) {
224 blocks[w][h].getBlock().setTypeIdAndData( material[w][h], data[w][h], true );
225 }
226 }
227 }
228
229
230 /////// setters and getters
231 public String getOwner() {
232 return owner;
233 }
234
235 public Location getLeftUpper() {
236 return leftUpper;
237 }
238
239 public int getWidth() {
240 return width;
241 }
242
243 public int getHeight() {
244 return height;
245 }
246
247 public int getDirection() {
248 return direction;
249 }
250
251 public boolean isPrivate() {
252 return isPrivate;
253 }
254
255 }

  ViewVC Help
Powered by ViewVC 1.1.20