Begin working on the new UUID service API

This commit is contained in:
Alexander Söderberg 2020-05-17 12:40:25 +02:00
parent c58309b385
commit 7591c440c2
14 changed files with 472 additions and 35 deletions

View file

@ -46,7 +46,7 @@ import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.UUID; import java.util.UUID;
public class OfflineUUIDWrapper extends UUIDWrapper { public class OfflineUUIDWrapper implements UUIDWrapper {
private final Object[] arg = new Object[0]; private final Object[] arg = new Object[0];
private Method getOnline = null; private Method getOnline = null;

View file

@ -60,9 +60,7 @@ import java.util.UUID;
* *
* @version 5 * @version 5
*/ */
@SuppressWarnings({"unused", "WeakerAccess"}) @SuppressWarnings({"unused", "WeakerAccess"}) @NoArgsConstructor public class PlotAPI {
@NoArgsConstructor
public class PlotAPI {
/** /**
* Gets all plots. * Gets all plots.

View file

@ -76,7 +76,6 @@ public class Set extends SubCommand {
@Override public boolean set(PlotPlayer player, final Plot plot, String value) { @Override public boolean set(PlotPlayer player, final Plot plot, String value) {
PlotManager manager = player.getLocation().getPlotManager(); PlotManager manager = player.getLocation().getPlotManager();
String[] components = manager.getPlotComponents(plot.getId()); String[] components = manager.getPlotComponents(plot.getId());
boolean allowUnsafe = DebugAllowUnsafe.unsafeAllowed.contains(player.getUUID());
String[] args = value.split(" "); String[] args = value.split(" ");
String material = String material =

View file

@ -38,8 +38,7 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
@Beta @Beta public class CommentManager {
public class CommentManager {
public static final HashMap<String, CommentInbox> inboxes = new HashMap<>(); public static final HashMap<String, CommentInbox> inboxes = new HashMap<>();

View file

@ -42,8 +42,7 @@ import java.util.Map;
/** /**
* Container type for {@link PlotFlag plot flags}. * Container type for {@link PlotFlag plot flags}.
*/ */
@EqualsAndHashCode(of = "flagMap") @EqualsAndHashCode(of = "flagMap") public class FlagContainer {
public class FlagContainer {
private final Map<String, String> unknownFlags = new HashMap<>(); private final Map<String, String> unknownFlags = new HashMap<>();
private final Map<Class<?>, PlotFlag<?, ?>> flagMap = new HashMap<>(); private final Map<Class<?>, PlotFlag<?, ?>> flagMap = new HashMap<>();
@ -341,8 +340,7 @@ public class FlagContainer {
/** /**
* Handler for update events in {@link FlagContainer flag containers}. * Handler for update events in {@link FlagContainer flag containers}.
*/ */
@FunctionalInterface @FunctionalInterface public interface PlotFlagUpdateHandler {
public interface PlotFlagUpdateHandler {
/** /**
* Act on the flag update event * Act on the flag update event

View file

@ -41,8 +41,7 @@ import java.util.Collections;
* *
* @param <T> Value contained in the flag. * @param <T> Value contained in the flag.
*/ */
@EqualsAndHashCode(of = "value") @EqualsAndHashCode(of = "value") public abstract class PlotFlag<T, F extends PlotFlag<T, F>> {
public abstract class PlotFlag<T, F extends PlotFlag<T, F>> {
private final T value; private final T value;
private final Caption flagCategory; private final Caption flagCategory;

View file

@ -44,6 +44,8 @@ import java.util.Map;
*/ */
public class BlockTypeWrapper { public class BlockTypeWrapper {
private static final Map<BlockType, BlockTypeWrapper> blockTypes = new HashMap<>();
private static final Map<String, BlockTypeWrapper> blockCategories = new HashMap<>();
@Nullable @Getter private final BlockType blockType; @Nullable @Getter private final BlockType blockType;
@Nullable private final String blockCategoryId; @Nullable private final String blockCategoryId;
@Nullable private BlockCategory blockCategory; @Nullable private BlockCategory blockCategory;
@ -66,6 +68,19 @@ public class BlockTypeWrapper {
this.blockCategoryId = Preconditions.checkNotNull(blockCategoryId); this.blockCategoryId = Preconditions.checkNotNull(blockCategoryId);
} }
public static BlockTypeWrapper get(final BlockType blockType) {
return blockTypes.computeIfAbsent(blockType, BlockTypeWrapper::new);
}
public static BlockTypeWrapper get(final BlockCategory blockCategory) {
return blockCategories
.computeIfAbsent(blockCategory.getId(), id -> new BlockTypeWrapper(blockCategory));
}
public static BlockTypeWrapper get(final String blockCategoryId) {
return blockCategories.computeIfAbsent(blockCategoryId, BlockTypeWrapper::new);
}
@Override public String toString() { @Override public String toString() {
if (this.blockType != null) { if (this.blockType != null) {
final String key = this.blockType.toString(); final String key = this.blockType.toString();
@ -135,21 +150,6 @@ public class BlockTypeWrapper {
return Objects.hashCode(this.blockType, this.blockCategoryId); return Objects.hashCode(this.blockType, this.blockCategoryId);
} }
private static final Map<BlockType, BlockTypeWrapper> blockTypes = new HashMap<>();
private static final Map<String, BlockTypeWrapper> blockCategories = new HashMap<>();
public static BlockTypeWrapper get(final BlockType blockType) {
return blockTypes.computeIfAbsent(blockType, BlockTypeWrapper::new);
}
public static BlockTypeWrapper get(final BlockCategory blockCategory) {
return blockCategories
.computeIfAbsent(blockCategory.getId(), id -> new BlockTypeWrapper(blockCategory));
}
public static BlockTypeWrapper get(final String blockCategoryId) {
return blockCategories.computeIfAbsent(blockCategoryId, BlockTypeWrapper::new);
}
/** /**
* Prevents exceptions when loading/saving block categories * Prevents exceptions when loading/saving block categories

View file

@ -31,17 +31,17 @@ import org.jetbrains.annotations.NotNull;
import java.util.UUID; import java.util.UUID;
public abstract class UUIDWrapper { public interface UUIDWrapper {
@NotNull public abstract UUID getUUID(PlotPlayer player); @NotNull UUID getUUID(PlotPlayer player);
public abstract UUID getUUID(OfflinePlotPlayer player); UUID getUUID(OfflinePlotPlayer player);
public abstract UUID getUUID(String name); UUID getUUID(String name);
public abstract OfflinePlotPlayer getOfflinePlayer(UUID uuid); OfflinePlotPlayer getOfflinePlayer(UUID uuid);
public abstract OfflinePlotPlayer getOfflinePlayer(String name); OfflinePlotPlayer getOfflinePlayer(String name);
public abstract OfflinePlotPlayer[] getOfflinePlayers(); OfflinePlotPlayer[] getOfflinePlayers();
} }

View file

@ -0,0 +1,43 @@
/*
* _____ _ _ _____ _
* | __ \| | | | / ____| | |
* | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| |
* | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` |
* | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| |
* |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_|
* | |
* |_|
* 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 org.jetbrains.annotations.NotNull;
/**
* Thrown by {@link ServiceError} when something goes wrong
*/
public class ServiceError extends RuntimeException {
public ServiceError(@NotNull final String message) {
super(message);
}
public ServiceError(@NotNull final String message, @NotNull final Throwable cause) {
super(message, cause);
}
}

View file

@ -0,0 +1,49 @@
/*
* _____ _ _ _____ _
* | __ \| | | | / ____| | |
* | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| |
* | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` |
* | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| |
* |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_|
* | |
* |_|
* 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;
}
}

View file

@ -0,0 +1,53 @@
/*
* _____ _ _ _____ _
* | __ \| | | | / ____| | |
* | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| |
* | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` |
* | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| |
* |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_|
* | |
* |_|
* 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 org.jetbrains.annotations.NotNull;
import java.util.UUID;
/**
* A pair consisting of a UUID and a username
*/
public class UUIDMapping {
private final UUID uuid;
private final String username;
public UUIDMapping(@NotNull final UUID uuid, final String username) {
this.uuid = uuid;
this.username = username;
}
@NotNull public String getUsername() {
return this.username;
}
@NotNull public UUID getUuid() {
return this.uuid;
}
}

View file

@ -0,0 +1,166 @@
/*
* _____ _ _ _____ _
* | __ \| | | | / ____| | |
* | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| |
* | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` |
* | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| |
* |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_|
* | |
* |_|
* 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 com.google.common.collect.Lists;
import org.jetbrains.annotations.NotNull;
import java.util.List;
import java.util.ListIterator;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.function.Consumer;
/**
* An UUID pipeline is essentially an ordered list of
* {@link UUIDService uuid services} that each get the
* opportunity of providing usernames or UUIDs.
* <p>
* Each request is then passed through a secondary list of
* consumers, that can then be used to cache them, etc
*/
public class UUIDPipeline {
private final Executor executor;
private final List<UUIDService> serviceList;
private final List<Consumer<UUIDMapping>> consumerList;
public UUIDPipeline(@NotNull final Executor executor) {
this.executor = executor;
this.serviceList = Lists.newLinkedList();
this.consumerList = Lists.newLinkedList();
}
/**
* Register a UUID service
*
* @param uuidService UUID service to register
*/
public void registerService(@NotNull final UUIDService uuidService) {
this.serviceList.add(uuidService);
}
/**
* Register a mapping consumer
*
* @param mappingConsumer Consumer to register
*/
public void registerConsumer(@NotNull final Consumer<UUIDMapping> mappingConsumer) {
this.consumerList.add(mappingConsumer);
}
/**
* Get a copy of the service list
*
* @return Copy of service list
*/
public List<UUIDService> getServiceListInstance() {
final List<UUIDService> serviceList = Lists.newLinkedList(this.serviceList);
serviceList.add(EndOfPipeline.instance);
return serviceList;
}
private void consume(@NotNull final UUIDMapping mapping) {
for (final Consumer<UUIDMapping> consumer : this.consumerList) {
consumer.accept(mapping);
}
}
/**
* Asynchronously attempt to fetch the mapping from a given UUID or username
*
* @param request UUID or username
* @return Future that may complete with the mapping
*/
public CompletableFuture<UUIDMapping> get(@NotNull final Object request) {
if (!(request instanceof String) && !(request instanceof UUID)) {
throw new IllegalArgumentException("Request has to be either a username or UUID");
}
final CompletableFuture<UUIDMapping> future = new CompletableFuture<>();
final ListIterator<UUIDService> serviceListIterator
= this.getServiceListInstance().listIterator();
final Runnable[] runnable = new Runnable[1];
runnable[0] = () -> {
if (serviceListIterator.hasNext()) {
final UUIDService uuidService = serviceListIterator.next();
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
: (String) result;
final UUID uuid = request instanceof UUID ? (UUID) request
: (UUID) result;
final UUIDMapping mapping = new UUIDMapping(uuid, username);
future.complete(mapping);
this.consume(mapping);
}
}), this.executor);
} else {
throw new ServiceError("Pipeline is incomplete");
}
};
try {
// 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;
}
}
}

View file

@ -0,0 +1,74 @@
/*
* _____ _ _ _____ _
* | __ \| | | | / ____| | |
* | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| |
* | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` |
* | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| |
* |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_|
* | |
* |_|
* 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 org.jetbrains.annotations.NotNull;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
/**
* Service used to provide usernames from player UUIDs
*/
public interface UUIDService {
default CompletableFuture<?> get(@NotNull final Object request) {
if (request instanceof UUID) {
return get((UUID) request);
} else if (request instanceof String) {
return get((String) request);
} else {
throw new IllegalArgumentException("Request has to be either a username or UUID");
}
}
/**
* Get a stored username from the service if it exists.
* This should <b>not</b> trigger any fetching of
* usernames from other services.
* <p>
* If the username isn't stored in this service,
* this completes with an empty optional.
*
* @param uuid Player UUID
* @return Future that may contain the username if it exists
*/
@NotNull CompletableFuture<String> get(@NotNull final UUID uuid);
/**
* Get a stored UUID from the service if it exists.
* This should <b>not</b> trigger any fetching of
* UUID from other services.
* <p>
* If the UUID isn't stored in this service,
* this completes with an empty optional.
*
* @param username Player username
* @return Future that may contain the UUID if it exists
*/
@NotNull CompletableFuture<UUID> get(@NotNull final String username);
}

View file

@ -0,0 +1,59 @@
/*
* _____ _ _ _____ _
* | __ \| | | | / ____| | |
* | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| |
* | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` |
* | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| |
* |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_|
* | |
* |_|
* 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.offline;
import com.google.common.base.Charsets;
import com.plotsquared.core.configuration.Settings;
import com.plotsquared.core.uuid.ServiceFailure;
import com.plotsquared.core.uuid.UUIDService;
import org.jetbrains.annotations.NotNull;
import java.util.Locale;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
/**
* Name provider service that creates UUIDs from usernames
*/
public class OfflineModeUUIDService implements UUIDService {
@NotNull protected final UUID getFromUsername(@NotNull String username) {
if (Settings.UUID.FORCE_LOWERCASE) {
username = username.toLowerCase(Locale.ENGLISH);
}
return UUID.nameUUIDFromBytes(("OfflinePlayer:" + username).getBytes(Charsets.UTF_8));
}
@Override @NotNull public CompletableFuture<String> get(@NotNull final UUID uuid) {
// This service can only get UUIDs from usernames
return ServiceFailure.getFuture();
}
@Override @NotNull public CompletableFuture<UUID> get(@NotNull final String username) {
return CompletableFuture.completedFuture(this.getFromUsername(username));
}
}