Skip to content

Commit

Permalink
fix NPE
Browse files Browse the repository at this point in the history
  • Loading branch information
goumo committed Jan 18, 2025
1 parent fe6548b commit 75e713e
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.teammoeg.frostedheart.content.tips;

import com.teammoeg.chorda.util.lang.Components;
import com.teammoeg.frostedheart.FHMain;
import com.teammoeg.frostedheart.content.waypoint.ClientWaypointManager;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.event.ClientPlayerNetworkEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

@Mod.EventBusSubscriber(modid = FHMain.MODID, bus = Mod.EventBusSubscriber.Bus.FORGE, value = Dist.CLIENT)
public class ClientEvents {

@SubscribeEvent
public static void onPlayerLoggedIn(ClientPlayerNetworkEvent.LoggingIn event) {
// default tip
if (!TipManager.INSTANCE.hasTip("default")) {
Tip.builder("default").line(Components.str("Default Tip")).image(new ResourceLocation(FHMain.MODID, "textures/item/debug_item.png")).build().saveAsFile();
TipManager.INSTANCE.loadFromFile();
}
TipManager.INSTANCE.display().clearRenderQueue();
TipManager.INSTANCE.display().general("default");
// if (Minecraft.getInstance().gameSettings.getSoundLevel(SoundCategory.MUSIC) == 0) {
// TipDisplayManager.displayTip("music_warning", false);
// }
}

@SubscribeEvent
public static void onPlayerLoggedOut(ClientPlayerNetworkEvent.LoggingOut event) {
TipManager.INSTANCE.display().clearRenderQueue();
ClientWaypointManager.clear();
}
}
18 changes: 9 additions & 9 deletions src/main/java/com/teammoeg/frostedheart/content/tips/Tip.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,14 @@ public boolean hasNext() {
}

public boolean saveAsFile() {
if (isTipIdInvalid(this.id)) {
builder("exception").error(ErrorType.LOAD, Component.literal("ID: " + this.id), Component.translatable("tips.frostedheart.error.invalid_id")).build().forceDisplay();
if (this.id.isBlank()) {
builder("exception").error(ErrorType.SAVE, Component.translatable("tips.frostedheart.error.load.no_id")).build().forceDisplay();
return false;
} else if (isTipIdInvalid(this.id)) {
builder("exception").error(ErrorType.SAVE, Component.literal("ID: " + this.id), Component.translatable("tips.frostedheart.error.invalid_id")).build().forceDisplay();
return false;
} else if (TipManager.INSTANCE.hasTip(this.id)) {
builder("exception").error(ErrorType.SAVE, Component.literal("ID: " + this.id), Component.translatable("tips.frostedheart.error.load.duplicate_id")).build().forceDisplay();
return false;
}

Expand Down Expand Up @@ -244,7 +250,6 @@ public static class Builder {
public Builder(String id) {
if (isTipIdInvalid(id)) {
error(ErrorType.LOAD, Component.literal("ID: " + id), Component.translatable("tips.frostedheart.error.invalid_id"));
return;
}
this.id = id;
setTemporary();
Expand Down Expand Up @@ -392,7 +397,7 @@ public Builder fromNBT(CompoundTag nbt) {
this.contents.addAll(list);
}
if (id.isBlank()) {
error(ErrorType.OTHER, Component.literal("NBT does not contain tip"));
error(ErrorType.OTHER, Component.literal("NBT does not contain a tip"));
}
return this;
}
Expand All @@ -404,13 +409,8 @@ public Builder fromJson(JsonObject json) {
String s = json.get("id").getAsString();
if (s.isBlank()) {
error(ErrorType.LOAD,Component.translatable("tips.frostedheart.error.load.no_id"));
return this;
} else if (TipManager.INSTANCE.hasTip(s)) {
error(ErrorType.LOAD, Component.literal("ID: " + s), Component.translatable("tips.frostedheart.error.load.duplicate_id"));
return this;
} else if (isTipIdInvalid(s)) {
error(ErrorType.LOAD, Component.literal("ID: " + s), Component.translatable("tips.frostedheart.error.invalid_id"));
return this;
}
id = s;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ public boolean hasTip(String id) {
*/
public void loadFromFile() {
TIP_PATH.mkdirs();
display.clearRenderQueue();
loadedTips.clear();

// 加载所有 tip 文件
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ public TipEditsList(Minecraft pMinecraft, int pWidth, int pHeight, int pY0, int
var idEntry = (StringEntry)children().get(addEntry(new StringEntry("id", Component.translatable("gui.frostedheart.tip_editor.id"))));
idEntry.input.setMaxLength(240);
idEntry.input.setResponder(s -> {
if (TipManager.INSTANCE.hasTip(s) || Tip.isTipIdInvalid(s)) {
if (TipManager.INSTANCE.hasTip(s)) {
idEntry.input.setTextColor(ColorHelper.RED);
updatePreview(Component.translatable("tips.frostedheart.error.load.duplicate_id").withStyle(ChatFormatting.RED));
return;
} else if (Tip.isTipIdInvalid(s)) {
idEntry.input.setTextColor(ColorHelper.RED);
} else {
idEntry.input.setTextColor(ColorHelper.WHITE);
Expand All @@ -59,7 +63,7 @@ public TipEditsList(Minecraft pMinecraft, int pWidth, int pHeight, int pY0, int
updatePreview(
Component.translatable("tips.frostedheart.error.invalid_id").withStyle(ChatFormatting.RED),
Component.translatable("tips.frostedheart.error.load.tip_not_exists", s).withStyle(ChatFormatting.GOLD));
} else if (!TipManager.INSTANCE.hasTip(s)) {
} else if (!s.isBlank() && !TipManager.INSTANCE.hasTip(s)) {
nextTipEntry.input.setTextColor(0xFFFF9F00);
updatePreview(Component.translatable("tips.frostedheart.error.load.tip_not_exists", s).withStyle(ChatFormatting.GOLD, ChatFormatting.BOLD));
} else {
Expand Down Expand Up @@ -132,7 +136,7 @@ public IntegerEntry(String property, Component message) {
this.input.setResponder(s -> {
try {
input.setTextColor(ColorHelper.WHITE);
Integer.parseUnsignedInt(s, 16);
Integer.parseInt(s);
} catch (NumberFormatException e) {
input.setTextColor(ColorHelper.RED);
}
Expand All @@ -146,7 +150,7 @@ public JsonElement getValue() {
try {
value = Integer.parseInt(input.getValue());
} catch (NumberFormatException e) {
value = ColorHelper.CYAN;
value = 0;
}
return new JsonPrimitive(value);
}
Expand Down
23 changes: 0 additions & 23 deletions src/main/java/com/teammoeg/frostedheart/events/FHClientEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@
import com.teammoeg.frostedheart.content.research.research.effects.EffectShowCategory;
import com.teammoeg.frostedheart.content.scenario.client.ClientScene;
import com.teammoeg.frostedheart.content.scenario.client.dialog.HUDDialog;
import com.teammoeg.frostedheart.content.tips.Tip;
import com.teammoeg.frostedheart.content.tips.TipManager;
import com.teammoeg.frostedheart.content.waypoint.ClientWaypointManager;
import com.teammoeg.frostedheart.infrastructure.config.FHConfig;
import com.teammoeg.frostedheart.util.FHVersion;
Expand All @@ -56,7 +54,6 @@
import net.minecraft.network.chat.ClickEvent;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.network.chat.Style;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.FormattedCharSequence;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.player.Player;
Expand Down Expand Up @@ -464,26 +461,6 @@ public static void onWorldRender(RenderLevelStageEvent event) {
}
}

@SubscribeEvent
public static void onPlayerLoggedIn(ClientPlayerNetworkEvent.LoggingIn event) {
// default tip
if (!TipManager.INSTANCE.hasTip("default")) {
Tip.builder("default").line(Components.str("Default Tip")).image(new ResourceLocation(FHMain.MODID, "textures/item/debug_item.png")).build().saveAsFile();
TipManager.INSTANCE.loadFromFile();
}
TipManager.INSTANCE.display().clearRenderQueue();
TipManager.INSTANCE.display().general("default");
// if (Minecraft.getInstance().gameSettings.getSoundLevel(SoundCategory.MUSIC) == 0) {
// TipDisplayManager.displayTip("music_warning", false);
// }
}

@SubscribeEvent
public static void onPlayerLoggedOut(ClientPlayerNetworkEvent.LoggingOut event) {
TipManager.INSTANCE.display().clearRenderQueue();
ClientWaypointManager.clear();
}

// @SubscribeEvent
// public static void onGUIOpen(ScreenEvent.Opening event) {
// if (event.getScreen() instanceof TitleScreen) {
Expand Down

0 comments on commit 75e713e

Please sign in to comment.