mirror of
				https://github.com/IntellectualSites/PlotSquared.git
				synced 2025-11-04 03:03:43 +01:00 
			
		
		
		
	Lazy UUID conversion
This commit is contained in:
		@@ -193,7 +193,18 @@ public class PS {
 | 
			
		||||
                @Override
 | 
			
		||||
                public void run() {
 | 
			
		||||
                    PS.debug("Starting UUID caching");
 | 
			
		||||
                    UUIDHandler.startCaching(null);
 | 
			
		||||
                    UUIDHandler.startCaching(new Runnable() {
 | 
			
		||||
                        @Override
 | 
			
		||||
                        public void run() {
 | 
			
		||||
                            for (Plot plot : getPlots()) {
 | 
			
		||||
                                if (plot.owner != null && plot.temp != -1) {
 | 
			
		||||
                                    if (UUIDHandler.getName(plot.owner) == null) {
 | 
			
		||||
                                        UUIDHandler.implementation.unknown.add(plot.owner);
 | 
			
		||||
                                    }
 | 
			
		||||
                                }
 | 
			
		||||
                            }
 | 
			
		||||
                        }
 | 
			
		||||
                    });
 | 
			
		||||
                }
 | 
			
		||||
            }, 20);
 | 
			
		||||
            // create event util class
 | 
			
		||||
 
 | 
			
		||||
@@ -330,6 +330,15 @@ public interface AbstractDB {
 | 
			
		||||
    void resizeCluster(PlotCluster current, PlotClusterId resize);
 | 
			
		||||
 | 
			
		||||
    void movePlot(Plot originalPlot, Plot newPlot);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Replace a old uuid with a new one in the database<br>
 | 
			
		||||
     * - Useful for replacing a few uuids (not the entire database)<br>
 | 
			
		||||
     * - For entire conversion, the uuidconvert command scales better
 | 
			
		||||
     * @param old
 | 
			
		||||
     * @param now
 | 
			
		||||
     */
 | 
			
		||||
    void replaceUUID(UUID old, UUID now);
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * Don't fuck with this one, unless you enjoy it rough
 | 
			
		||||
 
 | 
			
		||||
@@ -420,6 +420,15 @@ public class DBFunc {
 | 
			
		||||
    public static void setPosition(final PlotCluster cluster, final String position) {
 | 
			
		||||
        dbManager.setPosition(cluster, position);
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * Replace all occurances of a uuid in the database with another one
 | 
			
		||||
     * @param old
 | 
			
		||||
     * @param now
 | 
			
		||||
     */
 | 
			
		||||
    public static void replaceUUID(UUID old, UUID now) {
 | 
			
		||||
        dbManager.replaceUUID(old, now);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static void close() {
 | 
			
		||||
        dbManager.close();
 | 
			
		||||
 
 | 
			
		||||
@@ -2572,6 +2572,28 @@ public class SQLManager implements AbstractDB {
 | 
			
		||||
        }
 | 
			
		||||
        commit();
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    @Override
 | 
			
		||||
    public void replaceUUID(final UUID old, final UUID now) {
 | 
			
		||||
        addGlobalTask(new Runnable() {
 | 
			
		||||
            @Override
 | 
			
		||||
            public void run() {
 | 
			
		||||
                try (Statement stmt = connection.createStatement()) {
 | 
			
		||||
                    stmt.executeUpdate("UPDATE `" + prefix + "cluster` SET `owner` = '" + now.toString() + "' WHERE `owner` = '" + old.toString() + "'");
 | 
			
		||||
                    stmt.executeUpdate("UPDATE `" + prefix + "cluster_helpers` SET `user_uuid` = '" + now.toString() + "' WHERE `user_uuid` = '" + old.toString() + "'");
 | 
			
		||||
                    stmt.executeUpdate("UPDATE `" + prefix + "cluster_invited` SET `user_uuid` = '" + now.toString() + "' WHERE `user_uuid` = '" + old.toString() + "'");
 | 
			
		||||
                    stmt.executeUpdate("UPDATE `" + prefix + "plot` SET `owner` = '" + now.toString() + "' WHERE `owner` = '" + old.toString() + "'");
 | 
			
		||||
                    stmt.executeUpdate("UPDATE `" + prefix + "plot_denied` SET `user_uuid` = '" + now.toString() + "' WHERE `user_uuid` = '" + old.toString() + "'");
 | 
			
		||||
                    stmt.executeUpdate("UPDATE `" + prefix + "plot_helpers` SET `user_uuid` = '" + now.toString() + "' WHERE `user_uuid` = '" + old.toString() + "'");
 | 
			
		||||
                    stmt.executeUpdate("UPDATE `" + prefix + "plot_trusted` SET `user_uuid` = '" + now.toString() + "' WHERE `user_uuid` = '" + old.toString() + "'");
 | 
			
		||||
                    stmt.close();
 | 
			
		||||
                }
 | 
			
		||||
                catch (Exception e) {
 | 
			
		||||
                    e.printStackTrace();
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void close() {
 | 
			
		||||
 
 | 
			
		||||
@@ -1,15 +1,20 @@
 | 
			
		||||
package com.intellectualcrafters.plot.util;
 | 
			
		||||
 | 
			
		||||
import java.util.HashMap;
 | 
			
		||||
import java.util.HashSet;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
import java.util.Set;
 | 
			
		||||
import java.util.UUID;
 | 
			
		||||
 | 
			
		||||
import com.google.common.base.Charsets;
 | 
			
		||||
import com.google.common.collect.BiMap;
 | 
			
		||||
import com.google.common.collect.HashBiMap;
 | 
			
		||||
import com.intellectualcrafters.plot.PS;
 | 
			
		||||
import com.intellectualcrafters.plot.config.C;
 | 
			
		||||
import com.intellectualcrafters.plot.config.Settings;
 | 
			
		||||
import com.intellectualcrafters.plot.database.DBFunc;
 | 
			
		||||
import com.intellectualcrafters.plot.object.OfflinePlotPlayer;
 | 
			
		||||
import com.intellectualcrafters.plot.object.Plot;
 | 
			
		||||
import com.intellectualcrafters.plot.object.PlotPlayer;
 | 
			
		||||
import com.intellectualcrafters.plot.object.RunnableVal;
 | 
			
		||||
import com.intellectualcrafters.plot.object.StringWrapper;
 | 
			
		||||
@@ -62,36 +67,82 @@ public abstract class UUIDHandlerImplementation {
 | 
			
		||||
        if (uuidMap.size() == 0) {
 | 
			
		||||
            uuidMap = toAdd;
 | 
			
		||||
        }
 | 
			
		||||
        TaskManager.runTask(new Runnable() {
 | 
			
		||||
            @Override
 | 
			
		||||
            public void run() {
 | 
			
		||||
                for (Map.Entry<StringWrapper, UUID> entry : toAdd.entrySet()) {
 | 
			
		||||
                    UUID uuid = entry.getValue();
 | 
			
		||||
                    StringWrapper name = entry.getKey();
 | 
			
		||||
                    if ((uuid == null) || (name == null)) {
 | 
			
		||||
                        continue;
 | 
			
		||||
                    }
 | 
			
		||||
                    BiMap<UUID, StringWrapper> inverse = uuidMap.inverse();
 | 
			
		||||
                    if (inverse.containsKey(uuid)) {
 | 
			
		||||
                        if (uuidMap.containsKey(name)) {
 | 
			
		||||
                            continue;
 | 
			
		||||
                        }
 | 
			
		||||
                        rename(uuid, name);
 | 
			
		||||
                        continue;
 | 
			
		||||
                    }
 | 
			
		||||
                    uuidMap.put(name, uuid);
 | 
			
		||||
                }
 | 
			
		||||
                PS.debug(C.PREFIX.s() + "&6Cached a total of: " + uuidMap.size() + " UUIDs");
 | 
			
		||||
        for (Map.Entry<StringWrapper, UUID> entry : toAdd.entrySet()) {
 | 
			
		||||
            UUID uuid = entry.getValue();
 | 
			
		||||
            StringWrapper name = entry.getKey();
 | 
			
		||||
            if ((uuid == null) || (name == null)) {
 | 
			
		||||
                continue;
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
            BiMap<UUID, StringWrapper> inverse = uuidMap.inverse();
 | 
			
		||||
            if (inverse.containsKey(uuid)) {
 | 
			
		||||
                if (uuidMap.containsKey(name)) {
 | 
			
		||||
                    continue;
 | 
			
		||||
                }
 | 
			
		||||
                rename(uuid, name);
 | 
			
		||||
                continue;
 | 
			
		||||
            }
 | 
			
		||||
            uuidMap.put(name, uuid);
 | 
			
		||||
        }
 | 
			
		||||
        PS.debug(C.PREFIX.s() + "&6Cached a total of: " + uuidMap.size() + " UUIDs");
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    public HashSet<UUID> unknown = new HashSet<>();
 | 
			
		||||
    
 | 
			
		||||
    public boolean add(final StringWrapper name, final UUID uuid) {
 | 
			
		||||
        if ((uuid == null) || (name == null)) {
 | 
			
		||||
        if ((uuid == null)) {
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
        if (name == null) {
 | 
			
		||||
            try {
 | 
			
		||||
                unknown.add(uuid);
 | 
			
		||||
            }
 | 
			
		||||
            catch (Exception e) {
 | 
			
		||||
                PS.log("&c(minor) Invalid UUID mapping: " + uuid);
 | 
			
		||||
                e.printStackTrace();
 | 
			
		||||
            }
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        /*
 | 
			
		||||
         * lazy UUID conversion:
 | 
			
		||||
         *  - Useful if the person misconfigured the database, or settings before PlotMe conversion
 | 
			
		||||
         */
 | 
			
		||||
        if (!Settings.OFFLINE_MODE) {
 | 
			
		||||
            UUID offline = UUID.nameUUIDFromBytes(("OfflinePlayer:" + name.value).getBytes(Charsets.UTF_8));
 | 
			
		||||
            if (!unknown.contains(offline) && !name.value.equals(name.value.toLowerCase())){
 | 
			
		||||
                offline = UUID.nameUUIDFromBytes(("OfflinePlayer:" + name.value).getBytes(Charsets.UTF_8));
 | 
			
		||||
                if (!unknown.contains(offline)) {
 | 
			
		||||
                    offline = null;
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            if (offline != null) {
 | 
			
		||||
                unknown.remove(offline);
 | 
			
		||||
                Set<Plot> plots = PS.get().getPlots(offline);
 | 
			
		||||
                if (plots.size() > 0) {
 | 
			
		||||
                    for (Plot plot : PS.get().getPlots(offline)) {
 | 
			
		||||
                        plot.owner = uuid;
 | 
			
		||||
                    }
 | 
			
		||||
                    DBFunc.replaceUUID(offline, uuid);
 | 
			
		||||
                    PS.debug("&cDetected invalid UUID stored for: " + name.value);
 | 
			
		||||
                    PS.debug("&7 - Did you recently switch to online-mode storage without running `uuidconvert`?");
 | 
			
		||||
                    PS.debug("&6PlotSquared will update incorrect entries when the user logs in, or you can reconstruct your database.");
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        try {
 | 
			
		||||
            uuidMap.put(name, uuid);
 | 
			
		||||
            UUID offline = uuidMap.put(name, uuid);
 | 
			
		||||
            if (offline != null && !offline.equals(uuid)) {
 | 
			
		||||
                Set<Plot> plots = PS.get().getPlots(offline);
 | 
			
		||||
                if (plots.size() > 0) {
 | 
			
		||||
                    for (Plot plot : PS.get().getPlots(offline)) {
 | 
			
		||||
                        plot.owner = uuid;
 | 
			
		||||
                    }
 | 
			
		||||
                    DBFunc.replaceUUID(offline, uuid);
 | 
			
		||||
                    PS.debug("&cDetected invalid UUID stored for (1): " + name.value);
 | 
			
		||||
                    PS.debug("&7 - Did you recently switch to online-mode storage without running `uuidconvert`?");
 | 
			
		||||
                    PS.debug("&6PlotSquared will update incorrect entries when the user logs in, or you can reconstruct your database.");
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        catch (Exception e) {
 | 
			
		||||
            BiMap<UUID, StringWrapper> inverse = uuidMap.inverse();
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user