oh hey there's already a vanilla helper for NBT blockstates

This commit is contained in:
Meredith Espinosa 2019-07-01 18:55:52 -07:00
parent f674627473
commit 5446755e94
2 changed files with 9 additions and 2 deletions

View File

@ -4,6 +4,7 @@ import io.github.cottonmc.templates.util.BlockStateUtil;
import net.fabricmc.fabric.api.block.entity.BlockEntityClientSerializable; import net.fabricmc.fabric.api.block.entity.BlockEntityClientSerializable;
import net.fabricmc.fabric.api.rendering.data.v1.RenderAttachmentBlockEntity; import net.fabricmc.fabric.api.rendering.data.v1.RenderAttachmentBlockEntity;
import net.fabricmc.fabric.api.server.PlayerStream; import net.fabricmc.fabric.api.server.PlayerStream;
import net.fabricmc.fabric.api.util.NbtType;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks; import net.minecraft.block.Blocks;
@ -11,6 +12,7 @@ import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType; import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.TagHelper;
import net.minecraft.util.math.Direction; import net.minecraft.util.math.Direction;
public abstract class TemplateEntity extends BlockEntity implements BlockEntityClientSerializable, RenderAttachmentBlockEntity { public abstract class TemplateEntity extends BlockEntity implements BlockEntityClientSerializable, RenderAttachmentBlockEntity {
@ -36,7 +38,8 @@ public abstract class TemplateEntity extends BlockEntity implements BlockEntityC
@Override @Override
public void fromTag(CompoundTag tag) { public void fromTag(CompoundTag tag) {
super.fromTag(tag); super.fromTag(tag);
renderedState = BlockStateUtil.fromTag(tag); if (tag.containsKey("BlockState", NbtType.COMPOUND)) renderedState = TagHelper.deserializeBlockState(tag.getCompound("BlockState"));
else renderedState = BlockStateUtil.fromTag(tag);
glowstone = tag.getBoolean("Glowstone"); glowstone = tag.getBoolean("Glowstone");
redstone = tag.getBoolean("Redstone"); redstone = tag.getBoolean("Redstone");
if (world != null && world.isClient) { if (world != null && world.isClient) {
@ -47,7 +50,7 @@ public abstract class TemplateEntity extends BlockEntity implements BlockEntityC
@Override @Override
public CompoundTag toTag(CompoundTag tag) { public CompoundTag toTag(CompoundTag tag) {
super.toTag(tag); super.toTag(tag);
BlockStateUtil.toTag(tag, renderedState); tag.put("BlockState", TagHelper.serializeBlockState(renderedState));
tag.putBoolean("Glowstone", glowstone); tag.putBoolean("Glowstone", glowstone);
tag.putBoolean("Redstone", redstone); tag.putBoolean("Redstone", redstone);
return tag; return tag;

View File

@ -10,6 +10,10 @@ import net.minecraft.util.registry.Registry;
import java.util.Optional; import java.util.Optional;
/**
* use {@link net.minecraft.util.TagHelper}, which I should have been using from the start oops
*/
@Deprecated
public class BlockStateUtil { public class BlockStateUtil {
public static BlockState fromTag(CompoundTag tag) { public static BlockState fromTag(CompoundTag tag) {
Block block = Registry.BLOCK.get(new Identifier(tag.getString("Block"))); Block block = Registry.BLOCK.get(new Identifier(tag.getString("Block")));