Refactor code

This commit is contained in:
GeorgH93 2018-05-22 15:16:37 +02:00
parent 74d2a547be
commit b3993fcc9a
No known key found for this signature in database
GPG Key ID: D1630D37F9E4B3C8
5 changed files with 79 additions and 76 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016, 2017 GeorgH93
* Copyright (C) 2016-2018 GeorgH93
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -38,7 +38,7 @@ public interface MinepacksPlugin
static @Nullable MinepacksPlugin getInstance()
{
Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin("Minepacks");
return (plugin != null && plugin instanceof MinepacksPlugin && plugin.isEnabled()) ? (MinepacksPlugin) plugin : null;
return (plugin instanceof MinepacksPlugin && plugin.isEnabled()) ? (MinepacksPlugin) plugin : null;
}
/**
@ -50,7 +50,7 @@ public interface MinepacksPlugin
static @NotNull Version getVersion()
{
Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin("Minepacks");
if(plugin != null && plugin instanceof MinepacksPlugin && plugin.isEnabled())
if(plugin instanceof MinepacksPlugin && plugin.isEnabled())
{
return new Version(plugin.getDescription().getVersion());
}

View File

@ -215,7 +215,7 @@ public Collection<GameMode> getAllowedGameModes()
try
{
//noinspection deprecation
gm = GameMode.getByValue(Integer.valueOf(string));
gm = GameMode.getByValue(Integer.parseInt(string));
}
catch(NumberFormatException ignored) {}
if(gm == null)

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2017 GeorgH93
* Copyright (C) 2017, 2018 GeorgH93
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -19,6 +19,12 @@
import at.pcgamingfreaks.yaml.YAML;
import org.bukkit.ChatColor;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.logging.Logger;
public class OldFileUpdater
{
public static void updateConfig(YAML oldYAML, YAML newYAML)
@ -63,4 +69,69 @@ public static void updateConfig(YAML oldYAML, YAML newYAML)
newYAML.set("BackpackTitleOther", oldYAML.getString("BackpackTitle", "&b{OwnerName} Backpack").replaceAll("%s", "{OwnerName}"));
}
}
public static void updateLanguage(YAML oldYAML, YAML newYAML, Logger logger)
{
Map<String, String> simpleConverter = new LinkedHashMap<>(), advancedConverter = new LinkedHashMap<>();
String[] keys;
String helper;
for(String key : oldYAML.getKeys(true))
{
try
{
keys = key.split("\\.");
if(keys.length == 3)
{
switch(keys[1])
{
case "Console":
switch(keys[2])
{
case "NotFromConsole": advancedConverter.put("Language.NotFromConsole", ChatColor.RED + oldYAML.getString(key)); break;
}
break;
case "Ingame":
helper = keys[0] + "." + keys[1] + ".";
switch(keys[2])
{
case "NoPermission": advancedConverter.put(key, ChatColor.RED + oldYAML.getString(key)); break;
case "OwnBackPackClose": simpleConverter.put(helper + "OwnBackpackClose", key); break;
case "PlayerBackPackClose": advancedConverter.put(helper + "PlayerBackpackClose", oldYAML.getString(key).replace("%s", "{OwnerName}")); break;
case "InvalidBackpack": simpleConverter.put(key, key); break;
case "BackpackCleaned": simpleConverter.put(key, key); break;
case "Cooldown": advancedConverter.put(key, ChatColor.DARK_GREEN + oldYAML.getString(key)); break;
}
break;
case "Description":
helper = "Language.Commands.Description.";
simpleConverter.put(helper + keys[2], key);
break;
}
}
}
catch(Exception e)
{
logger.warning("Failed to convert the old \"" + key + "\" language value into the corresponding new one.");
e.printStackTrace();
}
}
// Patch them into the lang file
try
{
for(Map.Entry<String, String> entry : advancedConverter.entrySet())
{
newYAML.set(entry.getKey(), entry.getValue());
}
for(Map.Entry<String, String> entry : simpleConverter.entrySet())
{
newYAML.set(entry.getKey(), oldYAML.getString(entry.getValue()));
}
}
catch(Exception e)
{
logger.warning("Failed to write the old language values into the new language file.");
e.printStackTrace();
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 GeorgH93
* Copyright (C) 2016, 2018 GeorgH93
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -21,7 +21,6 @@
import at.pcgamingfreaks.Bukkit.ItemStackSerializer.ItemStackSerializer;
import at.pcgamingfreaks.Bukkit.ItemStackSerializer.NBTItemStackSerializer;
import org.bukkit.Bukkit;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
@ -33,8 +32,6 @@ public class InventorySerializer
public InventorySerializer()
{
String name = Bukkit.getServer().getClass().getPackage().getName();
String[] version = name.substring(name.lastIndexOf('.') + 2).split("_");
try
{
if(NBTItemStackSerializer.isMCVersionCompatible())

View File

@ -17,15 +17,11 @@
package at.pcgamingfreaks.Minepacks.Bukkit.Database;
import at.pcgamingfreaks.Minepacks.Bukkit.Database.Helper.OldFileUpdater;
import at.pcgamingfreaks.YamlFileManager;
import at.pcgamingfreaks.yaml.YAML;
import org.bukkit.ChatColor;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.LinkedHashMap;
import java.util.Map;
public class Language extends at.pcgamingfreaks.Bukkit.Language
{
private static final int LANG_VERSION = 11, UPGRADE_THRESHOLD = 11;
@ -43,68 +39,7 @@ protected void doUpgrade(YamlFileManager oldLang)
{
if(oldLang.getVersion() < 10) // Pre v2.0 versions
{
YAML oldYAML = oldLang.getYaml(), newYAML = getLang();
Map<String, String> simpleConverter = new LinkedHashMap<>(), advancedConverter = new LinkedHashMap<>();
String[] keys;
String helper;
for(String key : oldYAML.getKeys(true))
{
try
{
keys = key.split("\\.");
if(keys.length == 3)
{
switch(keys[1])
{
case "Console":
switch(keys[2])
{
case "NotFromConsole": advancedConverter.put("Language.NotFromConsole", ChatColor.RED + oldYAML.getString(key)); break;
}
break;
case "Ingame":
helper = keys[0] + "." + keys[1] + ".";
switch(keys[2])
{
case "NoPermission": advancedConverter.put(key, ChatColor.RED + oldYAML.getString(key)); break;
case "OwnBackPackClose": simpleConverter.put(helper + "OwnBackpackClose", key); break;
case "PlayerBackPackClose": advancedConverter.put(helper + "PlayerBackpackClose", oldYAML.getString(key).replace("%s", "{OwnerName}")); break;
case "InvalidBackpack": simpleConverter.put(key, key); break;
case "BackpackCleaned": simpleConverter.put(key, key); break;
case "Cooldown": advancedConverter.put(key, ChatColor.DARK_GREEN + oldYAML.getString(key)); break;
}
break;
case "Description":
helper = "Language.Commands.Description.";
simpleConverter.put(helper + keys[2], key);
break;
}
}
}
catch(Exception e)
{
plugin.getLogger().warning("Failed to convert the old \"" + key + "\" language value into the corresponding new one.");
e.printStackTrace();
}
}
// Patch them into the lang file
try
{
for(Map.Entry<String, String> entry : advancedConverter.entrySet())
{
newYAML.set(entry.getKey(), entry.getValue());
}
for(Map.Entry<String, String> entry : simpleConverter.entrySet())
{
newYAML.set(entry.getKey(), oldYAML.getString(entry.getValue()));
}
}
catch(Exception e)
{
plugin.getLogger().warning("Failed to write the old language values into the new language file.");
e.printStackTrace();
}
OldFileUpdater.updateLanguage(oldLang.getYaml(), getYaml(), plugin.getLogger());
}
else
{