Преглед изворни кода

This command probably works

t00thpick1 пре 9 година
родитељ
комит
dcd79e87e1

+ 46 - 0
src/main/java/com/gmail/nossr50/commands/MHDCommand.java

@@ -0,0 +1,46 @@
+package com.gmail.nossr50.commands;
+
+import java.util.List;
+
+import org.bukkit.command.Command;
+import org.bukkit.command.CommandSender;
+import org.bukkit.command.TabExecutor;
+import com.gmail.nossr50.mcMMO;
+import com.gmail.nossr50.config.Config;
+import com.gmail.nossr50.database.FlatfileDatabaseManager;
+import com.gmail.nossr50.database.SQLDatabaseManager;
+import com.gmail.nossr50.datatypes.player.McMMOPlayer;
+import com.gmail.nossr50.util.player.UserManager;
+
+import com.google.common.collect.ImmutableList;
+
+public class MHDCommand implements TabExecutor {
+
+    @Override
+    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
+        if (mcMMO.getDatabaseManager() instanceof SQLDatabaseManager) {
+            SQLDatabaseManager m = (SQLDatabaseManager) mcMMO.getDatabaseManager();
+            m.resetMobHealthSettings();
+            for (McMMOPlayer player : UserManager.getPlayers()) {
+                player.getProfile().setMobHealthbarType(Config.getInstance().getMobHealthbarDefault());
+            }
+            sender.sendMessage("Mob health reset");
+            return true;
+        }
+        if (mcMMO.getDatabaseManager() instanceof FlatfileDatabaseManager) {
+            FlatfileDatabaseManager m = (FlatfileDatabaseManager) mcMMO.getDatabaseManager();
+            m.resetMobHealthSettings();
+            for (McMMOPlayer player : UserManager.getPlayers()) {
+                player.getProfile().setMobHealthbarType(Config.getInstance().getMobHealthbarDefault());
+            }
+            sender.sendMessage("Mob health reset");
+            return true;
+        }
+        return false;
+    }
+
+    @Override
+    public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
+        return ImmutableList.of();
+    }
+}

+ 53 - 0
src/main/java/com/gmail/nossr50/database/FlatfileDatabaseManager.java

@@ -1289,4 +1289,57 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
     public static int EXP_ALCHEMY = 40;
     public static int UUID_INDEX = 41;
     public static int SCOREBOARD_TIPS = 42;
+
+    public void resetMobHealthSettings() {
+        BufferedReader in = null;
+        FileWriter out = null;
+        String usersFilePath = mcMMO.getUsersFilePath();
+
+        synchronized (fileWritingLock) {
+            try {
+                in = new BufferedReader(new FileReader(usersFilePath));
+                StringBuilder writer = new StringBuilder();
+                String line;
+
+                while ((line = in.readLine()) != null) {
+                    // Remove empty lines from the file
+                    if (line.isEmpty()) {
+                        continue;
+                    }
+                    String[] character = line.split(":");
+                    
+                    character[HEALTHBAR] = Config.getInstance().getMobHealthbarDefault().toString();
+                    
+                    line = new StringBuilder(org.apache.commons.lang.StringUtils.join(character, ":")).append(":").toString();
+
+                    writer.append(line).append("\r\n");
+                }
+
+                // Write the new file
+                out = new FileWriter(usersFilePath);
+                out.write(writer.toString());
+            }
+            catch (IOException e) {
+                mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
+            }
+            finally {
+                if (in != null) {
+                    try {
+                        in.close();
+                    }
+                    catch (IOException e) {
+                        // Ignore
+                    }
+                }
+                if (out != null) {
+                    try {
+                        out.close();
+                    }
+                    catch (IOException e) {
+                        // Ignore
+                    }
+                }
+            }
+        }
+    }
 }

+ 19 - 0
src/main/java/com/gmail/nossr50/database/SQLDatabaseManager.java

@@ -1417,4 +1417,23 @@ public final class SQLDatabaseManager implements DatabaseManager {
         LOAD,
         SAVE;
     }
+
+    public void resetMobHealthSettings() {
+        PreparedStatement statement = null;
+        Connection connection = null;
+
+        try {
+            connection = getConnection(PoolIdentifier.MISC);
+            statement = connection.prepareStatement("UPDATE " + tablePrefix + "huds SET mobhealthbar = ?");
+            statement.setString(1, Config.getInstance().getMobHealthbarDefault().toString());
+            statement.executeUpdate();
+        }
+        catch (SQLException ex) {
+            printErrors(ex);
+        }
+        finally {
+            tryClose(statement);
+            tryClose(connection);
+        }
+    }
 }

+ 11 - 0
src/main/java/com/gmail/nossr50/util/commands/CommandRegistrationManager.java

@@ -7,6 +7,7 @@ import org.bukkit.command.PluginCommand;
 
 import com.gmail.nossr50.mcMMO;
 import com.gmail.nossr50.commands.KrakenCommand;
+import com.gmail.nossr50.commands.MHDCommand;
 import com.gmail.nossr50.commands.McImportCommand;
 import com.gmail.nossr50.commands.McabilityCommand;
 import com.gmail.nossr50.commands.McconvertCommand;
@@ -390,6 +391,15 @@ public final class CommandRegistrationManager {
         command.setUsage(LocaleLoader.getString("Commands.Usage.1", "mobhealth", "<DISABLED | HEARTS | BAR>"));
         command.setExecutor(new MobhealthCommand());
     }
+    
+    private static void registerMHDCommand() {
+        PluginCommand command = mcMMO.p.getCommand("mhd");
+        command.setDescription("Resets all mob health bar settings for all players to the default"); //TODO: Localize
+        command.setPermission("mcmmo.commands.mhd");
+        command.setPermissionMessage(permissionsMessage);
+        command.setUsage(LocaleLoader.getString("Commands.Usage.0", "mhd"));
+        command.setExecutor(new MHDCommand());
+    }
 
     private static void registerMcscoreboardCommand() {
         PluginCommand command = mcMMO.p.getCommand("mcscoreboard");
@@ -430,6 +440,7 @@ public final class CommandRegistrationManager {
         registerMcrefreshCommand();
         registerMcscoreboardCommand();
         registerMobhealthCommand();
+        registerMHDCommand();
         registerXprateCommand();
 
         // Chat Commands

+ 5 - 0
src/main/resources/plugin.yml

@@ -111,6 +111,8 @@ commands:
     mobhealth:
         aliases: [mcmobhealth]
         description: Change the style of the mob healthbar
+    mhd:
+        description: Sets all players mob health settings to default
     mcscoreboard:
         aliases: [mcsb]
         description: Manage your mcMMO Scoreboard
@@ -947,6 +949,9 @@ permissions:
         description: Allows access to the mmoshowdb command
     mcmmo.commands.mobhealth:
          description: Allows access to the mobhealth command
+    mcmmo.commands.mhd:
+        default: false
+        description: Allows access to the mhd command
     mcmmo.commands.party.*:
         default: false
         description: Implies access to all mcmmo.commands.party permissions.