Added name to UUID conversion

This commit is contained in:
Norbi Peti 2018-06-27 21:59:12 +02:00
parent 362d1d823c
commit ecaecd4136
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56

View file

@ -43,7 +43,7 @@ public class InvYamlStorage extends InvConfStorage {
@Override @Override
public void load(Inventory pinv, Target target) { public void load(Inventory pinv, Target target) {
load(pinv, YamlConfiguration.loadConfiguration(getFile(pinv, target))); load(pinv, YamlConfiguration.loadConfiguration(getFile(pinv, target, false)));
} }
@Override @Override
@ -52,7 +52,11 @@ public class InvYamlStorage extends InvConfStorage {
yml.options().header("DO NOT MODIFY THIS FILE"); yml.options().header("DO NOT MODIFY THIS FILE");
store(pinv, yml); store(pinv, yml);
try { try {
yml.save(getFile(pinv, target)); File nameFile=getFile(pinv, target, false);
File uuidFile=getFile(pinv, target, true);
if(!nameFile.equals(uuidFile)) //It'd be recreated right after, still, don't remove if the same
nameFile.delete(); //Delete file with name so it doesn't get loaded again
yml.save(uuidFile);
} catch (IOException e) { } catch (IOException e) {
mod.getLog().warn("Failed to save Inventory for Player " + pinv.getPlayer().getName()); mod.getLog().warn("Failed to save Inventory for Player " + pinv.getPlayer().getName());
e.printStackTrace(); e.printStackTrace();
@ -61,19 +65,28 @@ public class InvYamlStorage extends InvConfStorage {
@Override @Override
public void remove(Inventory pinv, Target target) { public void remove(Inventory pinv, Target target) {
getFile(pinv, target).delete(); getFile(pinv, target, true).delete();
} }
@Override @Override
public boolean contains(Inventory pinv, Target target) { public boolean contains(Inventory pinv, Target target) {
return getFile(pinv, target).exists(); return getFile(pinv, target, false).exists();
} }
protected File getFile(Inventory pinv, Target target) { protected File getFile(Inventory pinv, Target target, boolean uuidonly) {
File file;
String player;
do {
player = uuidonly ? pinv.getPlayer().getUniqueId().toString() : pinv.getPlayer().getName();
if (target != default_target) { if (target != default_target) {
return new File(dir, pinv.getPlayer().getUniqueId() + "_" + target.toString().toLowerCase() + SUFFIX); file = new File(dir, player + "_" + target.toString().toLowerCase() + SUFFIX);
} else { } else {
return new File(dir, pinv.getPlayer().getUniqueId() + SUFFIX); file = new File(dir, player + SUFFIX);
} }
if(uuidonly)
return file; //Use file with UUID, even if doesn't exist
uuidonly = true; //Run again with UUID, then return...
} while(!file.exists()); //...if the file with name is not found
return file; //Found file with player name
} }
} }