is.brendan.WarpMarkers.WarpMarkersWarpInfo.java Source code

Java tutorial

Introduction

Here is the source code for is.brendan.WarpMarkers.WarpMarkersWarpInfo.java

Source

/*
MapMarkers Minecraft Bukkit plugin for showing Essentials warps 
and warp events on maps generated by Minecraft Overviewer.
Copyright (C) 2011 Brendan Johan Lee 
Email: brendan (at) vanntett.net
    
This program 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 2
of the License, or (at your option) any later version.
    
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
MA  02110-1301, USA.
*/

package is.brendan.WarpMarkers;

import java.util.HashMap;
import java.util.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.lang.Long;
import java.lang.Integer;
import java.text.SimpleDateFormat;
import java.io.File;

import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.util.config.Configuration;

import com.earth2me.essentials.Warps;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

/**
 * Class to hold extended information about warps. 
 *
 * @author Brendan Johan Lee - deadcyclo@vanntett.net
 */

public class WarpMarkersWarpInfo {
    private final Integer ACCESSED = 0;
    private final Integer CREATED = 1;
    private final Integer DELETED = 2;
    private Warps warps;
    private Server server;
    private File warpsFolder;
    private final WarpMarkers plugin;
    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private ArrayList<HashMap> updates = new ArrayList<HashMap>();

    public WarpMarkersWarpInfo(Warps warps, Server server, File dataFolder, WarpMarkers plugin) {
        this.warps = warps;
        this.server = server;
        this.plugin = plugin;
        warpsFolder = new File(dataFolder, "warps");
        if (!warpsFolder.exists())
            warpsFolder.mkdirs();
    }

    public JSONArray load() {
        JSONArray jsonList = new JSONArray();

        final List<String> warpNameList = new ArrayList<String>(warps.getWarpNames());
        final Iterator<String> iterator = warpNameList.iterator();

        while (iterator.hasNext()) {
            String warpName = iterator.next();
            jsonList.add(getWarp(warpName));
        }
        return jsonList;
    }

    public JSONArray getUpdates(int updatesLife) {
        JSONArray jsonList = new JSONArray();

        final Iterator<HashMap> iterator = updates.iterator();

        while (iterator.hasNext()) {
            HashMap update = iterator.next();
            if (System.currentTimeMillis() - (Long) update.get("timestamp") < updatesLife) {
                jsonList.add(getUpdate(update));
            }
        }
        return jsonList;
    }

    private JSONObject getUpdate(HashMap update) {
        JSONObject retval = new JSONObject();
        retval.put("type", (Integer) update.get("type"));
        retval.put("name", (String) update.get("name"));
        retval.put("time", (String) update.get("time"));
        retval.put("by", (String) update.get("by"));
        retval.put("uid", (Long) update.get("uid"));
        return retval;
    }

    public boolean touch(String what, String warpname, String username) {
        try {
            Location location = warps.getWarp(warpname);
            Configuration conf = getOrMakeWarpConf(warpname);
            conf.setProperty(what + "time", dateFormat.format(new Date()));
            conf.setProperty(what + "by", username);
            conf.save();
            if (what.equals("accessed")) {
                addUpdate(ACCESSED, warpname, username);
            } else {
                addUpdate(CREATED, warpname, username);
            }
        } catch (java.lang.Exception e) {
            return false; /* Invalid warpname */
        }
        return true;
    }

    public void del(String warpname, String username) {
        addUpdate(DELETED, warpname, username);
    }

    private void addUpdate(Integer type, String warpname, String username) {
        HashMap update = new HashMap();
        update.put("type", type);
        update.put("name", warpname);
        update.put("time", dateFormat.format(new Date()));
        update.put("by", username);
        update.put("uid", (Long) WarpMarkersUID.get());
        update.put("timestamp", System.currentTimeMillis());
        updates.add(update);
    }

    private Configuration getOrMakeWarpConf(String name) {
        File confFile = new File(warpsFolder, name.toLowerCase() + ".yml");
        Configuration conf = new Configuration(confFile);
        if (!confFile.exists()) {
            conf.setProperty("accessedby", "n/a");
            conf.setProperty("accessedtime", "n/a");
            conf.setProperty("createdby", "n/a");
            conf.setProperty("createdtime", "n/a");
            conf.save();
        } else {
            conf.load();
        }
        return conf;
    }

    private JSONObject getWarp(String name) {
        JSONObject retval = new JSONObject();
        try {
            Location location = warps.getWarp(name);
            Configuration conf = getOrMakeWarpConf(name);
            retval.put("name", name);
            retval.put("accessedby", conf.getProperty("accessedby"));
            retval.put("accessedtime", conf.getProperty("accessedtime"));
            retval.put("createdby", conf.getProperty("createdby"));
            retval.put("createdtime", conf.getProperty("createdtime"));
            retval.put("world", location.getWorld().getName());
            retval.put("x", location.getBlockX());
            retval.put("y", location.getBlockY());
            retval.put("z", location.getBlockZ());
        } catch (java.lang.Exception e) {
            plugin.log(Level.WARNING, "getWarp tried to load non-existing warp");
        }
        return retval;
    }
}