2021-07-09 22:24:26 +02:00
|
|
|
package com.jozufozu.flywheel.vanilla;
|
|
|
|
|
2021-07-13 00:02:08 +02:00
|
|
|
import java.util.Calendar;
|
|
|
|
|
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
|
2021-07-09 22:24:26 +02:00
|
|
|
import com.jozufozu.flywheel.backend.instancing.IDynamicInstance;
|
|
|
|
import com.jozufozu.flywheel.backend.instancing.tile.TileEntityInstance;
|
2021-07-24 08:46:30 +02:00
|
|
|
import com.jozufozu.flywheel.backend.material.MaterialManager;
|
2021-07-22 05:28:20 +02:00
|
|
|
import com.jozufozu.flywheel.backend.state.TextureRenderState;
|
2021-07-09 22:24:26 +02:00
|
|
|
import com.jozufozu.flywheel.core.Materials;
|
2021-07-12 00:54:51 +02:00
|
|
|
import com.jozufozu.flywheel.core.materials.ModelData;
|
2021-07-09 22:24:26 +02:00
|
|
|
import com.jozufozu.flywheel.core.materials.OrientedData;
|
|
|
|
import com.jozufozu.flywheel.core.model.ModelPart;
|
|
|
|
import com.jozufozu.flywheel.util.AnimationTickHolder;
|
2021-07-12 00:54:51 +02:00
|
|
|
import com.jozufozu.flywheel.util.transform.MatrixTransformStack;
|
2021-07-10 20:33:01 +02:00
|
|
|
|
2021-07-09 22:24:26 +02:00
|
|
|
import it.unimi.dsi.fastutil.floats.Float2FloatFunction;
|
2021-07-12 00:54:51 +02:00
|
|
|
import net.minecraft.block.AbstractChestBlock;
|
2021-07-09 22:24:26 +02:00
|
|
|
import net.minecraft.block.Block;
|
|
|
|
import net.minecraft.block.ChestBlock;
|
|
|
|
import net.minecraft.client.renderer.Atlases;
|
|
|
|
import net.minecraft.client.renderer.model.RenderMaterial;
|
|
|
|
import net.minecraft.state.properties.ChestType;
|
|
|
|
import net.minecraft.tileentity.ChestTileEntity;
|
2021-07-10 20:33:01 +02:00
|
|
|
import net.minecraft.tileentity.IChestLid;
|
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2021-07-09 22:24:26 +02:00
|
|
|
import net.minecraft.tileentity.TileEntityMerger;
|
|
|
|
import net.minecraft.util.math.vector.Quaternion;
|
|
|
|
import net.minecraft.util.math.vector.Vector3f;
|
|
|
|
|
2021-07-10 20:33:01 +02:00
|
|
|
public class ChestInstance<T extends TileEntity & IChestLid> extends TileEntityInstance<T> implements IDynamicInstance {
|
2021-07-09 22:24:26 +02:00
|
|
|
|
2021-09-05 01:40:40 +02:00
|
|
|
private final MatrixTransformStack stack = new MatrixTransformStack();
|
2021-07-09 22:24:26 +02:00
|
|
|
private final OrientedData body;
|
2021-07-12 00:54:51 +02:00
|
|
|
private final ModelData lid;
|
2021-07-09 22:24:26 +02:00
|
|
|
|
|
|
|
private final Float2FloatFunction lidProgress;
|
|
|
|
private final RenderMaterial renderMaterial;
|
2021-07-10 20:33:01 +02:00
|
|
|
@Nonnull
|
|
|
|
private final ChestType chestType;
|
|
|
|
private final Quaternion baseRotation;
|
2021-07-09 22:24:26 +02:00
|
|
|
|
2021-07-12 00:54:51 +02:00
|
|
|
private float lastProgress = Float.NaN;
|
|
|
|
|
2021-08-24 07:34:27 +02:00
|
|
|
public ChestInstance(MaterialManager materialManager, T tile) {
|
2021-07-09 22:24:26 +02:00
|
|
|
super(materialManager, tile);
|
|
|
|
|
|
|
|
Block block = blockState.getBlock();
|
|
|
|
|
2021-07-15 20:36:24 +02:00
|
|
|
chestType = blockState.hasProperty(ChestBlock.TYPE) ? blockState.getValue(ChestBlock.TYPE) : ChestType.SINGLE;
|
|
|
|
renderMaterial = Atlases.chooseMaterial(tile, chestType, isChristmas());
|
2021-07-09 22:24:26 +02:00
|
|
|
|
|
|
|
body = baseInstance()
|
|
|
|
.setPosition(getInstancePosition());
|
2021-07-12 00:54:51 +02:00
|
|
|
lid = lidInstance();
|
2021-07-09 22:24:26 +02:00
|
|
|
|
2021-07-12 00:54:51 +02:00
|
|
|
if (block instanceof AbstractChestBlock) {
|
2021-07-09 22:24:26 +02:00
|
|
|
|
2021-07-10 20:33:01 +02:00
|
|
|
// MatrixStack stack = new MatrixStack();
|
|
|
|
//
|
|
|
|
// stack.push();
|
2021-07-15 20:36:24 +02:00
|
|
|
float horizontalAngle = blockState.getValue(ChestBlock.FACING).toYRot();
|
2021-07-10 20:33:01 +02:00
|
|
|
|
2021-07-15 20:36:24 +02:00
|
|
|
baseRotation = Vector3f.YP.rotationDegrees(-horizontalAngle);
|
2021-07-10 20:33:01 +02:00
|
|
|
|
|
|
|
body.setRotation(baseRotation);
|
|
|
|
|
2021-07-12 00:54:51 +02:00
|
|
|
AbstractChestBlock<?> chestBlock = (AbstractChestBlock<?>) block;
|
2021-07-09 22:24:26 +02:00
|
|
|
|
2021-07-15 20:36:24 +02:00
|
|
|
TileEntityMerger.ICallbackWrapper<? extends ChestTileEntity> wrapper = chestBlock.combine(blockState, world, getWorldPosition(), true);
|
2021-07-09 22:24:26 +02:00
|
|
|
|
2021-07-15 20:36:24 +02:00
|
|
|
this.lidProgress = wrapper.apply(ChestBlock.opennessCombiner(tile));
|
2021-07-09 22:24:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
} else {
|
2021-07-15 20:36:24 +02:00
|
|
|
baseRotation = Quaternion.ONE;
|
2021-07-09 22:24:26 +02:00
|
|
|
lidProgress = $ -> 0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void beginFrame() {
|
|
|
|
float progress = lidProgress.get(AnimationTickHolder.getPartialTicks());
|
|
|
|
|
2021-07-12 00:54:51 +02:00
|
|
|
if (lastProgress == progress) return;
|
|
|
|
|
|
|
|
lastProgress = progress;
|
|
|
|
|
2021-07-09 22:24:26 +02:00
|
|
|
progress = 1.0F - progress;
|
|
|
|
progress = 1.0F - progress * progress * progress;
|
|
|
|
|
|
|
|
float angleX = -(progress * ((float) Math.PI / 2F));
|
|
|
|
|
2021-09-05 01:40:40 +02:00
|
|
|
stack.setIdentity()
|
|
|
|
.translate(getInstancePosition())
|
2021-07-12 00:54:51 +02:00
|
|
|
.translate(0, 9f/16f, 0)
|
|
|
|
.centre()
|
2021-07-10 20:33:01 +02:00
|
|
|
.multiply(baseRotation)
|
2021-07-12 00:54:51 +02:00
|
|
|
.unCentre()
|
|
|
|
.translate(0, 0, 1f / 16f)
|
2021-07-15 20:36:24 +02:00
|
|
|
.multiply(Vector3f.XP.rotation(angleX))
|
2021-07-12 00:54:51 +02:00
|
|
|
.translate(0, 0, -1f / 16f);
|
2021-07-10 20:33:01 +02:00
|
|
|
|
2021-07-12 00:54:51 +02:00
|
|
|
lid.setTransform(stack.unwrap());
|
2021-07-09 22:24:26 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void updateLight() {
|
|
|
|
relight(getWorldPosition(), body, lid);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void remove() {
|
|
|
|
body.delete();
|
|
|
|
lid.delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
private OrientedData baseInstance() {
|
|
|
|
|
2021-07-22 05:28:20 +02:00
|
|
|
return materialManager.solid(TextureRenderState.get(renderMaterial.atlasLocation()))
|
|
|
|
.material(Materials.ORIENTED)
|
|
|
|
.model("base_" + renderMaterial.texture(), this::getBaseModel)
|
2021-07-09 22:24:26 +02:00
|
|
|
.createInstance();
|
|
|
|
}
|
|
|
|
|
2021-07-12 00:54:51 +02:00
|
|
|
private ModelData lidInstance() {
|
2021-07-09 22:24:26 +02:00
|
|
|
|
2021-07-22 05:28:20 +02:00
|
|
|
return materialManager.solid(TextureRenderState.get(renderMaterial.atlasLocation()))
|
|
|
|
.material(Materials.TRANSFORMED)
|
|
|
|
.model("lid_" + renderMaterial.texture(), this::getLidModel)
|
2021-07-09 22:24:26 +02:00
|
|
|
.createInstance();
|
|
|
|
}
|
|
|
|
|
2021-07-23 21:26:32 +02:00
|
|
|
private ModelPart getBaseModel() {
|
2021-07-09 22:24:26 +02:00
|
|
|
|
2021-07-10 20:33:01 +02:00
|
|
|
switch (chestType) {
|
|
|
|
case LEFT:
|
|
|
|
return ModelPart.builder(64, 64)
|
2021-07-15 20:36:24 +02:00
|
|
|
.sprite(renderMaterial.sprite())
|
2021-07-10 20:33:01 +02:00
|
|
|
.cuboid()
|
|
|
|
.textureOffset(0, 19)
|
|
|
|
.start(0, 0, 1)
|
|
|
|
.size(15, 10, 14)
|
|
|
|
.endCuboid()
|
|
|
|
.build();
|
|
|
|
case RIGHT:
|
|
|
|
return ModelPart.builder(64, 64)
|
2021-07-15 20:36:24 +02:00
|
|
|
.sprite(renderMaterial.sprite())
|
2021-07-10 20:33:01 +02:00
|
|
|
.cuboid()
|
|
|
|
.textureOffset(0, 19)
|
|
|
|
.start(1, 0, 1)
|
|
|
|
.size(15, 10, 14)
|
|
|
|
.endCuboid()
|
|
|
|
.build();
|
|
|
|
}
|
|
|
|
|
2021-07-09 22:24:26 +02:00
|
|
|
return ModelPart.builder(64, 64)
|
2021-07-15 20:36:24 +02:00
|
|
|
.sprite(renderMaterial.sprite())
|
2021-07-09 22:24:26 +02:00
|
|
|
.cuboid()
|
|
|
|
.textureOffset(0, 19)
|
|
|
|
.start(1, 0, 1)
|
|
|
|
.end(15, 10, 15)
|
|
|
|
.endCuboid()
|
|
|
|
.build();
|
|
|
|
}
|
|
|
|
|
2021-07-23 21:26:32 +02:00
|
|
|
private ModelPart getLidModel() {
|
2021-07-09 22:24:26 +02:00
|
|
|
|
2021-07-10 20:33:01 +02:00
|
|
|
switch (chestType) {
|
|
|
|
case LEFT:
|
|
|
|
return ModelPart.builder(64, 64)
|
2021-07-15 20:36:24 +02:00
|
|
|
.sprite(renderMaterial.sprite())
|
2021-07-10 20:33:01 +02:00
|
|
|
.cuboid()
|
|
|
|
.textureOffset(0, 0)
|
|
|
|
.start(0, 0, 1)
|
|
|
|
.size(15, 5, 14)
|
|
|
|
.endCuboid()
|
|
|
|
.cuboid()
|
|
|
|
.start(0, -2, 15)
|
|
|
|
.size(1, 4, 1)
|
|
|
|
.endCuboid()
|
|
|
|
.build();
|
|
|
|
case RIGHT:
|
|
|
|
return ModelPart.builder(64, 64)
|
2021-07-15 20:36:24 +02:00
|
|
|
.sprite(renderMaterial.sprite())
|
2021-07-10 20:33:01 +02:00
|
|
|
.cuboid()
|
|
|
|
.textureOffset(0, 0)
|
|
|
|
.start(1, 0, 1)
|
|
|
|
.size(15, 5, 14)
|
|
|
|
.endCuboid()
|
|
|
|
.cuboid()
|
|
|
|
.start(15, -2, 15)
|
|
|
|
.size(1, 4, 1)
|
|
|
|
.endCuboid()
|
|
|
|
.build();
|
|
|
|
}
|
|
|
|
|
2021-07-09 22:24:26 +02:00
|
|
|
return ModelPart.builder(64, 64)
|
2021-07-15 20:36:24 +02:00
|
|
|
.sprite(renderMaterial.sprite())
|
2021-07-09 22:24:26 +02:00
|
|
|
.cuboid()
|
|
|
|
.textureOffset(0, 0)
|
|
|
|
.start(1, 0, 1)
|
2021-07-10 20:33:01 +02:00
|
|
|
.size(14, 5, 14)
|
2021-07-09 22:24:26 +02:00
|
|
|
.endCuboid()
|
|
|
|
.cuboid()
|
|
|
|
.start(7, -2, 15)
|
|
|
|
.size(2, 4, 1)
|
|
|
|
.endCuboid()
|
|
|
|
.build();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isChristmas() {
|
|
|
|
Calendar calendar = Calendar.getInstance();
|
|
|
|
return calendar.get(Calendar.MONTH) + 1 == 12 && calendar.get(Calendar.DATE) >= 24 && calendar.get(Calendar.DATE) <= 26;
|
|
|
|
}
|
|
|
|
}
|