Skip to content

Commit

Permalink
add backpack previews
Browse files Browse the repository at this point in the history
  • Loading branch information
Blast-MC committed Feb 12, 2024
1 parent 35f0321 commit d1e744c
Show file tree
Hide file tree
Showing 13 changed files with 394 additions and 16 deletions.
21 changes: 16 additions & 5 deletions src/main/java/gg/projecteden/titan/config/ConfigItem.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package gg.projecteden.titan.config;

import gg.projecteden.titan.config.annotations.*;
import gg.projecteden.titan.config.annotations.Description;
import gg.projecteden.titan.config.annotations.Group;
import gg.projecteden.titan.config.annotations.Name;
import gg.projecteden.titan.config.annotations.OldConfig;
import gg.projecteden.titan.saturn.SaturnUpdater;
import lombok.Getter;
import lombok.Setter;
Expand All @@ -11,8 +14,7 @@
import java.util.List;
import java.util.stream.Collectors;

import static gg.projecteden.titan.config.annotations.Group.Saturn;
import static gg.projecteden.titan.config.annotations.Group.Utilities;
import static gg.projecteden.titan.config.annotations.Group.*;

@Getter
@Setter
Expand Down Expand Up @@ -89,10 +91,19 @@ public static List<Field> getAll() {
This prevents flickering and has no impact on performance.""")
public static final ConfigItem<Boolean> STOP_ENTITY_CULLING = new ConfigItem<>(true);

@Disabled
@Group(Utilities)
@Group(Backpacks)
@Name("Show Backpack Previews")
@Description("Should Titan render previews of Backpacks\nwhen hovered in your inventory?")
public static final ConfigItem<Boolean> DO_BACKPACK_PREVIEWS = new ConfigItem<>(true);

@Group(Backpacks)
@Name("Backpack Previews Require Shift-Key")
@Description("Should Backpack previews only show\nwhile the shift-key is pressed?")
public static final ConfigItem<Boolean> PREVIEWS_REQUIRE_SHIFT = new ConfigItem<>(true);

@Group(Backpacks)
@Name("Use Backpack Colors")
@Description("Should Backpack previews use the color of the Backpack?")
public static final ConfigItem<Boolean> USE_BACKGROUND_COLORS = new ConfigItem<>(true);

}
4 changes: 1 addition & 3 deletions src/main/java/gg/projecteden/titan/config/ModMenuImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import gg.projecteden.titan.config.annotations.Disabled;
import gg.projecteden.titan.config.annotations.Group;
import gg.projecteden.titan.config.annotations.Name;
import gg.projecteden.titan.saturn.Saturn;
import gg.projecteden.titan.saturn.SaturnUpdater;
import me.shedaniel.clothconfig2.api.ConfigBuilder;
import me.shedaniel.clothconfig2.api.ConfigCategory;
import me.shedaniel.clothconfig2.api.ConfigEntryBuilder;
Expand All @@ -19,7 +17,7 @@
import java.util.LinkedHashMap;
import java.util.Map;

import static gg.projecteden.titan.Utils.camelCase;
import static gg.projecteden.titan.utils.Utils.camelCase;

public class ModMenuImpl implements ModMenuApi {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@

String Saturn = "Saturn";
String Utilities = "Utilities";
String Backpacks = "Backpacks";

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import java.util.concurrent.Executor;

import static gg.projecteden.titan.Titan.MOD_ID;
import static gg.projecteden.titan.Utils.isOnEden;
import static gg.projecteden.titan.config.ConfigItem.*;
import static gg.projecteden.titan.utils.Utils.isOnEden;

public class ResourcePackEvents {

Expand Down
133 changes: 133 additions & 0 deletions src/main/java/gg/projecteden/titan/mixin/HandledScreenMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package gg.projecteden.titan.mixin;

import com.mojang.blaze3d.systems.RenderSystem;
import gg.projecteden.titan.config.ConfigItem;
import gg.projecteden.titan.utils.InventoryOverlay;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.ingame.HandledScreen;
import net.minecraft.client.render.DiffuseLighting;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.inventory.Inventory;
import net.minecraft.inventory.SimpleInventory;
import net.minecraft.item.DyeableHorseArmorItem;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.screen.slot.Slot;
import net.minecraft.util.collection.DefaultedList;
import net.minecraft.util.math.MathHelper;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.awt.*;
import java.util.List;

import static gg.projecteden.titan.utils.Utils.getStoredItems;

@Mixin(HandledScreen.class)
public class HandledScreenMixin {
@Shadow @Nullable protected Slot focusedSlot;

@Inject(method = "drawMouseoverTooltip", at = @At(value = "INVOKE", shift = At.Shift.BEFORE,
target = "Lnet/minecraft/client/gui/DrawContext;drawTooltip(Lnet/minecraft/client/font/TextRenderer;Ljava/util/List;Ljava/util/Optional;II)V"), cancellable = true)
private void onRenderTooltip(DrawContext drawContext, int x, int y, CallbackInfo ci) {
if (!ConfigItem.DO_BACKPACK_PREVIEWS.getValue())
return;

if (ConfigItem.PREVIEWS_REQUIRE_SHIFT.getValue() && !Screen.hasShiftDown())
return;

if (this.focusedSlot != null && this.focusedSlot.hasStack())
onRenderTooltipLast(drawContext, this.focusedSlot.getStack(), x, y, ci);
}

@Unique
private void onRenderTooltipLast(DrawContext context, ItemStack stack, int x, int y, CallbackInfo ci) {
if (!stack.hasNbt())
return;

if (getStoredItems(stack).isEmpty()) {
return;
}

renderItemContentsPreview(stack, x, y, context, ci);
}

@Unique
public void renderItemContentsPreview(ItemStack stack, int baseX, int baseY, DrawContext drawContext, CallbackInfo ci) {
DefaultedList<ItemStack> items = getStoredItems(stack);

InventoryOverlay.InventoryRenderType type = getType(stack);
InventoryOverlay.InventoryProperties props = InventoryOverlay.getInventoryPropsTemp(type, items.size());

int screenWidth = MinecraftClient.getInstance().getWindow().getScaledWidth();
int screenHeight = MinecraftClient.getInstance().getWindow().getScaledHeight();
int height = props.height + 18;
int x = MathHelper.clamp(baseX + 8 , 0, screenWidth - props.width);
int y = MathHelper.clamp(baseY - height, 0, screenHeight - height);

if (baseY - height != y) { // it has been clamped to not go off the screen - cancel rendering the actual tooltip because it renders on top
ci.cancel();
}

if (ConfigItem.USE_BACKGROUND_COLORS.getValue() && stack.getItem() instanceof DyeableHorseArmorItem dyeable) {
if (!dyeable.hasColor(stack))
RenderSystem.setShaderColor(1f, 1f, 1f, 1f);
else {
Color color = new Color(dyeable.getColor(stack));
RenderSystem.setShaderColor(color.getRed() / 255f, color.getGreen() / 255f, color.getBlue() / 255f, 1f);
}
}
else
RenderSystem.setShaderColor(1f, 1f, 1f, 1f);

DiffuseLighting.disableGuiDepthLighting();

MatrixStack matrixStack = RenderSystem.getModelViewStack();
matrixStack.push();
matrixStack.translate(0, 0, 500);
RenderSystem.applyModelViewMatrix();

InventoryOverlay.renderInventoryBackground(type, x, y);

DiffuseLighting.enableGuiDepthLighting();

Inventory inv = getAsInventory(items);
InventoryOverlay.renderInventoryStacks(type, inv, x + props.slotOffsetX, y + props.slotOffsetY, props.slotsPerRow, 0, -1, MinecraftClient.getInstance(), drawContext);

matrixStack.pop();
RenderSystem.applyModelViewMatrix();
}

@Unique
private InventoryOverlay.InventoryRenderType getType(ItemStack stack) {
NbtCompound nbt = stack.getNbt();
if (nbt == null)
return InventoryOverlay.InventoryRenderType.FIXED_27;

if (nbt.contains("BP_TIER_NETHERITE") || nbt.contains("BP_TIER_DIAMOND"))
return InventoryOverlay.InventoryRenderType.FIXED_54;
if (nbt.contains("BP_TIER_GOLD"))
return InventoryOverlay.InventoryRenderType.FIXED_45;
if (nbt.contains("BP_TIER_IRON"))
return InventoryOverlay.InventoryRenderType.FIXED_36;
return InventoryOverlay.InventoryRenderType.FIXED_27;
}

@Unique
private Inventory getAsInventory(List<ItemStack> items) {
SimpleInventory inv = new SimpleInventory(items.size());

for (int slot = 0; slot < items.size(); ++slot)
inv.setStack(slot, items.get(slot));

return inv;
}

}
52 changes: 52 additions & 0 deletions src/main/java/gg/projecteden/titan/mixin/ItemStackMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package gg.projecteden.titan.mixin;

import gg.projecteden.titan.config.ConfigItem;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.item.TooltipContext;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.List;

import static gg.projecteden.titan.utils.Utils.getStoredItems;

@Mixin(ItemStack.class)
public abstract class ItemStackMixin {

@Shadow public abstract boolean hasNbt();

@Shadow public abstract ItemStack copy();

@Unique
private static final Text HOVER = Text.literal("Hold ").formatted(Formatting.DARK_AQUA)
.append(Text.literal("Shift").formatted(Formatting.YELLOW))
.append(" to view contents").formatted(Formatting.DARK_AQUA);

@Inject(at = @At("RETURN"), method = "Lnet/minecraft/item/ItemStack;getTooltip"
+ "(Lnet/minecraft/entity/player/PlayerEntity;Lnet/minecraft/client/item/TooltipContext;)Ljava/util/List;")
private void addBackpackPreviewLore(PlayerEntity player, TooltipContext context, CallbackInfoReturnable<List<Text>> ci) {
if (!ConfigItem.DO_BACKPACK_PREVIEWS.getValue())
return;

if (!ConfigItem.PREVIEWS_REQUIRE_SHIFT.getValue() || Screen.hasShiftDown())
return;

if (!this.hasNbt())
return;

if (getStoredItems(this.copy()).isEmpty())
return;

var tooltip = ci.getReturnValue();
tooltip.add(Text.empty());
tooltip.add(HOVER);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import java.util.Objects;

import static gg.projecteden.titan.Titan.MOD_ID;
import static gg.projecteden.titan.Utils.isOnEden;
import static gg.projecteden.titan.utils.Utils.isOnEden;

public class ServerChannel {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import java.nio.file.Path;
import java.util.List;

import static gg.projecteden.titan.Utils.getGitResponse;
import static gg.projecteden.titan.saturn.Saturn.PATH;
import static gg.projecteden.titan.utils.Utils.getGitResponse;

public enum SaturnUpdater {
GIT {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package gg.projecteden.titan.update;

import gg.projecteden.titan.Utils;
import gg.projecteden.titan.utils.Utils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/gg/projecteden/titan/update/TitanUpdater.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.google.gson.Gson;
import gg.projecteden.titan.Titan;
import gg.projecteden.titan.Utils;
import gg.projecteden.titan.utils.Utils;
import net.minecraft.client.MinecraftClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
Expand All @@ -14,7 +14,6 @@
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class TitanUpdater {

Expand Down
Loading

0 comments on commit d1e744c

Please sign in to comment.