94
94
95
95
import javax .annotation .Nonnull ;
96
96
import java .util .ArrayList ;
97
+ import java .util .Arrays ;
97
98
import java .util .List ;
98
99
import java .util .Optional ;
100
+ import java .util .function .BiConsumer ;
101
+ import java .util .function .Consumer ;
99
102
import java .util .stream .IntStream ;
100
103
101
104
import static blusunrize .immersiveengineering .ImmersiveEngineering .rl ;
@@ -116,7 +119,7 @@ public void onResourceManagerReload(@Nonnull ResourceManager resourceManager)
116
119
public void onPlayerTick (PlayerTickEvent .Post event )
117
120
{
118
121
final var player = event .getEntity ();
119
- if (player == null || !player .level ().isClientSide ||player !=ClientUtils .mc ().player )
122
+ if (!player .level ().isClientSide ||player !=ClientUtils .mc ().player )
120
123
return ;
121
124
if (this .shieldToggleTimer > 0 )
122
125
this .shieldToggleTimer --;
@@ -165,6 +168,7 @@ public void onPlayerTick(PlayerTickEvent.Post event)
165
168
if (steps !=null &&steps .length > 0 )
166
169
ZoomHandler .fovZoom = steps [ZoomHandler .getCurrentZoomStep (steps )];
167
170
}
171
+ break ;
168
172
}
169
173
}
170
174
}
@@ -307,10 +311,23 @@ else if(equipped instanceof RailgunItem||equipped instanceof IEShieldItem)
307
311
@ SubscribeEvent
308
312
public void onRenderOverlayPre (RenderGuiLayerEvent .Pre event )
309
313
{
310
- ItemStack equipped = ClientUtils .mc ().player .getItemInHand (InteractionHand .MAIN_HAND );
311
- // early exit if not handling a zoom tool
312
- if (!(event .getName ().equals (VanillaGuiLayers .CROSSHAIR )&&ZoomHandler .isZooming &&equipped .getItem () instanceof IZoomTool tool ))
314
+ // early exit if not handling zooming
315
+ if (!event .getName ().equals (VanillaGuiLayers .CROSSHAIR )||!ZoomHandler .isZooming )
313
316
return ;
317
+
318
+ // check for a zoom tool and get its step array
319
+ Player player = ClientUtils .mc ().player ;
320
+ float [] zoomSteps = Arrays .stream (InteractionHand .values ())
321
+ .map (player ::getItemInHand )
322
+ .mapMulti ((BiConsumer <ItemStack , Consumer <float []>>)(itemStack , consumer ) -> {
323
+ if (itemStack .getItem () instanceof IZoomTool zoomTool &&zoomTool .canZoom (itemStack , player ))
324
+ consumer .accept (zoomTool .getZoomSteps (itemStack , player ));
325
+ }).findFirst ().orElse (null );
326
+
327
+ if (zoomSteps ==null ||zoomSteps .length <= 1 )
328
+ return ;
329
+
330
+ // cancel default crosshair
314
331
event .setCanceled (true );
315
332
316
333
GuiGraphics graphics = event .getGuiGraphics ();
@@ -338,44 +355,40 @@ public void onRenderOverlayPre(RenderGuiLayerEvent.Pre event)
338
355
graphics .blitSprite (ieLoc ("hud/scope" ), 0 , 0 , resMin , resMin );
339
356
RenderSystem .disableBlend ();
340
357
341
- float [] steps = tool .getZoomSteps (equipped , ClientUtils .mc ().player );
342
- if (steps !=null &&steps .length > 1 )
358
+ // draw gauge on the right side
359
+ transform .translate (218 /256f *resMin , 64 /256f *resMin , 0 );
360
+ graphics .blitSprite (ieLoc ("hud/gauge_vertical" ), 0 , 0 , Math .round (24 /256f *resMin ), Math .round (128 /256f *resMin ));
361
+
362
+ float stepLength = 118 /(float )zoomSteps .length ;
363
+ float stepOffset = (stepLength -7 )/2f ;
364
+ // move inside the gauge
365
+ RenderSystem .enableBlend ();
366
+ transform .translate (5 /256f *resMin , (5 +stepOffset )/256f *resMin , 0 );
367
+
368
+ // draw markers for the possible steps
369
+ int curStep = -1 ;
370
+ float dist = 0 ;
371
+ int innerWidth = Math .round (14 /256f *resMin );
372
+ int innerHeight = Math .round (7 /256f *resMin );
373
+
374
+ for (int i = 0 ; i < zoomSteps .length ; i ++)
343
375
{
344
- // draw gauge on the right side
345
- transform .translate (218 /256f *resMin , 64 /256f *resMin , 0 );
346
- graphics .blitSprite (ieLoc ("hud/gauge_vertical" ), 0 , 0 , Math .round (24 /256f *resMin ), Math .round (128 /256f *resMin ));
347
-
348
- float stepLength = 118 /(float )steps .length ;
349
- float stepOffset = (stepLength -7 )/2f ;
350
- // move inside the gauge
351
- RenderSystem .enableBlend ();
352
- transform .translate (5 /256f *resMin , (5 +stepOffset )/256f *resMin , 0 );
353
-
354
- // draw markers for the possible steps
355
- int curStep = -1 ;
356
- float dist = 0 ;
357
- int innerWidth = Math .round (14 /256f *resMin );
358
- int innerHeight = Math .round (7 /256f *resMin );
359
-
360
- for (int i = 0 ; i < steps .length ; i ++)
376
+ graphics .blitSprite (ieLoc ("hud/gauge_vertical_step" ), 0 , 0 , innerWidth , innerHeight );
377
+ transform .translate (0 , stepLength /256 *resMin , 0 );
378
+ if (curStep ==-1 ||Math .abs (zoomSteps [i ]-ZoomHandler .fovZoom ) < dist )
361
379
{
362
- graphics .blitSprite (ieLoc ("hud/gauge_vertical_step" ), 0 , 0 , innerWidth , innerHeight );
363
- transform .translate (0 , stepLength /256 *resMin , 0 );
364
- if (curStep ==-1 ||Math .abs (steps [i ]-ZoomHandler .fovZoom ) < dist )
365
- {
366
- curStep = i ;
367
- dist = Math .abs (steps [i ]-ZoomHandler .fovZoom );
368
- }
380
+ curStep = i ;
381
+ dist = Math .abs (zoomSteps [i ]-ZoomHandler .fovZoom );
369
382
}
370
- transform .translate (0 , -118 /256f *resMin , 0 );
371
-
372
- // draw the pointer and text for current level
373
- transform .translate (0 , curStep *stepLength /256 *resMin , 0 );
374
- graphics .blitSprite (ieLoc ("hud/gauge_vertical_pointer" ), 0 , 0 , innerWidth , innerHeight );
375
- transform .translate (16 /256f *resMin , 1 /256f *resMin , 0 );
376
- graphics .drawString (ClientUtils .font (), (1 /steps [curStep ])+"x" , 0 , 0 , 0xffffff , false );
377
- RenderSystem .disableBlend ();
378
383
}
384
+ transform .translate (0 , -118 /256f *resMin , 0 );
385
+
386
+ // draw the pointer and text for current level
387
+ transform .translate (0 , curStep *stepLength /256 *resMin , 0 );
388
+ graphics .blitSprite (ieLoc ("hud/gauge_vertical_pointer" ), 0 , 0 , innerWidth , innerHeight );
389
+ transform .translate (16 /256f *resMin , 1 /256f *resMin , 0 );
390
+ graphics .drawString (ClientUtils .font (), (1 /zoomSteps [curStep ])+"x" , 0 , 0 , 0xffffff , false );
391
+ RenderSystem .disableBlend ();
379
392
transform .popPose ();
380
393
}
381
394
@@ -415,8 +428,9 @@ public void onFOVUpdate(ComputeFovModifierEvent event)
415
428
Player player = ClientUtils .mc ().player ;
416
429
417
430
// Check if player is holding a zoom-allowing item
418
- ItemStack equipped = player .getItemInHand (InteractionHand .MAIN_HAND );
419
- boolean mayZoom = equipped .getItem () instanceof IZoomTool &&((IZoomTool )equipped .getItem ()).canZoom (equipped , player );
431
+ boolean mayZoom = Arrays .stream (InteractionHand .values ())
432
+ .map (player ::getItemInHand )
433
+ .anyMatch (s -> s .getItem () instanceof IZoomTool zoomTool &&zoomTool .canZoom (s , player ));
420
434
// Set zoom if allowed, otherwise stop zooming
421
435
if (ZoomHandler .isZooming )
422
436
{
@@ -436,10 +450,12 @@ public void onPlayerTurn(CalculatePlayerTurnEvent event)
436
450
{
437
451
if (event .getCinematicCameraEnabled ())
438
452
return ;
453
+
439
454
// Check if player is holding a zoom-allowing item and using them
440
455
Player player = ClientUtils .mc ().player ;
441
- ItemStack equipped = player .getItemInHand (InteractionHand .MAIN_HAND );
442
- boolean mayZoom = equipped .getItem () instanceof IZoomTool &&((IZoomTool )equipped .getItem ()).canZoom (equipped , player );
456
+ boolean mayZoom = Arrays .stream (InteractionHand .values ())
457
+ .map (player ::getItemInHand )
458
+ .anyMatch (s -> s .getItem () instanceof IZoomTool zoomTool &&zoomTool .canZoom (s , player ));
443
459
if (ZoomHandler .isZooming &&mayZoom )
444
460
{
445
461
// final math is: (m*0.6 + 0.2)³ * 8; where m is the mouse sensitivity
@@ -459,21 +475,26 @@ public void onMouseEvent(MouseScrollingEvent event)
459
475
Player player = ClientUtils .mc ().player ;
460
476
if (event .getScrollDeltaY ()!=0 &&ClientUtils .mc ().screen ==null &&player !=null )
461
477
{
462
- ItemStack equipped = player .getItemInHand (InteractionHand .MAIN_HAND );
463
- // Handle zoom steps
464
- if (equipped .getItem () instanceof IZoomTool &&((IZoomTool )equipped .getItem ()).canZoom (equipped , player )&&ZoomHandler .isZooming )
478
+ // Handle zooming in and out
479
+ if (ZoomHandler .isZooming )
465
480
{
466
- float [] steps = ((IZoomTool )equipped .getItem ()).getZoomSteps (equipped , player );
467
- if (steps !=null &&steps .length > 0 )
481
+ float [] zoomSteps = Arrays .stream (InteractionHand .values ())
482
+ .map (player ::getItemInHand )
483
+ .mapMulti ((BiConsumer <ItemStack , Consumer <float []>>)(itemStack , consumer ) -> {
484
+ if (itemStack .getItem () instanceof IZoomTool zoomTool &&zoomTool .canZoom (itemStack , player ))
485
+ consumer .accept (zoomTool .getZoomSteps (itemStack , player ));
486
+ }).findFirst ().orElse (null );
487
+ if (zoomSteps !=null &&zoomSteps .length > 0 )
468
488
{
469
- int curStep = ZoomHandler .getCurrentZoomStep (steps );
489
+ int curStep = ZoomHandler .getCurrentZoomStep (zoomSteps );
470
490
int newStep = curStep +(event .getScrollDeltaY () > 0 ?-1 : 1 );
471
- if (newStep >= 0 &&newStep < steps .length )
472
- ZoomHandler .fovZoom = steps [newStep ];
491
+ if (newStep >= 0 &&newStep < zoomSteps .length )
492
+ ZoomHandler .fovZoom = zoomSteps [newStep ];
473
493
event .setCanceled (true );
474
494
}
475
495
}
476
496
497
+ ItemStack equipped = player .getItemInHand (InteractionHand .MAIN_HAND );
477
498
// Handle sneak + scrolling
478
499
if (player .isShiftKeyDown ())
479
500
{
0 commit comments