Spigot API - 播放器可见性切换不起作用

时间:2017-07-15 04:53:29

标签: minecraft bukkit

我正在尝试为中心服务器创建一个插件来切换可见性。像Hypixel,或任何其他具有集线器的半流行服务器。

主类:

package com.infxnty.HubCore;

import java.util.ArrayList;
import java.util.List;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;

public class Main extends JavaPlugin implements Listener {

    private static Main Instance;

    @Override
    public void onEnable() {
        setInstance(this);

        String text = "&1&lHubCore Enabled! &8» &aCoded by &b&lInfxnty";

        System.out.println(ChatColor.translateAlternateColorCodes('&', text));

        this.getConfig().options().copyDefaults();
        saveDefaultConfig();

        getCommand("star").setExecutor(new StarCommand(this));
        Bukkit.getPluginManager().registerEvents(new Gamemode(), this);
        Bukkit.getPluginManager().registerEvents(this, this);
    }


    String serverone = this.getConfig().getString("Serverone");
    String servertwo = this.getConfig().getString("Servertwo");
    String serverthree = this.getConfig().getString("Serverthree");
    String guititle = this.getConfig().getString("GUI-Title");
    /* Array Lists */
    ArrayList<Player> enabled = new ArrayList<>();

    /* Star UI */

    public void applyStarUI(Player player) {

        // Beginning 
        Inventory gui = Bukkit.createInventory(null, 9, ChatColor.translateAlternateColorCodes('&', guititle));

        // Lores
        List<String> factionsLore = new ArrayList<>();
        factionsLore.add(" ");
        factionsLore.add(ChatColor.GRAY + "Click to connect");
        factionsLore.add(ChatColor.GRAY + "to " + servertwo + "!");

        List<String> cannoningLore = new ArrayList<>();
        cannoningLore.add(" ");
        cannoningLore.add(ChatColor.GRAY + "Click to connect");
        cannoningLore.add(ChatColor.GRAY + "to " + serverone + "!");

        List<String> eventsLore = new ArrayList<>();
        eventsLore.add(" ");
        eventsLore.add(ChatColor.GRAY + "Click to connect");
        eventsLore.add(ChatColor.GRAY + "to " + serverthree + "!");

        // ItemStacks


        ItemStack cannoning;
        ItemMeta cannoningMeta;
        cannoning = new ItemStack(Material.DISPENSER);
        cannoningMeta = cannoning.getItemMeta();

        cannoningMeta.setDisplayName(ChatColor.DARK_RED + serverone);
        cannoningMeta.setLore(cannoningLore);
        cannoning.setItemMeta(cannoningMeta);


        ItemStack factions;
        ItemMeta factionsMeta;
        factions = new ItemStack(Material.DIAMOND_SWORD);
        factionsMeta = factions.getItemMeta();

        factionsMeta.setDisplayName(ChatColor.DARK_RED + servertwo);
        factionsMeta.setLore(factionsLore);
        factions.setItemMeta(factionsMeta);


        ItemStack events;
        ItemMeta eventsMeta;
        events = new ItemStack(Material.FIREWORK);
        eventsMeta = events.getItemMeta();

        eventsMeta.setDisplayName(ChatColor.DARK_RED + serverthree);
        eventsMeta.setLore(eventsLore);
        events.setItemMeta(eventsMeta);

        // Inventory Setting

        gui.setItem(0, cannoning);
        gui.setItem(4, factions);
        gui.setItem(8, events);

        // Final

        player.openInventory(gui);

    }

    public static Main getInstance() {
        return Instance;
    }

    public static void setInstance(Main instance) {
        Instance = instance;
    }

}

Gamemode Class:

package com.infxnty.HubCore;

import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntitySpawnEvent;
import org.bukkit.event.entity.FoodLevelChangeEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import com.infxnty.HubCore.Main;

public class Gamemode implements Listener {     

    @SuppressWarnings("deprecation")
    @EventHandler
    public void onJoin(PlayerJoinEvent e) {
        e.getPlayer().setMaxHealth(2.0);
        e.getPlayer().setHealth(2.0);

        Player p = e.getPlayer();
        int slot = 0;
        int slot2 = 8;

        ItemStack star;
        ItemMeta starMeta;
        star = new ItemStack(Material.NETHER_STAR);
        starMeta = star.getItemMeta();

        starMeta.setDisplayName(ChatColor.AQUA + "Server Selector");
        star.setItemMeta(starMeta);

        ItemStack visibility;
        ItemMeta visibilityMeta;
        visibility = new ItemStack(Material.SLIME_BALL);
        visibilityMeta = visibility.getItemMeta();

        visibilityMeta.setDisplayName(ChatColor.GREEN + "Player Visibility Enabled!");
        visibility.setItemMeta(visibilityMeta);

        // Setting item in their inventory
        p.getInventory().setItem(slot, star);
        p.getInventory().setItem(slot2, visibility);

        }

        String serverone = Main.getInstance().getConfig().getString("Serverone");
        String servertwo = Main.getInstance().getConfig().getString("Servertwo");
        String serverthree = Main.getInstance().getConfig().getString("Serverthree");
        String guititle = Main.getInstance().getConfig().getString("GUI-Title");


        @EventHandler
        public void onPlayerClickInv(InventoryClickEvent e) {

        Player player = (Player) e.getWhoClicked();

        Inventory Inv = e.getClickedInventory();

        // Clicked inventory obj

        ItemStack visibility = new ItemStack(Material.SLIME_BALL);
        ItemStack star = new ItemStack(Material.NETHER_STAR);

        // Title of the inventory (container.inventory is the name of a default player inventory)
        if(Inv.getTitle().equals("container.inventory")){
            if (e.getCurrentItem().isSimilar(visibility)) {
                e.setCancelled(true);
            }
        // Checking if clicked item is your compass or anything else
        if(e.getCurrentItem().isSimilar(star)) {
            e.setCancelled(true);
            }


        if (ChatColor.translateAlternateColorCodes('&', e.getClickedInventory().getTitle()).equals(guititle)) {
            if (e.getCurrentItem() != null) {
            e.setCancelled(true);

                switch (e.getCurrentItem().getType()) {
                case DISPENSER:
                    player.performCommand("server " + serverone);
                    player.sendMessage(ChatColor.RED + "Sending you to our " + serverone + "Server!");
                    break;
                case DIAMOND_SWORD:
                    player.performCommand("server " + servertwo);
                    player.sendMessage(ChatColor.RED + "Sending you to our " + servertwo + "Server!");
                    break;
                case FIREWORK:
                    player.performCommand("server " + serverthree);
                    player.sendMessage(ChatColor.RED + "Sending you to our " + serverthree + "Server!");
                    break;
                default:
                    return;
                }

            player.closeInventory();
        }

        }
        e.setCancelled(true);
    }
}

    @SuppressWarnings("deprecation")
    @EventHandler
    public void onRespawn(PlayerRespawnEvent e) {
        e.getPlayer().setMaxHealth(2.0);
        e.getPlayer().setHealth(2.0);

    }

    @EventHandler
    public void onDrop(PlayerDropItemEvent e) {
        e.setCancelled(true);

    }

    @EventHandler
    public void onEntitySpawn(EntitySpawnEvent e) {
        e.setCancelled(true);

    }

    @EventHandler
    public void onFoodChange(FoodLevelChangeEvent e) {
        e.setCancelled(true);

    }

    @EventHandler
    public void onDamage(EntityDamageEvent e) {
        e.setCancelled(true);

    }

    @SuppressWarnings({ "deprecation" })
    @EventHandler
    public void onClick(PlayerInteractEvent e) {

        Player p = e.getPlayer();

        int slot = 8;

        ItemStack visibility;
        ItemMeta visibilityMeta;
        visibility = new ItemStack(Material.SLIME_BALL);
        visibilityMeta = visibility.getItemMeta();

        visibilityMeta.setDisplayName(ChatColor.GREEN + "Player Visibility Enabled!");
        visibility.setItemMeta(visibilityMeta);

        ItemStack visibility3;
        ItemMeta visibility3Meta;
        visibility3 = new ItemStack(Material.FIREBALL);
        visibility3Meta = visibility3.getItemMeta();

        visibility3Meta.setDisplayName(ChatColor.GRAY + "Player Visibility Disabled");
        visibility3.setItemMeta(visibility3Meta);

        if (p.getItemInHand().getType().equals(Material.NETHER_STAR)) {
            p.performCommand("star");

        }

        if (p.getItemInHand().getType().equals(Material.SLIME_BALL)) {
            p.getInventory().setItem(slot, visibility3);

        }

        if (p.getItemInHand().getType().equals(Material.FIREBALL)) {
            p.getInventory().setItem(slot, visibility);

        }

    }

}

右键点击游戏中的粘液球时,它会变为正确的项目,然后立即变回。如果你想在游戏中看到它,请加入我的测试服务器,因为我可能没有正确描述它。

IP:172.93.238.170:28584(版本1.11.2)

对于那些对Spigot API有什么困惑的人来说,它适用于Minecraft。如果你能提供我搞乱的代码行,以及如何改变它,那就太好了。谢谢!

1 个答案:

答案 0 :(得分:1)

发生的事情是你在代码中切换粘液球然后在你要切换它的下一行。

此:

    if (p.getItemInHand().getType().equals(Material.NETHER_STAR)) {
        p.performCommand("star");

    }

    if (p.getItemInHand().getType().equals(Material.SLIME_BALL)) {
        p.getInventory().setItem(slot, visibility3);

    }

    if (p.getItemInHand().getType().equals(Material.FIREBALL)) {
        p.getInventory().setItem(slot, visibility);

    }

应改为:

    if (p.getItemInHand().getType().equals(Material.NETHER_STAR)) {
        p.performCommand("star");

    }else if (p.getItemInHand().getType().equals(Material.SLIME_BALL)) {
        p.getInventory().setItem(slot, visibility3);

    }else if (p.getItemInHand().getType().equals(Material.FIREBALL)) {
        p.getInventory().setItem(slot, visibility);

    }

基本上你将粘液球改为火球,下一段代码会将火球改回泥球。添加else语句使得如果项目被更改,它将不会检查if语句中的其余项目。希望有意义!