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

@@ -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) ->