use pattern variables

This commit is contained in:
dfsek
2021-11-27 08:34:03 -07:00
parent 2d316fa042
commit 2307897b31
16 changed files with 33 additions and 58 deletions

View File

@@ -34,8 +34,7 @@ public class Point {
@Override
public boolean equals(Object obj) {
if(!(obj instanceof Point)) return false;
Point that = (Point) obj;
if(!(obj instanceof Point that)) return false;
return this.x == that.x && this.z == that.z;
}
}

View File

@@ -40,12 +40,11 @@ public class BufferedLootApplication implements BufferedItem {
public void paste(Vector3 origin, World world) {
try {
BlockEntity data = world.getBlockState(origin);
if(!(data instanceof Container)) {
if(!(data instanceof Container container)) {
LOGGER.error("Failed to place loot at {}; block {} is not a container", origin, data);
return;
}
Container container = (Container) data;
LootPopulateEvent event = new LootPopulateEvent(container, table, world.getConfig().getPack(), structure);
platform.getEventManager().callEvent(event);
if(event.isCancelled()) return;

View File

@@ -33,8 +33,7 @@ public class Block implements Item<Block.ReturnInfo<?>> {
Map<String, Variable<?>> scope = new HashMap<>(variableMap);
for(Item<?> item : items) {
Object result = item.apply(implementationArguments, scope);
if(result instanceof ReturnInfo) {
ReturnInfo<?> level = (ReturnInfo<?>) result;
if(result instanceof ReturnInfo<?> level) {
if(!level.getLevel().equals(ReturnLevel.NONE)) return level;
}
}

View File

@@ -79,10 +79,8 @@ public final class Either<L, R> {
@Override
public boolean equals(Object obj) {
if(!(obj instanceof Either)) return false;
Either<?, ?> that = (Either<?, ?>) obj;
if(!(obj instanceof Either<?, ?> that)) return false;
return (this.leftPresent && that.leftPresent && Objects.equals(this.left, that.left))
|| (!this.leftPresent && !that.leftPresent && Objects.equals(this.right, that.right));
}

View File

@@ -52,8 +52,7 @@ public final class ReflectionUtil {
public static Class<?> getRawType(Type type) {
if(type instanceof Class<?>) {
return (Class<?>) type;
} else if(type instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) type;
} else if(type instanceof ParameterizedType parameterizedType) {
Type rawType = parameterizedType.getRawType();
return (Class<?>) rawType;
} else if(type instanceof GenericArrayType) {

View File

@@ -38,39 +38,31 @@ public class TypeKey<T> {
return true;
} else if(a instanceof Class) {
return a.equals(b);
} else if(a instanceof ParameterizedType) {
if(!(b instanceof ParameterizedType)) {
} else if(a instanceof ParameterizedType pa) {
if(!(b instanceof ParameterizedType pb)) {
return false;
}
ParameterizedType pa = (ParameterizedType) a;
ParameterizedType pb = (ParameterizedType) b;
return Objects.equals(pa.getOwnerType(), pb.getOwnerType())
&& pa.getRawType().equals(pb.getRawType())
&& Arrays.equals(pa.getActualTypeArguments(), pb.getActualTypeArguments());
} else if(a instanceof GenericArrayType) {
if(!(b instanceof GenericArrayType)) {
} else if(a instanceof GenericArrayType ga) {
if(!(b instanceof GenericArrayType gb)) {
return false;
}
GenericArrayType ga = (GenericArrayType) a;
GenericArrayType gb = (GenericArrayType) b;
return equals(ga.getGenericComponentType(), gb.getGenericComponentType());
} else if(a instanceof WildcardType) {
if(!(b instanceof WildcardType)) {
} else if(a instanceof WildcardType wa) {
if(!(b instanceof WildcardType wb)) {
return false;
}
WildcardType wa = (WildcardType) a;
WildcardType wb = (WildcardType) b;
return Arrays.equals(wa.getUpperBounds(), wb.getUpperBounds())
&& Arrays.equals(wa.getLowerBounds(), wb.getLowerBounds());
} else if(a instanceof TypeVariable) {
if(!(b instanceof TypeVariable)) {
} else if(a instanceof TypeVariable<?> va) {
if(!(b instanceof TypeVariable<?> vb)) {
return false;
}
TypeVariable<?> va = (TypeVariable<?>) a;
TypeVariable<?> vb = (TypeVariable<?>) b;
return va.getGenericDeclaration() == vb.getGenericDeclaration()
&& va.getName().equals(vb.getName());
} else {

View File

@@ -189,8 +189,7 @@ public class Vector2 implements Cloneable {
}
public boolean equals(Object obj) {
if(!(obj instanceof Vector2)) return false;
Vector2 other = (Vector2) obj;
if(!(obj instanceof Vector2 other)) return false;
return MathUtil.equals(this.x, other.x) && MathUtil.equals(this.z, other.z);
}

View File

@@ -328,8 +328,7 @@ public class Vector3 implements Cloneable {
*/
@Override
public boolean equals(Object obj) {
if(!(obj instanceof Vector3)) return false;
Vector3 other = (Vector3) obj;
if(!(obj instanceof Vector3 other)) return false;
return MathUtil.equals(x, other.getX()) && MathUtil.equals(y, other.getY()) && MathUtil.equals(z, other.getZ());
}

View File

@@ -34,8 +34,6 @@ public class AddonsCommand implements CommandTemplate {
@Override
public void execute(CommandSender sender) {
sender.sendMessage("Installed Addons:");
platform.getAddons().forEach(addon -> {
sender.sendMessage(" - " + addon.getID());
});
platform.getAddons().forEach(addon -> sender.sendMessage(" - " + addon.getID()));
}
}

View File

@@ -33,8 +33,7 @@ public class LinkedHashMapLoader implements TypeLoader<LinkedHashMap<Object, Obj
public LinkedHashMap<Object, Object> load(AnnotatedType t, Object c, ConfigLoader loader) throws LoadException {
Map<String, Object> config = (Map<String, Object>) c;
LinkedHashMap<Object, Object> map = new LinkedHashMap<>();
if(t instanceof AnnotatedParameterizedType) {
AnnotatedParameterizedType pType = (AnnotatedParameterizedType) t;
if(t instanceof AnnotatedParameterizedType pType) {
AnnotatedType key = pType.getAnnotatedActualTypeArguments()[0];
AnnotatedType value = pType.getAnnotatedActualTypeArguments()[1];
for(Map.Entry<String, Object> entry : config.entrySet()) {

View File

@@ -35,8 +35,7 @@ public class ProbabilityCollectionLoader implements TypeLoader<ProbabilityCollec
public ProbabilityCollection<Object> load(AnnotatedType type, Object o, ConfigLoader configLoader) throws LoadException {
ProbabilityCollection<Object> collection = new ProbabilityCollection<>();
if(type instanceof AnnotatedParameterizedType) {
AnnotatedParameterizedType pType = (AnnotatedParameterizedType) type;
if(type instanceof AnnotatedParameterizedType pType) {
AnnotatedType generic = pType.getAnnotatedActualTypeArguments()[0];
if(o instanceof Map) {
Map<Object, Integer> map = (Map<Object, Integer>) o;

View File

@@ -41,11 +41,9 @@ public class MetaListLikePreprocessor extends MetaPreprocessor<Meta> {
@SuppressWarnings("unchecked")
@Override
public @NotNull <T> Result<T> process(AnnotatedType t, T c, ConfigLoader loader, Meta annotation) {
if(t.getType() instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) t.getType();
if(parameterizedType.getRawType() instanceof Class) { // Should always be true but we check anyways
Class<?> baseClass = (Class<?>) parameterizedType.getRawType();
if(t.getType() instanceof ParameterizedType parameterizedType) {
if(parameterizedType.getRawType() instanceof Class<?> baseClass) { // Should always be true but we check anyways
if((List.class.isAssignableFrom(baseClass) || Set.class.isAssignableFrom(baseClass)) &&
c instanceof List) { // List or set metaconfig
List<Object> list = (List<Object>) c;

View File

@@ -44,11 +44,9 @@ public class MetaMapPreprocessor extends MetaPreprocessor<Meta> {
@SuppressWarnings("unchecked")
@Override
public @NotNull <T> Result<T> process(AnnotatedType t, T c, ConfigLoader loader, Meta annotation) {
if(t.getType() instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) t.getType();
if(parameterizedType.getRawType() instanceof Class) { // Should always be true but we check anyways
Class<?> baseClass = (Class<?>) parameterizedType.getRawType();
if(t.getType() instanceof ParameterizedType parameterizedType) {
if(parameterizedType.getRawType() instanceof Class<?> baseClass) { // Should always be true but we check anyways
if(Map.class.isAssignableFrom(baseClass) && c instanceof Map) { // Map metaconfig
Map<Object, Object> map = (Map<Object, Object>) c;

View File

@@ -62,8 +62,7 @@ public class ChunkCoordinate implements Serializable {
@Override
public boolean equals(Object obj) {
if(!(obj instanceof ChunkCoordinate)) return false;
ChunkCoordinate other = (ChunkCoordinate) obj;
if(!(obj instanceof ChunkCoordinate other)) return false;
return other.getX() == x && other.getZ() == z;
}
}

View File

@@ -127,8 +127,7 @@ public class BukkitWorld implements World {
@Override
public boolean equals(Object obj) {
if(!(obj instanceof BukkitWorld)) return false;
BukkitWorld other = (BukkitWorld) obj;
if(!(obj instanceof BukkitWorld other)) return false;
return other.getHandle().equals(delegate);
}

View File

@@ -54,7 +54,8 @@ public abstract class BlockEntityMixin {
}
public boolean terra$update(boolean applyPhysics) {
if(hasWorld()) world.getChunk(pos).setBlockEntity((net.minecraft.block.entity.BlockEntity) (Object) this);
if(hasWorld()) //noinspection ConstantConditions
world.getChunk(pos).setBlockEntity((net.minecraft.block.entity.BlockEntity) (Object) this);
return true;
}