Java Code Examples for cn.nukkit.utils.MainLogger

The following examples show how to use cn.nukkit.utils.MainLogger. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: Jupiter   Source File: BanList.java    License: GNU General Public License v3.0 6 votes vote down vote up
public void load() {
    this.list = new LinkedHashMap<>();
    File file = new File(this.file);
    try {
        if (!file.exists()) {
            file.createNewFile();
            this.save();
        } else {

            LinkedList<TreeMap<String, String>> list = new Gson().fromJson(Utils.readFile(this.file), new TypeToken<LinkedList<TreeMap<String, String>>>() {
            }.getType());
            for (TreeMap<String, String> map : list) {
                BanEntry entry = BanEntry.fromMap(map);
                this.list.put(entry.getName(), entry);
            }
        }
    } catch (IOException e) {
        MainLogger.getLogger().error("Could not load ban list: ", e);
    }

}
 
Example 2
Source Project: Jupiter   Source File: BanList.java    License: GNU General Public License v3.0 6 votes vote down vote up
public void save() {
    this.removeExpired();

    try {
        File file = new File(this.file);
        if (!file.exists()) {
            file.createNewFile();
        }

        LinkedList<LinkedHashMap<String, String>> list = new LinkedList<>();
        for (BanEntry entry : this.list.values()) {
            list.add(entry.getMap());
        }
        Utils.writeFile(this.file, new ByteArrayInputStream(new GsonBuilder().setPrettyPrinting().create().toJson(list).getBytes(StandardCharsets.UTF_8)));
    } catch (IOException e) {
        MainLogger.getLogger().error("Could not save ban list ", e);
    }
}
 
Example 3
Source Project: Jupiter   Source File: PluginManager.java    License: GNU General Public License v3.0 6 votes vote down vote up
public void enablePlugin(Plugin plugin) {
    if (!plugin.isEnabled()) {
        try {
            for (Permission permission : plugin.getDescription().getPermissions()) {
                this.addPermission(permission);
            }
            plugin.getPluginLoader().enablePlugin(plugin);
        } catch (Throwable e) {
            MainLogger logger = this.server.getLogger();
            if (logger != null) {
                logger.logException(new RuntimeException(e));
            }
            this.disablePlugin(plugin);
        }
    }
}
 
Example 4
Source Project: Jupiter   Source File: PluginManager.java    License: GNU General Public License v3.0 6 votes vote down vote up
public void disablePlugin(Plugin plugin) {
    if (plugin.isEnabled()) {
        try {
            plugin.getPluginLoader().disablePlugin(plugin);
        } catch (Exception e) {
            MainLogger logger = this.server.getLogger();
            if (logger != null) {
                logger.logException(e);
            }
        }

        this.server.getScheduler().cancelTask(plugin);
        HandlerList.unregisterAll(plugin);
        for (Permission permission : plugin.getDescription().getPermissions()) {
            this.removePermission(permission);
        }
    }
}
 
Example 5
Source Project: Jupiter   Source File: CraftingGrid.java    License: GNU General Public License v3.0 6 votes vote down vote up
public void removeFromAll(Item item) {
    int count = item.getCount();

    for (int i = 0; i < this.size; i++) {
        Item target = this.getItem(i);

        if (target.equals(item, true, false)) {
            count--;
            target.count--;
            this.setItem(i, target);
            if (count <= 0) break;
        }
    }

    if (count != 0) {
        MainLogger.getLogger().debug("Unexpected ingredient count (" + count + ") in crafting grid");
    }
}
 
Example 6
Source Project: Nukkit   Source File: BanList.java    License: GNU General Public License v3.0 6 votes vote down vote up
public void load() {
    this.list = new LinkedHashMap<>();
    File file = new File(this.file);
    try {
        if (!file.exists()) {
            file.createNewFile();
            this.save();
        } else {

            LinkedList<TreeMap<String, String>> list = new Gson().fromJson(Utils.readFile(this.file), new TypeToken<LinkedList<TreeMap<String, String>>>() {
            }.getType());
            for (TreeMap<String, String> map : list) {
                BanEntry entry = BanEntry.fromMap(map);
                this.list.put(entry.getName(), entry);
            }
        }
    } catch (IOException e) {
        MainLogger.getLogger().error("Could not load ban list: ", e);
    }

}
 
Example 7
Source Project: Nukkit   Source File: BanList.java    License: GNU General Public License v3.0 6 votes vote down vote up
public void save() {
    this.removeExpired();

    try {
        File file = new File(this.file);
        if (!file.exists()) {
            file.createNewFile();
        }

        LinkedList<LinkedHashMap<String, String>> list = new LinkedList<>();
        for (BanEntry entry : this.list.values()) {
            list.add(entry.getMap());
        }
        Utils.writeFile(this.file, new ByteArrayInputStream(new GsonBuilder().setPrettyPrinting().create().toJson(list).getBytes(StandardCharsets.UTF_8)));
    } catch (IOException e) {
        MainLogger.getLogger().error("Could not save ban list ", e);
    }
}
 
Example 8
Source Project: Nukkit   Source File: PluginManager.java    License: GNU General Public License v3.0 6 votes vote down vote up
public void enablePlugin(Plugin plugin) {
    if (!plugin.isEnabled()) {
        try {
            for (Permission permission : plugin.getDescription().getPermissions()) {
                this.addPermission(permission);
            }
            plugin.getPluginLoader().enablePlugin(plugin);
        } catch (Throwable e) {
            MainLogger logger = this.server.getLogger();
            if (logger != null) {
                logger.logException(new RuntimeException(e));
            }
            this.disablePlugin(plugin);
        }
    }
}
 
Example 9
Source Project: Nukkit   Source File: PluginManager.java    License: GNU General Public License v3.0 6 votes vote down vote up
public void disablePlugin(Plugin plugin) {
    if (plugin.isEnabled()) {
        try {
            plugin.getPluginLoader().disablePlugin(plugin);
        } catch (Exception e) {
            MainLogger logger = this.server.getLogger();
            if (logger != null) {
                logger.logException(e);
            }
        }

        this.server.getScheduler().cancelTask(plugin);
        HandlerList.unregisterAll(plugin);
        for (Permission permission : plugin.getDescription().getPermissions()) {
            this.removePermission(permission);
        }
    }
}
 
Example 10
Source Project: Nukkit   Source File: Item.java    License: GNU General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private static void initCreativeItems() {
    clearCreativeItems();

    Config config = new Config(Config.YAML);
    config.load(Server.class.getClassLoader().getResourceAsStream("creativeitems.json"));
    List<Map> list = config.getMapList("items");

    for (Map map : list) {
        try {
            addCreativeItem(fromJson(map));
        } catch (Exception e) {
            MainLogger.getLogger().logException(e);
        }
    }
}
 
Example 11
Source Project: Nukkit   Source File: ItemMap.java    License: GNU General Public License v3.0 6 votes vote down vote up
public void setImage(BufferedImage image) {
    try {
        if (image.getHeight() != 128 || image.getWidth() != 128) { //resize
            this.image = new BufferedImage(128, 128, image.getType());
            Graphics2D g = this.image.createGraphics();
            g.drawImage(image, 0, 0, 128, 128, null);
            g.dispose();
        } else {
            this.image = image;
        }

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(this.image, "png", baos);

        this.getNamedTag().putByteArray("Colors", baos.toByteArray());
    } catch (IOException e) {
        MainLogger.getLogger().logException(e);
    }
}
 
Example 12
Source Project: Nukkit   Source File: CraftingManager.java    License: GNU General Public License v3.0 6 votes vote down vote up
public CraftingManager() {
    InputStream recipesStream = Server.class.getClassLoader().getResourceAsStream("recipes.json");
    if (recipesStream == null) {
        throw new AssertionError("Unable to find recipes.json");
    }

    Config recipesConfig = new Config(Config.JSON);
    recipesConfig.load(recipesStream);
    this.loadRecipes(recipesConfig);

    String path = Server.getInstance().getDataPath() + "custom_recipes.json";
    File filePath = new File(path);

    if (filePath.exists()) {
        Config customRecipes = new Config(filePath, Config.JSON);
        this.loadRecipes(customRecipes);
    }
    this.rebuildPacket();

    MainLogger.getLogger().info("Loaded " + this.recipes.size() + " recipes.");
}
 
Example 13
Source Project: Nukkit   Source File: BanList.java    License: GNU General Public License v3.0 6 votes vote down vote up
public void load() {
    this.list = new LinkedHashMap<>();
    File file = new File(this.file);
    try {
        if (!file.exists()) {
            file.createNewFile();
            this.save();
        } else {

            LinkedList<TreeMap<String, String>> list = new Gson().fromJson(Utils.readFile(this.file), new TypeToken<LinkedList<TreeMap<String, String>>>() {
            }.getType());
            for (TreeMap<String, String> map : list) {
                BanEntry entry = BanEntry.fromMap(map);
                this.list.put(entry.getName(), entry);
            }
        }
    } catch (IOException e) {
        MainLogger.getLogger().error("Could not load ban list: ", e);
    }

}
 
Example 14
Source Project: Nukkit   Source File: BanList.java    License: GNU General Public License v3.0 6 votes vote down vote up
public void save() {
    this.removeExpired();

    try {
        File file = new File(this.file);
        if (!file.exists()) {
            file.createNewFile();
        }

        LinkedList<LinkedHashMap<String, String>> list = new LinkedList<>();
        for (BanEntry entry : this.list.values()) {
            list.add(entry.getMap());
        }
        Utils.writeFile(this.file, new ByteArrayInputStream(new GsonBuilder().setPrettyPrinting().create().toJson(list).getBytes(StandardCharsets.UTF_8)));
    } catch (IOException e) {
        MainLogger.getLogger().error("Could not save ban list ", e);
    }
}
 
Example 15
Source Project: Nukkit   Source File: PluginManager.java    License: GNU General Public License v3.0 6 votes vote down vote up
public void enablePlugin(Plugin plugin) {
    if (!plugin.isEnabled()) {
        try {
            for (Permission permission : plugin.getDescription().getPermissions()) {
                this.addPermission(permission);
            }
            plugin.getPluginLoader().enablePlugin(plugin);
        } catch (Throwable e) {
            MainLogger logger = this.server.getLogger();
            if (logger != null) {
                logger.logException(new RuntimeException(e));
            }
            this.disablePlugin(plugin);
        }
    }
}
 
Example 16
Source Project: Nukkit   Source File: PluginManager.java    License: GNU General Public License v3.0 6 votes vote down vote up
public void disablePlugin(Plugin plugin) {
    if (plugin.isEnabled()) {
        try {
            plugin.getPluginLoader().disablePlugin(plugin);
        } catch (Exception e) {
            MainLogger logger = this.server.getLogger();
            if (logger != null) {
                logger.logException(e);
            }
        }

        this.server.getScheduler().cancelTask(plugin);
        HandlerList.unregisterAll(plugin);
        for (Permission permission : plugin.getDescription().getPermissions()) {
            this.removePermission(permission);
        }
    }
}
 
Example 17
Source Project: Nukkit   Source File: CraftingGrid.java    License: GNU General Public License v3.0 6 votes vote down vote up
public void removeFromAll(Item item) {
    int count = item.getCount();

    for (int i = 0; i < this.size; i++) {
        Item target = this.getItem(i);

        if (target.equals(item, true, false)) {
            count--;
            target.count--;
            this.setItem(i, target);
            if (count <= 0) break;
        }
    }

    if (count != 0) {
        MainLogger.getLogger().debug("Unexpected ingredient count (" + count + ") in crafting grid");
    }
}
 
Example 18
Source Project: Jupiter   Source File: RakNetInterface.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
    public void handleEncapsulated(String identifier, EncapsulatedPacket packet, int flags) {
        if (this.players.containsKey(identifier)) {
            DataPacket pk = null;
            try {
                if (packet.buffer.length > 0) {
                    if (packet.buffer[0] == PING_DataPacket.ID) {
                        PING_DataPacket pingPacket = new PING_DataPacket();
                        pingPacket.buffer = packet.buffer;
                        pingPacket.decode();

                        this.networkLatency.put(identifier, (int) pingPacket.pingID);
                        return;
                    }

                    pk = this.getPacket(packet.buffer);
                    if (pk != null) {
                        pk.decode();
                        this.players.get(identifier).handleDataPacket(pk);
                    }
                }
            } catch (Exception e) {
                this.server.getLogger().logException(e);
                if (Nukkit.DEBUG > 1 && pk != null) {
                    MainLogger logger = this.server.getLogger();
//                    if (logger != null) {
                    logger.debug("Packet " + pk.getClass().getName() + " 0x" + Binary.bytesToHexString(packet.buffer));
                    //logger.logException(e);
//                    }
                }

                if (this.players.containsKey(identifier)) {
                    this.handler.blockAddress(this.players.get(identifier).getAddress(), 5);
                }
            }
        }
    }
 
Example 19
Source Project: Jupiter   Source File: ItemMap.java    License: GNU General Public License v3.0 5 votes vote down vote up
public BufferedImage loadImageFromNBT() {
    try {
        byte[] data = getNamedTag().getByteArray("Colors");
        return ImageIO.read(new ByteArrayInputStream(data));
    } catch (IOException e) {
        MainLogger.getLogger().logException(e);
    }

    return null;
}
 
Example 20
Source Project: Jupiter   Source File: ConsoleCommandSender.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public void sendMessage(String message) {
    message = this.getServer().getLanguage().translateString(message);
    for (String line : message.trim().split("\n")) {
        MainLogger.getLogger().info(line);
    }
}
 
Example 21
Source Project: Jupiter   Source File: ConsoleCommandSender.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public void sendImportantMessage(String message) {
    message = this.getServer().getLanguage().translateString(message);
    for (String line : message.trim().split("\n")) {
        MainLogger.getLogger().info(line);
    }
}
 
Example 22
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
    boolean result = false;
    ArrayList<String> commands = new ArrayList<>();
    for (String formatString : formatStrings) {
        try {
            commands.add(buildCommand(formatString, args));
        } catch (Exception e) {
            if (e instanceof IllegalArgumentException) {
                sender.sendMessage(TextFormat.RED + e.getMessage());
            } else {
                sender.sendMessage(new TranslationContainer(TextFormat.RED + "%commands.generic.exception"));
                MainLogger logger = sender.getServer().getLogger();
                if (logger != null) {
                    logger.logException(e);
                }
            }
            return false;
        }
    }

    for (String command : commands) {
        result |= Server.getInstance().dispatchCommand(sender, command);
    }

    return result;
}
 
Example 23
Source Project: Jupiter   Source File: SimpleCommandMap.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean dispatch(CommandSender sender, String cmdLine) {
    ArrayList<String> parsed = parseArguments(cmdLine);
    if (parsed.size() == 0) {
        return false;
    }

    String sentCommandLabel = parsed.remove(0).toLowerCase();
    String[] args = parsed.toArray(new String[parsed.size()]);
    Command target = this.getCommand(sentCommandLabel);

    if (target == null) {
        return false;
    }

    target.timing.startTiming();
    try {
        target.execute(sender, sentCommandLabel, args);
    } catch (Exception e) {
        sender.sendMessage(new TranslationContainer(TextFormat.RED + "%commands.generic.exception"));
        this.server.getLogger().critical(this.server.getLanguage().translateString("nukkit.command.exception", cmdLine, target.toString(), Utils.getExceptionMessage(e)));
        MainLogger logger = sender.getServer().getLogger();
        if (logger != null) {
            logger.logException(e);
        }
    }
    target.timing.stopTiming();

    return true;
}
 
Example 24
Source Project: Nukkit   Source File: Entity.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static Entity createEntity(String name, FullChunk chunk, CompoundTag nbt, Object... args) {
    Entity entity = null;

    if (knownEntities.containsKey(name)) {
        Class<? extends Entity> clazz = knownEntities.get(name);

        if (clazz == null) {
            return null;
        }

        for (Constructor constructor : clazz.getConstructors()) {
            if (entity != null) {
                break;
            }

            if (constructor.getParameterCount() != (args == null ? 2 : args.length + 2)) {
                continue;
            }

            try {
                if (args == null || args.length == 0) {
                    entity = (Entity) constructor.newInstance(chunk, nbt);
                } else {
                    Object[] objects = new Object[args.length + 2];

                    objects[0] = chunk;
                    objects[1] = nbt;
                    System.arraycopy(args, 0, objects, 2, args.length);
                    entity = (Entity) constructor.newInstance(objects);

                }
            } catch (Exception e) {
                MainLogger.getLogger().logException(e);
            }

        }
    }

    return entity;
}
 
Example 25
Source Project: Nukkit   Source File: RakNetInterface.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
    public void handleEncapsulated(String identifier, EncapsulatedPacket packet, int flags) {
        if (this.players.containsKey(identifier)) {
            DataPacket pk = null;
            try {
                if (packet.buffer.length > 0) {
                    if (packet.buffer[0] == PING_DataPacket.ID) {
                        PING_DataPacket pingPacket = new PING_DataPacket();
                        pingPacket.buffer = packet.buffer;
                        pingPacket.decode();

                        this.networkLatency.put(identifier, (int) pingPacket.pingID);
                        return;
                    }

                    pk = this.getPacket(packet.buffer);
                    if (pk != null) {
                        pk.decode();
                        this.players.get(identifier).handleDataPacket(pk);
                    }
                }
            } catch (Exception e) {
                this.server.getLogger().logException(e);
                if (Nukkit.DEBUG > 1 && pk != null) {
                    MainLogger logger = this.server.getLogger();
//                    if (logger != null) {
                    logger.debug("Packet " + pk.getClass().getName() + " 0x" + Binary.bytesToHexString(packet.buffer));
                    //logger.logException(e);
//                    }
                }

                if (this.players.containsKey(identifier)) {
                    this.handler.blockAddress(this.players.get(identifier).getAddress(), 5);
                }
            }
        }
    }
 
Example 26
Source Project: Nukkit   Source File: ItemMap.java    License: GNU General Public License v3.0 5 votes vote down vote up
protected BufferedImage loadImageFromNBT() {
    try {
        byte[] data = getNamedTag().getByteArray("Colors");
        image = ImageIO.read(new ByteArrayInputStream(data));
        return image;
    } catch (IOException e) {
        MainLogger.getLogger().logException(e);
    }

    return null;
}
 
Example 27
Source Project: Nukkit   Source File: ConsoleCommandSender.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public void sendMessage(String message) {
    message = this.getServer().getLanguage().translateString(message);
    for (String line : message.trim().split("\n")) {
        MainLogger.getLogger().info(line);
    }
}
 
Example 28
Source Project: Nukkit   Source File: FormattedCommandAlias.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
    boolean result = false;
    ArrayList<String> commands = new ArrayList<>();
    for (String formatString : formatStrings) {
        try {
            commands.add(buildCommand(formatString, args));
        } catch (Exception e) {
            if (e instanceof IllegalArgumentException) {
                sender.sendMessage(TextFormat.RED + e.getMessage());
            } else {
                sender.sendMessage(new TranslationContainer(TextFormat.RED + "%commands.generic.exception"));
                MainLogger logger = sender.getServer().getLogger();
                if (logger != null) {
                    logger.logException(e);
                }
            }
            return false;
        }
    }

    for (String command : commands) {
        result |= Server.getInstance().dispatchCommand(sender, command);
    }

    return result;
}
 
Example 29
Source Project: Nukkit   Source File: SimpleCommandMap.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean dispatch(CommandSender sender, String cmdLine) {
    ArrayList<String> parsed = parseArguments(cmdLine);
    if (parsed.size() == 0) {
        return false;
    }

    String sentCommandLabel = parsed.remove(0).toLowerCase();
    String[] args = parsed.toArray(new String[0]);
    Command target = this.getCommand(sentCommandLabel);

    if (target == null) {
        return false;
    }

    target.timing.startTiming();
    try {
        target.execute(sender, sentCommandLabel, args);
    } catch (Exception e) {
        sender.sendMessage(new TranslationContainer(TextFormat.RED + "%commands.generic.exception"));
        this.server.getLogger().critical(this.server.getLanguage().translateString("nukkit.command.exception", cmdLine, target.toString(), Utils.getExceptionMessage(e)));
        MainLogger logger = sender.getServer().getLogger();
        if (logger != null) {
            logger.logException(e);
        }
    }
    target.timing.stopTiming();

    return true;
}
 
Example 30
private static EntityMetadata replaceMetadata(EntityMetadata data, long from, long to) {
    boolean changed = false;

    for (Integer key : replaceMetadata) {
        try {
            EntityData ed = data.get(key);

            if (ed == null) {
                continue;
            }

            if (ed.getType() != Entity.DATA_TYPE_LONG) {
                MainLogger.getLogger().info("Wrong entity data type (" + key + ") expected 'Long' got '" + dataTypeToString(ed.getType()) + "'");
                continue;
            }

            long value = ((LongEntityData) ed).getData();

            if (value == from) {
                if (!changed) {
                    data = cloneMetadata(data);
                    changed = true;
                }

                data.putLong(key, to);
            }
        } catch (Exception e) {
            MainLogger.getLogger().error("Exception while replacing metadata '" + key + "'", e);
        }
    }

    if (!changed) return null;

    return data;
}