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
@@ -34,8 +34,7 @@ public class Point {
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if(!(obj instanceof Point)) return false; if(!(obj instanceof Point that)) return false;
Point that = (Point) obj;
return this.x == that.x && this.z == that.z; return this.x == that.x && this.z == that.z;
} }
} }
@@ -40,11 +40,10 @@ public class BufferedLootApplication implements BufferedItem {
public void paste(Vector3 origin, World world) { public void paste(Vector3 origin, World world) {
try { try {
BlockEntity data = world.getBlockState(origin); 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); LOGGER.error("Failed to place loot at {}; block {} is not a container", origin, data);
return; return;
} }
Container container = (Container) data;
LootPopulateEvent event = new LootPopulateEvent(container, table, world.getConfig().getPack(), structure); LootPopulateEvent event = new LootPopulateEvent(container, table, world.getConfig().getPack(), structure);
platform.getEventManager().callEvent(event); platform.getEventManager().callEvent(event);
@@ -33,8 +33,7 @@ public class Block implements Item<Block.ReturnInfo<?>> {
Map<String, Variable<?>> scope = new HashMap<>(variableMap); Map<String, Variable<?>> scope = new HashMap<>(variableMap);
for(Item<?> item : items) { for(Item<?> item : items) {
Object result = item.apply(implementationArguments, scope); Object result = item.apply(implementationArguments, scope);
if(result instanceof ReturnInfo) { if(result instanceof ReturnInfo<?> level) {
ReturnInfo<?> level = (ReturnInfo<?>) result;
if(!level.getLevel().equals(ReturnLevel.NONE)) return level; if(!level.getLevel().equals(ReturnLevel.NONE)) return level;
} }
} }
@@ -79,9 +79,7 @@ public final class Either<L, R> {
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if(!(obj instanceof Either)) return false; if(!(obj instanceof Either<?, ?> that)) return false;
Either<?, ?> that = (Either<?, ?>) obj;
return (this.leftPresent && that.leftPresent && Objects.equals(this.left, that.left)) return (this.leftPresent && that.leftPresent && Objects.equals(this.left, that.left))
|| (!this.leftPresent && !that.leftPresent && Objects.equals(this.right, that.right)); || (!this.leftPresent && !that.leftPresent && Objects.equals(this.right, that.right));
@@ -52,8 +52,7 @@ public final class ReflectionUtil {
public static Class<?> getRawType(Type type) { public static Class<?> getRawType(Type type) {
if(type instanceof Class<?>) { if(type instanceof Class<?>) {
return (Class<?>) type; return (Class<?>) type;
} else if(type instanceof ParameterizedType) { } else if(type instanceof ParameterizedType parameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) type;
Type rawType = parameterizedType.getRawType(); Type rawType = parameterizedType.getRawType();
return (Class<?>) rawType; return (Class<?>) rawType;
} else if(type instanceof GenericArrayType) { } else if(type instanceof GenericArrayType) {
@@ -38,39 +38,31 @@ public class TypeKey<T> {
return true; return true;
} else if(a instanceof Class) { } else if(a instanceof Class) {
return a.equals(b); return a.equals(b);
} else if(a instanceof ParameterizedType) { } else if(a instanceof ParameterizedType pa) {
if(!(b instanceof ParameterizedType)) { if(!(b instanceof ParameterizedType pb)) {
return false; return false;
} }
ParameterizedType pa = (ParameterizedType) a;
ParameterizedType pb = (ParameterizedType) b;
return Objects.equals(pa.getOwnerType(), pb.getOwnerType()) return Objects.equals(pa.getOwnerType(), pb.getOwnerType())
&& pa.getRawType().equals(pb.getRawType()) && pa.getRawType().equals(pb.getRawType())
&& Arrays.equals(pa.getActualTypeArguments(), pb.getActualTypeArguments()); && Arrays.equals(pa.getActualTypeArguments(), pb.getActualTypeArguments());
} else if(a instanceof GenericArrayType) { } else if(a instanceof GenericArrayType ga) {
if(!(b instanceof GenericArrayType)) { if(!(b instanceof GenericArrayType gb)) {
return false; return false;
} }
GenericArrayType ga = (GenericArrayType) a;
GenericArrayType gb = (GenericArrayType) b;
return equals(ga.getGenericComponentType(), gb.getGenericComponentType()); return equals(ga.getGenericComponentType(), gb.getGenericComponentType());
} else if(a instanceof WildcardType) { } else if(a instanceof WildcardType wa) {
if(!(b instanceof WildcardType)) { if(!(b instanceof WildcardType wb)) {
return false; return false;
} }
WildcardType wa = (WildcardType) a;
WildcardType wb = (WildcardType) b;
return Arrays.equals(wa.getUpperBounds(), wb.getUpperBounds()) return Arrays.equals(wa.getUpperBounds(), wb.getUpperBounds())
&& Arrays.equals(wa.getLowerBounds(), wb.getLowerBounds()); && Arrays.equals(wa.getLowerBounds(), wb.getLowerBounds());
} else if(a instanceof TypeVariable) { } else if(a instanceof TypeVariable<?> va) {
if(!(b instanceof TypeVariable)) { if(!(b instanceof TypeVariable<?> vb)) {
return false; return false;
} }
TypeVariable<?> va = (TypeVariable<?>) a;
TypeVariable<?> vb = (TypeVariable<?>) b;
return va.getGenericDeclaration() == vb.getGenericDeclaration() return va.getGenericDeclaration() == vb.getGenericDeclaration()
&& va.getName().equals(vb.getName()); && va.getName().equals(vb.getName());
} else { } else {
@@ -189,8 +189,7 @@ public class Vector2 implements Cloneable {
} }
public boolean equals(Object obj) { public boolean equals(Object obj) {
if(!(obj instanceof Vector2)) return false; if(!(obj instanceof Vector2 other)) return false;
Vector2 other = (Vector2) obj;
return MathUtil.equals(this.x, other.x) && MathUtil.equals(this.z, other.z); return MathUtil.equals(this.x, other.x) && MathUtil.equals(this.z, other.z);
} }
@@ -328,8 +328,7 @@ public class Vector3 implements Cloneable {
*/ */
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if(!(obj instanceof Vector3)) return false; if(!(obj instanceof Vector3 other)) return false;
Vector3 other = (Vector3) obj;
return MathUtil.equals(x, other.getX()) && MathUtil.equals(y, other.getY()) && MathUtil.equals(z, other.getZ()); return MathUtil.equals(x, other.getX()) && MathUtil.equals(y, other.getY()) && MathUtil.equals(z, other.getZ());
} }
@@ -34,8 +34,6 @@ public class AddonsCommand implements CommandTemplate {
@Override @Override
public void execute(CommandSender sender) { public void execute(CommandSender sender) {
sender.sendMessage("Installed Addons:"); sender.sendMessage("Installed Addons:");
platform.getAddons().forEach(addon -> { platform.getAddons().forEach(addon -> sender.sendMessage(" - " + addon.getID()));
sender.sendMessage(" - " + addon.getID());
});
} }
} }
@@ -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 { public LinkedHashMap<Object, Object> load(AnnotatedType t, Object c, ConfigLoader loader) throws LoadException {
Map<String, Object> config = (Map<String, Object>) c; Map<String, Object> config = (Map<String, Object>) c;
LinkedHashMap<Object, Object> map = new LinkedHashMap<>(); LinkedHashMap<Object, Object> map = new LinkedHashMap<>();
if(t instanceof AnnotatedParameterizedType) { if(t instanceof AnnotatedParameterizedType pType) {
AnnotatedParameterizedType pType = (AnnotatedParameterizedType) t;
AnnotatedType key = pType.getAnnotatedActualTypeArguments()[0]; AnnotatedType key = pType.getAnnotatedActualTypeArguments()[0];
AnnotatedType value = pType.getAnnotatedActualTypeArguments()[1]; AnnotatedType value = pType.getAnnotatedActualTypeArguments()[1];
for(Map.Entry<String, Object> entry : config.entrySet()) { for(Map.Entry<String, Object> entry : config.entrySet()) {
@@ -35,8 +35,7 @@ public class ProbabilityCollectionLoader implements TypeLoader<ProbabilityCollec
public ProbabilityCollection<Object> load(AnnotatedType type, Object o, ConfigLoader configLoader) throws LoadException { public ProbabilityCollection<Object> load(AnnotatedType type, Object o, ConfigLoader configLoader) throws LoadException {
ProbabilityCollection<Object> collection = new ProbabilityCollection<>(); ProbabilityCollection<Object> collection = new ProbabilityCollection<>();
if(type instanceof AnnotatedParameterizedType) { if(type instanceof AnnotatedParameterizedType pType) {
AnnotatedParameterizedType pType = (AnnotatedParameterizedType) type;
AnnotatedType generic = pType.getAnnotatedActualTypeArguments()[0]; AnnotatedType generic = pType.getAnnotatedActualTypeArguments()[0];
if(o instanceof Map) { if(o instanceof Map) {
Map<Object, Integer> map = (Map<Object, Integer>) o; Map<Object, Integer> map = (Map<Object, Integer>) o;
@@ -41,10 +41,8 @@ public class MetaListLikePreprocessor extends MetaPreprocessor<Meta> {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
public @NotNull <T> Result<T> process(AnnotatedType t, T c, ConfigLoader loader, Meta annotation) { public @NotNull <T> Result<T> process(AnnotatedType t, T c, ConfigLoader loader, Meta annotation) {
if(t.getType() instanceof ParameterizedType) { if(t.getType() instanceof ParameterizedType parameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) t.getType(); if(parameterizedType.getRawType() instanceof Class<?> baseClass) { // Should always be true but we check anyways
if(parameterizedType.getRawType() instanceof Class) { // Should always be true but we check anyways
Class<?> baseClass = (Class<?>) parameterizedType.getRawType();
if((List.class.isAssignableFrom(baseClass) || Set.class.isAssignableFrom(baseClass)) && if((List.class.isAssignableFrom(baseClass) || Set.class.isAssignableFrom(baseClass)) &&
c instanceof List) { // List or set metaconfig c instanceof List) { // List or set metaconfig
@@ -44,10 +44,8 @@ public class MetaMapPreprocessor extends MetaPreprocessor<Meta> {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
public @NotNull <T> Result<T> process(AnnotatedType t, T c, ConfigLoader loader, Meta annotation) { public @NotNull <T> Result<T> process(AnnotatedType t, T c, ConfigLoader loader, Meta annotation) {
if(t.getType() instanceof ParameterizedType) { if(t.getType() instanceof ParameterizedType parameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) t.getType(); if(parameterizedType.getRawType() instanceof Class<?> baseClass) { // Should always be true but we check anyways
if(parameterizedType.getRawType() instanceof Class) { // Should always be true but we check anyways
Class<?> baseClass = (Class<?>) parameterizedType.getRawType();
if(Map.class.isAssignableFrom(baseClass) && c instanceof Map) { // Map metaconfig if(Map.class.isAssignableFrom(baseClass) && c instanceof Map) { // Map metaconfig
Map<Object, Object> map = (Map<Object, Object>) c; Map<Object, Object> map = (Map<Object, Object>) c;
@@ -62,8 +62,7 @@ public class ChunkCoordinate implements Serializable {
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if(!(obj instanceof ChunkCoordinate)) return false; if(!(obj instanceof ChunkCoordinate other)) return false;
ChunkCoordinate other = (ChunkCoordinate) obj;
return other.getX() == x && other.getZ() == z; return other.getX() == x && other.getZ() == z;
} }
} }
@@ -127,8 +127,7 @@ public class BukkitWorld implements World {
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if(!(obj instanceof BukkitWorld)) return false; if(!(obj instanceof BukkitWorld other)) return false;
BukkitWorld other = (BukkitWorld) obj;
return other.getHandle().equals(delegate); return other.getHandle().equals(delegate);
} }
@@ -54,7 +54,8 @@ public abstract class BlockEntityMixin {
} }
public boolean terra$update(boolean applyPhysics) { 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; return true;
} }