@@ -82,141 +82,133 @@ public static void saveConf(boolean f) {
8282} public static void saveConf () { saveConf (false ); }
8383
8484private static void doSaveConf () {
85- SVTMR =null ; File f =new File (CONFIG_PATH ); String data = "" ;
85+ SVTMR =null ; File f =new File (CONFIG_PATH ); String data ;
8686
8787 //If Not Found, Create New Config File:
88- if (!f .exists ()) data =newConf (f ); else {
89- try { //Read Current Config File:
90- FileReader r =new FileReader (f ); int p =0 ,l ;
91- while (p < 3000 ) { l =r .read (); if (l <0 ) break ; data += fromCharCode (l ); p ++; }
92- r .close ();
93- } catch (IOException e ) { err ("saveConfig" , "IOException while reading file!" ); return ; }
94- }
88+ if (!f .exists ()) data =newConf (f ); else try { //Read Current Config File:
89+ StringBuilder d =new StringBuilder (); FileReader r =new FileReader (f ); int p =0 ,l ;
90+ while (p < 3000 ) { l =r .read (); if (l <0 ) break ; d .append ((char )l ); p ++; }
91+ r .close (); data =d .toString ();
92+ } catch (IOException e ) { err ("saveConfig" , "IOException while reading file!" ); return ; }
9593
9694 if (data .length () == 0 ) { err ("saveConfig" , "Save data string empty!" ); return ; }
9795
9896 //Separate And Overwrite BLOCKS Section:
99- int bPos = data .lastIndexOf ("blockList:" ); String bStr = data .substring (bPos );
100-
101- int bEnd = 0 , nl = 0 ; while (bEnd < 600 ) {
102- if (nl ==1 && bStr .charAt (bEnd ) != ' ' ) nl = 2 ;
103- if (nl ==2 ) { if (bStr .charAt (bEnd ) == '\n' ) break ; }
104- else nl = (bStr .charAt (bEnd ) == '\n' ) ? 1 : 0 ; bEnd ++;
105- } bEnd += bPos ;
97+ int bPos =data .lastIndexOf ("blockList:" ),bEnd =0 ,nl =0 ;
98+ String bStr =data .substring (bPos );
99+ while (bEnd <600 ) {
100+ if (nl ==1 && bStr .charAt (bEnd )!=' ' ) nl =2 ;
101+ if (nl ==2 ) { if (bStr .charAt (bEnd )=='\n' ) break ; } else nl =(bStr .charAt (bEnd )=='\n' )?1 :0 ;
102+ bEnd ++;
103+ }
104+ bEnd +=bPos ;
106105
107106 YamlConfiguration bConf = new YamlConfiguration (); ConfigurationSection bList = bConf .createSection ("blockList" );
108107 for (int i =0 ,l =BLOCKS .length ; i <l ; i ++) bList .set (BLOCKS .get (i ), BL_SPEED .get (i ));
109108 data = data .substring (0 ,bPos )+bConf .saveToString ()+data .substring (bEnd );
110109
111110 //Separate And Overwrite Elevators Section:
112- int sPos = data .lastIndexOf ("elevators:" ); data = data .substring (0 ,sPos );
113- YamlConfiguration eConf = new YamlConfiguration ();
114-
115- ConfigurationSection eList = eConf .createSection ("elevators" ); Object [] eKeys = elevators .keySet ().toArray ();
116-
117- //Generate Compressed Elevator Data:
118- for (int i =0 ,l =eKeys .length ; i <l ; i ++) eList .set ((String )eKeys [i ], elevators .get (eKeys [i ]).toSaveData ());
111+ YamlConfiguration eConf =new YamlConfiguration ();
112+ ConfigurationSection eList =eConf .createSection ("elevators" );
113+ for (String k : elevators .keySet ()) eList .set (k , elevators .get (k ).toSaveData ()); //Gen Compressed Elev Data
119114
120115 //Append New Data And Save File:
121- data += eConf .saveToString (); Writer file ; try {
122- file = new BufferedWriter (new OutputStreamWriter (new FileOutputStream (f ), StandardCharsets .UTF_8 ));
123- file .write (data ); file .close ();
124- } catch (IOException e ) { err ("saveConfig" , "IOException while saving file!" ); }
116+ data = data .substring (0 , data .lastIndexOf ("elevators:" ))+eConf .saveToString ();
117+ try {
118+ Writer w =new BufferedWriter (new OutputStreamWriter (new FileOutputStream (f ), StandardCharsets .UTF_8 ));
119+ w .write (data ); w .close (); dbg ("Saved " +elevators .size ()+" elevators!" );
120+ } catch (IOException e ) { err ("saveConfig" , "IOException while saving file!" ); }
125121}
126122
127123private static String newConf (File file ) {
128124 Bukkit .getServer ().getConsoleSender ().sendMessage (MSG_NEW_CONF );
129125 try { java .nio .file .Files .createDirectories (file .toPath ().getParent ()); }
130- catch (IOException e ) { err ("newConfig" , "IOException while creating directories!" ); return "" ; }
126+ catch (IOException e ) { err ("newConfig" , "IOException while creating directories!" ); return "" ; }
131127 return unpackFile ("config.yml" , file );
132128}
133129
134130public static Object loadConf () { try {
135131 File f =new File (CONFIG_PATH ); YamlConfiguration conf ; boolean pf =f .exists ();
136132 if (pf ) conf =YamlConfiguration .loadConfiguration (f ); else conf =new YamlConfiguration ();
137-
138- conf .setDefaults (defaults ); movingFloors = new ChuList <>(); CLTMR = null ;
133+ conf .setDefaults (defaults ); movingFloors =new ChuList <>(); CLTMR =null ;
139134
140135 //Load Global Settings:
141- DEBUG = conf .getBoolean ("debug" );
142- TITLE = c (conf .getString ("title" ));
143- CALL = c (conf .getString ("call" ));
144- ERROR = c (conf .getString ("error" ));
145- L_ST = c (conf .getString ("selStart" ));
146- L_END = c (conf .getString ("selEnd" ));
147- NODOOR = c (conf .getString ("noDoor" ));
148-
149- MSG_GOTO_ST = c (conf .getString ("msgGotoStart" ));
150- MSG_GOTO_END = c (conf .getString ("msgGotoEnd" ));
151- MSG_CALL = c (conf .getString ("msgCall" ));
152-
153- MSG_GOTO_ST = c (conf .getString ("msgGotoStart" ));
154- MSG_GOTO_END = c (conf .getString ("msgGotoEnd" ));
155- MSG_CALL = c (conf .getString ("msgCall" ));
156-
157- NOMV = c (StringEscapeUtils .unescapeJava (conf .getString ("noMove" )));
158- M_ATLV = c (StringEscapeUtils .unescapeJava (conf .getString ("mAtLevel" )));
159- ATLV = c (StringEscapeUtils .unescapeJava (conf .getString ("atLevel" )));
160- C_UP = c (StringEscapeUtils .unescapeJava (conf .getString ("callUp" )));
161- UP = c (StringEscapeUtils .unescapeJava (conf .getString ("up" )));
162- C_DOWN = c (StringEscapeUtils .unescapeJava (conf .getString ("callDown" )));
163- DOWN = c (StringEscapeUtils .unescapeJava (conf .getString ("down" )));
164-
165- RADIUS_MAX = conf .getInt ("floorMaxRadius" );
166- MOVE_RES = conf .getInt ("updateDelay" );
167- DOOR_HOLD = conf .getInt ("doorHoldTime" );
168- DOOR_SET = Material .valueOf (conf .getString ("doorBlock" ));
169-
170- ConfigurationSection bList = conf .getConfigurationSection ("blockList" );
171- BLOCKS = new ChuList <>(); BL_SPEED = new ChuList <>();
172- if (bList != null ) {
173- Object [] bKeys = bList .getKeys (false ).toArray ();
174- for (int b =0 ,g =bKeys .length ; b <g ; b ++) { //Remove any items from block list that aren't solid blocks:
175- Material mat = Material .valueOf ((String )bKeys [b ]);
176- if (mat .isSolid ()) { BLOCKS .push ((String )bKeys [b ]); BL_SPEED .push (bList .getInt ((String )bKeys [b ])); }
177- }
136+ DEBUG =conf .getBoolean ("debug" );
137+ TITLE =c (conf .getString ("title" ));
138+ CALL =c (conf .getString ("call" ));
139+ ERROR =c (conf .getString ("error" ));
140+ L_ST =c (conf .getString ("selStart" ));
141+ L_END =c (conf .getString ("selEnd" ));
142+ NODOOR =c (conf .getString ("noDoor" ));
143+
144+ MSG_GOTO_ST =c (conf .getString ("msgGotoStart" ));
145+ MSG_GOTO_END =c (conf .getString ("msgGotoEnd" ));
146+ MSG_CALL =c (conf .getString ("msgCall" ));
147+
148+ MSG_GOTO_ST =c (conf .getString ("msgGotoStart" ));
149+ MSG_GOTO_END =c (conf .getString ("msgGotoEnd" ));
150+ MSG_CALL =c (conf .getString ("msgCall" ));
151+
152+ NOMV =c (StringEscapeUtils .unescapeJava (conf .getString ("noMove" )));
153+ M_ATLV =c (StringEscapeUtils .unescapeJava (conf .getString ("mAtLevel" )));
154+ ATLV =c (StringEscapeUtils .unescapeJava (conf .getString ("atLevel" )));
155+ C_UP =c (StringEscapeUtils .unescapeJava (conf .getString ("callUp" )));
156+ UP =c (StringEscapeUtils .unescapeJava (conf .getString ("up" )));
157+ C_DOWN =c (StringEscapeUtils .unescapeJava (conf .getString ("callDown" )));
158+ DOWN =c (StringEscapeUtils .unescapeJava (conf .getString ("down" )));
159+
160+ RADIUS_MAX =conf .getInt ("floorMaxRadius" );
161+ MOVE_RES =conf .getInt ("updateDelay" );
162+ DOOR_HOLD =conf .getInt ("doorHoldTime" );
163+ DOOR_SET =getMat (conf .getString ("doorBlock" ));
164+
165+ ConfigurationSection bList =conf .getConfigurationSection ("blockList" );
166+ BLOCKS =new ChuList <>(); BL_SPEED =new ChuList <>();
167+ if (bList !=null ) for (String k : bList .getKeys (false )) { //Remove non-sold blocks:
168+ if (getMat (k ).isSolid ()) { BLOCKS .add (k ); BL_SPEED .add (bList .getInt (k )); }
178169 }
179170
180- if (BLOCKS .length < 1 ) { //Load Default Block Settings:
181- BLOCKS = new ChuList <>("IRON_BLOCK" , "GOLD_BLOCK" , "EMERALD_BLOCK" , "DIAMOND_BLOCK" , "LEGACY_STAINED_GLASS" , "GLASS" );
182- BL_SPEED = new ChuList <>(8 , 10 , 12 , 15 , 5 , 4 );
171+ if (BLOCKS .length < 1 ) { //Load Default Block Settings:
172+ BLOCKS = new ChuList <>("IRON_BLOCK" , "GOLD_BLOCK" , "EMERALD_BLOCK" , "DIAMOND_BLOCK" , "LEGACY_STAINED_GLASS" , "GLASS" );
173+ BL_SPEED = new ChuList <>(8 , 10 , 12 , 15 , 5 , 4 );
183174 }
184175
185176 //Load Compressed Elevator Data:
186- elevators .clear (); ConfigurationSection eList = conf .getConfigurationSection ("elevators" ); int eCnt =0 ;
187- if (eList != null ) { Object [] eKeys = eList .getKeys (false ).toArray (); for (int i =0 ,l =eKeys .length ; i <l ; i ++) {
188- Elevator elev = Elevator .fromSaveData (eList .getStringList ((String )eKeys [i ]));
189- if (elev != null ) elevators .put ((String )eKeys [i ], elev ); else { eCnt ++; err ("loadConfig" , "fromSaveData returned null at ID " +eKeys [i ]); }
190- }}
177+ elevators .clear (); ConfigurationSection eList =conf .getConfigurationSection ("elevators" );
178+ int eCnt =0 ; if (eList !=null ) for (String k : eList .getKeys (false )) {
179+ Elevator e =Elevator .fromSaveData (eList .getStringList (k ));
180+ if (e !=null ) elevators .put (k ,e );
181+ else { eCnt ++; err ("loadConfig" , "fromSaveData returned null at ID " +k ); }
182+ }
191183 return pf ?eCnt :"NOCONF" ;
192- } catch (Exception e ) { err ("loadConfig" , "Caught Exception: " + e .getMessage ()); return e .getMessage (); }}
184+ } catch (Exception e ) { err ("loadConfig" , e .getMessage ()); return e .getMessage (); }}
193185
194186public static void doConfLoad (CommandSender s ) {
195- Object err = loadConf ();
196- if (err == "NOCONF" ) doSaveConf (); //Create New Config.
187+ Object err = loadConf ();
188+ if (err == "NOCONF" ) doSaveConf (); //Create New Config.
197189 else if (err instanceof Integer ) { //Loaded Config Successfully.
198- if ((Integer )err > 0 ) {
199- String delMsg = MSG_DEL_ST + err + MSG_DEL_END ;
190+ if ((Integer )err > 0 ) {
191+ String delMsg = MSG_DEL_ST + err + MSG_DEL_END ;
200192 Bukkit .getServer ().getConsoleSender ().sendMessage (delMsg );
201193 if (s instanceof Player ) s .sendMessage (delMsg );
202- } doSaveConf ();
194+ }
195+ doSaveConf ();
203196 } else if (err !=null ) { //Error While Loading Config.
204- Bukkit .getServer ().getConsoleSender ().sendMessage (MSG_ERR_CONF + "\n " + err );
197+ Bukkit .getServer ().getConsoleSender ().sendMessage (MSG_ERR_CONF + "\n " + err );
205198 if (s instanceof Player ) s .sendMessage (MSG_ERR_CONF );
206199 }
207- if (s != null ) s .sendMessage ("§aElevators Plugin Reloaded!" );
208- } public static void doConfLoad () { doConfLoad ( null ); }
200+ if (s != null ) s .sendMessage ("§aElevators Plugin Reloaded!" );
201+ }
209202
210203//------------------- Useful Functions -------------------
211204
212- public static String locToString (Location loc ) {
213- return loc .getWorld ().getName ()+"-" +(int )loc .getX ()+"-" +(int )loc .getZ ();
205+ private static Material getMat (String m ) throws Exception {
206+ try { return Material .valueOf (m ); }
207+ catch (IllegalArgumentException e ) { throw new Exception ("No such material " +m ); }
214208}
215209
216- public static Location locFromString (String str ) {
217- String [] data = str .split ("-" ); World world = plugin .getServer ().getWorld (data [0 ]);
218- if (world ==null ) { err ("locFromString" , "World '" +data [0 ]+"' not found!" ); return null ; }
219- return new Location (world , Integer .parseInt (data [1 ]), 0 , Integer .parseInt (data [2 ]));
210+ public static String locToString (Location l ) {
211+ return l .getWorld ().getName ()+"-" +l .getBlockX ()+"-" +l .getBlockZ ();
220212}
221213
222214//Open/close doors & gates:
@@ -260,7 +252,7 @@ public static void setSign(Block sign, String[] lines) {
260252}
261253
262254public static void setLine (Block sign , int l , String str ) {
263- Sign s = ((Sign )sign .getState ()); s .setLine (l , str ==null ?"" :str ); s .update ();
255+ Sign s = ((Sign )sign .getState ()); s .setLine (l , str ==null ?"" :str ); s .update ();
264256}
265257
266258//Read lines from sign:
@@ -270,8 +262,8 @@ public static String[] lines(Block sign) {
270262
271263//Get block sign is attached to:
272264public static Block getSignBlock (Block s ) {
273- World w = s .getWorld (); int x = s .getX (), y = s .getY (), z = s .getZ ();
274- BlockFace f = ((WallSign )s .getBlockData ()).getFacing ();
265+ World w = s .getWorld (); int x = s .getX (), y = s .getY (), z = s .getZ ();
266+ BlockFace f = ((WallSign )s .getBlockData ()).getFacing ();
275267 switch (f ) {
276268 case NORTH : return w .getBlockAt (x ,y ,z +1 ); case SOUTH : return w .getBlockAt (x ,y ,z -1 );
277269 case WEST : return w .getBlockAt (x +1 ,y ,z ); case EAST : return w .getBlockAt (x -1 ,y ,z );
@@ -281,16 +273,17 @@ public static Block getSignBlock(Block s) {
281273
282274//Ensure there is a solid block behind the sign.
283275public static void addSignBlock (Block s ) {
284- Block b = Conf .getSignBlock (s ); if (! b . getType (). isSolid ()) setDoorBlock (b ,true );
276+ Block b = Conf .getSignBlock (s ); setDoorBlock (b ,true );
285277}
286278public static void setDoorBlock (Block b , boolean on ) {
287- b .setType (on ?Conf .DOOR_SET :Conf .AIR );
279+ Material m =b .getType ();
280+ if (on ?!m .isSolid ():m ==Conf .DOOR_SET ) b .setType (on ?Conf .DOOR_SET :Conf .AIR );
288281 if (on && b .getBlockData () instanceof MultipleFacing ) { //Connect block faces
289282 MultipleFacing f =(MultipleFacing )b .getBlockData (); Location l =b .getLocation ();
290- f .setFace (BlockFace .EAST , !l .clone ().add (1 ,0 ,0 ).getBlock ().isEmpty ());
291- f .setFace (BlockFace .WEST , !l .clone ().add (-1 ,0 ,0 ).getBlock ().isEmpty ());
292- f .setFace (BlockFace .NORTH , !l .clone ().add (0 ,0 ,-1 ).getBlock ().isEmpty ());
293- f .setFace (BlockFace .SOUTH , !l .clone ().add (0 ,0 ,1 ).getBlock ().isEmpty ());
283+ f .setFace (BlockFace .EAST , !l .clone ().add (1 ,0 ,0 ).getBlock ().isPassable ());
284+ f .setFace (BlockFace .WEST , !l .clone ().add (-1 ,0 ,0 ).getBlock ().isPassable ());
285+ f .setFace (BlockFace .NORTH , !l .clone ().add (0 ,0 ,-1 ).getBlock ().isPassable ());
286+ f .setFace (BlockFace .SOUTH , !l .clone ().add (0 ,0 ,1 ).getBlock ().isPassable ());
294287 b .setBlockData (f );
295288 }
296289}
@@ -307,7 +300,7 @@ public static boolean isCallSign(Block b, ConfData ref, Player pl, String perm)
307300}
308301
309302public static boolean isElevPlayer (Player pl , ConfData ref , String perm ) {
310- if (!hasPerm (pl , perm )) return false ; ref .data = Elevator .fromPlayer (pl ); return (ref .data !=null );
303+ if (!hasPerm (pl , perm )) return false ; ref .data = Elevator .fromEntity (pl ); return (ref .data !=null );
311304}
312305
313306//Find first null element in a RaichuList:
@@ -324,12 +317,12 @@ public static boolean hasPerm(Player pl, String perm) {
324317public static String unpackFile (String intPath , File dest ) {
325318 InputStream st = plugin .getResource (intPath );
326319 String s ; try {
327- StringBuilder sb =new StringBuilder (); int p =0 ,r ;
328- while (p < 3000 ) { r =st .read (); if (r <0 ) break ; sb .append (fromCharCode ( r ) ); p ++; }
329- st .close (); s =sb .toString ();
320+ StringBuilder d =new StringBuilder (); int p =0 ,r ;
321+ while (p < 3000 ) { r =st .read (); if (r <0 ) break ; d .append (( char ) r ); p ++; }
322+ st .close (); s =d .toString ();
330323 Writer file = new BufferedWriter (new OutputStreamWriter (new FileOutputStream (dest ), StandardCharsets .UTF_8 ));
331324 file .write (s ); file .close ();
332- } catch (Exception e ) { err ("unpackFile" , "Caught Exception: " + e .getMessage ()); return "" ; }
325+ } catch (Exception e ) { err ("unpackFile" , e .getMessage ()); return "" ; }
333326 return s ;
334327}
335328
@@ -363,11 +356,6 @@ public static String c(String str) {
363356 .getByChar (clr [i ].charAt (0 ))).append (clr [i ].substring (1 ));
364357 return c .toString ();
365358}
366-
367- public static Block getBlockBelowPlayer (Player p , boolean above ) {
368- Location loc = p .getLocation (); int pX = (int )Math .floor (loc .getX ()), pZ = (int )Math .floor (loc .getZ ());
369- double pY = loc .getY (); return p .getWorld ().getBlockAt (pX , (int )(above ?Math .ceil (pY +1.99 ):Math .floor (pY -1 )), pZ );
370- } public static Block getBlockBelowPlayer (Player p ) { return getBlockBelowPlayer (p , false ); }
371359}
372360
373361class ConfData {
0 commit comments