Skip to content

Commit

Permalink
update Some small code elements
Browse files Browse the repository at this point in the history
  • Loading branch information
sakura-ryoko committed Nov 22, 2024
1 parent d48604d commit 528b6ef
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 14 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ author = masa
mod_file_name = malilib-fabric

# Current mod version
mod_version = 0.22.0-sakura.7
mod_version = 0.22.0

# Minecraft, Fabric Loader and API and mappings versions
minecraft_version_out = 1.21.3
Expand Down
168 changes: 159 additions & 9 deletions src/main/java/fi/dy/masa/malilib/render/InventoryOverlay.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.mob.PiglinEntity;
import net.minecraft.entity.passive.AbstractHorseEntity;
import net.minecraft.entity.passive.LlamaEntity;
import net.minecraft.entity.passive.VillagerEntity;
import net.minecraft.entity.passive.WolfEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.entity.vehicle.AbstractChestBoatEntity;
Expand Down Expand Up @@ -64,6 +66,13 @@ public class InventoryOverlay
public static final Identifier TEXTURE_EMPTY_SHIELD = Identifier.ofVanilla("item/empty_armor_slot_shield");
public static final Identifier TEXTURE_LOCKED_SLOT = Identifier.ofVanilla("container/crafter/disabled_slot");

// Additional Empty Slot Textures
public static final Identifier TEXTURE_EMPTY_HORSE_ARMOR = Identifier.ofVanilla("container/slot/horse_armor");
public static final Identifier TEXTURE_EMPTY_LLAMA_ARMOR = Identifier.ofVanilla("container/slot/llama_armor");
public static final Identifier TEXTURE_EMPTY_SADDLE = Identifier.ofVanilla("container/slot/saddle");
public static final Identifier TEXTURE_EMPTY_BREWER_FUEL = Identifier.ofVanilla("container/slot/brewing_fuel");
public static final Identifier TEXTURE_EMPTY_POTION = Identifier.ofVanilla("container/slot/potion");

private static final EquipmentSlot[] VALID_EQUIPMENT_SLOTS = new EquipmentSlot[] { EquipmentSlot.HEAD, EquipmentSlot.CHEST, EquipmentSlot.LEGS, EquipmentSlot.FEET };
public static final InventoryProperties INV_PROPS_TEMP = new InventoryProperties();

Expand All @@ -73,6 +82,12 @@ public class InventoryOverlay
Identifier.ofVanilla("item/empty_armor_slot_leggings"),
Identifier.ofVanilla("item/empty_armor_slot_chestplate"),
Identifier.ofVanilla("item/empty_armor_slot_helmet")
/*
Identifier.ofVanilla("container/slot/boots"),
Identifier.ofVanilla("container/slot/leggings"),
Identifier.ofVanilla("container/slot/chestplate"),
Identifier.ofVanilla("container/slot/helmet")
*/
};

private static ItemStack hoveredStack = null;
Expand Down Expand Up @@ -213,6 +228,95 @@ public static void renderInventoryBackground54(int x, int y, BufferBuilder buffe
RenderUtils.drawTexturedRectBatched(x + 7, y + 7, 7, 17, 162, 108, buffer); // middle
}

public static void renderInventoryBackgroundSlots(InventoryRenderType type, Inventory inv, int x, int y, DrawContext drawContext)
{
if (type == InventoryRenderType.BREWING_STAND)
{
renderBrewerBackgroundSlots(inv, x, y, drawContext);
}
else if (type == InventoryRenderType.HORSE)
{
renderHorseArmorBackgroundSlots(inv, x, y, drawContext);
}
else if (type == InventoryRenderType.LLAMA)
{
renderLlamaArmorBackgroundSlots(inv, x, y, drawContext);
}
else if (type == InventoryRenderType.WOLF)
{
renderWolfArmorBackgroundSlots(inv, x, y, drawContext);
}
}

public static void renderBrewerBackgroundSlots(Inventory inv, int x, int y, DrawContext drawContext)
{
renderBrewerBackgroundSlots(inv, x, y, 0.9f, drawContext, 0, 0);
}

public static void renderBrewerBackgroundSlots(Inventory inv, int x, int y, float scale, DrawContext drawContext, double mouseX, double mouseY)
{
if (inv.getStack(0).isEmpty())
{
renderBackgroundSlotAt(x + 47, y + 42, scale, TEXTURE_EMPTY_POTION, drawContext, mouseX, mouseY);
}
if (inv.getStack(1).isEmpty())
{
renderBackgroundSlotAt(x + 70, y + 49, scale, TEXTURE_EMPTY_POTION, drawContext, mouseX, mouseY);
}
if (inv.getStack(2).isEmpty())
{
renderBackgroundSlotAt(x + 93, y + 42, scale, TEXTURE_EMPTY_POTION, drawContext, mouseX, mouseY);
}
if (inv.getStack(4).isEmpty())
{
renderBackgroundSlotAt(x + 8, y + 8, scale, TEXTURE_EMPTY_BREWER_FUEL, drawContext, mouseX, mouseY);
}
}

public static void renderHorseArmorBackgroundSlots(Inventory inv, int x, int y, DrawContext drawContext)
{
renderHorseArmorBackgroundSlots(inv, x, y, 0.9f, drawContext, 0, 0);
}

public static void renderHorseArmorBackgroundSlots(Inventory inv, int x, int y, float scale, DrawContext drawContext, double mouseX, double mouseY)
{
if (inv.getStack(0).isEmpty())
{
renderBackgroundSlotAt(x, y, scale, TEXTURE_EMPTY_HORSE_ARMOR, drawContext, mouseX, mouseY);
}

if (inv.getStack(1).isEmpty())
{
renderBackgroundSlotAt(x, y + 18, scale, TEXTURE_EMPTY_SADDLE, drawContext, mouseX, mouseY);
}
}

public static void renderLlamaArmorBackgroundSlots(Inventory inv, int x, int y, DrawContext drawContext)
{
renderLlamaArmorBackgroundSlots(inv, x, y, 0.9f, drawContext, 0, 0);
}

public static void renderLlamaArmorBackgroundSlots(Inventory inv, int x, int y, float scale, DrawContext drawContext, double mouseX, double mouseY)
{
if (inv.getStack(0).isEmpty())
{
renderBackgroundSlotAt(x, y, scale, TEXTURE_EMPTY_LLAMA_ARMOR, drawContext, mouseX, mouseY);
}
}

public static void renderWolfArmorBackgroundSlots(Inventory inv, int x, int y, DrawContext drawContext)
{
renderWolfArmorBackgroundSlots(inv, x, y, 0.9f, drawContext, 0, 0);
}

public static void renderWolfArmorBackgroundSlots(Inventory inv, int x, int y, float scale, DrawContext drawContext, double mouseX, double mouseY)
{
if (inv.getStack(0).isEmpty())
{
renderBackgroundSlotAt(x, y, scale, TEXTURE_EMPTY_HORSE_ARMOR, drawContext, mouseX, mouseY);
}
}

public static void renderEquipmentOverlayBackground(int x, int y, LivingEntity entity, DrawContext drawContext)
{
RenderUtils.color(1f, 1f, 1f, 1f);
Expand Down Expand Up @@ -317,7 +421,15 @@ else if (inv instanceof PlayerInventory)
}
else if (inv instanceof IEntityOwnedInventory inventory)
{
if (inventory.malilib$getEntityOwner() instanceof AbstractHorseEntity)
if (inventory.malilib$getEntityOwner() instanceof LlamaEntity)
{
return InventoryRenderType.LLAMA;
}
else if (inventory.malilib$getEntityOwner() instanceof WolfEntity)
{
return InventoryRenderType.WOLF;
}
else if (inventory.malilib$getEntityOwner() instanceof AbstractHorseEntity)
{
return InventoryRenderType.HORSE;
}
Expand Down Expand Up @@ -470,16 +582,23 @@ else if (entityType.equals(EntityType.HOPPER_MINECART))
return InventoryRenderType.HOPPER;
}
else if (entityType.equals(EntityType.HORSE) ||
entityType.equals(EntityType.DONKEY) ||
entityType.equals(EntityType.MULE) ||
entityType.equals(EntityType.CAMEL) ||
entityType.equals(EntityType.LLAMA) ||
entityType.equals(EntityType.TRADER_LLAMA) ||
entityType.equals(EntityType.SKELETON_HORSE) ||
entityType.equals(EntityType.ZOMBIE_HORSE))
entityType.equals(EntityType.DONKEY) ||
entityType.equals(EntityType.MULE) ||
entityType.equals(EntityType.CAMEL) ||
entityType.equals(EntityType.SKELETON_HORSE) ||
entityType.equals(EntityType.ZOMBIE_HORSE))
{
return InventoryRenderType.HORSE;
}
else if (entityType.equals(EntityType.LLAMA) ||
entityType.equals(EntityType.TRADER_LLAMA))
{
return InventoryRenderType.LLAMA;
}
else if (entityType.equals(EntityType.WOLF))
{
return InventoryRenderType.WOLF;
}
else if (entityType.equals(EntityType.VILLAGER) ||
entityType.equals(EntityType.ALLAY) ||
entityType.equals(EntityType.PILLAGER) ||
Expand Down Expand Up @@ -582,7 +701,7 @@ else if (type == InventoryRenderType.CRAFTER || type == InventoryRenderType.DISP
INV_PROPS_TEMP.width = 68;
INV_PROPS_TEMP.height = 68;
}
else if (type == InventoryRenderType.HORSE)
else if (type == InventoryRenderType.HORSE || type == InventoryRenderType.LLAMA || type == InventoryRenderType.WOLF)
{
INV_PROPS_TEMP.slotsPerRow = Math.max(1, totalSlots / 3);
INV_PROPS_TEMP.slotOffsetX = 8;
Expand Down Expand Up @@ -918,6 +1037,35 @@ public static void renderLockedSlotAt(float x, float y, float scale, DrawContext
}
}

public static void renderBackgroundSlotAt(float x, float y, Identifier texture, DrawContext drawContext)
{
renderBackgroundSlotAt(x, y, 0.9f, texture, drawContext, 0, 0);
}

public static void renderBackgroundSlotAt(float x, float y, float scale, Identifier texture, DrawContext drawContext, double mouseX, double mouseY)
{
MatrixStack matrixStack = drawContext.getMatrices();
int color = -1;

matrixStack.push();
matrixStack.translate(x, y, 0.f);
matrixStack.scale(scale, scale, 1);

RenderUtils.enableDiffuseLightingGui3D();
RenderUtils.color(1f, 1f, 1f, 1f);

drawContext.drawGuiTexture(RenderLayer::getGuiTextured, texture, 0, 0, 18, 18, color);
RenderUtils.forceDraw(drawContext);

RenderUtils.color(1f, 1f, 1f, 1f);
matrixStack.pop();

if (mouseX >= x && mouseX < x + 16 * scale && mouseY >= y && mouseY < y + 16 * scale)
{
hoveredStack = null;
}
}

public static void renderStackToolTip(int x, int y, ItemStack stack, MinecraftClient mc, DrawContext drawContext)
{
List<Text> list = stack.getTooltip(Item.TooltipContext.DEFAULT, mc.player, mc.options.advancedItemTooltips ? TooltipType.ADVANCED : TooltipType.BASIC);
Expand Down Expand Up @@ -956,6 +1104,8 @@ public enum InventoryRenderType
FURNACE,
HOPPER,
HORSE,
LLAMA,
WOLF,
FIXED_27,
FIXED_54,
VILLAGER,
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/fi/dy/masa/malilib/render/RenderUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -1371,6 +1371,7 @@ public static void renderShulkerBoxPreview(ItemStack stack, int baseX, int baseY

enableDiffuseLightingGui3D();

// TODO 1.21.4+
/*
if (type == InventoryOverlay.InventoryRenderType.BREWING_STAND)
{
Expand Down Expand Up @@ -1461,6 +1462,10 @@ public static void renderNbtItemsPreview(ItemStack stackIn, @Nonnull NbtCompound
{
if (InventoryUtils.hasNbtItems(itemsTag))
{
if (mc().world == null)
{
return;
}
DefaultedList<ItemStack> items = InventoryUtils.getNbtItems(itemsTag, -1, mc().world.getRegistryManager());

if (items.size() == 0)
Expand All @@ -1477,6 +1482,9 @@ public static void renderNbtItemsPreview(ItemStack stackIn, @Nonnull NbtCompound
int x = MathHelper.clamp(baseX + 8, 0, screenWidth - props.width);
int y = MathHelper.clamp(baseY - height, 0, screenHeight - height);

// Mask items behind the shulker box display, trying to minimize the sharp corners
drawTexturedRect(GuiBase.BG_TEXTURE, x + 1, y + 1, 0, 0, props.width - 2, props.height - 2, drawContext);

color(1f, 1f, 1f, 1f);
disableDiffuseLighting();

Expand Down
19 changes: 15 additions & 4 deletions src/main/java/fi/dy/masa/malilib/util/BlockUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.FluidBlock;
import net.minecraft.block.entity.BeehiveBlockEntity;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.block.entity.CrafterBlockEntity;
import net.minecraft.block.entity.SignText;
import net.minecraft.block.entity.*;
import net.minecraft.block.enums.Orientation;
import net.minecraft.component.type.ProfileComponent;
import net.minecraft.entity.effect.StatusEffect;
Expand Down Expand Up @@ -620,4 +617,18 @@ public static RegistryEntry<Block> getBlockEntry(Identifier id, @Nonnull Dynamic
return null;
}
}

/**
* Write a Block Entity's Data to an ItemStack (Removed from Vanilla, why?)
*
* @param stack
* @param be
* @param registry
*/
public static void setStackNbt(@Nonnull ItemStack stack, @Nonnull BlockEntity be, @Nonnull DynamicRegistryManager registry)
{
NbtCompound nbt = be.createComponentlessNbt(registry);
BlockItem.setBlockEntityData(stack, be.getType(), nbt);
stack.applyComponentsFrom(be.createComponentMap());
}
}

0 comments on commit 528b6ef

Please sign in to comment.