forked from cpw/ironchest
-
Notifications
You must be signed in to change notification settings - Fork 87
/
ICrystalChest.java
153 lines (108 loc) · 4.21 KB
/
ICrystalChest.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
package com.progwml6.ironchest.common.block.entity;
import com.progwml6.ironchest.common.block.IronChestsTypes;
import com.progwml6.ironchest.common.network.InventoryTopStacksSyncPacket;
import com.progwml6.ironchest.common.network.IronChestNetwork;
import net.minecraft.core.BlockPos;
import net.minecraft.core.NonNullList;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
public interface ICrystalChest {
default NonNullList<ItemStack> buildItemStackDataList() {
if (this.getChestType().isTransparent()) {
NonNullList<ItemStack> sortList = NonNullList.<ItemStack>withSize(this.getTopItems().size(), ItemStack.EMPTY);
int pos = 0;
for (ItemStack is : this.getTopItems()) {
if (!is.isEmpty()) {
sortList.set(pos, is);
} else {
sortList.set(pos, ItemStack.EMPTY);
}
pos++;
}
return sortList;
}
return NonNullList.<ItemStack>withSize(this.getTopItems().size(), ItemStack.EMPTY);
}
IronChestsTypes getChestType();
default void sendTopStacksPacket() {
NonNullList<ItemStack> stacks = this.buildItemStackDataList();
if (this.getChestLevel() != null && this.getChestLevel() instanceof ServerLevel && !this.getChestLevel().isClientSide) {
IronChestNetwork.getInstance().sendToClientsAround(new InventoryTopStacksSyncPacket(stacks, this.getChestWorldPosition()), (ServerLevel) this.getChestLevel(), this.getChestWorldPosition());
}
}
NonNullList<ItemStack> getTopItems();
Level getChestLevel();
BlockPos getChestWorldPosition();
void receiveMessageFromServer(NonNullList<ItemStack> topStacks);
default void sortTopStacks() {
if (!this.getChestType().isTransparent() || (this.getChestLevel() != null && this.getChestLevel().isClientSide)) {
return;
}
NonNullList<ItemStack> tempCopy = NonNullList.<ItemStack>withSize(this.getContainerSize(), ItemStack.EMPTY);
boolean hasStuff = false;
int compressedIdx = 0;
mainLoop:
for (int i = 0; i < this.getContainerSize(); i++) {
ItemStack itemStack = this.getItems().get(i);
if (!itemStack.isEmpty()) {
for (int j = 0; j < compressedIdx; j++) {
ItemStack tempCopyStack = tempCopy.get(j);
if (ItemStack.isSameIgnoreDurability(tempCopyStack, itemStack)) {
if (itemStack.getCount() != tempCopyStack.getCount()) {
tempCopyStack.grow(itemStack.getCount());
}
continue mainLoop;
}
}
tempCopy.set(compressedIdx, itemStack.copy());
compressedIdx++;
hasStuff = true;
}
}
if (!hasStuff && this.getHadStuff()) {
this.setHadStuff(false);
for (int i = 0; i < this.getTopItems().size(); i++) {
this.getTopItems().set(i, ItemStack.EMPTY);
}
if (this.getChestLevel() != null) {
BlockState blockState = this.getChestLevel().getBlockState(this.getChestWorldPosition());
this.getChestLevel().sendBlockUpdated(this.getChestWorldPosition(), blockState, blockState, 3);
}
return;
}
this.setHadStuff(true);
tempCopy.sort((stack1, stack2) -> {
if (stack1.isEmpty()) {
return 1;
} else if (stack2.isEmpty()) {
return -1;
} else {
return stack2.getCount() - stack1.getCount();
}
});
int slot = 0;
for (ItemStack itemStack : tempCopy) {
if (!itemStack.isEmpty() && itemStack.getCount() > 0) {
if (slot == this.getTopItems().size()) {
break;
}
this.getTopItems().set(slot, itemStack);
slot++;
}
}
for (int i = slot; i < this.getTopItems().size(); i++) {
this.getTopItems().set(i, ItemStack.EMPTY);
}
if (this.getChestLevel() != null) {
BlockState blockState = this.getChestLevel().getBlockState(this.getChestWorldPosition());
this.getChestLevel().sendBlockUpdated(this.getChestWorldPosition(), blockState, blockState, 3);
}
this.sendTopStacksPacket();
}
NonNullList<ItemStack> getItems();
void setHadStuff(boolean hadStuff);
boolean getHadStuff();
int getContainerSize();
}