ChestShop-3/src/main/java/com/Acrobot/ChestShop/Listeners/Block/SignCreate.java

75 lines
3.1 KiB
Java

package com.Acrobot.ChestShop.Listeners.Block;
import com.Acrobot.Breeze.Utils.BlockUtil;
import com.Acrobot.Breeze.Utils.StringUtil;
import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Events.PreShopCreationEvent;
import com.Acrobot.ChestShop.Events.ShopCreatedEvent;
import com.Acrobot.ChestShop.Listeners.Block.Break.SignBreak;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import com.Acrobot.ChestShop.UUIDs.NameManager;
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;
import static com.Acrobot.ChestShop.Permission.OTHER_NAME_DESTROY;
/**
* @author Acrobot
*/
public class SignCreate implements Listener {
@EventHandler(ignoreCancelled = true)
public static void onSignChange(SignChangeEvent event) {
Block signBlock = event.getBlock();
if (!BlockUtil.isSign(signBlock)) {
return;
}
Sign sign = (Sign) signBlock.getState();
if (ChestShopSign.isValid(event.getLines()) && !NameManager.canUseName(event.getPlayer(), OTHER_NAME_DESTROY, ChestShopSign.getOwner(event.getLines()))) {
event.setCancelled(true);
sign.update();
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()) + "'");
return;
}
String[] lines = StringUtil.stripColourCodes(event.getLines());
if (!ChestShopSign.isValidPreparedSign(lines)) {
// Check if a valid shop already existed previously
if (ChestShopSign.isValid(sign)) {
SignBreak.sendShopDestroyedEvent(sign, event.getPlayer());
}
return;
}
PreShopCreationEvent preEvent = new PreShopCreationEvent(event.getPlayer(), sign, lines);
ChestShop.callEvent(preEvent);
if (preEvent.getOutcome().shouldBreakSign()) {
event.setCancelled(true);
signBlock.breakNaturally();
ChestShop.logDebug("Shop sign creation at " + sign.getLocation() + " by " + event.getPlayer().getName() + " was cancelled (creation outcome: " + preEvent.getOutcome() + ") and the sign broken");
return;
}
for (byte i = 0; i < preEvent.getSignLines().length && i < 4; ++i) {
event.setLine(i, preEvent.getSignLine(i));
}
if (preEvent.isCancelled()) {
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()));
return;
}
ShopCreatedEvent postEvent = new ShopCreatedEvent(preEvent.getPlayer(), preEvent.getSign(), uBlock.findConnectedContainer(preEvent.getSign()), preEvent.getSignLines(), preEvent.getOwnerAccount());
ChestShop.callEvent(postEvent);
}
}