chunk sending / cfi

This commit is contained in:
Jesse Boyd
2019-10-26 14:21:49 +01:00
parent 1b28dcda40
commit 8356004ec9
56 changed files with 1378 additions and 999 deletions

View File

@ -3,6 +3,8 @@ package com.boydti.fawe.bukkit;
import com.boydti.fawe.Fawe;
import com.boydti.fawe.IFawe;
import com.boydti.fawe.beta.implementation.QueueHandler;
import com.boydti.fawe.beta.preloader.AsyncPreloader;
import com.boydti.fawe.beta.preloader.Preloader;
import com.boydti.fawe.bukkit.adapter.BukkitQueueHandler;
import com.boydti.fawe.bukkit.listener.BrushListener;
import com.boydti.fawe.bukkit.listener.BukkitImageListener;
@ -55,14 +57,14 @@ import org.bukkit.plugin.java.JavaPlugin;
public class FaweBukkit implements IFawe, Listener {
// private final WorldEditPlugin plugin;
private final Plugin plugin;
private VaultUtil vault;
private ItemUtil itemUtil;
private boolean listeningImages;
private BukkitImageListener imageListener;
//private CFIPacketListener packetListener;
public static boolean PAPER;
public VaultUtil getVault() {
return this.vault;
@ -70,6 +72,13 @@ public class FaweBukkit implements IFawe, Listener {
public FaweBukkit(Plugin plugin) {
this.plugin = plugin;
try {
Class.forName("com.destroystokyo.paper.Namespaced");
PAPER = true;
} catch (Throwable e) {
e.printStackTrace();
// TODO no paper
}
try {
Settings.IMP.TICK_LIMITER.ENABLED = !Bukkit.hasWhitelist();
Fawe.set(this);
@ -398,4 +407,12 @@ public class FaweBukkit implements IFawe, Listener {
return null;
// return ((BlocksHubBukkit) blocksHubPlugin).getApi();
}
@Override
public Preloader getPreloader() {
if (PAPER) {
return new AsyncPreloader();
}
return null;
}
}

View File

@ -2,6 +2,7 @@ package com.boydti.fawe.bukkit.adapter.mc1_14;
import com.boydti.fawe.Fawe;
import com.boydti.fawe.FaweCache;
import com.boydti.fawe.bukkit.FaweBukkit;
import com.boydti.fawe.bukkit.adapter.DelegateLock;
import com.boydti.fawe.config.Settings;
import com.boydti.fawe.object.collection.BitArray4096;
@ -21,6 +22,7 @@ import net.minecraft.server.v1_14_R1.DataPaletteBlock;
import net.minecraft.server.v1_14_R1.DataPaletteLinear;
import net.minecraft.server.v1_14_R1.GameProfileSerializer;
import net.minecraft.server.v1_14_R1.IBlockData;
import net.minecraft.server.v1_14_R1.PacketPlayOutMapChunk;
import net.minecraft.server.v1_14_R1.PlayerChunk;
import net.minecraft.server.v1_14_R1.PlayerChunkMap;
import org.bukkit.craftbukkit.v1_14_R1.CraftChunk;
@ -133,8 +135,6 @@ public class BukkitAdapter_1_14 {
}
}
private static boolean PAPER = true;
public static Chunk ensureLoaded(net.minecraft.server.v1_14_R1.World nmsWorld, int X, int Z) {
Chunk nmsChunk = nmsWorld.getChunkIfLoaded(X, Z);
if (nmsChunk != null) {
@ -143,7 +143,7 @@ public class BukkitAdapter_1_14 {
if (Fawe.isMainThread()) {
return nmsWorld.getChunkAt(X, Z);
}
if (PAPER) {
if (FaweBukkit.PAPER) {
CraftWorld craftWorld = nmsWorld.getWorld();
CompletableFuture<org.bukkit.Chunk> future = craftWorld.getChunkAtAsync(X, Z, true);
try {
@ -154,8 +154,7 @@ public class BukkitAdapter_1_14 {
} catch (ExecutionException e) {
e.printStackTrace();
} catch (Throwable e) {
System.out.println("Error, cannot load chunk async (paper not installed?)");
PAPER = false;
e.printStackTrace();
}
}
// TODO optimize

View File

@ -19,6 +19,7 @@
package com.boydti.fawe.bukkit.adapter.mc1_14;
import com.bekvon.bukkit.residence.commands.material;
import com.boydti.fawe.Fawe;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
@ -132,6 +133,7 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.OptionalInt;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.function.Supplier;
@ -590,6 +592,13 @@ public final class Spigot_v1_14_R4 extends CachedBukkitAdapter implements Bukkit
}
}
@Override
public OptionalInt getInternalBlockStateId(BlockState state) {
BlockMaterial_1_14 material = (BlockMaterial_1_14) state.getMaterial();
IBlockData mcState = material.getCraftBlockData().getState();
return OptionalInt.of(Block.REGISTRY_ID.getId(mcState));
}
@Override
public BlockState adapt(BlockData blockData) {
CraftBlockData cbd = ((CraftBlockData) blockData);

View File

@ -1,111 +0,0 @@
package com.boydti.fawe.bukkit.chat;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collection;
import org.apache.commons.lang.Validate;
/**
* Represents a wrapper around an array class of an arbitrary reference type,
* which properly implements "value" hash code and equality functions.
* <p>
* This class is intended for use as a key to a map.
* </p>
*
* @param <E> The type of elements in the array.
* @author Glen Husman
* @see Arrays
*/
public final class ArrayWrapper<E> {
/**
* Creates an array wrapper with some elements.
*
* @param elements The elements of the array.
*/
@SafeVarargs
public ArrayWrapper(E... elements) {
setArray(elements);
}
private E[] _array;
/**
* Retrieves a reference to the wrapped array instance.
*
* @return The array wrapped by this instance.
*/
public E[] getArray() {
return _array;
}
/**
* Set this wrapper to wrap a new array instance.
*
* @param array The new wrapped array.
*/
public void setArray(E[] array) {
Validate.notNull(array, "The array must not be null.");
_array = array;
}
/**
* Determines if this object has a value equivalent to another object.
*
* @see Arrays#equals(Object[], Object[])
*/
@SuppressWarnings("rawtypes")
@Override
public boolean equals(Object other) {
if (!(other instanceof ArrayWrapper)) {
return false;
}
return Arrays.equals(_array, ((ArrayWrapper) other)._array);
}
/**
* Gets the hash code represented by this objects value.
*
* @return This object's hash code.
* @see Arrays#hashCode(Object[])
*/
@Override
public int hashCode() {
return Arrays.hashCode(_array);
}
/**
* Converts an iterable element collection to an array of elements.
* The iteration order of the specified object will be used as the array element order.
*
* @param list The iterable of objects which will be converted to an array.
* @param c The type of the elements of the array.
* @return An array of elements in the specified iterable.
*/
@SuppressWarnings("unchecked")
public static <T> T[] toArray(Iterable<? extends T> list, Class<T> c) {
int size = -1;
if (list instanceof Collection<?>) {
@SuppressWarnings("rawtypes")
Collection coll = (Collection) list;
size = coll.size();
}
if (size < 0) {
size = 0;
// Ugly hack: Count it ourselves
for (@SuppressWarnings("unused") T element : list) {
size++;
}
}
T[] result = (T[]) Array.newInstance(c, size);
int i = 0;
for (T element : list) { // Assumes iteration order is consistent
result[i++] = element; // Assign array element at index THEN increment counter
}
return result;
}
}

View File

@ -1,18 +0,0 @@
package com.boydti.fawe.bukkit.chat;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
/**
* Represents an object that can be serialized to a JSON writer instance.
*/
interface JsonRepresentedObject {
/**
* Writes the JSON representation of this object to the specified writer.
* @param writer The JSON writer which will receive the object.
* @throws IOException If an error occurs writing to the stream.
*/
public void writeJson(JsonWriter writer) throws IOException;
}

View File

@ -1,46 +0,0 @@
package com.boydti.fawe.bukkit.chat;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
/**
* Represents a JSON string value.
* Writes by this object will not write name values nor begin/end objects in the JSON stream.
* All writes merely write the represented string value.
*/
final class JsonString implements JsonRepresentedObject, ConfigurationSerializable {
private String _value;
public JsonString(CharSequence value) {
_value = value == null ? null : value.toString();
}
@Override
public void writeJson(JsonWriter writer) throws IOException {
writer.value(getValue());
}
public String getValue() {
return _value;
}
public Map<String, Object> serialize() {
HashMap<String, Object> theSingleValue = new HashMap<>();
theSingleValue.put("stringValue", _value);
return theSingleValue;
}
public static JsonString deserialize(Map<String, Object> map) {
return new JsonString(map.get("stringValue").toString());
}
@Override
public String toString() {
return _value;
}
}

View File

@ -1,201 +0,0 @@
package com.boydti.fawe.bukkit.chat;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.Bukkit;
/**
* A class containing static utility methods and caches which are intended as reflective conveniences.
* Unless otherwise noted, upon failure methods will return {@code null}.
*/
public final class Reflection {
/**
* Stores loaded classes from the {@code net.minecraft.server} package.
*/
private static final Map<String, Class<?>> _loadedNMSClasses = new HashMap<>();
/**
* Stores loaded classes from the {@code org.bukkit.craftbukkit} package (and subpackages).
*/
private static final Map<String, Class<?>> _loadedOBCClasses = new HashMap<>();
private static final Map<Class<?>, Map<String, Field>> _loadedFields = new HashMap<>();
/**
* Contains loaded methods in a cache.
* The map maps [types to maps of [method names to maps of [parameter types to method instances]]].
*/
private static final Map<Class<?>, Map<String, Map<ArrayWrapper<Class<?>>, Method>>> _loadedMethods = new HashMap<>();
private static String _versionString;
private Reflection() { }
/**
* Gets the version string from the package name of the CraftBukkit server implementation.
* This is needed to bypass the JAR package name changing on each update.
*
* @return The version string of the OBC and NMS packages, <em>including the trailing dot</em>.
*/
public synchronized static String getVersion() {
if (_versionString == null) {
String name = Bukkit.getServer().getClass().getPackage().getName();
_versionString = name.substring(name.lastIndexOf('.') + 1) + ".";
}
return _versionString;
}
/**
* Gets a {@link Class} object representing a type contained within the {@code net.minecraft.server} versioned package.
* The class instances returned by this method are cached, such that no lookup will be done twice (unless multiple threads are accessing this method simultaneously).
*
* @param className The name of the class, excluding the package, within NMS.
* @return The class instance representing the specified NMS class, or {@code null} if it could not be loaded.
*/
public synchronized static Class<?> getNMSClass(String className) {
if (_loadedNMSClasses.containsKey(className)) {
return _loadedNMSClasses.get(className);
}
String fullName = "net.minecraft.server." + getVersion() + className;
Class<?> clazz;
try {
clazz = Class.forName(fullName);
} catch (ClassNotFoundException e) {
_loadedNMSClasses.put(className, null);
throw new RuntimeException(e);
}
_loadedNMSClasses.put(className, clazz);
return clazz;
}
/**
* Gets a {@link Class} object representing a type contained within the {@code org.bukkit.craftbukkit} versioned package.
* The class instances returned by this method are cached, such that no lookup will be done twice (unless multiple threads are accessing this method simultaneously).
*
* @param className The name of the class, excluding the package, within OBC. This name may contain a subpackage name, such as {@code inventory.CraftItemStack}.
* @return The class instance representing the specified OBC class, or {@code null} if it could not be loaded.
*/
public synchronized static Class<?> getOBCClass(String className) {
if (_loadedOBCClasses.containsKey(className)) {
return _loadedOBCClasses.get(className);
}
String fullName = "org.bukkit.craftbukkit." + getVersion() + className;
Class<?> clazz;
try {
clazz = Class.forName(fullName);
} catch (ClassNotFoundException e) {
_loadedOBCClasses.put(className, null);
throw new RuntimeException(e);
}
_loadedOBCClasses.put(className, clazz);
return clazz;
}
/**
* Attempts to get the NMS handle of a CraftBukkit object.
* <p>
* The only match currently attempted by this method is a retrieval by using a parameterless {@code getHandle()} method implemented by the runtime type of the specified object.
* </p>
*
* @param obj The object for which to retrieve an NMS handle.
* @return The NMS handle of the specified object, or {@code null} if it could not be retrieved using {@code getHandle()}.
*/
public synchronized static Object getHandle(Object obj) throws InvocationTargetException, IllegalAccessException, IllegalArgumentException {
return getMethod(obj.getClass(), "getHandle").invoke(obj);
}
/**
* Retrieves a {@link Field} instance declared by the specified class with the specified name.
* Java access modifiers are ignored during this retrieval. No guarantee is made as to whether the field
* returned will be an instance or static field.
* <p>
* A global caching mechanism within this class is used to store fields. Combined with synchronization, this guarantees that
* no field will be reflectively looked up twice.
* </p>
* <p>
* If a field is deemed suitable for return, {@link Field#setAccessible(boolean) setAccessible} will be invoked with an argument of {@code true} before it is returned.
* This ensures that callers do not have to check or worry about Java access modifiers when dealing with the returned instance.
* </p>
*
* @param clazz The class which contains the field to retrieve.
* @param name The declared name of the field in the class.
* @return A field object with the specified name declared by the specified class.
* @see Class#getDeclaredField(String)
*/
public synchronized static Field getField(Class<?> clazz, String name) {
Map<String, Field> loaded;
if (!_loadedFields.containsKey(clazz)) {
loaded = new HashMap<>();
_loadedFields.put(clazz, loaded);
} else {
loaded = _loadedFields.get(clazz);
}
if (loaded.containsKey(name)) {
// If the field is loaded (or cached as not existing), return the relevant value, which might be null
return loaded.get(name);
}
try {
Field field = clazz.getDeclaredField(name);
field.setAccessible(true);
loaded.put(name, field);
return field;
} catch (NoSuchFieldException | SecurityException e) {
// Error loading
e.printStackTrace();
// Cache field as not existing
loaded.put(name, null);
return null;
}
}
/**
* Retrieves a {@link Method} instance declared by the specified class with the specified name and argument types.
* Java access modifiers are ignored during this retrieval. No guarantee is made as to whether the field
* returned will be an instance or static field.
* <p>
* A global caching mechanism within this class is used to store method. Combined with synchronization, this guarantees that
* no method will be reflectively looked up twice.
* <p>
* If a method is deemed suitable for return, {@link Method#setAccessible(boolean) setAccessible} will be invoked with an argument of {@code true} before it is returned.
* This ensures that callers do not have to check or worry about Java access modifiers when dealing with the returned instance.
* <p>
* This method does <em>not</em> search superclasses of the specified type for methods with the specified signature.
* Callers wishing this behavior should use {@link Class#getDeclaredMethod(String, Class...)}.
*
* @param clazz The class which contains the method to retrieve.
* @param name The declared name of the method in the class.
* @param args The formal argument types of the method.
* @return A method object with the specified name declared by the specified class.
*/
public synchronized static Method getMethod(Class<?> clazz, String name, Class<?>... args) {
if (!_loadedMethods.containsKey(clazz)) {
_loadedMethods.put(clazz, new HashMap<>());
}
Map<String, Map<ArrayWrapper<Class<?>>, Method>> loadedMethodNames = _loadedMethods.get(clazz);
if (!loadedMethodNames.containsKey(name)) {
loadedMethodNames.put(name, new HashMap<>());
}
Map<ArrayWrapper<Class<?>>, Method> loadedSignatures = loadedMethodNames.get(name);
ArrayWrapper<Class<?>> wrappedArg = new ArrayWrapper<>(args);
if (loadedSignatures.containsKey(wrappedArg)) {
return loadedSignatures.get(wrappedArg);
}
for (Method m : clazz.getMethods()) {
if (m.getName().equals(name) && Arrays.equals(args, m.getParameterTypes())) {
m.setAccessible(true);
loadedSignatures.put(wrappedArg, m);
return m;
}
}
loadedSignatures.put(wrappedArg, null);
return null;
}
}

View File

@ -1,4 +0,0 @@
package com.boydti.fawe.bukkit.listener;
public class ChunkCache {
}

View File

@ -0,0 +1,196 @@
package com.boydti.fawe.bukkit.preloader;
import com.boydti.fawe.Fawe;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.math.BlockVector2;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.regions.Region;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.generator.ChunkGenerator;
import org.bukkit.plugin.PluginBase;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginLoader;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.io.InputStream;
import java.lang.ref.PhantomReference;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Logger;
public class PluginPreloader extends PluginBase {
private World world;
private Set<BlockVector2> loaded;
private int index;
private AtomicBoolean invalidator;
private final Object invalidatorLock;
public PluginPreloader() {
invalidator = new AtomicBoolean();
invalidatorLock = new Object();
}
public AtomicBoolean invalidate() {
synchronized (invalidatorLock) {
invalidator.set(false);
return invalidator = new AtomicBoolean(true);
}
}
private synchronized void unload() {
World oldWorld = world;
if (oldWorld != null) {
Set<BlockVector2> toUnload = loaded;
if (loaded != null && index > 0) {
Iterator<BlockVector2> iter = toUnload.iterator();
Fawe.get().getQueueHandler().sync(() -> {
for (int i = 0; i < index && iter.hasNext(); i++) {
BlockVector2 chunk = iter.next();
world.removePluginChunkTicket(chunk.getX(), chunk.getZ(), this);
}
});
}
}
this.world = null;
this.loaded = null;
this.index = 0;
}
public void update(Region region) {
AtomicBoolean invalidator = invalidate();
synchronized (this) {
com.sk89q.worldedit.world.World weWorld = region.getWorld();
if (weWorld == null) {
return;
}
unload();
index = 0;
world = BukkitAdapter.adapt(weWorld);
loaded = region.getChunks();
Iterator<BlockVector2> iter = loaded.iterator();
if (!invalidator.get()) return;
Fawe.get().getQueueHandler().syncWhenFree(() -> {
for (; iter.hasNext() && invalidator.get();index++) {
BlockVector2 chunk = iter.next();
if (!world.isChunkLoaded(chunk.getX(), chunk.getZ())) {
world.addPluginChunkTicket(chunk.getX(), chunk.getZ(), this);
}
}
});
}
}
public void clear() {
invalidate();
unload();
}
@Override
public @NotNull File getDataFolder() {
return null;
}
@Override
public @NotNull PluginDescriptionFile getDescription() {
return null;
}
@Override
public @NotNull FileConfiguration getConfig() {
return null;
}
@Override
public @Nullable InputStream getResource(@NotNull String filename) {
return null;
}
@Override
public void saveConfig() {
}
@Override
public void saveDefaultConfig() {
}
@Override
public void saveResource(@NotNull String resourcePath, boolean replace) {
}
@Override
public void reloadConfig() {
}
@Override
public @NotNull PluginLoader getPluginLoader() {
return null;
}
@Override
public @NotNull Server getServer() {
return null;
}
@Override
public boolean isEnabled() {
return true;
}
@Override
public void onDisable() {
}
@Override
public void onLoad() {
}
@Override
public void onEnable() {
}
@Override
public boolean isNaggable() {
return false;
}
@Override
public void setNaggable(boolean canNag) {
}
@Override
public @Nullable ChunkGenerator getDefaultWorldGenerator(@NotNull String worldName, @Nullable String id) {
return null;
}
@Override
public @NotNull Logger getLogger() {
return null;
}
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
return false;
}
@Override
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, String[] args) {
return null;
}
}

View File

@ -19,173 +19,173 @@ import org.bukkit.entity.ItemFrame;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
//import org.inventivetalent.mapmanager.MapManagerPlugin;
//import org.inventivetalent.mapmanager.controller.MapController;
//import org.inventivetalent.mapmanager.controller.MultiMapController;
//import org.inventivetalent.mapmanager.manager.MapManager;
//import org.inventivetalent.mapmanager.wrapper.MapWrapper;
import org.inventivetalent.mapmanager.MapManagerPlugin;
import org.inventivetalent.mapmanager.controller.MapController;
import org.inventivetalent.mapmanager.controller.MultiMapController;
import org.inventivetalent.mapmanager.manager.MapManager;
import org.inventivetalent.mapmanager.wrapper.MapWrapper;
public class BukkitImageViewer implements ImageViewer {
// private final MapManager mapManager;
// private final Player player;
// private BufferedImage last;
private final MapManager mapManager;
private final Player player;
private BufferedImage last;
private ItemFrame[][] frames;
// private boolean reverse;
private boolean reverse;
public BukkitImageViewer(Player player) {
// mapManager = ((MapManagerPlugin) Bukkit.getPluginManager().getPlugin("MapManager")).getMapManager();
// this.player = player;
mapManager = ((MapManagerPlugin) Bukkit.getPluginManager().getPlugin("MapManager")).getMapManager();
this.player = player;
}
//
public void selectFrame(ItemFrame start) {
// Location pos1 = start.getLocation().clone();
// Location pos2 = start.getLocation().clone();
//
// BlockFace facing = start.getFacing();
// int planeX = facing.getModX() == 0 ? 1 : 0;
// int planeY = facing.getModY() == 0 ? 1 : 0;
// int planeZ = facing.getModZ() == 0 ? 1 : 0;
//
// ItemFrame[][] res = find(pos1, pos2, facing);
// Location tmp;
// while (true) {
// if (res != null) {
// frames = res;
// }
// tmp = pos1.clone().subtract(planeX, planeY, planeZ);
// if ((res = find(tmp, pos2, facing)) != null) {
// pos1 = tmp;
// continue;
// }
// tmp = pos2.clone().add(planeX, planeY, planeZ);
// if ((res = find(pos1, tmp, facing)) != null) {
// pos2 = tmp;
// continue;
// }
// tmp = pos1.clone().subtract(planeX, 0, planeZ);
// if ((res = find(tmp, pos2, facing)) != null) {
// pos1 = tmp;
// continue;
// }
// tmp = pos2.clone().add(planeX, 0, planeZ);
// if ((res = find(pos1, tmp, facing)) != null) {
// pos2 = tmp;
// continue;
// }
// tmp = pos1.clone().subtract(0, 1, 0);
// if ((res = find(tmp, pos2, facing)) != null) {
// pos1 = tmp;
// continue;
// }
// tmp = pos2.clone().add(0, 1, 0);
// if ((res = find(pos1, tmp, facing)) != null) {
// pos2 = tmp;
// continue;
// }
// break;
// }
Location pos1 = start.getLocation().clone();
Location pos2 = start.getLocation().clone();
BlockFace facing = start.getFacing();
int planeX = facing.getModX() == 0 ? 1 : 0;
int planeY = facing.getModY() == 0 ? 1 : 0;
int planeZ = facing.getModZ() == 0 ? 1 : 0;
ItemFrame[][] res = find(pos1, pos2, facing);
Location tmp;
while (true) {
if (res != null) {
frames = res;
}
tmp = pos1.clone().subtract(planeX, planeY, planeZ);
if ((res = find(tmp, pos2, facing)) != null) {
pos1 = tmp;
continue;
}
tmp = pos2.clone().add(planeX, planeY, planeZ);
if ((res = find(pos1, tmp, facing)) != null) {
pos2 = tmp;
continue;
}
tmp = pos1.clone().subtract(planeX, 0, planeZ);
if ((res = find(tmp, pos2, facing)) != null) {
pos1 = tmp;
continue;
}
tmp = pos2.clone().add(planeX, 0, planeZ);
if ((res = find(pos1, tmp, facing)) != null) {
pos2 = tmp;
continue;
}
tmp = pos1.clone().subtract(0, 1, 0);
if ((res = find(tmp, pos2, facing)) != null) {
pos1 = tmp;
continue;
}
tmp = pos2.clone().add(0, 1, 0);
if ((res = find(pos1, tmp, facing)) != null) {
pos2 = tmp;
continue;
}
break;
}
}
//
public ItemFrame[][] getItemFrames() {
return frames;
}
//
// private ItemFrame[][] find(Location pos1, Location pos2, BlockFace facing) {
// try {
// Location distance = pos2.clone().subtract(pos1).add(1, 1, 1);
// int width = Math.max(distance.getBlockX(), distance.getBlockZ());
// ItemFrame[][] frames = new ItemFrame[width][distance.getBlockY()];
//
// World world = pos1.getWorld();
//
// this.reverse = (facing == BlockFace.NORTH || facing == BlockFace.EAST);
// int v = 0;
// for (double y = pos1.getY(); y <= pos2.getY(); y++, v++) {
// int h = 0;
// for (double z = pos1.getZ(); z <= pos2.getZ(); z++) {
// for (double x = pos1.getX(); x <= pos2.getX(); x++, h++) {
// Location pos = new Location(world, x, y, z);
// Collection<Entity> entities = world.getNearbyEntities(pos, 0.1, 0.1, 0.1);
// boolean contains = false;
// for (Entity ent : entities) {
// if (ent instanceof ItemFrame && ((ItemFrame) ent).getFacing() == facing) {
// ItemFrame itemFrame = (ItemFrame) ent;
// itemFrame.setRotation(Rotation.NONE);
// contains = true;
// frames[reverse ? width - 1 - h : h][v] = (ItemFrame) ent;
// break;
// }
// }
// if (!contains) return null;
// }
// }
// }
// return frames;
// } catch (Throwable e) {
// e.printStackTrace();
// }
// return null;
// }
private ItemFrame[][] find(Location pos1, Location pos2, BlockFace facing) {
try {
Location distance = pos2.clone().subtract(pos1).add(1, 1, 1);
int width = Math.max(distance.getBlockX(), distance.getBlockZ());
ItemFrame[][] frames = new ItemFrame[width][distance.getBlockY()];
World world = pos1.getWorld();
this.reverse = (facing == BlockFace.NORTH || facing == BlockFace.EAST);
int v = 0;
for (double y = pos1.getY(); y <= pos2.getY(); y++, v++) {
int h = 0;
for (double z = pos1.getZ(); z <= pos2.getZ(); z++) {
for (double x = pos1.getX(); x <= pos2.getX(); x++, h++) {
Location pos = new Location(world, x, y, z);
Collection<Entity> entities = world.getNearbyEntities(pos, 0.1, 0.1, 0.1);
boolean contains = false;
for (Entity ent : entities) {
if (ent instanceof ItemFrame && ((ItemFrame) ent).getFacing() == facing) {
ItemFrame itemFrame = (ItemFrame) ent;
itemFrame.setRotation(Rotation.NONE);
contains = true;
frames[reverse ? width - 1 - h : h][v] = (ItemFrame) ent;
break;
}
}
if (!contains) return null;
}
}
}
return frames;
} catch (Throwable e) {
e.printStackTrace();
}
return null;
}
@Override
public void view(Drawable drawable) {
// view(null, drawable);
view(null, drawable);
}
//
// private void view(@Nullable BufferedImage image, @Nullable Drawable drawable) {
// if (image == null && drawable == null) throw new IllegalArgumentException("An image or drawable must be provided. Both cannot be null");
// boolean initializing = last == null;
//
// if (this.frames != null) {
// if (image == null && drawable != null) image = drawable.draw();
// last = image;
// int width = frames.length;
// int height = frames[0].length;
// BufferedImage scaled = ImageUtil.getScaledInstance(image, 128 * width, 128 * height, RenderingHints.VALUE_INTERPOLATION_BILINEAR, false);
// MapWrapper mapWrapper = mapManager.wrapMultiImage(scaled, width, height);
// MultiMapController controller = (MultiMapController) mapWrapper.getController();
// controller.addViewer(player);
// controller.sendContent(player);
// controller.showInFrames(player, frames, true);
// } else {
// int slot = getMapSlot(player);
// if (slot == -1) {
// if (initializing) {
// player.getInventory().setItemInMainHand(new ItemStack(Material.MAP));
// } else {
// return;
// }
// } else if (player.getInventory().getHeldItemSlot() != slot) {
// player.getInventory().setHeldItemSlot(slot);
// }
// if (image == null && drawable != null) image = drawable.draw();
// last = image;
// BufferedImage scaled = ImageUtil.getScaledInstance(image, 128, 128, RenderingHints.VALUE_INTERPOLATION_BILINEAR, false);
// MapWrapper mapWrapper = mapManager.wrapImage(scaled);
// MapController controller = mapWrapper.getController();
// controller.addViewer(player);
// controller.sendContent(player);
// controller.showInHand(player, true);
// }
// }
//
// private int getMapSlot(Player player) {
// PlayerInventory inventory = player.getInventory();
// for (int i = 0; i < 9; i++) {
// ItemStack item = inventory.getItem(i);
// if (item != null && item.getType() == Material.MAP) {
// return i;
// }
// }
// return -1;
// }
//
private void view(@Nullable BufferedImage image, @Nullable Drawable drawable) {
if (image == null && drawable == null) throw new IllegalArgumentException("An image or drawable must be provided. Both cannot be null");
boolean initializing = last == null;
if (this.frames != null) {
if (image == null && drawable != null) image = drawable.draw();
last = image;
int width = frames.length;
int height = frames[0].length;
BufferedImage scaled = ImageUtil.getScaledInstance(image, 128 * width, 128 * height, RenderingHints.VALUE_INTERPOLATION_BILINEAR, false);
MapWrapper mapWrapper = mapManager.wrapMultiImage(scaled, width, height);
MultiMapController controller = (MultiMapController) mapWrapper.getController();
controller.addViewer(player);
controller.sendContent(player);
controller.showInFrames(player, frames, true);
} else {
int slot = getMapSlot(player);
if (slot == -1) {
if (initializing) {
player.getInventory().setItemInMainHand(new ItemStack(Material.MAP));
} else {
return;
}
} else if (player.getInventory().getHeldItemSlot() != slot) {
player.getInventory().setHeldItemSlot(slot);
}
if (image == null && drawable != null) image = drawable.draw();
last = image;
BufferedImage scaled = ImageUtil.getScaledInstance(image, 128, 128, RenderingHints.VALUE_INTERPOLATION_BILINEAR, false);
MapWrapper mapWrapper = mapManager.wrapImage(scaled);
MapController controller = mapWrapper.getController();
controller.addViewer(player);
controller.sendContent(player);
controller.showInHand(player, true);
}
}
private int getMapSlot(Player player) {
PlayerInventory inventory = player.getInventory();
for (int i = 0; i < 9; i++) {
ItemStack item = inventory.getItem(i);
if (item != null && item.getType() == Material.MAP) {
return i;
}
}
return -1;
}
public void refresh() {
// if (last != null) view(last, null);
if (last != null) view(last, null);
}
@Override
public void close() throws IOException {
// last = null;
last = null;
}
}

View File

@ -34,6 +34,7 @@ import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.OptionalInt;
public class BukkitBlockRegistry extends BundledBlockRegistry {
@ -93,6 +94,10 @@ public class BukkitBlockRegistry extends BundledBlockRegistry {
this.material = bukkitMaterial;
}
public int getId() {
return material.getId();
}
@Override
public boolean isAir() {
switch (material) {
@ -132,4 +137,9 @@ public class BukkitBlockRegistry extends BundledBlockRegistry {
}
return blocks;
}
@Override
public OptionalInt getInternalBlockStateId(BlockState state) {
return WorldEditPlugin.getInstance().getBukkitImplAdapter().getInternalBlockStateId(state);
}
}

View File

@ -24,6 +24,10 @@ import com.boydti.fawe.bukkit.FaweBukkit;
import com.boydti.fawe.config.Settings;
import com.boydti.fawe.object.RunnableVal;
import com.boydti.fawe.util.TaskManager;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.ProtocolManager;
import com.comphenix.protocol.injector.netty.WirePacket;
import com.sk89q.util.StringUtil;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.WorldEditException;
@ -47,10 +51,13 @@ import com.sk89q.worldedit.world.block.BlockStateHolder;
import com.sk89q.worldedit.world.block.BlockTypes;
import com.sk89q.worldedit.world.gamemode.GameMode;
import com.sk89q.worldedit.world.gamemode.GameModes;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;
import java.util.function.Supplier;
import javax.annotation.Nullable;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
@ -359,6 +366,21 @@ public class BukkitPlayer extends AbstractPlayerActor {
}
}
@Override
public void sendFakeChunk(int chunkX, int chunkZ, Supplier<byte[]> data) {
ProtocolManager manager = ProtocolLibrary.getProtocolManager();
// check if the chunk is in range
if (true) {
try {
byte[] raw = data.get();
WirePacket packet = new WirePacket(PacketType.Play.Server.MAP_CHUNK, raw);
manager.sendWirePacket(getPlayer(), packet);
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
@Override
public void sendTitle(String title, String sub) {
player.sendTitle(ChatColor.GOLD + title, ChatColor.GOLD + sub, 0, 70, 20);

View File

@ -21,7 +21,9 @@ package com.sk89q.worldedit.bukkit;
import static com.google.common.base.Preconditions.checkNotNull;
import com.boydti.fawe.Fawe;
import com.boydti.fawe.beta.IChunkGet;
import com.boydti.fawe.bukkit.FaweBukkit;
import com.boydti.fawe.bukkit.adapter.mc1_14.BukkitGetBlocks_1_14;
import com.boydti.fawe.config.Settings;
import com.sk89q.jnbt.CompoundTag;
@ -317,8 +319,19 @@ public class BukkitWorld extends AbstractWorld {
@Override
public void checkLoadedChunk(BlockVector3 pt) {
World world = getWorld();
world.getChunkAt(pt.getBlockX() >> 4, pt.getBlockZ() >> 4);
int X = pt.getBlockX() >> 4;
int Z = pt.getBlockZ() >> 4;
if (Fawe.isMainThread()) {
world.getChunkAt(X, Z);
} else if (!world.isChunkLoaded(X, Z)) {
if (FaweBukkit.PAPER) {
world.getChunkAtAsync(X, Z, true);
} else {
Fawe.get().getQueueHandler().sync(() -> {
world.getChunkAt(X, Z);
});
}
}
}
@Override
@ -516,7 +529,8 @@ public class BukkitWorld extends AbstractWorld {
}
@Override
public void sendChunk(int X, int Z, int mask) {
public void refreshChunk(int X, int Z) {
getWorld().refreshChunk(X, Z);
}
@Override

View File

@ -84,6 +84,7 @@ import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.world.ChunkUnloadEvent;
import org.bukkit.event.world.WorldInitEvent;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.metadata.MetadataValue;
@ -560,16 +561,24 @@ public class WorldEditPlugin extends JavaPlugin { //implements TabCompleter
*/
public BukkitPlayer wrapPlayer(Player player) {
synchronized (player) {
@NotNull List<MetadataValue> meta = player.getMetadata("WE");
if (meta == null || meta.isEmpty()) {
BukkitPlayer wePlayer = new BukkitPlayer(this, player);
BukkitPlayer wePlayer = getCachedPlayer(player);
if (wePlayer == null) {
wePlayer = new BukkitPlayer(this, player);
player.setMetadata("WE", new FixedMetadataValue(this, wePlayer));
return wePlayer;
}
return (BukkitPlayer) meta.get(0).value();
return wePlayer;
}
}
public BukkitPlayer getCachedPlayer(Player player) {
List<MetadataValue> meta = player.getMetadata("WE");
if (meta == null || meta.isEmpty()) {
return null;
}
return (BukkitPlayer) meta.get(0).value();
}
public Actor wrapCommandSender(CommandSender sender) {
if (sender instanceof Player) {
return wrapPlayer((Player) sender);