Java tutorial
/* * Statistician, a custom mongo backed statistic collector for Bukkit * Copyright (C) meggawatts <http://mcme.co> * * This file is part of Statistician. * * Statistician is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Statistician is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Statistician. If not, see <http://www.gnu.org/licenses/>. */ package co.mcme.statistician.database; import co.mcme.statistician.Statistician; import co.mcme.statistician.utilities.StatisticianLogger; import com.mongodb.DBObject; import com.mongodb.util.JSON; import lombok.Getter; import lombok.Setter; import org.bson.types.ObjectId; import org.bukkit.Server; import org.bukkit.World; import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; import org.codehaus.jackson.map.SerializationConfig; import org.codehaus.jackson.map.annotate.JsonSerialize; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.List; import java.util.logging.Level; public class StatsReport { @Getter @Setter private ObjectId _id = new ObjectId(); @Getter @Setter private Date upSince; @Getter @Setter private String mcVersion = ""; @Getter @Setter private String bukkitVersion = ""; @Getter @Setter private int port = 0; @Getter @Setter private List<String> motd = new ArrayList(); @Getter @Setter private int numPlayers = 0; @Getter @Setter private int maxPlayers = 0; @Getter @Setter private List<String> players = new ArrayList(); @Getter @Setter private List<String> plugins = new ArrayList(); @Getter @Setter private List<String> worlds = new ArrayList(); @Getter @Setter private EventReport events; public StatsReport(Server serverInstance) { upSince = Statistician.getUpSince(); this._id = new ObjectId(); this.mcVersion = serverInstance.getVersion(); this.bukkitVersion = serverInstance.getBukkitVersion(); this.port = serverInstance.getPort(); this.motd = Arrays.asList(serverInstance.getMotd().split("\\n")); this.numPlayers = serverInstance.getOnlinePlayers().size(); this.maxPlayers = serverInstance.getMaxPlayers(); ArrayList<String> tmpPlayers = new ArrayList(); for (Player p : serverInstance.getOnlinePlayers()) { tmpPlayers.add(p.getName()); } this.players = tmpPlayers; ArrayList<String> tmpPlugins = new ArrayList(); for (Plugin p : serverInstance.getPluginManager().getPlugins()) { tmpPlugins.add(p.getDescription().getFullName()); } this.plugins = tmpPlugins; ArrayList<String> tmpWorlds = new ArrayList(); for (World w : serverInstance.getWorlds()) { tmpWorlds.add(w.getName()); } this.worlds = tmpWorlds; events = new EventReport(Statistician.getEventListener()); } @Override public String toString() { try { return Statistician.getJsonMapper().writeValueAsString(this); } catch (IOException ex) { StatisticianLogger.getLog().log(Level.SEVERE, ex.getMessage(), ex); } return null; } public String toPrettyString() { try { Statistician.getJsonMapper().configure(SerializationConfig.Feature.INDENT_OUTPUT, true); String string = Statistician.getJsonMapper().writeValueAsString(this); Statistician.getJsonMapper().configure(SerializationConfig.Feature.INDENT_OUTPUT, false); return string; } catch (IOException ex) { StatisticianLogger.getLog().log(Level.SEVERE, ex.getMessage(), ex); } return null; } public DBObject toDBObject() { return (DBObject) JSON.parse(toString()); } }