Skip to content
This repository has been archived by the owner on Mar 10, 2024. It is now read-only.

Commit

Permalink
Merge branch '1.17-2.0' into 1.18-2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Siphalor committed Jul 9, 2022
2 parents ff49d50 + 57f1950 commit 8c592a5
Show file tree
Hide file tree
Showing 11 changed files with 149 additions and 38 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/build-example-datapack.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
name: Update example datapack
on:
push:
paths:
- src/testmod/resources/**
- .github/workflows/update-readme.yaml
workflow_dispatch:

jobs:
publish:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Upload artifact
uses: actions/upload-artifact@v3
with:
name: example-datapack
path: src/testmod/resources/**
34 changes: 34 additions & 0 deletions .github/workflows/update-readme.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
name: Update Readme
on:
workflow_run:
types:
- completed
workflows:
- Update example datapack

jobs:
update-example-datapack:
runs-on: ubuntu-latest
steps:
- name: Fetch artifact links
uses: michpohl/expose-artifacts@5e82c4b92a1005dc88b8aa4922211426392a55c9
with:
github-token: "${{ secrets.GITHUB_TOKEN }}"
output-var-name: ARTIFACT_URL
contains-string: example-datapack

- name: Checkout
uses: actions/checkout@v2
with:
ref: ${{ github.event.workflow_run.head_branch }}

- name: Update README
run: |
sed -i -e "s/\[Example Datapack\](.*)/[Example Datapack]($(echo ${{ env.ARTIFACT_URL_0 }} | sed -e s/\\//\\\\\\//g))/" README.md
- name: Auto-Commit
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: Update link to example datapack
file_pattern: README.md
2 changes: 1 addition & 1 deletion .giup
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"merge-paths": [
"1.15-2.0",
"1.15-2.0->1.16-2.0->1.17-2.0->1.18-2.0"
"1.15-2.0->1.16-2.0->1.17-2.0->1.18-2.0->1.19-2.0"
],
"commands": [
{
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Nbt Crafting 2.0 ![supported Minecraft versions: 1.15 | 1.16 | 1.17 | 1.18](https://img.shields.io/badge/support%20for%20MC-1.15%20%7C%201.16%20%7C%201.17%20%7C%201.18-%2356AD56)
# Nbt Crafting 2.0 ![supported Minecraft versions: 1.15 | 1.16 | 1.17 | 1.18](https://img.shields.io/badge/support%20for%20MC-1.15%20%7C%201.16%20%7C%201.17%20%7C%201.18%20%7C%201.19-%2356AD56)

[![curseforge downloads](http://cf.way2muchnoise.eu/full_nbt-crafting_downloads.svg)](https://minecraft.curseforge.com/projects/nbt-crafting)
[![modrinth downloads](https://img.shields.io/badge/dynamic/json?color=5da545&label=modrinth&prefix=downloads%20&query=downloads&url=https://api.modrinth.com/api/v1/mod/18ztUZP5&style=flat&logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAxMSAxMSIgd2lkdGg9IjE0LjY2NyIgaGVpZ2h0PSIxNC42NjciICB4bWxuczp2PSJodHRwczovL3ZlY3RhLmlvL25hbm8iPjxkZWZzPjxjbGlwUGF0aCBpZD0iQSI+PHBhdGggZD0iTTAgMGgxMXYxMUgweiIvPjwvY2xpcFBhdGg+PC9kZWZzPjxnIGNsaXAtcGF0aD0idXJsKCNBKSI+PHBhdGggZD0iTTEuMzA5IDcuODU3YTQuNjQgNC42NCAwIDAgMS0uNDYxLTEuMDYzSDBDLjU5MSA5LjIwNiAyLjc5NiAxMSA1LjQyMiAxMWMxLjk4MSAwIDMuNzIyLTEuMDIgNC43MTEtMi41NTZoMGwtLjc1LS4zNDVjLS44NTQgMS4yNjEtMi4zMSAyLjA5Mi0zLjk2MSAyLjA5MmE0Ljc4IDQuNzggMCAwIDEtMy4wMDUtMS4wNTVsMS44MDktMS40NzQuOTg0Ljg0NyAxLjkwNS0xLjAwM0w4LjE3NCA1LjgybC0uMzg0LS43ODYtMS4xMTYuNjM1LS41MTYuNjk0LS42MjYuMjM2LS44NzMtLjM4N2gwbC0uMjEzLS45MS4zNTUtLjU2Ljc4Ny0uMzcuODQ1LS45NTktLjcwMi0uNTEtMS44NzQuNzEzLTEuMzYyIDEuNjUxLjY0NSAxLjA5OC0xLjgzMSAxLjQ5MnptOS42MTQtMS40NEE1LjQ0IDUuNDQgMCAwIDAgMTEgNS41QzExIDIuNDY0IDguNTAxIDAgNS40MjIgMCAyLjc5NiAwIC41OTEgMS43OTQgMCA0LjIwNmguODQ4QzEuNDE5IDIuMjQ1IDMuMjUyLjgwOSA1LjQyMi44MDljMi42MjYgMCA0Ljc1OCAyLjEwMiA0Ljc1OCA0LjY5MSAwIC4xOS0uMDEyLjM3Ni0uMDM0LjU2bC43NzcuMzU3aDB6IiBmaWxsLXJ1bGU9ImV2ZW5vZGQiIGZpbGw9IiM1ZGE0MjYiLz48L2c+PC9zdmc+)](https://modrinth.com/mod/nbt-crafting)
Expand All @@ -15,7 +15,10 @@ This can be achieved through the reintroduced `data` attribute.

**You can find more information in the [wiki](https://mcwiki.siphalor.de/nbt-crafting/v2).**

## Example
## Examples

You can find a datapack with some examples here: [Example Datapack](https://github.com/Siphalor/nbt-crafting/suites/7028990982/artifacts/276408387)

A simple recipe allowing to craft wooden swords with 20 damage from diamond swords with 2 damage.

```json
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ org.gradle.jvmargs=-Xmx1G
loader_version=0.12.5

# Mod Properties
mod_version = 2.1.3
mod_release = release
mod_version = 2.1.4-alpha.1
mod_release = alpha
mod_mc_version_specifier = 1.18.2
mod_mc_versions = 1.18.2
mod_id = nbtcrafting
Expand Down
22 changes: 15 additions & 7 deletions src/main/java/de/siphalor/nbtcrafting/NbtCrafting.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package de.siphalor.nbtcrafting;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
Expand All @@ -25,9 +26,6 @@
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.mojang.datafixers.util.Pair;

import de.siphalor.nbtcrafting.recipe.WrappedRecipeSerializer;

import io.netty.buffer.Unpooled;
import it.unimi.dsi.fastutil.ints.*;
import net.fabricmc.api.ModInitializer;
Expand All @@ -51,6 +49,7 @@
import de.siphalor.nbtcrafting.recipe.AnvilRecipe;
import de.siphalor.nbtcrafting.recipe.BrewingRecipe;
import de.siphalor.nbtcrafting.recipe.IngredientRecipe;
import de.siphalor.nbtcrafting.recipe.WrappedRecipeSerializer;
import de.siphalor.nbtcrafting.recipe.cauldron.CauldronRecipe;
import de.siphalor.nbtcrafting.recipe.cauldron.CauldronRecipeSerializer;
import de.siphalor.nbtcrafting.util.duck.IServerPlayerEntity;
Expand Down Expand Up @@ -193,8 +192,7 @@ public static <S extends RecipeSerializer<T>, T extends Recipe<?>> S registerRec
return Registry.register(Registry.RECIPE_SERIALIZER, serializerId, recipeSerializer);
}

public static PacketByteBuf createAdvancedRecipeSyncPacket(RecipeManager recipeManager) {
PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer());
public static List<PacketByteBuf> createAdvancedRecipeSyncPackets(RecipeManager recipeManager) {
advancedIngredientSerializationEnabled.set(true);
List<Recipe<?>> recipes = recipeManager.values().stream().filter(recipe -> {
for (Ingredient ingredient : recipe.getIngredients()) {
Expand All @@ -204,17 +202,27 @@ public static PacketByteBuf createAdvancedRecipeSyncPacket(RecipeManager recipeM
}
return false;
}).collect(Collectors.toList());
buf.writeVarInt(recipes.size());

List<PacketByteBuf> packets = new ArrayList<>();
PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer());
buf.writeVarInt(0);

for (Recipe<?> recipe : recipes) {
@SuppressWarnings("rawtypes")
RecipeSerializer serializer = recipe.getSerializer();
buf.writeIdentifier(Registry.RECIPE_SERIALIZER.getId(serializer));
buf.writeIdentifier(recipe.getId());
//noinspection unchecked
serializer.write(buf, recipe);

if (buf.readableBytes() > 1_900_000) { // max packet size is 2^21=2_097_152 bytes
packets.add(buf);
buf = new PacketByteBuf(Unpooled.buffer());
buf.writeVarInt(0);
}
}
advancedIngredientSerializationEnabled.set(false);
return buf;
return packets;
}

public static boolean isAdvancedIngredientSerializationEnabled() {
Expand Down
48 changes: 33 additions & 15 deletions src/main/java/de/siphalor/nbtcrafting/client/NbtCraftingClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.networking.v1.ClientLoginNetworking;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.networking.v1.PacketSender;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.ingame.AnvilScreen;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.client.network.ClientPlayNetworkHandler;
import net.minecraft.recipe.Recipe;
import net.minecraft.recipe.RecipeManager;
import net.minecraft.recipe.RecipeSerializer;
Expand All @@ -54,25 +56,41 @@ public void onInitializeClient() {
buf.readString();
});

ClientPlayNetworking.registerGlobalReceiver(NbtCrafting.UPDATE_ADVANCED_RECIPES_PACKET_ID, (client, handler, buf, responseSender) -> {
RecipeManager recipeManager = handler.getRecipeManager();
Map<RecipeType<?>, Map<Identifier, Recipe<?>>> recipeMap = ((RecipeManagerAccessor) recipeManager).getRecipes();
recipeMap = new HashMap<>(recipeMap);
ClientPlayNetworking.registerGlobalReceiver(NbtCrafting.UPDATE_ADVANCED_RECIPES_PACKET_ID, NbtCraftingClient::receiveAdvancedRecipePacket);
}

int recipeCount = buf.readVarInt();
NbtCrafting.advancedIngredientSerializationEnabled.set(true);
for (int i = 0; i < recipeCount; i++) {
RecipeSerializer<?> serializer = Registry.RECIPE_SERIALIZER.get(buf.readIdentifier());
Identifier id = buf.readIdentifier();
private static synchronized void receiveAdvancedRecipePacket(MinecraftClient client, ClientPlayNetworkHandler handler, PacketByteBuf buf, PacketSender responseSender) {
RecipeManager recipeManager = handler.getRecipeManager();
Map<RecipeType<?>, Map<Identifier, Recipe<?>>> recipeMap = ((RecipeManagerAccessor) recipeManager).getRecipes();
recipeMap = new HashMap<>(recipeMap);

Recipe<?> recipe = serializer.read(id, buf);
Map<Identifier, Recipe<?>> recipeType = recipeMap.computeIfAbsent(recipe.getType(), rt -> new HashMap<>());
recipeType.put(id, recipe);
NbtCrafting.advancedIngredientSerializationEnabled.set(true);
int recipeCount = buf.readVarInt();
if (recipeCount == 0) {
while (buf.isReadable()) {
readRecipe(buf, recipeMap);
}
NbtCrafting.advancedIngredientSerializationEnabled.set(false);
} else { // Legacy support
for (int i = 0; i < recipeCount; i++) {
readRecipe(buf, recipeMap);
}
}
NbtCrafting.advancedIngredientSerializationEnabled.set(false);

((RecipeManagerAccessor) recipeManager).setRecipes(ImmutableMap.copyOf(recipeMap));
});
((RecipeManagerAccessor) recipeManager).setRecipes(ImmutableMap.copyOf(recipeMap));
}

private static void readRecipe(PacketByteBuf buf, Map<RecipeType<?>, Map<Identifier, Recipe<?>>> recipes) {
RecipeSerializer<?> serializer = Registry.RECIPE_SERIALIZER.get(buf.readIdentifier());
if (serializer == null) {
throw new IllegalStateException("Unknown recipe serializer on advanced recipe sync: " + buf.readIdentifier());
}

Identifier id = buf.readIdentifier();

Recipe<?> recipe = serializer.read(id, buf);
Map<Identifier, Recipe<?>> recipeType = recipes.computeIfAbsent(recipe.getType(), rt -> new HashMap<>());
recipeType.put(id, recipe);
}

public static RecipeManager getClientRecipeManager() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@

package de.siphalor.nbtcrafting.mixin.network;

import java.util.List;
import java.util.stream.Collectors;
import de.siphalor.nbtcrafting.NbtCrafting;

import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.network.ClientConnection;
Expand All @@ -33,7 +32,8 @@
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import de.siphalor.nbtcrafting.NbtCrafting;
import java.util.List;
import java.util.stream.Collectors;

@Mixin(PlayerManager.class)
public class MixinPlayerManager {
Expand Down Expand Up @@ -65,8 +65,10 @@ public void beforeRegistrySync(ClientConnection connection, ServerPlayerEntity p
public void afterRecipeSync(ClientConnection connection, ServerPlayerEntity player, CallbackInfo ci) {
if (NbtCrafting.hasClientMod(player)) {
NbtCrafting.logInfo("Syncing advanced recipe data to player " + player.getEntityName());
PacketByteBuf buf = NbtCrafting.createAdvancedRecipeSyncPacket(server.getRecipeManager());
ServerPlayNetworking.send(player, NbtCrafting.UPDATE_ADVANCED_RECIPES_PACKET_ID, buf);
List<PacketByteBuf> packets = NbtCrafting.createAdvancedRecipeSyncPackets(server.getRecipeManager());
for (PacketByteBuf packet : packets) {
ServerPlayNetworking.send(player, NbtCrafting.UPDATE_ADVANCED_RECIPES_PACKET_ID, packet);
}
} else {
NbtCrafting.logInfo("Skipping advanced recipe data synchronization for vanillish player " + player.getEntityName());
}
Expand All @@ -80,10 +82,11 @@ public void onDataPacksReloaded(CallbackInfo ci) {
List<ServerPlayerEntity> nbtcPlayers = players.stream().filter(NbtCrafting::hasClientMod).collect(Collectors.toList());
if (!nbtcPlayers.isEmpty()) {
NbtCrafting.logInfo("Syncing advanced recipe data to " + nbtcPlayers.size() + " players");
PacketByteBuf buf = NbtCrafting.createAdvancedRecipeSyncPacket(server.getRecipeManager());
for (ServerPlayerEntity player : nbtcPlayers) {
NbtCrafting.logInfo("Syncing to player " + player.getEntityName());
ServerPlayNetworking.send(player, NbtCrafting.UPDATE_ADVANCED_RECIPES_PACKET_ID, buf);
List<PacketByteBuf> packets = NbtCrafting.createAdvancedRecipeSyncPackets(server.getRecipeManager());
for (PacketByteBuf packet : packets) {
for (ServerPlayerEntity player : nbtcPlayers) {
ServerPlayNetworking.send(player, NbtCrafting.UPDATE_ADVANCED_RECIPES_PACKET_ID, packet);
}
}
} else {
NbtCrafting.logInfo("No advanced recipe data needs to be synced!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ public ItemStack[] craftAll(Inventory inv) {
public DefaultedList<ItemStack> getRemainder(Inventory inv) {
DefaultedList<ItemStack> stacks = DefaultedList.ofSize(4, ItemStack.EMPTY);
Map<String, Object> reference = new HashMap<>();
reference.put("ingredient", inv.getStack(3));
reference.put("ingredient", NbtUtil.getTagOrEmpty(inv.getStack(3)));
stacks.set(3, RecipeUtil.getRemainder(inv.getStack(3), ingredient, reference));

for (int i = 0; i < 3; i++) {
if (base.test(inv.getStack(i))) {
reference.put("base", inv.getStack(i));
reference.put("base", NbtUtil.getTagOrEmpty(inv.getStack(i)));
stacks.set(i, RecipeUtil.getRemainder(inv.getStack(i), base, reference));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"type": "nbtcrafting:brewing",
"base": {
"potion": "minecraft:awkward"
},
"ingredient": {
"item": "minecraft:pufferfish_bucket",
"remainder": {
"item": "minecraft:pufferfish_bucket",
"data": {
"$": "ingredient"
}
}
},
"result": {
"potion": "minecraft:water_breathing"
}
}
6 changes: 6 additions & 0 deletions src/testmod/resources/pack.mcmeta
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"pack": {
"pack_format": 6,
"description": "Nbt Crafting test recipes"
}
}

0 comments on commit 8c592a5

Please sign in to comment.