Skip to content

Commit

Permalink
Merge pull request #2 from ZhuRuoLing/releases/1.20.1
Browse files Browse the repository at this point in the history
🌟 实现部分方块状态 物品模型 方块模型的DataProvider
  • Loading branch information
Gu-ZT authored Sep 4, 2024
2 parents 8359ba3 + 08697a4 commit e1c2541
Show file tree
Hide file tree
Showing 23 changed files with 1,066 additions and 47 deletions.

This file was deleted.

This file was deleted.

33 changes: 33 additions & 0 deletions common/src/main/java/dev/anvilcraft/lib/data/DataProviderType.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package dev.anvilcraft.lib.data;

import dev.anvilcraft.lib.data.provider.AnvilLibBlockModelProvider;
import dev.anvilcraft.lib.data.provider.AnvilLibBlockStateProvider;
import dev.anvilcraft.lib.data.provider.AnvilLibItemModelProvider;
import dev.anvilcraft.lib.data.provider.LanguageProvider;
import dev.anvilcraft.lib.data.provider.RegistratorRecipeProvider;
import dev.anvilcraft.lib.data.provider.RegistratorTagsProvider;
import dev.anvilcraft.lib.data.provider.UpsideDownLanguageProvider;
import net.minecraft.data.DataGenerator;
import net.minecraft.data.DataProvider;
import net.minecraft.world.item.Item;
Expand All @@ -11,8 +18,34 @@

public interface DataProviderType<P extends DataProvider> {
DataProviderType<AnvilLibItemModelProvider> ITEM_MODEL = new DataProviderType<>() {
@Override
public void create(DataGenerator.@NotNull PackGenerator generator, @NotNull String namespace, @NotNull List<Consumer<AnvilLibItemModelProvider>> consumer) {
generator.addProvider(packOutput -> new AnvilLibItemModelProvider(
"models/item",
namespace,
packOutput
));
}
};
DataProviderType<AnvilLibBlockModelProvider> BLOCK_MODEL = new DataProviderType<>() {
@Override
public void create(DataGenerator.@NotNull PackGenerator generator, @NotNull String namespace, @NotNull List<Consumer<AnvilLibBlockModelProvider>> consumer) {
generator.addProvider(packOutput -> new AnvilLibBlockModelProvider(
"models/block",
namespace,
packOutput
));
}
};
DataProviderType<AnvilLibBlockStateProvider> BLOCK_STATE = new DataProviderType<AnvilLibBlockStateProvider>() {
@Override
public void create(DataGenerator.@NotNull PackGenerator generator, @NotNull String namespace, @NotNull List<Consumer<AnvilLibBlockStateProvider>> consumer) {
generator.addProvider(packOutput -> new AnvilLibBlockStateProvider(
"blockstates",
namespace,
packOutput
));
}
};
DataProviderType<RegistratorRecipeProvider> RECIPE = new DataProviderType<>() {
@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dev.anvilcraft.lib.data;

import com.google.gson.JsonElement;

public interface JsonSerializable {
JsonElement toJsonElement();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package dev.anvilcraft.lib.data.file;

import net.minecraft.resources.ResourceLocation;

public class BlockModelFile extends ModelFile<BlockModelFile> {

public BlockModelFile(ResourceLocation location) {
super(location);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package dev.anvilcraft.lib.data.file;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.minecraft.resources.ResourceLocation;

import java.util.HashMap;
import java.util.Map;

public class BlockStateFile extends ResourceFile {

private final Map<BlockStateVariantKey, BlockStateVariant> variants = new HashMap<>();

public BlockStateFile(ResourceLocation location) {
super(location);
}

public BlockStateFile single(BlockStateVariant v) {
variants.put(new BlockStateVariantKey(), v);
return this;
}

public BlockStateFile variant(
BlockStateVariantKey key,
BlockStateVariant variant
) {
this.variants.put(key, variant);
return this;
}

@Override
public JsonElement toJsonElement() {
JsonObject root = new JsonObject();
JsonObject variants = new JsonObject();
this.variants.forEach((key, var) -> variants.add(key.toString(), var.toJsonElement()));
root.add("variants", variants);
return root;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package dev.anvilcraft.lib.data.file;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import dev.anvilcraft.lib.data.JsonSerializable;
import lombok.Getter;
import net.minecraft.resources.ResourceLocation;

import java.util.function.Consumer;

@Getter
public class BlockStateVariant implements JsonSerializable {

private String model;
private int rotationX = 0;
private int rotationY = 0;
private boolean uvLock = false;

public BlockStateVariant model(String model) {
this.model = model;
return this;
}

public BlockStateVariant model(ResourceLocation model) {
return model(model.toString());
}

public BlockStateVariant rotationX(int rotation) {
rotationX = rotation;
return this;
}

public BlockStateVariant uvLock(boolean l){
this.uvLock = l;
return this;
}

public BlockStateVariant rotationY(int rotation) {
rotationY = rotation;
return this;
}

public static BlockStateVariant build(Consumer<BlockStateVariant> builder){
BlockStateVariant key = new BlockStateVariant();
builder.accept(key);
return key;
}

public static BlockStateVariant fromModel(ResourceLocation resourceLocation){
String ns = resourceLocation.getNamespace();
String path = resourceLocation.getPath();
if (path.startsWith("models/")){
path = path.replaceFirst("models/", "");
}
return new BlockStateVariant().model(new ResourceLocation(ns, path));
}

@Override
public JsonElement toJsonElement() {
JsonObject root = new JsonObject();
root.add("model", new JsonPrimitive(model));
root.add("x", new JsonPrimitive(rotationX));
root.add("y", new JsonPrimitive(rotationY));
root.add("uvlock", new JsonPrimitive(uvLock));
return root;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package dev.anvilcraft.lib.data.file;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import dev.anvilcraft.lib.data.JsonSerializable;
import net.minecraft.world.level.block.state.properties.Property;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

public class BlockStateVariantKey implements JsonSerializable {
private final List<BlockStateVariantKeyPair<?, ?>> keyPairs = new ArrayList<>();

public <T extends Property<V>, V extends Comparable<V>> BlockStateVariantKey then(
T property,
V value
) {
keyPairs.add(BlockStateVariantKeyPair.of(property, value));
return this;
}

public static <T extends Property<V>, V extends Comparable<V>> BlockStateVariantKey with(
T property,
V value
) {
return new BlockStateVariantKey().then(property, value);
}

public static BlockStateVariantKey build(Consumer<BlockStateVariantKey> builder) {
BlockStateVariantKey key = new BlockStateVariantKey();
builder.accept(key);
return key;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < keyPairs.size(); i++) {
if (i != 0) {
sb.append(",");
}
sb.append(keyPairs.get(i).toString());
}
return sb.toString();
}

@Override
public JsonElement toJsonElement() {
JsonObject root = new JsonObject();
keyPairs.forEach(kp -> kp.encodeIntoJsonObject(root));
return root;
}

public record BlockStateVariantKeyPair<T extends Property<V>, V extends Comparable<V>>(T property, V value) {

@Override
public String toString() {
return property.getName() + "=" + property.getName(value);
}

public static <T extends Property<V>, V extends Comparable<V>> BlockStateVariantKeyPair<T, V> of(
T property,
V value
) {
return new BlockStateVariantKeyPair<>(property, value);
}

public void encodeIntoJsonObject(JsonObject root) {
root.add(property.getName(), new JsonPrimitive(property.getName(value)));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package dev.anvilcraft.lib.data.file;

import net.minecraft.resources.ResourceLocation;

public class ItemModelFile extends ModelFile<ItemModelFile> {
public ItemModelFile(ResourceLocation location) {
super(location);
}


}
70 changes: 70 additions & 0 deletions common/src/main/java/dev/anvilcraft/lib/data/file/ModelFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package dev.anvilcraft.lib.data.file;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import lombok.Getter;
import net.minecraft.resources.ResourceLocation;

import java.util.HashMap;
import java.util.Map;

/**
* simple implementation for block/item model file
*/
@SuppressWarnings("unchecked")
@Getter
public class ModelFile<T extends ModelFile<T>> extends ResourceFile {
protected final Map<String, String> textures = new HashMap<>();
protected String parent;
protected String renderType;

public ModelFile(ResourceLocation location) {
super(location);
}

public T texture(String key, String texture){
textures.put(key, texture);
return (T) this;
}

public T texture(String key, ResourceLocation texture){
textures.put(key, texture.toString());
return (T) this;
}


public T parent(String p){
this.parent = p;
return (T) this;
}

public T parent(ResourceLocation p){
this.parent = p.toString();
return (T) this;
}

public T renderType(String r){
this.renderType = r;
return (T) this;
}

@Override
public JsonElement toJsonElement(){
JsonObject jsonObject = new JsonObject();
if (!textures.isEmpty()){
JsonObject jTextures = new JsonObject();
textures.forEach((k,v) -> {
jTextures.add(k, new JsonPrimitive(v));
});
jsonObject.add("textures", jTextures);
}
if (parent != null){
jsonObject.add("parent", new JsonPrimitive(parent));
}
if (renderType != null){
jsonObject.add("render_type", new JsonPrimitive(renderType));
}
return jsonObject;
}
}
Loading

0 comments on commit e1c2541

Please sign in to comment.