Skip to content

Commit

Permalink
Make being a network component depend on a tag
Browse files Browse the repository at this point in the history
  • Loading branch information
MattiDragon committed Apr 22, 2022
1 parent 57df888 commit 736de36
Show file tree
Hide file tree
Showing 16 changed files with 60 additions and 62 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.github.mattidragon.extendeddrawers.block;

import io.github.mattidragon.extendeddrawers.block.base.Lockable;
import io.github.mattidragon.extendeddrawers.block.base.NetworkComponent;
import io.github.mattidragon.extendeddrawers.util.NetworkHelper;
import net.fabricmc.fabric.api.transfer.v1.item.ItemStorage;
import net.fabricmc.fabric.api.transfer.v1.item.ItemVariant;
import net.fabricmc.fabric.api.transfer.v1.item.PlayerInventoryStorage;
Expand All @@ -21,13 +21,12 @@

import static io.github.mattidragon.extendeddrawers.util.DrawerInteractionStatusManager.getAndResetInsertStatus;

@SuppressWarnings("UnstableApiUsage")
public class ControllerBlock extends Block implements Lockable, NetworkComponent {
@SuppressWarnings({"UnstableApiUsage", "deprecation"}) // transfer api and mojank block method deprecation
public class ControllerBlock extends Block implements Lockable {
public ControllerBlock(Settings settings) {
super(settings);

//noinspection UnstableApiUsage
ItemStorage.SIDED.registerForBlocks((world, pos, state, entity, dir) -> new CombinedStorage<>(NetworkComponent.getConnectedStorages(world, pos)), this);
ItemStorage.SIDED.registerForBlocks((world, pos, state, entity, dir) -> new CombinedStorage<>(NetworkHelper.getConnectedStorages(world, pos)), this);
}

@Override
Expand Down Expand Up @@ -58,7 +57,7 @@ public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEnt

@Override
public ActionResult toggleLock(BlockState state, World world, BlockPos pos, Vec3d hitPos, Direction side) {
var storages = NetworkComponent.getConnectedStorages(world, pos);
var storages = NetworkHelper.getConnectedStorages(world, pos);
var stateSum = storages.stream()
.map(storage -> storage.locked)
.reduce(0, (count, value) -> count + (value ? 1 : -1), Integer::sum);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

import io.github.mattidragon.extendeddrawers.block.base.BaseBlock;
import io.github.mattidragon.extendeddrawers.block.base.Lockable;
import io.github.mattidragon.extendeddrawers.block.base.NetworkComponent;
import io.github.mattidragon.extendeddrawers.block.base.Upgradable;
import io.github.mattidragon.extendeddrawers.block.entity.DrawerBlockEntity;
import io.github.mattidragon.extendeddrawers.drawer.DrawerSlot;
import io.github.mattidragon.extendeddrawers.item.UpgradeItem;
import io.github.mattidragon.extendeddrawers.registry.ModBlocks;
import io.github.mattidragon.extendeddrawers.util.DrawerRaycastUtil;
Expand Down Expand Up @@ -37,7 +35,7 @@
import static io.github.mattidragon.extendeddrawers.util.DrawerInteractionStatusManager.getAndResetInsertStatus;

@SuppressWarnings({"UnstableApiUsage", "deprecation"}) // transfer api and mojank block method deprecation
public class DrawerBlock extends BaseBlock<DrawerBlockEntity> implements Lockable, Upgradable, NetworkComponent {
public class DrawerBlock extends BaseBlock<DrawerBlockEntity> implements Lockable, Upgradable {
public static final DirectionProperty FACING = Properties.HORIZONTAL_FACING;

public final int slots;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.github.mattidragon.extendeddrawers.block;

import io.github.mattidragon.extendeddrawers.block.base.BaseBlock;
import io.github.mattidragon.extendeddrawers.block.base.NetworkComponent;
import io.github.mattidragon.extendeddrawers.block.entity.ShadowDrawerBlockEntity;
import io.github.mattidragon.extendeddrawers.registry.ModBlocks;
import io.github.mattidragon.extendeddrawers.util.DrawerRaycastUtil;
Expand All @@ -14,7 +13,6 @@
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.DirectionProperty;
Expand All @@ -32,8 +30,8 @@
import static io.github.mattidragon.extendeddrawers.util.DrawerInteractionStatusManager.getAndResetExtractionTimer;
import static io.github.mattidragon.extendeddrawers.util.DrawerInteractionStatusManager.getAndResetInsertStatus;

@SuppressWarnings("UnstableApiUsage")
public class ShadowDrawerBlock extends BaseBlock<ShadowDrawerBlockEntity> implements NetworkComponent {
@SuppressWarnings({"UnstableApiUsage", "deprecation"}) // transfer api and mojank block method deprecation
public class ShadowDrawerBlock extends BaseBlock<ShadowDrawerBlockEntity> {
public static final DirectionProperty FACING = Properties.HORIZONTAL_FACING;

public ShadowDrawerBlock(Settings settings) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package io.github.mattidragon.extendeddrawers.block.entity;

import io.github.mattidragon.extendeddrawers.block.DrawerBlock;
import io.github.mattidragon.extendeddrawers.drawer.DrawerSlot;
import io.github.mattidragon.extendeddrawers.block.ShadowDrawerBlock;
import io.github.mattidragon.extendeddrawers.block.base.NetworkComponent;
import io.github.mattidragon.extendeddrawers.drawer.DrawerSlot;
import io.github.mattidragon.extendeddrawers.item.UpgradeItem;
import io.github.mattidragon.extendeddrawers.registry.ModBlocks;
import io.github.mattidragon.extendeddrawers.util.NetworkHelper;
import net.fabricmc.fabric.api.transfer.v1.item.ItemStorage;
import net.fabricmc.fabric.api.transfer.v1.item.ItemVariant;
import net.fabricmc.fabric.api.transfer.v1.storage.Storage;
Expand Down Expand Up @@ -44,7 +44,7 @@ private void onSlotChanged() {
markDirty();
assert world != null;
world.updateListeners(pos, getCachedState(), getCachedState(), Block.NOTIFY_LISTENERS);
NetworkComponent.findConnectedComponents(world, pos, (world1, pos1) -> world1.getBlockState(pos1).getBlock() instanceof ShadowDrawerBlock)
NetworkHelper.findConnectedComponents(world, pos, (world1, pos1) -> world1.getBlockState(pos1).getBlock() instanceof ShadowDrawerBlock)
.forEach(pos1 -> {
var state1 = world.getBlockState(pos1);
world.updateListeners(pos1, state1, state1, Block.NOTIFY_LISTENERS);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.github.mattidragon.extendeddrawers.block.entity;

import io.github.mattidragon.extendeddrawers.block.base.NetworkComponent;
import io.github.mattidragon.extendeddrawers.registry.ModBlocks;
import io.github.mattidragon.extendeddrawers.util.NetworkHelper;
import net.fabricmc.fabric.api.transfer.v1.item.ItemStorage;
import net.fabricmc.fabric.api.transfer.v1.item.ItemVariant;
import net.fabricmc.fabric.api.transfer.v1.storage.Storage;
Expand All @@ -28,7 +28,7 @@ public ShadowDrawerBlockEntity(BlockPos pos, BlockState state) {
}

public Storage<ItemVariant> createStorage() {
return new CombinedStorage<>(NetworkComponent.getConnectedStorages(world, pos).stream().filter(slot -> slot.item.equals(item) || slot.item.isBlank()).toList());
return new CombinedStorage<>(NetworkHelper.getConnectedStorages(world, pos).stream().filter(slot -> slot.item.equals(item) || slot.item.isBlank()).toList());
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
package io.github.mattidragon.extendeddrawers.client.renderer;

import com.google.common.collect.Collections2;
import com.ibm.icu.impl.locale.XCldrStub;
import io.github.mattidragon.extendeddrawers.block.DrawerBlock;
import io.github.mattidragon.extendeddrawers.drawer.DrawerSlot;
import io.github.mattidragon.extendeddrawers.block.entity.DrawerBlockEntity;
import io.github.mattidragon.extendeddrawers.item.UpgradeItem;
import io.github.mattidragon.extendeddrawers.registry.ModItems;
import io.github.mattidragon.extendeddrawers.util.CollectionUtils;
import io.github.mattidragon.extendeddrawers.drawer.DrawerSlot;
import net.fabricmc.fabric.api.renderer.v1.RendererAccess;
import net.fabricmc.fabric.api.renderer.v1.mesh.MutableQuadView;
import net.minecraft.client.MinecraftClient;
Expand All @@ -25,12 +20,9 @@
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.Matrix4f;
import net.minecraft.util.math.Vec3f;
import org.apache.commons.compress.utils.Lists;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Objects;
import java.util.function.Predicate;
import java.util.stream.StreamSupport;

import static io.github.mattidragon.extendeddrawers.ExtendedDrawers.id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import io.github.mattidragon.extendeddrawers.block.ShadowDrawerBlock;
import io.github.mattidragon.extendeddrawers.block.entity.ShadowDrawerBlockEntity;
import net.fabricmc.fabric.api.transfer.v1.storage.StorageUtil;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.OverlayTexture;
import net.minecraft.client.render.VertexConsumerProvider;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.github.mattidragon.extendeddrawers.datagen;

import io.github.mattidragon.extendeddrawers.registry.ModBlocks;
import io.github.mattidragon.extendeddrawers.registry.ModTags;
import net.fabricmc.fabric.api.datagen.v1.FabricDataGenerator;
import net.fabricmc.fabric.api.datagen.v1.provider.FabricTagProvider;

Expand All @@ -10,5 +12,7 @@ public DrawersBlockTagProvider(FabricDataGenerator dataGenerator) {

@Override
protected void generateTags() {
getOrCreateTagBuilder(ModTags.BlockTags.DRAWERS).add(ModBlocks.SHADOW_DRAWER, ModBlocks.SINGLE_DRAWER, ModBlocks.DOUBLE_DRAWER, ModBlocks.QUAD_DRAWER);
getOrCreateTagBuilder(ModTags.BlockTags.NETWORK_COMPONENTS).addTag(ModTags.BlockTags.DRAWERS).add(ModBlocks.CONTROLLER, ModBlocks.CONNECTOR);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.mattidragon.extendeddrawers.datagen;

import io.github.mattidragon.extendeddrawers.registry.ModItems;
import io.github.mattidragon.extendeddrawers.registry.ModTags;
import net.fabricmc.fabric.api.datagen.v1.FabricDataGenerator;
import net.fabricmc.fabric.api.datagen.v1.provider.FabricTagProvider;
import org.jetbrains.annotations.Nullable;
Expand All @@ -12,6 +13,6 @@ public DrawersItemTagProvider(FabricDataGenerator dataGenerator, @Nullable Block

@Override
protected void generateTags() {
getOrCreateTagBuilder(ExtendedDrawersDatagen.DRAWERS).add(ModItems.SHADOW_DRAWER, ModItems.SINGLE_DRAWER, ModItems.DOUBLE_DRAWER, ModItems.QUAD_DRAWER);
getOrCreateTagBuilder(ModTags.ItemTags.DRAWERS).add(ModItems.SHADOW_DRAWER, ModItems.SINGLE_DRAWER, ModItems.DOUBLE_DRAWER, ModItems.QUAD_DRAWER);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import io.github.mattidragon.extendeddrawers.registry.ModItems;
import net.fabricmc.fabric.api.datagen.v1.FabricDataGenerator;
import net.fabricmc.fabric.api.datagen.v1.provider.FabricModelProvider;
import net.minecraft.data.client.*;
import net.minecraft.data.client.BlockStateModelGenerator;
import net.minecraft.data.client.ItemModelGenerator;
import net.minecraft.data.client.Models;
import net.minecraft.data.client.TextureMap;

import static io.github.mattidragon.extendeddrawers.ExtendedDrawers.id;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.mattidragon.extendeddrawers.datagen;

import io.github.mattidragon.extendeddrawers.registry.ModItems;
import io.github.mattidragon.extendeddrawers.registry.ModTags;
import net.fabricmc.fabric.api.datagen.v1.FabricDataGenerator;
import net.fabricmc.fabric.api.datagen.v1.provider.FabricRecipeProvider;
import net.fabricmc.fabric.api.tag.convention.v1.ConventionalItemTags;
Expand Down Expand Up @@ -87,7 +88,7 @@ private void offerControllerRecipe(Consumer<RecipeJsonProvider> exporter) {
ShapedRecipeJsonBuilder.create(ModItems.CONTROLLER)
.input('I', Items.IRON_INGOT)
.input('C', Items.COBBLESTONE)
.input('D', ExtendedDrawersDatagen.DRAWERS)
.input('D', ModTags.ItemTags.DRAWERS)
.pattern("CIC")
.pattern("IDI")
.pattern("CIC")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,8 @@

import net.fabricmc.fabric.api.datagen.v1.DataGeneratorEntrypoint;
import net.fabricmc.fabric.api.datagen.v1.FabricDataGenerator;
import net.minecraft.item.Item;
import net.minecraft.tag.TagKey;
import net.minecraft.util.registry.Registry;

import static io.github.mattidragon.extendeddrawers.ExtendedDrawers.id;

public class ExtendedDrawersDatagen implements DataGeneratorEntrypoint {
public static final TagKey<Item> DRAWERS = TagKey.of(Registry.ITEM_KEY, id("drawers"));

@Override
public void onInitializeDataGenerator(FabricDataGenerator dataGenerator) {
dataGenerator.addProvider(DrawersModelProvider::new);
Expand All @@ -19,5 +12,4 @@ public void onInitializeDataGenerator(FabricDataGenerator dataGenerator) {
var blockTagProvider = dataGenerator.addProvider(DrawersBlockTagProvider::new);
dataGenerator.addProvider(new DrawersItemTagProvider(dataGenerator, blockTagProvider));
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package io.github.mattidragon.extendeddrawers.registry;

import io.github.mattidragon.extendeddrawers.block.ConnectorBlock;
import io.github.mattidragon.extendeddrawers.block.ControllerBlock;
import io.github.mattidragon.extendeddrawers.block.DrawerBlock;
import io.github.mattidragon.extendeddrawers.block.ShadowDrawerBlock;
import io.github.mattidragon.extendeddrawers.block.entity.DrawerBlockEntity;
import io.github.mattidragon.extendeddrawers.block.entity.ShadowDrawerBlockEntity;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.fabricmc.fabric.api.object.builder.v1.block.entity.FabricBlockEntityTypeBuilder;
import net.minecraft.block.Block;
import net.minecraft.block.MapColor;
import net.minecraft.block.Material;
import net.minecraft.block.entity.BlockEntityType;
Expand All @@ -20,7 +20,7 @@ public class ModBlocks {
public static final DrawerBlock SINGLE_DRAWER = new DrawerBlock(FabricBlockSettings.of(Material.WOOD).strength(2f, 3f).sounds(BlockSoundGroup.WOOD), 1);
public static final DrawerBlock DOUBLE_DRAWER = new DrawerBlock(FabricBlockSettings.of(Material.WOOD).strength(2f, 3f).sounds(BlockSoundGroup.WOOD), 2);
public static final DrawerBlock QUAD_DRAWER = new DrawerBlock(FabricBlockSettings.of(Material.WOOD).strength(2f, 3f).sounds(BlockSoundGroup.WOOD), 4);
public static final ConnectorBlock CONNECTOR = new ConnectorBlock(FabricBlockSettings.of(Material.WOOD).strength(2f, 3f).sounds(BlockSoundGroup.WOOD));
public static final Block CONNECTOR = new Block(FabricBlockSettings.of(Material.WOOD).strength(2f, 3f).sounds(BlockSoundGroup.WOOD));
public static final ControllerBlock CONTROLLER = new ControllerBlock(FabricBlockSettings.of(Material.STONE).strength(3f, 9f).sounds(BlockSoundGroup.STONE));
public static final ShadowDrawerBlock SHADOW_DRAWER = new ShadowDrawerBlock(FabricBlockSettings.of(Material.STONE, MapColor.PALE_YELLOW).strength(3f, 9f).sounds(BlockSoundGroup.STONE));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.github.mattidragon.extendeddrawers.registry;

import net.minecraft.block.Block;
import net.minecraft.item.Item;
import net.minecraft.tag.TagKey;
import net.minecraft.util.registry.Registry;

import static io.github.mattidragon.extendeddrawers.ExtendedDrawers.id;

public class ModTags {
public static class ItemTags {
public static final TagKey<Item> DRAWERS = TagKey.of(Registry.ITEM_KEY, id("drawers"));

}

public static class BlockTags {
public static final TagKey<Block> DRAWERS = TagKey.of(Registry.BLOCK_KEY, id("drawers"));
public static final TagKey<Block> NETWORK_COMPONENTS = TagKey.of(Registry.BLOCK_KEY, id("network_components"));

}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package io.github.mattidragon.extendeddrawers.block.base;
package io.github.mattidragon.extendeddrawers.util;

import com.google.common.collect.Queues;
import io.github.mattidragon.extendeddrawers.block.DrawerBlock;
import io.github.mattidragon.extendeddrawers.drawer.DrawerSlot;
import io.github.mattidragon.extendeddrawers.block.entity.DrawerBlockEntity;
import io.github.mattidragon.extendeddrawers.drawer.DrawerSlot;
import io.github.mattidragon.extendeddrawers.registry.ModTags;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.World;

import java.util.*;
import java.util.function.BiPredicate;

public interface NetworkComponent {

static Optional<BlockPos> findFirstConnectedComponent(World world, BlockPos pos, BiPredicate<World, BlockPos> predicate) {
public class NetworkHelper {
public static Optional<BlockPos> findFirstConnectedComponent(World world, BlockPos pos, BiPredicate<World, BlockPos> predicate) {
var toSearch = Queues.newArrayDeque(Collections.singleton(pos));
var searched = new ArrayList<>();

Expand All @@ -22,7 +22,7 @@ static Optional<BlockPos> findFirstConnectedComponent(World world, BlockPos pos,
searched.add(searching);

var state = world.getBlockState(searching);
if (!(state.getBlock() instanceof NetworkComponent))
if (!state.isIn(ModTags.BlockTags.NETWORK_COMPONENTS))
continue;
if (predicate.test(world, searching))
return Optional.of(searching);
Expand All @@ -37,7 +37,7 @@ static Optional<BlockPos> findFirstConnectedComponent(World world, BlockPos pos,
return Optional.empty();
}

static List<BlockPos> findConnectedComponents(World world, BlockPos pos, BiPredicate<World, BlockPos> predicate) {
public static List<BlockPos> findConnectedComponents(World world, BlockPos pos, BiPredicate<World, BlockPos> predicate) {
var toSearch = Queues.newArrayDeque(Collections.singleton(pos));
var searched = new ArrayList<>();
var found = new ArrayList<BlockPos>();
Expand All @@ -47,7 +47,7 @@ static List<BlockPos> findConnectedComponents(World world, BlockPos pos, BiPredi
searched.add(searching);

var state = world.getBlockState(searching);
if (!(state.getBlock() instanceof NetworkComponent))
if (!state.isIn(ModTags.BlockTags.NETWORK_COMPONENTS))
continue;
if (predicate.test(world, searching))
found.add(searching);
Expand All @@ -62,7 +62,7 @@ static List<BlockPos> findConnectedComponents(World world, BlockPos pos, BiPredi
return found;
}

static List<DrawerSlot> getConnectedStorages(World world, BlockPos pos) {
public static List<DrawerSlot> getConnectedStorages(World world, BlockPos pos) {
return findAllDrawers(world, pos).stream()
.map(world::getBlockEntity)
.map(DrawerBlockEntity.class::cast)
Expand All @@ -72,7 +72,7 @@ static List<DrawerSlot> getConnectedStorages(World world, BlockPos pos) {
.toList();
}

static List<BlockPos> findAllDrawers(World world, BlockPos pos) {
return NetworkComponent.findConnectedComponents(world, pos, (world1, pos1) -> world1.getBlockState(pos1).getBlock() instanceof DrawerBlock);
public static List<BlockPos> findAllDrawers(World world, BlockPos pos) {
return NetworkHelper.findConnectedComponents(world, pos, (world1, pos1) -> world1.getBlockState(pos1).getBlock() instanceof DrawerBlock);
}
}

0 comments on commit 736de36

Please sign in to comment.