1414import java .util .Collection ;
1515import java .util .List ;
1616import java .util .TreeMap ;
17-
17+ import java . util . regex . Pattern ;
1818import net .kyori .adventure .text .Component ;
1919import net .kyori .adventure .text .serializer .legacy .LegacyComponentSerializer ;
2020import org .apache .commons .text .StringEscapeUtils ;
2121import org .bukkit .Bukkit ;
2222import org .bukkit .Location ;
2323import org .bukkit .Material ;
2424import org .bukkit .Sound ;
25- import org .bukkit .World ;
2625import org .bukkit .block .Block ;
2726import org .bukkit .block .BlockFace ;
27+ import org .bukkit .block .BlockState ;
2828import org .bukkit .block .Sign ;
29- import org .bukkit .block .data .Bisected ;
30- import org .bukkit .block .data .MultipleFacing ;
31- import org .bukkit .block .data .Openable ;
29+ import org .bukkit .block .data .*;
3230import org .bukkit .block .data .type .*;
3331import org .bukkit .block .sign .Side ;
3432import org .bukkit .block .sign .SignSide ;
@@ -56,14 +54,16 @@ public class Conf {
5654public static ChuList <Integer > BL_SPEED ;
5755public static Material DOOR_SET ;
5856public static boolean DEBUG =false ;
57+ static int L_STL , L_ENDL ;
5958
6059//Constants:
61- public static final String MSG_NEW_CONF ="§ e[Elevators] § bCould not load config. Creating new config file..." ,
62- MSG_ERR_CONF ="§ e[Elevators] § cError while loading config!" , MSG_DBG ="§ e[Elevators] § r" ,
63- MSG_ERR_ST ="§ e[Elevators] § eError in § b" , MSG_ERR_MID ="§ e: § c" , MSG_DEL_ST ="§ e[Elevators] § b" ,
64- MSG_DEL_END =" § esaved elevators were deleted because they are invalid!" ,
60+ public static final String MSG_NEW_CONF ="& e[Elevators] & bCould not load config. Creating new config file..." ,
61+ MSG_ERR_CONF ="& e[Elevators] & cError while loading config!" , MSG_DBG ="& e[Elevators] & r" ,
62+ MSG_ERR_ST ="& e[Elevators] & eError in & b" , MSG_ERR_MID ="& e: & c" , MSG_DEL_ST ="& e[Elevators] & b" ,
63+ MSG_DEL_END =" & esaved elevators were deleted because they are invalid!" ,
6564 CONFIG_PATH ="plugins/Elevators/config.yml" ;
6665static final Material AIR = Material .AIR ;
66+ static final Pattern R_CLR = Pattern .compile ("&." );
6767
6868static MemoryConfiguration defaults = new MemoryConfiguration ();
6969static void initDefaults (Main _plugin ) {
@@ -143,7 +143,7 @@ private static void doSaveConf() {
143143}
144144
145145private static String newConf (File file ) {
146- Bukkit . getServer (). getConsoleSender (). sendMessage ( MSG_NEW_CONF );
146+ msg ( null , MSG_NEW_CONF );
147147 try { java .nio .file .Files .createDirectories (file .toPath ().getParent ()); }
148148 catch (IOException e ) {
149149 err ("newConfig" , "IOException while creating directories!" );
@@ -163,15 +163,17 @@ static Object loadConf() { try {
163163 CALL =conf .getString ("call" );
164164 ERROR =conf .getString ("error" );
165165 L_ST =conf .getString ("selStart" );
166+ L_STL =Conf .rc (L_ST ).length ();
166167 L_END =conf .getString ("selEnd" );
168+ L_ENDL =Conf .rc (L_END ).length ();
167169 NODOOR =conf .getString ("noDoor" );
168170
169- MSG_GOTO_ST =c ( conf .getString ("msgGotoStart" ) );
170- MSG_GOTO_END =c ( conf .getString ("msgGotoEnd" ) );
171- MSG_CALL =c ( conf .getString ("msgCall" ) );
172- MSG_NOT_FOUND =c ( conf .getString ("msgNotFound" ) );
173- MSG_PERM =c ( conf .getString ("msgPerm" ) );
174- MSG_PERM_END =c ( conf .getString ("msgPermEnd" ) );
171+ MSG_GOTO_ST =conf .getString ("msgGotoStart" );
172+ MSG_GOTO_END =conf .getString ("msgGotoEnd" );
173+ MSG_CALL =conf .getString ("msgCall" );
174+ MSG_NOT_FOUND =conf .getString ("msgNotFound" );
175+ MSG_PERM =conf .getString ("msgPerm" );
176+ MSG_PERM_END =conf .getString ("msgPermEnd" );
175177
176178 NOMV =StringEscapeUtils .unescapeJava (conf .getString ("noMove" ));
177179 M_ATLV =StringEscapeUtils .unescapeJava (conf .getString ("mAtLevel" ));
@@ -212,16 +214,16 @@ static void doConfLoad(CommandSender s) {
212214 if (err =="NOCONF" ) doSaveConf (); //Create New Config.
213215 else if (err instanceof Integer ) { //Loaded Config Successfully.
214216 if ((Integer )err >0 ) {
215- String delMsg =MSG_DEL_ST + err + MSG_DEL_END ;
216- Bukkit . getServer (). getConsoleSender (). sendMessage ( delMsg );
217- if (s instanceof Player ) s . sendMessage ( delMsg );
217+ String delMsg =MSG_DEL_ST + err + MSG_DEL_END ;
218+ msg ( null , delMsg );
219+ if (s instanceof Player ) msg ( s , delMsg );
218220 }
219221 doSaveConf ();
220222 } else if (err !=null ) { //Error While Loading Config.
221- Bukkit . getServer (). getConsoleSender (). sendMessage ( MSG_ERR_CONF + "\n " + err );
222- if (s instanceof Player ) s . sendMessage ( MSG_ERR_CONF );
223+ msg ( null , MSG_ERR_CONF + "\n " + err );
224+ if (s instanceof Player ) msg ( s , MSG_ERR_CONF );
223225 }
224- if (s !=null ) s . sendMessage ( "§ aElevators Plugin Reloaded!" );
226+ if (s !=null ) msg ( s , "& aElevators Plugin Reloaded!" );
225227}
226228
227229//------------------- Useful Functions -------------------
@@ -237,9 +239,8 @@ static String locToString(Location l) {
237239
238240//Open/close doors & gates:
239241static void setDoor (Block b , boolean on ) {
240- if (b .getBlockData () instanceof Openable ) {
241- Openable d = (Openable )b .getBlockData ();
242- if (d instanceof Door && ((Door )d ).getHalf () != Bisected .Half .BOTTOM ) return ;
242+ if (b .getBlockData () instanceof Openable d ) {
243+ if (d instanceof Door && ((Door )d ).getHalf () != Bisected .Half .BOTTOM ) return ;
243244 d .setOpen (on ); b .setBlockData (d ); playDoorSound (b ,on );
244245 }
245246}
@@ -258,7 +259,15 @@ else if(d instanceof TrapDoor) {
258259
259260//Set state of levers
260261static void setPowered (Block b , boolean on ) {
261- if (b .getBlockData () instanceof Switch s ) { s .setPowered (on ); b .setBlockData (s ); }
262+ if (b .getBlockData () instanceof Switch d ) {
263+ d .setPowered (on ); b .setBlockData (d );
264+ b .getState ().update ();
265+ //Force update on attached (a bit hacky)
266+ b =getAttached (b );
267+ BlockState b1 =b .getState (), b2 =b .getState ();
268+ b1 .setType (Material .REDSTONE_BLOCK );
269+ b1 .update (true ); b2 .update (true );
270+ }
262271}
263272
264273//Sign Read/Write
@@ -281,41 +290,42 @@ static void lines(Block b, String[] l) {
281290 s .update ();
282291}
283292
284- //Get block sign is attached to
285- static Block getSignBlock (Block s ) {
286- World w =s .getWorld (); int x =s .getX (), y =s .getY (), z =s .getZ ();
287- BlockFace f =((WallSign )s .getBlockData ()).getFacing ();
288- switch (f ) {
289- case NORTH : return w .getBlockAt (x ,y ,z +1 ); case SOUTH : return w .getBlockAt (x ,y ,z -1 );
290- case WEST : return w .getBlockAt (x +1 ,y ,z ); default : return w .getBlockAt (x -1 ,y ,z );
291- }
293+ //Get attached block for sign/lever/etc
294+ static Block getAttached (Block b ) {
295+ BlockData d = b .getBlockData ();
296+ BlockFace f = ((Directional )d ).getFacing ().getOppositeFace ();
297+ if (d instanceof FaceAttachable a ) f =switch (a .getAttachedFace ()) {
298+ case WALL -> f ;
299+ case FLOOR -> BlockFace .DOWN ;
300+ case CEILING -> BlockFace .UP ;
301+ };
302+ return b .getRelative (f );
292303}
293304
294305//Ensure there is a solid block behind the sign
295- static void addSignBlock (Block s ) { setDoorBlock (Conf .getSignBlock (s ),true ); }
306+ static void addSignBlock (Block s ) { setDoorBlock (Conf .getAttached (s ),true ); }
296307static void setDoorBlock (Block b , boolean on ) {
297308 Material m =b .getType ();
298309 if (on ?!m .isSolid ():m ==Conf .DOOR_SET ) b .setType (on ?Conf .DOOR_SET :Conf .AIR );
299310 if (on && b .getBlockData () instanceof MultipleFacing f ) { //Connect block faces
300- Location l =b .getLocation ();
301- f .setFace (BlockFace .EAST , !l .clone ().add (1 ,0 ,0 ).getBlock ().isPassable ());
302- f .setFace (BlockFace .WEST , !l .clone ().add (-1 ,0 ,0 ).getBlock ().isPassable ());
303- f .setFace (BlockFace .NORTH , !l .clone ().add (0 ,0 ,-1 ).getBlock ().isPassable ());
304- f .setFace (BlockFace .SOUTH , !l .clone ().add (0 ,0 ,1 ).getBlock ().isPassable ());
311+ f .setFace (BlockFace .EAST , !b .getRelative (BlockFace .EAST ).isPassable ());
312+ f .setFace (BlockFace .WEST , !b .getRelative (BlockFace .WEST ).isPassable ());
313+ f .setFace (BlockFace .NORTH , !b .getRelative (BlockFace .NORTH ).isPassable ());
314+ f .setFace (BlockFace .SOUTH , !b .getRelative (BlockFace .SOUTH ).isPassable ());
305315 b .setBlockData (f );
306316 }
307317}
308318
309319//Determine if sign or call sign was clicked on:
310320static boolean isElevSign (Block b , ConfData ref , Player p ) {
311321 if (!hasPerm (p ,Main .PERM_USE ,true )) return false ; if (b .getBlockData () instanceof WallSign && TITLE .equals (line (b ,0 ))) {
312- ref .data =Elevator .fromElevSign (b ); if (ref .data ==null ) p . sendMessage ( MSG_NOT_FOUND ); return (ref .data !=null );
322+ ref .data =Elevator .fromElevSign (b ); if (ref .data ==null ) msg ( p , MSG_NOT_FOUND ); return (ref .data !=null );
313323 } return false ;
314324}
315325
316326static boolean isCallSign (Block b , ConfData ref , Player p ) {
317327 if (!hasPerm (p ,Main .PERM_USE ,true )) return false ; if (b .getBlockData () instanceof WallSign && CALL .equals (line (b ,0 ))) {
318- ref .data =Elevator .fromCallSign (b ); if (ref .data ==null ) p . sendMessage ( MSG_NOT_FOUND ); return (ref .data !=null );
328+ ref .data =Elevator .fromCallSign (b ); if (ref .data ==null ) msg ( p , MSG_NOT_FOUND ); return (ref .data !=null );
319329 } return false ;
320330}
321331
@@ -332,7 +342,8 @@ static int findFirstEmpty(ChuList<ChuList<FallingBlock>> list) {
332342
333343static boolean hasPerm (Player p , String perm , boolean m ) {
334344 boolean h =p .hasPermission (perm );
335- if (m && !h ) p .sendMessage (MSG_PERM +perm +MSG_PERM_END ); return h ;
345+ if (m && !h ) msg (p , MSG_PERM +perm +MSG_PERM_END );
346+ return h ;
336347}
337348
338349static String unpackFile (String intPath , File dest ) {
@@ -347,21 +358,23 @@ static String unpackFile(String intPath, File dest) {
347358 return s ;
348359}
349360
350- static String cs (Component c ) { return LegacyComponentSerializer .legacyAmpersand ().serialize (c ); }
351- static Component sc (String s ) { return LegacyComponentSerializer .legacyAmpersand ().deserialize (s ); }
352- static String c (String s ) { return LegacyComponentSerializer .legacySection ().serialize (sc (s )); }
361+ static String cs (Component c ) { return LegacyComponentSerializer .legacyAmpersand ().serialize (c ); } //Comp -> Str
362+ static Component sc (String s ) { return LegacyComponentSerializer .legacyAmpersand ().deserialize (s ); } //Str -> Comp
363+ static String rc (String s ) { return R_CLR .matcher (s ).replaceAll ("" ); } //Strip Color
364+
365+ static void msg (CommandSender cm , String s ) { if (cm ==null ) cm =Bukkit .getConsoleSender (); cm .sendMessage (sc (s )); }
353366static void dbg (String str ) {
354367 if (DEBUG ) {
355- String msg =MSG_DBG +str ; Bukkit . getConsoleSender (). sendMessage ( msg );
368+ String m =MSG_DBG +str ; msg ( null , m );
356369 Collection <? extends Player > pl =Bukkit .getOnlinePlayers ();
357- for (Player p : pl ) if (hasPerm (p ,Main .PERM_RELOAD ,false )) p . sendMessage ( msg );
370+ for (Player p : pl ) if (hasPerm (p ,Main .PERM_RELOAD ,false )) msg ( p , m );
358371 }
359372}
360373static void err (String func , String cause ) {
361374 if (DEBUG ) {
362- String msg =MSG_ERR_ST +func +MSG_ERR_MID +cause ; Bukkit . getConsoleSender (). sendMessage ( msg );
375+ String m =MSG_ERR_ST +func +MSG_ERR_MID +cause ; msg ( null , m );
363376 Collection <? extends Player > pl =Bukkit .getOnlinePlayers ();
364- for (Player p : pl ) if (hasPerm (p ,Main .PERM_RELOAD ,false )) p . sendMessage ( msg );
377+ for (Player p : pl ) if (hasPerm (p ,Main .PERM_RELOAD ,false )) msg ( p , m );
365378 }
366379}
367380}
0 commit comments