001/*
002 * Trident - A Multithreaded Server Alternative
003 * Copyright 2014 The TridentSDK Team
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *    http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package net.tridentsdk;
018
019import jdk.nashorn.internal.ir.annotations.Immutable;
020import net.tridentsdk.config.Config;
021import net.tridentsdk.registry.Registered;
022import net.tridentsdk.util.TridentLogger;
023
024import javax.imageio.ImageIO;
025import java.awt.image.BufferedImage;
026import java.io.File;
027import java.io.IOException;
028
029/**
030 * Information displayed to the client in the server list
031 *
032 * @author The TridentSDK Team
033 * @since 0.3-alpha-DP
034 */
035@Immutable
036public class PingInfo {
037    private final Config config = new Config(Trident.fileContainer().resolve("server.json")); // Init code
038    private final String motd = config.getString("motd", Defaults.MOTD);
039    private final File file = new File(config.getString("image-position", Defaults.MOTD_IMAGE_LOCATION));
040    private final int maxPlayers = config.getInt("max-players", Defaults.MAX_PLAYERS);
041    private final boolean canChangeImage = config.getBoolean("image-changing-allowed", Defaults.IMAGE_CHANGING_ALLOWED);
042
043    public PingInfo() {
044    }
045
046    /**
047     * A string containing the current broadcast MOTD of the server
048     *
049     * @return a string containing the MOTD of the server, may be empty, never null
050     */
051    public String motd() {
052        return motd;
053    }
054
055    /**
056     * Returns the {@link java.io.File} that represents the picture displayed next to the server listing on clients
057     *
058     * @return the file that represents the picture sent to clients when they ping the server
059     * @see #motdImage() for the representing the image sent to clients
060     */
061    public File motdPicture() {
062        return file;
063    }
064
065    /**
066     * Gets the {@link java.awt.image.BufferedImage} that represents the Motd picture sent to clients
067     *
068     * @return the image sent to clients
069     * @see #motdPicture() for the file itself
070     */
071    public BufferedImage motdImage() {
072        BufferedImage img = null;
073
074        try {
075            img = ImageIO.read(motdPicture());
076        } catch (IOException ex) {
077            TridentLogger.get().error(ex);
078        }
079
080        return img;
081    }
082
083    /**
084     * Obtains the highest minecraft server version which supports clients
085     *
086     * @return the minecraft server version
087     */
088    public String version() {
089        return "1.9";
090    }
091
092    /**
093     * Sets the MOTD image sent to clients, may or may not take a server restart to take effect
094     *
095     * @param image the image to set it to
096     * @return 0 for success, -1 if this feature is disabled in config, -2 for generic failure
097     */
098    public int setMotdImage(BufferedImage image) {
099        if (!canChangeImage) {
100            return -1;
101        }
102
103        try {
104            ImageIO.write(image, "PNG", motdPicture());
105            return 0;
106        } catch (IOException ignored) {
107            return -2;
108        }
109    }
110
111    /**
112     * Gets the maximum number of players allowed on the server
113     *
114     * @return the maximum number of players the server will allow
115     */
116    public int maxPlayers() {
117        return maxPlayers;
118    }
119
120    /**
121     * Obtains the amount of players on the server
122     *
123     * @return the amount of players on the server
124     */
125    public int playerCount() {
126        return Registered.players().size();
127    }
128}