Refactor code to use ListValue.wrap(Stream)

Simplifies code (improving readability) and unifies `Stream<Value>` to `ListValue` handling
This commit is contained in:
altrisi authored and GitHub committed 2023-01-19 01:40:29 +01:00
1 parent dd1dc3756a
commit 17249c3c77
15 files changed
+66 -85

No files matched your search

+10 -13
View File
@@ -42,17 +42,14 @@ import net.minecraft.SharedConstants;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Holder;
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.Rotations;
import net.minecraft.core.particles.ParticleOptions;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtIo;
import net.minecraft.nbt.NbtUtils;
import net.minecraft.nbt.StringTag;
import net.minecraft.nbt.Tag;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.network.chat.Component;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientboundClearTitlesPacket;
@@ -572,7 +569,7 @@ public class Auxiliary {
});
else
targetList.forEach(target -> {
map.put(target.getScoreboardName(), (MutableComponent) title);
map.put(target.getScoreboardName(), title);
total.getAndIncrement();
});
HUDController.update_hud(((CarpetContext)c).server(), targetList);
@@ -621,7 +618,7 @@ public class Auxiliary {
);
return ListValue.of(
retval,
ListValue.wrap(output.stream().map(FormattedTextValue::new).collect(Collectors.toList())),
ListValue.wrap(output.stream().map(FormattedTextValue::new)),
FormattedTextValue.of(error[0])
);
}
@@ -757,19 +754,19 @@ public class Auxiliary {
Map<Value, Value> plopData = new HashMap<>();
CarpetContext cc = (CarpetContext)c;
plopData.put(StringValue.of("scarpet_custom"),
ListValue.wrap(FeatureGenerator.featureMap.keySet().stream().sorted().map(StringValue::of).collect(Collectors.toList()))
ListValue.wrap(FeatureGenerator.featureMap.keySet().stream().sorted().map(StringValue::of))
);
plopData.put(StringValue.of("features"),
ListValue.wrap(cc.registry(Registries.FEATURE).keySet().stream().sorted().map(ValueConversions::of).collect(Collectors.toList()))
ListValue.wrap(cc.registry(Registries.FEATURE).keySet().stream().sorted().map(ValueConversions::of))
);
plopData.put(StringValue.of("configured_features"),
ListValue.wrap(cc.registry(Registries.CONFIGURED_FEATURE).keySet().stream().sorted().map(ValueConversions::of).collect(Collectors.toList()))
ListValue.wrap(cc.registry(Registries.CONFIGURED_FEATURE).keySet().stream().sorted().map(ValueConversions::of))
);
plopData.put(StringValue.of("structure_types"),
ListValue.wrap(cc.registry(Registries.STRUCTURE_TYPE).keySet().stream().sorted().map(ValueConversions::of).collect(Collectors.toList()))
ListValue.wrap(cc.registry(Registries.STRUCTURE_TYPE).keySet().stream().sorted().map(ValueConversions::of))
);
plopData.put(StringValue.of("structures"),
ListValue.wrap(cc.registry(Registries.STRUCTURE).keySet().stream().sorted().map(ValueConversions::of).collect(Collectors.toList()))
ListValue.wrap(cc.registry(Registries.STRUCTURE).keySet().stream().sorted().map(ValueConversions::of))
);
return MapValue.wrap(plopData);
}
@@ -841,7 +838,7 @@ public class Auxiliary {
FileArgument fdesc = FileArgument.from(lv,true, FileArgument.Reason.READ);
Stream<String> files = ((CarpetScriptHost) c.host).listFolder(fdesc);
if (files == null) return Value.NULL;
return ListValue.wrap(files.map(StringValue::of).collect(Collectors.toList()));
return ListValue.wrap(files.map(StringValue::of));
});
expression.addContextFunction("read_file", 2, (c, t, lv) ->
@@ -868,7 +865,7 @@ public class Auxiliary {
{
List<String> content = ((CarpetScriptHost) c.host).readTextResource(fdesc);
if (content == null) return Value.NULL;
retVal = ListValue.wrap(content.stream().map(StringValue::new).collect(Collectors.toList()));
retVal = ListValue.wrap(content.stream().map(StringValue::new));
}
return retVal;
});
@@ -1015,7 +1012,7 @@ public class Auxiliary {
CarpetContext cc = (CarpetContext) c;
CommandStorage storage = cc.server().getCommandStorage();
if (lv.size() == 0)
return ListValue.wrap(storage.keys().map(i -> new StringValue(nameFromRegistryId(i))).collect(Collectors.toList()));
return ListValue.wrap(storage.keys().map(i -> new StringValue(nameFromRegistryId(i))));
String key = lv.get(0).getString();
CompoundTag old_nbt = storage.get(InputValidator.identifierOf(key));
if (lv.size() == 2) {
@@ -23,9 +23,7 @@ import java.util.Locale;
import java.util.Set;
import java.util.UUID;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.ResourceLocation;
@@ -157,7 +155,7 @@ public class Entities {
CommandSourceStack source = ((CarpetContext)c).source();
EntityValue.EntityClassDescriptor eDesc = EntityValue.getEntityDescriptor(who, source.getServer());
List<? extends Entity> entityList = source.getLevel().getEntities(eDesc.directType, eDesc.filteringPredicate);
return ListValue.wrap(entityList.stream().map(EntityValue::new).collect(Collectors.toList()));
return ListValue.wrap(entityList.stream().map(EntityValue::new));
});
expression.addContextFunction("entity_area", -1, (c, t, lv) ->
@@ -185,7 +183,7 @@ public class Entities {
AABB area = centerBox.inflate(range.x, range.y, range.z);
EntityValue.EntityClassDescriptor eDesc = EntityValue.getEntityDescriptor(who, cc.server());
List<? extends Entity> entityList = cc.level().getEntities(eDesc.directType, area,eDesc.filteringPredicate);
return ListValue.wrap(entityList.stream().map(EntityValue::new).collect(Collectors.toList()));
return ListValue.wrap(entityList.stream().map(EntityValue::new));
});
expression.addContextFunction("entity_selector", -1, (c, t, lv) ->
@@ -253,7 +251,7 @@ public class Entities {
if (lv.size() < 2) throw new InternalExpressionException("'entity_load_handler' required the entity type, and a function to call");
Value entityValue = lv.get(0);
List<String> descriptors = (entityValue instanceof ListValue)
? ((ListValue) entityValue).getItems().stream().map(Value::getString).collect(Collectors.toList())
? ((ListValue) entityValue).getItems().stream().map(Value::getString).toList()
: Collections.singletonList(entityValue.getString());
Set<EntityType<? extends Entity>> types = new HashSet<>();
descriptors.forEach(s -> types.addAll(EntityValue.getEntityDescriptor(s, ((CarpetContext) c).server()).types));
@@ -26,12 +26,10 @@ import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import net.minecraft.commands.arguments.item.ItemInput;
import net.minecraft.core.HolderSet;
import net.minecraft.core.Registry;
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
@@ -70,17 +68,17 @@ public class Inventories {
CarpetContext cc = (CarpetContext)c;
Registry<Item> items = cc.registry(Registries.ITEM);
if (lv.size() == 0)
return ListValue.wrap(items.keySet().stream().map(ValueConversions::of).collect(Collectors.toList()));
return ListValue.wrap(items.keySet().stream().map(ValueConversions::of));
String tag = lv.get(0).getString();
Optional<HolderSet.Named<Item>> itemTag = items.getTag(TagKey.create(Registries.ITEM, InputValidator.identifierOf(tag)));
if (itemTag.isEmpty()) return Value.NULL;
return ListValue.wrap(itemTag.get().stream().map(b -> ValueConversions.of(items.getKey(b.value()))).collect(Collectors.toList()));
return ListValue.wrap(itemTag.get().stream().map(b -> ValueConversions.of(items.getKey(b.value()))));
/*
TagContainer tagManager = cc.s.getServer(). getTags();
String tag = lv.get(0).getString();
net.minecraft.tags.Tag<Item> itemTag = tagManager.getOrEmpty(Registry.ITEM_REGISTRY).getTag(InputValidator.identifierOf(tag));
if (itemTag == null) return Value.NULL;
return ListValue.wrap(itemTag.getValues().stream().map(b -> ValueConversions.of(Registry.ITEM.getKey(b))).collect(Collectors.toList()));
return ListValue.wrap(itemTag.getValues().stream().map(b -> ValueConversions.of(Registry.ITEM.getKey(b))));
*/
});
@@ -90,11 +88,11 @@ public class Inventories {
Registry<Item> blocks = cc.registry(Registries.ITEM);
if (lv.size() == 0)
return ListValue.wrap(blocks.getTagNames().map(ValueConversions::of).collect(Collectors.toList()));
return ListValue.wrap(blocks.getTagNames().map(ValueConversions::of));
Item item = NBTSerializableValue.parseItem(lv.get(0).getString(), cc.registryAccess()).getItem();
if (lv.size() == 1)
{
return ListValue.wrap( blocks.getTags().filter(e -> e.getSecond().stream().anyMatch(h -> (h.value() == item))).map(e -> ValueConversions.of(e.getFirst())).collect(Collectors.toList()));
return ListValue.wrap( blocks.getTags().filter(e -> e.getSecond().stream().anyMatch(h -> (h.value() == item))).map(e -> ValueConversions.of(e.getFirst())));
}
String tag = lv.get(1).getString();
Optional<HolderSet.Named<Item>> tagSet = blocks.getTag(TagKey.create(Registries.ITEM, InputValidator.identifierOf(tag)));
@@ -106,10 +104,10 @@ public class Inventories {
/*
TagContainer tagManager = cc.s.getServer().getTags();
if (lv.size() == 0)
return ListValue.wrap(tagManager.getOrEmpty(Registry.ITEM_REGISTRY).getAvailableTags().stream().map(ValueConversions::of).collect(Collectors.toList()));
return ListValue.wrap(tagManager.getOrEmpty(Registry.ITEM_REGISTRY).getAvailableTags().stream().map(ValueConversions::of));
Item item = NBTSerializableValue.parseItem(lv.get(0).getString()).getItem();
if (lv.size() == 1)
return ListValue.wrap(tagManager.getOrEmpty(Registry.ITEM_REGISTRY).getAllTags().entrySet().stream().filter(e -> e.getValue().contains(item)).map(e -> ValueConversions.of(e.getKey())).collect(Collectors.toList()));
return ListValue.wrap(tagManager.getOrEmpty(Registry.ITEM_REGISTRY).getAllTags().entrySet().stream().filter(e -> e.getValue().contains(item)).map(e -> ValueConversions.of(e.getKey())));
String tag = lv.get(1).getString();
net.minecraft.tags.Tag<Item> itemTag = tagManager.getOrEmpty(Registry.ITEM_REGISTRY).getTag(InputValidator.identifierOf(tag));
if (itemTag == null) return Value.NULL;
@@ -398,7 +396,7 @@ public class Inventories {
else if (owner instanceof LivingEntity villager)
{
// stolen from LookTargetUtil.give((VillagerEntity)owner, droppedStack, (LivingEntity) owner);
double double_1 = villager.getY() - 0.30000001192092896D + (double)villager.getEyeHeight();
double double_1 = villager.getY() - 0.30000001192092896D + villager.getEyeHeight();
item = new ItemEntity(villager.level, villager.getX(), double_1, villager.getZ(), droppedStack);
Vec3 vec3d_1 = villager.getViewVector(1.0F).normalize().scale(0.3);// new Vec3d(0, 0.3, 0);
item.setDeltaMovement(vec3d_1);
@@ -18,7 +18,6 @@ import carpet.script.value.Value;
import com.google.common.collect.Lists;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
@@ -66,12 +65,12 @@ public class Scoreboards {
CarpetContext cc = (CarpetContext)c;
Scoreboard scoreboard = cc.server().getScoreboard();
if (lv.size()==0)
return ListValue.wrap(scoreboard.getObjectiveNames().stream().map(StringValue::new).collect(Collectors.toList()));
return ListValue.wrap(scoreboard.getObjectiveNames().stream().map(StringValue::new));
String objectiveName = lv.get(0).getString();
Objective objective = scoreboard.getOrCreateObjective(objectiveName);
if (objective == null) return Value.NULL;
if (lv.size()==1)
return ListValue.wrap(scoreboard.getPlayerScores(objective).stream().map(s -> new StringValue(s.getOwner())).collect(Collectors.toList()));
return ListValue.wrap(scoreboard.getPlayerScores(objective).stream().map(s -> new StringValue(s.getOwner())));
String key = getScoreboardKeyFromValue(lv.get(1));
if(lv.size()==2)
{
@@ -246,11 +245,11 @@ public class Scoreboards {
CarpetContext cc = (CarpetContext)c;
ServerScoreboard scoreboard = cc.server().getScoreboard();
if(lv.size() == 0)
return ListValue.wrap(scoreboard.getTeamNames().stream().map(StringValue::of).collect(Collectors.toList()));
return ListValue.wrap(scoreboard.getTeamNames().stream().map(StringValue::of));
if (lv.size() != 1) return Value.NULL;
PlayerTeam team = scoreboard.getPlayerTeam(lv.get(0).getString());
if(team == null) return Value.NULL;
return ListValue.wrap(team.getPlayers().stream().map(StringValue::of).collect(Collectors.toList()));
return ListValue.wrap(team.getPlayers().stream().map(StringValue::of));
});
@@ -398,7 +397,7 @@ public class Scoreboards {
CustomBossEvents bossBarManager = ((CarpetContext)c).server().getCustomBossEvents();
if(lv.size() > 3) throw new InternalExpressionException("'bossbar' accepts max three arguments");
if(lv.size() == 0) return ListValue.wrap(bossBarManager.getEvents().stream().map(CustomBossEvent::getTextId).map(ResourceLocation::toString).map(StringValue::of).collect(Collectors.toList()));
if(lv.size() == 0) return ListValue.wrap(bossBarManager.getEvents().stream().map(CustomBossEvent::getTextId).map(ResourceLocation::toString).map(StringValue::of));
String id = lv.get(0).getString();
ResourceLocation identifier;
@@ -458,7 +457,7 @@ public class Scoreboards {
}
return Value.FALSE;
case "players":
if (propertyValue == null) return ListValue.wrap(bossBar.getPlayers().stream().map(EntityValue::new).collect(Collectors.toList()));
if (propertyValue == null) return ListValue.wrap(bossBar.getPlayers().stream().map(EntityValue::new));
if(propertyValue instanceof ListValue)
{
bossBar.removeAllPlayers();
@@ -44,7 +44,6 @@ import net.minecraft.core.Holder;
import net.minecraft.core.HolderSet;
import net.minecraft.core.QuartPos;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.tags.TagKey;
@@ -342,7 +341,7 @@ public class WorldAccess {
new NumericValue(p.getPoiType().value().maxTickets() - ((PoiRecord_scarpetMixin)p).getFreeTickets()),
ValueConversions.of(p.getPos())
)
).collect(Collectors.toList()));
));
});
//poi_set(pos, null) poi_set(pos, type, occupied?,
@@ -1104,7 +1103,7 @@ public class WorldAccess {
BlockState state = locator.block.getBlockState();
StateDefinition<Block, BlockState> states = state.getBlock().getStateDefinition();
return ListValue.wrap(states.getProperties().stream().map(
p -> new StringValue(p.getName())).collect(Collectors.toList())
p -> new StringValue(p.getName()))
);
});
@@ -1136,12 +1135,12 @@ public class WorldAccess {
CarpetContext cc = (CarpetContext)c;
Registry<Block> blocks = cc.registry(Registries.BLOCK);
if (lv.size() == 0)
return ListValue.wrap(blocks.keySet().stream().map(ValueConversions::of).collect(Collectors.toList()));
return ListValue.wrap(blocks.keySet().stream().map(ValueConversions::of));
ResourceLocation tag = InputValidator.identifierOf(lv.get(0).getString());
Optional<HolderSet.Named<Block>> tagset = blocks.getTag(TagKey.create(Registries.BLOCK, tag));
if (tagset.isEmpty()) return Value.NULL;
return ListValue.wrap(tagset.get().stream().map(b -> ValueConversions.of(blocks.getKey(b.value()))).collect(Collectors.toList()));
return ListValue.wrap(tagset.get().stream().map(b -> ValueConversions.of(blocks.getKey(b.value()))));
});
expression.addContextFunction("block_tags", -1, (c, t, lv) ->
@@ -1149,12 +1148,12 @@ public class WorldAccess {
CarpetContext cc = (CarpetContext)c;
Registry<Block> blocks = cc.registry(Registries.BLOCK);
if (lv.size() == 0)
return ListValue.wrap(blocks.getTagNames().map(ValueConversions::of).collect(Collectors.toList()));
return ListValue.wrap(blocks.getTagNames().map(ValueConversions::of));
BlockArgument blockLocator = BlockArgument.findIn(cc, lv, 0, true);
if (blockLocator.offset == lv.size())
{
Block target = blockLocator.block.getBlockState().getBlock();
return ListValue.wrap( blocks.getTags().filter(e -> e.getSecond().stream().anyMatch(h -> (h.value() == target))).map(e -> ValueConversions.of(e.getFirst())).collect(Collectors.toList()));
return ListValue.wrap( blocks.getTags().filter(e -> e.getSecond().stream().anyMatch(h -> (h.value() == target))).map(e -> ValueConversions.of(e.getFirst())));
}
String tag = lv.get(blockLocator.offset).getString();
Optional<HolderSet.Named<Block>> tagSet = blocks.getTag(TagKey.create(Registries.BLOCK, InputValidator.identifierOf(tag)));
@@ -1165,12 +1164,12 @@ public class WorldAccess {
/* before
TagContainer tagManager = cc.s.getServer().getTags();
if (lv.size() == 0)
return ListValue.wrap(tagManager.getOrEmpty(Registry.BLOCK_REGISTRY).getAvailableTags().stream().map(ValueConversions::of).collect(Collectors.toList()));
return ListValue.wrap(tagManager.getOrEmpty(Registry.BLOCK_REGISTRY).getAvailableTags().stream().map(ValueConversions::of));
BlockArgument blockLocator = BlockArgument.findIn(cc, lv, 0, true);
if (blockLocator.offset == lv.size())
{
Block target = blockLocator.block.getBlockState().getBlock();
return ListValue.wrap(tagManager.getOrEmpty(Registry.BLOCK_REGISTRY).getAllTags().entrySet().stream().filter(e -> e.getValue().contains(target)).map(e -> ValueConversions.of(e.getKey())).collect(Collectors.toList()));
return ListValue.wrap(tagManager.getOrEmpty(Registry.BLOCK_REGISTRY).getAllTags().entrySet().stream().filter(e -> e.getValue().contains(target)).map(e -> ValueConversions.of(e.getKey())));
}
String tag = lv.get(blockLocator.offset).getString();
net.minecraft.tags.Tag<Block> blockTag = tagManager.getOrEmpty(Registry.BLOCK_REGISTRY).getTag(InputValidator.identifierOf(tag));
@@ -1303,7 +1302,7 @@ public class WorldAccess {
if (lv.size() == locator.offset)
return ListValue.wrap(references.entrySet().stream().
filter(e -> e.getValue()!= null && !e.getValue().isEmpty()).
map(e -> new StringValue(NBTSerializableValue.nameFromRegistryId(reg.getKey(e.getKey())))).collect(Collectors.toList())
map(e -> new StringValue(NBTSerializableValue.nameFromRegistryId(reg.getKey(e.getKey()))))
);
String simpleStructureName = lv.get(locator.offset).getString().toLowerCase(Locale.ROOT);
Structure structureName = reg.get(InputValidator.identifierOf(simpleStructureName));
@@ -1313,7 +1312,7 @@ public class WorldAccess {
return ListValue.wrap(structureReferences.longStream().mapToObj(l -> ListValue.of(
new NumericValue(16*ChunkPos.getX(l)),
Value.ZERO,
new NumericValue(16*ChunkPos.getZ(l)))).collect(Collectors.toList()));
new NumericValue(16*ChunkPos.getZ(l)))));
});
expression.addContextFunction("structure_eligibility", -1, (c, t, lv) ->