Skip to content

Commit

Permalink
Storage Blocks now render their channels in your inventory
Browse files Browse the repository at this point in the history
  • Loading branch information
kyrptonaught committed Dec 30, 2019
1 parent a5f2346 commit 76f14ee
Show file tree
Hide file tree
Showing 18 changed files with 170 additions and 83 deletions.
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ org.gradle.jvmargs=-Xmx2G
# Fabric Properties
# check these on https://fabricmc.net/use
minecraft_version=1.15.1
yarn_mappings=1.15.1+build.17:v2
loader_version=0.7.2+build.175
yarn_mappings=1.15.1+build.18:v2
loader_version=0.7.3+build.176

# Mod Properties
mod_version=1.0.0b5
Expand All @@ -14,4 +14,4 @@ archives_base_name=linkedstorage

# Dependencies
# currently not on the main fabric site, check on the maven: https://maven.fabricmc.net/net/fabricmc/fabric
fabric_version=0.4.25+build.282-1.15
fabric_version=0.4.26+build.283-1.15
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,27 @@
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.StorageBlockEntity;
import net.kyrptonaught.linkedstorage.client.DummyStorageBlockEntity;
import net.kyrptonaught.linkedstorage.client.DummyStorageBlockEntityRenderer;
import net.kyrptonaught.linkedstorage.client.StorageBlockRenderer;
import net.kyrptonaught.linkedstorage.client.StorageContainerScreen;
import net.kyrptonaught.linkedstorage.inventory.LinkedInventoryHelper;
import net.kyrptonaught.linkedstorage.register.ModBlocks;
import net.kyrptonaught.linkedstorage.register.ModItems;
import net.minecraft.block.entity.BlockEntityType;
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");
public static BlockEntityType<DummyStorageBlockEntity> dummy = BlockEntityType.Builder.create(DummyStorageBlockEntity::new, ModBlocks.storageBlock).build(null);

@Override
public void onInitializeClient() {
BlockEntityRendererRegistry.INSTANCE.register(dummy, DummyStorageBlockEntityRenderer::new);
BlockEntityRendererRegistry.INSTANCE.register(StorageBlock.blockEntity, StorageBlockRenderer::new);
ScreenProviderRegistry.INSTANCE.registerFactory(new Identifier(LinkedStorageMod.MOD_ID, "linkedstorage"), (syncId, identifier, player, buf) ->
new StorageContainerScreen(LinkedStorageMod.getContainer(syncId, player, buf.readByteArray(), buf.readBlockPos()), player.inventory));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@
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;

Expand Down Expand Up @@ -59,21 +56,22 @@ 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) playSound(SoundEvents.BLOCK_ENDER_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);
if (animationAngle < 0.5F && float_2 >= 0.5F) playSound(SoundEvents.BLOCK_ENDER_CHEST_CLOSE);
}
}
}

@Environment(EnvType.CLIENT)
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);
this.world.playSound(d, e, f, soundEvent, SoundCategory.BLOCKS, 0.5F, this.world.random.nextFloat() * 0.1F + 0.9F, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,16 @@
import net.minecraft.block.*;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.client.item.TooltipContext;
import net.minecraft.entity.EntityContext;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.SidedInventory;
import net.minecraft.item.DyeItem;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack;
import net.minecraft.item.*;
import net.minecraft.state.StateManager;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.Identifier;
import net.minecraft.text.Text;
import net.minecraft.text.TranslatableText;
import net.minecraft.util.*;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
Expand All @@ -32,15 +31,18 @@
import net.minecraft.world.IWorld;
import net.minecraft.world.World;

import javax.annotation.Nullable;
import java.util.List;

public class StorageBlock extends HorizontalFacingBlock implements BlockEntityProvider, InventoryProvider {
public static BlockEntityType<StorageBlockEntity> blockEntity;

public StorageBlock(Settings block$Settings_1) {
super(block$Settings_1);
Registry.register(Registry.BLOCK, new Identifier(LinkedStorageMod.MOD_ID, "storageblock"), this);
blockEntity = Registry.register(Registry.BLOCK_ENTITY, LinkedStorageMod.MOD_ID + ":storageblock", BlockEntityType.Builder.create(StorageBlockEntity::new, this).build(null));
Registry.register(Registry.ITEM, new Identifier(LinkedStorageMod.MOD_ID, "storageblock"), new BlockItem(this, new Item.Settings().group(LinkedStorageMod.GROUP)));
this.setDefaultState(this.stateManager.getDefaultState().with(FACING, Direction.NORTH));

}

private boolean didHitButton(VoxelShape button, BlockPos pos, Vec3d hit) {
Expand Down Expand Up @@ -132,6 +134,16 @@ public VoxelShape getOutlineShape(BlockState state, BlockView view, BlockPos pos
return SHAPE;
}

@Environment(EnvType.CLIENT)
public void buildTooltip(ItemStack stack, @Nullable BlockView view, List<Text> tooltip, TooltipContext options) {
byte[] channel;
if (LinkedInventoryHelper.itemHasChannel(stack))
channel = LinkedInventoryHelper.getItemChannel(stack);
else channel = LinkedInventoryHelper.getDefaultChannel();
String name = DyeColor.byId(channel[0]).getName() + ", " + DyeColor.byId(channel[1]).getName() + ", " + DyeColor.byId(channel[2]).getName();
tooltip.add(new TranslatableText("text.linkeditem.channel", name).formatted(Formatting.GRAY));
}

@Override
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
builder.add(FACING);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ private StorageBlockEntity(BlockEntityType<?> blockEntityType_1) {
super(blockEntityType_1);
}

StorageBlockEntity() {
public StorageBlockEntity() {
this(StorageBlock.blockEntity);
}

Expand Down Expand Up @@ -64,6 +64,10 @@ public void setChannel(byte[] channel) {
if (!world.isClient) sync();
}

public void setChannelNoUpdate(byte[] channel) {
this.dyeChannel = channel;
}

public byte[] getChannel() {
return dyeChannel;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package net.kyrptonaught.linkedstorage.client;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.client.rendereregistry.v1.BlockEntityRendererRegistry;
import net.kyrptonaught.linkedstorage.LinkedStorageModClient;
import net.kyrptonaught.linkedstorage.register.ModBlocks;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType;
@Environment(EnvType.CLIENT)
public class DummyStorageBlockEntity extends BlockEntity {
public DummyStorageBlockEntity() {
super(LinkedStorageModClient.dummy);
}

private byte[] dyeChannel;

public void setChannel(byte[] channel) {
this.dyeChannel = channel;
}

byte[] getChannel() {
return dyeChannel;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package net.kyrptonaught.linkedstorage.client;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.kyrptonaught.linkedstorage.LinkedStorageModClient;
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.util.DyeColor;
import net.minecraft.util.Identifier;
@Environment(EnvType.CLIENT)
public class DummyStorageBlockEntityRenderer extends BlockEntityRenderer<DummyStorageBlockEntity> {
private static final Identifier WOOL_TEXTURE = new Identifier("textures/block/white_wool.png");

public DummyStorageBlockEntityRenderer(BlockEntityRenderDispatcher dispatcher) {
super(dispatcher);

}

@Override
public void render(DummyStorageBlockEntity blockEntity, float tickDelta, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light, int overlay) {
byte[] dyes = blockEntity.getChannel();
float[] color1 = DyeColor.byId(dyes[0]).getColorComponents();
float[] color2 = DyeColor.byId(dyes[1]).getColorComponents();
float[] color3 = DyeColor.byId(dyes[2]).getColorComponents();

LinkedChestModel model = new LinkedChestModel();

//matrices.push();
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
@@ -1,10 +1,11 @@
package net.kyrptonaught.linkedstorage.client;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
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;
Expand All @@ -19,10 +20,9 @@
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;


@Environment(EnvType.CLIENT)
public class StorageBlockRenderer extends BlockEntityRenderer<StorageBlockEntity> {
private static final Identifier WOOL_TEXTURE = new Identifier("textures/block/white_wool.png");
private ModelPart button1, button2, button3;

public StorageBlockRenderer(BlockEntityRenderDispatcher dispatcher) {
super(dispatcher);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package net.kyrptonaught.linkedstorage.client;

import com.mojang.blaze3d.platform.GlStateManager;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.gui.screen.ingame.AbstractContainerScreen;
import net.minecraft.container.Container;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.text.TranslatableText;
import net.minecraft.util.Identifier;

@Environment(EnvType.CLIENT)
public class StorageContainerScreen extends AbstractContainerScreen<Container> {
private static final Identifier TEXTURE = new Identifier("textures/gui/container/generic_54.png");
private final int rows = 3;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ public static byte[] getDefaultChannel() {
public static String getChannelName(byte[] dyeChannel) {
return dyeChannel[0] + "" + dyeChannel[1] + "" + dyeChannel[2];
}

public static byte[] getItemChannelOrDefault(ItemStack stack) {
if(itemHasChannel(stack))
return stack.getOrCreateTag().getByteArray("dyechannel");
return getDefaultChannel();
}
public static Boolean itemHasChannel(ItemStack stack) {
CompoundTag tag = stack.getOrCreateTag();
if (tag.contains("dyechannel", 11)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package net.kyrptonaught.linkedstorage.mixin;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.kyrptonaught.linkedstorage.block.StorageBlock;
import net.kyrptonaught.linkedstorage.client.DummyStorageBlockEntity;
import net.kyrptonaught.linkedstorage.inventory.LinkedInventoryHelper;
import net.minecraft.client.render.VertexConsumerProvider;
import net.minecraft.client.render.block.entity.BlockEntityRenderDispatcher;
import net.minecraft.client.render.item.BuiltinModelItemRenderer;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.item.BlockItem;
import net.minecraft.item.ItemStack;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;


@Mixin(BuiltinModelItemRenderer.class)
@Environment(EnvType.CLIENT)
public class StorageBlockItemRenderMixin {
private static DummyStorageBlockEntity DUMMY = new DummyStorageBlockEntity();

@Inject(at = @At("HEAD"), method = "render", cancellable = true)
private void render(ItemStack itemStack, MatrixStack matrixStack, VertexConsumerProvider consumerProvider, int light, int overlay, CallbackInfo info) {
if (itemStack.getItem() instanceof BlockItem && ((BlockItem) itemStack.getItem()).getBlock() instanceof StorageBlock) {
DUMMY.setChannel(LinkedInventoryHelper.getItemChannelOrDefault(itemStack));
BlockEntityRenderDispatcher.INSTANCE.renderEntity(DUMMY, matrixStack, consumerProvider, light, overlay);
info.cancel();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
package net.kyrptonaught.linkedstorage.register;

import net.kyrptonaught.linkedstorage.LinkedStorageMod;
import net.kyrptonaught.linkedstorage.block.StorageBlock;
import net.kyrptonaught.linkedstorage.block.StorageBlockItem;
import net.minecraft.block.Block;
import net.minecraft.block.Material;
import net.minecraft.item.Item;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.Direction;
import net.minecraft.util.registry.Registry;
import net.minecraft.util.shape.VoxelShape;

public class ModBlocks {
public static Block storageBlock;
public static Item storageBlockItem;

public static void register() {
storageBlock = new StorageBlock(Block.Settings.of(Material.METAL).strength(2.5f, 2.5f));
storageBlockItem = Registry.register(Registry.ITEM, new Identifier(LinkedStorageMod.MOD_ID, "storageblock"), new StorageBlockItem(storageBlock, new Item.Settings().group(LinkedStorageMod.GROUP)));
}

public static VoxelShape rotate(Direction facing, double x, double y, double z, double x2, double y2, double z2) {
Expand Down
Loading

0 comments on commit 76f14ee

Please sign in to comment.