-
Notifications
You must be signed in to change notification settings - Fork 756
/
TinkerStationContainerWrapper.java
146 lines (129 loc) · 4.41 KB
/
TinkerStationContainerWrapper.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
package slimeknights.tconstruct.tables.block.entity.inventory;
import lombok.Setter;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import slimeknights.mantle.recipe.container.ISingleStackContainer;
import slimeknights.tconstruct.library.recipe.TinkerRecipeTypes;
import slimeknights.tconstruct.library.recipe.material.MaterialRecipe;
import slimeknights.tconstruct.library.recipe.tinkerstation.IMutableTinkerStationContainer;
import slimeknights.tconstruct.library.tools.nbt.ToolStack;
import slimeknights.tconstruct.tables.block.entity.table.TinkerStationBlockEntity;
import javax.annotation.Nullable;
import java.util.Optional;
import static slimeknights.tconstruct.tables.block.entity.table.TinkerStationBlockEntity.INPUT_SLOT;
import static slimeknights.tconstruct.tables.block.entity.table.TinkerStationBlockEntity.TINKER_SLOT;
public class TinkerStationContainerWrapper implements IMutableTinkerStationContainer {
private final TinkerStationBlockEntity station;
/** Cache of the material recipes found in each slot */
private MaterialRecipe[] materials;
/** Cache of whether each slot has been searched for a material */
private boolean[] searchedMaterial;
/** Cached tool instance to save lookup effort */
@Nullable
private ToolStack tool;
private MaterialRecipe lastMaterialRecipe;
@Nullable @Setter
private Player player;
/**
* Creates a new wrapper instance for the station
* @param station Station instance
*/
public TinkerStationContainerWrapper(TinkerStationBlockEntity station) {
this.station = station;
int count = station.getInputCount();
this.materials = new MaterialRecipe[count];
this.searchedMaterial = new boolean[count];
}
/**
* Finds a material recipe for the given slot
* @param stack Stack in slot
* @return Material recipe found, or null if missing
*/
@Nullable
private MaterialRecipe findMaterialRecipe(ItemStack stack) {
// must have world
Level world = station.getLevel();
if (world == null) {
return null;
}
// try last recipe
ISingleStackContainer inv = () -> stack;
if (lastMaterialRecipe != null && lastMaterialRecipe.matches(inv, world)) {
return lastMaterialRecipe;
}
// try to find a new recipe
Optional<MaterialRecipe> newRecipe = world.getRecipeManager().getRecipeFor(TinkerRecipeTypes.MATERIAL.get(), inv, world);
if (newRecipe.isPresent()) {
lastMaterialRecipe = newRecipe.get();
return lastMaterialRecipe;
}
// if none found, return null
return null;
}
/**
* Clears the cached inputs
*/
public void refreshInput(int slot) {
if (slot == TINKER_SLOT) {
tool = null;
} else if (slot >= INPUT_SLOT && slot < station.getInputCount() + INPUT_SLOT) {
this.materials[slot - 1] = null;
this.searchedMaterial[slot - 1] = false;
}
}
/** Refreshes the size of this based on the size of the tinker station */
public void resize() {
int count = station.getInputCount();
if (count != materials.length) {
this.materials = new MaterialRecipe[count];
this.searchedMaterial = new boolean[count];
}
}
@Override
public ItemStack getTinkerableStack() {
return this.station.getItem(TINKER_SLOT);
}
@Override
public ToolStack getTinkerable() {
if (tool == null) {
tool = ToolStack.from(getTinkerableStack());
}
return tool;
}
@Override
public ItemStack getInput(int index) {
if (index < 0 || index >= station.getInputCount()) {
return ItemStack.EMPTY;
}
return this.station.getItem(index + TinkerStationBlockEntity.INPUT_SLOT);
}
@Override
public int getInputCount() {
return station.getInputCount();
}
@Nullable
@Override
public MaterialRecipe getInputMaterial(int index) {
if (index < 0 || index >= station.getInputCount()) {
return null;
}
if (!searchedMaterial[index]) {
materials[index] = findMaterialRecipe(getInput(index));
searchedMaterial[index] = true;
}
return materials[index];
}
@Override
public void setInput(int index, ItemStack stack) {
if (index >= 0 && index < station.getInputCount()) {
this.station.setItem(index + TinkerStationBlockEntity.INPUT_SLOT, stack);
}
}
@Override
public void giveItem(ItemStack stack) {
if (player != null) {
player.getInventory().placeItemBackInInventory(stack);
}
}
}