Skip to content

Commit

Permalink
feat: Add some fancy item rendering to the Blender
Browse files Browse the repository at this point in the history
  • Loading branch information
BlayTheNinth committed Dec 31, 2023
1 parent 0cbe6aa commit 2228d38
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@

public class BlenderBlockEntity extends BalmBlockEntity implements BalmMenuProvider, BalmContainerProvider, BalmEnergyStorageProvider {

private static final int SYNC_INTERVAL = 20;

public static final int DATA_PROGRESS = 0;
public static final int DATA_MAX_PROGRESS = 1;
public static final int DATA_LOCKED_INPUTS = 2;
Expand Down Expand Up @@ -98,6 +100,9 @@ public void setEnergy(int energy) {
private int progress;
private int maxProgress;
private int energyCostPerTick;
private boolean dirtyForSync;
private int ticksSinceLastSync;
private float animationTicks;

protected final ContainerData dataAccess = new ContainerData() {
public int get(int i) {
Expand Down Expand Up @@ -150,7 +155,9 @@ public void load(CompoundTag tag) {
super.load(tag);

container.deserialize(tag.getCompound("Items"));
energyStorage.deserialize(tag.get("Energy"));
if (tag.contains("Energy")) {
energyStorage.deserialize(tag.get("Energy"));
}
progress = tag.getInt("Progress");
maxProgress = tag.getInt("MaxProgress");
lockedInputs = tag.getBoolean("LockedInputs");
Expand All @@ -165,6 +172,14 @@ protected void saveAdditional(CompoundTag tag) {
tag.putBoolean("LockedInputs", this.lockedInputs);
}

@Override
protected void writeUpdateTag(CompoundTag tag) {
super.writeUpdateTag(tag);
tag.put("Items", container.serialize());
tag.putInt("Progress", this.progress);
tag.putInt("MaxProgress", this.maxProgress);
}

@Override
public Container getContainer() {
return container;
Expand All @@ -176,6 +191,14 @@ public Container getContainer(Direction side) {
}

public void serverTick() {
if (ticksSinceLastSync >= 20 && dirtyForSync) {
sync();
dirtyForSync = false;
ticksSinceLastSync = 0;
} else {
ticksSinceLastSync++;
}

transferOutputs();

final var recipe = selectRecipe(randomSource).orElse(null);
Expand Down Expand Up @@ -255,4 +278,19 @@ private Optional<BlenderRecipe> selectRecipe(RandomSource random) {
public EnergyStorage getEnergyStorage() {
return energyStorage;
}

public Container getInputContainer() {
return inputContainer;
}

@Override
public void setChanged() {
super.setChanged();
dirtyForSync = true;
}

public float animate(float partialTicks) {
animationTicks += partialTicks;
return animationTicks;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,48 @@
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;
import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;
import org.joml.AxisAngle4f;
import org.joml.Quaternionf;

public class BlenderRenderer implements BlockEntityRenderer<BlenderBlockEntity> {

public BlenderRenderer(BlockEntityRendererProvider.Context context) {
}

@Override
public void render(BlenderBlockEntity blockEntity, float partialTicks, PoseStack poseStack, MultiBufferSource bufferIn, int combinedLightIn, int combinedOverlayIn) {
public void render(BlenderBlockEntity blockEntity, float partialTicks, PoseStack poseStack, MultiBufferSource buffer, int combinedLight, int combinedOverlayIn) {
final var inputContainer = blockEntity.getInputContainer();
poseStack.pushPose();
final var itemScale = 0.2f;
poseStack.translate(0.5f, 0.5f, 0.5f);
poseStack.scale(itemScale, itemScale, itemScale);
final var animationTime = blockEntity.animate(partialTicks);
final var offsetH = 0.5f;
final var offsetV = 0.5f;
final var speedH = 0.2f;
final var speedV = 0.05f;
final var angleSpeed = 0.25f;
final var cosah = Math.cos(animationTime * speedH) * offsetH;
final var sinah = Math.sin(animationTime * speedH) * offsetH;
for (int i = 0; i < inputContainer.getContainerSize(); i++) {
poseStack.pushPose();
poseStack.translate(i * 0.2f - 0.5f, i * 0.2f - 0.5f, 0f);
double sinav = Math.sin(i * animationTime * speedV) * offsetV;
if (i % 2 == 0) {
poseStack.translate(cosah, sinav, sinah);
} else {
poseStack.translate(sinah, sinav, cosah);
}
final var angle = (float) ((i + 1) * Math.PI * 2 / inputContainer.getContainerSize() + animationTime * angleSpeed);
poseStack.mulPose(new Quaternionf(new AxisAngle4f(angle, 0f, 1f, 0f)));

final var itemStack = inputContainer.getItem(i);
if (!itemStack.isEmpty()) {
RenderUtils.renderItem(itemStack, combinedLight, poseStack, buffer, blockEntity.getLevel());
}
poseStack.popPose();
}
poseStack.popPose();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
},
{
"name": "corner",
"from": [3, 2, 3],
"from": [4, 2, 4],
"to": [5, 14, 5],
"faces": {
"north": {"uv": [14, 2, 16, 14], "texture": "#1"},
Expand All @@ -47,8 +47,8 @@
},
{
"name": "corner",
"from": [11, 2, 3],
"to": [13, 14, 5],
"from": [11, 2, 4],
"to": [12, 14, 5],
"faces": {
"north": {"uv": [14, 2, 16, 14], "texture": "#1"},
"east": {"uv": [14, 2, 16, 14], "texture": "#1"},
Expand All @@ -59,7 +59,7 @@
{
"name": "corner",
"from": [11, 2, 11],
"to": [13, 14, 13],
"to": [12, 14, 12],
"faces": {
"north": {"uv": [14, 2, 16, 14], "texture": "#1"},
"east": {"uv": [14, 2, 16, 14], "texture": "#1"},
Expand All @@ -69,8 +69,8 @@
},
{
"name": "corner",
"from": [3, 2, 11],
"to": [5, 14, 13],
"from": [4, 2, 11],
"to": [5, 14, 12],
"faces": {
"north": {"uv": [14, 2, 16, 14], "texture": "#1"},
"east": {"uv": [14, 2, 16, 14], "texture": "#1"},
Expand All @@ -89,7 +89,7 @@
},
{
"name": "glass",
"from": [4, 2, 4],
"from": [4, 2, 5],
"to": [5, 14, 11],
"rotation": {"angle": 0, "axis": "y", "origin": [8, 0, 8]},
"faces": {
Expand All @@ -98,7 +98,7 @@
},
{
"name": "glass",
"from": [11, 2, 4],
"from": [11, 2, 5],
"to": [12, 14, 11],
"rotation": {"angle": 0, "axis": "y", "origin": [8, 0, 8]},
"faces": {
Expand Down

0 comments on commit 2228d38

Please sign in to comment.