Skip to content

Commit

Permalink
Opening an chest/inventory now opens all other instances of that inve…
Browse files Browse the repository at this point in the history
…ntory in game for all players

cleanup
  • Loading branch information
kyrptonaught committed Dec 30, 2019
1 parent 76f14ee commit 80d5c71
Show file tree
Hide file tree
Showing 16 changed files with 135 additions and 63 deletions.
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.18:v2
loader_version=0.7.3+build.176

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
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.inventory.LinkedInventoryHelper;
import net.kyrptonaught.linkedstorage.network.ChannelViewers;
import net.kyrptonaught.linkedstorage.network.OpenStoragePacket;
import net.kyrptonaught.linkedstorage.network.SetDyePacket;
import net.kyrptonaught.linkedstorage.register.ModBlocks;
Expand All @@ -13,7 +15,6 @@
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 +25,14 @@ 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(), buf.readBlockPos()));
ContainerProviderRegistry.INSTANCE.registerFactory(new Identifier(MOD_ID, "linkedstorage"), (syncId, id, player, buf) -> getContainer(syncId, player, buf.readByteArray()));
SetDyePacket.registerReceivePacket();
OpenStoragePacket.registerReceivePacket();
ChannelViewers.registerChannelWatcher();
}

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);
static LinkedContainer getContainer(int id, PlayerEntity player, byte[] channel) {
ChannelViewers.addViewerFor(LinkedInventoryHelper.getChannelName(channel), player);
return new LinkedContainer(id, player.inventory, ChannelManager.getManager(player.getEntityWorld().getLevelProperties()).getInv(channel), channel);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,19 @@
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.network.UpdateViewerList;
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);
Expand All @@ -28,8 +27,11 @@ public class LinkedStorageModClient implements ClientModInitializer {
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));
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, channel);
});

ColorProviderRegistryImpl.ITEM.register((stack, layer) -> {
if (layer == 0 || !LinkedInventoryHelper.itemHasChannel(stack))
return DyeColor.WHITE.getMaterialColor().color;
Expand All @@ -39,5 +41,8 @@ public void onInitializeClient() {
ClientSpriteRegistryCallback.event(TexturedRenderLayers.CHEST_ATLAS_TEXTURE).register((atlasTexture, registry) -> {
registry.register(TEXTURE);
});
UpdateViewerList.registerReceivePacket();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,24 @@
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.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType;
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.MathHelper;
import net.minecraft.world.World;

import java.util.List;

@EnvironmentInterfaces({@EnvironmentInterface(value = EnvType.CLIENT, itf = ChestAnimationProgress.class)})
@EnvironmentInterfaces({@EnvironmentInterface(value = EnvType.CLIENT, itf = ChestAnimationProgress.class), @EnvironmentInterface(value = EnvType.CLIENT, itf = Tickable.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;
protected int countViewers() {
return 0;
}

@Override
Expand All @@ -52,9 +34,11 @@ public float getAnimationProgress(float f) {
private float lastAnimationAngle;

@Override
@Environment(EnvType.CLIENT)
public void tick() {
System.out.println(world.isClient);
if (world != null && world.isClient) {
int viewerCount = countViewers(world, this, pos.getX(), pos.getY(), pos.getZ());
int viewerCount = countViewers();
lastAnimationAngle = animationAngle;
if (viewerCount > 0 && animationAngle == 0.0F) playSound(SoundEvents.BLOCK_ENDER_CHEST_OPEN);
if (viewerCount == 0 && animationAngle > 0.0F || viewerCount > 0 && animationAngle < 1.0F) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
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 {
Expand Down Expand Up @@ -135,7 +134,7 @@ public VoxelShape getOutlineShape(BlockState state, BlockView view, BlockPos pos
}

@Environment(EnvType.CLIENT)
public void buildTooltip(ItemStack stack, @Nullable BlockView view, List<Text> tooltip, TooltipContext options) {
public void buildTooltip(ItemStack stack, BlockView view, List<Text> tooltip, TooltipContext options) {
byte[] channel;
if (LinkedInventoryHelper.itemHasChannel(stack))
channel = LinkedInventoryHelper.getItemChannel(stack);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package net.kyrptonaught.linkedstorage.block;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
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.network.ChannelViewers;
import net.kyrptonaught.linkedstorage.util.ChannelManager;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.nbt.CompoundTag;

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

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

public byte[] getChannel() {
return dyeChannel;
}
Expand All @@ -83,7 +80,8 @@ public CompoundTag toClientTag(CompoundTag tag) {
}

@Override
public boolean isPlayerViewing(PlayerEntity player) {
return ((LinkedContainer) player.container).linkedBlock.equals(pos);
@Environment(EnvType.CLIENT)
public int countViewers() {
return ChannelViewers.getViewersFor(LinkedInventoryHelper.getChannelName(dyeChannel)) ? 1 : 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
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> {
public class DummyStorageBlockEntityRenderer extends BlockEntityRenderer<DummyStorageBlockEntity> {
private static final Identifier WOOL_TEXTURE = new Identifier("textures/block/white_wool.png");

public DummyStorageBlockEntityRenderer(BlockEntityRenderDispatcher dispatcher) {
Expand All @@ -38,6 +39,6 @@ public void render(DummyStorageBlockEntity blockEntity, float tickDelta, MatrixS
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();
// matrices.pop();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
public class StorageContainerScreen extends AbstractContainerScreen<Container> {
private static final Identifier TEXTURE = new Identifier("textures/gui/container/generic_54.png");
private final int rows = 3;
public byte[] channel;

public StorageContainerScreen(Container container, PlayerInventory inventory) {
public StorageContainerScreen(Container container, PlayerInventory inventory, byte[] channel) {
super(container, inventory, new TranslatableText("container.linkedstorage"));
this.passEvents = true;
this.containerHeight = 114 + this.rows * 18;
this.channel = channel;
}

public void render(int int_1, int int_2, float float_1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
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 byte[] channel;

public LinkedContainer(int syncId, PlayerInventory playerInventory, Inventory inventory, BlockPos linkedBlock) {
public LinkedContainer(int syncId, PlayerInventory playerInventory, Inventory inventory, byte[] channel) {
super(null, syncId, playerInventory, inventory, 3);
this.linkedBlock = linkedBlock;
this.channel = channel;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ 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");
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
Expand Up @@ -32,7 +32,6 @@ public ActionResult useOnBlock(ItemUsageContext context) {
if (playerEntity.isSneaking() && context.getWorld().getBlockState(context.getBlockPos()).getBlock() instanceof StorageBlock) {
byte[] channel = LinkedInventoryHelper.getBlockChannel(context.getWorld(), context.getBlockPos());
LinkedInventoryHelper.setItemChannel(channel, context.getStack());
context.getPlayer().addChatMessage(new TranslatableText("text.linkedstorage.set", LinkedInventoryHelper.getChannelName(channel)), false);
} else use(context.getWorld(), context.getPlayer(), context.getHand());
}
return ActionResult.SUCCESS;
Expand All @@ -44,10 +43,7 @@ public TypedActionResult<ItemStack> use(World world, PlayerEntity playerEntity,
if (!world.isClient) {
if (LinkedInventoryHelper.itemHasChannel(stack)) {
byte[] channel = LinkedInventoryHelper.getItemChannel(stack);
ContainerProviderRegistry.INSTANCE.openContainer(new Identifier(LinkedStorageMod.MOD_ID, "linkedstorage"), playerEntity, (buf) -> {
buf.writeByteArray(channel);
buf.writeBlockPos(playerEntity.getBlockPos());
});
ContainerProviderRegistry.INSTANCE.openContainer(new Identifier(LinkedStorageMod.MOD_ID, "linkedstorage"), playerEntity, (buf) -> buf.writeByteArray(channel));
}
}
return new TypedActionResult<>(ActionResult.SUCCESS, stack);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package net.kyrptonaught.linkedstorage.network;

import net.fabricmc.fabric.api.event.server.ServerTickCallback;
import net.kyrptonaught.linkedstorage.inventory.LinkedContainer;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.MinecraftServer;

import java.util.HashMap;
import java.util.HashSet;
import java.util.UUID;

public class ChannelViewers {
private static HashMap<String, HashSet<String>> viewers = new HashMap<>();

public static Boolean getViewersFor(String channel) {
if (!viewers.containsKey(channel)) return false;
return viewers.get(channel).size() > 0;
}

static void addViewerFor(String channel, String uuid) {
if (!viewers.containsKey(channel)) viewers.put(channel, new HashSet<>());
viewers.get(channel).add(uuid);
}

public static void addViewerFor(String channel, PlayerEntity player) {
addViewerFor(channel, player.getUuidAsString());
if (!player.world.isClient)
UpdateViewerList.sendPacket(player.getServer(), channel, player.getUuid(), true);
}

static void removeViewerFor(String channel, String player) {
viewers.getOrDefault(channel, new HashSet<>()).remove(player);
}

private static void removeViewerForServer(String channel, String player, MinecraftServer server) {
removeViewerFor(channel, player);
UpdateViewerList.sendPacket(server, channel, UUID.fromString(player), false);
}

public static void registerChannelWatcher() {
ServerTickCallback.EVENT.register(server -> {
for (String channel : ChannelViewers.viewers.keySet())
for (String uuid : ChannelViewers.viewers.get(channel)) {
PlayerEntity player = server.getPlayerManager().getPlayer(UUID.fromString(uuid));
if (player == null || !(player.container instanceof LinkedContainer)) {
removeViewerForServer(channel, uuid, server);
}
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ public static void registerReceivePacket() {
packetContext.getTaskQueue().execute(() -> {
PlayerEntity player = packetContext.getPlayer();
World world = player.getEntityWorld();
ContainerProviderRegistry.INSTANCE.openContainer(new Identifier(LinkedStorageMod.MOD_ID, "linkedstorage"), packetContext.getPlayer(), (buf) -> {
buf.writeByteArray(LinkedInventoryHelper.getBlockChannel(world, pos));
buf.writeBlockPos(pos);
});
ContainerProviderRegistry.INSTANCE.openContainer(new Identifier(LinkedStorageMod.MOD_ID, "linkedstorage"), packetContext.getPlayer(), (buf) -> buf.writeByteArray(LinkedInventoryHelper.getBlockChannel(world, pos)));
});
});
}
Expand Down
Loading

0 comments on commit 80d5c71

Please sign in to comment.