@@ -75,24 +75,23 @@ private void scanForMatchingTorchesToFillInternalStorage(ItemStack staff, Player
7575 }
7676
7777 public ItemStack getCurrentTorch (ItemStack stack ) {
78- return getFromHandler (stack , handler -> getCurrentTorchIndex (stack ) < handler .size () ? handler . getStackInSlot ( getCurrentTorchIndex ( stack )) : ItemStack . EMPTY );
78+ return getFromHandler (stack , handler -> handler . getStackInSlot ( getCurrentTorchIndex (stack , handler .size ())) );
7979 }
8080
8181 public int getTorchCount (ItemStack stack ) {
82- return getFromHandler (stack , handler -> getCurrentTorchIndex (stack ) < handler .size () ? handler . getAmountAsInt ( getCurrentTorchIndex ( stack )) : 0 );
82+ return getFromHandler (stack , handler -> handler . getAmountAsInt ( getCurrentTorchIndex (stack , handler .size ())) );
8383 }
8484
8585 private void cycleTorchMode (ItemStack stack , boolean next ) {
86- ItemStack currentTorch = getCurrentTorch (stack );
87- if (getCurrentTorchIndex (stack ) != TORCH_SLOT && currentTorch .isEmpty ()) {
88- return ;
89- }
9086 runOnHandler (stack , handler -> {
9187 int slots = handler .size ();
88+ int currentIndex = getCurrentTorchIndex (stack , slots );
89+ if (currentIndex != TORCH_SLOT && handler .getStackInSlot (currentIndex ).isEmpty ()) {
90+ return ;
91+ }
9292 if (slots == 1 ) {
9393 return ;
9494 }
95- int currentIndex = getCurrentTorchIndex (stack );
9695 stack .set (ModDataComponents .TORCH_INDEX , (byte ) Math .floorMod (currentIndex + (next ? 1 : -1 ), slots ));
9796 });
9897 }
@@ -101,6 +100,15 @@ private int getCurrentTorchIndex(ItemStack stack) {
101100 return stack .getOrDefault (ModDataComponents .TORCH_INDEX , (byte ) 0 );
102101 }
103102
103+ private int getCurrentTorchIndex (ItemStack stack , int slots ) {
104+ int currentIndex = getCurrentTorchIndex (stack );
105+ if (currentIndex < TORCH_SLOT || currentIndex >= slots ) {
106+ currentIndex = Mth .clamp (currentIndex , TORCH_SLOT , slots - 1 );
107+ stack .set (ModDataComponents .TORCH_INDEX , (byte ) currentIndex );
108+ }
109+ return currentIndex ;
110+ }
111+
104112 @ Override
105113 protected void addMoreInformation (ItemStack staff , HolderLookup .@ Nullable Provider registries , TooltipBuilder tooltipBuilder ) {
106114 StringJoiner joiner = new StringJoiner (";" );
@@ -175,8 +183,7 @@ private boolean removeTorches(Player player, ItemStack staff, BlockPos placeBloc
175183 int distance = (int ) player .getEyePosition (1 ).distanceTo (new Vec3 (placeBlockAt .getX (), placeBlockAt .getY (), placeBlockAt .getZ ()));
176184 int cost = 1 + distance / Config .COMMON .items .sojournerStaff .tilePerCostMultiplier .get ();
177185
178- int torchIndex = getCurrentTorchIndex (staff );
179- return useCharge (staff , torchIndex , cost );
186+ return getFromHandler (staff , handler -> useCharge (staff , getCurrentTorchIndex (staff , handler .size ()), cost ));
180187 }
181188 return true ;
182189 }
@@ -232,9 +239,7 @@ protected boolean removeSlotWhenEmpty(int slot) {
232239 protected void removeSlot (ItemStack containerStack , int slot ) {
233240 runOnHandler (containerStack , handler -> {
234241 handler .removeSlot (slot );
235- if (getCurrentTorchIndex (containerStack ) >= handler .size ()) {
236- cycleTorchMode (containerStack , false );
237- }
242+ getCurrentTorchIndex (containerStack , createHandler (containerStack ).size ());
238243 });
239244
240245 }
0 commit comments