Skip to content

Commit

Permalink
Refector Texture Block NBT
Browse files Browse the repository at this point in the history
Now stores the block registry path name instead of stack, as we no longer need its metadata
  • Loading branch information
KnightMiner committed Nov 23, 2019
1 parent aa369ec commit 7e4f026
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 96 deletions.
Expand Up @@ -10,6 +10,7 @@
import knightminer.inspirations.library.Util;
import knightminer.inspirations.library.client.ClientUtil;
import knightminer.inspirations.library.util.TextureBlockUtil;
import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.ScreenManager;
import net.minecraft.client.renderer.color.BlockColors;
Expand Down Expand Up @@ -103,8 +104,10 @@ public void registerBlockColors(ColorHandlerEvent.Block event) {
}
TileEntity te = world.getTileEntity(pos);
if(te != null) {
ItemStack stack = ItemStack.read(TextureBlockUtil.getTextureBlock(te));
return ClientUtil.getStackBlockColorsSafe(stack, world, pos, 0);
Block block = TextureBlockUtil.getTextureBlock(te);
if (block != null) {
return ClientUtil.getStackBlockColorsSafe(new ItemStack(block), world, pos, 0);
}
}
return FoliageColors.getDefault();
},
Expand Down Expand Up @@ -152,9 +155,9 @@ public void registerItemColors(ColorHandlerEvent.Item event) {
return -1;
}

ItemStack textureStack = TextureBlockUtil.getStackTexture(stack);
if(!textureStack.isEmpty()) {
return itemColors.getColor(textureStack, 0);
Block block = TextureBlockUtil.getTextureBlock(stack);
if(block != null) {
return itemColors.getColor(new ItemStack(block), 0);
} else {
return FoliageColors.getDefault();
}
Expand Down
Expand Up @@ -97,7 +97,7 @@ public BlockState getStateForPlacement(BlockItemUseContext context) {
@Override
public void onBlockPlacedBy(World world, BlockPos pos, BlockState state, LivingEntity placer, ItemStack stack) {
super.onBlockPlacedBy(world, pos, state, placer, stack);
TextureBlockUtil.placeTextureBlock(world, pos, stack);
TextureBlockUtil.updateTextureBlock(world, pos, stack);
}

@Override
Expand Down Expand Up @@ -331,7 +331,7 @@ public BlockState mirror(BlockState state, Mirror mirror) {

@Override
public ItemStack getPickBlock(BlockState state, RayTraceResult target, IBlockReader world, BlockPos pos, PlayerEntity player) {
return TextureBlockUtil.getBlockItemStack(world, pos, state);
return TextureBlockUtil.getPickBlock(world, pos, state);
}

@Override
Expand Down
Expand Up @@ -11,12 +11,9 @@
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemStack;
import net.minecraft.tags.BlockTags;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockRenderLayer;
import net.minecraft.util.NonNullList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.world.IBlockReader;
Expand Down Expand Up @@ -57,13 +54,6 @@ public boolean isEnabled() {
return Config.enableEnlightenedBush.get();
}

@Override
public void fillItemGroup(ItemGroup group, NonNullList<ItemStack> items) {
if(shouldAddtoItemGroup(group)) {
TextureBlockUtil.addBlocksFromTag(BlockTags.LEAVES, this, items);
}
}

/*
* Properties
*/
Expand Down Expand Up @@ -94,12 +84,12 @@ public boolean canEntitySpawn(BlockState state, @Nonnull IBlockReader worldIn, @
@Override
public void onBlockPlacedBy(World world, BlockPos pos, BlockState state, @Nullable LivingEntity placer, ItemStack stack) {
super.onBlockPlacedBy(world, pos, state, placer, stack);
TextureBlockUtil.placeTextureBlock(world, pos, stack);
TextureBlockUtil.updateTextureBlock(world, pos, stack);
}

@Nonnull
@Override
public ItemStack getPickBlock(BlockState state, RayTraceResult target, IBlockReader world, BlockPos pos, PlayerEntity player) {
return TextureBlockUtil.getBlockItemStack(world, pos, state);
return TextureBlockUtil.getPickBlock(world, pos, state);
}
}
Expand Up @@ -14,7 +14,6 @@
import net.minecraft.inventory.container.Container;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.INBT;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.play.server.SUpdateTileEntityPacket;
import net.minecraft.util.Hand;
Expand Down Expand Up @@ -195,24 +194,7 @@ public SUpdateTileEntityPacket getUpdatePacket() {
@Override
public void onDataPacket(NetworkManager net, SUpdateTileEntityPacket pkt) {
CompoundNBT tag = pkt.getNbtCompound();
INBT texture = tag.get(TextureBlockUtil.TAG_TEXTURE);
if(texture != null) {
getTileData().put(TextureBlockUtil.TAG_TEXTURE, texture);
}
TextureBlockUtil.updateTextureBlock(this, tag);
read(tag);
}

/* NBT */

@Override
public void read(CompoundNBT tags) {
super.read(tags);

// pull the old texture string into the proper location if found
CompoundNBT forgeData = tags.getCompound("ForgeData");
if(forgeData.contains(TextureBlockUtil.TAG_TEXTURE, 8)) {
forgeData.putString("texture_path", forgeData.getString(TextureBlockUtil.TAG_TEXTURE));
forgeData.remove(TextureBlockUtil.TAG_TEXTURE);
}
}
}
Expand Up @@ -3,7 +3,6 @@
import knightminer.inspirations.building.InspirationsBuilding;
import knightminer.inspirations.library.util.TextureBlockUtil;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.INBT;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.play.server.SUpdateTileEntityPacket;
import net.minecraft.tileentity.TileEntity;
Expand Down Expand Up @@ -37,10 +36,7 @@ public SUpdateTileEntityPacket getUpdatePacket() {
@Override
public void onDataPacket(NetworkManager net, SUpdateTileEntityPacket pkt) {
CompoundNBT tag = pkt.getNbtCompound();
INBT texture = tag.get(TextureBlockUtil.TAG_TEXTURE);
if(texture != null) {
getTileData().put(TextureBlockUtil.TAG_TEXTURE, texture);
}
TextureBlockUtil.updateTextureBlock(this, tag);
read(tag);
}
}
Expand Up @@ -5,7 +5,6 @@
import com.google.gson.JsonObject;
import knightminer.inspirations.library.util.TextureBlockUtil;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.storage.loot.LootContext;
Expand All @@ -31,8 +30,7 @@ public FillTexturedBlockLootFunction(ILootCondition[] conditions) {
protected ItemStack doApply(@Nonnull ItemStack stack, @Nonnull LootContext context) {
TileEntity te = context.get(LootParameters.BLOCK_ENTITY);
if (te != null) {
CompoundNBT nbt = TextureBlockUtil.getTextureBlock(te);
stack.getOrCreateTag().put(TextureBlockUtil.TAG_TEXTURE, nbt.copy());
stack = TextureBlockUtil.setStackTexture(stack, TextureBlockUtil.getTextureBlockName(te));
}
return stack;
}
Expand Down
Expand Up @@ -27,9 +27,7 @@ public TextureBlockItem(Block block, BlockItem.Properties props, Tag<Block> texT
@Override
public void fillItemGroup(@Nonnull ItemGroup group, @Nonnull NonNullList<ItemStack> items) {
if (shouldAddtoItemGroup(group) && isInGroup(group)) {
for(Block texture: texTag.getAllElements()) {
items.add(TextureBlockUtil.createTexturedStack(getBlock(), texture));
}
TextureBlockUtil.addBlocksFromTag(texTag, this.getBlock(), items);
}
}

Expand All @@ -40,9 +38,9 @@ public void addInformation(@Nonnull ItemStack stack, @Nullable World worldIn, @N
return;
}

ItemStack texture = TextureBlockUtil.getStackTexture(stack);
if(!texture.isEmpty()) {
tooltip.add(texture.getDisplayName());
Block block = TextureBlockUtil.getTextureBlock(stack);
if(block != null) {
tooltip.add(block.getNameTextComponent());
}
}
}
Expand Up @@ -14,8 +14,8 @@
import net.minecraft.client.renderer.texture.MissingTextureSprite;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.item.Item;
import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ResourceLocation;
Expand Down Expand Up @@ -145,9 +145,8 @@ public static String getTexturePath(TileEntity te) {
String texture = te.getTileData().getString(TAG_TEXTURE_PATH);
if (texture.isEmpty()) {
// load it from saved block
ItemStack stack = ItemStack.read(te.getTileData().getCompound(TextureBlockUtil.TAG_TEXTURE));
if (!stack.isEmpty()) {
Block block = Block.getBlockFromItem(stack.getItem());
Block block = TextureBlockUtil.getTextureBlock(te);
if (block != null) {
texture = ModelHelper.getTextureFromBlockstate(block.getDefaultState()).getName().toString();
te.getTileData().putString(TAG_TEXTURE_PATH, texture);
}
Expand Down
@@ -1,9 +1,6 @@
package knightminer.inspirations.library.recipe;

import javax.annotation.Nonnull;

import com.google.gson.JsonObject;

import knightminer.inspirations.Inspirations;
import knightminer.inspirations.library.util.TagUtil;
import knightminer.inspirations.library.util.TextureBlockUtil;
Expand All @@ -19,6 +16,8 @@
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.common.crafting.CraftingHelper;

import javax.annotation.Nonnull;

public class TextureRecipe extends ShapedRecipe {

public final Ingredient texture; // first one found of these determines the output block used
Expand All @@ -44,9 +43,9 @@ public ItemStack getCraftingResult(CraftingInventory craftMatrix) {
Block outBlock = Block.getBlockFromItem(result.getItem());

// Special case for Enlightened Bushes - grab their underlying texture instead of the bush.
ItemStack recurTexture = TextureBlockUtil.getStackTexture(stack);
if (!recurTexture.isEmpty()) {
stack = recurTexture;
Block recurBlock = TextureBlockUtil.getTextureBlock(stack);
if (recurBlock != null) {
stack = new ItemStack(recurBlock);
}

return TextureBlockUtil.createTexturedStack(outBlock, Block.getBlockFromItem(stack.getItem()));
Expand Down

0 comments on commit 7e4f026

Please sign in to comment.