forked from cpw/ironchest
-
Notifications
You must be signed in to change notification settings - Fork 87
/
ChestUpgradeItem.java
173 lines (133 loc) · 5.8 KB
/
ChestUpgradeItem.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 com.progwml6.ironchest.common.item;
import com.progwml6.ironchest.common.block.IronChestsBlocks;
import com.progwml6.ironchest.common.block.regular.AbstractIronChestBlock;
import com.progwml6.ironchest.common.block.IronChestsTypes;
import com.progwml6.ironchest.common.block.regular.entity.AbstractIronChestBlockEntity;
import com.progwml6.ironchest.common.block.trapped.entity.AbstractTrappedIronChestBlockEntity;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.TrappedChestBlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.ChestBlock;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.block.entity.ChestBlockEntity;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.InteractionResult;
import net.minecraft.core.Direction;
import net.minecraft.core.NonNullList;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.world.level.Level;
public class ChestUpgradeItem extends Item {
private final IronChestsUpgradeType type;
public ChestUpgradeItem(IronChestsUpgradeType 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(IronChestsTypes.WOOD)) {
if (!(world.getBlockState(blockPos).getBlock() instanceof ChestBlock)) {
return InteractionResult.PASS;
}
} else {
for (Block block : IronChestsTypes.get(this.type.source)) {
if (!passed)
passed = world.getBlockState(blockPos).getBlock().defaultBlockState() == block.defaultBlockState();
}
}
if (!passed) {
return InteractionResult.PASS;
}
BlockEntity tileEntity = world.getBlockEntity(blockPos);
if (this.type.canUpgrade(IronChestsTypes.WOOD)) {
if (!(tileEntity instanceof ChestBlockEntity)) {
return InteractionResult.PASS;
}
}
AbstractIronChestBlockEntity newChest = null;
Component customName = null;
NonNullList<ItemStack> chestContents = NonNullList.withSize(27, ItemStack.EMPTY);
Direction chestFacing = Direction.NORTH;
BlockState iBlockState = IronChestsBlocks.COPPER_CHEST.get().defaultBlockState();
if (tileEntity != null) {
if (tileEntity instanceof AbstractIronChestBlockEntity chest) {
BlockState chestState = world.getBlockState(blockPos);
if (AbstractIronChestBlockEntity.getOpenCount(world, blockPos) > 0) {
return InteractionResult.PASS;
}
if (!chest.canOpen(entityPlayer)) {
return InteractionResult.PASS;
}
boolean trapped = tileEntity instanceof AbstractTrappedIronChestBlockEntity;
if(trapped) {
iBlockState = IronChestsTypes.get(this.type.target).get(1).defaultBlockState();
} else {
iBlockState = IronChestsTypes.get(this.type.target).get(0).defaultBlockState();
}
chestContents = chest.getItems();
chestFacing = chestState.getValue(AbstractIronChestBlock.FACING);
customName = chest.getCustomName();
iBlockState = iBlockState.setValue(AbstractIronChestBlock.FACING, chestFacing);
newChest = this.type.target.makeEntity(blockPos, iBlockState, trapped);
} else if (tileEntity instanceof ChestBlockEntity chest) {
BlockState chestState = world.getBlockState(blockPos);
chestFacing = chestState.getValue(ChestBlock.FACING);
if (ChestBlockEntity.getOpenCount(world, blockPos) > 0) {
return InteractionResult.PASS;
}
if (!chest.canOpen(entityPlayer)) {
return InteractionResult.PASS;
}
if (!this.type.canUpgrade(IronChestsTypes.WOOD)) {
return InteractionResult.PASS;
}
chestContents = NonNullList.withSize(chest.getContainerSize(), ItemStack.EMPTY);
for (int slot = 0; slot < chestContents.size(); slot++) {
chestContents.set(slot, chest.getItem(slot));
}
customName = chest.getCustomName();
boolean trapped = tileEntity instanceof TrappedChestBlockEntity;
if(trapped) {
iBlockState = IronChestsTypes.get(this.type.source).get(1).defaultBlockState();
} else {
iBlockState = IronChestsTypes.get(this.type.source).get(0).defaultBlockState();
}
iBlockState = iBlockState.setValue(AbstractIronChestBlock.FACING, chestFacing);
newChest = this.type.target.makeEntity(blockPos, iBlockState, trapped);
}
}
if (newChest == null) {
return InteractionResult.PASS;
}
world.removeBlockEntity(blockPos);
world.removeBlock(blockPos, false);
world.setBlock(blockPos, iBlockState, 3);
world.setBlockEntity(newChest);
world.sendBlockUpdated(blockPos, iBlockState, iBlockState, 3);
BlockEntity tileEntity2 = world.getBlockEntity(blockPos);
if (tileEntity2 instanceof AbstractIronChestBlockEntity) {
if (customName != null) {
((AbstractIronChestBlockEntity) tileEntity2).setCustomName(customName);
}
((AbstractIronChestBlockEntity) tileEntity2).setItems(chestContents);
}
if (!entityPlayer.getAbilities().instabuild) {
itemStack.shrink(1);
}
return InteractionResult.SUCCESS;
}
}