Bukkit插件:空白符号

时间:2016-07-30 22:00:43

标签: java plugins minecraft bukkit

我正在尝试制作插件,这将允许您复制标志。将文本复制到我的变量是可以的,但问题是将其粘贴到另一个符号上。我已经阅读了很多文章,所以我试图在onSignChange上做延迟任务,但仍然无法正常工作:/这是我的代码:

public class Core extends JavaPlugin implements Listener {

    private boolean enabled = false;
    private boolean copyMode = false;

    private String l1;
    private String l2;
    private String l3;
    private String l4;

    public void onEnable() {
        getLogger().info("Plugin SignCopyReloaded was successfully enabled!");
        getServer().getPluginManager().registerEvents(this, this);
    }

    public void onDisable() {
        getLogger().info("Plugin SignCopyReloaded was successfully disabled!");
    }

    public boolean onCommand(CommandSender sender, Command cmd, String lavel, String[] args) {
        if(cmd.getName().equalsIgnoreCase("sc") && sender instanceof Player) {
            if(sender.hasPermission("sc.use")) {
                if(args.length > 0) {
                    switch (args[0]) {
                        case "enable":  enabled = true;
                                        sender.sendMessage(ChatColor.GREEN + "Enabled");
                                        break;
                        case "disable": enabled = false;
                                        sender.sendMessage(ChatColor.GREEN + "Disabled");
                                        break;
                        case "copy":    copyMode = true;
                                        enabled = true;
                                        sender.sendMessage(ChatColor.GREEN + "Copy mode enabled");
                                        break;
                    }
                } else {
                    sender.sendMessage(ChatColor.RED + "Usage: /sc enable/disable. When enabled, then use /sc copy and right click the sign you want to copy. Then right click anywhere you want to paste. To copy a new sign type /sc copy again.");
                }
            } else {
                sender.sendMessage(ChatColor.RED + "You must have permission sc.use!");
            }
        } else {
            sender.sendMessage(ChatColor.RED + "Only player can use this command!");
        }
    return true;
    }

    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent event) {
        if (event.getAction() == Action.RIGHT_CLICK_BLOCK && enabled == true) {
            if(event.getClickedBlock().getState() instanceof Sign) {
                if(copyMode == true) {
                    Sign sign = (Sign) event.getClickedBlock().getState();
                    l1 = sign.getLine(0);
                    l2 = sign.getLine(1);
                    l3 = sign.getLine(2);
                    l4 = sign.getLine(3);
                    copyMode = false;
                    event.getPlayer().sendMessage(ChatColor.GREEN + "Copy mode disabled. Now rightclick on blank sign.");
                } else {
                    Sign sign = (Sign) event.getClickedBlock().getState();
                    sign.setLine(0, l1);
                    sign.setLine(1, "Ahoj");
                    sign.setLine(2, l3);
                    sign.setLine(3, l4);
                    sign.update(true);
                }
            }
        }
    }

     @EventHandler
        public void onSignChange(SignChangeEvent event) {
             if(enabled == true) {
                 Sign sign = (Sign) event.getBlock().getState();
                    event.setCancelled(true);
                    Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                        @Override
                        public void run() {
                            sign.setLine(0, l1);
                            sign.setLine(1, "Ahoj");
                            sign.setLine(2, l3);
                            sign.setLine(3, l4);
                            sign.update(true);
                        }
                    });
             }
     }
}

1 个答案:

答案 0 :(得分:0)

只需使用SignChangeEvent的setLine方法

即可

例如event.setLine(0,l1);用于更改第一行。

这是javadoc引用: https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/event/block/SignChangeEvent.html#setLine(int,%20java.lang.String)