返回文档

开发者教程

Maven依赖

<project>
...
    <repositories>
        <repository>
            <id>bintray-repo</id>
            <url>https://dl.bintray.com/ichbinjoe/public/</url>
        </repository>
        ...
    </repositories>
...
    <dependencies>
        <dependency>
            <groupId>com.vexsoftware</groupId>
            <artifactId>nuvotifier-universal</artifactId>
            <version>2.6.0</version>
            <scope>provided</scope>
        </dependency>
        ...
    </dependencies>
...
</project>

Gradle依赖

...
repositories {
    maven {
        url 'https://dl.bintray.com/ichbinjoe/public/'
    }
    ...
}
...
dependencies {
    compileOnly "com.vexsoftware:nuvotifier-universal:version"
    ...
}
...

监听投票

投票监听器

import com.vexsoftware.votifier.model.Vote;
import com.vexsoftware.votifier.model.VoteListener;

public class BasicVoteListener implements VoteListener {

    public void voteMade(Vote vote) {
        System.out.println("Received: " + vote);
    }

}

注册监听器

public void onEnable() {
    [...]
    getServer().getPluginManager()
        .registerEvents(new CustomEventListener(), this);
    [...]
}

添加插件运行依赖

depends: [Votifier]

示例插件

下面我写了一个很简单的VoteParty,每当票数达到5时,奖励全部在线玩家64个钻石

package org.akadia.voteparty;

import com.vexsoftware.votifier.model.VotifierEvent;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.concurrent.Semaphore;

public class VotePartyMain extends JavaPlugin implements Listener {

    int counter; // 计数器
    static Semaphore mutex = new Semaphore(1); // 互斥锁
    String command = "give %player% minecraft:diamond 64"; // 运行的指令

    @Override
    public void onEnable() {
        counter = 0; // 初始化计数器
        this.getServer().getPluginManager().registerEvents(this, this); // 注册监听器
    }

    @Override
    public void onDisable() {

    }

    @EventHandler(priority = EventPriority.NORMAL)
    public void onVotifierEvent(VotifierEvent event) {
        try {
            mutex.acquire(); // 上锁
            counter++;
            System.out.println(counter);
            if (counter == 5) { // 如果达到目标
                counter = 0; // 重置计数器

                for (Player onlinePlayer : this.getServer().getOnlinePlayers()) { // 发放奖励
                    this.getServer().dispatchCommand(Bukkit.getConsoleSender(),
                            command.replaceAll("%player%", onlinePlayer.getName())
                    );
                }
            }

        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            mutex.release(); // 解锁
        }
    }
}

引用