From 17249c3c77ad5de5319fdcb043abacfabc7b7bf2 Mon Sep 17 00:00:00 2001 From: altrisi Date: Thu, 19 Jan 2023 01:40:29 +0100 Subject: [PATCH] Refactor code to use `ListValue.wrap(Stream)` Simplifies code (improving readability) and unifies `Stream` to `ListValue` handling --- .../java/carpet/script/api/Auxiliary.java | 23 ++++++++----------- src/main/java/carpet/script/api/Entities.java | 8 +++---- .../java/carpet/script/api/Inventories.java | 18 +++++++-------- .../java/carpet/script/api/Scoreboards.java | 13 +++++------ .../java/carpet/script/api/WorldAccess.java | 21 ++++++++--------- .../script/command/CommandArgument.java | 8 +++---- .../carpet/script/language/ControlFlow.java | 2 +- .../script/language/DataStructures.java | 3 +-- .../carpet/script/language/Functions.java | 5 ++-- .../java/carpet/script/utils/BiomeInfo.java | 3 +-- .../java/carpet/script/utils/SystemInfo.java | 13 +++++------ .../java/carpet/script/value/EntityValue.java | 14 +++++------ .../script/value/NBTSerializableValue.java | 6 ++--- src/main/java/carpet/script/value/Value.java | 3 +-- .../carpet/script/value/ValueConversions.java | 11 ++++----- 15 files changed, 66 insertions(+), 85 deletions(-) diff --git a/src/main/java/carpet/script/api/Auxiliary.java b/src/main/java/carpet/script/api/Auxiliary.java index a82786a0..3357fb87 100644 --- a/src/main/java/carpet/script/api/Auxiliary.java +++ b/src/main/java/carpet/script/api/Auxiliary.java @@ -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 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 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 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) { diff --git a/src/main/java/carpet/script/api/Entities.java b/src/main/java/carpet/script/api/Entities.java index be7205ec..00b6036c 100644 --- a/src/main/java/carpet/script/api/Entities.java +++ b/src/main/java/carpet/script/api/Entities.java @@ -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 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 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 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> types = new HashSet<>(); descriptors.forEach(s -> types.addAll(EntityValue.getEntityDescriptor(s, ((CarpetContext) c).server()).types)); diff --git a/src/main/java/carpet/script/api/Inventories.java b/src/main/java/carpet/script/api/Inventories.java index 29015287..f03bb6e7 100644 --- a/src/main/java/carpet/script/api/Inventories.java +++ b/src/main/java/carpet/script/api/Inventories.java @@ -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 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> 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 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 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> 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 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); diff --git a/src/main/java/carpet/script/api/Scoreboards.java b/src/main/java/carpet/script/api/Scoreboards.java index 8332dcaf..dae7769b 100644 --- a/src/main/java/carpet/script/api/Scoreboards.java +++ b/src/main/java/carpet/script/api/Scoreboards.java @@ -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(); diff --git a/src/main/java/carpet/script/api/WorldAccess.java b/src/main/java/carpet/script/api/WorldAccess.java index cc9a546f..2f6768fb 100644 --- a/src/main/java/carpet/script/api/WorldAccess.java +++ b/src/main/java/carpet/script/api/WorldAccess.java @@ -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 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 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> 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 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> 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 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) -> diff --git a/src/main/java/carpet/script/command/CommandArgument.java b/src/main/java/carpet/script/command/CommandArgument.java index 280e10e9..5fcd34ba 100644 --- a/src/main/java/carpet/script/command/CommandArgument.java +++ b/src/main/java/carpet/script/command/CommandArgument.java @@ -2,7 +2,6 @@ package carpet.script.command; import carpet.CarpetServer; import carpet.fakes.BlockStateArgumentInterface; -import carpet.script.CarpetContext; import carpet.script.CarpetScriptHost; import carpet.script.argument.FunctionArgument; import carpet.script.value.BlockValue; @@ -96,7 +95,6 @@ import net.minecraft.core.Direction; import net.minecraft.core.Holder; import net.minecraft.core.HolderSet; import net.minecraft.core.Registry; -import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.core.registries.Registries; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceKey; @@ -613,7 +611,7 @@ public abstract class CommandArgument protected Value getValueFromContext(CommandContext context, String param) throws CommandSyntaxException { Collection founds = net.minecraft.commands.arguments.EntityArgument.getOptionalEntities(context, param); - if (!single) return ListValue.wrap(founds.stream().map(EntityValue::new).collect(Collectors.toList())); + if (!single) return ListValue.wrap(founds.stream().map(EntityValue::new)); if (founds.size() == 0) return Value.NULL; if (founds.size() == 1) return new EntityValue(founds.iterator().next()); throw new SimpleCommandExceptionType(Component.literal("Multiple entities returned while only one was requested"+" for custom type "+suffix)).create(); @@ -653,7 +651,7 @@ public abstract class CommandArgument protected Value getValueFromContext(CommandContext context, String param) throws CommandSyntaxException { Collection profiles = GameProfileArgument.getGameProfiles(context, param); - if (!single) return ListValue.wrap(profiles.stream().map(p -> StringValue.of(p.getName())).collect(Collectors.toList())); + if (!single) return ListValue.wrap(profiles.stream().map(p -> StringValue.of(p.getName()))); int size = profiles.size(); if (size == 0) return Value.NULL; if (size == 1) return StringValue.of(profiles.iterator().next().getName()); @@ -694,7 +692,7 @@ public abstract class CommandArgument protected Value getValueFromContext(CommandContext context, String param) throws CommandSyntaxException { Collection holders = ScoreHolderArgument.getNames(context, param); - if (!single) return ListValue.wrap(holders.stream().map(StringValue::of).collect(Collectors.toList())); + if (!single) return ListValue.wrap(holders.stream().map(StringValue::of)); int size = holders.size(); if (size == 0) return Value.NULL; if (size == 1) return StringValue.of(holders.iterator().next()); diff --git a/src/main/java/carpet/script/language/ControlFlow.java b/src/main/java/carpet/script/language/ControlFlow.java index d118a514..21fa4106 100644 --- a/src/main/java/carpet/script/language/ControlFlow.java +++ b/src/main/java/carpet/script/language/ControlFlow.java @@ -112,7 +112,7 @@ public class ControlFlow { StringValue.of(f.getString()), NumericValue.of(f.getToken().lineno+1), NumericValue.of(f.getToken().linepos+1) - )).collect(Collectors.toList())), + ))), StringValue.of("locals"), MapValue.wrap(ret.context.variables.entrySet().stream().filter(e -> !e.getKey().equals("_trace")).collect(Collectors.toMap( e -> StringValue.of(e.getKey()), diff --git a/src/main/java/carpet/script/language/DataStructures.java b/src/main/java/carpet/script/language/DataStructures.java index 81a1c393..1b9ed5e4 100644 --- a/src/main/java/carpet/script/language/DataStructures.java +++ b/src/main/java/carpet/script/language/DataStructures.java @@ -17,7 +17,6 @@ import carpet.script.value.NumericValue; import carpet.script.value.StringValue; import carpet.script.value.Value; import com.google.gson.JsonParseException; -import com.google.gson.JsonParser; import java.nio.charset.StandardCharsets; import java.util.ArrayList; @@ -186,7 +185,7 @@ public class DataStructures { if (v instanceof MapValue) return ListValue.wrap(((MapValue) v).getMap().entrySet().stream().map( (p) -> ListValue.of(p.getKey(), p.getValue()) - ).collect(Collectors.toList())); + )); return Value.NULL; }); diff --git a/src/main/java/carpet/script/language/Functions.java b/src/main/java/carpet/script/language/Functions.java index 7765d777..28953ed7 100644 --- a/src/main/java/carpet/script/language/Functions.java +++ b/src/main/java/carpet/script/language/Functions.java @@ -18,7 +18,6 @@ import carpet.script.value.Value; import java.util.ArrayList; import java.util.List; import java.util.Locale; -import java.util.stream.Collectors; public class Functions { public static void apply(Expression expression) // public just to get the javadoc right @@ -31,10 +30,10 @@ public class Functions { c.host.importModule(c, moduleName); moduleName = moduleName.toLowerCase(Locale.ROOT); if (lv.size() > 1) - c.host.importNames(c, expression.module, moduleName, lv.subList(1, lv.size()).stream().map(Value::getString).collect(Collectors.toList())); + c.host.importNames(c, expression.module, moduleName, lv.subList(1, lv.size()).stream().map(Value::getString).toList()); if (t == Context.VOID) return Value.NULL; - return ListValue.wrap(c.host.availableImports(moduleName).map(StringValue::new).collect(Collectors.toList())); + return ListValue.wrap(c.host.availableImports(moduleName).map(StringValue::new)); }); diff --git a/src/main/java/carpet/script/utils/BiomeInfo.java b/src/main/java/carpet/script/utils/BiomeInfo.java index 69b44889..f36054a1 100644 --- a/src/main/java/carpet/script/utils/BiomeInfo.java +++ b/src/main/java/carpet/script/utils/BiomeInfo.java @@ -10,7 +10,6 @@ import java.util.HashMap; import java.util.Locale; import java.util.Map; import java.util.function.BiFunction; -import java.util.stream.Collectors; import net.minecraft.core.BlockPos; import net.minecraft.core.Registry; @@ -25,7 +24,7 @@ public class BiomeInfo //put("top_material", (w, b) -> new BlockValue( b.getGenerationSettings(). getSurfaceConfig().getTopMaterial(), null, null)); //put("under_material", (w, b) -> new BlockValue( b.getGenerationSettings().getSurfaceConfig().getUnderMaterial(), null, null)); //put("category", (w, b) -> StringValue.of(Biome.getBiomeCategory(Holder.direct(b)).getName())); - put("tags", (w, b) -> ListValue.wrap(w.registryAccess().registryOrThrow(Registries.BIOME).getTags().filter(p -> p.getSecond().stream().anyMatch(h -> h.value() == b)).map(p -> p.getFirst().location()).map(ValueConversions::of).collect(Collectors.toList()))); + put("tags", (w, b) -> ListValue.wrap(w.registryAccess().registryOrThrow(Registries.BIOME).getTags().filter(p -> p.getSecond().stream().anyMatch(h -> h.value() == b)).map(p -> p.getFirst().location()).map(ValueConversions::of))); put("temperature", (w, b) -> NumericValue.of(b.getBaseTemperature())); put("fog_color", (w, b) -> ValueConversions.ofRGB(b.getSpecialEffects().getFogColor())); diff --git a/src/main/java/carpet/script/utils/SystemInfo.java b/src/main/java/carpet/script/utils/SystemInfo.java index e5112a1c..99390853 100644 --- a/src/main/java/carpet/script/utils/SystemInfo.java +++ b/src/main/java/carpet/script/utils/SystemInfo.java @@ -35,18 +35,17 @@ import java.util.List; import java.util.Map; import java.util.Objects; import java.util.function.Function; -import java.util.stream.Collectors; public class SystemInfo { - private static final Map> options = new HashMap>(){{ + private static final Map> options = new HashMap<>(){{ put("app_name", c -> { String name = c.host.getName(); return name == null?Value.NULL:new StringValue(name); }); - put("app_list", c -> ListValue.wrap(((CarpetScriptHost)c.host).scriptServer().modules.keySet().stream().filter(Objects::nonNull).map(StringValue::new).collect(Collectors.toList()))); + put("app_list", c -> ListValue.wrap(((CarpetScriptHost)c.host).scriptServer().modules.keySet().stream().filter(Objects::nonNull).map(StringValue::new))); put("app_scope", c -> StringValue.of((c.host).isPerUser()?"player":"global")); - put("app_players", c -> ListValue.wrap(c.host.getUserList().stream().map(StringValue::new).collect(Collectors.toList()))); + put("app_players", c -> ListValue.wrap(c.host.getUserList().stream().map(StringValue::new))); put("world_name", c -> new StringValue(c.server().getWorldData().getLevelName())); put("world_seed", c -> new NumericValue(c.level().getSeed())); @@ -59,7 +58,7 @@ public class SystemInfo { String tlf = serverPath.getName(nodeCount-2).toString(); return StringValue.of(tlf); }); - put("world_dimensions", c -> ListValue.wrap(c.server().levelKeys().stream().map(k -> ValueConversions.of(k.location())).collect(Collectors.toList()))); + put("world_dimensions", c -> ListValue.wrap(c.server().levelKeys().stream().map(k -> ValueConversions.of(k.location())))); put("world_spawn_point", c -> { LevelData prop = c.server().overworld().getLevelData(); return ListValue.of(NumericValue.of(prop.getXSpawn()), NumericValue.of(prop.getYSpawn()), NumericValue.of(prop.getZSpawn())); @@ -143,7 +142,7 @@ public class SystemInfo { final long[] tickArray = c.server().tickTimes; for (int i=currentReportedTick+100; i > currentReportedTick; i--) { - ticks.add(new NumericValue(((double)tickArray[i % 100])/1000000.0)); + ticks.add(new NumericValue((tickArray[i % 100])/1000000.0)); } return ListValue.wrap(ticks); }); @@ -219,7 +218,7 @@ public class SystemInfo { } public static Value getAll() { - return ListValue.wrap(options.keySet().stream().map(StringValue::of).collect(Collectors.toList())); + return ListValue.wrap(options.keySet().stream().map(StringValue::of)); } } diff --git a/src/main/java/carpet/script/value/EntityValue.java b/src/main/java/carpet/script/value/EntityValue.java index ffc3d44a..037a918d 100644 --- a/src/main/java/carpet/script/value/EntityValue.java +++ b/src/main/java/carpet/script/value/EntityValue.java @@ -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; diff --git a/src/main/java/carpet/script/value/NBTSerializableValue.java b/src/main/java/carpet/script/value/NBTSerializableValue.java index d48721a8..272f3fb1 100644 --- a/src/main/java/carpet/script/value/NBTSerializableValue.java +++ b/src/main/java/carpet/script/value/NBTSerializableValue.java @@ -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; diff --git a/src/main/java/carpet/script/value/Value.java b/src/main/java/carpet/script/value/Value.java index a7d55e38..c2ed9544 100644 --- a/src/main/java/carpet/script/value/Value.java +++ b/src/main/java/carpet/script/value/Value.java @@ -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, Cloneable @@ -179,7 +178,7 @@ public abstract class Value implements Comparable, 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) { diff --git a/src/main/java/carpet/script/value/ValueConversions.java b/src/main/java/carpet/script/value/ValueConversions.java index 6b28bfd0..2535886d 100644 --- a/src/main/java/carpet/script/value/ValueConversions.java +++ b/src/main/java/carpet/script/value/ValueConversions.java @@ -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)