net.visualillusionsent.newu.StationTracker.java Source code

Java tutorial

Introduction

Here is the source code for net.visualillusionsent.newu.StationTracker.java

Source

/*
 * This file is part of NewU.
 *
 * Copyright  2014-2014 Visual Illusions Entertainment
 *
 * NewU is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License v3 as published by
 * the Free Software Foundation, either version 3 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 v3 for more details.
 *
 * You should have received a copy of the GNU General Public License v3 along with this program.
 * If not, see http://www.gnu.org/licenses/gpl.html.
 */
package net.visualillusionsent.newu;

import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import net.canarymod.api.entity.living.humanoid.Player;
import net.canarymod.api.world.position.Location;
import net.visualillusionsent.utils.JarUtils;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.jar.JarFile;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;

/**
 * @author Jason (darkdiplomat)
 */
final class StationTracker {
    private final Random randy = new Random();
    private final Map<String, NewUStation> stations = new ConcurrentHashMap<String, NewUStation>();
    private final List<String> respawnMessages;
    private final Logger logger;

    StationTracker(NewU newU) {
        logger = newU.getPluginLogger();
        loadStations();
        respawnMessages = Collections.unmodifiableList(loadMessages());
    }

    final boolean addStation(NewUStation station) {
        // Check that we are beyond 50 blocks from another station
        synchronized (stations) {
            for (NewUStation preExisting : stations.values()) {
                if (preExisting.getStationLocation().getDistance(station.getStationLocation()) < 50) {
                    return false;
                } else if (preExisting.getName().equals(station.getName())) {
                    return false;
                }
            }
        }
        stations.put(station.getName(), station);
        storeStations();
        return true;
    }

    final boolean removeStation(String name) {
        NewUStation station = stations.remove(name);
        storeStations();
        return station != null;
    }

    Location getClosestRespawn(Player player) {
        double distance = -1;
        NewUStation going = null;

        synchronized (stations) {
            for (NewUStation station : stations.values()) {
                if (station.hasDiscovered(player)) {
                    double dist = station.distanceFrom(player);
                    if (distance == -1) {
                        distance = dist;
                        going = station;
                    } else if (dist < distance) {
                        distance = dist;
                        going = station;
                    }
                }
            }
        }
        return going != null ? going.getRespawnLocation() : null;
    }

    NewUStation getClosestStation(Player player) {
        double distance = -1;
        NewUStation closest = null;

        synchronized (stations) {
            for (NewUStation station : stations.values()) {
                double dist = station.distanceFrom(player);
                if (distance == -1) {
                    distance = dist;
                    closest = station;
                } else if (dist < distance) {
                    distance = dist;
                    closest = station;
                }
            }
        }
        if (closest != null && closest.distanceFrom(player) > 25) {
            return null;
        }
        return closest;
    }

    final String getRandomMessage() {
        return respawnMessages.get(randy.nextInt(respawnMessages.size()));
    }

    private ArrayList<String> loadMessages() {
        ArrayList<String> temp = new ArrayList<String>();
        Scanner scanner = null;
        try {
            JarFile disJar = new JarFile(JarUtils.getJarPath(getClass()));
            ZipEntry entry = disJar.getEntry("resources/lang/en_US.lang");
            scanner = new Scanner(disJar.getInputStream(entry));
            while (scanner.hasNextLine()) {
                temp.add(scanner.nextLine());
            }
        } catch (Exception ex) {
            logger.log(Level.SEVERE, "Failed to load reconstruction messages...");
        } finally {
            if (scanner != null) {
                scanner.close();
            }
        }
        return temp;
    }

    final boolean storeStations() {
        boolean failure = false;
        File stationsJSON = new File(NewU.cfgDir, "stations.json.tmp"); //Store to temp first
        JsonWriter writer = null;
        PrintWriter pWriter = null;
        try {
            stationsJSON.createNewFile();
            pWriter = new PrintWriter(stationsJSON);
            writer = new JsonWriter(pWriter);
            writer.beginObject(); // Master Object
            pWriter.println();
            for (NewUStation station : stations.values()) {
                pWriter.print("\t");
                writer.name("Station");
                writer.beginObject(); // Station object
                writer.name("Name");
                writer.value(station.getName());
                writer.name("Location");
                writer.beginObject(); // Location Object
                writer.name("World");
                Location loc = station.getStationLocation();
                writer.value(loc.getWorldName());
                writer.name("Dimension");
                writer.value(loc.getType().getName());
                writer.name("X");
                writer.value(loc.getBlockX());
                writer.name("Y");
                writer.value(loc.getBlockY());
                writer.name("Z");
                writer.value(loc.getBlockZ());
                writer.endObject(); // End Location
                writer.name("Discoverers");
                writer.beginArray(); // Discoverers Array
                for (String discoverer : station.discoverers()) {
                    writer.value(discoverer);
                }
                writer.endArray(); // End Discoverers
                writer.endObject(); // End Station
                pWriter.println();
            }
            writer.endObject(); // End Master
        } catch (Exception ex) {
            logger.log(Level.SEVERE, "Failed to store stations...");
            failure = true;
        } finally {
            try {
                if (writer != null)
                    writer.close();
            } catch (IOException ioex) {
                //IGNORED
            }
            if (pWriter != null)
                pWriter.close();
        }
        if (!failure) {
            failure = !stationsJSON.renameTo(new File(NewU.cfgDir, "stations.json"));
        }

        return !failure;
    }

    private void loadStations() {
        try {
            File stationsJSON = new File(NewU.cfgDir, "stations.json");
            if (!stationsJSON.exists()) {
                stationsJSON.createNewFile();
                return;
            }

            JsonReader reader = new JsonReader(new FileReader(stationsJSON));
            reader.beginObject(); // Begin main object
            while (reader.hasNext()) {
                String name = reader.nextName();
                if (name.equals("Station")) {
                    NewUStation temp = null;
                    reader.beginObject(); // Begin Station
                    String foundName = null;
                    while (reader.hasNext()) {
                        name = reader.nextName();
                        if (name.equals("Name")) {
                            foundName = reader.nextString();
                            continue;
                        } else if (name.equals("Location")) {
                            reader.beginObject(); // Begin Location
                            temp = new NewUStation(foundName, reader); // Pass reader into NewUStation object for parsing
                            reader.endObject(); // End Location
                        } else if (name.equals("Discoverers")) {
                            reader.beginArray(); // Begin Discoverers
                            while (reader.hasNext()) {
                                if (temp != null) {
                                    temp.addDiscoverer(reader.nextString());
                                }
                            }
                            reader.endArray(); // End Discoverers
                        } else {
                            reader.skipValue(); // UNKNOWN THING
                        }
                    }
                    if (temp != null) {
                        stations.put(temp.getName(), temp);
                    }
                    reader.endObject(); //End Station
                }
            }

            reader.endObject(); // End main object
            reader.close();
        } catch (IOException e) {
            logger.log(Level.SEVERE, "Failed to load stations...");
        }
    }
}