-
Notifications
You must be signed in to change notification settings - Fork 10
/
IronShulkerBoxUpgradeItem.java
174 lines (137 loc) · 6.65 KB
/
IronShulkerBoxUpgradeItem.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
174
package com.progwml6.ironshulkerbox.common.item;
import com.progwml6.ironshulkerbox.common.block.AbstractIronShulkerBoxBlock;
import com.progwml6.ironshulkerbox.common.block.IronShulkerBoxesTypes;
import com.progwml6.ironshulkerbox.common.block.entity.AbstractIronShulkerBoxBlockEntity;
import com.progwml6.ironshulkerbox.common.registraton.IronShulkerBoxesBlocks;
import net.minecraft.ChatFormatting;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.NonNullList;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.DyeColor;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.ShulkerBoxBlock;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.ShulkerBoxBlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import javax.annotation.Nullable;
import java.util.List;
public class IronShulkerBoxUpgradeItem extends Item {
private final IronShulkerBoxesUpgradeType type;
public IronShulkerBoxUpgradeItem(IronShulkerBoxesUpgradeType type, Properties properties) {
super(properties);
this.type = type;
}
@Override
public InteractionResult onItemUseFirst(ItemStack stack, UseOnContext context) {
Player entityPlayer = context.getPlayer();
BlockPos blockPos = context.getClickedPos();
Level world = context.getLevel();
ItemStack itemStack = context.getItemInHand();
boolean passed = false;
if (world.isClientSide) {
return InteractionResult.PASS;
}
if (entityPlayer == null) {
return InteractionResult.PASS;
}
if (this.type.canUpgrade(IronShulkerBoxesTypes.VANILLA)) {
if (world.getBlockState(blockPos).getBlock() instanceof ShulkerBoxBlock) {
passed = true;
}
} else {
if (!(world.getBlockState(blockPos).getBlock() instanceof AbstractIronShulkerBoxBlock block)) {
passed = true;
} else {
if (block.defaultBlockState() == IronShulkerBoxesTypes.get(this.type.source, block.getColor()).defaultBlockState()) {
passed = true;
}
}
}
if (!passed) {
return InteractionResult.PASS;
}
BlockEntity blockEntity = world.getBlockEntity(blockPos);
if (this.type.canUpgrade(IronShulkerBoxesTypes.VANILLA)) {
if (!(blockEntity instanceof ShulkerBoxBlockEntity)) {
return InteractionResult.PASS;
}
}
AbstractIronShulkerBoxBlockEntity newShulkerBox = null;
Component customName = null;
NonNullList<ItemStack> shulkerBoxContents = NonNullList.withSize(27, ItemStack.EMPTY);
Direction shulkerBoxFacing;
DyeColor shulkerBoxColor;
BlockState iBlockState = IronShulkerBoxesBlocks.COPPER_SHULKER_BOX.get().defaultBlockState();
if (blockEntity != null) {
if (blockEntity instanceof AbstractIronShulkerBoxBlockEntity shulkerBox) {
BlockState shulkerBoxState = world.getBlockState(blockPos);
if (AbstractIronShulkerBoxBlockEntity.getOpenCount(world, blockPos) > 0) {
return InteractionResult.PASS;
}
if (!shulkerBox.canOpen(entityPlayer)) {
return InteractionResult.PASS;
}
shulkerBoxContents = shulkerBox.getItems();
shulkerBoxFacing = shulkerBoxState.getValue(AbstractIronShulkerBoxBlock.FACING);
customName = shulkerBox.getCustomName();
shulkerBoxColor = shulkerBox.getColor();
iBlockState = IronShulkerBoxesTypes.get(this.type.target, shulkerBoxColor).defaultBlockState();
iBlockState = iBlockState.setValue(AbstractIronShulkerBoxBlock.FACING, shulkerBoxFacing);
newShulkerBox = this.type.target.makeEntity(blockPos, iBlockState, shulkerBoxColor);
} else if (blockEntity instanceof ShulkerBoxBlockEntity shulkerBox) {
BlockState shulkerBoxState = world.getBlockState(blockPos);
if (!shulkerBox.canOpen(entityPlayer)) {
return InteractionResult.PASS;
}
if (!this.type.canUpgrade(IronShulkerBoxesTypes.VANILLA)) {
return InteractionResult.PASS;
}
shulkerBoxContents = NonNullList.withSize(shulkerBox.getContainerSize(), ItemStack.EMPTY);
for (int slot = 0; slot < shulkerBoxContents.size(); slot++) {
shulkerBoxContents.set(slot, shulkerBox.getItem(slot));
}
shulkerBoxFacing = shulkerBoxState.getValue(ShulkerBoxBlock.FACING);
customName = shulkerBox.getCustomName();
shulkerBoxColor = ((ShulkerBoxBlock) shulkerBoxState.getBlock()).getColor();
iBlockState = IronShulkerBoxesTypes.get(this.type.target, shulkerBoxColor).defaultBlockState();
iBlockState = iBlockState.setValue(AbstractIronShulkerBoxBlock.FACING, shulkerBoxFacing);
shulkerBox.clearContent();
newShulkerBox = this.type.target.makeEntity(blockPos, iBlockState, shulkerBoxColor);
}
}
if (newShulkerBox == null) {
return InteractionResult.PASS;
}
world.removeBlockEntity(blockPos);
world.removeBlock(blockPos, false);
world.setBlock(blockPos, iBlockState, 3);
world.setBlockEntity(newShulkerBox);
world.sendBlockUpdated(blockPos, iBlockState, iBlockState, 3);
BlockEntity tileEntity2 = world.getBlockEntity(blockPos);
if (tileEntity2 instanceof AbstractIronShulkerBoxBlockEntity) {
if (customName != null) {
((AbstractIronShulkerBoxBlockEntity) tileEntity2).setCustomName(customName);
}
((AbstractIronShulkerBoxBlockEntity) tileEntity2).setItems(shulkerBoxContents);
}
if (!entityPlayer.getAbilities().instabuild) {
itemStack.shrink(1);
}
return InteractionResult.SUCCESS;
}
@Override
public void appendHoverText(ItemStack pStack, @Nullable Level pLevel, List<Component> pTooltipComponents, TooltipFlag pIsAdvanced) {
Component SOURCE = Component.translatable("ironshulkerbox." + this.type.source.getEnglishName().toLowerCase()).withStyle(ChatFormatting.BOLD);
Component TARGET = Component.translatable("ironshulkerbox." + this.type.target.getEnglishName().toLowerCase()).withStyle(ChatFormatting.BOLD);
pTooltipComponents.add(Component.translatable("item.ironshulkerbox.shulker_box_upgrade.upgrade", SOURCE, TARGET).withStyle(ChatFormatting.DARK_RED));
pTooltipComponents.add(Component.translatable("item.ironshulkerbox.shulker_box_upgrade.color").withStyle(ChatFormatting.GOLD));
super.appendHoverText(pStack, pLevel, pTooltipComponents, pIsAdvanced);
}
}