Skip to content

Commit

Permalink
Add item default components output
Browse files Browse the repository at this point in the history
  • Loading branch information
Nickid2018 committed Oct 8, 2024
1 parent 00c93b6 commit 98cf2a1
Show file tree
Hide file tree
Showing 12 changed files with 161 additions and 15 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ dependencies {
compileOnly('org.lwjgl:lwjgl-stb:3.3.3')
compileOnly('org.joml:joml:1.10.5')
compileOnly('com.mojang:brigadier:1.3.10')
compileOnly('com.mojang:datafixerupper:8.0.16')
}

subprojects {
Expand Down Expand Up @@ -77,6 +78,7 @@ subprojects {
api 'com.google.guava:guava:32.1.3-jre'
api 'net.sf.jopt-simple:jopt-simple:6.0-alpha-3'
api 'com.mojang:brigadier:1.3.10'
api 'com.mojang:datafixerupper:8.0.16'
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package io.github.nickid2018.genwiki.autovalue;

import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import io.github.nickid2018.genwiki.autovalue.wikidata.DoubleNumberWikiData;
import io.github.nickid2018.genwiki.autovalue.wikidata.StringWikiData;
import io.github.nickid2018.genwiki.autovalue.wikidata.WikiData;
import lombok.SneakyThrows;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.level.GameRules;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package io.github.nickid2018.genwiki.autovalue;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.mojang.serialization.JsonOps;
import io.github.nickid2018.genwiki.autovalue.wikidata.*;
import io.github.nickid2018.genwiki.InjectionEntrypoint;
import io.github.nickid2018.genwiki.util.LanguageUtils;
import lombok.SneakyThrows;
import net.minecraft.core.component.DataComponents;
import net.minecraft.core.component.TypedDataComponent;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.food.FoodProperties;
import net.minecraft.resources.ResourceKey;
Expand All @@ -27,8 +31,10 @@ public class ItemDataExtractor {
.setFallback(0, 0)
.setFallbackNil(true);
private static final AttributeModifiersWikiData ATTRIBUTE_MODIFIERS = new AttributeModifiersWikiData();
private static final CodecWikiData DEFAULT_COMPONENTS = new CodecWikiData();

@SneakyThrows
@SuppressWarnings({"unchecked", "rawtypes"})
public static void extractItemData(MinecraftServer serverObj) {
Map<Item, String> itemKeyMap = new HashMap<>();
for (ResourceKey<Item> itemKey : BuiltInRegistries.ITEM.registryKeySet()) {
Expand Down Expand Up @@ -60,6 +66,20 @@ public static void extractItemData(MinecraftServer serverObj) {
FOOD_PROPERTIES.put(itemID, foodProperties.nutrition(), foodProperties.saturation());
} else
FOOD_PROPERTIES.put(itemID, 0, 0);

Map<String, JsonElement> data = new TreeMap<>();
for (TypedDataComponent component : itemStack.getComponents()) {
String type = BuiltInRegistries.DATA_COMPONENT_TYPE.getKey(component.type()).getPath();
if (component.type().codec() == null) continue;
JsonElement element = (JsonElement) component.type().codec().encodeStart(
serverObj.registryAccess().createSerializationContext(JsonOps.INSTANCE),
component.value()
).getOrThrow();
data.put(type, element);
}
JsonObject all = new JsonObject();
data.forEach(all::add);
DEFAULT_COMPONENTS.add(itemID, all);
}

CreativeModeTabs.tryRebuildTabContents(InjectionEntrypoint.featureFlagSet, true, serverObj.registryAccess());
Expand Down Expand Up @@ -90,5 +110,6 @@ public static void extractItemData(MinecraftServer serverObj) {
WikiData.write(MAX_DAMAGE, "item_max_damage.txt");
WikiData.write(FOOD_PROPERTIES, "item_food_properties.txt");
WikiData.write(ATTRIBUTE_MODIFIERS, "item_attribute_modifiers.txt");
WikiData.write(DEFAULT_COMPONENTS, "item_default_components.json");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.github.nickid2018.genwiki.autovalue.wikidata;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import java.util.Map;
import java.util.TreeMap;

public class CodecWikiData extends JsonWikiData {

private final Map<String, JsonElement> json = new TreeMap<>();

public void add(String name, JsonElement data) {
json.put(name, data);
}

@Override
public JsonElement asJsonData() {
JsonObject obj = new JsonObject();
json.forEach(obj::add);
return obj;
}
}
23 changes: 18 additions & 5 deletions src/main/java/io/github/nickid2018/genwiki/util/WebUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import com.google.gson.JsonParser;
import it.unimi.dsi.fastutil.objects.Object2BooleanMap;
import lombok.extern.slf4j.Slf4j;
import me.tongfei.progressbar.ProgressBar;
import me.tongfei.progressbar.ProgressBarBuilder;
import me.tongfei.progressbar.ProgressBarStyle;
import org.apache.commons.io.IOUtils;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
Expand All @@ -12,9 +15,7 @@
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.util.HashSet;
import java.util.Map;
Expand All @@ -33,15 +34,27 @@ public static JsonElement getJson(String url) throws IOException {
return client.execute(get, response -> {
if (response.getCode() != 200)
throw new IOException("Failed to get json from " + url + ": " + response.getCode());
return JsonParser.parseReader(new InputStreamReader(response.getEntity().getContent(), StandardCharsets.UTF_8));
return JsonParser.parseReader(new InputStreamReader(
response.getEntity().getContent(),
StandardCharsets.UTF_8
));
});
}
}

public static void downloadFile(String url, File file) throws IOException {
if (!file.getParentFile().isDirectory())
file.getParentFile().mkdirs();
try (InputStream input = new URI(url).toURL().openStream(); FileOutputStream output = new FileOutputStream(file)) {
try (
InputStream input = ProgressBar.wrap(
new URI(url).toURL().openConnection().getInputStream(),
new ProgressBarBuilder()
.setTaskName(url.substring(url.lastIndexOf('/') + 1))
.setStyle(ProgressBarStyle.ASCII)
.showSpeed()
.continuousUpdate()
); FileOutputStream output = new FileOutputStream(file)
) {
IOUtils.copy(input, output);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
Expand Down
7 changes: 7 additions & 0 deletions wrapped-mc/src/main/java/net/minecraft/core/HolderLookup.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package net.minecraft.core;

import com.mojang.serialization.DynamicOps;
import io.github.nickid2018.util.SneakyUtil;
import net.minecraft.resources.RegistryOps;

public interface HolderLookup {

interface Provider {

default <V> RegistryOps<V> createSerializationContext(DynamicOps<V> dynamicOps) {
return SneakyUtil.sneakyNotNull();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package net.minecraft.core.component;

public interface DataComponentMap {
import java.util.Set;

public interface DataComponentMap
extends Iterable<TypedDataComponent<?>>{

<T> T get(DataComponentType<? extends T> var1);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
package net.minecraft.core.component;

import com.mojang.serialization.Codec;

public interface DataComponentType<T> {

Codec<T> codec();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package net.minecraft.core.component;

public record TypedDataComponent<T>(DataComponentType<T> type, T value) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.github.nickid2018.util.SneakyUtil;
import net.minecraft.core.DefaultedRegistry;
import net.minecraft.core.Registry;
import net.minecraft.core.component.DataComponentType;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.ai.attributes.Attribute;
Expand All @@ -20,4 +21,5 @@ public class BuiltInRegistries {
public static final Registry<Potion> POTION = SneakyUtil.sneakyNotNull();
public static final DefaultedRegistry<Block> BLOCK = SneakyUtil.sneakyNotNull();
public static final Registry<Attribute> ATTRIBUTE = SneakyUtil.sneakyNotNull();
public static final Registry<DataComponentType<?>> DATA_COMPONENT_TYPE = SneakyUtil.sneakyNotNull();
}
74 changes: 74 additions & 0 deletions wrapped-mc/src/main/java/net/minecraft/resources/RegistryOps.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package net.minecraft.resources;

import com.mojang.datafixers.util.Pair;
import com.mojang.serialization.DataResult;
import com.mojang.serialization.DynamicOps;

import java.util.stream.Stream;

public class RegistryOps<T> implements DynamicOps<T> {
@Override
public T empty() {
return null;
}

@Override
public <U> U convertTo(DynamicOps<U> outOps, T input) {
return null;
}

@Override
public DataResult<Number> getNumberValue(T input) {
return null;
}

@Override
public T createNumeric(Number i) {
return null;
}

@Override
public DataResult<String> getStringValue(T input) {
return null;
}

@Override
public T createString(String value) {
return null;
}

@Override
public DataResult<T> mergeToList(T list, T value) {
return null;
}

@Override
public DataResult<T> mergeToMap(T map, T key, T value) {
return null;
}

@Override
public DataResult<Stream<Pair<T, T>>> getMapValues(T input) {
return null;
}

@Override
public T createMap(Stream<Pair<T, T>> map) {
return null;
}

@Override
public DataResult<Stream<T>> getStream(T input) {
return null;
}

@Override
public T createList(Stream<T> input) {
return null;
}

@Override
public T remove(T input, String key) {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package net.minecraft.world.item;

import com.mojang.serialization.Codec;
import io.github.nickid2018.util.SneakyUtil;
import net.minecraft.core.Holder;
import net.minecraft.core.component.DataComponentMap;
import net.minecraft.tags.TagKey;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.level.ItemLike;
Expand Down Expand Up @@ -37,8 +38,4 @@ public Rarity getRarity() {
public DataComponentMap getComponents() {
throw new RuntimeException();
}

public boolean is(TagKey<Item> tagKey) {
throw new RuntimeException();
}
}

0 comments on commit 98cf2a1

Please sign in to comment.