Skip to content

Commit

Permalink
Glass item frame attachment logic for new sign aligmnents
Browse files Browse the repository at this point in the history
  • Loading branch information
Vazkii committed Dec 4, 2023
1 parent 63c7dfd commit 97d5c92
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
package org.violetmoon.quark.content.building.client.render.entity;

import java.util.List;

import org.jetbrains.annotations.NotNull;
import org.violetmoon.quark.base.Quark;
import org.violetmoon.quark.content.building.entity.GlassItemFrame;
import org.violetmoon.quark.content.building.entity.GlassItemFrame.SignAttachment;
import org.violetmoon.quark.content.building.module.GlassItemFrameModule;

import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.datafixers.util.Pair;
import com.mojang.math.Axis;

import net.minecraft.client.Minecraft;
import net.minecraft.client.model.geom.ModelLayers;
import net.minecraft.client.model.geom.ModelPart;
Expand All @@ -25,20 +34,19 @@
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.inventory.InventoryMenu;
import net.minecraft.world.item.*;
import net.minecraft.world.item.BannerItem;
import net.minecraft.world.item.DyeColor;
import net.minecraft.world.item.ItemDisplayContext;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.MapItem;
import net.minecraft.world.item.ShieldItem;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.entity.BannerBlockEntity;
import net.minecraft.world.level.block.entity.BannerPattern;
import net.minecraft.world.level.saveddata.maps.MapItemSavedData;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.client.event.RenderItemInFrameEvent;
import net.minecraftforge.common.MinecraftForge;
import org.jetbrains.annotations.NotNull;
import org.violetmoon.quark.base.Quark;
import org.violetmoon.quark.content.building.entity.GlassItemFrame;
import org.violetmoon.quark.content.building.module.GlassItemFrameModule;

import java.util.List;

/**
* @author WireSegal
Expand Down Expand Up @@ -132,23 +140,47 @@ protected void renderItemStack(GlassItemFrame itemFrame, PoseStack matrix, Multi
matrix.pushPose();
MapItemSavedData mapdata = MapItem.getSavedData(stack, itemFrame.level());

sign: if(itemFrame.isOnSign()) {

if(itemFrame.isOnSign()) {
SignAttachment attach = itemFrame.getSignAttachment();

Direction ourDirection = itemFrame.getDirection().getOpposite();

int signRotation = itemFrame.getOnSignRotation();
Direction signDirection = SIGN_DIRECTIONS.get(signRotation / 4);
if(signRotation % 4 == 0 ? (signDirection != ourDirection) : (signDirection.getOpposite() == ourDirection))
break sign;


int ourRotation = SIGN_DIRECTIONS.indexOf(ourDirection) * 4;
int rotation = signRotation - ourRotation;
float angle = -rotation * 22.5F;

matrix.translate(0, 0.35, 0.8);
matrix.scale(0.4F, 0.4F, 0.4F);
matrix.translate(0, 0, 0.5);
float scale = 0.32F;

switch(attach) {
case STANDING_BEHIND:
angle += 180F;
break;
case WALL_SIGN:
angle = 0;
matrix.translate(0.0, -0.3, 0.45);
break;
case HANGING_FROM_WALL:
angle = 0;
matrix.translate(0.0, -0.52, -0.01);

default: break;
}

matrix.translate(0, 0.35, 0.98);
matrix.scale(scale, scale, scale);
matrix.mulPose(Axis.YP.rotationDegrees(angle));

switch(attach) {
case HANGING_IN_FRONT:
matrix.translate(0.0, -0.52 / scale, -0.075);
break;
case HANGING_BEHIND:
matrix.translate(0.0, -0.52 / scale, 0.3);
break;

default: break;
}

matrix.translate(0, 0, -0.5);
matrix.translate(0, 0, -0.085);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@
import net.minecraft.world.item.*;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.StandingSignBlock;
import net.minecraft.world.level.block.WallHangingSignBlock;
import net.minecraft.world.level.block.WallSignBlock;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.saveddata.maps.MapItemSavedData;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.HitResult;
Expand All @@ -35,6 +38,8 @@

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.UUID;

public class GlassItemFrame extends ItemFrame implements IEntityAdditionalSpawnData {
Expand All @@ -52,7 +57,8 @@ public enum SignAttachment {
NOT_ATTACHED,
STANDING_IN_FRONT,
STANDING_BEHIND,
WALL,
WALL_SIGN,
HANGING_FROM_WALL,
HANGING_IN_FRONT,
HANGING_BEHIND
}
Expand Down Expand Up @@ -124,9 +130,44 @@ private void updateIsOnSign() {

if(this.direction.getAxis() != Direction.Axis.Y){
BlockState back = level().getBlockState(getBehindPos());
if(back.is(BlockTags.STANDING_SIGNS)) {
attachment = SignAttachment.STANDING_IN_FRONT;
onSignRotation = back.getValue(StandingSignBlock.ROTATION);
boolean standing = back.is(BlockTags.STANDING_SIGNS);
boolean hangingCeil = back.is(BlockTags.CEILING_HANGING_SIGNS);

if(standing || hangingCeil) {
onSignRotation = back.getValue(BlockStateProperties.ROTATION_16);

double[] angles = new double[] { 0, 0, 0, 180, -90, 90 };
double ourRotation = angles[getDirection().getOpposite().ordinal()];
double signRotation = back.getValue(BlockStateProperties.ROTATION_16) / 16.0 * 360.0;

if(signRotation > 180)
signRotation = signRotation - 360;

double diff = ourRotation - signRotation;
double absDiff = Math.abs(diff);
if(diff < 0)
absDiff -= 0.01; // hacky countermeasure to prevent two frames equidistant in different directions from both attaching

if(absDiff < 45)
attachment = (standing ? SignAttachment.STANDING_IN_FRONT : SignAttachment.HANGING_IN_FRONT);
else if(absDiff >= 135 && absDiff < 225)
attachment = (standing ? SignAttachment.STANDING_BEHIND : SignAttachment.HANGING_BEHIND);

return;
}

if(back.is(BlockTags.WALL_SIGNS)) {
Direction signDir = back.getValue(WallSignBlock.FACING);
if(signDir == getDirection())
attachment = SignAttachment.WALL_SIGN;

return;
}

if(back.is(BlockTags.WALL_HANGING_SIGNS)) {
Direction signDir = back.getValue(WallHangingSignBlock.FACING);
if(signDir == getDirection() || signDir == getDirection().getOpposite())
attachment = SignAttachment.HANGING_FROM_WALL;
}
}
}
Expand Down

0 comments on commit 97d5c92

Please sign in to comment.