2013-01-24 22:35:28 +01:00
package com.Acrobot.ChestShop.Listeners.Block ;
import com.Acrobot.Breeze.Utils.BlockUtil ;
2015-05-31 18:27:11 +02:00
import com.Acrobot.Breeze.Utils.StringUtil ;
2013-01-24 22:35:28 +01:00
import com.Acrobot.ChestShop.ChestShop ;
import com.Acrobot.ChestShop.Events.PreShopCreationEvent ;
import com.Acrobot.ChestShop.Events.ShopCreatedEvent ;
2023-06-13 01:22:30 +02:00
import com.Acrobot.ChestShop.Listeners.Block.Break.SignBreak ;
2013-01-24 22:35:28 +01:00
import com.Acrobot.ChestShop.Signs.ChestShopSign ;
2019-07-08 18:21:21 +02:00
import com.Acrobot.ChestShop.UUIDs.NameManager ;
2013-01-24 22:35:28 +01:00
import com.Acrobot.ChestShop.Utils.uBlock ;
import org.bukkit.block.Block ;
import org.bukkit.block.Sign ;
import org.bukkit.event.EventHandler ;
import org.bukkit.event.Listener ;
import org.bukkit.event.block.SignChangeEvent ;
2019-07-08 18:21:21 +02:00
import static com.Acrobot.ChestShop.Permission.OTHER_NAME_DESTROY ;
2013-01-24 22:35:28 +01:00
/ * *
* @author Acrobot
* /
public class SignCreate implements Listener {
2018-11-21 00:45:39 +01:00
@EventHandler ( ignoreCancelled = true )
2013-01-24 22:35:28 +01:00
public static void onSignChange ( SignChangeEvent event ) {
Block signBlock = event . getBlock ( ) ;
if ( ! BlockUtil . isSign ( signBlock ) ) {
return ;
}
2019-07-08 18:21:21 +02:00
Sign sign = ( Sign ) signBlock . getState ( ) ;
2022-02-17 16:34:25 +01:00
if ( ChestShopSign . isValid ( event . getLines ( ) ) & & ! NameManager . canUseName ( event . getPlayer ( ) , OTHER_NAME_DESTROY , ChestShopSign . getOwner ( event . getLines ( ) ) ) ) {
2019-07-08 18:21:21 +02:00
event . setCancelled ( true ) ;
sign . update ( ) ;
2022-07-06 01:54:44 +02:00
ChestShop . logDebug ( " Shop sign creation at " + sign . getLocation ( ) + " by " + event . getPlayer ( ) . getName ( ) + " was cancelled as they weren't able to create a shop for the account ' " + ChestShopSign . getOwner ( event . getLines ( ) ) + " ' " ) ;
2019-07-08 18:21:21 +02:00
return ;
}
String [ ] lines = StringUtil . stripColourCodes ( event . getLines ( ) ) ;
if ( ! ChestShopSign . isValidPreparedSign ( lines ) ) {
2023-06-13 01:22:30 +02:00
// Check if a valid shop already existed previously
if ( ChestShopSign . isValid ( sign ) ) {
SignBreak . sendShopDestroyedEvent ( sign , event . getPlayer ( ) ) ;
}
2013-01-24 22:35:28 +01:00
return ;
}
2019-07-08 18:21:21 +02:00
PreShopCreationEvent preEvent = new PreShopCreationEvent ( event . getPlayer ( ) , sign , lines ) ;
2013-01-24 22:35:28 +01:00
ChestShop . callEvent ( preEvent ) ;
2018-05-07 16:32:51 +02:00
2020-05-14 00:13:56 +02:00
if ( preEvent . getOutcome ( ) . shouldBreakSign ( ) ) {
2021-04-16 22:03:37 +02:00
event . setCancelled ( true ) ;
2020-05-04 23:33:34 +02:00
signBlock . breakNaturally ( ) ;
2022-07-06 01:54:44 +02:00
ChestShop . logDebug ( " Shop sign creation at " + sign . getLocation ( ) + " by " + event . getPlayer ( ) . getName ( ) + " was cancelled (creation outcome: " + preEvent . getOutcome ( ) + " ) and the sign broken " ) ;
2020-05-04 23:33:34 +02:00
return ;
}
2020-05-06 21:17:50 +02:00
for ( byte i = 0 ; i < preEvent . getSignLines ( ) . length & & i < 4 ; + + i ) {
2013-01-24 22:35:28 +01:00
event . setLine ( i , preEvent . getSignLine ( i ) ) ;
}
2018-05-07 16:32:51 +02:00
2017-11-20 13:44:59 +01:00
if ( preEvent . isCancelled ( ) ) {
2022-07-06 01:54:44 +02:00
ChestShop . logDebug ( " Shop sign creation at " + sign . getLocation ( ) + " by " + event . getPlayer ( ) . getName ( ) + " was cancelled (creation outcome: " + preEvent . getOutcome ( ) + " ) and sign lines were set to " + String . join ( " , " , preEvent . getSignLines ( ) ) ) ;
2017-11-20 13:44:59 +01:00
return ;
}
2013-01-24 22:35:28 +01:00
2019-08-22 15:03:27 +02:00
ShopCreatedEvent postEvent = new ShopCreatedEvent ( preEvent . getPlayer ( ) , preEvent . getSign ( ) , uBlock . findConnectedContainer ( preEvent . getSign ( ) ) , preEvent . getSignLines ( ) , preEvent . getOwnerAccount ( ) ) ;
2013-01-24 22:35:28 +01:00
ChestShop . callEvent ( postEvent ) ;
}
}