Fix for forge

This commit is contained in:
Jesse Boyd 2016-06-06 18:46:02 +10:00
parent ed08bf8a9c
commit b30007a4e4
6 changed files with 489 additions and 19 deletions

View File

@ -488,9 +488,6 @@ public class BukkitQueue18R3 extends BukkitQueue_0<Chunk, ChunkSection[], char[]
} }
} }
} }
nmsChunk.initLighting();
if (flag) { if (flag) {
if (mode == RelightMode.ALL) { if (mode == RelightMode.ALL) {
nmsChunk.initLighting(); nmsChunk.initLighting();

View File

@ -0,0 +1,418 @@
package com.boydti.fawe.config;
import com.boydti.fawe.Fawe;
import com.boydti.fawe.configuration.MemorySection;
import com.boydti.fawe.configuration.file.YamlConfiguration;
import com.boydti.fawe.util.StringMan;
import java.io.File;
import java.io.PrintWriter;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Config {
// TODO
/**
* Get the value for a node<br>
* Probably throws some error if you try to get a non existent key
* @param key
* @param <T>
* @return
*/
public static <T> T get(String key, Class root) {
String[] split = key.split("\\.");
Object instance = getInstance(split, root);
if (instance != null) {
Field field = getField(split, instance);
if (field != null) {
try {
return (T) field.get(instance);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
Fawe.debug("Failed to get config option: " + key);
return null;
}
/**
* Set the value of a specific node<br>
* Probably throws some error if you supply non existing keys or invalid values
* @param key config node
* @param value value
*
*/
public static void set(String key, Object value, Class root) {
String[] split = key.split("\\.");
Object instance = getInstance(split, root);
if (instance != null) {
Field field = getField(split, instance);
if (field != null) {
try {
if (field.getAnnotation(Final.class) != null) {
return;
}
field.set(instance, value);
return;
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
Fawe.debug("Failed to set config option: " + key + ": " + value + " | " + instance);
}
public static boolean load(File file, Class root) {
if (!file.exists()) {
return false;
}
YamlConfiguration yml = YamlConfiguration.loadConfiguration(file);
for (String key : yml.getKeys(true)) {
Object value = yml.get(key);
if (value instanceof MemorySection) {
continue;
}
set(key, value, root);
}
return true;
}
/**
* Set all values in the file (load first to avoid overwriting)
* @param file
*/
public static void save(File file, Class root) {
try {
if (!file.exists()) {
file.getParentFile().mkdirs();
file.createNewFile();
}
PrintWriter writer = new PrintWriter(file);
Class clazz = root;
Object instance = root.newInstance();
save(writer, clazz, instance, 0);
writer.close();
} catch (Throwable e) {
e.printStackTrace();
}
}
/**
* Indicates that a field should be instantiated / created
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface Create {}
/**
* Indicates that a field cannot be modified
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface Final {}
/**
* Creates a comment
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD,ElementType.TYPE})
public @interface Comment {
String[] value();
}
/**
* The names of any default blocks
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD,ElementType.TYPE})
public @interface BlockName {
String[] value();
}
/**
* Any field or class with is not part of the config
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD,ElementType.TYPE})
public @interface Ignore {}
@Ignore // This is not part of the config
public static class ConfigBlock<T> {
private HashMap<String, T> INSTANCES = new HashMap<>();
public T get(String key) {
return INSTANCES.get(key);
}
public void put(String key, T value) {
INSTANCES.put(key, value);
}
public Collection<T> getInstances() {
return INSTANCES.values();
}
public Collection<String> getSections() {
return INSTANCES.keySet();
}
private Map<String, T> getRaw() {
return INSTANCES;
}
}
/**
* Get the static fields in a section
* @param clazz
* @return
*/
public static Map<String, Object> getFields(Class clazz) {
HashMap<String, Object> map = new HashMap<>();
for (Field field : clazz.getFields()) {
if (java.lang.reflect.Modifier.isStatic(field.getModifiers())) {
try {
map.put(toNodeName(field.getName()), field.get(null));
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
return map;
}
private static String toYamlString(Object value, String spacing) {
if (value instanceof List) {
Collection<?> listValue = (Collection<?>) value;
if (listValue.isEmpty()) {
return "[]";
}
StringBuilder m = new StringBuilder();
for (Object obj : listValue) {
m.append(System.lineSeparator() + spacing + "- " + toYamlString(obj, spacing));
}
return m.toString();
}
if (value instanceof String) {
String stringValue = (String) value;
if (stringValue.isEmpty()) {
return "''";
}
return "\"" + stringValue + "\"";
}
return value != null ? value.toString() : "null";
}
private static void save(PrintWriter writer, Class clazz, Object instance, int indent) {
try {
String CTRF = System.lineSeparator();
String spacing = StringMan.repeat(" ", indent);
for (Field field : clazz.getFields()) {
if (field.getAnnotation(Ignore.class) != null) {
continue;
}
Comment comment = field.getAnnotation(Comment.class);
if (comment != null) {
for (String commentLine : comment.value()) {
writer.write(spacing + "# " + commentLine + CTRF);
}
}
Create create = field.getAnnotation(Create.class);
if (create != null) {
Object value = field.get(instance);
if (value == null && field.getType() != ConfigBlock.class) {
setAccessible(field);
Class<?>[] classes = clazz.getDeclaredClasses();
for (Class current : classes) {
if (StringMan.isEqual(current.getSimpleName(), field.getName())) {
field.set(instance, current.newInstance());
break;
}
}
}
continue;
} else {
writer.write(spacing + toNodeName(field.getName() + ": ") + toYamlString(field.get(instance), spacing) + CTRF);
}
}
for (Class<?> current : clazz.getClasses()) {
if (current.isInterface() || current.getAnnotation(Ignore.class) != null) {
continue;
}
if (indent == 0) {
writer.write(CTRF);
}
Comment comment = current.getAnnotation(Comment.class);
if (comment != null) {
for (String commentLine : comment.value()) {
writer.write(spacing + "# " + commentLine + CTRF);
}
}
writer.write(spacing + toNodeName(current.getSimpleName()) + ":" + CTRF);
BlockName blockNames = current.getAnnotation(BlockName.class);
if (blockNames != null) {
Field instanceField = clazz.getDeclaredField(toFieldName(current.getSimpleName()));
setAccessible(instanceField);
ConfigBlock value = (ConfigBlock) instanceField.get(instance);
if (value == null) {
value = new ConfigBlock();
instanceField.set(instance, value);
for (String blockName : blockNames.value()) {
value.put(blockName, current.newInstance());
}
}
// Save each instance
for (Map.Entry<String, Object> entry: ((Map<String, Object>) value.getRaw()).entrySet()) {
String key = entry.getKey();
writer.write(spacing + " " + toNodeName(key) + ":" + CTRF);
save(writer, current, entry.getValue(), indent + 4);
}
continue;
} else {
save(writer, current, current.newInstance(), indent + 2);
}
}
} catch (Throwable e) {
e.printStackTrace();
}
}
/**
* Get the field for a specific config node
* @param split the node (split by period)
* @return
*/
private static Field getField(String[] split, Class root) {
Object instance = getInstance(split, root);
if (instance == null) {
return null;
}
return getField(split, instance);
}
/**
* Get the field for a specific config node and instance<br>
* Note: As expiry can have multiple blocks there will be multiple instances
* @param split the node (split by period)
* @param instance the instance
* @return
*/
private static Field getField(String[] split, Object instance) {
try {
Field field = instance.getClass().getField(toFieldName(split[split.length - 1]));
setAccessible(field);
return field;
} catch (Throwable e) {
Fawe.debug("Invalid config field: " + StringMan.join(split, ".") + " for " + toNodeName(instance.getClass().getSimpleName()));
return null;
}
}
/**
* Get the instance for a specific config node
* @param split the node (split by period)
* @return The instance or null
*/
private static Object getInstance(String[] split, Class root) {
try {
Class<?> clazz = root == null ? MethodHandles.lookup().lookupClass() : root;
Object instance = clazz.newInstance();
while (split.length > 0) {
switch (split.length) {
case 1:
return instance;
default:
Class found = null;
Class<?>[] classes = clazz.getDeclaredClasses();
for (Class current : classes) {
if (StringMan.isEqual(current.getSimpleName(), toFieldName(split[0]))) {
found = current;
break;
}
}
try {
Field instanceField = clazz.getDeclaredField(toFieldName(split[0]));
setAccessible(instanceField);
if (instanceField.getType() != ConfigBlock.class) {
Object value = instanceField.get(instance);
if (value == null) {
value = found.newInstance();
instanceField.set(instance, value);
}
clazz = found;
instance = value;
split = Arrays.copyOfRange(split, 1, split.length);
continue;
}
ConfigBlock value = (ConfigBlock) instanceField.get(instance);
if (value == null) {
value = new ConfigBlock();
instanceField.set(instance, value);
}
instance = value.get(split[1]);
if (instance == null) {
instance = found.newInstance();
value.put(split[1], instance);
}
clazz = found;
split = Arrays.copyOfRange(split, 2, split.length);
continue;
} catch (NoSuchFieldException ignore) {}
if (found != null) {
split = Arrays.copyOfRange(split, 1, split.length);
clazz = found;
instance = clazz.newInstance();
continue;
}
return null;
}
}
} catch (Throwable e) {
e.printStackTrace();
}
return null;
}
/**
* Translate a node to a java field name
* @param node
* @return
*/
private static String toFieldName(String node) {
return node.toUpperCase().replaceAll("-","_");
}
/**
* Translate a field to a config node
* @param field
* @return
*/
private static String toNodeName(String field) {
return field.toLowerCase().replace("_","-");
}
/**
* Set some field to be accesible
* @param field
* @throws NoSuchFieldException
* @throws IllegalAccessException
*/
private static void setAccessible(Field field) throws NoSuchFieldException, IllegalAccessException {
field.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
}
}

View File

@ -65,7 +65,8 @@ public abstract class FawePlayer<T> {
Field fieldBasePlayer = actor.getClass().getDeclaredField("basePlayer"); Field fieldBasePlayer = actor.getClass().getDeclaredField("basePlayer");
fieldBasePlayer.setAccessible(true); fieldBasePlayer.setAccessible(true);
Player player = (Player) fieldBasePlayer.get(actor); Player player = (Player) fieldBasePlayer.get(actor);
return wrap(player); FawePlayer<Object> result = wrap(player);
return (FawePlayer<V>) (result == null ? wrap(player.getName()) : result);
} catch (Throwable e) { } catch (Throwable e) {
MainUtil.handleError(e); MainUtil.handleError(e);
return Fawe.imp().wrap(actor.getName()); return Fawe.imp().wrap(actor.getName());

View File

@ -7,8 +7,8 @@ import com.boydti.fawe.forge.v0.ForgeQueue_All;
import com.boydti.fawe.object.EditSessionWrapper; import com.boydti.fawe.object.EditSessionWrapper;
import com.boydti.fawe.object.FaweCommand; import com.boydti.fawe.object.FaweCommand;
import com.boydti.fawe.object.FawePlayer; import com.boydti.fawe.object.FawePlayer;
import com.boydti.fawe.regions.FaweMaskManager;
import com.boydti.fawe.object.FaweQueue; import com.boydti.fawe.object.FaweQueue;
import com.boydti.fawe.regions.FaweMaskManager;
import com.boydti.fawe.util.MainUtil; import com.boydti.fawe.util.MainUtil;
import com.boydti.fawe.util.TaskManager; import com.boydti.fawe.util.TaskManager;
import com.boydti.fawe.wrappers.WorldWrapper; import com.boydti.fawe.wrappers.WorldWrapper;
@ -19,7 +19,6 @@ import java.io.File;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List;
import java.util.UUID; import java.util.UUID;
import javax.management.InstanceAlreadyExistsException; import javax.management.InstanceAlreadyExistsException;
import net.minecraft.command.ServerCommandManager; import net.minecraft.command.ServerCommandManager;
@ -72,8 +71,7 @@ public class FaweForge implements IFawe {
EntityPlayerMP player = null; EntityPlayerMP player = null;
if (obj instanceof String) { if (obj instanceof String) {
MinecraftServer server = MinecraftServer.getServer(); MinecraftServer server = MinecraftServer.getServer();
List<EntityPlayerMP> list = server.getConfigurationManager().getPlayerList((String) obj); player = server.getConfigurationManager().func_152612_a((String) obj);
player = list.size() == 1 ? list.get(0) : null;
} else if (obj instanceof EntityPlayerMP) { } else if (obj instanceof EntityPlayerMP) {
player = (EntityPlayerMP) obj; player = (EntityPlayerMP) obj;
} }

View File

@ -441,9 +441,9 @@ public class ForgeQueue_All extends NMSMappedFaweQueue<World, Chunk, ExtendedBlo
solid++; solid++;
currentIdArray[k] = n; currentIdArray[k] = n;
if (data) { if (data) {
int x = FaweCache.CACHE_X[j][k]; int x = FaweCache.CACHE_X[0][k];
int y = FaweCache.CACHE_Y[j][k]; int y = FaweCache.CACHE_Y[0][k];
int z = FaweCache.CACHE_Z[j][k]; int z = FaweCache.CACHE_Z[0][k];
int newData = newDataArray == null ? 0 : newDataArray.get(x, y, z); int newData = newDataArray == null ? 0 : newDataArray.get(x, y, z);
int currentData = currentDataArray == null ? 0 : currentDataArray.get(x, y, z); int currentData = currentDataArray == null ? 0 : currentDataArray.get(x, y, z);
if (newData != currentData) { if (newData != currentData) {
@ -608,22 +608,50 @@ public class ForgeQueue_All extends NMSMappedFaweQueue<World, Chunk, ExtendedBlo
if (!nmsChunk.isChunkLoaded) { if (!nmsChunk.isChunkLoaded) {
return false; return false;
} }
World nmsWorld = nmsChunk.worldObj;
boolean flag = !nmsWorld.provider.hasNoSky;
ExtendedBlockStorage[] sections = nmsChunk.getBlockStorageArray(); ExtendedBlockStorage[] sections = nmsChunk.getBlockStorageArray();
if (mode == RelightMode.ALL) { if (mode == RelightMode.ALL) {
for (int i = 0; i < sections.length; i++) { for (int i = 0; i < sections.length; i++) {
ExtendedBlockStorage section = sections[i]; ExtendedBlockStorage section = sections[i];
if (section != null) { if (section != null) {
section.setSkylightArray(new NibbleArray(4096, 4));
section.setBlocklightArray(new NibbleArray(4096, 4)); section.setBlocklightArray(new NibbleArray(4096, 4));
if (flag) {
section.setSkylightArray(new NibbleArray(4096, 4));
} }
} }
} }
}
if (flag) {
if (mode == RelightMode.ALL) {
nmsChunk.generateSkylightMap(); nmsChunk.generateSkylightMap();
} else {
int i = nmsChunk.getTopFilledSegment();
for (int x = 0; x < 16; ++x) {
for (int z = 0; z < 16; ++z) {
int l = 15;
int y = i + 16 - 1;
do {
int opacity = nmsChunk.func_150808_b(x, y, z);
if (opacity == 0 && l != 15) {
opacity = 1;
}
l -= opacity;
if (l > 0) {
ExtendedBlockStorage section = sections[y >> 4];
if (section != null) {
section.setExtSkylightValue(x, y & 15, z, l);
}
}
--y;
} while (y > 0 && l > 0);
}
}
}
}
if (bc.getTotalRelight() == 0 && mode == RelightMode.MINIMAL) { if (bc.getTotalRelight() == 0 && mode == RelightMode.MINIMAL) {
return true; return true;
} }
net.minecraft.world.World nmsWorld = nmsChunk.worldObj;
int X = fc.getX() << 4; int X = fc.getX() << 4;
int Z = fc.getZ() << 4; int Z = fc.getZ() << 4;

View File

@ -199,22 +199,50 @@ public class ForgeQueue_All extends NMSMappedFaweQueue<World, Chunk, ExtendedBlo
if (!nmsChunk.isLoaded()) { if (!nmsChunk.isLoaded()) {
return false; return false;
} }
World nmsWorld = nmsChunk.getWorld();
boolean flag = !nmsWorld.provider.getHasNoSky();
ExtendedBlockStorage[] sections = nmsChunk.getBlockStorageArray(); ExtendedBlockStorage[] sections = nmsChunk.getBlockStorageArray();
if (mode == RelightMode.ALL) { if (mode == RelightMode.ALL) {
for (int i = 0; i < sections.length; i++) { for (int i = 0; i < sections.length; i++) {
ExtendedBlockStorage section = sections[i]; ExtendedBlockStorage section = sections[i];
if (section != null) { if (section != null) {
section.setSkylightArray(new NibbleArray());
section.setBlocklightArray(new NibbleArray()); section.setBlocklightArray(new NibbleArray());
if (flag) {
section.setSkylightArray(new NibbleArray());
} }
} }
} }
}
if (flag) {
if (mode == RelightMode.ALL) {
nmsChunk.generateSkylightMap(); nmsChunk.generateSkylightMap();
} else {
int i = nmsChunk.getTopFilledSegment();
for (int x = 0; x < 16; ++x) {
for (int z = 0; z < 16; ++z) {
int l = 15;
int y = i + 16 - 1;
do {
int opacity = nmsChunk.getBlockLightOpacity(new BlockPos(x, y, z));
if (opacity == 0 && l != 15) {
opacity = 1;
}
l -= opacity;
if (l > 0) {
ExtendedBlockStorage section = sections[y >> 4];
if (section != null) {
section.setExtSkylightValue(x, y & 15, z, l);
}
}
--y;
} while (y > 0 && l > 0);
}
}
}
}
if (bc.getTotalRelight() == 0 && mode == RelightMode.MINIMAL) { if (bc.getTotalRelight() == 0 && mode == RelightMode.MINIMAL) {
return true; return true;
} }
net.minecraft.world.World nmsWorld = nmsChunk.getWorld();
int X = fc.getX() << 4; int X = fc.getX() << 4;
int Z = fc.getZ() << 4; int Z = fc.getZ() << 4;