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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
package gregtech.common.tileentities.automation;
import static gregtech.api.enums.Textures.BlockIcons.AUTOMATION_ITEMDISTRIBUTOR;
import static gregtech.api.enums.Textures.BlockIcons.AUTOMATION_ITEMDISTRIBUTOR_GLOW;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.util.ForgeDirection;
import com.gtnewhorizons.modularui.api.screen.ModularWindow;
import com.gtnewhorizons.modularui.api.screen.UIBuildContext;
import com.gtnewhorizons.modularui.common.widget.DrawableWidget;
import gregtech.api.enums.Textures;
import gregtech.api.gui.modularui.GTUITextures;
import gregtech.api.interfaces.ITexture;
import gregtech.api.interfaces.metatileentity.IMetaTileEntity;
import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
import gregtech.api.metatileentity.implementations.MTEBuffer;
import gregtech.api.render.TextureFactory;
import gregtech.api.util.GTUtility;
public class MTEItemDistributor extends MTEBuffer {
private byte[] itemsPerSide = new byte[6];
private ForgeDirection currentSide = ForgeDirection.DOWN;
private byte currentSideItemCount = 0;
public MTEItemDistributor(int aID, String aName, String aNameRegional, int aTier) {
super(
aID,
aName,
aNameRegional,
aTier,
28,
new String[] { "Distributes Items between different Machine Sides", "Default Items per Machine Side: 0",
"Use Screwdriver to increase/decrease Items per Side" });
}
public MTEItemDistributor(int aID, String aName, String aNameRegional, int aTier, int aInvSlotCount,
String aDescription) {
super(aID, aName, aNameRegional, aTier, aInvSlotCount, aDescription);
}
public MTEItemDistributor(String aName, int aTier, int aInvSlotCount, String aDescription,
ITexture[][][] aTextures) {
super(aName, aTier, aInvSlotCount, aDescription, aTextures);
}
public MTEItemDistributor(String aName, int aTier, int aInvSlotCount, String[] aDescription,
ITexture[][][] aTextures) {
super(aName, aTier, aInvSlotCount, aDescription, aTextures);
}
@Override
public IMetaTileEntity newMetaEntity(IGregTechTileEntity aTileEntity) {
return new MTEItemDistributor(
this.mName,
this.mTier,
this.mInventory.length,
this.mDescriptionArray,
this.mTextures);
}
@Override
public ITexture getOverlayIcon() {
return TextureFactory.of(
TextureFactory.of(AUTOMATION_ITEMDISTRIBUTOR),
TextureFactory.builder()
.addIcon(AUTOMATION_ITEMDISTRIBUTOR_GLOW)
.glow()
.build());
}
@Override
public boolean allowPutStack(IGregTechTileEntity aBaseMetaTileEntity, int aIndex, ForgeDirection side,
ItemStack aStack) {
return side == aBaseMetaTileEntity.getFrontFacing();
}
@Override
public ITexture[] getTexture(IGregTechTileEntity aBaseMetaTileEntity, ForgeDirection side, ForgeDirection aFacing,
int colorIndex, boolean aActive, boolean redstoneLevel) {
if (side == aFacing) {
return mTextures[0][colorIndex + 1];
} else {
return mTextures[1][colorIndex + 1];
}
}
@Override
public ITexture[][][] getTextureSet(ITexture[] aTextures) {
ITexture[][][] returnTextures = new ITexture[2][17][];
ITexture baseIcon = getOverlayIcon(), pipeIcon = TextureFactory.of(Textures.BlockIcons.OVERLAY_PIPE_OUT);
for (int i = 0; i < 17; i++) {
returnTextures[0][i] = new ITexture[] { Textures.BlockIcons.MACHINE_CASINGS[mTier][i], baseIcon };
returnTextures[1][i] = new ITexture[] { Textures.BlockIcons.MACHINE_CASINGS[mTier][i], pipeIcon, baseIcon };
}
return returnTextures;
}
@Override
public boolean isInputFacing(ForgeDirection side) {
return getBaseMetaTileEntity().getFrontFacing() == side || itemsPerSide[side.ordinal()] == 0;
}
@Override
public boolean isOutputFacing(ForgeDirection side) {
return getBaseMetaTileEntity().getFrontFacing() != side && itemsPerSide[side.ordinal()] > 0;
}
@Override
public boolean isValidSlot(int aIndex) {
return aIndex < this.mInventory.length - 1;
}
@Override
public void loadNBTData(NBTTagCompound aNBT) {
super.loadNBTData(aNBT);
itemsPerSide = aNBT.getByteArray("mItemsPerSide");
if (itemsPerSide.length != 6) {
itemsPerSide = new byte[6];
}
currentSide = ForgeDirection.getOrientation(aNBT.getByte("mCurrentSide"));
currentSideItemCount = aNBT.getByte("mCurrentSideItemCount");
}
@Override
protected void moveItems(IGregTechTileEntity aBaseMetaTileEntity, long aTimer) {
int currentSideOrdinal = currentSide.ordinal();
fillStacksIntoFirstSlots();
int movedItems;
TileEntity adjacentTileEntity = aBaseMetaTileEntity.getTileEntityAtSide(currentSide);
int inspectedSides = 0;
while (itemsPerSide[currentSideOrdinal] == 0) {
currentSideOrdinal = ((currentSideOrdinal + 1) % 6);
currentSide = ForgeDirection.getOrientation(currentSideOrdinal);
currentSideItemCount = 0;
adjacentTileEntity = aBaseMetaTileEntity.getTileEntityAtSide(currentSide);
inspectedSides += 1;
if (inspectedSides == 6) {
return;
}
}
movedItems = GTUtility.moveOneItemStack(
aBaseMetaTileEntity,
adjacentTileEntity,
currentSide,
currentSide.getOpposite(),
null,
false,
(byte) 64,
(byte) 1,
(byte) (itemsPerSide[currentSideOrdinal] - currentSideItemCount),
(byte) 1);
currentSideItemCount += movedItems;
if (currentSideItemCount >= itemsPerSide[currentSideOrdinal]) {
currentSideOrdinal = ((currentSideOrdinal + 1) % 6);
currentSide = ForgeDirection.getOrientation(currentSideOrdinal);
currentSideItemCount = 0;
}
if (movedItems > 0 || aBaseMetaTileEntity.hasInventoryBeenModified()) {
mSuccess = 50;
}
fillStacksIntoFirstSlots();
}
@Override
public void onScrewdriverRightClick(ForgeDirection side, EntityPlayer aPlayer, float aX, float aY, float aZ) {
final int ordinalSide = side.ordinal();
// Adjust items per side by 1 or -1, constrained to the cyclic interval [0, 127]
itemsPerSide[ordinalSide] += aPlayer.isSneaking() ? -1 : 1;
itemsPerSide[ordinalSide] = (byte) ((itemsPerSide[ordinalSide] + 128) % 128);
GTUtility.sendChatToPlayer(aPlayer, GTUtility.trans("211", "Items per side: ") + itemsPerSide[ordinalSide]);
}
@Override
public void saveNBTData(NBTTagCompound aNBT) {
super.saveNBTData(aNBT);
aNBT.setByteArray("mItemsPerSide", itemsPerSide);
aNBT.setByte("mCurrentSide", (byte) currentSide.ordinal());
aNBT.setByte("mCurrentSideItemCount", currentSideItemCount);
}
@Override
public void setItemNBT(NBTTagCompound aNBT) {
super.setItemNBT(aNBT);
boolean hasSettings = false;
for (byte i : itemsPerSide) {
if (i != 0) {
hasSettings = true;
break;
}
}
if (hasSettings) aNBT.setByteArray("mItemsPerSide", itemsPerSide);
}
@Override
public void addUIWidgets(ModularWindow.Builder builder, UIBuildContext buildContext) {
super.addUIWidgets(builder, buildContext);
addEmitRedstoneIfFullButton(builder);
addInvertRedstoneButton(builder);
builder.widget(
new DrawableWidget().setDrawable(GTUITextures.PICTURE_ARROW_22_RED.apply(87, true))
.setPos(62, 60)
.setSize(87, 22));
addInventorySlots(builder);
}
}
|