|
| 1 | +package com.tomboshoven.minecraft.magicmirror.blocks.tileentities.modifiers; |
| 2 | + |
| 3 | +import com.google.common.collect.Lists; |
| 4 | +import com.tomboshoven.minecraft.magicmirror.blocks.modifiers.MagicMirrorModifier; |
| 5 | +import com.tomboshoven.minecraft.magicmirror.blocks.tileentities.MagicMirrorBaseTileEntity; |
| 6 | +import com.tomboshoven.minecraft.magicmirror.reflection.Reflection; |
| 7 | +import com.tomboshoven.minecraft.magicmirror.reflection.modifiers.BannerReflectionModifier; |
| 8 | +import com.tomboshoven.minecraft.magicmirror.reflection.modifiers.BannerReflectionModifierClient; |
| 9 | +import mcp.MethodsReturnNonnullByDefault; |
| 10 | +import net.minecraft.block.BannerBlock; |
| 11 | +import net.minecraft.entity.player.PlayerEntity; |
| 12 | +import net.minecraft.inventory.InventoryHelper; |
| 13 | +import net.minecraft.item.DyeColor; |
| 14 | +import net.minecraft.item.ItemStack; |
| 15 | +import net.minecraft.nbt.CompoundNBT; |
| 16 | +import net.minecraft.nbt.ListNBT; |
| 17 | +import net.minecraft.tileentity.BannerPattern; |
| 18 | +import net.minecraft.util.Hand; |
| 19 | +import net.minecraft.util.math.BlockPos; |
| 20 | +import net.minecraft.util.text.ITextComponent; |
| 21 | +import net.minecraft.world.World; |
| 22 | +import net.minecraftforge.fml.DistExecutor; |
| 23 | +import org.apache.commons.lang3.tuple.Pair; |
| 24 | + |
| 25 | +import javax.annotation.Nullable; |
| 26 | +import javax.annotation.ParametersAreNonnullByDefault; |
| 27 | +import java.util.List; |
| 28 | + |
| 29 | + |
| 30 | +@ParametersAreNonnullByDefault |
| 31 | +@MethodsReturnNonnullByDefault |
| 32 | +public class BannerMagicMirrorTileEntityModifier extends MagicMirrorTileEntityModifier { |
| 33 | + /** |
| 34 | + * The initial color of the banner (before any patterns are applied) |
| 35 | + */ |
| 36 | + private DyeColor baseColor = DyeColor.BLACK; |
| 37 | + /** |
| 38 | + * The banner NBT tag, stored here for removal. |
| 39 | + */ |
| 40 | + @Nullable |
| 41 | + private CompoundNBT bannerNBT; |
| 42 | + /** |
| 43 | + * The banner name. |
| 44 | + */ |
| 45 | + @Nullable |
| 46 | + private ITextComponent name; |
| 47 | + /** |
| 48 | + * The object that modifies the reflection in the mirror to show the banner in the background. |
| 49 | + */ |
| 50 | + @Nullable |
| 51 | + private BannerReflectionModifier reflectionModifier; |
| 52 | + |
| 53 | + public BannerMagicMirrorTileEntityModifier(MagicMirrorModifier modifier) { |
| 54 | + super(modifier); |
| 55 | + } |
| 56 | + |
| 57 | + public BannerMagicMirrorTileEntityModifier(MagicMirrorModifier modifier, DyeColor baseColor, @Nullable CompoundNBT bannerNBT, @Nullable ITextComponent name) { |
| 58 | + super(modifier); |
| 59 | + this.baseColor = baseColor; |
| 60 | + this.bannerNBT = bannerNBT != null ? bannerNBT.copy() : null; |
| 61 | + this.name = name; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public void remove(World world, BlockPos pos) { |
| 66 | + ItemStack itemStack = new ItemStack(BannerBlock.forColor(baseColor)); |
| 67 | + if (bannerNBT != null) { |
| 68 | + itemStack.getOrCreateTag().put("BlockEntityTag", bannerNBT); |
| 69 | + } |
| 70 | + if (name != null) { |
| 71 | + itemStack.setDisplayName(name); |
| 72 | + } |
| 73 | + InventoryHelper.spawnItemStack(world, pos.getX(), pos.getY(), pos.getZ(), itemStack); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public CompoundNBT write(CompoundNBT nbt) { |
| 78 | + super.write(nbt); |
| 79 | + nbt.putInt("BannerColor", baseColor.getId()); |
| 80 | + if (bannerNBT != null) { |
| 81 | + nbt.put("BannerData", bannerNBT.copy()); |
| 82 | + } |
| 83 | + nbt.putString("BannerName", ITextComponent.Serializer.toJson(name)); |
| 84 | + return nbt; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public void read(CompoundNBT nbt) { |
| 89 | + super.read(nbt); |
| 90 | + baseColor = DyeColor.byId(nbt.getInt("BannerColor")); |
| 91 | + CompoundNBT bannerData = nbt.getCompound("BannerData"); |
| 92 | + if (!bannerData.isEmpty()) { |
| 93 | + bannerNBT = bannerData.copy(); |
| 94 | + } |
| 95 | + name = ITextComponent.Serializer.fromJson(nbt.getString("BannerName")); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public void activate(MagicMirrorBaseTileEntity tileEntity) { |
| 100 | + Reflection reflection = tileEntity.getReflection(); |
| 101 | + if (reflection != null) { |
| 102 | + List<Pair<BannerPattern, DyeColor>> patternList = Lists.newArrayList(); |
| 103 | + patternList.add(Pair.of(BannerPattern.BASE, baseColor)); |
| 104 | + if (bannerNBT != null) { |
| 105 | + // Get the patterns from the NBT data |
| 106 | + ListNBT patterns = bannerNBT.getList("Patterns", 10); |
| 107 | + int size = patterns.size(); |
| 108 | + for (int i = 0; i < size; ++i) { |
| 109 | + CompoundNBT pattern = patterns.getCompound(i); |
| 110 | + BannerPattern bannerPattern = BannerPattern.byHash(pattern.getString("Pattern")); |
| 111 | + if (bannerPattern != null) { |
| 112 | + DyeColor bannerPatternColor = DyeColor.byId(pattern.getInt("Color")); |
| 113 | + patternList.add(Pair.of(bannerPattern, bannerPatternColor)); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + reflectionModifier = createReflectionModifier(patternList); |
| 119 | + |
| 120 | + reflection.addModifier(reflectionModifier); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + private static BannerReflectionModifier createReflectionModifier(List<? extends Pair<BannerPattern, DyeColor>> patternList) { |
| 125 | + return DistExecutor.runForDist( |
| 126 | + () -> () -> new BannerReflectionModifierClient(patternList), |
| 127 | + () -> () -> new BannerReflectionModifier(patternList) |
| 128 | + ); |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public void deactivate(MagicMirrorBaseTileEntity tileEntity) { |
| 133 | + if (reflectionModifier != null) { |
| 134 | + Reflection reflection = tileEntity.getReflection(); |
| 135 | + if (reflection != null) { |
| 136 | + reflection.removeModifier(reflectionModifier); |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public boolean tryPlayerActivate(MagicMirrorBaseTileEntity tileEntity, PlayerEntity playerIn, Hand hand) { |
| 143 | + // No activation behavior |
| 144 | + return false; |
| 145 | + } |
| 146 | +} |
0 commit comments