2021-07-19 00:05:12 +02:00
|
|
|
package com.jozufozu.flywheel.vanilla;
|
|
|
|
|
2022-06-20 08:25:44 +02:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
2022-04-11 02:02:28 +02:00
|
|
|
import java.util.function.Function;
|
|
|
|
|
2023-03-31 01:52:51 +02:00
|
|
|
import com.jozufozu.flywheel.api.event.RenderStage;
|
2022-01-04 06:41:08 +01:00
|
|
|
import com.jozufozu.flywheel.api.instance.DynamicInstance;
|
2022-07-23 01:41:39 +02:00
|
|
|
import com.jozufozu.flywheel.api.instancer.InstancedPart;
|
2023-04-03 08:58:28 +02:00
|
|
|
import com.jozufozu.flywheel.api.instancer.InstancerProvider;
|
2023-04-04 21:36:54 +02:00
|
|
|
import com.jozufozu.flywheel.lib.instance.AbstractBlockEntityInstance;
|
2023-03-31 01:52:51 +02:00
|
|
|
import com.jozufozu.flywheel.lib.material.Materials;
|
|
|
|
import com.jozufozu.flywheel.lib.model.SimpleLazyModel;
|
|
|
|
import com.jozufozu.flywheel.lib.modelpart.ModelPart;
|
|
|
|
import com.jozufozu.flywheel.lib.struct.StructTypes;
|
|
|
|
import com.jozufozu.flywheel.lib.struct.TransformedPart;
|
|
|
|
import com.jozufozu.flywheel.lib.transform.TransformStack;
|
2021-07-19 00:05:12 +02:00
|
|
|
import com.jozufozu.flywheel.util.AnimationTickHolder;
|
2022-02-09 04:26:36 +01:00
|
|
|
import com.mojang.blaze3d.vertex.PoseStack;
|
2021-11-18 23:59:39 +01:00
|
|
|
import com.mojang.math.Quaternion;
|
|
|
|
import com.mojang.math.Vector3f;
|
2021-07-19 00:05:12 +02:00
|
|
|
|
2022-04-11 02:02:28 +02:00
|
|
|
import net.minecraft.Util;
|
2021-09-15 08:45:29 +02:00
|
|
|
import net.minecraft.client.renderer.Sheets;
|
2021-07-19 00:05:12 +02:00
|
|
|
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
|
2021-11-18 23:59:39 +01:00
|
|
|
import net.minecraft.core.Direction;
|
2021-09-15 08:45:29 +02:00
|
|
|
import net.minecraft.world.item.DyeColor;
|
2021-11-18 23:59:39 +01:00
|
|
|
import net.minecraft.world.level.block.ShulkerBoxBlock;
|
2021-09-15 08:45:29 +02:00
|
|
|
import net.minecraft.world.level.block.entity.ShulkerBoxBlockEntity;
|
2021-07-19 00:05:12 +02:00
|
|
|
|
2023-04-04 21:36:54 +02:00
|
|
|
public class ShulkerBoxInstance extends AbstractBlockEntityInstance<ShulkerBoxBlockEntity> implements DynamicInstance {
|
2021-07-19 00:05:12 +02:00
|
|
|
|
2022-07-30 00:58:34 +02:00
|
|
|
private static final Function<TextureAtlasSprite, SimpleLazyModel> BASE = Util.memoize(it -> new SimpleLazyModel(() -> makeBaseModel(it), Materials.SHULKER));
|
|
|
|
private static final Function<TextureAtlasSprite, SimpleLazyModel> LID = Util.memoize(it -> new SimpleLazyModel(() -> makeLidModel(it), Materials.SHULKER));
|
2022-04-11 02:02:28 +02:00
|
|
|
|
2021-07-19 00:05:12 +02:00
|
|
|
private final TextureAtlasSprite texture;
|
|
|
|
|
2022-06-20 08:25:44 +02:00
|
|
|
private final TransformedPart base;
|
|
|
|
private final TransformedPart lid;
|
2022-02-09 04:26:36 +01:00
|
|
|
private final PoseStack stack = new PoseStack();
|
2021-07-19 00:05:12 +02:00
|
|
|
|
|
|
|
private float lastProgress = Float.NaN;
|
|
|
|
|
2023-04-03 08:58:28 +02:00
|
|
|
public ShulkerBoxInstance(InstancerProvider instancerManager, ShulkerBoxBlockEntity blockEntity) {
|
2022-05-16 05:09:47 +02:00
|
|
|
super(instancerManager, blockEntity);
|
2021-07-19 00:05:12 +02:00
|
|
|
|
2022-01-04 06:41:08 +01:00
|
|
|
DyeColor color = blockEntity.getColor();
|
2021-07-19 00:05:12 +02:00
|
|
|
if (color == null) {
|
2021-09-15 08:45:29 +02:00
|
|
|
texture = Sheets.DEFAULT_SHULKER_TEXTURE_LOCATION.sprite();
|
2021-07-19 00:05:12 +02:00
|
|
|
} else {
|
2021-09-15 08:45:29 +02:00
|
|
|
texture = Sheets.SHULKER_TEXTURE_LOCATION.get(color.getId()).sprite();
|
2021-07-19 00:05:12 +02:00
|
|
|
}
|
|
|
|
Quaternion rotation = getDirection().getRotation();
|
|
|
|
|
2022-02-09 04:26:36 +01:00
|
|
|
TransformStack tstack = TransformStack.cast(stack);
|
|
|
|
|
|
|
|
tstack.translate(getInstancePosition())
|
2021-07-19 00:05:12 +02:00
|
|
|
.scale(0.9995f)
|
|
|
|
.translateAll(0.00025)
|
|
|
|
.centre()
|
|
|
|
.multiply(rotation)
|
|
|
|
.unCentre();
|
|
|
|
|
2022-02-09 04:26:36 +01:00
|
|
|
base = makeBaseInstance().setTransform(stack);
|
2021-07-19 00:05:12 +02:00
|
|
|
|
2022-02-09 04:26:36 +01:00
|
|
|
tstack.translateY(0.25);
|
2021-07-19 00:05:12 +02:00
|
|
|
|
2022-02-09 04:26:36 +01:00
|
|
|
lid = makeLidInstance().setTransform(stack);
|
2021-07-19 00:05:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void beginFrame() {
|
2022-01-04 06:41:08 +01:00
|
|
|
float progress = blockEntity.getProgress(AnimationTickHolder.getPartialTicks());
|
2021-07-19 00:05:12 +02:00
|
|
|
|
|
|
|
if (progress == lastProgress) return;
|
|
|
|
lastProgress = progress;
|
|
|
|
|
|
|
|
Quaternion spin = Vector3f.YP.rotationDegrees(270.0F * progress);
|
|
|
|
|
2022-04-12 00:11:39 +02:00
|
|
|
TransformStack.cast(stack)
|
|
|
|
.pushPose()
|
2021-07-19 00:05:12 +02:00
|
|
|
.centre()
|
|
|
|
.multiply(spin)
|
|
|
|
.unCentre()
|
|
|
|
.translateY(progress * 0.5f);
|
|
|
|
|
2022-02-09 04:26:36 +01:00
|
|
|
lid.setTransform(stack);
|
2021-07-19 00:05:12 +02:00
|
|
|
|
2021-11-24 00:07:31 +01:00
|
|
|
stack.popPose();
|
2021-07-19 00:05:12 +02:00
|
|
|
}
|
|
|
|
|
2022-06-20 08:25:44 +02:00
|
|
|
@Override
|
|
|
|
public void addCrumblingParts(List<InstancedPart> data) {
|
|
|
|
Collections.addAll(data, base, lid);
|
|
|
|
}
|
|
|
|
|
2021-07-19 00:05:12 +02:00
|
|
|
@Override
|
2023-04-04 21:36:54 +02:00
|
|
|
protected void _delete() {
|
2021-07-19 00:05:12 +02:00
|
|
|
base.delete();
|
|
|
|
lid.delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void updateLight() {
|
|
|
|
relight(pos, base, lid);
|
|
|
|
}
|
|
|
|
|
2022-06-20 08:25:44 +02:00
|
|
|
private TransformedPart makeBaseInstance() {
|
2023-04-03 08:58:28 +02:00
|
|
|
return instancerManager.getInstancer(StructTypes.TRANSFORMED, BASE.apply(texture), RenderStage.AFTER_BLOCK_ENTITIES)
|
2021-07-19 00:05:12 +02:00
|
|
|
.createInstance();
|
|
|
|
}
|
|
|
|
|
2022-06-20 08:25:44 +02:00
|
|
|
private TransformedPart makeLidInstance() {
|
2023-04-03 08:58:28 +02:00
|
|
|
return instancerManager.getInstancer(StructTypes.TRANSFORMED, LID.apply(texture), RenderStage.AFTER_BLOCK_ENTITIES)
|
2021-07-19 00:05:12 +02:00
|
|
|
.createInstance();
|
|
|
|
}
|
|
|
|
|
2022-04-11 02:02:28 +02:00
|
|
|
private static ModelPart makeBaseModel(TextureAtlasSprite texture) {
|
2021-11-09 05:48:02 +01:00
|
|
|
return ModelPart.builder("shulker_base", 64, 64)
|
2021-07-19 00:05:12 +02:00
|
|
|
.sprite(texture)
|
|
|
|
.cuboid()
|
|
|
|
.textureOffset(0, 28)
|
|
|
|
.size(16, 8, 16)
|
|
|
|
.invertYZ()
|
|
|
|
.endCuboid()
|
|
|
|
.build();
|
|
|
|
}
|
|
|
|
|
2022-04-11 02:02:28 +02:00
|
|
|
private static ModelPart makeLidModel(TextureAtlasSprite texture) {
|
2021-11-09 05:48:02 +01:00
|
|
|
return ModelPart.builder("shulker_lid", 64, 64)
|
2021-07-19 00:05:12 +02:00
|
|
|
.sprite(texture)
|
|
|
|
.cuboid()
|
|
|
|
.size(16, 12, 16)
|
|
|
|
.invertYZ()
|
|
|
|
.endCuboid()
|
|
|
|
.build();
|
|
|
|
}
|
|
|
|
|
|
|
|
private Direction getDirection() {
|
|
|
|
if (blockState.getBlock() instanceof ShulkerBoxBlock) {
|
|
|
|
return blockState.getValue(ShulkerBoxBlock.FACING);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Direction.UP;
|
|
|
|
}
|
|
|
|
}
|