11package world .bentobox .warps .listeners ;
22
3- import java .util .*;
3+ import java .util .HashMap ;
4+ import java .util .Iterator ;
5+ import java .util .Map ;
6+ import java .util .Objects ;
7+ import java .util .Optional ;
8+ import java .util .UUID ;
49import java .util .stream .Collectors ;
510
611import org .bukkit .Bukkit ;
1015import org .bukkit .World ;
1116import org .bukkit .block .Block ;
1217import org .bukkit .block .Sign ;
18+ import org .bukkit .block .sign .Side ;
19+ import org .bukkit .block .sign .SignSide ;
1320import org .bukkit .entity .Player ;
1421import org .bukkit .event .EventHandler ;
1522import org .bukkit .event .EventPriority ;
1825import org .bukkit .event .block .SignChangeEvent ;
1926import org .bukkit .event .world .ChunkLoadEvent ;
2027import org .bukkit .scheduler .BukkitRunnable ;
21- import org .eclipse .jdt .annotation .Nullable ;
2228
2329import world .bentobox .bentobox .BentoBox ;
2430import world .bentobox .bentobox .api .events .addon .AddonEvent ;
2834import world .bentobox .bentobox .api .user .User ;
2935import world .bentobox .bentobox .database .objects .Island ;
3036import world .bentobox .bentobox .util .Util ;
31- import world .bentobox .warps .objects .PlayerWarp ;
3237import world .bentobox .warps .Warp ;
3338import world .bentobox .warps .event .WarpRemoveEvent ;
39+ import world .bentobox .warps .objects .PlayerWarp ;
3440
3541/**
3642 * Handles warping. Players can add one sign
@@ -106,7 +112,6 @@ public void onSignBreak(BlockBreakEvent e) {
106112 Block b = e .getBlock ();
107113 boolean inWorld = addon .getPlugin ().getIWM ().inWorld (b .getWorld ());
108114 // Signs only
109- // FIXME: When we drop support for 1.13, switch to Tag.SIGNS
110115 if (!b .getType ().name ().contains ("SIGN" )
111116 || (inWorld && !addon .inRegisteredWorld (b .getWorld ()))
112117 || (!inWorld && !addon .getSettings ().isAllowInOtherWorlds ())
@@ -125,6 +130,13 @@ public void onSignBreak(BlockBreakEvent e) {
125130 }
126131 }
127132
133+ /**
134+ * Check if this block is a registered warp sign owned by player so that it can be acted on
135+ * @param player - player trying to do something to the sign
136+ * @param b - sign block
137+ * @param inWorld - true if this is a BentoBox game world
138+ * @return true if this player is op, has mod bypass permission, or is the sign owner
139+ */
128140 private boolean isPlayersSign (Player player , Block b , boolean inWorld ) {
129141 // Welcome sign detected - check to see if it is this player's sign
130142 Map <UUID , PlayerWarp > list = addon .getWarpSignsManager ().getWarpMap (b .getWorld ());
@@ -133,10 +145,19 @@ private boolean isPlayersSign(Player player, Block b, boolean inWorld) {
133145 || player .isOp () || player .hasPermission (reqPerm ));
134146 }
135147
148+ /**
149+ * Checks if this block is a warp sign. Requires it to have the correct title and be registered as a warp sign
150+ * @param b warp sign block
151+ * @return true if it is a valid warp sign
152+ */
136153 private boolean isWarpSign (Block b ) {
137- Sign s = (Sign ) b .getState ();
138- return s .getLine (0 ).equalsIgnoreCase (ChatColor .GREEN + addon .getSettings ().getWelcomeLine ())
139- && addon .getWarpSignsManager ().getWarpMap (b .getWorld ()).values ().stream ().anyMatch (playerWarp -> playerWarp .getLocation ().equals (s .getLocation ()));
154+ if (b .getState () instanceof Sign s ) {
155+ SignSide side = s .getSide (Side .FRONT );
156+ return side .getLine (0 ).equalsIgnoreCase (ChatColor .GREEN + addon .getSettings ().getWelcomeLine ())
157+ && addon .getWarpSignsManager ().getWarpMap (b .getWorld ()).values ().stream ()
158+ .anyMatch (playerWarp -> playerWarp .getLocation ().equals (s .getLocation ()));
159+ }
160+ return false ;
140161 }
141162
142163 /**
@@ -146,24 +167,27 @@ private boolean isWarpSign(Block b) {
146167 */
147168 @ EventHandler (priority = EventPriority .NORMAL , ignoreCancelled = true )
148169 public void onSignWarpCreate (SignChangeEvent e ) {
170+ User user = Objects .requireNonNull (User .getInstance (e .getPlayer ()));
149171 Block b = e .getBlock ();
172+ Location loc = b .getLocation ();
150173 boolean inWorld = addon .getPlugin ().getIWM ().inWorld (b .getWorld ());
151174 if ((inWorld && !addon .inRegisteredWorld (b .getWorld ())) || (!inWorld && !addon .getSettings ().isAllowInOtherWorlds ()) ) {
152175 return ;
153176 }
154177 String title = e .getLine (0 );
155- User user = Objects .requireNonNull (User .getInstance (e .getPlayer ()));
178+ if (title != null && !title .equalsIgnoreCase (addon .getSettings ().getWelcomeLine ()) && addon .getWarpSignsManager ().isWarpAt (loc )) {
179+ UUID owner = addon .getWarpSignsManager ().getWarpOwnerUUID (loc ).orElse (null );
180+ addon .getWarpSignsManager ().removeWarp (loc );
181+ Bukkit .getPluginManager ().callEvent (new WarpRemoveEvent (loc , user .getUniqueId (), owner ));
182+ return ;
183+ }
156184 // Check if someone is changing their own sign
157185 if (title != null && title .equalsIgnoreCase (addon .getSettings ().getWelcomeLine ())) {
158186 // Welcome sign detected - check permissions
159187 if (noPerms (user , b .getWorld (), inWorld )) {
160188 return ;
161189 }
162- // TODO: These checks are useless if the sign is placed outside a BSB world.
163- // This will mean level and rank requirements are nil in the case of allow-in-other-worlds: true.
164- // I'm not sure if there is a better way around this without adding new API checking for primary
165- // or last island accessed with relevant permissions.
166- // ignored.
190+
167191 if (inWorld && noLevelOrIsland (user , b .getWorld ())) {
168192 e .setLine (0 , ChatColor .RED + addon .getSettings ().getWelcomeLine ());
169193 return ;
@@ -182,25 +206,32 @@ public void onSignWarpCreate(SignChangeEvent e) {
182206 // so,
183207 // deactivate it
184208 Block oldSignBlock = oldSignLoc .getBlock ();
185- // FIXME: When we drop support for 1.13, switch to Tag.SIGNS
186- if (oldSignBlock .getType ().name ().contains ("SIGN" )) {
209+ if (oldSignBlock .getState () instanceof Sign oldSign ) {
187210 // The block is still a sign
188- Sign oldSign = (Sign ) oldSignBlock .getState ();
189- if (oldSign .getLine (0 ).equalsIgnoreCase (ChatColor .GREEN + addon .getSettings ().getWelcomeLine ())) {
190- oldSign .setLine (0 , ChatColor .RED + addon .getSettings ().getWelcomeLine ());
211+ SignSide front = oldSign .getSide (Side .FRONT );
212+ SignSide back = oldSign .getSide (Side .BACK );
213+ String welcome = ChatColor .GREEN + addon .getSettings ().getWelcomeLine ();
214+ String disabled = ChatColor .RED + addon .getSettings ().getWelcomeLine ();
215+ boolean remove = false ;
216+ if (front .getLine (0 ).equalsIgnoreCase (welcome )) {
217+ front .setLine (0 , disabled );
218+ remove = true ;
219+ } else if (back .getLine (0 ).equalsIgnoreCase (welcome )) {
220+ back .setLine (0 , disabled );
221+ remove = true ;
222+ }
223+ if (remove ) {
191224 oldSign .update (true , false );
192225 user .sendMessage (WARPS_DEACTIVATE );
193- addon .getWarpSignsManager ().removeWarp (oldSignBlock .getWorld (), user .getUniqueId ());
194- @ Nullable
195226 UUID owner = addon .getWarpSignsManager ().getWarpOwnerUUID (oldSignLoc ).orElse (null );
227+ addon .getWarpSignsManager ().removeWarp (oldSignBlock .getWorld (), user .getUniqueId ());
196228 Bukkit .getPluginManager ().callEvent (new WarpRemoveEvent (oldSign .getLocation (), user .getUniqueId (), owner ));
197229 }
198230 }
199231 // Set up the new warp sign
200232 }
201233 addSign (e , user , b );
202234 }
203-
204235 }
205236
206237 private boolean hasCorrectIslandRank (Block b , User user ) {
0 commit comments