如何复制标志第1行输入的内容?它应该是打开还是关闭我想将它移动到第3行。这些行从0开始并转到3.所以如果有人在第二行打开,那么它将在最后一行显示为打开
package me.mcmatt.shops;
import java.util.Arrays;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.block.Block;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.SignChangeEvent;
import org.bukkit.event.player.PlayerInteractEvent;
public class Signs implements Listener {
@
EventHandler
public void onSignChange(SignChangeEvent e) {
if (e.getLine(0).equalsIgnoreCase("[shop]")) {
Block attached = e.getBlock().getRelative(0, -1, 0);
String name = e.getPlayer().getDisplayName();
if (!(attached.getType() == Material.CHEST))
e.getPlayer().sendMessage(ChatColor.RED + "Please place the shop on a chest!");
else {
if (!e.getPlayer().hasPermission("shops.create"))
e.getPlayer().sendMessage(ChatColor.RED + "You don't have permission to create a shop! (shops.create)");
else {
if (!Arrays.asList("open", "closed").contains(e.getLine(1).toLowerCase())) {
e.getPlayer().sendMessage(ChatColor.RED + "You must specify if the shop is open or closed on the second line!");
} else {
Sign o = (Sign) e.getBlock().getState();
e.setLine(0, "§9[Shop]");
e.setLine(1, "§4" + name + "'s");
e.setLine(2, "§4Shop");
e.setLine(3, o.getLine(1));
e.getPlayer().sendMessage(ChatColor.GREEN + "Shop Created!");
e.getPlayer().playSound(e.getPlayer().getLocation(), Sound.LEVEL_UP, 10, 10);
}
}
}
}
}
@
EventHandler
public void onPlayerInteract(PlayerInteractEvent e) {
if (e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
Player p = e.getPlayer();
Block b = e.getClickedBlock();
Material m = b.getType();
if (!(m == Material.SIGN_POST)) {
return;
} else {
Sign sign = (Sign) e.getClickedBlock().getState();
if ((sign.getLine(0).equalsIgnoreCase("§9[Shop]"))) {
p.sendMessage("I right clicked the sign!");
}
}
}
}
}
答案 0 :(得分:1)
只需检查正在设置的行,并使用适当的值设置第3行:
//on SignChangeEvent 'event'
event.setLine(3, event.getLine(1));
正如您在问题中所做的那样,请检查其余代码的逻辑,以确保它能够正确执行。
就我个人而言,我不会尝试将某人的输入直接设置为输出,而是我会接受他们给我的内容然后相应地使用它:
boolean closed = true;
if ("open".equalsIgnoreCase(event.getLine(1)) {
closed = false;
}
String lineThree = closed ? "Closed" : "Open";