2021-07-01 20:16:25 +00:00
|
|
|
package com.fastasyncworldedit.core.util;
|
2018-08-12 14:03:07 +00:00
|
|
|
|
2021-07-23 15:48:51 +00:00
|
|
|
import com.fastasyncworldedit.core.function.mask.ResettableMask;
|
2018-08-12 14:03:07 +00:00
|
|
|
import com.sk89q.worldedit.extent.Extent;
|
2020-03-03 03:53:50 +00:00
|
|
|
import com.sk89q.worldedit.function.mask.AbstractExtentMask;
|
2018-08-12 14:03:07 +00:00
|
|
|
import com.sk89q.worldedit.function.mask.Mask;
|
2020-06-13 18:41:36 +00:00
|
|
|
import com.sk89q.worldedit.function.mask.MaskIntersection;
|
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
import java.lang.reflect.Field;
|
|
|
|
import java.util.Collection;
|
|
|
|
|
|
|
|
public class MaskTraverser {
|
|
|
|
private final Mask mask;
|
|
|
|
|
|
|
|
public MaskTraverser(Mask start) {
|
|
|
|
this.mask = start;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void reset(Extent newExtent) {
|
|
|
|
reset(mask, newExtent);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void reset(Mask mask, Extent newExtent) {
|
|
|
|
if (mask == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (mask instanceof ResettableMask) {
|
|
|
|
((ResettableMask) mask).reset();
|
|
|
|
}
|
|
|
|
Class<?> current = mask.getClass();
|
|
|
|
while (current.getSuperclass() != null) {
|
2020-03-03 03:53:50 +00:00
|
|
|
if (mask instanceof AbstractExtentMask) {
|
|
|
|
AbstractExtentMask mask1 = (AbstractExtentMask) mask;
|
|
|
|
mask1.setExtent(newExtent);
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
Field field = current.getDeclaredField("extent");
|
|
|
|
field.setAccessible(true);
|
|
|
|
field.set(mask, newExtent);
|
|
|
|
} catch (NoSuchFieldException | IllegalAccessException ignored) {
|
|
|
|
}
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
2020-06-13 18:41:36 +00:00
|
|
|
if (mask instanceof MaskIntersection) {
|
|
|
|
MaskIntersection mask1 = (MaskIntersection) mask;
|
|
|
|
try {
|
|
|
|
Field field = mask1.getClass().getDeclaredField("masks");
|
|
|
|
field.setAccessible(true);
|
|
|
|
Collection<Mask> masks = (Collection<Mask>) field.get(mask);
|
|
|
|
for (Mask next : masks) {
|
|
|
|
reset(next, newExtent);
|
|
|
|
}
|
|
|
|
} catch (NoSuchFieldException | IllegalAccessException ignored) {
|
|
|
|
}
|
|
|
|
}
|
2018-08-12 14:03:07 +00:00
|
|
|
try {
|
|
|
|
Field field = current.getDeclaredField("mask");
|
|
|
|
field.setAccessible(true);
|
|
|
|
Mask next = (Mask) field.get(mask);
|
|
|
|
reset(next, newExtent);
|
2019-07-23 02:22:32 +00:00
|
|
|
} catch (NoSuchFieldException | IllegalAccessException ignored) {
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
|
|
|
try {
|
|
|
|
Field field = current.getDeclaredField("masks");
|
|
|
|
field.setAccessible(true);
|
|
|
|
Collection<Mask> masks = (Collection<Mask>) field.get(mask);
|
|
|
|
for (Mask next : masks) {
|
|
|
|
reset(next, newExtent);
|
|
|
|
}
|
2019-07-23 02:22:32 +00:00
|
|
|
} catch (NoSuchFieldException | IllegalAccessException ignored) {
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
|
|
|
current = current.getSuperclass();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|