Minecraft插件WorldEdit获得区域

时间:2015-06-18 20:48:47

标签: java minecraft bukkit

我试图解决区域的所有者到目前为止我一直试图选择区域这是我的代码:

package pl.maccraft.regs;


import java.util.logging.Logger;

import net.milkbowl.vault.economy.Economy;

import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.World;
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.player.PlayerInteractEvent;

import com.sk89q.worldguard.bukkit.RegionContainer;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.domains.DefaultDomain;
import com.sk89q.worldguard.protection.managers.RegionManager;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;

import org.bukkit.plugin.Plugin;



public final class egs extends JavaPlugin implements Listener {
    private static final Logger log = Logger.getLogger("Minecraft");
    public static Economy econ = null;
    @Override
    public void onEnable() {
        getServer().getPluginManager().registerEvents(this, this);
        getWorldGuard();
        if (!setupEconomy() ) {
            log.severe(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName()));
            getServer().getPluginManager().disablePlugin(this);
            return;
        }
    }
    private WorldGuardPlugin getWorldGuard() {
        Plugin plugin = getServer().getPluginManager().getPlugin("WorldGuard");

        // WorldGuard may not be loaded
        if (plugin == null || !(plugin instanceof WorldGuardPlugin)) {
            return null; // Maybe you want throw an exception instead
        }

        return (WorldGuardPlugin) plugin;

    }
    private boolean setupEconomy() {
        if (getServer().getPluginManager().getPlugin("Vault") == null) {
            return false;
        }
        RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
        if (rsp == null) {
            return false;
        }
        econ = rsp.getProvider();
        return econ != null;
    }
    @EventHandler
    public void playerInteract(PlayerInteractEvent event){
        Player player = event.getPlayer();
        if(event.getAction() == org.bukkit.event.block.Action.LEFT_CLICK_BLOCK){
        Block b = event.getClickedBlock();
        if (b.getType() == (Material.SIGN) || b.getType() == (Material.SIGN_POST) || b.getType() == (Material.WALL_SIGN)){
            Sign sign = (Sign) b.getState();
            if (sign.getLine(0).equals("[sell]")){
                event.setCancelled(true);
                RegionContainer container = getWorldGuard().getRegionContainer();
                RegionManager regions = container.get(world);
                if (regions != null) {
                    ProtectedRegion region = regions.getRegion("spawn");
                    Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "say stan:" + region);
                } else {
                    // The world has no region support or region data failed to load
                }
                    sign.setLine(0, ChatColor.DARK_RED + "[sprzedam]");
                    sign.setLine(1, player.getName());
                    sign.setLine(3, ChatColor.DARK_RED + sign.getLine(2));
                    sign.update(true);  

            }
        }
        }
        if(event.getAction() == org.bukkit.event.block.Action.RIGHT_CLICK_BLOCK){
            Block b = event.getClickedBlock();
            if (b.getType() == (Material.SIGN) || b.getType() == (Material.SIGN_POST) || b.getType() == (Material.WALL_SIGN)){
                Sign sign = (Sign) b.getState();
                if (sign.getLine(0).equals(ChatColor.DARK_RED + "[sprzedam]")){
                    if (event.getPlayer().getName() != sign.getLine(1)){
                        sign.setLine(0, ChatColor.DARK_RED + "[sprzedane]");
                        sign.setLine(2, event.getPlayer().getName());
                        sign.update(true);
                        Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "say stan:" + econ.getBalance(player));
                    }else{
                        Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "say nie możesz kupić własnej działki");
                    }
                }
            }
        }


    }

}

问题出在这一部分:

RegionContainer container = getWorldGuard().getRegionContainer();
            RegionManager regions = container.get(world);
            if (regions != null) {
                ProtectedRegion region = regions.getRegion("spawn");
                Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "say stan:" + region);
            } else {
                // The world has no region support or region data failed to load
            }

世界下面有红色下划线,当我悬停它时,它给了我:世界无法解析变量。我无法弄清楚为什么会发生这种情况因为这是从WorldGuard API页面复制过去的 - http://docs.enginehub.org/manual/worldguard/latest/developer/regions/managers/ 请帮帮我

1 个答案:

答案 0 :(得分:1)

WorldGuard API中用于按名称获取区域的代码只是一个示例。您的IDE抱怨,因为它实际上找不到您传递给get()方法的名为world的变量。您需要将自己的world对象提供给container.get(world)方法,在这种情况下,该方法可能是您的事件侦听器方法中的玩家世界。例如:container.get(player.getWorld());可能会有效。