/
ShulkerBoxUpgradeItem.java
170 lines (133 loc) · 6.54 KB
/
ShulkerBoxUpgradeItem.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
package com.progwml6.ironshulkerbox.common.items;
import com.progwml6.ironshulkerbox.common.block.GenericIronShulkerBlock;
import com.progwml6.ironshulkerbox.common.block.IronShulkerBoxesTypes;
import com.progwml6.ironshulkerbox.common.block.tileentity.GenericIronShulkerBoxTileEntity;
import net.minecraft.block.BlockState;
import net.minecraft.block.ShulkerBoxBlock;
import net.minecraft.client.resources.I18n;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.DyeColor;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUseContext;
import net.minecraft.tileentity.ShulkerBoxTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.Direction;
import net.minecraft.util.NonNullList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.World;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import javax.annotation.Nullable;
import java.util.List;
public class ShulkerBoxUpgradeItem extends Item {
private final IronShulkerBoxesUpgradeType type;
public ShulkerBoxUpgradeItem(IronShulkerBoxesUpgradeType type, Properties properties) {
super(properties);
this.type = type;
}
@Override
@OnlyIn(Dist.CLIENT)
public void addInformation(ItemStack stack, @Nullable World worldIn, List<ITextComponent> tooltip, ITooltipFlag flagIn) {
if (I18n.hasKey("ironshulkerbox.upgrade.tooltip")) {
if (I18n.hasKey("ironshulkerbox." + this.type.source.name) && I18n.hasKey("ironshulkerbox." + this.type.target.name)) {
tooltip.add((new TranslationTextComponent("ironshulkerbox.upgrade.tooltip", new TranslationTextComponent("ironshulkerbox." + this.type.source.name).mergeStyle(TextFormatting.BOLD), new TranslationTextComponent("ironshulkerbox." + this.type.target.name).mergeStyle(TextFormatting.BOLD))).mergeStyle(TextFormatting.DARK_RED));
}
}
if (I18n.hasKey("ironshulkerbox.color.tooltip")) {
tooltip.add((new TranslationTextComponent("ironshulkerbox.color.tooltip", 0)).mergeStyle(TextFormatting.GOLD));
}
super.addInformation(stack, worldIn, tooltip, flagIn);
}
@Override
public ActionResultType onItemUseFirst(ItemStack stack, ItemUseContext context) {
PlayerEntity entityPlayer = context.getPlayer();
BlockPos blockPos = context.getPos();
World world = context.getWorld();
ItemStack itemStack = context.getItem();
if (world.isRemote) {
return ActionResultType.PASS;
}
if (this.type.canUpgrade(IronShulkerBoxesTypes.VANILLA)) {
if (!(world.getBlockState(blockPos).getBlock() instanceof ShulkerBoxBlock)) {
return ActionResultType.PASS;
}
} else {
if (!(world.getBlockState(blockPos).getBlock() instanceof GenericIronShulkerBlock)) {
return ActionResultType.PASS;
} else {
GenericIronShulkerBlock block = (GenericIronShulkerBlock) world.getBlockState(blockPos).getBlock();
if (block.getDefaultState() != IronShulkerBoxesTypes.get(this.type.source, block.getColor())) {
return ActionResultType.PASS;
}
}
}
TileEntity tileEntity = world.getTileEntity(blockPos);
GenericIronShulkerBoxTileEntity newShulkerBox = null;
ITextComponent customName = null;
NonNullList<ItemStack> shulkerBoxContents = NonNullList.<ItemStack>withSize(27, ItemStack.EMPTY);
Direction shulkerBoxFacing = Direction.UP;
DyeColor shulkerBoxColor = DyeColor.PURPLE;
if (tileEntity != null) {
if (tileEntity instanceof GenericIronShulkerBoxTileEntity) {
GenericIronShulkerBoxTileEntity shulkerBox = (GenericIronShulkerBoxTileEntity) tileEntity;
BlockState shulkerBoxState = world.getBlockState(blockPos);
shulkerBoxContents = NonNullList.<ItemStack>withSize(shulkerBox.getSizeInventory(), ItemStack.EMPTY);
for (int i = 0; i < shulkerBoxContents.size(); i++) {
shulkerBoxContents.set(i, shulkerBox.getStackInSlot(i));
}
shulkerBoxFacing = shulkerBoxState.get(ShulkerBoxBlock.FACING);
customName = shulkerBox.getCustomName();
shulkerBoxColor = shulkerBox.getColor();
newShulkerBox = this.type.target.makeEntity(shulkerBoxColor);
shulkerBox.clear();
//shulkerBox.setDestroyedByCreativePlayer(true);
if (newShulkerBox == null) {
return ActionResultType.PASS;
}
} else if (tileEntity instanceof ShulkerBoxTileEntity) {
BlockState shulkerBoxState = world.getBlockState(blockPos);
shulkerBoxFacing = shulkerBoxState.get(net.minecraft.block.ShulkerBoxBlock.FACING);
ShulkerBoxTileEntity shulkerBox = (ShulkerBoxTileEntity) tileEntity;
if (!this.type.canUpgrade(IronShulkerBoxesTypes.VANILLA)) {
return ActionResultType.PASS;
}
shulkerBoxContents = NonNullList.<ItemStack>withSize(shulkerBox.getSizeInventory(), ItemStack.EMPTY);
for (int i = 0; i < shulkerBoxContents.size(); i++) {
shulkerBoxContents.set(i, shulkerBox.getStackInSlot(i));
}
shulkerBoxColor = ((net.minecraft.block.ShulkerBoxBlock) world.getBlockState(blockPos).getBlock()).getColor();
if (shulkerBoxColor == null) {
shulkerBoxColor = DyeColor.PURPLE;
}
customName = shulkerBox.getCustomName();
shulkerBox.clear();
//shulkerBox.setDestroyedByCreativePlayer(true);
newShulkerBox = this.type.target.makeEntity(shulkerBoxColor);
}
}
tileEntity.updateContainingBlockInfo();
world.removeBlock(blockPos, false);
world.removeTileEntity(blockPos);
BlockState iBlockState = IronShulkerBoxesTypes.get(this.type.target, shulkerBoxColor).with(ShulkerBoxBlock.FACING, shulkerBoxFacing);
world.setTileEntity(blockPos, newShulkerBox);
world.setBlockState(blockPos, iBlockState, 3);
world.notifyBlockUpdate(blockPos, iBlockState, iBlockState, 3);
TileEntity tileEntity2 = world.getTileEntity(blockPos);
if (tileEntity2 instanceof GenericIronShulkerBoxTileEntity) {
if (customName != null) {
((GenericIronShulkerBoxTileEntity) tileEntity2).setCustomName(customName);
}
((GenericIronShulkerBoxTileEntity) tileEntity2).setItems(shulkerBoxContents);
}
if (!entityPlayer.abilities.isCreativeMode) {
itemStack.shrink(1);
}
return ActionResultType.SUCCESS;
}
}