Skip to content

Commit

Permalink
Fix loading drawers with empty item and non-zero amount and improve t…
Browse files Browse the repository at this point in the history
…ooltips a bit
  • Loading branch information
MattiDragon committed Jun 26, 2023
1 parent e0fdf28 commit d49e69b
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 8 deletions.
1 change: 0 additions & 1 deletion changelog/1.4.3+1.19.2.md

This file was deleted.

3 changes: 3 additions & 0 deletions changelog/1.4.3+1.19.4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* Fix automatic compression recipes having the wrong scale
* Fix drawers behaving weirdly when they store a removed item
* Improve tooltips
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.item.TooltipContext;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.BlockItem;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.registry.Registries;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.DirectionProperty;
Expand All @@ -44,6 +44,7 @@
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

Expand All @@ -59,11 +60,32 @@ public CompactingDrawerBlock(Settings settings) {
@Override
public void appendTooltip(ItemStack stack, @Nullable BlockView world, List<Text> tooltip, TooltipContext options) {
var nbt = BlockItem.getBlockEntityNbt(stack);
if (nbt == null) return;
if (nbt == null || !(world instanceof World realWorld)) return;

var list = nbt.getList("items", NbtElement.COMPOUND_TYPE).stream()
.map(NbtCompound.class::cast)
.map(slot -> new ResourceAmount<>(ItemVariant.fromNbt(slot.getCompound("item")), slot.getLong("amount")))
var storageNbt = nbt.getCompound("storage");

if (Screen.hasShiftDown()) {
if (Registries.ITEM.get(Identifier.tryParse(storageNbt.getString("upgrade"))) instanceof UpgradeItem upgrade) {
tooltip.add(upgrade.getName().copy().formatted(Formatting.AQUA));
}

var modifierText = Text.empty()
.append(Text.literal("V").formatted(storageNbt.getBoolean("voiding") ? Formatting.WHITE : Formatting.DARK_GRAY))
.append(Text.literal("L").formatted(storageNbt.getBoolean("locked") ? Formatting.WHITE : Formatting.DARK_GRAY))
.append(Text.literal("H").formatted(storageNbt.getBoolean("hidden") ? Formatting.WHITE : Formatting.DARK_GRAY));
tooltip.add(Text.translatable("tooltip.extended_drawers.modifiers", modifierText).formatted(Formatting.GRAY));
} else {
tooltip.add(Text.translatable("tooltip.extended_drawers.shift_for_modifiers").formatted(Formatting.GRAY));
}
tooltip.add(Text.empty());

var drawer = new CompactingDrawerBlockEntity(BlockPos.ORIGIN, ModBlocks.COMPACTING_DRAWER.getDefaultState());
drawer.setWorld(realWorld);
drawer.readNbt(nbt);
var storage = drawer.storage;

var list = Arrays.stream(storage.getActiveSlots())
.map(slot -> new ResourceAmount<>(slot.getResource(), slot.getAmount()))
.filter(resource -> !resource.resource().isBlank())
.toList();
if (list.isEmpty()) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ public void appendTooltip(ItemStack stack, @Nullable BlockView blockView, List<T
if (list.isEmpty()) return;
boolean shift = Screen.hasShiftDown();

tooltip.add(Text.translatable("tooltip.extended_drawers.shift_for_modifiers").formatted(Formatting.GRAY));
if (!shift) {
tooltip.add(Text.translatable("tooltip.extended_drawers.shift_for_modifiers").formatted(Formatting.GRAY));
tooltip.add(Text.empty());
}

if (!list.stream().allMatch(DrawerSlot::isBlank) || shift)
tooltip.add(Text.translatable("tooltip.extended_drawers.drawer_contents").formatted(Formatting.GRAY));
Expand All @@ -99,6 +102,9 @@ public void appendTooltip(ItemStack stack, @Nullable BlockView blockView, List<T
.append(Text.literal("V").formatted(slot.isVoiding() ? Formatting.WHITE : Formatting.DARK_GRAY))
.append(Text.literal("L").formatted(slot.isLocked() ? Formatting.WHITE : Formatting.DARK_GRAY))
.append(Text.literal("H").formatted(slot.isHidden() ? Formatting.WHITE : Formatting.DARK_GRAY));
if (slot.getUpgrade() != null) {
text.append(" ").append(slot.getUpgrade().getName().copy().formatted(Formatting.AQUA));
}
}
tooltip.add(text.formatted(Formatting.GRAY));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ public void readNbt(NbtCompound nbt) {
DrawerStorage.super.readNbt(nbt);
item = ItemVariant.fromNbt(nbt.getCompound("item"));
amount = nbt.getLong("amount");
if (item.isBlank()) amount = 0; // Avoids dupes with drawers of removed items
updatePending = true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ public void readNbt(NbtCompound nbt) {
DrawerStorage.super.readNbt(nbt);
item = ItemVariant.fromNbt(nbt.getCompound("item"));
amount = nbt.getLong("amount");
if (item.isBlank()) amount = 0; // Avoids dupes with drawers of removed items
}

@Override
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/assets/extended_drawers/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

"tooltip.extended_drawers.shift_for_modifiers": "Press SHIFT for modifiers",
"tooltip.extended_drawers.drawer_contents": "Contains:",
"tooltip.extended_drawers.modifiers": "Modifiers: %s",
"tooltip.extended_drawers.empty": "Empty",
"itemGroup.extended_drawers.main": "Extended Drawers",
"extended_drawer.drawer.upgrade_fail": "Can't change upgrade; too many items.",
Expand Down

0 comments on commit d49e69b

Please sign in to comment.