Skip to content

Commit

Permalink
copySignTextToClipBoard support targeting sign block in litematica …
Browse files Browse the repository at this point in the history
…schematic world
  • Loading branch information
Fallen-Breath committed Jul 14, 2024
1 parent 1fc855f commit d454521
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.common.base.Joiner;
import fi.dy.masa.malilib.util.InfoUtils;
import me.fallenbreath.tweakermore.util.EntityUtil;
import me.fallenbreath.tweakermore.util.compat.litematica.LitematicaUtils;
import net.minecraft.block.AbstractSignBlock;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
Expand All @@ -33,57 +34,93 @@
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.hit.HitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;

import java.util.Arrays;
import java.util.stream.Collectors;

//#if MC >= 11600
//$$ import me.fallenbreath.tweakermore.mixins.tweaks.features.copySignTextToClipBoard.SignBlockEntityAccessor;
//#endif

import java.util.Arrays;
import java.util.stream.Collectors;

public class SignTextCopier
{
public static void copySignText()
{
MinecraftClient mc = MinecraftClient.getInstance();
ClientPlayerEntity player = EntityUtil.getCurrentPlayerOrFreeCameraEntity();
if (player != null && mc.world != null && mc.crosshairTarget != null && mc.crosshairTarget.getType() == HitResult.Type.BLOCK)
if (player != null && mc.world != null)
{
BlockPos blockPos = ((BlockHitResult)mc.crosshairTarget).getBlockPos();
BlockState blockState = mc.world.getBlockState(blockPos);
BlockPos blockPos = null;
SignBlockEntity blockEntity = null;
String copiedTextKey = null;
if (LitematicaUtils.isRenderingEnabled())
{
blockPos = LitematicaUtils.getSchematicWorldCrosshairTargetPos(player);
blockEntity = tryGetSignBlockEntity(LitematicaUtils.getSchematicWorld(), blockPos);
copiedTextKey = "sign_copied_schematic";
}
if (blockEntity == null && mc.crosshairTarget != null && mc.crosshairTarget.getType() == HitResult.Type.BLOCK)
{
blockPos = ((BlockHitResult)mc.crosshairTarget).getBlockPos();
blockEntity = tryGetSignBlockEntity(mc.world, blockPos);
copiedTextKey = "sign_copied";
}

if (blockEntity != null)
{
BlockState blockState = blockEntity.getCachedState();
Text[] texts = getSignTexts(blockEntity, player);

String text = Joiner.on("\n").join(
Arrays.stream(texts).
map(Text::getString).
collect(Collectors.toList())
);
if (!text.isEmpty())
{
mc.keyboard.setClipboard(text);
InfoUtils.printActionbarMessage("tweakermore.impl.copySignTextToClipBoard." + copiedTextKey, blockState.getBlock().getName());
}
else
{
InfoUtils.printActionbarMessage("tweakermore.impl.copySignTextToClipBoard.empty_sign", blockState.getBlock().getName());
}
return;
}
}
InfoUtils.printActionbarMessage("tweakermore.impl.copySignTextToClipBoard.no_sign");
}

private static Text[] getSignTexts(SignBlockEntity blockEntity, ClientPlayerEntity player)
{
//#if MC >= 12004
//$$ return blockEntity.getText((blockEntity).isPlayerFacingFront(player)).getMessages(false);
//#elseif MC >= 12000
//$$ return blockEntity.getTextFacing(player).getMessages(false);
//#elseif MC >= 11600
//$$ return ((SignBlockEntityAccessor)blockEntity).getTexts();
//#else
return blockEntity.text;
//#endif
}

@Nullable
private static SignBlockEntity tryGetSignBlockEntity(@Nullable World world, @Nullable BlockPos pos)
{
if (world != null && pos != null)
{
BlockState blockState = world.getBlockState(pos);
if (blockState.getBlock() instanceof AbstractSignBlock)
{
BlockEntity blockEntity = mc.world.getBlockEntity(blockPos);
BlockEntity blockEntity = world.getBlockEntity(pos);
if (blockEntity instanceof SignBlockEntity)
{
//#if MC >= 12004
//$$ Text[] texts = ((SignBlockEntity)blockEntity).getText(((SignBlockEntity)blockEntity).isPlayerFacingFront(player)).getMessages(false);
//#elseif MC >= 12000
//$$ Text[] texts = ((SignBlockEntity)blockEntity).getTextFacing(player).getMessages(false);
//#elseif MC >= 11600
//$$ Text[] texts = ((SignBlockEntityAccessor)blockEntity).getTexts();
//#else
Text[] texts = ((SignBlockEntity)blockEntity).text;
//#endif

String text = Joiner.on("\n").join(
Arrays.stream(texts).
map(Text::getString).
collect(Collectors.toList())
);
if (!text.isEmpty())
{
mc.keyboard.setClipboard(text);
InfoUtils.printActionbarMessage("tweakermore.impl.copySignTextToClipBoard.sign_copied", blockState.getBlock().getName());
}
else
{
InfoUtils.printActionbarMessage("tweakermore.impl.copySignTextToClipBoard.empty_sign", blockState.getBlock().getName());
}
return;
return (SignBlockEntity)blockEntity;
}
}
}
InfoUtils.printActionbarMessage("tweakermore.impl.copySignTextToClipBoard.no_sign");
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* This file is part of the TweakerMore project, licensed under the
* GNU Lesser General Public License v3.0
*
* Copyright (C) 2024 Fallen_Breath and contributors
*
* TweakerMore is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* TweakerMore is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with TweakerMore. If not, see <https://www.gnu.org/licenses/>.
*/

package me.fallenbreath.tweakermore.util.compat.litematica;

import fi.dy.masa.litematica.config.Configs;
import fi.dy.masa.litematica.util.RayTraceUtils;
import fi.dy.masa.litematica.world.SchematicWorldHandler;
import me.fallenbreath.tweakermore.util.FabricUtil;
import me.fallenbreath.tweakermore.util.ModIds;
import net.minecraft.client.MinecraftClient;
import net.minecraft.entity.Entity;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;

import java.util.Optional;

public class LitematicaUtils
{
private static final boolean LITEMATICA_LOADED = FabricUtil.isModLoaded(ModIds.litematica);

@Nullable
public static World getSchematicWorld()
{
if (LITEMATICA_LOADED)
{
return SchematicWorldHandler.getSchematicWorld();
}
return null;
}

public static boolean isRenderingEnabled()
{
if (LITEMATICA_LOADED)
{
return Configs.Visuals.ENABLE_RENDERING.getBooleanValue();
}
return false;
}

@Nullable
public static BlockPos getSchematicWorldCrosshairTargetPos(Entity cameraEntity)
{
if (LITEMATICA_LOADED)
{
// reference: fi.dy.masa.litematica.render.OverlayRenderer.renderHoverInfo
MinecraftClient mc = MinecraftClient.getInstance();
if (mc.world != null)
{
return Optional.ofNullable(RayTraceUtils.getGenericTrace(
mc.world, cameraEntity, 10, true
//#if MC >= 11600
//$$ , Configs.InfoOverlays.INFO_OVERLAYS_TARGET_FLUIDS.getBooleanValue()
//#endif
//#if MC >= 11700
//$$ , false // includeVerifier
//#endif
))
.map(RayTraceUtils.RayTraceWrapper::getBlockHitResult)
.map(BlockHitResult::getBlockPos)
.orElse(null);
}
}
return null;
}
}
1 change: 1 addition & 0 deletions src/main/resources/assets/tweakermore/lang/en_us.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1110,6 +1110,7 @@ tweakermore:
item_copied: Copied data of %1$s to clipboard
copySignTextToClipBoard:
sign_copied: Copied texts from %1$s
sign_copied_schematic: Copied texts from %1$s (schematic)
no_sign: You are not pointing to a sign block
empty_sign: '%1$s does not contain any text'
creativePickBlockWithState:
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/assets/tweakermore/lang/zh_cn.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1110,6 +1110,7 @@ tweakermore:
item_copied: 已将物品%1$s的数据复制到剪贴板
copySignTextToClipBoard:
sign_copied: 已从%1$s中复制了文本
sign_copied_schematic: 已从%1$s中复制了文本 (原理图)
no_sign: 玩家未指向告示牌方块
empty_sign: '%1$s不包含任何文本'
creativePickBlockWithState:
Expand Down

0 comments on commit d454521

Please sign in to comment.