aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/moulberry/notenoughupdates/miscgui/KatSitterOverlay.java
blob: c2908807ead1d73b6ac27670ec5295147dcc878b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * Copyright (C) 2022 NotEnoughUpdates contributors
 *
 * This file is part of NotEnoughUpdates.
 *
 * NotEnoughUpdates is free software: you can redistribute it
 * and/or modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 *
 * NotEnoughUpdates is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with NotEnoughUpdates. If not, see <https://www.gnu.org/licenses/>.
 */

package io.github.moulberry.notenoughupdates.miscgui;

import com.google.gson.JsonObject;
import io.github.moulberry.notenoughupdates.NotEnoughUpdates;
import io.github.moulberry.notenoughupdates.miscfeatures.PetInfoOverlay;
import io.github.moulberry.notenoughupdates.mixins.AccessorGuiContainer;
import io.github.moulberry.notenoughupdates.util.PetLeveling;
import io.github.moulberry.notenoughupdates.util.Utils;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.inventory.GuiChest;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.init.Blocks;
import net.minecraft.inventory.ContainerChest;
import net.minecraft.inventory.Slot;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.client.event.GuiScreenEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

public class KatSitterOverlay {
	public KatSitterOverlay() {
		MinecraftForge.EVENT_BUS.register(this);
	}

	@SubscribeEvent
	public void onGuiDrawn(GuiScreenEvent.DrawScreenEvent.Post event) {
		if (!(event.gui instanceof GuiChest)) return;
		if (!NotEnoughUpdates.INSTANCE.config.petOverlay.showKatSitting) return;
		GuiChest gui = (GuiChest) event.gui;
		ContainerChest container = (ContainerChest) gui.inventorySlots;
		if (!"Pet Sitter".equals(container.getLowerChestInventory().getDisplayName().getUnformattedText())) return;
		Slot slot = container.getSlot(13);
		if (slot == null || !slot.getHasStack() || slot.getStack() == null) return;
		ItemStack item = slot.getStack();
		NBTTagCompound tagCompound = item.getTagCompound();
		if (tagCompound == null || !tagCompound.hasKey("ExtraAttributes", 10)) return;
		NBTTagCompound extra = tagCompound.getCompoundTag("ExtraAttributes");
		if (extra == null || !extra.hasKey("id", 8) ||
			!"PET".equals(extra.getString("id")) || !extra.hasKey("petInfo", 8))
			return;
		JsonObject petInfo = NotEnoughUpdates.INSTANCE.manager.gson.fromJson(extra.getString("petInfo"), JsonObject.class);
		if (petInfo == null || !petInfo.has("exp") || !petInfo.has("tier") || !petInfo.has("type")) return;
		String petId = petInfo.get("type").getAsString();
		double xp = petInfo.get("exp").getAsDouble();
		PetInfoOverlay.Rarity rarity = PetInfoOverlay.Rarity.valueOf(petInfo.get("tier").getAsString());
		Slot katSlot = container.getSlot(22);
		PetInfoOverlay.Rarity upgradedRarity = rarity.nextRarity();
		boolean nextRarityPresent = katSlot.getStack() != null && katSlot.getStack().getItem() != Item.getItemFromBlock(
			Blocks.barrier) && upgradedRarity != null;
		renderPetInformation(
			PetLeveling
				.getPetLevelingForPet(petId, rarity)
				.getPetLevel(xp)
				.getCurrentLevel(),
			nextRarityPresent ?
				PetLeveling
					.getPetLevelingForPet(petId, rarity)
					.getPetLevel(xp)
					.getCurrentLevel() : null,
			gui
		);
	}

	public void renderPetInformation(int currentLevel, Integer upgradedLevel, GuiChest gui) {
		FontRenderer font = Minecraft.getMinecraft().fontRendererObj;
		String currentText = "Current pet level: " + currentLevel;
		int currentWidth = font.getStringWidth(currentText);
		String upgradedText = "Upgraded pet level: " + upgradedLevel;
		int upgradedWidth = font.getStringWidth(upgradedText);
		int left = ((AccessorGuiContainer) gui).getGuiLeft() - 30 - (upgradedLevel == null ? Math.max(
			upgradedWidth,
			currentWidth
		) : currentWidth);
		GlStateManager.disableLighting();
		GlStateManager.color(1F, 1F, 1F, 1F);
		Utils.drawStringScaled(currentText, left, ((AccessorGuiContainer) gui).getGuiTop() + 25, false, 0xFFD700, 1F);
		if (upgradedLevel != null)
			Utils.drawStringScaled(
				upgradedText,
				left,
				((AccessorGuiContainer) gui).getGuiTop() + 45,
				false,
				0xFFD700,
				1F
			);
	}



}