mirror of
				https://github.com/IntellectualSites/PlotSquared.git
				synced 2025-11-04 03:03:43 +01:00 
			
		
		
		
	Improve design
This commit is contained in:
		@@ -1,49 +0,0 @@
 | 
				
			|||||||
/*
 | 
					 | 
				
			||||||
 *       _____  _       _    _____                                _
 | 
					 | 
				
			||||||
 *      |  __ \| |     | |  / ____|                              | |
 | 
					 | 
				
			||||||
 *      | |__) | | ___ | |_| (___   __ _ _   _  __ _ _ __ ___  __| |
 | 
					 | 
				
			||||||
 *      |  ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` |
 | 
					 | 
				
			||||||
 *      | |    | | (_) | |_ ____) | (_| | |_| | (_| | | |  __/ (_| |
 | 
					 | 
				
			||||||
 *      |_|    |_|\___/ \__|_____/ \__, |\__,_|\__,_|_|  \___|\__,_|
 | 
					 | 
				
			||||||
 *                                    | |
 | 
					 | 
				
			||||||
 *                                    |_|
 | 
					 | 
				
			||||||
 *            PlotSquared plot management system for Minecraft
 | 
					 | 
				
			||||||
 *                  Copyright (C) 2020 IntellectualSites
 | 
					 | 
				
			||||||
 *
 | 
					 | 
				
			||||||
 *     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
 | 
					 | 
				
			||||||
 *     the Free Software Foundation, either version 3 of the License, or
 | 
					 | 
				
			||||||
 *     (at your option) any later version.
 | 
					 | 
				
			||||||
 *
 | 
					 | 
				
			||||||
 *     This program is distributed in the hope that it will be useful,
 | 
					 | 
				
			||||||
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
					 | 
				
			||||||
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
					 | 
				
			||||||
 *     GNU General Public License for more details.
 | 
					 | 
				
			||||||
 *
 | 
					 | 
				
			||||||
 *     You should have received a copy of the GNU General Public License
 | 
					 | 
				
			||||||
 *     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | 
					 | 
				
			||||||
 */
 | 
					 | 
				
			||||||
package com.plotsquared.core.uuid;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import java.util.concurrent.CompletableFuture;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
/**
 | 
					 | 
				
			||||||
 * Thrown by a {@link UUIDService} when it cannot
 | 
					 | 
				
			||||||
 * complete a request. This is not an error and
 | 
					 | 
				
			||||||
 * will be dealt with silently.
 | 
					 | 
				
			||||||
 */
 | 
					 | 
				
			||||||
public class ServiceFailure extends Throwable {
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    private static final ServiceFailure instance = new ServiceFailure();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public static <T> CompletableFuture<T> getFuture() {
 | 
					 | 
				
			||||||
        final CompletableFuture<T> completableFuture = new CompletableFuture<>();
 | 
					 | 
				
			||||||
        completableFuture.completeExceptionally(instance);
 | 
					 | 
				
			||||||
        return completableFuture;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Override public Throwable fillInStackTrace() {
 | 
					 | 
				
			||||||
        return this;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
@@ -28,8 +28,11 @@ package com.plotsquared.core.uuid;
 | 
				
			|||||||
import com.google.common.collect.Lists;
 | 
					import com.google.common.collect.Lists;
 | 
				
			||||||
import org.jetbrains.annotations.NotNull;
 | 
					import org.jetbrains.annotations.NotNull;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.util.ArrayList;
 | 
				
			||||||
 | 
					import java.util.Collection;
 | 
				
			||||||
 | 
					import java.util.Collections;
 | 
				
			||||||
import java.util.List;
 | 
					import java.util.List;
 | 
				
			||||||
import java.util.ListIterator;
 | 
					import java.util.Optional;
 | 
				
			||||||
import java.util.UUID;
 | 
					import java.util.UUID;
 | 
				
			||||||
import java.util.concurrent.CompletableFuture;
 | 
					import java.util.concurrent.CompletableFuture;
 | 
				
			||||||
import java.util.concurrent.Executor;
 | 
					import java.util.concurrent.Executor;
 | 
				
			||||||
@@ -79,9 +82,7 @@ public class UUIDPipeline {
 | 
				
			|||||||
     * @return Copy of service list
 | 
					     * @return Copy of service list
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    public List<UUIDService> getServiceListInstance() {
 | 
					    public List<UUIDService> getServiceListInstance() {
 | 
				
			||||||
        final List<UUIDService> serviceList = Lists.newLinkedList(this.serviceList);
 | 
					        return Collections.unmodifiableList(this.serviceList);
 | 
				
			||||||
        serviceList.add(EndOfPipeline.instance);
 | 
					 | 
				
			||||||
        return serviceList;
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private void consume(@NotNull final UUIDMapping mapping) {
 | 
					    private void consume(@NotNull final UUIDMapping mapping) {
 | 
				
			||||||
@@ -93,74 +94,34 @@ public class UUIDPipeline {
 | 
				
			|||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * Asynchronously attempt to fetch the mapping from a given UUID or username
 | 
					     * Asynchronously attempt to fetch the mapping from a given UUID or username
 | 
				
			||||||
     *
 | 
					     *
 | 
				
			||||||
     * @param request UUID or username
 | 
					     * @param requests UUIDs or usernames
 | 
				
			||||||
     * @return Future that may complete with the mapping
 | 
					     * @return Future that may complete with the mapping
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    public CompletableFuture<UUIDMapping> get(@NotNull final Object request) {
 | 
					    public CompletableFuture<Collection<UUIDMapping>> get(@NotNull final Collection<Object> requests) {
 | 
				
			||||||
        if (!(request instanceof String) && !(request instanceof UUID)) {
 | 
					        final List<UUIDService> serviceList = this.getServiceListInstance();
 | 
				
			||||||
            throw new IllegalArgumentException("Request has to be either a username or UUID");
 | 
					        return CompletableFuture.supplyAsync(() -> {
 | 
				
			||||||
        }
 | 
					            final List<UUIDMapping> mappings = new ArrayList<>(requests.size());
 | 
				
			||||||
        final CompletableFuture<UUIDMapping> future = new CompletableFuture<>();
 | 
					            outer: for (final Object request : requests) {
 | 
				
			||||||
        final ListIterator<UUIDService> serviceListIterator
 | 
					                if (!(request instanceof String) && !(request instanceof UUID)) {
 | 
				
			||||||
            = this.getServiceListInstance().listIterator();
 | 
					                    throw new IllegalArgumentException("Request has to be either a username or UUID");
 | 
				
			||||||
        final Runnable[] runnable = new Runnable[1];
 | 
					                }
 | 
				
			||||||
        runnable[0] = () -> {
 | 
					                for (final UUIDService service : serviceList) {
 | 
				
			||||||
            if (serviceListIterator.hasNext()) {
 | 
					                    final Optional<?> result = service.get(request);
 | 
				
			||||||
                final UUIDService uuidService = serviceListIterator.next();
 | 
					                    if (result.isPresent()) {
 | 
				
			||||||
                uuidService.get(request).whenCompleteAsync(((result, throwable) -> {
 | 
					 | 
				
			||||||
                    if (throwable != null) {
 | 
					 | 
				
			||||||
                        if (throwable instanceof ServiceFailure) {
 | 
					 | 
				
			||||||
                            try {
 | 
					 | 
				
			||||||
                                runnable[0].run();
 | 
					 | 
				
			||||||
                            } catch (final Throwable inner) {
 | 
					 | 
				
			||||||
                                future.completeExceptionally(inner);
 | 
					 | 
				
			||||||
                            }
 | 
					 | 
				
			||||||
                        } else {
 | 
					 | 
				
			||||||
                            future.completeExceptionally(throwable);
 | 
					 | 
				
			||||||
                        }
 | 
					 | 
				
			||||||
                    } else {
 | 
					 | 
				
			||||||
                        final String username = request instanceof String ? (String) request
 | 
					                        final String username = request instanceof String ? (String) request
 | 
				
			||||||
                            : (String) result;
 | 
					                            : (String) result.get();
 | 
				
			||||||
                        final UUID uuid = request instanceof UUID ? (UUID) request
 | 
					                        final UUID uuid = request instanceof UUID ? (UUID) request
 | 
				
			||||||
                            : (UUID) result;
 | 
					                            : (UUID) result.get();
 | 
				
			||||||
                        final UUIDMapping mapping = new UUIDMapping(uuid, username);
 | 
					                        final UUIDMapping mapping = new UUIDMapping(uuid, username);
 | 
				
			||||||
                        future.complete(mapping);
 | 
					 | 
				
			||||||
                        this.consume(mapping);
 | 
					                        this.consume(mapping);
 | 
				
			||||||
 | 
					                        mappings.add(mapping);
 | 
				
			||||||
 | 
					                        continue outer;
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
                }), this.executor);
 | 
					                }
 | 
				
			||||||
            } else {
 | 
					                throw new ServiceError("End of pipeline");
 | 
				
			||||||
                throw new ServiceError("Pipeline is incomplete");
 | 
					 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        };
 | 
					            return mappings;
 | 
				
			||||||
        try {
 | 
					        }, this.executor);
 | 
				
			||||||
            // Start the pipeline traversal
 | 
					 | 
				
			||||||
            runnable[0].run();
 | 
					 | 
				
			||||||
        } catch (final Throwable throwable) {
 | 
					 | 
				
			||||||
            future.completeExceptionally(throwable);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        return future;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    /**
 | 
					 | 
				
			||||||
     * Indicates that the end of the pipeline has been reached, this
 | 
					 | 
				
			||||||
     * will cause the request to fail, as no service was able to
 | 
					 | 
				
			||||||
     * fulfil the request
 | 
					 | 
				
			||||||
     */
 | 
					 | 
				
			||||||
    private static class EndOfPipeline implements UUIDService {
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        public static final EndOfPipeline instance = new EndOfPipeline();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        @Override @NotNull public CompletableFuture<String> get(@NotNull final UUID uuid) {
 | 
					 | 
				
			||||||
            final CompletableFuture<String> future = new CompletableFuture<>();
 | 
					 | 
				
			||||||
            future.completeExceptionally(new ServiceError("End of pipeline"));
 | 
					 | 
				
			||||||
            return future;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        @Override @NotNull public CompletableFuture<UUID> get(@NotNull final String username) {
 | 
					 | 
				
			||||||
            final CompletableFuture<UUID> future = new CompletableFuture<>();
 | 
					 | 
				
			||||||
            future.completeExceptionally(new ServiceError("End of pipeline"));
 | 
					 | 
				
			||||||
            return future;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -27,15 +27,15 @@ package com.plotsquared.core.uuid;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import org.jetbrains.annotations.NotNull;
 | 
					import org.jetbrains.annotations.NotNull;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.util.Optional;
 | 
				
			||||||
import java.util.UUID;
 | 
					import java.util.UUID;
 | 
				
			||||||
import java.util.concurrent.CompletableFuture;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Service used to provide usernames from player UUIDs
 | 
					 * Service used to provide usernames from player UUIDs
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
public interface UUIDService {
 | 
					public interface UUIDService {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    default CompletableFuture<?> get(@NotNull final Object request) {
 | 
					    default Optional<?> get(@NotNull final Object request) {
 | 
				
			||||||
        if (request instanceof UUID) {
 | 
					        if (request instanceof UUID) {
 | 
				
			||||||
            return get((UUID) request);
 | 
					            return get((UUID) request);
 | 
				
			||||||
        } else if (request instanceof String) {
 | 
					        } else if (request instanceof String) {
 | 
				
			||||||
@@ -54,9 +54,9 @@ public interface UUIDService {
 | 
				
			|||||||
     * this completes with an empty optional.
 | 
					     * this completes with an empty optional.
 | 
				
			||||||
     *
 | 
					     *
 | 
				
			||||||
     * @param uuid Player UUID
 | 
					     * @param uuid Player UUID
 | 
				
			||||||
     * @return Future that may contain the username if it exists
 | 
					     * @return Optional that may contain the username if it exists
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    @NotNull CompletableFuture<String> get(@NotNull final UUID uuid);
 | 
					    @NotNull Optional<String> get(@NotNull final UUID uuid);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * Get a stored UUID from the service if it exists.
 | 
					     * Get a stored UUID from the service if it exists.
 | 
				
			||||||
@@ -67,8 +67,8 @@ public interface UUIDService {
 | 
				
			|||||||
     * this completes with an empty optional.
 | 
					     * this completes with an empty optional.
 | 
				
			||||||
     *
 | 
					     *
 | 
				
			||||||
     * @param username Player username
 | 
					     * @param username Player username
 | 
				
			||||||
     * @return Future that may contain the UUID if it exists
 | 
					     * @return Optional  that may contain the UUID if it exists
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    @NotNull CompletableFuture<UUID> get(@NotNull final String username);
 | 
					    @NotNull Optional<UUID> get(@NotNull final String username);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -27,13 +27,12 @@ package com.plotsquared.core.uuid.offline;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import com.google.common.base.Charsets;
 | 
					import com.google.common.base.Charsets;
 | 
				
			||||||
import com.plotsquared.core.configuration.Settings;
 | 
					import com.plotsquared.core.configuration.Settings;
 | 
				
			||||||
import com.plotsquared.core.uuid.ServiceFailure;
 | 
					 | 
				
			||||||
import com.plotsquared.core.uuid.UUIDService;
 | 
					import com.plotsquared.core.uuid.UUIDService;
 | 
				
			||||||
import org.jetbrains.annotations.NotNull;
 | 
					import org.jetbrains.annotations.NotNull;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.util.Locale;
 | 
					import java.util.Locale;
 | 
				
			||||||
 | 
					import java.util.Optional;
 | 
				
			||||||
import java.util.UUID;
 | 
					import java.util.UUID;
 | 
				
			||||||
import java.util.concurrent.CompletableFuture;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Name provider service that creates UUIDs from usernames
 | 
					 * Name provider service that creates UUIDs from usernames
 | 
				
			||||||
@@ -47,13 +46,12 @@ public class OfflineModeUUIDService implements UUIDService {
 | 
				
			|||||||
        return UUID.nameUUIDFromBytes(("OfflinePlayer:" + username).getBytes(Charsets.UTF_8));
 | 
					        return UUID.nameUUIDFromBytes(("OfflinePlayer:" + username).getBytes(Charsets.UTF_8));
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Override @NotNull public CompletableFuture<String> get(@NotNull final UUID uuid) {
 | 
					    @Override @NotNull public Optional<String> get(@NotNull final UUID uuid) {
 | 
				
			||||||
        // This service can only get UUIDs from usernames
 | 
					        return Optional.empty();
 | 
				
			||||||
        return ServiceFailure.getFuture();
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Override @NotNull public CompletableFuture<UUID> get(@NotNull final String username) {
 | 
					    @Override @NotNull public Optional<UUID> get(@NotNull final String username) {
 | 
				
			||||||
        return CompletableFuture.completedFuture(this.getFromUsername(username));
 | 
					        return Optional.of(this.getFromUsername(username));
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user