Search in sources :

Example 6 with JavaPlugin

use of org.bukkit.plugin.java.JavaPlugin in project NoCheatPlus by NoCheatPlus.

the class CommandUtil method getCommands.

/**
 * Get all Command instances, that NCP can get hold of. Attempt to get a SimpleCommandMap instance from the server to get the actually registered commands, but also get commands from JavaPlugin instances.
 * @return
 */
public static Collection<Command> getCommands() {
    final Collection<Command> commands = new LinkedHashSet<Command>(500);
    // All (?) commands from the SimpleCommandMap of the server, if available.
    final CommandMap commandMap = getCommandMap();
    if (commandMap != null && commandMap instanceof SimpleCommandMap) {
        commands.addAll(((SimpleCommandMap) commandMap).getCommands());
    }
    // Fall-back: plugin commands.
    for (final Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
        if (plugin instanceof JavaPlugin) {
            final JavaPlugin javaPlugin = (JavaPlugin) plugin;
            final Map<String, Map<String, Object>> map = javaPlugin.getDescription().getCommands();
            if (map != null) {
                for (String label : map.keySet()) {
                    Command command = javaPlugin.getCommand(label);
                    if (command != null) {
                        commands.add(command);
                    }
                }
            }
        }
    }
    return commands;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Command(org.bukkit.command.Command) SimpleCommandMap(org.bukkit.command.SimpleCommandMap) JavaPlugin(org.bukkit.plugin.java.JavaPlugin) SimpleCommandMap(org.bukkit.command.SimpleCommandMap) Map(java.util.Map) CommandMap(org.bukkit.command.CommandMap) SimpleCommandMap(org.bukkit.command.SimpleCommandMap) CommandMap(org.bukkit.command.CommandMap) Plugin(org.bukkit.plugin.Plugin) JavaPlugin(org.bukkit.plugin.java.JavaPlugin)

Example 7 with JavaPlugin

use of org.bukkit.plugin.java.JavaPlugin in project BKCommonLib by bergerhealer.

the class MapWidgetAnvil method setWindowOpen.

private void setWindowOpen(boolean enabled) {
    if (this._isWindowOpen == enabled) {
        return;
    }
    this._isWindowOpen = enabled;
    // Close all old views first (to be sure), both when enabling and disabling
    for (InventoryView view : this._openInventories) {
        view.close();
    }
    this._openInventories.clear();
    if (enabled) {
        // Start listening
        JavaPlugin plugin = this.getDisplay().getPlugin();
        Bukkit.getPluginManager().registerEvents(this._listener, plugin);
        // Reset text
        this._text = "";
        // Open windows for all viewing players
        for (Player player : (_openFor.isEmpty() ? this.display.getViewers() : _openFor)) {
            if (_openFor.isEmpty() && !this.display.isControlling(player)) {
                continue;
            }
            final InventoryView view = EntityPlayerHandle.fromBukkit(player).openAnvilWindow(null);
            // Required for handling text changes < MC 1.9
            if (!CommonCapabilities.HAS_PREPARE_ANVIL_EVENT) {
                ContainerAnvilHandle container = ContainerAnvilHandle.fromBukkit(view);
                LegacyContainerAnvilHook hook = ClassHook.get(container.getRaw(), LegacyContainerAnvilHook.class);
                hook.textChangeCallback = () -> handleTextChange(view);
            }
            this._openInventories.add(view);
            this.refreshButtons(view);
        }
        // If it couldn't be opened for anyone, close itself
        if (this._openInventories.isEmpty()) {
            setWindowOpen(false);
        }
    } else {
        // Unregister event listener
        CommonUtil.unregisterListener(this._listener);
        // Deactivate the widget (to be sure)
        deactivate();
        // Event
        onClose();
    }
}
Also used : InventoryView(org.bukkit.inventory.InventoryView) Player(org.bukkit.entity.Player) LegacyContainerAnvilHook(com.bergerkiller.bukkit.common.internal.hooks.LegacyContainerAnvilHook) JavaPlugin(org.bukkit.plugin.java.JavaPlugin) ContainerAnvilHandle(com.bergerkiller.generated.net.minecraft.world.inventory.ContainerAnvilHandle)

Example 8 with JavaPlugin

use of org.bukkit.plugin.java.JavaPlugin in project BKCommonLib by bergerhealer.

the class CommonMapController method handleMapShowEvent.

@SuppressWarnings("deprecation")
protected synchronized void handleMapShowEvent(MapShowEvent event) {
    // Check if there are other map displays that should be shown to the player automatically
    // This uses the 'isGlobal()' property of the display
    MapDisplayInfo info = CommonMapController.this.getInfo(event.getMapUUID());
    boolean hasDisplay = false;
    if (info != null) {
        for (MapSession session : info.getSessions()) {
            if (session.display.isGlobal()) {
                session.display.addOwner(event.getPlayer());
                hasDisplay = true;
                break;
            }
        }
    }
    // When defined in the NBT of the item, construct the Map Display automatically
    // Do not do this when one was already assigned (global, or during event handling)
    // We initialize the display the next tick using the plugin owner's task to avoid
    // BKCommonLib showing up in timings when onAttached() is slow.
    MapDisplayProperties properties = MapDisplayProperties.of(event.getMapItem());
    if (!hasDisplay && !event.hasDisplay() && properties != null) {
        Class<? extends MapDisplay> displayClass = properties.getMapDisplayClass();
        if (displayClass != null) {
            Plugin plugin = properties.getPlugin();
            if (plugin instanceof JavaPlugin) {
                try {
                    MapDisplay display = displayClass.newInstance();
                    event.setDisplay((JavaPlugin) plugin, display);
                } catch (InstantiationException | IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    CommonUtil.callEvent(event);
}
Also used : MapDisplayProperties(com.bergerkiller.bukkit.common.map.MapDisplayProperties) MapDisplay(com.bergerkiller.bukkit.common.map.MapDisplay) MapDisplayInfo(com.bergerkiller.bukkit.common.map.binding.MapDisplayInfo) JavaPlugin(org.bukkit.plugin.java.JavaPlugin) MapSession(com.bergerkiller.bukkit.common.map.MapSession) Plugin(org.bukkit.plugin.Plugin) JavaPlugin(org.bukkit.plugin.java.JavaPlugin) CommonPlugin(com.bergerkiller.bukkit.common.internal.CommonPlugin)

Example 9 with JavaPlugin

use of org.bukkit.plugin.java.JavaPlugin in project MyMaid2 by jaoafa.

the class Messenger method LoadMessenger.

/**
 * メッセンジャーをロードする。
 * @return 実行できたかどうか
 * @author mine_book000
 * @throws Exception 何かしらのExceptionが発生したときに発生(FileNotFoundException, IOException)
 */
public static boolean LoadMessenger() throws Exception {
    Messages.clear();
    JSONParser parser = new JSONParser();
    String json = "";
    try {
        JavaPlugin plugin = JavaPlugin();
        File file = new File(plugin.getDataFolder() + File.separator + "messenger.json");
        BufferedReader br = new BufferedReader(new FileReader(file));
        String separator = System.getProperty("line.separator");
        String str;
        while ((str = br.readLine()) != null) {
            json += str + separator;
        }
        br.close();
    } catch (FileNotFoundException e1) {
        e1.printStackTrace();
        throw new FileNotFoundException(e1.getMessage());
    } catch (IOException e1) {
        e1.printStackTrace();
        throw new IOException(e1.getMessage());
    }
    JSONArray array;
    try {
        array = (JSONArray) parser.parse(json);
    } catch (ParseException e1) {
        array = new JSONArray();
    }
    for (Object obj : array) {
        String message = obj.toString();
        Messages.add(message);
    }
    return true;
}
Also used : BufferedReader(java.io.BufferedReader) FileNotFoundException(java.io.FileNotFoundException) JSONArray(org.json.simple.JSONArray) JavaPlugin(org.bukkit.plugin.java.JavaPlugin) JSONParser(org.json.simple.parser.JSONParser) FileReader(java.io.FileReader) IOException(java.io.IOException) ParseException(org.json.simple.parser.ParseException) File(java.io.File)

Example 10 with JavaPlugin

use of org.bukkit.plugin.java.JavaPlugin in project MyMaid2 by jaoafa.

the class Messenger method SaveMessenger.

/**
 * メッセンジャーをセーブする。
 * @return 実行できたかどうか
 * @author mine_book000
 * @throws Exception IOExceptionの発生時に発生
 */
@SuppressWarnings("unchecked")
public static boolean SaveMessenger() throws Exception {
    JSONArray array = new JSONArray();
    for (String message : Messages) {
        array.add(message);
    }
    try {
        JavaPlugin plugin = JavaPlugin();
        File file = new File(plugin.getDataFolder() + File.separator + "messenger.json");
        FileWriter filewriter = new FileWriter(file);
        filewriter.write(array.toJSONString());
        filewriter.close();
    } catch (IOException e) {
        e.printStackTrace();
        throw new Exception("IOException");
    }
    return true;
}
Also used : FileWriter(java.io.FileWriter) JSONArray(org.json.simple.JSONArray) JavaPlugin(org.bukkit.plugin.java.JavaPlugin) IOException(java.io.IOException) File(java.io.File) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ParseException(org.json.simple.parser.ParseException)

Aggregations

JavaPlugin (org.bukkit.plugin.java.JavaPlugin)11 File (java.io.File)4 FileNotFoundException (java.io.FileNotFoundException)4 IOException (java.io.IOException)4 Plugin (org.bukkit.plugin.Plugin)4 JSONArray (org.json.simple.JSONArray)4 ParseException (org.json.simple.parser.ParseException)4 MapDisplay (com.bergerkiller.bukkit.common.map.MapDisplay)2 MapSession (com.bergerkiller.bukkit.common.map.MapSession)2 Plugin (com.karuslabs.plugin.annotations.annotations.Plugin)2 BufferedReader (java.io.BufferedReader)2 FileReader (java.io.FileReader)2 FileWriter (java.io.FileWriter)2 Map (java.util.Map)2 Player (org.bukkit.entity.Player)2 JSONObject (org.json.simple.JSONObject)2 JSONParser (org.json.simple.parser.JSONParser)2 CommonPlugin (com.bergerkiller.bukkit.common.internal.CommonPlugin)1 LegacyContainerAnvilHook (com.bergerkiller.bukkit.common.internal.hooks.LegacyContainerAnvilHook)1 MapDisplayProperties (com.bergerkiller.bukkit.common.map.MapDisplayProperties)1