diff --git a/src/main/java/aeronicamc/mods/mxtune/render/InstrumentOverlayRenderer.java b/src/main/java/aeronicamc/mods/mxtune/render/InstrumentOverlayRenderer.java index 2a375889..307dd27f 100644 --- a/src/main/java/aeronicamc/mods/mxtune/render/InstrumentOverlayRenderer.java +++ b/src/main/java/aeronicamc/mods/mxtune/render/InstrumentOverlayRenderer.java @@ -1,6 +1,6 @@ package aeronicamc.mods.mxtune.render; -import aeronicamc.mods.mxtune.MXTune; +import aeronicamc.mods.mxtune.init.ModItems; import aeronicamc.mods.mxtune.sound.ClientAudio; import aeronicamc.mods.mxtune.util.IInstrument; import aeronicamc.mods.mxtune.util.SheetMusicHelper; @@ -12,9 +12,11 @@ import net.minecraft.util.text.TextFormatting; import net.minecraftforge.client.event.RenderGameOverlayEvent; +import static aeronicamc.mods.mxtune.render.RenderHelper.getPlayer; import static aeronicamc.mods.mxtune.render.RenderHelper.mc; public class InstrumentOverlayRenderer { + private static final ItemStack PLACARD_ITEM = new ItemStack(ModItems.PLACARD_ITEM.get()); public static void render(ItemStack itemStack, RenderGameOverlayEvent.Post event) { // Display basic info about the instrument and tune. Optionally, displays some debug info depending on MXTune.isDevEnv flag. @@ -26,25 +28,30 @@ public static void render(ItemStack itemStack, RenderGameOverlayEvent.Post event final ITextComponent infoText = new StringTextComponent("").append(SheetMusicHelper.getFormattedMusicDuration(sheetMusic)) .append(String.format(" %s %s", mc.getSoundManager().getDebugString(), ClientAudio.getDebugString())).withStyle(TextFormatting.WHITE); - final int offset = Math.max(Math.max(mc.font.width(titleText), mc.font.width(MXTune.isDevEnv() ? infoText : extraText)) + 40, RenderHelper.WIDTH); + final int offset = Math.max(Math.max(mc.font.width(titleText), mc.font.width(isPlacardInHotBar() ? infoText : extraText)) + 40, RenderHelper.WIDTH); final MatrixStack pPoseStack = event.getMatrixStack(); - mc.getTextureManager().bind(RenderHelper.TEXTURE); + RenderSystem.color3f(1.0F, 1.0F, 1.0F); RenderHelper.blit(pPoseStack, 0, 0, 0, 0, RenderHelper.WIDTH, RenderHelper.HEIGHT); RenderHelper.blit(pPoseStack, ((offset - RenderHelper.WIDTH)/2) + 5, 0, 10, 0, RenderHelper.WIDTH -10, RenderHelper.HEIGHT); RenderHelper.blit(pPoseStack, offset - RenderHelper.WIDTH + 10, 0, 10, 0, RenderHelper.WIDTH, RenderHelper.HEIGHT); + mc.getItemRenderer().renderAndDecorateItem(itemStack, 8, 8); mc.font.draw(pPoseStack, titleText, 30.0F, 7.0F, -11534256); - mc.font.draw(pPoseStack, MXTune.isDevEnv() ? infoText : extraText, 30.0F, 17.0F, -11534256); - mc.getItemRenderer().renderAndDecorateItem(itemStack, 8, 8); + mc.font.draw(pPoseStack, isPlacardInHotBar() ? infoText : extraText, 30.0F, 17.0F, -11534256); - if (MXTune.isDevEnv()) { - final int[] posY = new int[1]; - posY[0] = 25; + if (isPlacardInHotBar()) { + final int[] posY = { 0 }; + posY[0] = (25); ClientAudio.getAudioData() .forEach(audioData -> mc.font.drawShadow(pPoseStack, audioData.getInfo(), 5, posY[0] += 10, -11534256)); } } } + + private static boolean isPlacardInHotBar() { + int slot = getPlayer().inventory.findSlotMatchingItem(PLACARD_ITEM); + return slot >= 0 && slot < 9; + } } diff --git a/src/main/java/aeronicamc/mods/mxtune/render/RenderEvents.java b/src/main/java/aeronicamc/mods/mxtune/render/RenderEvents.java index 663c5192..c530efec 100644 --- a/src/main/java/aeronicamc/mods/mxtune/render/RenderEvents.java +++ b/src/main/java/aeronicamc/mods/mxtune/render/RenderEvents.java @@ -39,6 +39,7 @@ import java.util.List; import static aeronicamc.mods.mxtune.render.ModRenderType.*; +import static aeronicamc.mods.mxtune.render.RenderHelper.getPlayer; import static aeronicamc.mods.mxtune.render.RenderHelper.mc; @SuppressWarnings("deprecation") @@ -52,7 +53,7 @@ public static void event(ItemTooltipEvent event) { if (event.getItemStack().getItem().equals(ModBlocks.MUSIC_BLOCK.get().asItem())) event.getToolTip().add(new TranslationTextComponent("tooltip.mxtune.block_music.help").withStyle(TextFormatting.YELLOW)); - else if (event.getItemStack().getItem().equals(ModItems.MUSIC_PAPER.get())) + if (event.getItemStack().getItem().equals(ModItems.MUSIC_PAPER.get())) event.getToolTip().add(new TranslationTextComponent("tooltip.mxtune.music_paper.help").withStyle(TextFormatting.YELLOW)); } @@ -60,8 +61,6 @@ else if (event.getItemStack().getItem().equals(ModItems.MUSIC_PAPER.get())) @SubscribeEvent public static void event(DrawHighlightEvent.HighlightBlock event) { - if (mc.player == null ) - return; if (mc.options.renderDebug) return; final BlockRayTraceResult blockRayTraceResult = event.getTarget(); final IRenderTypeBuffer renderTypeBuffer = event.getBuffers(); @@ -69,13 +68,13 @@ public static void event(DrawHighlightEvent.HighlightBlock event) final MatrixStack matrixStack = event.getMatrix(); final Vector3d camera = activeRenderInfo.getPosition(); - final World level = mc.player.level; + final World level = getPlayer().level; final BlockPos blockPos = blockRayTraceResult.getBlockPos(); final BlockState blockState = level.getBlockState(blockRayTraceResult.getBlockPos()); // Highlight blocks for tool use - if (mc.player.inventory.getSelected().getItem() instanceof MusicVenueToolItem) + if (getPlayer().inventory.getSelected().getItem() instanceof MusicVenueToolItem) { if (event.isCancelable()) event.setCanceled(true); @@ -87,18 +86,18 @@ public static void event(DrawHighlightEvent.HighlightBlock event) } // Show the pre-placement info panel outline. - if (mc.player.inventory.getSelected().getItem() instanceof MusicVenueInfoItem) + if (getPlayer().inventory.getSelected().getItem() instanceof MusicVenueInfoItem) { if (event.isCancelable()) event.setCanceled(true); if (!blockState.isAir(level, blockPos) && level.getWorldBorder().isWithinBounds(blockPos)) { - final ItemUseContext useContext = new ItemUseContext(mc.player, Hand.MAIN_HAND, blockRayTraceResult); + final ItemUseContext useContext = new ItemUseContext(getPlayer(), Hand.MAIN_HAND, blockRayTraceResult); final BlockPos clickedPos = useContext.getClickedPos(); final Direction facing = useContext.getClickedFace(); final BlockPos placementPos = clickedPos.relative(facing); - if (MusicVenueInfoItem.mayPlace(mc.player, facing, useContext.getItemInHand(), placementPos)) + if (MusicVenueInfoItem.mayPlace(getPlayer(), facing, useContext.getItemInHand(), placementPos)) { final HangingEntity infoEntity = new MusicVenueInfoEntity(useContext.getLevel(), placementPos, facing); if (infoEntity.survives()) @@ -116,10 +115,8 @@ public static void event(DrawHighlightEvent.HighlightBlock event) @SubscribeEvent public static void event(DrawHighlightEvent.HighlightEntity event) { - if (mc.player == null ) - return; if (mc.options.renderDebug) return; - if (!(mc.player.inventory.getSelected().getItem() instanceof MusicVenueToolItem)) return; + if (!(getPlayer().inventory.getSelected().getItem() instanceof MusicVenueToolItem)) return; if (event.isCancelable()) event.setCanceled(true); final EntityRayTraceResult entityRayTraceResult = event.getTarget(); @@ -136,19 +133,19 @@ public static void event(DrawHighlightEvent.HighlightEntity event) @SubscribeEvent public static void event(RenderGameOverlayEvent.Post event) { - if (mc.player == null || mc.options.renderDebug ) return; - final ItemStack itemStack = mc.player.inventory.getSelected(); + if (mc.options.renderDebug ) return; + final ItemStack itemStack = getPlayer().inventory.getSelected(); InstrumentOverlayRenderer.render(itemStack, event); - MusicVenueToolOverlayRenderer.render(mc.player, itemStack, event); + MusicVenueToolOverlayRenderer.render(getPlayer(), itemStack, event); } static void renderGroupStatusPlacard(MatrixStack pMatrixStack, IRenderTypeBuffer.Impl pBuffer, ActiveRenderInfo pActiveRenderInfo, float pPartialTicks, ClippingHelper pClippingHelper) { - if (mc.player != null && mc.level != null && GroupClient.hasGroups()) + if (GroupClient.hasGroups()) { ItemStack placardStack = new ItemStack(ModItems.PLACARD_ITEM.get()); Vector3d cam = pActiveRenderInfo.getPosition(); - List nearLivingEntities = mc.level.getEntities(null, mc.player.getBoundingBox().inflate(48)); + List nearLivingEntities = getPlayer().level.getEntities(null, getPlayer().getBoundingBox().inflate(48)); if (!nearLivingEntities.isEmpty()) { nearLivingEntities.stream().filter(p -> pClippingHelper.isVisible(p.getBoundingBoxForCulling())).forEach(livingEntity -> { diff --git a/src/main/java/aeronicamc/mods/mxtune/render/RenderHelper.java b/src/main/java/aeronicamc/mods/mxtune/render/RenderHelper.java index fd64bc6e..0575f00a 100644 --- a/src/main/java/aeronicamc/mods/mxtune/render/RenderHelper.java +++ b/src/main/java/aeronicamc/mods/mxtune/render/RenderHelper.java @@ -4,6 +4,7 @@ import com.mojang.blaze3d.vertex.IVertexBuilder; import net.minecraft.block.BlockState; import net.minecraft.client.Minecraft; +import net.minecraft.client.entity.player.ClientPlayerEntity; import net.minecraft.client.gui.AbstractGui; import net.minecraft.client.gui.FontRenderer; import net.minecraft.client.renderer.ActiveRenderInfo; @@ -22,6 +23,8 @@ import net.minecraft.util.text.ITextComponent; import net.minecraft.world.World; +import java.util.Objects; + @SuppressWarnings("unused") public class RenderHelper { @@ -30,6 +33,10 @@ public class RenderHelper static final int HEIGHT = 32; static final Minecraft mc = Minecraft.getInstance(); + static ClientPlayerEntity getPlayer() { + return Objects.requireNonNull(mc.player); + } + private RenderHelper() { /* NOP */ } /**