PageRenderTime 46ms CodeModel.GetById 15ms app.highlight 24ms RepoModel.GetById 4ms app.codeStats 0ms

/worldedit-core/src/main/java/com/sk89q/worldedit/command/SnapshotCommands.java

https://gitlab.com/Skull3x/WorldEdit
Java | 272 lines | 207 code | 41 blank | 24 comment | 34 complexity | af6c613496b4ddbaad1710f0b2e8bbdf MD5 | raw file
  1/*
  2 * WorldEdit, a Minecraft world manipulation toolkit
  3 * Copyright (C) sk89q <http://www.sk89q.com>
  4 * Copyright (C) WorldEdit team and contributors
  5 *
  6 * This program is free software: you can redistribute it and/or modify it
  7 * under the terms of the GNU Lesser General Public License as published by the
  8 * Free Software Foundation, either version 3 of the License, or
  9 * (at your option) any later version.
 10 *
 11 * This program is distributed in the hope that it will be useful, but WITHOUT
 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 14 * for more details.
 15 *
 16 * You should have received a copy of the GNU Lesser General Public License
 17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 18 */
 19
 20// $Id$
 21
 22package com.sk89q.worldedit.command;
 23
 24import com.sk89q.minecraft.util.commands.Command;
 25import com.sk89q.minecraft.util.commands.CommandContext;
 26import com.sk89q.minecraft.util.commands.CommandPermissions;
 27import com.sk89q.worldedit.*;
 28import com.sk89q.worldedit.entity.Player;
 29import com.sk89q.worldedit.world.snapshot.InvalidSnapshotException;
 30import com.sk89q.worldedit.world.snapshot.Snapshot;
 31import com.sk89q.worldedit.world.storage.MissingWorldException;
 32
 33import java.io.File;
 34import java.io.IOException;
 35import java.text.DateFormat;
 36import java.text.SimpleDateFormat;
 37import java.util.Calendar;
 38import java.util.List;
 39import java.util.logging.Logger;
 40
 41/**
 42 * Snapshot commands.
 43 */
 44public class SnapshotCommands {
 45
 46    private static final Logger logger = Logger.getLogger("Minecraft.WorldEdit");
 47    private static final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
 48    
 49    private final WorldEdit we;
 50
 51    public SnapshotCommands(WorldEdit we) {
 52        this.we = we;
 53    }
 54
 55    @Command(
 56            aliases = { "list" },
 57            usage = "[num]",
 58            desc = "List snapshots",
 59            min = 0,
 60            max = 1
 61    )
 62    @CommandPermissions("worldedit.snapshots.list")
 63    public void list(Player player, LocalSession session, EditSession editSession, CommandContext args) throws WorldEditException {
 64
 65        LocalConfiguration config = we.getConfiguration();
 66
 67        if (config.snapshotRepo == null) {
 68            player.printError("Snapshot/backup restore is not configured.");
 69            return;
 70        }
 71
 72        try {
 73            List<Snapshot> snapshots = config.snapshotRepo.getSnapshots(true, player.getWorld().getName());
 74
 75            if (!snapshots.isEmpty()) {
 76
 77                int num = args.argsLength() > 0 ? Math.min(40, Math.max(5, args.getInteger(0))) : 5;
 78
 79                player.print("Snapshots for world: '" + player.getWorld().getName() + "'");
 80                for (byte i = 0; i < Math.min(num, snapshots.size()); i++) {
 81                    player.print((i + 1) + ". " + snapshots.get(i).getName());
 82                }
 83
 84                player.print("Use /snap use [snapshot] or /snap use latest.");
 85            } else {
 86                player.printError("No snapshots are available. See console for details.");
 87
 88                // Okay, let's toss some debugging information!
 89                File dir = config.snapshotRepo.getDirectory();
 90
 91                try {
 92                    logger.info("WorldEdit found no snapshots: looked in: "
 93                            + dir.getCanonicalPath());
 94                } catch (IOException e) {
 95                    logger.info("WorldEdit found no snapshots: looked in "
 96                            + "(NON-RESOLVABLE PATH - does it exist?): "
 97                            + dir.getPath());
 98                }
 99            }
100        } catch (MissingWorldException ex) {
101            player.printError("No snapshots were found for this world.");
102        }
103    }
104
105    @Command(
106            aliases = { "use" },
107            usage = "<snapshot>",
108            desc = "Choose a snapshot to use",
109            min = 1,
110            max = 1
111    )
112    @CommandPermissions("worldedit.snapshots.restore")
113    public void use(Player player, LocalSession session, EditSession editSession, CommandContext args) throws WorldEditException {
114
115        LocalConfiguration config = we.getConfiguration();
116
117        if (config.snapshotRepo == null) {
118            player.printError("Snapshot/backup restore is not configured.");
119            return;
120        }
121
122        String name = args.getString(0);
123
124        // Want the latest snapshot?
125        if (name.equalsIgnoreCase("latest")) {
126            try {
127                Snapshot snapshot = config.snapshotRepo.getDefaultSnapshot(player.getWorld().getName());
128
129                if (snapshot != null) {
130                    session.setSnapshot(null);
131                    player.print("Now using newest snapshot.");
132                } else {
133                    player.printError("No snapshots were found.");
134                }
135            } catch (MissingWorldException ex) {
136                player.printError("No snapshots were found for this world.");
137            }
138        } else {
139            try {
140                session.setSnapshot(config.snapshotRepo.getSnapshot(name));
141                player.print("Snapshot set to: " + name);
142            } catch (InvalidSnapshotException e) {
143                player.printError("That snapshot does not exist or is not available.");
144            }
145        }
146    }
147
148    @Command(
149            aliases = { "sel" },
150            usage = "<index>",
151            desc = "Choose the snapshot based on the list id",
152            min = 1,
153            max = 1
154    )
155    @CommandPermissions("worldedit.snapshots.restore")
156    public void sel(Player player, LocalSession session, EditSession editSession, CommandContext args) throws WorldEditException {
157        LocalConfiguration config = we.getConfiguration();
158
159        if (config.snapshotRepo == null) {
160            player.printError("Snapshot/backup restore is not configured.");
161            return;
162        }
163
164        int index = -1;
165        try {
166            index = Integer.parseInt(args.getString(0));
167        } catch (NumberFormatException e) {
168            player.printError("Invalid index, " + args.getString(0) + " is not a valid integer.");
169            return;
170        }
171
172        if (index < 1) {
173            player.printError("Invalid index, must be equal or higher then 1.");
174            return;
175        }
176
177        try {
178            List<Snapshot> snapshots = config.snapshotRepo.getSnapshots(true, player.getWorld().getName());
179            if (snapshots.size() < index) {
180                player.printError("Invalid index, must be between 1 and " + snapshots.size() + ".");
181                return;
182            }
183            Snapshot snapshot = snapshots.get(index - 1);
184            if (snapshot == null) {
185                player.printError("That snapshot does not exist or is not available.");
186                return;
187            }
188            session.setSnapshot(snapshot);
189            player.print("Snapshot set to: " + snapshot.getName());
190        } catch (MissingWorldException e) {
191            player.printError("No snapshots were found for this world.");
192        }
193    }
194
195    @Command(
196            aliases = { "before" },
197            usage = "<date>",
198            desc = "Choose the nearest snapshot before a date",
199            min = 1,
200            max = -1
201    )
202    @CommandPermissions("worldedit.snapshots.restore")
203    public void before(Player player, LocalSession session, EditSession editSession, CommandContext args) throws WorldEditException {
204
205        LocalConfiguration config = we.getConfiguration();
206
207        if (config.snapshotRepo == null) {
208            player.printError("Snapshot/backup restore is not configured.");
209            return;
210        }
211
212        Calendar date = session.detectDate(args.getJoinedStrings(0));
213
214        if (date == null) {
215            player.printError("Could not detect the date inputted.");
216        } else {
217            try {
218                Snapshot snapshot = config.snapshotRepo.getSnapshotBefore(date, player.getWorld().getName());
219
220                if (snapshot == null) {
221                    dateFormat.setTimeZone(session.getTimeZone());
222                    player.printError("Couldn't find a snapshot before "
223                            + dateFormat.format(date.getTime()) + ".");
224                } else {
225                    session.setSnapshot(snapshot);
226                    player.print("Snapshot set to: " + snapshot.getName());
227                }
228            } catch (MissingWorldException ex) {
229                player.printError("No snapshots were found for this world.");
230            }
231        }
232    }
233
234    @Command(
235            aliases = { "after" },
236            usage = "<date>",
237            desc = "Choose the nearest snapshot after a date",
238            min = 1,
239            max = -1
240    )
241    @CommandPermissions("worldedit.snapshots.restore")
242    public void after(Player player, LocalSession session, EditSession editSession, CommandContext args) throws WorldEditException {
243
244        LocalConfiguration config = we.getConfiguration();
245
246        if (config.snapshotRepo == null) {
247            player.printError("Snapshot/backup restore is not configured.");
248            return;
249        }
250
251        Calendar date = session.detectDate(args.getJoinedStrings(0));
252
253        if (date == null) {
254            player.printError("Could not detect the date inputted.");
255        } else {
256            try {
257                Snapshot snapshot = config.snapshotRepo.getSnapshotAfter(date, player.getWorld().getName());
258                if (snapshot == null) {
259                    dateFormat.setTimeZone(session.getTimeZone());
260                    player.printError("Couldn't find a snapshot after "
261                            + dateFormat.format(date.getTime()) + ".");
262                } else {
263                    session.setSnapshot(snapshot);
264                    player.print("Snapshot set to: " + snapshot.getName());
265                }
266            } catch (MissingWorldException ex) {
267                player.printError("No snapshots were found for this world.");
268            }
269        }
270    }
271
272}