Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Account for backpack item in curios slot #728

Open
wants to merge 6 commits into
base: 1.21.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ dependencies {
implementation "curse.maven:sophisticated-core-618298:6119300"

// Curios testing
// runtimeOnly "top.theillusivec4.curios:curios-neoforge:9.2.2+1.21.1"
// compileOnly "top.theillusivec4.curios:curios-neoforge:9.2.2+1.21.1:api"
// runtimeOnly "top.theillusivec4.curios:curios-neoforge:9.2.2+1.21.1"
compileOnly "top.theillusivec4.curios:curios-neoforge:9.2.2+1.21.1:api"

testImplementation 'org.junit.jupiter:junit-jupiter:5.7.1'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class Compat {
public static final String IRIS = "iris";
public static final String COSMETIC_ARMOR_REWORKED = "cosmeticarmorreworked";
public static final String SOPHISTICATED_BACKPACKS = "sophisticatedbackpacks";
public static final String CURIOS = "curios";

private static final Map<String, List<String>> ALIAS = Map.of(
IRIS, List.of("oculus"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import by.dragonsurvivalteam.dragonsurvival.common.capability.DragonStateHandler;
import by.dragonsurvivalteam.dragonsurvival.common.capability.DragonStateProvider;
import by.dragonsurvivalteam.dragonsurvival.common.entity.DragonEntity;
import by.dragonsurvivalteam.dragonsurvival.compat.Compat;
import by.dragonsurvivalteam.dragonsurvival.config.obj.ConfigOption;
import by.dragonsurvivalteam.dragonsurvival.config.obj.ConfigSide;
import by.dragonsurvivalteam.dragonsurvival.registry.datagen.Translation;
Expand All @@ -11,9 +12,11 @@
import com.mojang.blaze3d.vertex.VertexConsumer;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.phys.Vec3;
import net.p3pp3rf1y.sophisticatedbackpacks.backpack.BackpackItem;
import net.p3pp3rf1y.sophisticatedbackpacks.backpack.wrapper.BackpackWrapper;
import net.p3pp3rf1y.sophisticatedbackpacks.backpack.wrapper.IBackpackWrapper;
import net.p3pp3rf1y.sophisticatedbackpacks.client.render.BackpackModelManager;
Expand All @@ -24,7 +27,11 @@
import software.bernie.geckolib.cache.object.GeoBone;
import software.bernie.geckolib.renderer.GeoEntityRenderer;
import software.bernie.geckolib.renderer.layer.GeoRenderLayer;
import top.theillusivec4.curios.api.CuriosApi;
import top.theillusivec4.curios.api.SlotResult;

import java.util.List;
import java.util.Optional;


public class DragonBackpackRenderLayer extends GeoRenderLayer<DragonEntity> {
Expand All @@ -33,9 +40,13 @@ public class DragonBackpackRenderLayer extends GeoRenderLayer<DragonEntity> {
@ConfigOption(side = ConfigSide.CLIENT, category = "rendering", key = "render_backpack")
public static Boolean renderBackpack = true;

private final boolean isCurioLoaded;

public DragonBackpackRenderLayer(GeoEntityRenderer<DragonEntity> renderer) {

super(renderer);

isCurioLoaded = Compat.isModLoaded(Compat.CURIOS);
}

@Override
Expand All @@ -51,57 +62,118 @@ public void renderForBone(PoseStack poseStack, DragonEntity animatable, GeoBone
if(player == null) return;

DragonStateHandler handler = DragonStateProvider.getData(player);

if(!handler.isDragon()) return;

Optional<PlayerInventoryProvider.RenderInfo> backpackRenderInfo = getRenderInfo(player);

PlayerInventoryProvider.get().getBackpackFromRendered(player).ifPresent(backpackRenderInfo -> {
if(backpackRenderInfo.isEmpty()) return;

poseStack.pushPose();
poseStack.pushPose();

ItemStack backpack = backpackRenderInfo.getBackpack();
IBackpackWrapper wrapper = BackpackWrapper.fromStack(backpack);
ItemStack backpack = backpackRenderInfo.get().getBackpack();
IBackpackWrapper wrapper = BackpackWrapper.fromStack(backpack);

int clothColor = wrapper.getMainColor();
int borderColor = wrapper.getAccentColor();
IBackpackModel model = BackpackModelManager.getBackpackModel(backpack.getItem());
int clothColor = wrapper.getMainColor();
int borderColor = wrapper.getAccentColor();
IBackpackModel model = BackpackModelManager.getBackpackModel(backpack.getItem());

Vec3 posOffset = new Vec3(bone.getPivotX(), bone.getPivotY(), bone.getPivotZ());

Vec3 rotOffset = Vec3.ZERO;
Vec3 posOffset = new Vec3(bone.getPivotX(), bone.getPivotY(), bone.getPivotZ());

Vec3 scale = new Vec3(1, 1, 1);

if(handler.body().value().backpackOffsets().isPresent()) {
DragonBody.BackpackOffsets backpackOffsets = handler.body().value().backpackOffsets().get();
Vec3 rotOffset = Vec3.ZERO;

scale = backpackOffsets.scale();
posOffset = posOffset.add(backpackOffsets.posOffset());
rotOffset = backpackOffsets.rotOffset();
}
Vec3 scale = new Vec3(1, 1, 1);


if(handler.body().value().backpackOffsets().isPresent()) {
DragonBody.BackpackOffsets backpackOffsets = handler.body().value().backpackOffsets().get();

transformModel(poseStack, posOffset, rotOffset, scale);
scale = backpackOffsets.scale();
posOffset = posOffset.add(backpackOffsets.posOffset());
rotOffset = backpackOffsets.rotOffset();
}

model.render(null, player, poseStack, bufferSource, packedLight, clothColor, borderColor, backpack.getItem(), wrapper.getRenderInfo());
poseStack.popPose();

});
transformModel(poseStack, posOffset, rotOffset, scale);

model.render(null, player, poseStack, bufferSource, packedLight, clothColor, borderColor, backpack.getItem(), wrapper.getRenderInfo());
poseStack.popPose();

}


private void transformModel(PoseStack poseStack, Vec3 posOffset, Vec3 rotOffset, Vec3 scale) {

Vec3 rot = rotOffset.add(0, 0, 180);

Quaternionf quat = new Quaternionf().rotationZYX((float) Math.toRadians(rot.x), (float) Math.toRadians(rot.y), (float) Math.toRadians(rot.z));

poseStack.rotateAround(quat, 0, 1.1f, 0);

posOffset = posOffset.scale(1 / 32f);

// The backpack rendering is slightly offset to center the pivot in back middle
poseStack.translate(posOffset.x, -posOffset.y + 0.5, -posOffset.z - 0.1);

poseStack.scale((float) scale.x, (float) scale.y, (float) scale.z);

}

private Optional<PlayerInventoryProvider.RenderInfo> getRenderInfo(Player player) {

Optional<ItemStack> backpackStack = Optional.empty();
boolean isArmorSlot = false;

if(isCurioLoaded) {
backpackStack = getBackpackFromCurios(player);
}

if(backpackStack.isEmpty()) {
backpackStack = getBackpackFromChestSlot(player);
isArmorSlot = true;
}

if(backpackStack.isPresent()) {
return Optional.of(new PlayerInventoryProvider.RenderInfo(backpackStack.get(), isArmorSlot));
}

return Optional.empty();

}


private Optional<ItemStack> getBackpackFromCurios(Player player) {

if(CuriosApi.getCuriosInventory(player).isPresent()) {

List<SlotResult> curioBackSlots = CuriosApi.getCuriosInventory(player).get().findCurios("back");

for(SlotResult backpackItem : curioBackSlots) {

if(!backpackItem.slotContext().visible())
continue;

ItemStack itemStack = backpackItem.stack();

if(itemStack.getItem() instanceof BackpackItem) {
return Optional.of(itemStack);
}

return Optional.empty();
}
}

return Optional.empty();

}

private Optional<ItemStack> getBackpackFromChestSlot(Player player) {

ItemStack armorSlot = player.getInventory().armor.get(EquipmentSlot.CHEST.getIndex());

if(armorSlot.getItem() instanceof BackpackItem) {
return Optional.of(armorSlot);
}

return Optional.empty();

}
}