9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-28 03:19:14 +00:00

完成高版本配方映射

This commit is contained in:
XiaoMoMi
2025-07-10 04:09:10 +08:00
parent 225d0f90a1
commit b77e6a42be
30 changed files with 388 additions and 36 deletions

View File

@@ -21,7 +21,7 @@ dependencies {
implementation("net.momirealms:sparrow-nbt-codec:${rootProject.properties["sparrow_nbt_version"]}")
implementation("net.momirealms:sparrow-nbt-legacy-codec:${rootProject.properties["sparrow_nbt_version"]}")
// S3
implementation("net.momirealms:craft-engine-s3:0.3")
implementation("net.momirealms:craft-engine-s3:0.4")
// Util
compileOnly("net.momirealms:sparrow-util:${rootProject.properties["sparrow_util_version"]}")
// Adventure

View File

@@ -97,4 +97,8 @@ public interface ItemManager<T> extends Manageable, ModelGenerator {
Item<T> decode(FriendlyByteBuf byteBuf);
void encode(FriendlyByteBuf byteBuf, Item<T> item);
Item<T> s2c(Item<T> item, Player player);
Item<T> c2s(Item<T> item);
}

View File

@@ -1,30 +1,51 @@
package net.momirealms.craftengine.core.item.recipe.network;
import com.mojang.datafixers.util.Either;
import net.momirealms.craftengine.core.entity.player.Player;
import net.momirealms.craftengine.core.item.recipe.network.display.RecipeDisplay;
import net.momirealms.craftengine.core.registry.BuiltInRegistries;
import net.momirealms.craftengine.core.util.FriendlyByteBuf;
import net.momirealms.craftengine.core.util.Key;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.OptionalInt;
public record RecipeBookDisplayEntry(RecipeDisplayId displayId, RecipeDisplay display, OptionalInt group, int category, Optional<List<Integer>> ingredients) {
public record RecipeBookDisplayEntry(RecipeDisplayId displayId, RecipeDisplay display, OptionalInt group, int category, Optional<List<Ingredient>> ingredients) {
public static RecipeBookDisplayEntry read(FriendlyByteBuf buffer) {
RecipeDisplayId displayId = RecipeDisplayId.read(buffer);
RecipeDisplay display = buffer.readById(BuiltInRegistries.RECIPE_DISPLAY_TYPE).read(buffer);
RecipeDisplay display = RecipeDisplay.read(buffer);
OptionalInt group = buffer.readOptionalVarInt();
int category = buffer.readVarInt(); // simplify the registry lookup since we don't care about the category
Optional<List<Integer>> requirements = buffer.readOptional(buf -> buf.readCollection(ArrayList::new, FriendlyByteBuf::readVarInt)); // simplify the registry lookup since we don't care about the ingredient ids
Optional<List<Ingredient>> requirements = buffer.readOptional(buf -> buf.readCollection(ArrayList::new, byteBuf -> new Ingredient(byteBuf.readHolderSet()))); // simplify the registry lookup since we don't care about the ingredient ids
return new RecipeBookDisplayEntry(displayId, display, group, category, requirements);
}
public void applyClientboundData(Player player) {
this.display.applyClientboundData(player);
}
public void write(FriendlyByteBuf buffer) {
this.displayId.write(buffer);
this.display.write(buffer);
buffer.writeOptionalVarInt(this.group);
buffer.writeVarInt(this.category);
buffer.writeOptional(this.ingredients, (buf, recipeIngredients) -> buf.writeCollection(recipeIngredients, FriendlyByteBuf::writeVarInt));
buffer.writeOptional(this.ingredients, (buf, recipeIngredients) -> buf.writeCollection(recipeIngredients, (byteBuf, ingredient) -> byteBuf.writeHolderSet(ingredient.holderSet)));
}
@Override
public @NotNull String toString() {
return "RecipeBookDisplayEntry{" +
"category=" + category +
", displayId=" + displayId +
", display=" + display +
", group=" + group +
", ingredients=" + ingredients +
'}';
}
public record Ingredient(Either<List<Integer>, Key> holderSet) {
}
}

View File

@@ -1,9 +1,14 @@
package net.momirealms.craftengine.core.item.recipe.network;
import net.momirealms.craftengine.core.entity.player.Player;
import net.momirealms.craftengine.core.util.FriendlyByteBuf;
public record RecipeBookEntry(RecipeBookDisplayEntry entry, byte flags) {
public void applyClientboundData(Player player) {
this.entry.applyClientboundData(player);
}
public static RecipeBookEntry read(FriendlyByteBuf buffer) {
RecipeBookDisplayEntry displayEntry = RecipeBookDisplayEntry.read(buffer);
byte flags = buffer.readByte();

View File

@@ -1,6 +1,7 @@
package net.momirealms.craftengine.core.item.recipe.network;
import net.momirealms.craftengine.core.util.FriendlyByteBuf;
import org.jetbrains.annotations.NotNull;
public record RecipeDisplayId(int id) {
@@ -11,4 +12,11 @@ public record RecipeDisplayId(int id) {
public static RecipeDisplayId read(FriendlyByteBuf buffer) {
return new RecipeDisplayId(buffer.readVarInt());
}
@Override
public @NotNull String toString() {
return "RecipeDisplayId{" +
"id=" + id +
'}';
}
}

View File

@@ -1,28 +1,50 @@
package net.momirealms.craftengine.core.item.recipe.network.display;
import net.momirealms.craftengine.core.entity.player.Player;
import net.momirealms.craftengine.core.item.recipe.network.display.slot.SlotDisplay;
import net.momirealms.craftengine.core.util.FriendlyByteBuf;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
public record FurnaceRecipeDisplay(SlotDisplay ingredient, SlotDisplay result, SlotDisplay craftingStation, int duration, float experience) implements RecipeDisplay {
public record FurnaceRecipeDisplay(SlotDisplay ingredient, SlotDisplay fuel, SlotDisplay result, SlotDisplay craftingStation, int duration, float experience) implements RecipeDisplay {
public static FurnaceRecipeDisplay read(FriendlyByteBuf buffer) {
SlotDisplay ingredient = SlotDisplay.read(buffer);
SlotDisplay fuel = SlotDisplay.read(buffer);
SlotDisplay result = SlotDisplay.read(buffer);
SlotDisplay craftingStation = SlotDisplay.read(buffer);
int duration = buffer.readVarInt();
float experience = buffer.readFloat();
return new FurnaceRecipeDisplay(ingredient, result, craftingStation, duration, experience);
return new FurnaceRecipeDisplay(ingredient, fuel, result, craftingStation, duration, experience);
}
@Override
public void applyClientboundData(Player player) {
this.ingredient.applyClientboundData(player);
this.fuel.applyClientboundData(player);
this.result.applyClientboundData(player);
this.craftingStation.applyClientboundData(player);
}
@Override
public void write(FriendlyByteBuf buf) {
buf.writeVarInt(2);
this.ingredient.write(buf);
this.fuel.write(buf);
this.result.write(buf);
this.craftingStation.write(buf);
buf.writeVarInt(this.duration);
buf.writeFloat(this.experience);
}
@Override
public @NotNull String toString() {
return "FurnaceRecipeDisplay{" +
"craftingStation=" + craftingStation +
", ingredient=" + ingredient +
", fuel=" + fuel +
", result=" + result +
", duration=" + duration +
", experience=" + experience +
'}';
}
}

View File

@@ -1,5 +1,7 @@
package net.momirealms.craftengine.core.item.recipe.network.display;
import net.momirealms.craftengine.core.entity.player.Player;
import net.momirealms.craftengine.core.registry.BuiltInRegistries;
import net.momirealms.craftengine.core.util.FriendlyByteBuf;
import java.util.function.Function;
@@ -8,6 +10,12 @@ public interface RecipeDisplay {
void write(FriendlyByteBuf buf);
void applyClientboundData(Player player);
static RecipeDisplay read(final FriendlyByteBuf buf) {
return buf.readById(BuiltInRegistries.RECIPE_DISPLAY_TYPE).read(buf);
}
record Type(Function<FriendlyByteBuf, RecipeDisplay> reader) {
public RecipeDisplay read(final FriendlyByteBuf buf) {

View File

@@ -18,7 +18,7 @@ public final class RecipeDisplays {
register(CRAFTING_SHAPELESS, new RecipeDisplay.Type(ShapelessCraftingRecipeDisplay::read));
register(CRAFTING_SHAPED, new RecipeDisplay.Type(ShapedCraftingRecipeDisplay::read));
register(FURNACE, new RecipeDisplay.Type(FurnaceRecipeDisplay::read));
register(STONECUTTER, new RecipeDisplay.Type(SmithingRecipeDisplay::read));
register(STONECUTTER, new RecipeDisplay.Type(StonecutterRecipeDisplay::read));
register(SMITHING, new RecipeDisplay.Type(SmithingRecipeDisplay::read));
}

View File

@@ -1,9 +1,9 @@
package net.momirealms.craftengine.core.item.recipe.network.display;
import net.momirealms.craftengine.core.entity.player.Player;
import net.momirealms.craftengine.core.item.recipe.network.display.slot.SlotDisplay;
import net.momirealms.craftengine.core.item.recipe.network.display.slot.SlotDisplays;
import net.momirealms.craftengine.core.registry.BuiltInRegistries;
import net.momirealms.craftengine.core.util.FriendlyByteBuf;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
@@ -19,12 +19,33 @@ public record ShapedCraftingRecipeDisplay(int width, int height, List<SlotDispla
return new ShapedCraftingRecipeDisplay(width, height, ingredients, result, craftingStation);
}
@Override
public void applyClientboundData(Player player) {
for (SlotDisplay ingredient : this.ingredients) {
ingredient.applyClientboundData(player);
}
this.result.applyClientboundData(player);
this.craftingStation.applyClientboundData(player);
}
@Override
public void write(FriendlyByteBuf buf) {
buf.writeVarInt(1);
buf.writeVarInt(this.width);
buf.writeVarInt(this.height);
buf.writeCollection(this.ingredients, (byteBuf, slotDisplay) -> slotDisplay.write(buf));
this.result.write(buf);
this.craftingStation.write(buf);
}
@Override
public @NotNull String toString() {
return "ShapedCraftingRecipeDisplay{" +
"craftingStation=" + craftingStation +
", width=" + width +
", height=" + height +
", ingredients=" + ingredients +
", result=" + result +
'}';
}
}

View File

@@ -1,7 +1,9 @@
package net.momirealms.craftengine.core.item.recipe.network.display;
import net.momirealms.craftengine.core.entity.player.Player;
import net.momirealms.craftengine.core.item.recipe.network.display.slot.SlotDisplay;
import net.momirealms.craftengine.core.util.FriendlyByteBuf;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
@@ -15,10 +17,29 @@ public record ShapelessCraftingRecipeDisplay(List<SlotDisplay> ingredients, Slot
return new ShapelessCraftingRecipeDisplay(ingredients, result, craftingStation);
}
@Override
public void applyClientboundData(Player player) {
for (SlotDisplay ingredient : ingredients) {
ingredient.applyClientboundData(player);
}
this.result.applyClientboundData(player);
this.craftingStation.applyClientboundData(player);
}
@Override
public void write(FriendlyByteBuf buf) {
buf.writeVarInt(0);
buf.writeCollection(this.ingredients, (byteBuf, slotDisplay) -> slotDisplay.write(buf));
this.result.write(buf);
this.craftingStation.write(buf);
}
@Override
public @NotNull String toString() {
return "ShapelessCraftingRecipeDisplay{" +
"craftingStation=" + craftingStation +
", ingredients=" + ingredients +
", result=" + result +
'}';
}
}

View File

@@ -1,10 +1,9 @@
package net.momirealms.craftengine.core.item.recipe.network.display;
import net.momirealms.craftengine.core.entity.player.Player;
import net.momirealms.craftengine.core.item.recipe.network.display.slot.SlotDisplay;
import net.momirealms.craftengine.core.util.FriendlyByteBuf;
import java.util.ArrayList;
import java.util.List;
import org.jetbrains.annotations.NotNull;
public record SmithingRecipeDisplay(SlotDisplay template, SlotDisplay base, SlotDisplay addition, SlotDisplay result, SlotDisplay craftingStation) implements RecipeDisplay {
@@ -17,12 +16,33 @@ public record SmithingRecipeDisplay(SlotDisplay template, SlotDisplay base, Slot
return new SmithingRecipeDisplay(template, base, addition, result, craftingStation);
}
@Override
public void applyClientboundData(Player player) {
this.template.applyClientboundData(player);
this.base.applyClientboundData(player);
this.addition.applyClientboundData(player);
this.result.applyClientboundData(player);
this.craftingStation.applyClientboundData(player);
}
@Override
public void write(FriendlyByteBuf buf) {
buf.writeVarInt(4);
this.template.write(buf);
this.base.write(buf);
this.addition.write(buf);
this.result.write(buf);
this.craftingStation.write(buf);
}
@Override
public @NotNull String toString() {
return "SmithingRecipeDisplay{" +
"addition=" + addition +
", template=" + template +
", base=" + base +
", result=" + result +
", craftingStation=" + craftingStation +
'}';
}
}

View File

@@ -1,28 +1,40 @@
package net.momirealms.craftengine.core.item.recipe.network.display;
import net.momirealms.craftengine.core.entity.player.Player;
import net.momirealms.craftengine.core.item.recipe.network.display.slot.SlotDisplay;
import net.momirealms.craftengine.core.util.FriendlyByteBuf;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
public record StonecutterRecipeDisplay(SlotDisplay input, SlotDisplay result, SlotDisplay craftingStation) implements RecipeDisplay {
public record SmithingRecipeDisplay(SlotDisplay template, SlotDisplay base, SlotDisplay addition, SlotDisplay result, SlotDisplay craftingStation) implements RecipeDisplay {
public static SmithingRecipeDisplay read(FriendlyByteBuf buffer) {
SlotDisplay template = SlotDisplay.read(buffer);
SlotDisplay base = SlotDisplay.read(buffer);
SlotDisplay addition = SlotDisplay.read(buffer);
public static StonecutterRecipeDisplay read(FriendlyByteBuf buffer) {
SlotDisplay input = SlotDisplay.read(buffer);
SlotDisplay result = SlotDisplay.read(buffer);
SlotDisplay craftingStation = SlotDisplay.read(buffer);
return new SmithingRecipeDisplay(template, base, addition, result, craftingStation);
return new StonecutterRecipeDisplay(input, result, craftingStation);
}
@Override
public void applyClientboundData(Player player) {
this.input.applyClientboundData(player);
this.result.applyClientboundData(player);
this.craftingStation.applyClientboundData(player);
}
@Override
public void write(FriendlyByteBuf buf) {
this.template.write(buf);
this.base.write(buf);
this.addition.write(buf);
buf.writeVarInt(3);
this.input.write(buf);
this.result.write(buf);
this.craftingStation.write(buf);
}
@Override
public @NotNull String toString() {
return "StonecutterRecipeDisplay{" +
"craftingStation=" + craftingStation +
", input=" + input +
", result=" + result +
'}';
}
}

View File

@@ -11,5 +11,11 @@ public class AnyFuelDisplay implements SlotDisplay {
@Override
public void write(FriendlyByteBuf buf) {
buf.writeVarInt(1);
}
@Override
public String toString() {
return "AnyFuelDisplay{}";
}
}

View File

@@ -1,5 +1,6 @@
package net.momirealms.craftengine.core.item.recipe.network.display.slot;
import net.momirealms.craftengine.core.entity.player.Player;
import net.momirealms.craftengine.core.util.FriendlyByteBuf;
import java.util.ArrayList;
@@ -17,12 +18,27 @@ public class CompositeSlotDisplay implements SlotDisplay {
return new CompositeSlotDisplay(slots);
}
@Override
public void applyClientboundData(Player player) {
for (SlotDisplay slotDisplay : this.slots) {
slotDisplay.applyClientboundData(player);
}
}
@Override
public void write(FriendlyByteBuf buf) {
buf.writeVarInt(7);
buf.writeCollection(this.slots, (byteBuf, slotDisplay) -> slotDisplay.write(buf));
}
public List<SlotDisplay> slots() {
return this.slots;
}
@Override
public String toString() {
return "CompositeSlotDisplay{" +
"slots=" + slots +
'}';
}
}

View File

@@ -11,5 +11,11 @@ public class EmptySlotDisplay implements SlotDisplay {
@Override
public void write(FriendlyByteBuf buf) {
buf.writeVarInt(0);
}
@Override
public String toString() {
return "EmptySlotDisplay{}";
}
}

View File

@@ -16,10 +16,18 @@ public class ItemSlotDisplay implements SlotDisplay {
@Override
public void write(FriendlyByteBuf buf) {
buf.writeVarInt(2);
buf.writeVarInt(this.item);
}
public int item() {
return item;
}
@Override
public String toString() {
return "ItemSlotDisplay{" +
"item=" + item +
'}';
}
}

View File

@@ -1,28 +1,46 @@
package net.momirealms.craftengine.core.item.recipe.network.display.slot;
import net.momirealms.craftengine.core.entity.player.Player;
import net.momirealms.craftengine.core.item.Item;
import net.momirealms.craftengine.core.plugin.CraftEngine;
import net.momirealms.craftengine.core.util.FriendlyByteBuf;
public class ItemStackSlotDisplay implements SlotDisplay {
private final Item<?> item;
private Item<Object> item;
public ItemStackSlotDisplay(Item<?> item) {
public ItemStackSlotDisplay(Item<Object> item) {
this.item = item;
}
public static ItemStackSlotDisplay read(FriendlyByteBuf buf) {
Item<?> itemStack = CraftEngine.instance().itemManager().decode(buf);
Item<Object> itemStack = CraftEngine.instance().itemManager().decode(buf);
return new ItemStackSlotDisplay(itemStack);
}
@SuppressWarnings("unchecked")
@Override
public void write(FriendlyByteBuf buf) {
CraftEngine.instance().itemManager().encode(buf, (Item<Object>) this.item);
buf.writeVarInt(3);
CraftEngine.instance().itemManager().encode(buf, this.item);
}
@Override
public void applyClientboundData(Player player) {
System.out.println("gai ni ma");
this.item = CraftEngine.instance().itemManager().s2c(this.item, player);
}
public Item<?> item() {
return item;
return this.item;
}
public void setItem(Item<Object> item) {
this.item = item;
}
@Override
public String toString() {
return "ItemStackSlotDisplay{" +
"item=" + this.item.getLiteralObject() +
'}';
}
}

View File

@@ -1,5 +1,6 @@
package net.momirealms.craftengine.core.item.recipe.network.display.slot;
import net.momirealms.craftengine.core.entity.player.Player;
import net.momirealms.craftengine.core.registry.BuiltInRegistries;
import net.momirealms.craftengine.core.util.FriendlyByteBuf;
@@ -9,6 +10,9 @@ public interface SlotDisplay {
void write(FriendlyByteBuf buf);
default void applyClientboundData(Player player) {
}
static SlotDisplay read(FriendlyByteBuf buf) {
return buf.readById(BuiltInRegistries.SLOT_DISPLAY_TYPE).read(buf);
}

View File

@@ -6,6 +6,7 @@ import net.momirealms.craftengine.core.util.AdventureHelper;
import net.momirealms.craftengine.core.util.FriendlyByteBuf;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.VersionHelper;
import org.jetbrains.annotations.NotNull;
public class SmithingTrimDemoSlotDisplay implements SlotDisplay {
private final SlotDisplay base;
@@ -46,6 +47,7 @@ public class SmithingTrimDemoSlotDisplay implements SlotDisplay {
@Override
public void write(FriendlyByteBuf buf) {
buf.writeVarInt(5);
this.base.write(buf);
this.material.write(buf);
if (VersionHelper.isOrAbove1_21_5()) {
@@ -59,6 +61,25 @@ public class SmithingTrimDemoSlotDisplay implements SlotDisplay {
}
}
@Override
public String toString() {
return "SmithingTrimDemoSlotDisplay{" +
"base=" + base +
", material=" + material +
", trimPattern=" + trimPattern +
", either=" + either +
'}';
}
public record TrimPattern(Key assetId, Component description, boolean decal) {
@Override
public @NotNull String toString() {
return "TrimPattern{" +
"assetId=" + assetId +
", description=" + description +
", decal=" + decal +
'}';
}
}
}

View File

@@ -16,6 +16,14 @@ public class TagSlotDisplay implements SlotDisplay {
@Override
public void write(FriendlyByteBuf buf) {
buf.writeVarInt(4);
buf.writeKey(this.tag);
}
@Override
public String toString() {
return "TagSlotDisplay{" +
"tag=" + tag +
'}';
}
}

View File

@@ -19,7 +19,16 @@ public class WithRemainderSlotDisplay implements SlotDisplay {
@Override
public void write(FriendlyByteBuf buf) {
buf.writeVarInt(6);
this.input.write(buf);
this.remainder.write(buf);
}
@Override
public String toString() {
return "WithRemainderSlotDisplay{" +
"input=" + input +
", remainder=" + remainder +
'}';
}
}

View File

@@ -413,6 +413,33 @@ public class FriendlyByteBuf extends ByteBuf {
});
}
public Either<List<Integer>, Key> readHolderSet() {
int id = this.readVarInt();
if (id == 0) {
return Either.right(readKey());
} else {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < id - 1; ++i) {
list.add(readVarInt());
}
return Either.left(list);
}
}
public void writeHolderSet(Either<List<Integer>, Key> holderSet) {
holderSet.ifLeft(
ints -> {
writeVarInt(ints.size() + 1);
for (Integer anInt : ints) {
writeVarInt(anInt);
}
}
).ifRight(key -> {
writeVarInt(0);
writeKey(key);
});
}
public FriendlyByteBuf writeVarLong(long value) {
while ((value & -128L) != 0L) {
this.writeByte((int) (value & 127L) | 128);