Refactor code to use ListValue.wrap(Stream)
Simplifies code (improving readability) and unifies `Stream<Value>` to `ListValue` handling
This commit is contained in:
1 parent
dd1dc3756a
commit
17249c3c77
15 files changed
+66
-85
No files matched your search
@@ -456,16 +456,16 @@ public class EntityValue extends Value
|
||||
put("type", (e, a) -> new StringValue(nameFromRegistryId(e.getLevel().registryAccess().registryOrThrow(Registries.ENTITY_TYPE).getKey(e.getType()))));
|
||||
put("is_riding", (e, a) -> BooleanValue.of(e.isPassenger()));
|
||||
put("is_ridden", (e, a) -> BooleanValue.of(e.isVehicle()));
|
||||
put("passengers", (e, a) -> ListValue.wrap(e.getPassengers().stream().map(EntityValue::new).collect(Collectors.toList())));
|
||||
put("passengers", (e, a) -> ListValue.wrap(e.getPassengers().stream().map(EntityValue::new)));
|
||||
put("mount", (e, a) -> (e.getVehicle()!=null)?new EntityValue(e.getVehicle()):Value.NULL);
|
||||
put("unmountable", (e, a) -> BooleanValue.of(((EntityInterface)e).isPermanentVehicle()));
|
||||
// deprecated
|
||||
put("tags", (e, a) -> ListValue.wrap(e.getTags().stream().map(StringValue::new).collect(Collectors.toList())));
|
||||
put("tags", (e, a) -> ListValue.wrap(e.getTags().stream().map(StringValue::new)));
|
||||
|
||||
put("scoreboard_tags", (e, a) -> ListValue.wrap(e.getTags().stream().map(StringValue::new).collect(Collectors.toList())));
|
||||
put("scoreboard_tags", (e, a) -> ListValue.wrap(e.getTags().stream().map(StringValue::new)));
|
||||
put("entity_tags", (e, a) -> {
|
||||
EntityType<?> type = e.getType();
|
||||
return ListValue.wrap(e.getServer().registryAccess().registryOrThrow(Registries.ENTITY_TYPE).getTags().filter(entry -> entry.getSecond().stream().anyMatch(h -> h.value()==type)).map(entry -> ValueConversions.of(entry.getFirst())).collect(Collectors.toList()));
|
||||
return ListValue.wrap(e.getServer().registryAccess().registryOrThrow(Registries.ENTITY_TYPE).getTags().filter(entry -> entry.getSecond().stream().anyMatch(h -> h.value()==type)).map(entry -> ValueConversions.of(entry.getFirst())));
|
||||
});
|
||||
// deprecated
|
||||
put("has_tag", (e, a) -> BooleanValue.of(e.getTags().contains(a.getString())));
|
||||
@@ -558,7 +558,7 @@ public class EntityValue extends Value
|
||||
put("home", (e, a) -> {
|
||||
if (e instanceof Mob)
|
||||
{
|
||||
return (((Mob) e).getRestrictRadius () > 0)?new BlockValue(null, (ServerLevel) e.getCommandSenderWorld(), ((PathfinderMob) e).getRestrictCenter()):Value.FALSE;
|
||||
return (((Mob) e).getRestrictRadius () > 0)?new BlockValue(null, e.getCommandSenderWorld(), ((PathfinderMob) e).getRestrictCenter()):Value.FALSE;
|
||||
}
|
||||
return Value.NULL;
|
||||
});
|
||||
@@ -840,7 +840,7 @@ public class EntityValue extends Value
|
||||
ServerPlayerInteractionManagerInterface manager = (ServerPlayerInteractionManagerInterface) (((ServerPlayer) e).gameMode);
|
||||
BlockPos pos = manager.getCurrentBreakingBlock();
|
||||
if (pos == null) return Value.NULL;
|
||||
return new BlockValue(null, (ServerLevel) e.level, pos);
|
||||
return new BlockValue(null, e.level, pos);
|
||||
}
|
||||
return Value.NULL;
|
||||
});
|
||||
@@ -926,7 +926,7 @@ public class EntityValue extends Value
|
||||
switch (hitres.getType())
|
||||
{
|
||||
case MISS: return Value.NULL;
|
||||
case BLOCK: return new BlockValue(null, (ServerLevel) e.getCommandSenderWorld(), ((BlockHitResult)hitres).getBlockPos() );
|
||||
case BLOCK: return new BlockValue(null, e.getCommandSenderWorld(), ((BlockHitResult)hitres).getBlockPos() );
|
||||
case ENTITY: return new EntityValue(((EntityHitResult)hitres).getEntity());
|
||||
}
|
||||
return Value.NULL;
|
||||
|
||||
@@ -17,7 +17,6 @@ import java.util.Map;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
import net.minecraft.commands.arguments.NbtPathArgument;
|
||||
import net.minecraft.commands.arguments.item.ItemInput;
|
||||
import net.minecraft.commands.arguments.item.ItemParser;
|
||||
@@ -609,7 +608,7 @@ public class NBTSerializableValue extends Value implements ContainerValueInterfa
|
||||
return Value.NULL;
|
||||
if (tags.size()==1 && !valString.endsWith("[]"))
|
||||
return NBTSerializableValue.decodeTag(tags.get(0));
|
||||
return ListValue.wrap(tags.stream().map(NBTSerializableValue::decodeTag).collect(Collectors.toList()));
|
||||
return ListValue.wrap(tags.stream().map(NBTSerializableValue::decodeTag));
|
||||
}
|
||||
catch (CommandSyntaxException ignored) { }
|
||||
return Value.NULL;
|
||||
@@ -696,8 +695,7 @@ public class NBTSerializableValue extends Value implements ContainerValueInterfa
|
||||
|
||||
public static class IncompatibleTypeException extends RuntimeException
|
||||
{
|
||||
private IncompatibleTypeException() {}
|
||||
public Value val;
|
||||
public final Value val;
|
||||
public IncompatibleTypeException(Value val)
|
||||
{
|
||||
this.val = val;
|
||||
|
||||
@@ -10,7 +10,6 @@ import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.regex.PatternSyntaxException;
|
||||
import java.util.stream.Collectors;
|
||||
import net.minecraft.nbt.Tag;
|
||||
|
||||
public abstract class Value implements Comparable<Value>, Cloneable
|
||||
@@ -179,7 +178,7 @@ public abstract class Value implements Comparable<Value>, Cloneable
|
||||
}
|
||||
try
|
||||
{
|
||||
return ListValue.wrap(Arrays.stream(getString().split(delimiter.getString())).map(StringValue::new).collect(Collectors.toList()));
|
||||
return ListValue.wrap(Arrays.stream(getString().split(delimiter.getString())).map(StringValue::new));
|
||||
}
|
||||
catch (PatternSyntaxException pse)
|
||||
{
|
||||
|
||||
@@ -54,7 +54,6 @@ import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
@@ -274,12 +273,12 @@ public class ValueConversions
|
||||
}
|
||||
if (v instanceof PositionTracker)
|
||||
{
|
||||
return new BlockValue(null, (ServerLevel) e.getCommandSenderWorld(), ((PositionTracker)v).currentBlockPosition());
|
||||
return new BlockValue(null, e.getCommandSenderWorld(), ((PositionTracker)v).currentBlockPosition());
|
||||
}
|
||||
if (v instanceof WalkTarget)
|
||||
{
|
||||
return ListValue.of(
|
||||
new BlockValue(null, (ServerLevel) e.getCommandSenderWorld(), ((WalkTarget)v).getTarget().currentBlockPosition()),
|
||||
new BlockValue(null, e.getCommandSenderWorld(), ((WalkTarget)v).getTarget().currentBlockPosition()),
|
||||
new NumericValue(((WalkTarget) v).getSpeedModifier()),
|
||||
new NumericValue(((WalkTarget) v).getCloseEnoughDist())
|
||||
);
|
||||
@@ -297,11 +296,11 @@ public class ValueConversions
|
||||
Object el = l.get(0);
|
||||
if (el instanceof Entity)
|
||||
{
|
||||
return ListValue.wrap(l.stream().map(o -> new EntityValue((Entity)o)).collect(Collectors.toList()));
|
||||
return ListValue.wrap(l.stream().map(o -> new EntityValue((Entity)o)));
|
||||
}
|
||||
if (el instanceof GlobalPos)
|
||||
{
|
||||
return ListValue.wrap(l.stream().map(o -> of((GlobalPos) o)).collect(Collectors.toList()));
|
||||
return ListValue.wrap(l.stream().map(o -> of((GlobalPos) o)));
|
||||
}
|
||||
}
|
||||
return Value.NULL;
|
||||
@@ -480,7 +479,7 @@ public class ValueConversions
|
||||
if (o == null)
|
||||
return Value.NULL;
|
||||
if (o instanceof List)
|
||||
return ListValue.wrap(((List<?>) o).stream().map(oo -> guess(serverWorld, oo)).collect(Collectors.toList()));
|
||||
return ListValue.wrap(((List<?>) o).stream().map(oo -> guess(serverWorld, oo)));
|
||||
if (o instanceof BlockPos)
|
||||
return new BlockValue(null, serverWorld, (BlockPos)o);
|
||||
if (o instanceof Entity)
|
||||
|
||||
Reference in new issue
Block a user