|
| 1 | +/* |
| 2 | + * Copyright (c) 2014-2026 Wurst-Imperium and contributors. |
| 3 | + * |
| 4 | + * This source code is subject to the terms of the GNU General Public |
| 5 | + * License, version 3. If a copy of the GPL was not distributed with this |
| 6 | + * file, You can obtain one at: https://www.gnu.org/licenses/gpl-3.0.txt |
| 7 | + */ |
| 8 | +package net.wurstclient.hacks; |
| 9 | + |
| 10 | +import java.util.Optional; |
| 11 | + |
| 12 | +import net.minecraft.core.Holder.Reference; |
| 13 | +import net.minecraft.core.Registry; |
| 14 | +import net.minecraft.core.RegistryAccess; |
| 15 | +import net.minecraft.core.registries.Registries; |
| 16 | +import net.minecraft.world.item.ItemStack; |
| 17 | +import net.minecraft.world.item.enchantment.Enchantment; |
| 18 | +import net.minecraft.world.item.enchantment.EnchantmentHelper; |
| 19 | +import net.minecraft.world.item.enchantment.Enchantments; |
| 20 | +import net.minecraft.world.level.block.Block; |
| 21 | +import net.minecraft.world.level.block.state.BlockState; |
| 22 | +import net.wurstclient.Category; |
| 23 | +import net.wurstclient.SearchTags; |
| 24 | +import net.wurstclient.hack.Hack; |
| 25 | +import net.wurstclient.settings.BlockListSetting; |
| 26 | +import net.wurstclient.settings.CheckboxSetting; |
| 27 | +import net.wurstclient.util.ChatUtils; |
| 28 | + |
| 29 | +@SearchTags({"silkonly", "silk only", "silk touch only"}) |
| 30 | +public final class SilkOnlyHack extends Hack |
| 31 | +{ |
| 32 | + private final BlockListSetting blockList = new BlockListSetting( |
| 33 | + "Block List", "Blocks that can only be broken with Silk Touch enabled.", |
| 34 | + "minecraft:ender_chest", "minecraft:bookshelf", |
| 35 | + "minecraft:amethyst_cluster", |
| 36 | + |
| 37 | + // Glass (any kind) |
| 38 | + "minecraft:glass", "minecraft:tinted_glass", "minecraft:glass_pane", |
| 39 | + "minecraft:white_stained_glass", "minecraft:orange_stained_glass", |
| 40 | + "minecraft:magenta_stained_glass", "minecraft:light_blue_stained_glass", |
| 41 | + "minecraft:yellow_stained_glass", "minecraft:lime_stained_glass", |
| 42 | + "minecraft:pink_stained_glass", "minecraft:gray_stained_glass", |
| 43 | + "minecraft:light_gray_stained_glass", "minecraft:cyan_stained_glass", |
| 44 | + "minecraft:purple_stained_glass", "minecraft:blue_stained_glass", |
| 45 | + "minecraft:brown_stained_glass", "minecraft:green_stained_glass", |
| 46 | + "minecraft:red_stained_glass", "minecraft:black_stained_glass", |
| 47 | + "minecraft:white_stained_glass_pane", |
| 48 | + "minecraft:orange_stained_glass_pane", |
| 49 | + "minecraft:magenta_stained_glass_pane", |
| 50 | + "minecraft:light_blue_stained_glass_pane", |
| 51 | + "minecraft:yellow_stained_glass_pane", |
| 52 | + "minecraft:lime_stained_glass_pane", |
| 53 | + "minecraft:pink_stained_glass_pane", |
| 54 | + "minecraft:gray_stained_glass_pane", |
| 55 | + "minecraft:light_gray_stained_glass_pane", |
| 56 | + "minecraft:cyan_stained_glass_pane", |
| 57 | + "minecraft:purple_stained_glass_pane", |
| 58 | + "minecraft:blue_stained_glass_pane", |
| 59 | + "minecraft:brown_stained_glass_pane", |
| 60 | + "minecraft:green_stained_glass_pane", |
| 61 | + "minecraft:red_stained_glass_pane", |
| 62 | + "minecraft:black_stained_glass_pane", |
| 63 | + |
| 64 | + // Light blocks people often want to preserve |
| 65 | + "minecraft:glowstone", "minecraft:sea_lantern", |
| 66 | + |
| 67 | + // Silverfish |
| 68 | + "minecraft:infested_stone"); |
| 69 | + |
| 70 | + private final CheckboxSetting chatMessage = new CheckboxSetting( |
| 71 | + "Chat message", |
| 72 | + "Print a chat message when refusing to break a listed block.", true); |
| 73 | + |
| 74 | + private net.minecraft.core.BlockPos lastRefusePos; |
| 75 | + private long lastRefuseMs; |
| 76 | + |
| 77 | + public SilkOnlyHack() |
| 78 | + { |
| 79 | + super("SilkOnly"); |
| 80 | + setCategory(Category.BLOCKS); |
| 81 | + addSetting(blockList); |
| 82 | + addSetting(chatMessage); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Returns true if the hack should refuse to break the given block state |
| 87 | + * with the given tool. |
| 88 | + */ |
| 89 | + public boolean shouldRefuseBreak(net.minecraft.core.BlockPos pos, |
| 90 | + BlockState state, ItemStack tool) |
| 91 | + { |
| 92 | + if(!isEnabled() || pos == null || state == null) |
| 93 | + return false; |
| 94 | + if(MC.player == null || MC.level == null) |
| 95 | + return false; |
| 96 | + if(MC.player.getAbilities().instabuild) |
| 97 | + return false; |
| 98 | + |
| 99 | + Block block = state.getBlock(); |
| 100 | + if(block == null || !blockList.contains(block)) |
| 101 | + return false; |
| 102 | + |
| 103 | + if(hasSilkTouch(tool)) |
| 104 | + return false; |
| 105 | + |
| 106 | + maybeMessage(pos, block); |
| 107 | + return true; |
| 108 | + } |
| 109 | + |
| 110 | + private void maybeMessage(net.minecraft.core.BlockPos pos, Block block) |
| 111 | + { |
| 112 | + if(!chatMessage.isChecked()) |
| 113 | + return; |
| 114 | + |
| 115 | + long now = System.currentTimeMillis(); |
| 116 | + if(lastRefusePos != null && lastRefusePos.equals(pos) |
| 117 | + && now - lastRefuseMs < 1000L) |
| 118 | + return; |
| 119 | + |
| 120 | + lastRefusePos = pos; |
| 121 | + lastRefuseMs = now; |
| 122 | + |
| 123 | + String name = getBlockDisplayName(block); |
| 124 | + ChatUtils.message("Refusing To Break " + name + ". Use Silk Touch!"); |
| 125 | + } |
| 126 | + |
| 127 | + private static String getBlockDisplayName(Block block) |
| 128 | + { |
| 129 | + if(block == null) |
| 130 | + return "block"; |
| 131 | + |
| 132 | + try |
| 133 | + { |
| 134 | + ItemStack asItem = new ItemStack(block.asItem()); |
| 135 | + if(!asItem.isEmpty()) |
| 136 | + { |
| 137 | + String s = asItem.getHoverName().getString(); |
| 138 | + if(s != null && !s.isBlank()) |
| 139 | + return s; |
| 140 | + } |
| 141 | + }catch(Throwable ignored) |
| 142 | + {} |
| 143 | + |
| 144 | + try |
| 145 | + { |
| 146 | + return net.minecraft.core.registries.BuiltInRegistries.BLOCK |
| 147 | + .getKey(block).toString(); |
| 148 | + }catch(Throwable ignored) |
| 149 | + { |
| 150 | + return "block"; |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + private static boolean hasSilkTouch(ItemStack stack) |
| 155 | + { |
| 156 | + if(stack == null || stack.isEmpty()) |
| 157 | + return false; |
| 158 | + |
| 159 | + if(MC.level == null) |
| 160 | + return false; |
| 161 | + |
| 162 | + try |
| 163 | + { |
| 164 | + RegistryAccess access = MC.level.registryAccess(); |
| 165 | + Registry<Enchantment> registry = |
| 166 | + access.lookupOrThrow(Registries.ENCHANTMENT); |
| 167 | + Optional<Reference<Enchantment>> silk = |
| 168 | + registry.get(Enchantments.SILK_TOUCH); |
| 169 | + |
| 170 | + int lvl = silk.map( |
| 171 | + ref -> EnchantmentHelper.getItemEnchantmentLevel(ref, stack)) |
| 172 | + .orElse(0); |
| 173 | + return lvl > 0; |
| 174 | + |
| 175 | + }catch(Throwable t) |
| 176 | + { |
| 177 | + return false; |
| 178 | + } |
| 179 | + } |
| 180 | +} |
0 commit comments