/[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 1580 - (show annotations) (download)
Sun Jul 17 19:28:22 2011 UTC (12 years, 10 months ago) by torben
File size: 4226 byte(s)
hmmm maybe it works better now?
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 private Location leftUpper;
18
19
20 private Location blocks[][]; // [width][height] - [0][0] == leftUpper
21
22 private int material;
23
24 private int direction;
25
26 private int width;
27 private int height;
28
29 private String owner;
30
31
32 public Door(Block sign, int width, int height, DoorStorage store, String owner) throws ConflictingDoorException{
33 leftUpper = sign.getLocation().clone();
34
35 this.width = width;
36 this.height = height;
37
38 this.owner = owner;
39
40 blocks = new Location[width][height];
41
42 direction = sign.getData() - 2;
43
44 switch ( sign.getData() ) {
45 case 2: //facing east
46 leftUpper.setZ( leftUpper.getZ() + 1 );
47 break;
48 case 3: //facing west
49 leftUpper.setZ( leftUpper.getZ() - 1 );
50 break;
51 case 4: //facing north
52 leftUpper.setX( leftUpper.getX() + 1);
53 break;
54 case 5: //facing south
55 leftUpper.setX( leftUpper.getX() - 1);
56 break;
57 }
58
59
60
61 material = leftUpper.getBlock().getTypeId();
62
63 loadBlocks();
64
65
66 for (int w=0; w<width; w++) {
67 for (int h=0; h<height; h++) {
68 Door d = store.findDoor( blocks[w][h] );
69 if (d != null) {
70 throw new ConflictingDoorException();
71 }
72 }
73 }
74
75 }
76
77 public Door(String input, Server server) {
78 String parts[] = input.split(":");
79
80 World w = server.getWorld( parts[0] );
81
82 leftUpper = new Location(w, Integer.parseInt(parts[1]), Integer.parseInt(parts[2]), Integer.parseInt(parts[3]) );
83 direction = Integer.parseInt( parts[4] );
84 material = Integer.parseInt( parts[5] );
85
86 width = Integer.parseInt( parts[6] );
87 height = Integer.parseInt( parts[7] );
88 owner = parts[8];
89
90
91 blocks = new Location[width][height];
92
93 loadBlocks();
94 }
95
96 public void powerChange(World world, int oldCurrent, int newCurrent ) {
97 if (isPowered() == true) {
98 open();
99 } else {
100 close();
101 }
102 }
103
104 public boolean isPowered() {
105 for (int w=0; w<width; w++) {
106 for (int h=0; h<height; h++) {
107 Block b = blocks[w][h].getBlock();
108
109 if ( b.isBlockPowered() || b.isBlockIndirectlyPowered() )
110 return true;
111 }
112 }
113
114 return false;
115 }
116
117 private void loadBlocks() {
118
119 for (int w=0; w<width; w++) {
120 for (int h=0; h<height; h++) {
121 int x = leftUpper.getBlockX();
122 int y = leftUpper.getBlockY();
123 int z = leftUpper.getBlockZ();
124 switch(direction) {
125 case EAST:
126 x -= w;
127 break;
128 case WEST:
129 x += w;
130 break;
131 case NORTH:
132 z += w;
133 break;
134 case SOUTH:
135 z -= w;
136 break;
137 default:
138 System.out.println("[SecretDoor] this should never happen");
139 }
140 y -= h;
141
142 //System.out.println("Add block: " + x + ":" + y + ":" + z);
143
144 blocks[w][h] = new Location( leftUpper.getWorld(), x, y, z);
145
146 }
147 }
148
149 }
150
151 public String toCsv() {
152 StringBuilder sb = new StringBuilder();
153 sb.append( leftUpper.getWorld().getName() );
154
155 sb.append( ":" + leftUpper.getBlockX() );
156 sb.append( ":" + leftUpper.getBlockY() );
157 sb.append( ":" + leftUpper.getBlockZ() );
158
159 sb.append( ":" + direction );
160
161 sb.append( ":" + material );
162
163 sb.append( ":" + width );
164 sb.append( ":" + height );
165 sb.append( ":" + owner);
166
167 return sb.toString();
168 }
169
170
171 public void registerMap(Map<Location,Door> map) {
172 for (int w=0; w<width; w++) {
173 for (int h=0; h<height; h++) {
174 map.put(blocks[w][h], this);
175 }
176 }
177 }
178
179 public void unregisterMap(Map<Location,Door> map) {
180 for (int w=0; w<width; w++) {
181 for (int h=0; h<height; h++) {
182 map.remove( blocks[w][h] );
183 }
184 }
185
186 }
187
188 public void open() {
189 for (int w=0; w<width; w++) {
190 for (int h=0; h<height; h++) {
191 blocks[w][h].getBlock().setTypeId(0, false);
192 }
193 }
194
195 }
196
197 public void close() {
198 for (int w=0; w<width; w++) {
199 for (int h=0; h<height; h++) {
200 blocks[w][h].getBlock().setTypeId( material );
201 }
202 }
203 }
204
205
206 /////// setters and getters
207 public String getOwner() {
208 return owner;
209 }
210
211 }

  ViewVC Help
Powered by ViewVC 1.1.20