3
3
import com .github .elenterius .biomancy .BiomancyMod ;
4
4
import com .github .elenterius .biomancy .init .ModBlockEntities ;
5
5
import com .github .elenterius .biomancy .init .ModCapabilities ;
6
- import com .github .elenterius .biomancy .inventory .BehavioralInventory ;
7
- import com .github .elenterius .biomancy .inventory .itemhandler .HandlerBehaviors ;
6
+ import com .github .elenterius .biomancy .inventory .InventoryHandler ;
7
+ import com .github .elenterius .biomancy .inventory .InventoryHandlers ;
8
+ import com .github .elenterius .biomancy .inventory .ItemHandlerUtil ;
8
9
import com .github .elenterius .biomancy .menu .BioForgeMenu ;
9
10
import com .github .elenterius .biomancy .styles .TextComponentUtil ;
11
+ import com .github .elenterius .biomancy .util .PlayerInteractionPredicate ;
10
12
import com .github .elenterius .biomancy .util .fuel .FluidFuelConsumerHandler ;
11
13
import com .github .elenterius .biomancy .util .fuel .FuelHandler ;
12
14
import com .github .elenterius .biomancy .util .fuel .IFuelHandler ;
16
18
import net .minecraft .network .chat .Component ;
17
19
import net .minecraft .server .level .ServerLevel ;
18
20
import net .minecraft .util .Mth ;
19
- import net .minecraft .world .Containers ;
20
21
import net .minecraft .world .MenuProvider ;
21
22
import net .minecraft .world .Nameable ;
22
23
import net .minecraft .world .entity .player .Inventory ;
23
24
import net .minecraft .world .entity .player .Player ;
24
25
import net .minecraft .world .inventory .AbstractContainerMenu ;
25
- import net .minecraft .world .inventory .ContainerLevelAccess ;
26
26
import net .minecraft .world .item .ItemStack ;
27
27
import net .minecraft .world .level .Level ;
28
28
import net .minecraft .world .level .block .entity .BlockEntity ;
45
45
import javax .annotation .Nonnull ;
46
46
import java .util .Objects ;
47
47
48
- public class BioForgeBlockEntity extends BlockEntity implements MenuProvider , Nameable , GeoBlockEntity {
48
+ public class BioForgeBlockEntity extends BlockEntity implements MenuProvider , PlayerInteractionPredicate , Nameable , GeoBlockEntity {
49
49
50
50
public static final int FUEL_SLOTS = 1 ;
51
51
public static final int MAX_FUEL = 1_000 ;
52
52
static final int OPENERS_CHANGE_EVENT = 1 ;
53
+
53
54
protected final int tickOffset = BiomancyMod .GLOBAL_RANDOM .nextInt (20 );
55
+
54
56
private final BioForgeStateData stateData ;
55
57
private final FuelHandler fuelHandler ;
56
- private final BehavioralInventory <?> fuelInventory ;
58
+ private final InventoryHandler fuelInventory ;
57
59
58
60
private final ContainerOpenersCounter openersCounter = new ContainerOpenersCounter () {
59
61
@@ -82,7 +84,9 @@ protected boolean isOwnContainer(Player player) {
82
84
return false ;
83
85
}
84
86
};
87
+
85
88
private final AnimatableInstanceCache cache = GeckoLibUtil .createInstanceCache (this );
89
+
86
90
protected int ticks = tickOffset ;
87
91
private boolean playWorkingAnimation = false ;
88
92
private int nearbyTimer = -10 ;
@@ -91,9 +95,7 @@ protected boolean isOwnContainer(Player player) {
91
95
92
96
public BioForgeBlockEntity (BlockPos worldPosition , BlockState blockState ) {
93
97
super (ModBlockEntities .BIO_FORGE .get (), worldPosition , blockState );
94
- fuelInventory = BehavioralInventory .createServerContents (FUEL_SLOTS , HandlerBehaviors ::filterFuel , this ::canPlayerOpenInv , this ::setChanged );
95
- fuelInventory .setOpenInventoryConsumer (this ::startOpen );
96
- fuelInventory .setCloseInventoryConsumer (this ::stopOpen );
98
+ fuelInventory = InventoryHandlers .filterFuel (FUEL_SLOTS , this ::onInventoryChanged );
97
99
98
100
fuelHandler = FuelHandler .createNutrientFuelHandler (MAX_FUEL , this ::setChanged );
99
101
stateData = new BioForgeStateData (fuelHandler );
@@ -117,17 +119,22 @@ public boolean triggerEvent(int id, int type) {
117
119
return super .triggerEvent (id , type );
118
120
}
119
121
120
- public boolean canPlayerOpenInv (Player player ) {
122
+ @ Override
123
+ public boolean canPlayerInteract (Player player ) {
121
124
if (level == null || level .getBlockEntity (worldPosition ) != this ) return false ;
122
125
return player .distanceToSqr (Vec3 .atCenterOf (worldPosition )) < 8d * 8d ;
123
126
}
124
127
125
- private void startOpen (Player player ) {
128
+ protected void onInventoryChanged () {
129
+ if (level != null && !level .isClientSide ) setChanged ();
130
+ }
131
+
132
+ public void startOpen (Player player ) {
126
133
if (remove || player .isSpectator ()) return ;
127
134
openersCounter .incrementOpeners (player , Objects .requireNonNull (getLevel ()), getBlockPos (), getBlockState ());
128
135
}
129
136
130
- private void stopOpen (Player player ) {
137
+ public void stopOpen (Player player ) {
131
138
if (remove || player .isSpectator ()) return ;
132
139
openersCounter .decrementOpeners (player , Objects .requireNonNull (getLevel ()), getBlockPos (), getBlockState ());
133
140
}
@@ -140,7 +147,11 @@ public void recheckOpen() {
140
147
@ Nullable
141
148
@ Override
142
149
public AbstractContainerMenu createMenu (int containerId , Inventory playerInventory , Player player ) {
143
- return BioForgeMenu .createServerMenu (containerId , playerInventory , fuelInventory , stateData , ContainerLevelAccess .create (level , getBlockPos ()));
150
+ return BioForgeMenu .createServerMenu (containerId , playerInventory , this );
151
+ }
152
+
153
+ public InventoryHandler getFuelInventory () {
154
+ return fuelInventory ;
144
155
}
145
156
146
157
public BioForgeStateData getStateData () {
@@ -176,11 +187,11 @@ public int getMaxFuelAmount() {
176
187
}
177
188
178
189
public ItemStack getStackInFuelSlot () {
179
- return fuelInventory .getItem (0 );
190
+ return fuelInventory .getStackInSlot (0 );
180
191
}
181
192
182
193
public void setStackInFuelSlot (ItemStack stack ) {
183
- fuelInventory .setItem (0 , stack );
194
+ fuelInventory .setStackInSlot (0 , stack );
184
195
}
185
196
186
197
@ Override
@@ -198,7 +209,7 @@ public void load(CompoundTag tag) {
198
209
}
199
210
200
211
public void dropAllInvContents (Level level , BlockPos pos ) {
201
- Containers .dropContents (level , pos , fuelInventory );
212
+ ItemHandlerUtil .dropContents (level , pos , fuelInventory );
202
213
}
203
214
204
215
@ Nonnull
@@ -207,7 +218,7 @@ public <T> LazyOptional<T> getCapability(Capability<T> cap, @Nullable Direction
207
218
if (remove ) return super .getCapability (cap , side );
208
219
209
220
if (cap == ModCapabilities .ITEM_HANDLER && side != null && side .getAxis ().isHorizontal ()) {
210
- return fuelInventory .getOptionalItemHandler ().cast ();
221
+ return fuelInventory .getLazyOptional ().cast ();
211
222
}
212
223
213
224
if (cap == ModCapabilities .FLUID_HANDLER ) {
0 commit comments