diff --git a/shared/src/main/java/net/blay09/mods/forbiddensmoothies/block/entity/BlenderBlockEntity.java b/shared/src/main/java/net/blay09/mods/forbiddensmoothies/block/entity/BlenderBlockEntity.java index 70d6215..83445dc 100644 --- a/shared/src/main/java/net/blay09/mods/forbiddensmoothies/block/entity/BlenderBlockEntity.java +++ b/shared/src/main/java/net/blay09/mods/forbiddensmoothies/block/entity/BlenderBlockEntity.java @@ -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; @@ -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) { @@ -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"); @@ -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; @@ -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); @@ -255,4 +278,19 @@ private Optional 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; + } } diff --git a/shared/src/main/java/net/blay09/mods/forbiddensmoothies/client/render/BlenderRenderer.java b/shared/src/main/java/net/blay09/mods/forbiddensmoothies/client/render/BlenderRenderer.java index 781b422..b38cc5f 100644 --- a/shared/src/main/java/net/blay09/mods/forbiddensmoothies/client/render/BlenderRenderer.java +++ b/shared/src/main/java/net/blay09/mods/forbiddensmoothies/client/render/BlenderRenderer.java @@ -5,6 +5,8 @@ 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 { @@ -12,7 +14,39 @@ 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(); } } diff --git a/shared/src/main/resources/assets/forbiddensmoothies/models/block/blender.json b/shared/src/main/resources/assets/forbiddensmoothies/models/block/blender.json index 6c74b2e..5e81b32 100644 --- a/shared/src/main/resources/assets/forbiddensmoothies/models/block/blender.json +++ b/shared/src/main/resources/assets/forbiddensmoothies/models/block/blender.json @@ -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"}, @@ -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"}, @@ -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"}, @@ -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"}, @@ -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": { @@ -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": {