-
Notifications
You must be signed in to change notification settings - Fork 79
/
ItemMirrorPrint.java
173 lines (144 loc) · 4.52 KB
/
ItemMirrorPrint.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package mod.chiselsandbits.items;
import java.util.ArrayList;
import java.util.List;
import mod.chiselsandbits.chiseledblock.NBTBlobConverter;
import mod.chiselsandbits.chiseledblock.TileEntityBlockChiseled;
import mod.chiselsandbits.chiseledblock.data.VoxelBlob;
import mod.chiselsandbits.core.ChiselsAndBits;
import mod.chiselsandbits.core.ClientSide;
import mod.chiselsandbits.helpers.LocalStrings;
import mod.chiselsandbits.helpers.ModUtil;
import mod.chiselsandbits.interfaces.IPatternItem;
import mod.chiselsandbits.render.helpers.SimpleInstanceCache;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
public class ItemMirrorPrint extends Item implements IPatternItem
{
public ItemMirrorPrint()
{
}
SimpleInstanceCache<ItemStack, List<String>> toolTipCache = new SimpleInstanceCache<ItemStack, List<String>>( null, new ArrayList<String>() );
@Override
@SideOnly( Side.CLIENT )
public void addInformation(
final ItemStack stack,
final World worldIn,
final List<String> tooltip,
final ITooltipFlag advanced )
{
super.addInformation( stack, worldIn, tooltip, advanced );
ChiselsAndBits.getConfig().helpText( LocalStrings.HelpMirrorPrint, tooltip,
ClientSide.instance.getKeyName( Minecraft.getMinecraft().gameSettings.keyBindUseItem ) );
if ( isWritten( stack ) )
{
if ( ClientSide.instance.holdingShift() )
{
if ( toolTipCache.needsUpdate( stack ) )
{
final VoxelBlob blob = ModUtil.getBlobFromStack( stack, null );
toolTipCache.updateCachedValue( blob.listContents( new ArrayList<String>() ) );
}
tooltip.addAll( toolTipCache.getCached() );
}
else
{
tooltip.add( LocalStrings.ShiftDetails.getLocal() );
}
}
}
@Override
public String getUnlocalizedName(
final ItemStack stack )
{
if ( isWritten( stack ) )
{
return super.getUnlocalizedName( stack ) + "_written";
}
return super.getUnlocalizedName( stack );
}
@Override
public EnumActionResult onItemUse(
final EntityPlayer player,
final World world,
final BlockPos pos,
final EnumHand hand,
final EnumFacing side,
final float hitX,
final float hitY,
final float hitZ )
{
final ItemStack stack = player.getHeldItem( hand );
if ( !player.canPlayerEdit( pos, side, stack ) )
{
return EnumActionResult.SUCCESS;
}
if ( !isWritten( stack ) )
{
final NBTTagCompound comp = getCompoundFromBlock( world, pos, player, side );
if ( comp != null )
{
stack.setTagCompound( comp );
return EnumActionResult.SUCCESS;
}
return EnumActionResult.FAIL;
}
return EnumActionResult.FAIL;
}
protected NBTTagCompound getCompoundFromBlock(
final World world,
final BlockPos pos,
final EntityPlayer player,
final EnumFacing face )
{
final TileEntityBlockChiseled te = ModUtil.getChiseledTileEntity( world, pos, false );
if ( te != null )
{
final NBTTagCompound comp = new NBTTagCompound();
te.writeChisleData( comp );
final TileEntityBlockChiseled tmp = new TileEntityBlockChiseled();
tmp.readChisleData( comp );
final VoxelBlob bestBlob = tmp.getBlob();
tmp.setBlob( bestBlob.mirror( face.getAxis() ) );
tmp.writeChisleData( comp );
comp.setByte( ModUtil.NBT_SIDE, (byte) ModUtil.getPlaceFace( player ).ordinal() );
return comp;
}
return null;
}
@Override
public ItemStack getPatternedItem(
final ItemStack stack,
final boolean wantRealItems )
{
if ( !isWritten( stack ) )
{
return null;
}
final NBTTagCompound tag = ModUtil.getTagCompound( stack );
// Detect and provide full blocks if pattern solid full and solid.
final NBTBlobConverter conv = new NBTBlobConverter();
conv.readChisleData( tag, VoxelBlob.VERSION_ANY );
final IBlockState blk = conv.getPrimaryBlockState();
final ItemStack itemstack = new ItemStack( ChiselsAndBits.getBlocks().getConversionWithDefault( blk ), 1 );
itemstack.setTagInfo( ModUtil.NBT_BLOCKENTITYTAG, tag );
return itemstack;
}
@Override
public boolean isWritten(
final ItemStack stack )
{
return stack != null && stack.hasTagCompound();
}
}