Skip to content

Commit

Permalink
Animations!
Browse files Browse the repository at this point in the history
Fix issues with handheld storage not opening
  • Loading branch information
kyrptonaught committed Dec 30, 2019
1 parent 12507d2 commit a5f2346
Show file tree
Hide file tree
Showing 15 changed files with 221 additions and 183 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ dependencies {
//modApi "blue.endless:jankson:+"
//include "blue.endless:jankson:+"

modApi 'com.github.Siphalor:nbt-crafting:1.15-SNAPSHOT'
include 'com.github.Siphalor:nbt-crafting:1.15-SNAPSHOT'
modApi 'com.github.Siphalor:nbt-crafting:1.15-SNAPSHOT'
include 'com.github.Siphalor:nbt-crafting:1.15-SNAPSHOT'
modApi "com.github.NerdHubMC.Cardinal-Components-API:cardinal-components-base:+"
modApi "com.github.NerdHubMC.Cardinal-Components-API:cardinal-components-level:+"
modApi "com.github.NerdHubMC.Cardinal-Components-API:cardinal-components-world:+"
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ yarn_mappings=1.15.1+build.17:v2
loader_version=0.7.2+build.175

# Mod Properties
mod_version=1.0.0b4
mod_version=1.0.0b5
maven_group = net.fabricmc
archives_base_name=linkedstorage

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.client.itemgroup.FabricItemGroupBuilder;
import net.fabricmc.fabric.api.container.ContainerProviderRegistry;
import net.kyrptonaught.linkedstorage.inventory.LinkedContainer;
import net.kyrptonaught.linkedstorage.network.OpenStoragePacket;
import net.kyrptonaught.linkedstorage.network.SetDyePacket;
import net.kyrptonaught.linkedstorage.register.ModBlocks;
import net.kyrptonaught.linkedstorage.register.ModItems;
import net.kyrptonaught.linkedstorage.util.ChannelManager;
import net.minecraft.container.Container;
import net.minecraft.container.GenericContainer;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;

public class LinkedStorageMod implements ModInitializer {
public static final String MOD_ID = "linkedstorage";
Expand All @@ -24,12 +24,12 @@ public void onInitialize() {
ChannelManager.init();
ModBlocks.register();
ModItems.register();
ContainerProviderRegistry.INSTANCE.registerFactory(new Identifier(MOD_ID, "linkedstorage"), (syncId, id, player, buf) -> getContainer(syncId, player, buf.readByteArray()));
ContainerProviderRegistry.INSTANCE.registerFactory(new Identifier(MOD_ID, "linkedstorage"), (syncId, id, player, buf) -> getContainer(syncId, player, buf.readByteArray(), buf.readBlockPos()));
SetDyePacket.registerReceivePacket();
OpenStoragePacket.registerReceivePacket();
}

static Container getContainer(int id, PlayerEntity player, byte[] channel) {
return GenericContainer.createGeneric9x3(id, player.inventory, ChannelManager.getManager(player.getEntityWorld().getLevelProperties()).getInv(channel));
static LinkedContainer getContainer(int id, PlayerEntity player, byte[] channel, BlockPos pos) {
return new LinkedContainer(id, player.inventory, ChannelManager.getManager(player.getEntityWorld().getLevelProperties()).getInv(channel), pos);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,33 @@
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.rendereregistry.v1.BlockEntityRendererRegistry;
import net.fabricmc.fabric.api.client.screen.ScreenProviderRegistry;
import net.fabricmc.fabric.api.event.client.ClientSpriteRegistryCallback;
import net.fabricmc.fabric.impl.client.rendering.ColorProviderRegistryImpl;
import net.kyrptonaught.linkedstorage.block.StorageBlock;
import net.kyrptonaught.linkedstorage.block.StorageBlockRenderer;
import net.kyrptonaught.linkedstorage.client.StorageBlockRenderer;
import net.kyrptonaught.linkedstorage.client.StorageContainerScreen;
import net.kyrptonaught.linkedstorage.inventory.LinkedInventoryHelper;
import net.kyrptonaught.linkedstorage.register.ModItems;
import net.minecraft.client.render.TexturedRenderLayers;
import net.minecraft.util.DyeColor;
import net.minecraft.util.Identifier;

public class LinkedStorageModClient implements ClientModInitializer {
public static final Identifier TEXTURE = new Identifier(LinkedStorageMod.MOD_ID, "block/chest");

@Override
public void onInitializeClient() {
BlockEntityRendererRegistry.INSTANCE.register(StorageBlock.blockEntity, StorageBlockRenderer::new);
ScreenProviderRegistry.INSTANCE.registerFactory(new Identifier(LinkedStorageMod.MOD_ID, "linkedstorage"), (syncId, identifier, player, buf) ->
{
byte[] channel = buf.readByteArray();
return new StorageContainerScreen(LinkedStorageMod.getContainer(syncId, player, channel), player.inventory);
});
new StorageContainerScreen(LinkedStorageMod.getContainer(syncId, player, buf.readByteArray(), buf.readBlockPos()), player.inventory));
ColorProviderRegistryImpl.ITEM.register((stack, layer) -> {
if (layer == 0 || !LinkedInventoryHelper.itemHasChannel(stack))
return DyeColor.WHITE.getMaterialColor().color;
byte[] colors = LinkedInventoryHelper.getItemChannel(stack);
return DyeColor.byId(colors[layer - 1]).getMaterialColor().color;
}, ModItems.storageItem);
ClientSpriteRegistryCallback.event(TexturedRenderLayers.CHEST_ATLAS_TEXTURE).register((atlasTexture, registry) -> {
registry.register(TEXTURE);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package net.kyrptonaught.linkedstorage.block;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.api.EnvironmentInterface;
import net.fabricmc.api.EnvironmentInterfaces;
import net.kyrptonaught.linkedstorage.inventory.LinkedContainer;
import net.minecraft.block.ChestBlock;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.block.enums.ChestType;
import net.minecraft.client.block.ChestAnimationProgress;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvent;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.Tickable;
import net.minecraft.util.math.Box;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.World;

import java.util.List;

@EnvironmentInterfaces({@EnvironmentInterface(value = EnvType.CLIENT, itf = ChestAnimationProgress.class)})
public class OpenableBlockEntity extends BlockEntity implements ChestAnimationProgress, Tickable {
OpenableBlockEntity(BlockEntityType<?> blockEntityType) {
super(blockEntityType);
}

@Environment(EnvType.CLIENT)
private static int countViewers(World world, OpenableBlockEntity instance, int x, int y, int z) {
int viewers = 0;
List<PlayerEntity> playersInRange = world.getNonSpectatingEntities(PlayerEntity.class, new Box(x - 5, y - 5, z - 5, x + 6, y + 6, z + 6));

for (PlayerEntity player : playersInRange) {
if (player.container instanceof LinkedContainer && instance.isPlayerViewing(player))
viewers++;
}
return viewers;
}

@Environment(EnvType.CLIENT)
public boolean isPlayerViewing(PlayerEntity playe) {
return true;
}

@Override
@Environment(EnvType.CLIENT)
public float getAnimationProgress(float f) {
return MathHelper.lerp(f, lastAnimationAngle, animationAngle);
}

private float animationAngle;
private float lastAnimationAngle;

@Override
public void tick() {
if (world != null && world.isClient) {
int viewerCount = countViewers(world, this, pos.getX(), pos.getY(), pos.getZ());
lastAnimationAngle = animationAngle;
if (viewerCount > 0 && animationAngle == 0.0F) playSound(SoundEvents.BLOCK_CHEST_OPEN);
if (viewerCount == 0 && animationAngle > 0.0F || viewerCount > 0 && animationAngle < 1.0F) {
float float_2 = animationAngle;
if (viewerCount > 0) animationAngle += 0.1F;
else animationAngle -= 0.1F;
animationAngle = MathHelper.clamp(animationAngle, 0, 1);
if (animationAngle < 0.5F && float_2 >= 0.5F) playSound(SoundEvents.BLOCK_CHEST_CLOSE);
}
}
}

private void playSound(SoundEvent soundEvent) {
double d = (double) this.pos.getX() + 0.5D;
double e = (double) this.pos.getY() + 0.5D;
double f = (double) this.pos.getZ() + 0.5D;
this.world.playSound(null, d, e, f, soundEvent, SoundCategory.BLOCKS, 0.5F, this.world.random.nextFloat() * 0.1F + 0.9F);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,12 @@ public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEnt
if (stack.getItem() instanceof DyeItem) {
if (!checkButons(state, pos, hit))
OpenStoragePacket.sendPacket(pos);
} else OpenStoragePacket.sendPacket(pos);
} else {
// StorageBlockEntity be = (StorageBlockEntity) world.getBlockEntity(pos);
// if (be.viewerCount < 0) be.viewerCount = 0;
// ++be.viewerCount;
OpenStoragePacket.sendPacket(pos);
}
return ActionResult.SUCCESS;

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package net.kyrptonaught.linkedstorage.block;

import net.fabricmc.fabric.api.block.entity.BlockEntityClientSerializable;
import net.kyrptonaught.linkedstorage.inventory.LinkedContainer;
import net.kyrptonaught.linkedstorage.inventory.LinkedInventory;
import net.kyrptonaught.linkedstorage.inventory.LinkedInventoryHelper;
import net.kyrptonaught.linkedstorage.util.ChannelManager;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.nbt.CompoundTag;

public class StorageBlockEntity extends BlockEntity implements BlockEntityClientSerializable {
public class StorageBlockEntity extends OpenableBlockEntity implements BlockEntityClientSerializable {
private byte[] dyeChannel = LinkedInventoryHelper.getDefaultChannel();
private LinkedInventory linkedInventory;

Expand Down Expand Up @@ -76,4 +77,9 @@ public void fromClientTag(CompoundTag tag) {
public CompoundTag toClientTag(CompoundTag tag) {
return toTag(tag);
}

@Override
public boolean isPlayerViewing(PlayerEntity player) {
return ((LinkedContainer) player.container).linkedBlock.equals(pos);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package net.kyrptonaught.linkedstorage.client;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.model.Model;
import net.minecraft.client.model.ModelPart;
import net.minecraft.client.render.RenderLayer;
import net.minecraft.client.render.VertexConsumer;
import net.minecraft.client.util.math.MatrixStack;

@Environment(EnvType.CLIENT)
public class LinkedChestModel extends Model {
private ModelPart lid;
protected ModelPart base;
private ModelPart latch;
public ModelPart button1, button2, button3;

public LinkedChestModel() {
super(RenderLayer::getEntityCutout);
this.textureWidth = 64;
this.textureHeight = 64;
this.base = new ModelPart(64, 64, 0, 19); // 818
this.base.addCuboid(1.0F, 0.0F, 1.0F, 14.0F, 10.0F, 14.0F, 0.0F);
this.lid = new ModelPart(64, 64, 0, 0);
this.lid.addCuboid(1.0F, 0.0F, 0.0F, 14.0F, 5.0F, 14.0F, 0.0F);//817
this.lid.pivotY = 9.0F;
this.lid.pivotZ = 1.0F;
this.latch = new ModelPart(64, 64, 0, 0);
this.latch.addCuboid(7.0F, -1.0F, 15.0F, 2.0F, 4.0F, 1.0F, 0.0F); //819
this.latch.pivotY = 8.0F;
button1 = new ModelPart(64, 64, 0, 19);
button2 = new ModelPart(64, 64, 0, 19);
button3 = new ModelPart(64, 64, 0, 19);
button1.addCuboid(4, 5, 5, 2, 1, 4, 0);
button2.addCuboid(7, 5, 5, 2, 1, 4, 0);
button3.addCuboid(10, 5, 5, 2, 1, 4, 0);
button1.pivotY = 9f;
button1.pivotZ = 1f;
button2.pivotY = 9f;
button2.pivotZ = 1f;
button3.pivotY = 9f;
button3.pivotZ = 1f;
}

public void setLidPitch(float pitch) {
pitch = 1.0f - pitch;
button1.pitch = button2.pitch = button3.pitch = latch.pitch = lid.pitch = -((1.0F - pitch * pitch * pitch) * 1.5707964F);
}

public void render(MatrixStack matrixStack, VertexConsumer vertexConsumer, int i, int j) {
render(matrixStack, vertexConsumer, i, j, 1, 1, 1, 1);
}

@Override
public void render(MatrixStack stack, VertexConsumer consumer, int i, int j, float r, float g, float b, float f) {
base.render(stack, consumer, i, j);
lid.render(stack, consumer, i, j);
latch.render(stack, consumer, i, j);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package net.kyrptonaught.linkedstorage.block;
package net.kyrptonaught.linkedstorage.client;

import net.kyrptonaught.linkedstorage.LinkedStorageModClient;
import net.kyrptonaught.linkedstorage.block.StorageBlock;
import net.kyrptonaught.linkedstorage.block.StorageBlockEntity;
import net.minecraft.block.BlockState;
import net.minecraft.client.model.ModelPart;
import net.minecraft.client.render.RenderLayer;
import net.minecraft.client.render.TexturedRenderLayers;
import net.minecraft.client.render.VertexConsumer;
import net.minecraft.client.render.VertexConsumerProvider;
import net.minecraft.client.render.block.entity.BlockEntityRenderDispatcher;
import net.minecraft.client.render.block.entity.BlockEntityRenderer;
import net.minecraft.client.util.SpriteIdentifier;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.client.util.math.Vector3f;
import net.minecraft.util.DyeColor;
Expand All @@ -15,18 +21,11 @@


public class StorageBlockRenderer extends BlockEntityRenderer<StorageBlockEntity> {

private static final Identifier WOOL_TEXTURE = new Identifier("textures/block/white_wool.png");
private ModelPart button1, button2, button3;
private static final Identifier TEXTURE = new Identifier("textures/block/white_wool.png");

public StorageBlockRenderer(BlockEntityRenderDispatcher dispatcher) {
super(dispatcher);
button1 = new ModelPart(64, 64, 0, 19);
button1.addCuboid(10, 14, 6, 2, 1, 4, 0);
button2 = new ModelPart(64, 64, 0, 19);
button2.addCuboid(7, 14, 6, 2, 1, 4, 0);
button3 = new ModelPart(64, 64, 0, 19);
button3.addCuboid(4, 14, 6, 2, 1, 4, 0);

}

Expand All @@ -41,15 +40,22 @@ public void render(StorageBlockEntity blockEntity, float tickDelta, MatrixStack
BlockPos pos = blockEntity.getPos();
BlockState state = world.getBlockState(pos);

LinkedChestModel model = new LinkedChestModel();

matrices.push();
float f = state.get(StorageBlock.FACING).getOpposite().asRotation();
float f = state.get(StorageBlock.FACING).asRotation();
matrices.translate(0.5D, 0.5D, 0.5D);
matrices.multiply(Vector3f.POSITIVE_Y.getDegreesQuaternion(-f));
matrices.translate(-0.5D, -0.5D, -0.5D);

button1.render(matrices, vertexConsumers.getBuffer(RenderLayer.getEntityCutout(TEXTURE)), light, overlay, color1[0], color1[1], color1[2], 1);
button2.render(matrices, vertexConsumers.getBuffer(RenderLayer.getEntityCutout(TEXTURE)), light, overlay, color2[0], color2[1], color2[2], 1);
button3.render(matrices, vertexConsumers.getBuffer(RenderLayer.getEntityCutout(TEXTURE)), light, overlay, color3[0], color3[1], color3[2], 1);
model.setLidPitch(blockEntity.getAnimationProgress(tickDelta));
SpriteIdentifier spriteIdentifier = new SpriteIdentifier(TexturedRenderLayers.CHEST_ATLAS_TEXTURE, LinkedStorageModClient.TEXTURE);
VertexConsumer vertexConsumer = spriteIdentifier.getVertexConsumer(vertexConsumers, RenderLayer::getEntityCutout);
model.render(matrices, vertexConsumer, light, overlay);

model.button1.render(matrices, vertexConsumers.getBuffer(RenderLayer.getEntityCutout(WOOL_TEXTURE)), light, overlay, color1[0], color1[1], color1[2], 1);
model.button2.render(matrices, vertexConsumers.getBuffer(RenderLayer.getEntityCutout(WOOL_TEXTURE)), light, overlay, color2[0], color2[1], color2[2], 1);
model.button3.render(matrices, vertexConsumers.getBuffer(RenderLayer.getEntityCutout(WOOL_TEXTURE)), light, overlay, color3[0], color3[1], color3[2], 1);
matrices.pop();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package net.kyrptonaught.linkedstorage.inventory;

import net.minecraft.container.GenericContainer;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.util.math.BlockPos;

public class LinkedContainer extends GenericContainer {
public BlockPos linkedBlock;

public LinkedContainer(int syncId, PlayerInventory playerInventory, Inventory inventory, BlockPos linkedBlock) {
super(null, syncId, playerInventory, inventory, 3);
this.linkedBlock = linkedBlock;
}
}
Loading

0 comments on commit a5f2346

Please sign in to comment.