com.magistuarmory.block.PaviseBlockEntity Maven / Gradle / Ivy
package com.magistuarmory.block;
import com.google.common.collect.Lists;
import com.magistuarmory.EpicKnights;
import com.mojang.datafixers.util.Pair;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Holder;
import net.minecraft.core.Registry;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.DyeColor;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.ShieldItem;
import net.minecraft.world.level.block.entity.*;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.Nullable;
import java.util.List;
public class PaviseBlockEntity extends BlockEntity
{
String shieldId = "wood_pavese";
@Nullable
private DyeColor baseColor;
@Nullable
private ListTag itemPatterns;
private boolean enchanted = false;
private CompoundTag stackCompound;
@Nullable
private List, DyeColor>> patterns;
public PaviseBlockEntity(BlockPos blockpos, BlockState blockstate)
{
super(ModBlockEntityTypes.PAVISE.get(), blockpos, blockstate);
}
@Nullable
public static ListTag getItemPatterns(ItemStack stack)
{
ListTag listtag = null;
CompoundTag compound = BlockItem.m_186336_(stack);
if (compound != null && compound.m_128425_("Patterns", 9))
listtag = compound.m_128437_("Patterns", 10).m_6426_();
return listtag;
}
public void fromItem(ItemStack stack)
{
this.itemPatterns = getItemPatterns(stack);
this.baseColor = ShieldItem.m_43102_(stack);
this.stackCompound = stack.m_41783_();
this.enchanted = stack.m_41793_();
this.patterns = null;
}
@Override
protected void m_183515_(CompoundTag compound)
{
super.m_183515_(compound);
compound.m_128359_("ShieldId", this.shieldId);
if (this.baseColor != null)
compound.m_128405_("Base", this.baseColor.m_41060_());
compound.m_128379_("Enchanted", this.enchanted);
if (this.stackCompound != null)
compound.m_128365_("ItemStack", this.stackCompound);
if (this.itemPatterns != null)
compound.m_128365_("Patterns", this.itemPatterns);
}
public boolean hasFoil()
{
return this.enchanted;
}
public boolean isPainted()
{
return this.getBaseColor() != null;
}
public void m_142466_(CompoundTag compound)
{
super.m_142466_(compound);
this.shieldId = compound.m_128461_("ShieldId");
if (compound.m_128441_("Base"))
this.baseColor = DyeColor.m_41053_(compound.m_128451_("Base"));
this.stackCompound = compound.m_128469_("ItemStack");
this.enchanted = compound.m_128471_("Enchanted");
this.itemPatterns = compound.m_128437_("Patterns", 10);
this.patterns = null;
}
public ClientboundBlockEntityDataPacket m_58483_()
{
return ClientboundBlockEntityDataPacket.m_195640_(this);
}
public CompoundTag m_5995_()
{
return this.m_187482_();
}
public static int getPatternCount(ItemStack stack)
{
CompoundTag compound = BlockItem.m_186336_(stack);
return compound != null && compound.m_128441_("Patterns") ? compound.m_128437_("Patterns", 10).size() : 0;
}
public List, DyeColor>> getPatterns()
{
if (this.patterns == null)
this.patterns = createPatterns(this.baseColor, this.itemPatterns);
return this.patterns;
}
public static List, DyeColor>> createPatterns(DyeColor color, @Nullable ListTag listtag)
{
List, DyeColor>> list = Lists.newArrayList();
list.add(Pair.of(Registry.f_235736_.m_206081_(BannerPatterns.f_222726_), color));
if (listtag == null)
return list;
for(int i = 0; i < listtag.size(); ++i)
{
CompoundTag compound = listtag.m_128728_(i);
Holder holder = BannerPattern.m_222700_(compound.m_128461_("Pattern"));
if (holder != null)
{
int j = compound.m_128451_("Color");
list.add(Pair.of(holder, DyeColor.m_41053_(j)));
}
}
return list;
}
public static void removeLastPattern(ItemStack stack)
{
CompoundTag compound = BlockItem.m_186336_(stack);
if (compound != null && compound.m_128425_("Patterns", 9))
{
ListTag listtag = compound.m_128437_("Patterns", 10);
if (listtag.isEmpty())
return;
listtag.remove(listtag.size() - 1);
if (listtag.isEmpty())
compound.m_128473_("Patterns");
BlockItem.m_186338_(stack, ModBlockEntityTypes.PAVISE.get(), compound);
}
}
public ItemStack getItem()
{
ItemStack stack = new ItemStack(Registry.f_122827_.m_7745_(new ResourceLocation(EpicKnights.ID, this.shieldId)));
stack.m_41751_(this.stackCompound.m_6426_());
return stack;
}
public DyeColor getBaseColor()
{
return this.baseColor;
}
public String getShieldId()
{
return this.shieldId;
}
}