Search in sources :

Example 1 with PlotBlockData

use of com.palmergames.bukkit.towny.regen.PlotBlockData in project Towny by ElgarL.

the class TownyFlatFileSource method loadRegenList.

@Override
public boolean loadRegenList() {
    TownyMessaging.sendDebugMsg("Loading Regen List");
    String line;
    BufferedReader fin = null;
    String[] split;
    PlotBlockData plotData;
    try {
        fin = new BufferedReader(new FileReader(rootFolder + dataFolder + FileMgmt.fileSeparator() + "regen.txt"));
        while ((line = fin.readLine()) != null) if (!line.equals("")) {
            split = line.split(",");
            plotData = loadPlotData(split[0], Integer.parseInt(split[1]), Integer.parseInt(split[2]));
            if (plotData != null) {
                TownyRegenAPI.addPlotChunk(plotData, false);
            }
        }
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    } finally {
        if (fin != null) {
            try {
                fin.close();
            } catch (IOException ignore) {
            }
        }
    }
}
Also used : BufferedReader(java.io.BufferedReader) PlotBlockData(com.palmergames.bukkit.towny.regen.PlotBlockData) FileReader(java.io.FileReader) IOException(java.io.IOException) NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) IOException(java.io.IOException) EOFException(java.io.EOFException) FileNotFoundException(java.io.FileNotFoundException) AlreadyRegisteredException(com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException) InvalidNameException(javax.naming.InvalidNameException) TownyException(com.palmergames.bukkit.towny.exceptions.TownyException)

Example 2 with PlotBlockData

use of com.palmergames.bukkit.towny.regen.PlotBlockData in project Towny by ElgarL.

the class TownyDatabaseHandler method removeTownBlock.

@Override
public void removeTownBlock(TownBlock townBlock) {
    Resident resident = null;
    Town town = null;
    try {
        resident = townBlock.getResident();
    } catch (NotRegisteredException e) {
    }
    try {
        town = townBlock.getTown();
    } catch (NotRegisteredException e) {
    }
    TownyWorld world = townBlock.getWorld();
    world.removeTownBlock(townBlock);
    saveWorld(world);
    deleteTownBlock(townBlock);
    saveTownBlockList();
    if (resident != null)
        saveResident(resident);
    if (town != null)
        saveTown(town);
    if (townBlock.getWorld().isUsingPlotManagementDelete())
        TownyRegenAPI.addDeleteTownBlockIdQueue(townBlock.getWorldCoord());
    // Move the plot to be restored
    if (townBlock.getWorld().isUsingPlotManagementRevert()) {
        PlotBlockData plotData = TownyRegenAPI.getPlotChunkSnapshot(townBlock);
        if (plotData != null && !plotData.getBlockList().isEmpty()) {
            TownyRegenAPI.addPlotChunk(plotData, true);
        }
    }
    universe.setChangedNotify(REMOVE_TOWN_BLOCK);
}
Also used : PlotBlockData(com.palmergames.bukkit.towny.regen.PlotBlockData)

Example 3 with PlotBlockData

use of com.palmergames.bukkit.towny.regen.PlotBlockData in project Towny by ElgarL.

the class TownCommand method newTown.

public Town newTown(TownyWorld world, String name, Resident resident, Coord key, Location spawn) throws TownyException {
    world.newTownBlock(key);
    TownyUniverse.getDataSource().newTown(name);
    Town town = TownyUniverse.getDataSource().getTown(name);
    town.addResident(resident);
    town.setMayor(resident);
    TownBlock townBlock = world.getTownBlock(key);
    townBlock.setTown(town);
    town.setHomeBlock(townBlock);
    // Set the plot permissions to mirror the towns.
    townBlock.setType(townBlock.getType());
    town.setSpawn(spawn);
    if (world.isUsingPlotManagementRevert()) {
        PlotBlockData plotChunk = TownyRegenAPI.getPlotChunk(townBlock);
        if (plotChunk != null) {
            // just claimed so stop regeneration.
            TownyRegenAPI.deletePlotChunk(plotChunk);
        } else {
            // Not regenerating so create a new snapshot.
            plotChunk = new PlotBlockData(townBlock);
            plotChunk.initialize();
        }
        // Save a snapshot.
        TownyRegenAPI.addPlotChunkSnapshot(plotChunk);
        plotChunk = null;
    }
    TownyMessaging.sendDebugMsg("Creating new Town account: " + "town-" + name);
    if (TownySettings.isUsingEconomy()) {
        // TODO
        try {
            town.setBalance(0, "Deleting Town");
        } catch (EconomyException e) {
            e.printStackTrace();
        }
    }
    TownyUniverse.getDataSource().saveResident(resident);
    TownyUniverse.getDataSource().saveTownBlock(townBlock);
    TownyUniverse.getDataSource().saveTown(town);
    TownyUniverse.getDataSource().saveWorld(world);
    TownyUniverse.getDataSource().saveTownList();
    TownyUniverse.getDataSource().saveTownBlockList();
    // Reset cache permissions for anyone in this TownBlock
    plugin.updateCache(townBlock.getWorldCoord());
    BukkitTools.getPluginManager().callEvent(new NewTownEvent(town));
    return town;
}
Also used : PlotBlockData(com.palmergames.bukkit.towny.regen.PlotBlockData) NewTownEvent(com.palmergames.bukkit.towny.event.NewTownEvent)

Example 4 with PlotBlockData

use of com.palmergames.bukkit.towny.regen.PlotBlockData in project Towny by ElgarL.

the class TownyFlatFileSource method loadPlotData.

/**
	 * Load PlotBlockData for regen at unclaim
	 * 
	 * @param townBlock
	 * @return PlotBlockData or null
	 */
@Override
public PlotBlockData loadPlotData(TownBlock townBlock) {
    String fileName = getPlotFilename(townBlock);
    int value;
    if (isFile(fileName)) {
        PlotBlockData plotBlockData = new PlotBlockData(townBlock);
        List<Integer> IntArr = new ArrayList<Integer>();
        int version = 0;
        DataInputStream fin = null;
        try {
            fin = new DataInputStream(new FileInputStream(fileName));
            //read the first 3 characters to test for version info
            byte[] key = new byte[3];
            fin.read(key, 0, 3);
            String test = new String(key);
            switch(elements.fromString(test)) {
                case VER:
                    // Read the file version
                    version = fin.read();
                    plotBlockData.setVersion(version);
                    // next entry is the plot height
                    plotBlockData.setHeight(fin.readInt());
                    break;
                default:
                    /*
					 * no version field so set height
					 * and push rest to queue
					 */
                    plotBlockData.setVersion(version);
                    // First entry is the plot height
                    plotBlockData.setHeight(key[0]);
                    IntArr.add((int) key[1]);
                    IntArr.add((int) key[2]);
            }
            /*
				 * Load plot block data based upon the stored version number.
				 */
            switch(version) {
                default:
                case 1:
                    // load remainder of file
                    while ((value = fin.read()) >= 0) {
                        IntArr.add(value);
                    }
                    break;
                case 2:
                    // load remainder of file
                    while ((value = fin.readInt()) >= 0) {
                        IntArr.add(value);
                    }
                    break;
            }
        } catch (EOFException e) {
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fin != null) {
                try {
                    fin.close();
                } catch (IOException ignore) {
                }
            }
        }
        plotBlockData.setBlockList(IntArr);
        plotBlockData.resetBlockListRestored();
        return plotBlockData;
    }
    return null;
}
Also used : ArrayList(java.util.ArrayList) EOFException(java.io.EOFException) PlotBlockData(com.palmergames.bukkit.towny.regen.PlotBlockData) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream)

Example 5 with PlotBlockData

use of com.palmergames.bukkit.towny.regen.PlotBlockData in project Towny by ElgarL.

the class RepeatingTimerTask method run.

@Override
public void run() {
    // Perform a single block regen in each regen area, if any are left to do.
    if (TownyRegenAPI.hasPlotChunks()) {
        // only execute if the correct amount of time has passed.
        if (Math.max(1L, TownySettings.getPlotManagementSpeed()) >= ++timerCounter) {
            for (PlotBlockData plotChunk : new ArrayList<PlotBlockData>(TownyRegenAPI.getPlotChunks().values())) {
                if (!plotChunk.restoreNextBlock()) {
                    TownyRegenAPI.deletePlotChunk(plotChunk);
                    TownyRegenAPI.deletePlotChunkSnapshot(plotChunk);
                }
            }
            timerCounter = 0L;
        }
    }
    // Take a snapshot of the next townBlock and save.
    if (TownyRegenAPI.hasWorldCoords()) {
        try {
            TownBlock townBlock = TownyRegenAPI.getWorldCoord().getTownBlock();
            PlotBlockData plotChunk = new PlotBlockData(townBlock);
            // Create a new snapshot.
            plotChunk.initialize();
            if (!plotChunk.getBlockList().isEmpty() && !(plotChunk.getBlockList() == null))
                // Save the snapshot.
                TownyRegenAPI.addPlotChunkSnapshot(plotChunk);
            plotChunk = null;
            townBlock.setLocked(false);
            TownyUniverse.getDataSource().saveTownBlock(townBlock);
            plugin.updateCache(townBlock.getWorldCoord());
            if (!TownyRegenAPI.hasWorldCoords())
                TownyLogger.log.info("Plot snapshots completed.");
        } catch (NotRegisteredException e) {
        // Not a townblock so ignore.
        }
    }
    // Perform the next plot_management block_delete
    if (TownyRegenAPI.hasDeleteTownBlockIdQueue()) {
        TownyRegenAPI.doDeleteTownBlockIds(TownyRegenAPI.getDeleteTownBlockIdQueue());
    }
}
Also used : NotRegisteredException(com.palmergames.bukkit.towny.exceptions.NotRegisteredException) ArrayList(java.util.ArrayList) PlotBlockData(com.palmergames.bukkit.towny.regen.PlotBlockData) TownBlock(com.palmergames.bukkit.towny.object.TownBlock)

Aggregations

PlotBlockData (com.palmergames.bukkit.towny.regen.PlotBlockData)5 NotRegisteredException (com.palmergames.bukkit.towny.exceptions.NotRegisteredException)2 EOFException (java.io.EOFException)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 NewTownEvent (com.palmergames.bukkit.towny.event.NewTownEvent)1 AlreadyRegisteredException (com.palmergames.bukkit.towny.exceptions.AlreadyRegisteredException)1 TownyException (com.palmergames.bukkit.towny.exceptions.TownyException)1 TownBlock (com.palmergames.bukkit.towny.object.TownBlock)1 BufferedReader (java.io.BufferedReader)1 DataInputStream (java.io.DataInputStream)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 FileReader (java.io.FileReader)1 InvalidNameException (javax.naming.InvalidNameException)1