Skip to content

Commit

Permalink
Support holding comment item in offhand
Browse files Browse the repository at this point in the history
  • Loading branch information
zbx1425 committed Sep 23, 2023
1 parent eb69a4c commit 5900839
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import cn.zbx1425.worldcomment.Main;
import cn.zbx1425.worldcomment.data.CommentEntry;
import cn.zbx1425.worldcomment.data.network.ImageDownload;
import cn.zbx1425.worldcomment.item.CommentToolItem;
import cn.zbx1425.worldcomment.mixin.LevelRendererAccessor;
import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
import net.minecraft.client.Minecraft;
Expand Down Expand Up @@ -47,7 +48,7 @@ public static void tick(float partialTicks, float hitDistance) {
boolean isPicked = clipPos.isPresent() && clipPos.get().distanceToSqr(pickStart) < vanillaDistSqr;
for (CommentEntry comment : blockData.getValue()) {
boolean isVisible = (comment.messageType - 1) >= 4
|| minecraft.player.getMainHandItem().is(Main.ITEM_COMMENT_TOOL.get());
|| CommentToolItem.Client.getHoldingCommentTool() != null;
if (isVisible) {
visibleComments.computeIfAbsent(comment.location, ignored -> new ArrayList<>()).add(comment);
if (isPicked) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import cn.zbx1425.worldcomment.Main;
import cn.zbx1425.worldcomment.gui.CommentListScreen;
import cn.zbx1425.worldcomment.gui.CommentToolScreen;
import cn.zbx1425.worldcomment.item.CommentToolItem;
import cn.zbx1425.worldcomment.render.OverlayLayer;
import net.minecraft.Util;
import net.minecraft.client.Minecraft;
Expand Down Expand Up @@ -49,8 +50,8 @@ public static boolean handleKeyF2() {
Minecraft minecraft = Minecraft.getInstance();
if (minecraft.player == null) return false;

ItemStack item = minecraft.player.getMainHandItem();
if (!item.is(Main.ITEM_COMMENT_TOOL.get())) return false;
ItemStack item = CommentToolItem.Client.getHoldingCommentTool();
if (item == null) return false;
if (item.getOrCreateTag().contains("uploadJobId", Tag.TAG_LONG)) return false;

if (minecraft.screen == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import cn.zbx1425.worldcomment.data.client.ClientWorldData;
import cn.zbx1425.worldcomment.data.client.ClientRayPicking;
import cn.zbx1425.worldcomment.data.network.ImageDownload;
import cn.zbx1425.worldcomment.item.CommentToolItem;
import cn.zbx1425.worldcomment.network.PacketCollectionRequestC2S;
import cn.zbx1425.worldcomment.network.PacketEntryActionC2S;
import com.mojang.blaze3d.systems.RenderSystem;
Expand Down Expand Up @@ -345,8 +346,7 @@ public static boolean handleKeyF5() {
Minecraft minecraft = Minecraft.getInstance();
if (minecraft.player == null) return false;

ItemStack item = minecraft.player.getMainHandItem();
if (!item.is(Main.ITEM_COMMENT_TOOL.get())) return false;
if (CommentToolItem.Client.getHoldingCommentTool() == null) return false;

minecraft.execute(() -> {
if (minecraft.screen instanceof CommentListScreen) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import cn.zbx1425.worldcomment.Main;
import cn.zbx1425.worldcomment.data.CommentEntry;
import cn.zbx1425.worldcomment.data.network.SubmitDispatcher;
import cn.zbx1425.worldcomment.item.CommentToolItem;
import com.mojang.blaze3d.platform.NativeImage;
import com.mojang.datafixers.types.templates.Check;
import net.minecraft.ChatFormatting;
Expand Down Expand Up @@ -208,8 +209,8 @@ private void sendReport() {
));
Minecraft.getInstance().player.displayClientMessage(
Component.translatable("gui.worldcomment.send_pending"), false);
ItemStack item = Minecraft.getInstance().player.getMainHandItem();
if (item.is(Main.ITEM_COMMENT_TOOL.get())) {
ItemStack item = CommentToolItem.Client.getHoldingCommentTool();
if (item != null) {
item.getOrCreateTag().putLong("uploadJobId", jobId);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,36 +35,55 @@ public CommentToolItem() {
if (!level.isClientSide) return InteractionResultHolder.pass(item);
if (!item.is(Main.ITEM_COMMENT_TOOL.get())) return InteractionResultHolder.fail(item);

if (item.getOrCreateTag().contains("uploadJobId", Tag.TAG_LONG)) {
long jobId = item.getOrCreateTag().getLong("uploadJobId");
HitResult hitResult = Minecraft.getInstance().hitResult;
if (hitResult != null && hitResult.getType() == HitResult.Type.BLOCK) {
BlockHitResult blockHitResult = (BlockHitResult) hitResult;
BlockPos facePos = blockHitResult.getBlockPos().relative(blockHitResult.getDirection());
boolean hasClearance = true;
for (int y = 0; y < 3; y++) {
if (level.getBlockState(facePos.offset(0, y, 0)).isSolid()) {
hasClearance = false;
break;
}
}
if (hasClearance) {
SubmitDispatcher.placeJobAt(jobId, facePos);
item.getOrCreateTag().remove("uploadJobId");
return InteractionResultHolder.success(item);
} else {
player.displayClientMessage(
Component.translatable("gui.worldcomment.send_insufficient_clearance"), false);
}
}
if (Client.placeUploadJob(level, player, item)) {
return InteractionResultHolder.success(item);
} else {

return InteractionResultHolder.fail(item);
}
return InteractionResultHolder.fail(item);
}

@Override
public @NotNull InteractionResult useOn(UseOnContext context) {
return super.useOn(context);
}

public static class Client {

public static ItemStack getHoldingCommentTool() {
Player player = Minecraft.getInstance().player;
if (player == null) return null;
ItemStack mainHandStack = player.getMainHandItem();
if (mainHandStack.is(Main.ITEM_COMMENT_TOOL.get())) return mainHandStack;
ItemStack offHandStack = player.getOffhandItem();
if (offHandStack.is(Main.ITEM_COMMENT_TOOL.get())) return offHandStack;
return null;
}

public static boolean placeUploadJob(Level level, Player player, ItemStack item) {
if (item.getOrCreateTag().contains("uploadJobId", Tag.TAG_LONG)) {
long jobId = item.getOrCreateTag().getLong("uploadJobId");
HitResult hitResult = Minecraft.getInstance().hitResult;
if (hitResult != null && hitResult.getType() == HitResult.Type.BLOCK) {
BlockHitResult blockHitResult = (BlockHitResult) hitResult;
BlockPos facePos = blockHitResult.getBlockPos().relative(blockHitResult.getDirection());
boolean hasClearance = true;
for (int y = 0; y < 3; y++) {
if (level.getBlockState(facePos.offset(0, y, 0)).isSolid()) {
hasClearance = false;
break;
}
}
if (hasClearance) {
SubmitDispatcher.placeJobAt(jobId, facePos);
item.getOrCreateTag().remove("uploadJobId");
return true;
} else {
player.displayClientMessage(
Component.translatable("gui.worldcomment.send_insufficient_clearance"), false);
}
}
}
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import cn.zbx1425.worldcomment.Main;
import cn.zbx1425.worldcomment.data.client.ClientRayPicking;
import cn.zbx1425.worldcomment.gui.IGuiCommon;
import cn.zbx1425.worldcomment.item.CommentToolItem;
import net.minecraft.client.KeyMapping;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.nbt.Tag;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;

import java.util.List;

Expand Down Expand Up @@ -55,8 +57,9 @@ public static void update() {
if (minecraft.player == null) {
return;
}
if (minecraft.player.getMainHandItem().is(Main.ITEM_COMMENT_TOOL.get())) {
if (minecraft.player.getMainHandItem().getOrCreateTag().contains("uploadJobId", Tag.TAG_LONG)) {
ItemStack item = CommentToolItem.Client.getHoldingCommentTool();
if (item != null) {
if (item.getOrCreateTag().contains("uploadJobId", Tag.TAG_LONG)) {
TIP_PLACE_COMMENT.visible = true;
return; // De-clutter
} else {
Expand Down

0 comments on commit 5900839

Please sign in to comment.