Skip to content

Commit f5bef06

Browse files
committed
fix: fix server crashing
1 parent 7e0e820 commit f5bef06

File tree

16 files changed

+281
-275
lines changed

16 files changed

+281
-275
lines changed

src/main/java/vazkii/psi/client/core/handler/ClientTickHandler.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* This class is distributed as part of the Psi Mod.
3-
* Get the Source Code in github:
3+
* Get the Source Code in GitHub:
44
* https://github.com/Vazkii/Psi
55
*
66
* Psi is Open Source and distributed under the
@@ -35,47 +35,43 @@ public class ClientTickHandler {
3535

3636
public ClientTickHandler() {}
3737

38-
@OnlyIn(Dist.CLIENT)
3938
private static void calcDelta() {
4039
float oldTotal = total;
4140
total = (float) ticksInGame + partialTicks;
4241
delta = total - oldTotal;
4342
}
4443

4544
@SubscribeEvent
46-
@OnlyIn(Dist.CLIENT)
4745
public static void renderTick(RenderFrameEvent.Pre event) {
4846
partialTicks = event.getPartialTick().getGameTimeDeltaPartialTick(false);
4947

5048
}
5149

5250
@SubscribeEvent
53-
@OnlyIn(Dist.CLIENT)
5451
public static void renderTick(RenderFrameEvent.Post event) {
5552
calcDelta();
5653
}
5754

5855
@SubscribeEvent
5956
public static void clientTick(ClientTickEvent.Pre event) {
60-
6157
Minecraft mc = Minecraft.getInstance();
62-
6358
boolean pressed = mc.options.keyJump.consumeClick();
59+
6460
if(mc.player != null && pressed && (!lastJumpKeyState && !mc.player.onGround())) {
6561
PsiArmorEvent.post(new PsiArmorEvent(mc.player, PsiArmorEvent.JUMP));
6662
MessageRegister.sendToServer(new MessageTriggerJumpSpell());
6763
}
64+
6865
lastJumpKeyState = pressed;
6966
}
7067

7168
@SubscribeEvent
7269
public static void clientTick(ClientTickEvent.Post event) {
73-
7470
Minecraft mc = Minecraft.getInstance();
7571

7672
HUDHandler.tick();
77-
7873
Screen gui = mc.screen;
74+
7975
if(gui == null && KeybindHandler.keybind.isDown()) {
8076
KeybindHandler.keyDown();
8177
}
@@ -84,6 +80,7 @@ public static void clientTick(ClientTickEvent.Post event) {
8480
++ticksInGame;
8581
partialTicks = 0.0F;
8682
}
83+
8784
calcDelta();
8885
}
8986

src/main/java/vazkii/psi/client/core/handler/ColorHandler.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* This class is distributed as part of the Psi Mod.
3-
* Get the Source Code in github:
3+
* Get the Source Code in GitHub:
44
* https://github.com/Vazkii/Psi
55
*
66
* Psi is Open Source and distributed under the
@@ -9,6 +9,8 @@
99
package vazkii.psi.client.core.handler;
1010

1111
import net.minecraft.util.Mth;
12+
import net.neoforged.api.distmarker.Dist;
13+
import net.neoforged.api.distmarker.OnlyIn;
1214
import net.neoforged.bus.api.SubscribeEvent;
1315
import net.neoforged.fml.common.EventBusSubscriber;
1416
import net.neoforged.neoforge.client.event.RegisterColorHandlersEvent;
@@ -20,7 +22,8 @@
2022
import vazkii.psi.common.item.component.ItemCADColorizer;
2123
import vazkii.psi.common.lib.LibMisc;
2224

23-
@EventBusSubscriber(modid = LibMisc.MOD_ID)
25+
@OnlyIn(Dist.CLIENT)
26+
@EventBusSubscriber(value = Dist.CLIENT, modid = LibMisc.MOD_ID)
2427
public class ColorHandler {
2528

2629
@SubscribeEvent
@@ -48,14 +51,6 @@ public static int slideColor(int[] color, float speed) {
4851
return slideColorTime(color[phase], color[nextPhase], (float) (dt * Math.PI));
4952
}
5053

51-
public static int slideColor(int color, int secondColor, double speed) {
52-
return slideColorTime(color, secondColor, (float) (ClientTickHandler.total * speed));
53-
}
54-
55-
public static int pulseColor(int source) {
56-
return pulseColor(source, 1f, 0.2f, 24);
57-
}
58-
5954
public static int pulseColor(int source, float speed, int magnitude) {
6055
return pulseColor(source, 1f, speed, magnitude);
6156
}

src/main/java/vazkii/psi/client/core/handler/HUDHandler.java

Lines changed: 87 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* This class is distributed as part of the Psi Mod.
3-
* Get the Source Code in github:
3+
* Get the Source Code in GitHub:
44
* https://github.com/Vazkii/Psi
55
*
66
* Psi is Open Source and distributed under the
@@ -46,7 +46,7 @@
4646

4747
import java.util.regex.Pattern;
4848

49-
@EventBusSubscriber(modid = LibMisc.MOD_ID)
49+
@EventBusSubscriber(modid = LibMisc.MOD_ID, value = Dist.CLIENT)
5050
public final class HUDHandler {
5151

5252
public static final LayeredDraw.Layer SOCKETABLE_EQUIPPED_NAME = (graphics, deltatracker) -> {
@@ -88,21 +88,22 @@ public static void register(RegisterGuiLayersEvent event) {
8888
}
8989

9090
public static void tick() {
91-
92-
if(remainingTime > 0) {
93-
--remainingTime;
91+
if(remainingTime < 0) {
92+
return;
9493
}
94+
95+
--remainingTime;
9596
}
9697

9798
private static boolean showsBar(PlayerData data, ItemStack stack) {
9899
if(stack.isEmpty()) {
99100
return false;
100-
} else {
101-
IPsiBarDisplay display = stack.getCapability(PsiAPI.PSI_BAR_DISPLAY_CAPABILITY);
102-
if(display != null)
103-
return display.shouldShow(data);
104-
return false;
105101
}
102+
103+
IPsiBarDisplay display = stack.getCapability(PsiAPI.PSI_BAR_DISPLAY_CAPABILITY);
104+
if(display != null)
105+
return display.shouldShow(data);
106+
return false;
106107
}
107108

108109
@OnlyIn(Dist.CLIENT)
@@ -250,99 +251,104 @@ public static void drawPsiBar(GuiGraphics graphics, DeltaTracker deltatracker) {
250251
@OnlyIn(Dist.CLIENT)
251252
private static void renderSocketableEquippedName(GuiGraphics graphics, DeltaTracker deltatracker) {
252253
Minecraft mc = Minecraft.getInstance();
254+
if(mc.player == null || mc.gui.toolHighlightTimer - 10 <= 0) {
255+
return;
256+
}
257+
253258
ItemStack stack = mc.player.getItemInHand(InteractionHand.MAIN_HAND);
254259
if(!ISocketable.isSocketable(stack)) {
255260
return;
256261
}
262+
257263
String name = ISocketable.getSocketedItemName(stack, "").getString();
258264
if(stack.isEmpty() || name.trim().isEmpty()) {
259265
return;
260266
}
261267

262-
int ticks = mc.gui.toolHighlightTimer;
263-
ticks -= 10;
268+
ISocketable socketable = ISocketable.socketable(stack);
269+
ItemStack bullet = socketable.getSelectedBullet();
270+
271+
int ticks = mc.gui.toolHighlightTimer - 10;
272+
int alpha = Math.min(255, (int) ((ticks - deltatracker.getGameTimeDeltaPartialTick(false)) * 256.0F / 10.0F));
273+
int color = ICADColorizer.DEFAULT_SPELL_COLOR + (alpha << 24);
274+
int x = graphics.guiWidth() / 2 - mc.font.width(name) / 2;
275+
int y = graphics.guiHeight() - 71;
276+
int w = mc.font.width(name);
277+
if(mc.player.isCreative()) {
278+
y += 14;
279+
}
264280

265-
if(ticks > 0) {
266-
ISocketable socketable = ISocketable.socketable(stack);
267-
ItemStack bullet = socketable.getSelectedBullet();
281+
graphics.drawString(mc.font, name, x, y, color, true);
282+
graphics.pose().pushPose();
283+
graphics.pose().translate(x + w, y - 6, 0);
284+
graphics.pose().scale(alpha / 255F, 1F, 1);
285+
graphics.renderFakeItem(bullet, 0, 0);
286+
graphics.pose().popPose();
287+
}
268288

269-
int alpha = Math.min(255, (int) ((ticks - deltatracker.getGameTimeDeltaPartialTick(false)) * 256.0F / 10.0F));
270-
int color = ICADColorizer.DEFAULT_SPELL_COLOR + (alpha << 24);
289+
@OnlyIn(Dist.CLIENT)
290+
private static void renderRemainingItems(GuiGraphics graphics, DeltaTracker deltatracker) {
291+
if(remainingTime <= 0 || remainingDisplayStack.isEmpty()) {
292+
return;
293+
}
271294

272-
int x = graphics.guiWidth() / 2 - mc.font.width(name) / 2;
273-
int y = graphics.guiHeight() - 71;
274-
if(mc.player.isCreative()) {
275-
y += 14;
276-
}
295+
int pos = maxRemainingTicks - remainingTime;
296+
Minecraft mc = Minecraft.getInstance();
297+
int remainingLeaveTicks = 20;
298+
int x = graphics.guiWidth() / 2 + 10 + Math.max(0, pos - remainingLeaveTicks);
299+
int y = graphics.guiHeight() / 2;
277300

278-
graphics.drawString(mc.font, name, x, y, color, true);
301+
int start = maxRemainingTicks - remainingLeaveTicks;
302+
float alpha = remainingTime + deltatracker.getGameTimeDeltaPartialTick(false) > start ? 1F : (remainingTime + deltatracker.getGameTimeDeltaPartialTick(false)) / start;
279303

280-
int w = mc.font.width(name);
281-
graphics.pose().pushPose();
282-
graphics.pose().translate(x + w, y - 6, 0);
283-
graphics.pose().scale(alpha / 255F, 1F, 1);
284-
graphics.renderFakeItem(bullet, 0, 0);
285-
graphics.pose().popPose();
286-
}
287-
}
304+
RenderSystem.setShaderColor(1F, 1F, 1F, alpha);
305+
int xp = x + (int) (16F * (1F - alpha));
306+
graphics.pose().pushPose();
307+
graphics.pose().translate(xp, y, 0F);
308+
graphics.pose().scale(alpha, 1F, 1F);
309+
graphics.renderFakeItem(remainingDisplayStack, 0, 0);
310+
graphics.pose().scale(1F / alpha, 1F, 1F);
311+
graphics.pose().translate(-xp, -y, 0F);
312+
RenderSystem.setShaderColor(1F, 1F, 1F, 1F);
288313

289-
@OnlyIn(Dist.CLIENT)
290-
private static void renderRemainingItems(GuiGraphics graphics, DeltaTracker deltatracker) {
291-
if(remainingTime > 0 && !remainingDisplayStack.isEmpty()) {
292-
int pos = maxRemainingTicks - remainingTime;
293-
Minecraft mc = Minecraft.getInstance();
294-
int remainingLeaveTicks = 20;
295-
int x = graphics.guiWidth() / 2 + 10 + Math.max(0, pos - remainingLeaveTicks);
296-
int y = graphics.guiHeight() / 2;
297-
298-
int start = maxRemainingTicks - remainingLeaveTicks;
299-
float alpha = remainingTime + deltatracker.getGameTimeDeltaPartialTick(false) > start ? 1F : (remainingTime + deltatracker.getGameTimeDeltaPartialTick(false)) / start;
300-
301-
RenderSystem.setShaderColor(1F, 1F, 1F, alpha);
302-
int xp = x + (int) (16F * (1F - alpha));
303-
graphics.pose().pushPose();
304-
graphics.pose().translate(xp, y, 0F);
305-
graphics.pose().scale(alpha, 1F, 1F);
306-
graphics.renderFakeItem(remainingDisplayStack, 0, 0);
307-
graphics.pose().scale(1F / alpha, 1F, 1F);
308-
graphics.pose().translate(-xp, -y, 0F);
309-
RenderSystem.setShaderColor(1F, 1F, 1F, 1F);
310-
311-
String text = remainingDisplayStack.getHoverName().plainCopy().withStyle(ChatFormatting.GREEN).getString();
312-
if(remainingCount >= 0) {
313-
int max = remainingDisplayStack.getMaxStackSize();
314-
int stacks = remainingCount / max;
315-
int rem = remainingCount % max;
316-
317-
if(stacks == 0) {
318-
text = "" + remainingCount;
319-
} else {
320-
text = remainingCount + " (" + ChatFormatting.AQUA + stacks + ChatFormatting.RESET + "*"
321-
+ ChatFormatting.GRAY + max + ChatFormatting.RESET + "+" + ChatFormatting.YELLOW + rem
322-
+ ChatFormatting.RESET + ")";
323-
}
324-
} else if(remainingCount == -1) {
325-
text = "∞";
314+
String text = remainingDisplayStack.getHoverName().plainCopy().withStyle(ChatFormatting.GREEN).getString();
315+
if(remainingCount >= 0) {
316+
int max = remainingDisplayStack.getMaxStackSize();
317+
int stacks = remainingCount / max;
318+
int rem = remainingCount % max;
319+
320+
if(stacks == 0) {
321+
text = "" + remainingCount;
322+
} else {
323+
text = remainingCount + " (" + ChatFormatting.AQUA + stacks + ChatFormatting.RESET + "*"
324+
+ ChatFormatting.GRAY + max + ChatFormatting.RESET + "+" + ChatFormatting.YELLOW + rem
325+
+ ChatFormatting.RESET + ")";
326326
}
327+
} else if(remainingCount == -1) {
328+
text = "∞";
329+
}
327330

328-
int color = 0x00FFFFFF | (int) (alpha * 0xFF) << 24;
329-
graphics.drawString(mc.font, text, x + 20, y + 6, color, true);
331+
int color = 0x00FFFFFF | (int) (alpha * 0xFF) << 24;
330332

331-
graphics.pose().popPose();
332-
}
333+
graphics.drawString(mc.font, text, x + 20, y + 6, color, true);
334+
graphics.pose().popPose();
333335
}
334336

335337
@OnlyIn(Dist.CLIENT)
336338
private static void renderHUDItem(GuiGraphics graphics, DeltaTracker deltatracker) {
337339
Minecraft mc = Minecraft.getInstance();
340+
if(mc.player == null) {
341+
return;
342+
}
343+
338344
ItemStack stack = mc.player.getMainHandItem();
339-
if(!stack.isEmpty() && stack.getItem() instanceof IHUDItem) {
340-
((IHUDItem) stack.getItem()).drawHUD(graphics, deltatracker.getGameTimeDeltaPartialTick(false), graphics.guiWidth(), graphics.guiHeight(), stack);
345+
if(!stack.isEmpty() && stack.getItem() instanceof IHUDItem hudItem) {
346+
hudItem.drawHUD(graphics, deltatracker.getGameTimeDeltaPartialTick(false), graphics.guiWidth(), graphics.guiHeight(), stack);
341347
}
342348

343349
stack = mc.player.getOffhandItem();
344-
if(!stack.isEmpty() && stack.getItem() instanceof IHUDItem) {
345-
((IHUDItem) stack.getItem()).drawHUD(graphics, deltatracker.getGameTimeDeltaPartialTick(false), graphics.guiWidth(), graphics.guiHeight(), stack);
350+
if(!stack.isEmpty() && stack.getItem() instanceof IHUDItem hudItem) {
351+
hudItem.drawHUD(graphics, deltatracker.getGameTimeDeltaPartialTick(false), graphics.guiWidth(), graphics.guiHeight(), stack);
346352
}
347353
}
348354

@@ -356,7 +362,11 @@ public static void setRemaining(Player player, ItemStack displayStack, Pattern p
356362
int count = 0;
357363
for(int i = 0; i < player.getInventory().getContainerSize(); i++) {
358364
ItemStack stack = player.getInventory().getItem(i);
359-
if(!stack.isEmpty() && (pattern == null ? ItemStack.isSameItem(displayStack, stack) : pattern.matcher(stack.getDescriptionId()).find())) {
365+
if(stack.isEmpty()) {
366+
continue;
367+
}
368+
369+
if(pattern == null ? ItemStack.isSameItem(displayStack, stack) : pattern.matcher(stack.getDescriptionId()).find()) {
360370
count += stack.getCount();
361371
}
362372
}

0 commit comments

Comments
 (0)