@@ -74,28 +74,36 @@ private void scanForMatchingTorchesToFillInternalStorage(ItemStack staff, Player
7474 }
7575
7676 public ItemStack getCurrentTorch (ItemStack stack ) {
77- return getFromHandler (stack , handler -> getCurrentTorchIndex (stack ) < handler .getSlots () ? handler . getStackInSlot ( getCurrentTorchIndex ( stack )) : ItemStack . EMPTY );
77+ return getFromHandler (stack , handler -> handler . getStackInSlot ( getCurrentTorchIndex (stack , handler .getSlots ())) );
7878 }
7979
8080 public int getTorchCount (ItemStack stack ) {
81- return getFromHandler (stack , handler -> getCurrentTorchIndex (stack ) < handler .getSlots () ? handler . getCountInSlot ( getCurrentTorchIndex ( stack )) : 0 );
81+ return getFromHandler (stack , handler -> handler . getCountInSlot ( getCurrentTorchIndex (stack , handler .getSlots ())) );
8282 }
8383
8484 private void cycleTorchMode (ItemStack stack , boolean next ) {
85- ItemStack currentTorch = getCurrentTorch (stack );
86- if (getCurrentTorchIndex (stack ) != TORCH_SLOT && currentTorch .isEmpty ()) {
87- return ;
88- }
8985 runOnHandler (stack , handler -> {
9086 int slots = handler .getSlots ();
9187 if (slots == 1 ) {
9288 return ;
9389 }
94- int currentIndex = getCurrentTorchIndex (stack );
90+ int currentIndex = getCurrentTorchIndex (stack , slots );
91+ if (currentIndex != TORCH_SLOT && handler .getStackInSlot (currentIndex ).isEmpty ()) {
92+ return ;
93+ }
9594 stack .set (ModDataComponents .TORCH_INDEX , (byte ) Math .floorMod (currentIndex + (next ? 1 : -1 ), slots ));
9695 });
9796 }
9897
98+ private int getCurrentTorchIndex (ItemStack stack , int slots ) {
99+ int currentIndex = getCurrentTorchIndex (stack );
100+ if (currentIndex < TORCH_SLOT || currentIndex >= slots ) {
101+ currentIndex = Mth .clamp (currentIndex , TORCH_SLOT , slots - 1 );
102+ stack .set (ModDataComponents .TORCH_INDEX , (byte ) currentIndex );
103+ }
104+ return currentIndex ;
105+ }
106+
99107 private int getCurrentTorchIndex (ItemStack stack ) {
100108 return stack .getOrDefault (ModDataComponents .TORCH_INDEX , (byte ) 0 );
101109 }
@@ -174,8 +182,7 @@ private boolean removeTorches(Player player, ItemStack staff, BlockPos placeBloc
174182 int distance = (int ) player .getEyePosition (1 ).distanceTo (new Vec3 (placeBlockAt .getX (), placeBlockAt .getY (), placeBlockAt .getZ ()));
175183 int cost = 1 + distance / Config .COMMON .items .sojournerStaff .tilePerCostMultiplier .get ();
176184
177- int torchIndex = getCurrentTorchIndex (staff );
178- return useCharge (staff , torchIndex , cost );
185+ return getFromHandler (staff , handler -> useCharge (staff , getCurrentTorchIndex (staff , handler .getSlots ()), cost ));
179186 }
180187 return true ;
181188 }
@@ -231,9 +238,7 @@ protected boolean removeSlotWhenEmpty(int slot) {
231238 protected void removeSlot (ItemStack containerStack , int slot ) {
232239 runOnHandler (containerStack , handler -> {
233240 handler .removeSlot (slot );
234- if (getCurrentTorchIndex (containerStack ) >= handler .getSlots ()) {
235- cycleTorchMode (containerStack , false );
236- }
241+ getCurrentTorchIndex (containerStack , createHandler (containerStack ).getSlots ());
237242 });
238243
239244 }
0 commit comments