| 1 | package net.minecraft.data.info;
|
| 2 |
|
| 3 | import com.google.gson.JsonArray;
|
| 4 | import com.google.gson.JsonObject;
|
| 5 | import java.nio.file.Path;
|
| 6 | import java.util.concurrent.CompletableFuture;
|
| 7 | import net.minecraft.core.HolderLookup;
|
| 8 | import net.minecraft.core.registries.Registries;
|
| 9 | import net.minecraft.data.CachedOutput;
|
| 10 | import net.minecraft.data.DataProvider;
|
| 11 | import net.minecraft.data.PackOutput;
|
| 12 | import net.minecraft.util.Util;
|
| 13 | import net.minecraft.world.level.block.Block;
|
| 14 | import net.minecraft.world.level.block.state.BlockState;
|
| 15 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 16 | import net.minecraft.world.level.block.state.properties.Property;
|
| 17 |
|
| 18 | /**
|
| 19 | * Access widened by fabric-data-generation-api-v1 to extendable
|
| 20 | */
|
| 21 | public class BlockListReport implements DataProvider {
|
| 22 | private final PackOutput output;
|
| 23 | private final CompletableFuture<HolderLookup.Provider> registries;
|
| 24 |
|
| 25 | public BlockListReport(final PackOutput output, final CompletableFuture<HolderLookup.Provider> registries) {
|
| 26 | this.output = output;
|
| 27 | this.registries = registries;
|
| 28 | }
|
| 29 |
|
| 30 | @Override
|
| 31 | public CompletableFuture<?> run(final CachedOutput cache) {
|
| 32 | Path path = this.output.getOutputFolder(PackOutput.Target.REPORTS).resolve("blocks.json");
|
| 33 | return this.registries.thenCompose(registries -> {
|
| 34 | JsonObject root = new JsonObject();
|
| 35 | registries.lookupOrThrow(Registries.BLOCK).listElements().forEach(block -> {
|
| 36 | JsonObject entry = new JsonObject();
|
| 37 | StateDefinition<Block, BlockState> definition = block.value().getStateDefinition();
|
| 38 | if (!definition.getProperties().isEmpty()) {
|
| 39 | JsonObject properties = new JsonObject();
|
| 40 |
|
| 41 | for (Property<?> property : definition.getProperties()) {
|
| 42 | JsonArray values = new JsonArray();
|
| 43 |
|
| 44 | for (Comparable<?> value : property.getPossibleValues()) {
|
| 45 | values.add(Util.getPropertyName(property, value));
|
| 46 | }
|
| 47 |
|
| 48 | properties.add(property.getName(), values);
|
| 49 | }
|
| 50 |
|
| 51 | entry.add("properties", properties);
|
| 52 | }
|
| 53 |
|
| 54 | JsonArray protocol = new JsonArray();
|
| 55 |
|
| 56 | for (BlockState state : definition.getPossibleStates()) {
|
| 57 | JsonObject stateEntry = new JsonObject();
|
| 58 | JsonObject properties = new JsonObject();
|
| 59 |
|
| 60 | for (Property<?> property : definition.getProperties()) {
|
| 61 | properties.addProperty(property.getName(), Util.getPropertyName(property, state.getValue(property)));
|
| 62 | }
|
| 63 |
|
| 64 | if (!properties.isEmpty()) {
|
| 65 | stateEntry.add("properties", properties);
|
| 66 | }
|
| 67 |
|
| 68 | stateEntry.addProperty("id", Block.getId(state));
|
| 69 | if (state == block.value().defaultBlockState()) {
|
| 70 | stateEntry.addProperty("default", true);
|
| 71 | }
|
| 72 |
|
| 73 | protocol.add(stateEntry);
|
| 74 | }
|
| 75 |
|
| 76 | entry.add("states", protocol);
|
| 77 | String id = block.getRegisteredName();
|
| 78 | root.add(id, entry);
|
| 79 | });
|
| 80 | return DataProvider.saveStable(cache, root, path);
|
| 81 | });
|
| 82 | }
|
| 83 |
|
| 84 | /**
|
| 85 | * Access widened by fabric-data-generation-api-v1 to extendable
|
| 86 | */
|
| 87 | @Override
|
| 88 | public String getName() {
|
| 89 | return "Block List";
|
| 90 | }
|
| 91 | }
|
| 92 |
|