Skip to content

Commit

Permalink
Bug fixes and datagen
Browse files Browse the repository at this point in the history
- fixed issue detecting custom recipes in alloy smelter
- fixed issue rendering mod armors
- re-ran datagen and replaced old item models and other resources
  • Loading branch information
Sephta committed Jun 23, 2024
1 parent 5883588 commit 96bb84d
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 188 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ private static boolean hasRecipe(AlloySmelterEntity entity) {

Optional<RecipeEntry<AlloySmelterRecipe>> recipeFromInventoryOpt = entity.getWorld()
.getRecipeManager()
.getFirstMatch(AlloySmelterRecipe.Type.INSTANCE, inventory, entity.getWorld());
.getFirstMatch(AlloySmelterRecipe.AlloySmelterRecipeType.INSTANCE, inventory, entity.getWorld());

if (recipeFromInventoryOpt.isPresent()) {
AlloySmelterRecipe recipe = recipeFromInventoryOpt.get().value();
Expand All @@ -237,7 +237,7 @@ private static void craftItem(AlloySmelterEntity entity) {

Optional<RecipeEntry<AlloySmelterRecipe>> recipeFromInventoryOpt = entity.getWorld()
.getRecipeManager()
.getFirstMatch(AlloySmelterRecipe.Type.INSTANCE, inventory, entity.getWorld());
.getFirstMatch(AlloySmelterRecipe.AlloySmelterRecipeType.INSTANCE, inventory, entity.getWorld());

if (recipeFromInventoryOpt.isPresent() && hasRecipe(entity)) {
AlloySmelterRecipe recipe = recipeFromInventoryOpt.get().value();
Expand Down
157 changes: 34 additions & 123 deletions src/main/java/net/stal/alloys/item/StalAlloysArmorMaterials.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,132 +19,43 @@
import net.stal.alloys.StalAlloys;

public class StalAlloysArmorMaterials extends ArmorMaterials{

public static final RegistryEntry<ArmorMaterial> BRASS;
public static final RegistryEntry<ArmorMaterial> STAINLESS_STEEL;

private static RegistryEntry<ArmorMaterial> register(String id, EnumMap<ArmorItem.Type, Integer> defense, int enchantability, RegistryEntry<SoundEvent> equipSound, float toughness, float knockbackResistance, Supplier<Ingredient> repairIngredient) {
List<ArmorMaterial.Layer> list = List.of(new ArmorMaterial.Layer(new Identifier(id)));
return register(id, defense, enchantability, equipSound, toughness, knockbackResistance, repairIngredient, list);
}

private static RegistryEntry<ArmorMaterial> register(String id, EnumMap<ArmorItem.Type, Integer> defense, int enchantability, RegistryEntry<SoundEvent> equipSound, float toughness, float knockbackResistance, Supplier<Ingredient> repairIngredient, List<ArmorMaterial.Layer> layers) {
EnumMap<ArmorItem.Type, Integer> enumMap = new EnumMap<ArmorItem.Type, Integer>(ArmorItem.Type.class);
ArmorItem.Type[] var9 = Type.values();
int var10 = var9.length;

for(int var11 = 0; var11 < var10; ++var11) {
ArmorItem.Type type = var9[var11];
enumMap.put(type, (Integer)defense.get(type));
}

return Registry.registerReference(Registries.ARMOR_MATERIAL, new Identifier(id), new ArmorMaterial(enumMap, enchantability, equipSound, repairIngredient, layers, toughness, knockbackResistance));
public static final RegistryEntry<ArmorMaterial> BRASS = registerArmorMaterial("brass", Util.make(new EnumMap<ArmorItem.Type, Integer>(ArmorItem.Type.class), (map) -> {
map.put(Type.BOOTS, 1);
map.put(Type.LEGGINGS, 4);
map.put(Type.CHESTPLATE, 5);
map.put(Type.HELMET, 2);
map.put(Type.BODY, 12);
}), 25, SoundEvents.ITEM_ARMOR_EQUIP_GOLD, 0.0F, 0.1F, () -> {
return Ingredient.ofItems(new ItemConvertible[]{StalAlloysItems.BRASS_INGOT});
});

public static final RegistryEntry<ArmorMaterial> STAINLESS_STEEL = registerArmorMaterial("stainless_steel", Util.make(new EnumMap<ArmorItem.Type, Integer>(ArmorItem.Type.class), (map) -> {
map.put(Type.BOOTS, 3);
map.put(Type.LEGGINGS, 6);
map.put(Type.CHESTPLATE, 8);
map.put(Type.HELMET, 3);
map.put(Type.BODY, 48);
}), 25, SoundEvents.ITEM_ARMOR_EQUIP_NETHERITE, 5.0F, 0.2F, () -> {
return Ingredient.ofItems(new ItemConvertible[]{StalAlloysItems.STAINLESS_STEEL_INGOT});
});

private static RegistryEntry<ArmorMaterial> registerArmorMaterial(String id, EnumMap<ArmorItem.Type, Integer> defense, int enchantability, RegistryEntry<SoundEvent> equipSound, float toughness, float knockbackResistance, Supplier<Ingredient> repairIngredient) {
return Registry.registerReference(
Registries.ARMOR_MATERIAL,
new Identifier(StalAlloys.MOD_ID, id),
new ArmorMaterial(
defense,
enchantability,
equipSound,
repairIngredient,
List.of(new ArmorMaterial.Layer(new Identifier(StalAlloys.MOD_ID, id))),
toughness,
knockbackResistance
)
);
}

static {
/**
DIAMOND = register("diamond", (EnumMap)Util.make(new EnumMap(ArmorItem.Type.class), (map) -> {
map.put(Type.BOOTS, 3);
map.put(Type.LEGGINGS, 6);
map.put(Type.CHESTPLATE, 8);
map.put(Type.HELMET, 3);
map.put(Type.BODY, 11);
}), 10, SoundEvents.ITEM_ARMOR_EQUIP_DIAMOND, 2.0F, 0.0F, () -> {
return Ingredient.ofItems(new ItemConvertible[]{Items.DIAMOND});
});
*/
BRASS = register("brass", Util.make(new EnumMap<ArmorItem.Type, Integer>(ArmorItem.Type.class), (map) -> {
map.put(Type.BOOTS, 1);
map.put(Type.LEGGINGS, 4);
map.put(Type.CHESTPLATE, 5);
map.put(Type.HELMET, 2);
map.put(Type.BODY, 8);
}), 12, SoundEvents.ITEM_ARMOR_EQUIP_GOLD, 2.0F, 0.1F, () -> {
return Ingredient.ofItems(new ItemConvertible[]{StalAlloysItems.BRASS_INGOT});
});

STAINLESS_STEEL = register("brass", Util.make(new EnumMap<ArmorItem.Type, Integer>(ArmorItem.Type.class), (map) -> {
map.put(Type.BOOTS, 3);
map.put(Type.LEGGINGS, 6);
map.put(Type.CHESTPLATE, 8);
map.put(Type.HELMET, 3);
map.put(Type.BODY, 12);
}), 48, SoundEvents.ITEM_ARMOR_EQUIP_NETHERITE, 5.0F, 0.2F, () -> {
return Ingredient.ofItems(new ItemConvertible[]{StalAlloysItems.STAINLESS_STEEL_INGOT});
});
}

public static void registerModArmorMaterials() {
StalAlloys.LOGGER.debug("Registering Armor Materials for " + StalAlloys.MOD_ID);
}

// BRASS("brass", 12, new int[]{1, 4, 5, 2}, 25, SoundEvents.ITEM_ARMOR_EQUIP_GOLD, 0.0f, 0.1f, () -> Ingredient.ofItems(StalAlloysItems.BRASS_INGOT)),
// STAINLESS_STEEL("stainless_steel", 48, new int[]{4, 7, 9, 4}, 25, SoundEvents.ITEM_ARMOR_EQUIP_NETHERITE, 5.0f, 0.2f, () -> Ingredient.ofItems(StalAlloysItems.STAINLESS_STEEL_INGOT));

// private static final int[] BASE_DURABILITY;
// private final String name;
// private final int durabilityMultiplier;
// private final int[] protectionAmounts;
// private final int enchantability;
// private final SoundEvent equipSound;
// private final float toughness;
// private final float knockbackResistance;
// private final Supplier<Ingredient> repairIngredientSupplier;

// private StalAlloysArmorMaterials(String name, int durabilityMultiplier, int[] protectionAmounts, int enchantability, SoundEvent equipSound, float toughness, float knockbackResistance, Supplier<Ingredient> repairIngredientSupplier) {
// this.name = name;
// this.durabilityMultiplier = durabilityMultiplier;
// this.protectionAmounts = protectionAmounts;
// this.enchantability = enchantability;
// this.equipSound = equipSound;
// this.toughness = toughness;
// this.knockbackResistance = knockbackResistance;
// this.repairIngredientSupplier = repairIngredientSupplier;
// }

// @Override
// public int getDurability(Type armorItemType) {
// return BASE_DURABILITY[armorItemType.getEquipmentSlot().getEntitySlotId()] * this.durabilityMultiplier;
// }

// @Override
// public int getProtection(Type armorItemType) {
// return this.protectionAmounts[armorItemType.getEquipmentSlot().getEntitySlotId()];
// }

// @Override
// public int getEnchantability() {
// return this.enchantability;
// }

// @Override
// public SoundEvent getEquipSound() {
// return this.equipSound;
// }

// @Override
// public Ingredient getRepairIngredient() {
// return this.repairIngredientSupplier.get();
// }

// @Override
// public String getName() {
// return StalAlloys.MOD_ID + ":" + this.name;
// }

// @Override
// public float getToughness() {
// return this.toughness;
// }

// @Override
// public float getKnockbackResistance() {
// return this.knockbackResistance;
// }

// static {
// BASE_DURABILITY = new int[]{13, 15, 16, 11};
// }


}
111 changes: 60 additions & 51 deletions src/main/java/net/stal/alloys/recipe/AlloySmelterRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.mojang.serialization.DataResult;
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.recipe.RecipeSerializer;

import net.minecraft.inventory.SimpleInventory;
import net.minecraft.item.ItemStack;
Expand All @@ -13,7 +14,6 @@
import net.minecraft.recipe.*;
import net.minecraft.world.World;
import net.minecraft.util.collection.DefaultedList;
import net.minecraft.util.dynamic.Codecs;

import java.util.List;

Expand All @@ -39,11 +39,11 @@ private AlloySmelterRecipeAttributes(String value) {
private static final int mNumberOfInputs = 2;
// private static final int mNumberOfOutputs = 1;

public AlloySmelterRecipe(ItemStack output, List<Ingredient> recipeItems, int cookingtime, int experience) {
mOutput = output;
mRecipeItems = recipeItems;
public AlloySmelterRecipe( int cookingtime, int experience, List<Ingredient> recipeItems, ItemStack output) {
mCookingTime = cookingtime;
mExperience = experience;
mRecipeItems = recipeItems;
mOutput = output;
}

@Override
Expand Down Expand Up @@ -84,86 +84,95 @@ public int getExperience() {
}

@Override
public RecipeSerializer<?> getSerializer() {
return Serializer.INSTANCE;
public RecipeSerializer<AlloySmelterRecipe> getSerializer() {
return AlloySmelterRecipeSerializer.INSTANCE;
}

@Override
public RecipeType<?> getType() {
return Type.INSTANCE;
public RecipeType<AlloySmelterRecipe> getType() {
return AlloySmelterRecipeType.INSTANCE;
}

public static class Type implements RecipeType<AlloySmelterRecipe> {
private Type() { }
public static final Type INSTANCE = new Type();
public static class AlloySmelterRecipeType implements RecipeType<AlloySmelterRecipe> {
private AlloySmelterRecipeType() {}
public static final AlloySmelterRecipeType INSTANCE = new AlloySmelterRecipeType();
public static final String ID = "alloy_smelter";
}

public static class Serializer implements RecipeSerializer<AlloySmelterRecipe> {
public static final Serializer INSTANCE = new Serializer();
public static class AlloySmelterRecipeSerializer implements RecipeSerializer<AlloySmelterRecipe> {
public AlloySmelterRecipeSerializer() {}
public static final AlloySmelterRecipeSerializer INSTANCE = new AlloySmelterRecipeSerializer();
public static final String ID = "alloy_smelter"; // name given in the json file

public static final MapCodec<AlloySmelterRecipe> CODEC = RecordCodecBuilder.mapCodec(in -> in.group(
ItemStack.VALIDATED_CODEC.fieldOf(AlloySmelterRecipeAttributes.RESULT.value).forGetter(r -> r.mOutput),
validateAmount(Ingredient.DISALLOW_EMPTY_CODEC, mNumberOfInputs).fieldOf(AlloySmelterRecipeAttributes.INGREDIENTS.value).forGetter(AlloySmelterRecipe::getIngredients),
Codecs.NONNEGATIVE_INT.fieldOf(AlloySmelterRecipeAttributes.COOKINGTIME.value).forGetter(AlloySmelterRecipe::getCookingTime),
Codecs.NONNEGATIVE_INT.fieldOf(AlloySmelterRecipeAttributes.EXPERIENCE.value).forGetter(AlloySmelterRecipe::getExperience)
).apply(in, AlloySmelterRecipe::new));
public static final MapCodec<AlloySmelterRecipe> CODEC = RecordCodecBuilder.mapCodec(instance -> instance.group(
Codec.INT.fieldOf(AlloySmelterRecipeAttributes.COOKINGTIME.value).forGetter(AlloySmelterRecipe::getCookingTime), // Cooking Time
Codec.INT.fieldOf(AlloySmelterRecipeAttributes.EXPERIENCE.value).forGetter(AlloySmelterRecipe::getExperience), // Experience
validateAmount(Ingredient.DISALLOW_EMPTY_CODEC, mNumberOfInputs)
.fieldOf(AlloySmelterRecipeAttributes.INGREDIENTS.value)
.forGetter(AlloySmelterRecipe::getIngredients), // Ingrediants
ItemStack.VALIDATED_UNCOUNTED_CODEC.fieldOf(AlloySmelterRecipeAttributes.RESULT.value).forGetter(r -> r.mOutput) // Result (output)
).apply(instance, AlloySmelterRecipe::new));

public static final PacketCodec<RegistryByteBuf, AlloySmelterRecipe> PACKET_CODEC =
PacketCodec.ofStatic(AlloySmelterRecipeSerializer::write, AlloySmelterRecipeSerializer::read);

private static Codec<List<Ingredient>> validateAmount(Codec<Ingredient> delegate, int max) {
return Ingredient.DISALLOW_EMPTY_CODEC.listOf().flatXmap((ingredients) -> {
Ingredient[] copyOfIngredients = (Ingredient[])ingredients.stream().filter((ingredient) -> {
return !ingredient.isEmpty();
}).toArray((i) -> {
return new Ingredient[i];
});
if (copyOfIngredients.length == 0) {
return DataResult.error(() -> {
return "No ingredients for shapeless recipe";
});
} else {
return copyOfIngredients.length > max ? DataResult.error(() -> {
return "Too many ingredients for shapeless recipe";
}) : DataResult.success(DefaultedList.copyOf(Ingredient.EMPTY, copyOfIngredients));
}
}, DataResult::success);
Codec<List<Ingredient>> validatedResult = Ingredient.DISALLOW_EMPTY_CODEC.listOf().flatXmap(
(ingredients) -> {
Ingredient[] copyOfIngredients = (Ingredient[])ingredients.stream().filter(ingredient -> {
return !ingredient.isEmpty();
}).toArray((i) -> {
return new Ingredient[i];
});

if (copyOfIngredients.length == 0) {
return DataResult.error(() -> {
return "No ingredients for shapeless recipe";
});
} else {
return copyOfIngredients.length > max ? DataResult.error(() -> {
return "Too many ingredients for shapeless recipe";
}) : DataResult.success(DefaultedList.copyOf(Ingredient.EMPTY, copyOfIngredients));
}
},
DataResult::success
);

return validatedResult;
}

@Override
public MapCodec<AlloySmelterRecipe> codec() {
return CODEC;
}

public static final PacketCodec<RegistryByteBuf, AlloySmelterRecipe> PACKET_CODEC = PacketCodec.ofStatic(Serializer::write, Serializer::read);
@Override
public PacketCodec<RegistryByteBuf, AlloySmelterRecipe> packetCodec() {
return PACKET_CODEC;
}

private static AlloySmelterRecipe read(RegistryByteBuf buf) {
DefaultedList<Ingredient> inputs = DefaultedList.ofSize(buf.readInt(), Ingredient.EMPTY);
int cookingtime = buf.readInt();
int experience = buf.readInt();
DefaultedList<Ingredient> inputs = DefaultedList.ofSize(buf.readInt(), Ingredient.EMPTY);

for (int i = 0; i < inputs.size(); i++) {
inputs.set(i, Ingredient.PACKET_CODEC.decode(buf));
}

return new AlloySmelterRecipe(/* output --> */ ItemStack.PACKET_CODEC.decode(buf), inputs, cookingtime, experience);
return new AlloySmelterRecipe(cookingtime, experience, inputs, /* output --> */ ItemStack.PACKET_CODEC.decode(buf));
}

private static void write(RegistryByteBuf buf, AlloySmelterRecipe recipe) {
buf.writeInt(recipe.getCookingTime());
buf.writeInt(recipe.getExperience());
buf.writeInt(recipe.getIngredients().size());

for (Ingredient ingredient : recipe.getIngredients()) {
Ingredient.PACKET_CODEC.encode(buf, ingredient);
}

ItemStack.PACKET_CODEC.encode(buf, recipe.getResult(null));

buf.writeInt(recipe.getCookingTime());
buf.writeInt(recipe.getExperience());
}

@Override
public MapCodec<AlloySmelterRecipe> codec() {
return CODEC;
}

@Override
public PacketCodec<RegistryByteBuf, AlloySmelterRecipe> packetCodec() {
return PACKET_CODEC;
}
}
}
8 changes: 4 additions & 4 deletions src/main/java/net/stal/alloys/recipe/StalAlloysRecipes.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ public static void registerRecipes() {
// Alloy Smelter Serializer
Registry.register(
Registries.RECIPE_SERIALIZER,
new Identifier(StalAlloys.MOD_ID, AlloySmelterRecipe.Serializer.ID),
AlloySmelterRecipe.Serializer.INSTANCE
new Identifier(StalAlloys.MOD_ID, AlloySmelterRecipe.AlloySmelterRecipeSerializer.ID),
AlloySmelterRecipe.AlloySmelterRecipeSerializer.INSTANCE
);

// Alloy Smelter Type
Registry.register(
Registries.RECIPE_TYPE,
new Identifier(StalAlloys.MOD_ID, AlloySmelterRecipe.Type.ID),
AlloySmelterRecipe.Type.INSTANCE
new Identifier(StalAlloys.MOD_ID, AlloySmelterRecipe.AlloySmelterRecipeType.ID),
AlloySmelterRecipe.AlloySmelterRecipeType.INSTANCE
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
}
],
"result": {
"item": "stal-alloys:brass_ingot"
"id": "stal-alloys:brass_ingot"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
}
],
"result": {
"item": "stal-alloys:bronze_ingot"
"id": "stal-alloys:bronze_ingot"
}
}
Loading

0 comments on commit 96bb84d

Please sign in to comment.