From b97b40e41362b0a17a73efa43c635b9f9ac71a9c Mon Sep 17 00:00:00 2001 From: Jesse Boyd Date: Sat, 2 Nov 2019 19:31:32 +0100 Subject: [PATCH] various use vector.add for compatibility with vs-flattened flush before remember share meta objects across players fix wg compatibility --- .../sk89q/worldedit/bukkit/BukkitPlayer.java | 18 ++++++- .../util/commands/SimpleInjector.java | 53 +++++++++++++++++++ .../platform/AbstractPlayerActor.java | 10 +++- .../platform/PlatformCommandManager.java | 3 +- .../sk89q/worldedit/math/BlockVector3.java | 5 ++ 5 files changed, 84 insertions(+), 5 deletions(-) create mode 100644 worldedit-core/src/main/java/com/sk89q/minecraft/util/commands/SimpleInjector.java diff --git a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitPlayer.java b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitPlayer.java index bb474f380..a7b2dd512 100644 --- a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitPlayer.java +++ b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitPlayer.java @@ -62,6 +62,7 @@ import java.util.HashMap; import java.util.Locale; import java.util.Map; import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; public class BukkitPlayer extends AbstractPlayerActor { @@ -69,18 +70,31 @@ public class BukkitPlayer extends AbstractPlayerActor { private WorldEditPlugin plugin; public BukkitPlayer(Player player) { - this(WorldEditPlugin.getInstance(), player); - Fawe.debug("Should not construct BukkitPlayer. Instead use BukkitAdapter.adapt(player)"); + super(getExistingMap(WorldEditPlugin.getInstance(), player)); + this.plugin = WorldEditPlugin.getInstance(); + this.player = player; } public BukkitPlayer(WorldEditPlugin plugin, Player player) { this.plugin = plugin; this.player = player; + init(); + } + + private void init() { if (Settings.IMP.CLIPBOARD.USE_DISK) { loadClipboardFromDisk(); } } + private static Map getExistingMap(WorldEditPlugin plugin, Player player) { + BukkitPlayer cached = plugin.getCachedPlayer(player); + if (cached != null) { + return cached.getRawMeta(); + } + return new ConcurrentHashMap<>(); + } + @Override public UUID getUniqueId() { return player.getUniqueId(); diff --git a/worldedit-core/src/main/java/com/sk89q/minecraft/util/commands/SimpleInjector.java b/worldedit-core/src/main/java/com/sk89q/minecraft/util/commands/SimpleInjector.java new file mode 100644 index 000000000..4ffefe26a --- /dev/null +++ b/worldedit-core/src/main/java/com/sk89q/minecraft/util/commands/SimpleInjector.java @@ -0,0 +1,53 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * Copyright (C) WorldEdit team and contributors + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser 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 Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package com.sk89q.minecraft.util.commands; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; + +public class SimpleInjector implements Injector { + + private static final Logger log = LoggerFactory.getLogger(SimpleInjector.class); + private Object[] args; + private Class[] argClasses; + + public SimpleInjector(Object... args) { + this.args = args; + argClasses = new Class[args.length]; + for (int i = 0; i < args.length; ++i) { + argClasses[i] = args[i].getClass(); + } + } + + @Override + public Object getInstance(Class clazz) { + try { + Constructor ctr = clazz.getConstructor(argClasses); + ctr.setAccessible(true); + return ctr.newInstance(args); + } catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) { + log.error("Error initializing commands class " + clazz, e); + return null; + } + } +} \ No newline at end of file diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/AbstractPlayerActor.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/AbstractPlayerActor.java index 41f626546..12ba7e815 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/AbstractPlayerActor.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/AbstractPlayerActor.java @@ -81,7 +81,15 @@ import org.jetbrains.annotations.NotNull; */ public abstract class AbstractPlayerActor implements Actor, Player, Cloneable { - private final ConcurrentHashMap meta = new ConcurrentHashMap<>(); + private final Map meta; + + public AbstractPlayerActor(Map meta) { + this.meta = meta; + } + + public AbstractPlayerActor() { + this(new ConcurrentHashMap<>()); + } @Override public Map getRawMeta() { diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandManager.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandManager.java index 1d6d2000f..582e00d81 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandManager.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandManager.java @@ -727,14 +727,13 @@ public final class PlatformCommandManager { if (context instanceof MemoizingValueAccess) { context = ((MemoizingValueAccess) context).snapshotMemory(); } else { - System.out.println("Invalid context " + context); } Optional editSessionOpt = context.injectedValue(Key.of(EditSession.class)); if (editSessionOpt.isPresent()) { EditSession editSession = editSessionOpt.get(); - session.remember(editSession); editSession.flushQueue(); + session.remember(editSession); long time = System.currentTimeMillis() - start; if (time > 1000) { diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/math/BlockVector3.java b/worldedit-core/src/main/java/com/sk89q/worldedit/math/BlockVector3.java index db39ad5e7..adf2d3f1c 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/math/BlockVector3.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/math/BlockVector3.java @@ -779,4 +779,9 @@ public abstract class BlockVector3 { return "(" + getX() + ", " + getY() + ", " + getZ() + ")"; } + //Used by VS fork + public BlockVector3 plus(BlockVector3 other) { + return add(other); + } + }