下面列出了怎么用org.bukkit.block.Sign的API类实例代码及写法,或者点击链接到github查看源代码。
@EventHandler
public void onBlockBreak(BlockBreakEvent event) {
if (event.isCancelled()) {
return;
}
if (event.getBlock().getState() instanceof Sign) {
Location loc = event.getBlock().getLocation();
if (manager.isSignRegistered(loc)) {
if (event.getPlayer().hasPermission(owner.getSignCreationPermission())) {
manager.unregisterSign(loc);
} else {
event.getPlayer().sendMessage(owner.returnTranslate("sign_can_not_been_destroyed"));
event.setCancelled(true);
}
}
}
}
public void sellCancelConfirmation(CommandSender src, Claim claim, Sign sign) {
final Player player = (Player) src;
final GDClaim gdClaim = (GDClaim) claim;
// check sell access
if (gdClaim.allowEdit(player) != null) {
GriefDefenderPlugin.sendMessage(player, MessageCache.getInstance().CLAIM_NOT_YOURS);
return;
}
final Component sellCancelConfirmationText = TextComponent.builder()
.append("\n[")
.append(MessageCache.getInstance().LABEL_CONFIRM.color(TextColor.GREEN))
.append("]\n")
.clickEvent(ClickEvent.runCommand(GDCallbackHolder.getInstance().createCallbackRunCommand(src, createSellCancelConfirmed(src, claim, sign), true)))
.build();
final Component message = TextComponent.builder()
.append(MessageCache.getInstance().ECONOMY_CLAIM_SALE_CANCEL_CONFIRMATION)
.append("\n")
.append(sellCancelConfirmationText)
.build();
GriefDefenderPlugin.sendMessage(src, message);
}
@Override
public DungeonSign createDungeonSign(Sign sign, String[] lines) {
String type = lines[0].substring(1, lines[0].length() - 1);
try {
Class<? extends DungeonSign> clss = plugin.getSignRegistry().get(type.toUpperCase());
if (clss == null) {
return null;
}
Constructor constructor = clss.getConstructor(DungeonsAPI.class, Sign.class, String[].class, InstanceWorld.class);
DungeonSign dSign = (DungeonSign) constructor.newInstance(plugin, sign, lines, this);
signs.put(sign.getBlock(), dSign);
return dSign;
} catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException
| IllegalArgumentException | InvocationTargetException exception) {
MessageUtil.log(plugin, "&4Could not create a dungeon sign of the type \"" + type
+ "\". A dungeon sign implementation needs a constructor with the types (DungeonsAPI, org.bukkit.block.Sign, String[], InstanceWorld).");
return null;
}
}
@Override
public LoopReturn loop(int rowIndex, SignRow.WrappedMetadataSign sign) {
Sign bukkitSign = sign.getBukkitSign();
if (!iterator.hasNext()) {
return LoopReturn.RETURN;
}
Entry<String, Statistic> entry = iterator.next();
Set<Variable> variables = Sets.newHashSet();
entry.getValue().supply(variables, null);
variables.add(new Variable("player", entry.getKey()));
variables.add(new Variable("rank", index + 1));
layoutConfig.getLayout().inflate(bukkitSign, variables);
bukkitSign.update();
sign.setMetadata(METADATA_ID, entry);
++index;
return LoopReturn.DEFAULT;
}
public static void setClaimForSale(Claim claim, Player player, Sign sign, double price) {
if (claim.isWilderness()) {
GriefDefenderPlugin.sendMessage(player, MessageCache.getInstance().ECONOMY_CLAIM_NOT_FOR_SALE);
return;
}
// if not owner of claim, validate perms
if (((GDClaim) claim).allowEdit(player) != null || !player.hasPermission(GDPermissions.COMMAND_CLAIM_INFO_OTHERS)) {
TextAdapter.sendComponent(player, MessageCache.getInstance().CLAIM_NOT_YOURS);
return;
}
final Component message = GriefDefenderPlugin.getInstance().messageData.getMessage(MessageStorage.ECONOMY_CLAIM_SALE_CONFIRMATION,
ImmutableMap.of(
"amount", price));
GriefDefenderPlugin.sendMessage(player, message);
final Component saleConfirmationText = TextComponent.builder("")
.append("\n[")
.append("Confirm", TextColor.GREEN)
.append("]\n")
.clickEvent(ClickEvent.runCommand(GDCallbackHolder.getInstance().createCallbackRunCommand(createSaleConfirmationConsumer(player, claim, sign, price))))
.build();
GriefDefenderPlugin.sendMessage(player, saleConfirmationText);
}
private static Consumer<CommandSender> createSaleConfirmationConsumer(CommandSender src, Claim claim, Sign sign, double price) {
return confirm -> {
claim.getEconomyData().setSalePrice(price);
claim.getEconomyData().setForSale(true);
claim.getEconomyData().setSaleSignPosition(VecHelper.toVector3i(sign.getLocation()));
claim.getData().save();
List<String> lines = new ArrayList<>(4);
lines.add(ChatColor.translateAlternateColorCodes('&', "&7[&bGD&7-&1sell&7]"));
lines.add(ChatColor.translateAlternateColorCodes('&', LegacyComponentSerializer.legacy().serialize(MessageCache.getInstance().ECONOMY_SIGN_SELL_DESCRIPTION)));
lines.add(ChatColor.translateAlternateColorCodes('&', "&4$" + String.format("%.2f", price)));
lines.add(ChatColor.translateAlternateColorCodes('&', LegacyComponentSerializer.legacy().serialize(MessageCache.getInstance().ECONOMY_SIGN_SELL_FOOTER)));
for (int i = 0; i < lines.size(); i++) {
sign.setLine(i, lines.get(i));
}
sign.update();
final Component message = GriefDefenderPlugin.getInstance().messageData.getMessage(MessageStorage.ECONOMY_CLAIM_SALE_CONFIRMED,
ImmutableMap.of("amount", price));
GriefDefenderPlugin.sendMessage(src, message);
};
}
public static void updateSignRentable(Claim claim, Sign sign) {
if (!isRentSign(claim, sign)) {
return;
}
final PaymentType paymentType = claim.getEconomyData().getPaymentType();
List<String> colorLines = new ArrayList<>(4);
colorLines.add(ChatColor.translateAlternateColorCodes('&', "&7[&bGD&7-&1rent&7]"));
colorLines.add(ChatColor.translateAlternateColorCodes('&', LegacyComponentSerializer.legacy().serialize(MessageCache.getInstance().ECONOMY_SIGN_RENT_DESCRIPTION)));
colorLines.add(ChatColor.translateAlternateColorCodes('&', "&4$" + claim.getEconomyData().getRentRate()));
for (int i = 0; i < colorLines.size(); i++) {
sign.setLine(i, colorLines.get(i));
}
sign.update();
}
/**
* Changes the owner of this shop to the given player.
*
* @param owner the new owner
*/
@Override
public void setOwner(@NotNull UUID owner) {
OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(owner);
//Get the sign at first
List<Sign> signs = this.getSigns();
//then setOwner
this.moderator.setOwner(owner);
//then change the sign
for (Sign shopSign : signs) {
shopSign.setLine(0, MsgUtil.getMessageOfflinePlayer("signs.header", offlinePlayer, ownerName(false)));
//Don't forgot update it
shopSign.update(true);
}
//Event
Bukkit.getPluginManager().callEvent(new ShopModeratorChangedEvent(this, this.moderator));
update();
}
@EventHandler
public void onBlockBreak(BlockBreakEvent event) {
if (event.isCancelled()) {
return;
}
if (event.getBlock().getState() instanceof Sign) {
Location loc = event.getBlock().getLocation();
if (manager.isSignRegistered(loc)) {
if (event.getPlayer().hasPermission(owner.getSignCreationPermission())) {
manager.unregisterSign(loc);
} else {
event.getPlayer().sendMessage(owner.returnTranslate("sign_can_not_been_destroyed"));
event.setCancelled(true);
}
}
}
}
@EventHandler
public void onChangeSign(SignChangeEvent event) {
if (event.isCancelled()) {
return;
}
if (event.getBlock().getState() instanceof Sign) {
Location loc = event.getBlock().getLocation();
if (SIGN_PREFIX.contains(event.getLine(0).toLowerCase())) {
if (event.getPlayer().hasPermission(owner.getSignCreationPermission())) {
if (manager.registerSign(loc, event.getLine(1))) {
event.getPlayer().sendMessage(owner.returnTranslate("sign_successfully_created"));
} else {
event.getPlayer().sendMessage(owner.returnTranslate("sign_can_not_been_created"));
event.setCancelled(true);
event.getBlock().breakNaturally();
}
}
}
}
}
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onInteract(PlayerInteractEvent event) {
if (event.getAction() == Action.RIGHT_CLICK_BLOCK && event.hasBlock() && event.getClickedBlock().getState() instanceof Sign) {
Sign sign = (Sign) event.getClickedBlock().getState();
if (sign.getLine(0).equalsIgnoreCase(ChatColor.DARK_BLUE + "[menu]")) {
sign.getLine(1);
ExtendedIconMenu iconMenu = ChestCommands.getFileNameToMenuMap().get(BukkitUtils.addYamlExtension(sign.getLine(1)));
if (iconMenu != null) {
if (event.getPlayer().hasPermission(iconMenu.getPermission())) {
iconMenu.open(event.getPlayer());
} else {
iconMenu.sendNoPermissionMessage(event.getPlayer());
}
} else {
sign.setLine(0, ChatColor.RED + ChatColor.stripColor(sign.getLine(0)));
event.getPlayer().sendMessage(ChestCommands.getLang().menu_not_found);
}
}
}
}
private void placeSignInWorld(AnniSign asign, String[] lore)
{
Location loc = asign.getLocation().toLocation();
Block block = loc.getWorld().getBlockAt(loc);//asign.getLocation().toLocation().getBlock();
if(block.getType() != Material.WALL_SIGN && block.getType() != Material.SIGN_POST)
block.getWorld().getBlockAt(loc).setType(asign.isSignPost() ? Material.SIGN_POST : Material.WALL_SIGN);
Sign sign = (Sign)block.getState();
if(sign != null)
{
for(int x = 0; x < lore.length; x++)
sign.setLine(x, lore[x]);
org.bukkit.material.Sign matSign = new org.bukkit.material.Sign(block.getType());
matSign.setFacingDirection(asign.getFacingDirection());
sign.setData(matSign);
sign.update(true);
}
}
@EventHandler
public void onSignChange(final SignChangeEvent e) {
final Block b = e.getBlock();
if (b.getState() instanceof Sign) {
plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, () -> {
if (b.getState() instanceof Sign) {
Sign sign = (Sign) e.getBlock().getState();
if (sign.getLine(0).equalsIgnoreCase("[atm]")) {
if (!e.getPlayer().hasPermission("tim.atm.place")) {
e.getPlayer().sendMessage(CC("&cYou dont have permissions to build ATM's!"));
sign.setLine(0, "");
} else {
sign.setLine(0, CC("&cATM"));
sign.update();
e.getPlayer().sendMessage(CC("&2ATM created! (You can also write something in the Lines 2-4)"));
}
}
}
}, 10L);
}
}
@EventHandler(priority = EventPriority.MONITOR)
public void onSignChanged(SignChangeEvent e) {
if (e.isCancelled() || e.getPlayer() == null
|| !plugin.getWorldManager().isSkyAssociatedWorld(e.getPlayer().getWorld())
|| !e.getLines()[0].equalsIgnoreCase("[usb]")
|| e.getLines()[1].trim().isEmpty()
|| !e.getPlayer().hasPermission("usb.island.signs.place")
|| !(e.getBlock().getType() == SkyBlockMenu.WALL_SIGN_MATERIAL)
|| !(e.getBlock().getState() instanceof Sign)
) {
return;
}
Sign sign = (Sign) e.getBlock().getState();
if(sign.getBlock().getState().getBlockData() instanceof WallSign) {
WallSign data = (WallSign) sign.getBlock().getState().getBlockData();
BlockFace attached = data.getFacing().getOppositeFace();
Block wallBlock = sign.getBlock().getRelative(attached);
if (isChest(wallBlock)) {
logic.addSign(sign, e.getLines(), (Chest) wallBlock.getState());
}
}
}
public void updateSigns(String fname) {
if (!RedProtect.get().config.configRoot().region_settings.enable_flag_sign) {
return;
}
List<Location> locs = RedProtect.get().config.getSigns(this.getID());
if (locs.size() > 0) {
for (Location loc : locs) {
if (loc.getBlock().getState() instanceof Sign) {
Sign s = (Sign) loc.getBlock().getState();
String[] lines = s.getLines();
if (lines[0].equalsIgnoreCase("[flag]")) {
if (lines[1].equalsIgnoreCase(fname) && this.name.equalsIgnoreCase(ChatColor.stripColor(lines[2]))) {
s.setLine(3, RedProtect.get().lang.get("region.value") + " " + ChatColor.translateAlternateColorCodes('&', RedProtect.get().lang.translBool(getFlagString(fname))));
s.update();
RedProtect.get().config.putSign(this.getID(), loc);
}
} else {
RedProtect.get().config.removeSign(this.getID(), loc);
}
} else {
RedProtect.get().config.removeSign(this.getID(), loc);
}
}
}
}
private void updateAngleSign(Block block) {
Sign sign = (Sign)block.getState();
sign.setLine(0, "YAW");
sign.setLine(1, ""+this.angle);
double a = this.angle;
if (a > 0) {
sign.setLine(2, "-->");
} else if (a < 0){
sign.setLine(2, "<--");
} else {
sign.setLine(2, "");
}
sign.setLine(3, "");
sign.update();
}
public void onPlayerInteract(Block block, Player player) {
DGroup dGroup = (DGroup) plugin.getPlayerGroup(player);
if (dGroup == null) {
MessageUtil.sendMessage(player, DMessage.ERROR_JOIN_GROUP.getMessage());
return;
}
if (!dGroup.getLeader().equals(player)) {
MessageUtil.sendMessage(player, DMessage.ERROR_NOT_LEADER.getMessage());
return;
}
if (dGroup.getGame() != null) {
MessageUtil.sendMessage(player, DMessage.ERROR_LEAVE_GAME.getMessage());
return;
}
Block topBlock = block.getRelative(0, startSign.getY() - block.getY(), 0);
if (!(topBlock.getState() instanceof Sign)) {
return;
}
Sign topSign = (Sign) topBlock.getState();
if (topSign.getLine(0).equals(DMessage.SIGN_GLOBAL_NEW_GAME.getMessage())) {
if (dungeon == null) {
MessageUtil.sendMessage(player, DMessage.ERROR_SIGN_WRONG_FORMAT.getMessage());
return;
}
dGroup.setDungeon(dungeon);
game = new DGame(plugin, dGroup);
update();
} else if (topSign.getLine(0).equals(DMessage.SIGN_GLOBAL_JOIN_GAME.getMessage())) {
game.addGroup(dGroup);
update();
}
}
public static boolean isSellSign(Block block) {
if (!isSign(block)) {
return false;
}
final Sign sign = (Sign) block.getState();
final String header = ChatColor.stripColor(sign.getLine(0));
if (header.equalsIgnoreCase(SELL_SIGN)) {
return true;
}
return false;
}
public static boolean isSellSign(Sign sign) {
if (sign == null) {
return false;
}
final String header = ChatColor.stripColor(sign.getLine(0));
if (header.equalsIgnoreCase(SELL_SIGN)) {
return true;
}
return false;
}
public static boolean isRentSign(Claim claim, Sign sign) {
if (sign == null) {
return false;
}
if (claim.getEconomyData() == null) {
return false;
}
if (claim.getEconomyData().getRentSignPosition() == null) {
return isRentSign(sign);
}
return claim.getEconomyData().getRentSignPosition().equals(VecHelper.toVector3i(sign.getLocation()));
}
public static Sign getSign(World world, Vector3i pos) {
if (pos == null) {
return null;
}
// Don't load chunks to update signs
if (!world.isChunkLoaded(pos.getX() >> 4, pos.getZ() >> 4)) {
return null;
}
return getSign(VecHelper.toLocation(world, pos));
}
/**
* Clears signs
*/
public void update() {
int y = -1 * verticalSigns;
while (startSign.getRelative(0, y + 1, 0).getState() instanceof Sign && y != 0) {
Sign subsign = (Sign) startSign.getRelative(0, y + 1, 0).getState();
subsign.setLine(0, "");
subsign.setLine(1, "");
subsign.setLine(2, "");
subsign.setLine(3, "");
subsign.update();
y++;
}
}
/**
* Deletes the shop from the list of shops and queues it for database deletion
*
* @param memoryOnly whether to delete from database
*/
@Override
public void delete(boolean memoryOnly) {
this.lastChangedAt = System.currentTimeMillis();
ShopDeleteEvent shopDeleteEvent = new ShopDeleteEvent(this, memoryOnly);
if (Util.fireCancellableEvent(shopDeleteEvent)) {
Util.debugLog("Shop deletion was canceled because a plugin canceled it.");
return;
}
isDeleted = true;
// Unload the shop
if (isLoaded) {
this.onUnload();
}
// Delete the signs around it
for (Sign s : this.getSigns()) {
s.getBlock().setType(Material.AIR);
}
// Delete it from the database
// Refund if necessary
if (plugin.getConfig().getBoolean("shop.refund")) {
plugin.getEconomy().deposit(this.getOwner(), plugin.getConfig().getDouble("shop.cost"));
}
if (memoryOnly) {
// Delete it from memory
plugin.getShopManager().removeShop(this);
} else {
plugin.getShopManager().removeShop(this);
plugin.getDatabaseHelper().removeShop(this);
}
}
/**
* Changes all lines of text on a sign near the shop
*
* @param lines The array of lines to change. Index is line number.
*/
@Override
public void setSignText(@NotNull String[] lines) {
for (Sign sign : this.getSigns()) {
if (Arrays.equals(sign.getLines(), lines)) {
Util.debugLog("Skipped new sign text setup: Same content");
continue;
}
for (int i = 0; i < lines.length; i++) {
sign.setLine(i, lines[i]);
}
sign.update(true);
}
}
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
if (event.getClickedBlock().getState() instanceof Sign) {
if (manager.isSignRegistered(event.getClickedBlock().getLocation())) {
SignBlock sign = manager.getSign(event.getClickedBlock().getLocation());
owner.onClick(event.getPlayer(), sign);
}
}
}
}
@Override
public boolean check() {
if (!shopkeeper.getChunkData().isChunkLoaded()) {
// only verify sign, if the chunk is currently loaded:
return false;
}
Sign sign = this.getSign();
if (sign == null) {
String worldName = shopkeeper.getWorldName();
int x = shopkeeper.getX();
int y = shopkeeper.getY();
int z = shopkeeper.getZ();
// removing the shopkeeper, because re-spawning might fail (ex. attached block missing) or could be abused
// (sign drop farming):
Log.debug("Shopkeeper sign at (" + worldName + "," + x + "," + y + "," + z + ") is no longer existing! Attempting respawn now.");
if (!this.spawn()) {
Log.warning("Shopkeeper sign at (" + worldName + "," + x + "," + y + "," + z + ") could not be replaced! Removing shopkeeper now!");
// delayed removal:
Bukkit.getScheduler().runTask(ShopkeepersPlugin.getInstance(), new Runnable() {
@Override
public void run() {
shopkeeper.delete();
}
});
}
return true;
}
// update sign content if requested:
if (updateSign) {
updateSign = false;
this.updateSign();
}
return false;
}
public boolean inflate(Sign sign, Set<Variable> variables) {
for (int i = 0; i < lines.size() && i < LINE_COUNT; i++) {
CustomizableLine line = lines.get(i);
String lineString = line.generate(variables);
sign.setLine(i, lineString);
}
return sign.update();
}
public void updateSign() {
Sign sign = this.getSign();
if (sign == null) {
updateSign = true; // request update, once the sign is available again
return;
}
// line 0: header
sign.setLine(0, Settings.signShopFirstLine);
// line 1: shop name
String name = shopkeeper.getName();
String line1 = "";
if (name != null) {
name = this.trimToNameLength(name);
line1 = name;
}
sign.setLine(1, line1);
// line 2: owner name
String line2 = "";
if (shopkeeper instanceof PlayerShopkeeper) {
line2 = ((PlayerShopkeeper) shopkeeper).getOwnerName();
}
sign.setLine(2, line2);
// line 3: empty
sign.setLine(3, "");
// apply sign changes:
sign.update();
}
public void restoresigns_cmd() {
CivMessage.send(sender, "restoring....");
for (StructureSign sign : CivGlobal.getStructureSigns()) {
BlockCoord bcoord = sign.getCoord();
Block block = bcoord.getBlock();
ItemManager.setTypeId(block, CivData.WALL_SIGN);
ItemManager.setData(block, sign.getDirection());
Sign s = (Sign)block.getState();
String[] lines = sign.getText().split("\n");
if (lines.length > 0) {
s.setLine(0, lines[0]);
}
if (lines.length > 1) {
s.setLine(1, lines[1]);
}
if (lines.length > 2) {
s.setLine(2, lines[2]);
}
if (lines.length > 3) {
s.setLine(3, lines[3]);
}
s.update();
}
}
private void handleBlockClick(Player player, Block block) {
if(block != null && block.getState() instanceof Sign) {
sign(block.getLocation()).ifPresent(
sign -> sign.connector().teleport(player)
);
}
}