forked from ChestShop-authors/ChestShop-3
-
Notifications
You must be signed in to change notification settings - Fork 2
/
SetPrice.java
69 lines (59 loc) · 2.27 KB
/
SetPrice.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.Acrobot.ChestShop.Commands;
import static com.Acrobot.ChestShop.Permission.ADMIN;
import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Configuration.Messages;
import com.Acrobot.ChestShop.Permission;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import org.bukkit.block.Block;
import org.bukkit.block.Sign;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.block.SignChangeEvent;
import org.bukkit.util.RayTraceResult;
public class SetPrice implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player player)) {
return false;
}
if (!Permission.has(sender, Permission.SET_PRICE_COMMAND)) {
sender.sendMessage(Messages.ACCESS_DENIED);
return true;
}
RayTraceResult result = player.rayTraceBlocks(8);
Block signBlock = null;
if (result != null) {
signBlock = result.getHitBlock();
}
if (signBlock == null || !ChestShopSign.isChestShop(signBlock)) {
sender.sendMessage(Messages.MUST_LOOK_AT_SHOP_SIGN);
return true;
}
Sign sign = (Sign) signBlock.getState();
if (!ChestShopSign.canAccess(player, sign) && !Permission.has(player, ADMIN)) {
sender.sendMessage(Messages.ACCESS_DENIED);
return true;
}
String newPriceLine = String.join(" ", args);
String[] line = sign.getLines();
line[2] = newPriceLine;
if (!ChestShopSign.isValidPreparedSign(line)) {
sender.sendMessage(Messages.INVALID_PRICE_LINE);
return true;
}
SignChangeEvent event = new SignChangeEvent(signBlock, player, line);
ChestShop.getPlugin().getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) {
sender.sendMessage(Messages.SHOP_UPDATE_FAILED);
return true;
}
line = event.getLines();
for (int i = 0; i < line.length; i++) {
sign.setLine(i, line[i]);
}
sign.update();
return true;
}
}