Use a set of blocks for pure daisy recipe sync, closes #3289

This adds a new type of pure daisy input which matches against a set of
blocks. Clients receive the full set of blocks instead of a tag name.
Bumped the network version to account for this change.
This commit is contained in:
Hubry committed 2020-05-15 22:45:48 +02:00
1 parent 1d5bf56f26
commit baee6d2a6f
4 files changed
+83 -7

No files matched your search

@@ -0,0 +1,54 @@
package vazkii.botania.common.crafting;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.network.PacketBuffer;
import net.minecraftforge.registries.ForgeRegistries;
import vazkii.botania.api.recipe.StateIngredient;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
public class StateIngredientBlocks implements StateIngredient {
private final Set<Block> blocks;
public StateIngredientBlocks(Set<Block> blocks) {
this.blocks = blocks;
}
@Override
public boolean test(BlockState state) {
return blocks.contains(state.getBlock());
}
@Override
public JsonObject serialize() {
JsonObject object = new JsonObject();
object.addProperty("type", "blocks");
JsonArray array = new JsonArray();
for (Block block : blocks) {
array.add(block.getRegistryName().toString());
}
object.add("blocks", array);
return object;
}
@Override
public void write(PacketBuffer buffer) {
buffer.writeVarInt(0);
buffer.writeVarInt(blocks.size());
for (Block block : blocks) {
buffer.writeRegistryIdUnsafe(ForgeRegistries.BLOCKS, block);
}
}
@Override
public List<BlockState> getDisplayed() {
return blocks.stream().map(Block::getDefaultState).collect(Collectors.toList());
}
}
@@ -8,6 +8,7 @@
*/
package vazkii.botania.common.crafting;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.mojang.datafixers.Dynamic;
@@ -29,6 +30,11 @@ import net.minecraftforge.registries.ForgeRegistries;
import vazkii.botania.api.recipe.StateIngredient;
import vazkii.botania.common.core.helper.ItemNBTHelper;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class StateIngredientHelper {
public static StateIngredient of(Block block) {
return new StateIngredientBlock(block);
@@ -46,6 +52,10 @@ public class StateIngredientHelper {
return new StateIngredientTag(id);
}
public static StateIngredient of(Set<Block> blocks) {
return new StateIngredientBlocks(blocks);
}
public static StateIngredient deserialize(JsonObject object) {
switch (JSONUtils.getString(object, "type")) {
case "tag":
@@ -54,6 +64,12 @@ public class StateIngredientHelper {
return new StateIngredientBlock(ForgeRegistries.BLOCKS.getValue(new ResourceLocation(JSONUtils.getString(object, "block"))));
case "state":
return new StateIngredientBlockState(readBlockState(object));
case "blocks":
HashSet<Block> blocks = new HashSet<>();
for (JsonElement element : JSONUtils.getJsonArray(object, "blocks")) {
blocks.add(ForgeRegistries.BLOCKS.getValue(new ResourceLocation(element.getAsString())));
}
return new StateIngredientBlocks(blocks);
default:
throw new JsonParseException("Unknown type!");
}
@@ -62,7 +78,13 @@ public class StateIngredientHelper {
public static StateIngredient read(PacketBuffer buffer) {
switch (buffer.readVarInt()) {
case 0:
return new StateIngredientTag(buffer.readResourceLocation());
int count = buffer.readVarInt();
Set<Block> set = new HashSet<>();
for (int i = 0; i < count; i++) {
Block block = buffer.readRegistryIdUnsafe(ForgeRegistries.BLOCKS);
set.add(block);
}
return new StateIngredientBlocks(set);
case 1:
return new StateIngredientBlock(buffer.readRegistryIdUnsafe(ForgeRegistries.BLOCKS));
case 2:
@@ -17,15 +17,16 @@ import net.minecraft.tags.BlockTags;
import net.minecraft.tags.Tag;
import net.minecraft.util.ResourceLocation;
import vazkii.botania.api.recipe.StateIngredient;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
public class StateIngredientTag implements StateIngredient {
public class StateIngredientTag extends StateIngredientBlocks {
private final Tag<Block> tag;
public StateIngredientTag(Tag<Block> tag) {
super(tag.getAllElements() instanceof Set ? (Set<Block>) tag.getAllElements() : new HashSet<>(tag.getAllElements()));
this.tag = tag;
}
@@ -48,8 +49,7 @@ public class StateIngredientTag implements StateIngredient {
@Override
public void write(PacketBuffer buffer) {
buffer.writeVarInt(0);
buffer.writeResourceLocation(tag.getId());
super.write(buffer); // We're sending super's contents instead as tags are sent *after* recipes.
}
@Override
@@ -24,7 +24,7 @@ import net.minecraftforge.fml.network.simple.SimpleChannel;
import vazkii.botania.common.lib.LibMisc;
public final class PacketHandler {
private static final String PROTOCOL = "5";
private static final String PROTOCOL = "6";
public static final SimpleChannel HANDLER = NetworkRegistry.newSimpleChannel(
new ResourceLocation(LibMisc.MOD_ID, "chan"),
() -> PROTOCOL,