[trunk] New mechanism for upgrading config files. This will be extended in the future.

New worth.yml autogenerated from the previous values.

git-svn-id: https://svn.java.net/svn/essentials~svn/trunk@1215 e251c2fe-e539-e718-e476-b85c1f46cddb
This commit is contained in:
snowleo 2011-04-16 13:36:09 +00:00
parent aaae6264cb
commit d311fc126f
3 changed files with 274 additions and 171 deletions

View File

@ -51,7 +51,6 @@ public class Essentials extends JavaPlugin
public Essentials() throws IOException public Essentials() throws IOException
{ {
loadClasses();
} }
public static void ensureEnabled(Server server) public static void ensureEnabled(Server server)
@ -62,10 +61,6 @@ public class Essentials extends JavaPlugin
pm.enablePlugin(ess); pm.enablePlugin(ess);
} }
public static void loadClasses()
{
}
public static Essentials getStatic() public static Essentials getStatic()
{ {
return staticThis; return staticThis;
@ -100,6 +95,7 @@ public class Essentials extends JavaPlugin
public void onEnable() public void onEnable()
{ {
setStatic(); setStatic();
new EssentialsUpgrade(this.getDescription().getVersion(), this.getDataFolder());
confList = new ArrayList<IConf>(); confList = new ArrayList<IConf>();
settings = new Settings(this.getDataFolder()); settings = new Settings(this.getDataFolder());
confList.add(settings); confList.add(settings);

View File

@ -0,0 +1,74 @@
package com.earth2me.essentials;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
public class EssentialsUpgrade {
private static boolean alreadyRun = false;
private final static Logger logger = Logger.getLogger("Minecraft");
EssentialsUpgrade(String version, File dataFolder) {
if (alreadyRun == true) return;
alreadyRun = true;
moveWorthValuesToWorthYml(dataFolder);
}
private void moveWorthValuesToWorthYml(File dataFolder) {
try {
File configFile = new File(dataFolder, "config.yml");
EssentialsConf conf = new EssentialsConf(configFile);
conf.load();
Worth w = new Worth(dataFolder);
for (Material mat : Material.values()) {
int id = mat.getId();
double value = conf.getDouble("worth-"+id, Double.NaN);
if (!Double.isNaN(value)) {
w.setPrice(new ItemStack(mat, 1, (short)0, (byte)0), value);
}
}
removeLinesFromConfig(configFile,"\\s*#?\\s*worth-[0-9]+.*", "# Worth values have been moved to worth.yml");
} catch (Throwable e) {
logger.log(Level.WARNING, "Error while upgrading the files", e);
}
}
private void removeLinesFromConfig(File file, String regex, String info) throws Exception {
boolean needUpdate = false;
BufferedReader br = new BufferedReader(new FileReader(file));
File tempFile = File.createTempFile("essentialsupgrade", ".yml");
BufferedWriter bw = new BufferedWriter(new FileWriter(tempFile));
do {
String line = br.readLine();
if (line == null) break;
if (line.matches(regex)) {
if (needUpdate == false && info != null) {
bw.write(info, 0, info.length());
bw.newLine();
}
needUpdate = true;
} else {
if (line.endsWith("\r\n")) {
bw.write(line, 0, line.length() - 2);
} else if (line.endsWith("\r") || line.endsWith("\n")) {
bw.write(line, 0, line.length() - 1);
} else {
bw.write(line, 0, line.length());
}
bw.newLine();
}
} while(true);
br.close();
bw.close();
if (needUpdate) {
file.renameTo(new File(file.getParentFile(), file.getName().concat("."+System.currentTimeMillis()+".upgraded")));
tempFile.renameTo(file);
}
}
}

View File

@ -1,166 +1,199 @@
worth-1: 2 worth:
worth-2: 1 stonebutton:
worth-3: 1 '0': 7.0
worth-4: 1 wood: 1.0
worth-5: 1 arrow: 10.0
worth-6: 2 diamondpickaxe: 1000.0
worth-7: 1000 rawfish: 5.0
worth-8: 1 minecart: 20.0
worth-9: 1 leatherchestplate: 40.0
worth-10: 1 storageminecart: 35.0
worth-11: 1 leaves:
worth-12: 1 '0': 0.0
worth-13: 1 feather: 3.0
worth-14: 45 goldchestplate: 600.0
worth-15: 18 mushroomsoup: 30.0
worth-16: 15 bread: 20.0
worth-17: 2 stationarywater:
worth-18: 0 '0': 1.0
worth-19: 80 workbench: 20.0
worth-20: 10 stonehoe: 40.0
worth-21: 100 brownmushroom: 2.0
worth-22: 500 wool:
worth-23: 9 '0': 8.0
worth-24: 5 mossycobblestone: 90.0
worth-25: 40 diamondhoe: 600.0
worth-35: 8 woodsword: 10.0
worth-37: 2 torch:
worth-38: 2 '0': 1.0
worth-39: 2 grass: 1.0
worth-40: 2 poweredminecart: 30.0
worth-41: 400 snowball: 1.0
worth-42: 160 goldenapple: 500.0
worth-43: 3 leatherleggings: 20.0
worth-44: 3 log:
worth-45: 40 '0': 2.0
worth-46: 10000 diamondaxe: 1000.0
worth-47: 20 slimeball: 50.0
worth-48: 90 fence: 10.0
worth-49: 130 stonespade: 40.0
worth-50: 1 claybrick: 10.0
worth-53: 8 noteblock: 40.0
worth-54: 15 ironaxe: 60.0
worth-55: 7 coalore: 15.0
worth-56: 200 clayball: 8.0
worth-57: 1500 fishingrod: 25.0
worth-58: 20 ironhoe: 60.0
worth-60: 3 goldrecord: 950.0
worth-61: 10 ironpickaxe: 80.0
worth-65: 10 irondoor: 35.0
worth-66: 40 bucket: 10.0
worth-67: 22 redrose: 2.0
worth-68: 1 grilledpork: 6.0
worth-69: 7 gravel: 1.0
worth-70: 10 wooddoor: 30.0
worth-71: 15 chainmailhelmet: 40.0
worth-72: 10 ironchestplate: 300.0
worth-73: 30 diamondblock: 1500.0
worth-74: 30 diamondhelmet: 1500.0
worth-76: 10 goldhelmet: 300.0
worth-77: 7 redstonetorchon:
worth-81: 10 '0': 10.0
worth-82: 3 ironspade: 40.0
worth-83: 15 furnace:
worth-85: 10 '0': 10.0
worth-86: 50 ironsword: 60.0
worth-91: 60 dispenser:
worth-256: 40 '0': 9.0
worth-257: 80 woodaxe: 10.0
worth-258: 60 seeds: 5.0
worth-260: 10 painting: 50.0
worth-261: 75 woodplate:
worth-262: 10 '0': 10.0
worth-263: 3 redstoneore: 30.0
worth-264: 230 diamondspade: 350.0
worth-265: 20 waterbucket: 10.0
worth-266: 50 water:
worth-267: 60 '0': 1.0
worth-268: 10 bedrock: 1000.0
worth-269: 10 irondoorblock:
worth-270: 10 '0': 15.0
worth-271: 10 goldhoe: 200.0
worth-272: 40 sand: 1.0
worth-273: 40 goldsword: 200.0
worth-274: 40 stoneaxe: 40.0
worth-275: 40 bookshelf: 20.0
worth-276: 700 ironblock: 160.0
worth-277: 350 jackolantern:
worth-278: 1000 '0': 60.0
worth-279: 1000 boat: 5.0
worth-280: 1 diamondchestplate: 3000.0
worth-281: 6 redstonewire:
worth-282: 30 '0': 7.0
worth-283: 200 redmushroom: 2.0
worth-284: 220 string: 5.0
worth-285: 300 stoneplate:
worth-286: 300 '0': 10.0
worth-287: 5 wallsign:
worth-288: 3 '0': 1.0
worth-289: 19 cactus:
worth-290: 10 '0': 10.0
worth-291: 40 sulphur: 19.0
worth-292: 60 rails:
worth-293: 600 '0': 40.0
worth-294: 200 ironore: 18.0
worth-295: 5 leatherhelmet: 20.0
worth-296: 9 stone: 2.0
worth-297: 20 egg: 1.0
worth-298: 20 diamondore: 200.0
worth-299: 40 woodhoe: 10.0
worth-300: 20 goldleggings: 400.0
worth-301: 17 chainmailleggings: 50.0
worth-302: 40 yellowflower: 2.0
worth-303: 40 ironhelmet: 120.0
worth-304: 50 obsidian: 130.0
worth-305: 30 dirt: 1.0
worth-306: 120 leather: 10.0
worth-307: 300 leatherboots: 17.0
worth-308: 250 lever:
worth-309: 50 '0': 7.0
worth-310: 1500 cobblestone: 1.0
worth-311: 3000 cake: 100.0
worth-312: 2200 woodstairs:
worth-313: 1500 '0': 8.0
worth-314: 300 ironingot: 20.0
worth-315: 600 goldore: 45.0
worth-316: 400 pumpkin:
worth-317: 250 '0': 50.0
worth-320: 6 bed: 25.0
worth-321: 50 watch: 100.0
worth-322: 500 ironleggings: 250.0
worth-323: 10 sign: 10.0
worth-324: 30 doublestep:
worth-325: 10 '0': 3.0
worth-326: 10 woodpickaxe: 10.0
worth-327: 30 stonepickaxe: 40.0
worth-328: 20 chainmailboots: 30.0
worth-329: 100 diamondleggings: 2200.0
worth-330: 35 cookedfish: 20.0
worth-331: 1 saddle: 100.0
worth-332: 1 cobblestonestairs:
worth-333: 5 '0': 22.0
worth-334: 10 tnt: 10000.0
worth-335: 15 glowingredstoneore: 30.0
worth-336: 10 apple: 10.0
worth-337: 8 woodspade: 10.0
worth-338: 10 goldingot: 50.0
worth-339: 30 diode: 10.0
worth-340: 35 soil:
worth-341: 50 '0': 3.0
worth-342: 35 clay: 3.0
worth-343: 30 goldblock: 400.0
worth-344: 1 stick: 1.0
worth-345: 50 paper: 30.0
worth-346: 25 brick: 40.0
worth-347: 100 stationarylava:
worth-348: 11 '0': 1.0
worth-349: 5 chest: 15.0
worth-350: 20 sandstone: 5.0
worth-351: 10 goldpickaxe: 300.0
worth-352: 10 compass: 50.0
worth-353: 5 sugarcane: 10.0
worth-354: 100 diamondsword: 700.0
worth-355: 25 goldboots: 250.0
worth-356: 10 sponge: 80.0
worth-2256: 950 stonesword: 40.0
worth-2257: 1000 coal:
'0': 3.0
goldaxe: 300.0
bone: 10.0
diamond: 230.0
glass: 10.0
goldspade: 220.0
lapisblock: 500.0
lavabucket: 30.0
wheat: 9.0
ladder:
'0': 10.0
sugarcaneblock:
'0': 15.0
bowl: 6.0
chainmailchestplate: 40.0
sapling:
'0': 2.0
diamondboots: 1500.0
lapisore: 100.0
lava:
'0': 1.0
milkbucket: 15.0
redstone: 1.0
greenrecord: 1000.0
inksack:
'0': 10.0
glowstonedust: 11.0
book: 35.0
bow: 75.0
ironboots: 50.0
step:
'0': 3.0
sugar: 5.0