Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: wrench covers from the back #3664

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 31 additions & 10 deletions src/main/java/com/refinedmods/refinedstorage/item/WrenchItem.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,42 @@
package com.refinedmods.refinedstorage.item;

import com.refinedmods.refinedstorage.RS;
import com.refinedmods.refinedstorage.api.network.INetwork;
import com.refinedmods.refinedstorage.api.network.node.ICoverable;
import com.refinedmods.refinedstorage.api.network.node.INetworkNode;
import com.refinedmods.refinedstorage.api.network.security.Permission;
import com.refinedmods.refinedstorage.apiimpl.network.node.cover.Cover;
import com.refinedmods.refinedstorage.util.NetworkUtils;
import com.refinedmods.refinedstorage.util.LevelUtils;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.block.Rotation;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.neoforged.neoforge.items.ItemHandlerHelper;
import org.joml.Vector3f;
import javax.annotation.Nullable;

public class WrenchItem extends Item {
public WrenchItem() {
super(new Item.Properties().stacksTo(1));
}

@Nullable
private static Direction getDirectionFromClickLocation(Vector3f location, @Nullable BlockEntity entity) {
if (entity == null) {
return null;
}
BlockPos pos = entity.getBlockPos();
Vector3f centered = new Vector3f((float)pos.getX() + 0.5f, (float)pos.getY() + 0.5f, (float)pos.getZ() + 0.5f);
Vector3f subbed = location.sub(centered);
Vector3f normalized = subbed.normalize();
return Direction.getNearest(normalized.x, normalized.y, normalized.z);
}

@Override
public InteractionResult onItemUseFirst(ItemStack stack, UseOnContext ctx) {
if (ctx.getLevel().isClientSide) {
Expand All @@ -36,15 +52,20 @@ public InteractionResult onItemUseFirst(ItemStack stack, UseOnContext ctx) {
}
BlockState state = ctx.getLevel().getBlockState(ctx.getClickedPos());

if (node instanceof ICoverable && ((ICoverable) node).getCoverManager().hasCover(ctx.getClickedFace())) {
Cover cover = ((ICoverable) node).getCoverManager().removeCover(ctx.getClickedFace());
if (cover != null) {
ItemStack stack1 = cover.getType().createStack();
CoverItem.setItem(stack1, cover.getStack());
ItemHandlerHelper.giveItemToPlayer(ctx.getPlayer(), stack1);
ctx.getLevel().sendBlockUpdated(ctx.getClickedPos(), state, state, 3);
ctx.getLevel().updateNeighborsAt(ctx.getClickedPos(), ctx.getLevel().getBlockState(ctx.getClickedPos()).getBlock());
return InteractionResult.SUCCESS;
if (node instanceof ICoverable) {
Vector3f location = ctx.getClickLocation().toVector3f();
BlockEntity entity = ctx.getLevel().getBlockEntity(ctx.getClickedPos());
Direction clickedSide = getDirectionFromClickLocation(location, entity);
if (((ICoverable) node).getCoverManager().hasCover(clickedSide)) {
Cover cover = ((ICoverable) node).getCoverManager().removeCover(clickedSide);
if (cover != null) {
ItemStack stack1 = cover.getType().createStack();
CoverItem.setItem(stack1, cover.getStack());
ItemHandlerHelper.giveItemToPlayer(ctx.getPlayer(), stack1);
ctx.getLevel().sendBlockUpdated(ctx.getClickedPos(), state, state, 3);
ctx.getLevel().updateNeighborsAt(ctx.getClickedPos(), ctx.getLevel().getBlockState(ctx.getClickedPos()).getBlock());
return InteractionResult.SUCCESS;
}
}
}

Expand Down