Skip to content

Commit

Permalink
use lookup api for client. Closes #22
Browse files Browse the repository at this point in the history
  • Loading branch information
ProjectET committed Jun 18, 2022
1 parent 5b73a9e commit a597a56
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import com.direwolf20.buildinggadgets.common.blocks.EffectBlock;
import com.direwolf20.buildinggadgets.common.items.modes.AbstractMode;
import com.direwolf20.buildinggadgets.common.items.modes.BuildingModes;
import com.direwolf20.buildinggadgets.common.network.C2S.PacketBindTool;
import com.direwolf20.buildinggadgets.common.network.C2S.PacketRotateMirror;
import com.direwolf20.buildinggadgets.common.network.S2C.LookupResult;
import com.direwolf20.buildinggadgets.common.tainted.building.BlockData;
import com.direwolf20.buildinggadgets.common.tainted.building.view.BuildContext;
import com.direwolf20.buildinggadgets.common.tainted.inventory.IItemIndex;
Expand All @@ -24,9 +24,9 @@
import com.direwolf20.buildinggadgets.common.util.ref.Reference.TagReference;
import com.direwolf20.buildinggadgets.common.world.MockBuilderWorld;
import com.google.common.collect.ImmutableMultiset;
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.transaction.Transaction;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
Expand Down Expand Up @@ -130,19 +130,12 @@ public void appendHoverText(ItemStack stack, @Nullable Level world, List<Compone
public InteractionResultHolder<ItemStack> use(Level world, Player player, InteractionHand hand) {
//On item use, if sneaking, select the block clicked on, else build -- This is called when you right click a tool NOT on a block.
ItemStack itemstack = player.getItemInHand(hand);
BlockHitResult posLookingAt = VectorHelper.getLookingAt(player, itemstack);

player.startUsingItem(hand);
if (!world.isClientSide) {
boolean lookingAtInventory = ItemStorage.SIDED.find(world, posLookingAt.getBlockPos(), posLookingAt.getDirection()) != null;
// Debug code for free energy
//itemstack.getCapability(CapabilityEnergy.ENERGY).ifPresent(e -> e.receiveEnergy(15000000, false));
if (player.isShiftKeyDown()) {
if (lookingAtInventory) {
LookupResult.sendToClient((ServerPlayer) player, lookingAtInventory);
return InteractionResultHolder.pass(itemstack);
}

InteractionResultHolder<Block> result = selectBlock(itemstack, player);
if (!result.getResult().consumesAction()) {
player.displayClientMessage(MessageTranslation.INVALID_BLOCK.componentTranslation(result.getObject().getName()).setStyle(Styles.AQUA), true);
Expand All @@ -154,6 +147,10 @@ public InteractionResultHolder<ItemStack> use(Level world, Player player, Intera
} else {
if (!player.isShiftKeyDown()) {
BaseRenderer.updateInventoryCache();
} else {
if (Screen.hasControlDown()) {
PacketBindTool.send();
}
}
}
return new InteractionResultHolder<>(InteractionResult.SUCCESS, itemstack);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
import com.direwolf20.buildinggadgets.client.renders.BaseRenderer;
import com.direwolf20.buildinggadgets.client.screen.GuiMod;
import com.direwolf20.buildinggadgets.client.screen.tooltip.TemplateData;
import com.direwolf20.buildinggadgets.client.screen.tooltip.TemplateTooltip;
import com.direwolf20.buildinggadgets.common.BuildingGadgets;
import com.direwolf20.buildinggadgets.common.commands.ForceUnloadedCommand;
import com.direwolf20.buildinggadgets.common.commands.OverrideBuildSizeCommand;
import com.direwolf20.buildinggadgets.common.commands.OverrideCopySizeCommand;
import com.direwolf20.buildinggadgets.common.component.BGComponent;
import com.direwolf20.buildinggadgets.common.network.C2S.PacketBindTool;
import com.direwolf20.buildinggadgets.common.network.S2C.LookupResult;
import com.direwolf20.buildinggadgets.common.network.Target;
import com.direwolf20.buildinggadgets.common.tainted.building.PlacementChecker;
import com.direwolf20.buildinggadgets.common.tainted.building.Region;
Expand All @@ -33,11 +31,9 @@
import com.direwolf20.buildinggadgets.common.util.ref.Reference.TagReference;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableSortedSet;
import com.mojang.blaze3d.platform.InputConstants;
import net.fabricmc.fabric.api.transfer.v1.context.ContainerItemContext;
import net.fabricmc.fabric.api.transfer.v1.item.ItemStorage;
import net.fabricmc.fabric.api.util.NbtType;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
Expand All @@ -47,7 +43,6 @@
import net.minecraft.network.protocol.PacketFlow;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.Container;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.player.Player;
Expand Down Expand Up @@ -275,11 +270,10 @@ public InteractionResultHolder<ItemStack> use(Level world, Player player, Intera
player.startUsingItem(hand);

BlockHitResult posLookingAt = VectorHelper.getLookingAt(player, stack);
boolean lookingAtInventory = ItemStorage.SIDED.find(world, posLookingAt.getBlockPos(), posLookingAt.getDirection()) != null;

if (!world.isClientSide()) {
boolean lookingAtInventory = ItemStorage.SIDED.find(world, posLookingAt.getBlockPos(), posLookingAt.getDirection()) != null;
if (player.isShiftKeyDown() && lookingAtInventory) {
LookupResult.sendToClient((ServerPlayer) player, lookingAtInventory);
return InteractionResultHolder.pass(stack);
}

Expand All @@ -291,6 +285,11 @@ public InteractionResultHolder<ItemStack> use(Level world, Player player, Intera
getActivePos(player, stack).ifPresent(pos -> build(stack, world, player, pos, hand));
}
} else {
if (player.isShiftKeyDown() && Screen.hasControlDown() && lookingAtInventory) {
PacketBindTool.send();
return InteractionResultHolder.pass(stack);
}

if (getToolMode(stack) == ToolMode.COPY) {
if (player.isShiftKeyDown() && world.getBlockState(posLookingAt.getBlockPos()) == Blocks.AIR.defaultBlockState()) {
GuiMod.COPY.openScreen(player);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import com.direwolf20.buildinggadgets.common.enchants.GadgetSilkTouch;
import com.direwolf20.buildinggadgets.common.items.modes.AbstractMode;
import com.direwolf20.buildinggadgets.common.items.modes.ExchangingModes;
import com.direwolf20.buildinggadgets.common.network.C2S.PacketBindTool;
import com.direwolf20.buildinggadgets.common.network.C2S.PacketRotateMirror;
import com.direwolf20.buildinggadgets.common.network.S2C.LookupResult;
import com.direwolf20.buildinggadgets.common.tainted.building.BlockData;
import com.direwolf20.buildinggadgets.common.tainted.building.tilesupport.ITileEntityData;
import com.direwolf20.buildinggadgets.common.tainted.building.tilesupport.TileSupport;
Expand All @@ -28,10 +28,10 @@
import com.google.common.collect.ImmutableMultiset;
import com.google.common.collect.LinkedHashMultiset;
import com.google.common.collect.Multiset;
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.transaction.Transaction;
import net.fabricmc.fabric.api.transfer.v1.transaction.TransactionContext;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Registry;
import net.minecraft.nbt.CompoundTag;
Expand Down Expand Up @@ -143,18 +143,11 @@ public void appendHoverText(ItemStack stack, @Nullable Level world, List<Compone
@Override
public InteractionResultHolder<ItemStack> use(Level world, Player player, InteractionHand hand) {
ItemStack itemstack = player.getItemInHand(hand);
BlockHitResult posLookingAt = VectorHelper.getLookingAt(player, itemstack);

player.startUsingItem(hand);

if (!world.isClientSide) {
boolean lookingAtInventory = ItemStorage.SIDED.find(world, posLookingAt.getBlockPos(), posLookingAt.getDirection()) != null;
if (player.isShiftKeyDown()) {
if (lookingAtInventory) {
LookupResult.sendToClient((ServerPlayer) player, lookingAtInventory);
return InteractionResultHolder.pass(itemstack);
}

InteractionResultHolder<Block> result = selectBlock(itemstack, player);
if (!result.getResult().consumesAction()) {
player.displayClientMessage(MessageTranslation.INVALID_BLOCK.componentTranslation(Registry.BLOCK.getKey(result.getObject())).setStyle(Styles.AQUA), true);
Expand All @@ -169,6 +162,10 @@ public InteractionResultHolder<ItemStack> use(Level world, Player player, Intera
} else {
if (!player.isShiftKeyDown()) {
BaseRenderer.updateInventoryCache();
} else {
if (Screen.hasControlDown()) {
PacketBindTool.send();
}
}
}
return new InteractionResultHolder<>(InteractionResult.SUCCESS, itemstack);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.direwolf20.buildinggadgets.common.network;

import com.direwolf20.buildinggadgets.common.network.S2C.LookupResult;
import com.direwolf20.buildinggadgets.common.network.bidirection.PacketSetRemoteInventoryCache;
import com.direwolf20.buildinggadgets.common.network.bidirection.PacketRequestTemplate;
import com.direwolf20.buildinggadgets.common.network.bidirection.PacketSetRemoteInventoryCache;
import com.direwolf20.buildinggadgets.common.network.bidirection.SplitPacketUpdateTemplate;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;

Expand All @@ -12,6 +11,5 @@ public static void registerMessages() {
ClientPlayNetworking.registerGlobalReceiver(PacketHandler.PacketRequestTemplate, new PacketRequestTemplate.Client());
ClientPlayNetworking.registerGlobalReceiver(PacketHandler.SplitPacketUpdateTemplate, new SplitPacketUpdateTemplate.Client());
ClientPlayNetworking.registerGlobalReceiver(PacketHandler.PacketSetRemoteInventoryCache, new PacketSetRemoteInventoryCache.Client());
ClientPlayNetworking.registerGlobalReceiver(PacketHandler.PacketLookupResult, new LookupResult.Client());
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ public static Optional<Storage<ItemVariant>> getLinkedInventory(Level world, Inv
return Optional.empty();
}

//TODO: Horrible hack i hope can make this better
Storage<ItemVariant> storage = null;
if(world.getBlockEntity(link.blockPos) instanceof Container) storage = InventoryStorage.of((Container) world.getBlockEntity(link.blockPos), link.face);
Storage<ItemVariant> storage = ItemStorage.SIDED.find(world, link.blockPos, link.face);

if (storage == null) {
// Unlink if the storage no longer exists
Expand Down

0 comments on commit a597a56

Please sign in to comment.