|
| 1 | +package net.earthcomputer.clientcommands.command; |
| 2 | + |
| 3 | +import com.mojang.brigadier.Command; |
| 4 | +import com.mojang.brigadier.CommandDispatcher; |
| 5 | +import com.mojang.brigadier.context.CommandContext; |
| 6 | +import com.mojang.brigadier.exceptions.CommandSyntaxException; |
| 7 | +import com.seedfinding.mcfeature.loot.LootContext; |
| 8 | +import com.seedfinding.mcfeature.loot.LootTable; |
| 9 | +import com.seedfinding.mcfeature.loot.MCLootTables; |
| 10 | +import com.seedfinding.mcfeature.loot.item.ItemStack; |
| 11 | +import net.earthcomputer.clientcommands.task.RenderDistanceScanTask; |
| 12 | +import net.earthcomputer.clientcommands.task.TaskManager; |
| 13 | +import net.earthcomputer.clientcommands.util.SeedfindingUtil; |
| 14 | +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; |
| 15 | +import net.minecraft.SharedConstants; |
| 16 | +import net.minecraft.client.Minecraft; |
| 17 | +import net.minecraft.client.multiplayer.ClientLevel; |
| 18 | +import net.minecraft.client.player.LocalPlayer; |
| 19 | +import net.minecraft.core.BlockPos; |
| 20 | +import net.minecraft.core.Holder; |
| 21 | +import net.minecraft.core.SectionPos; |
| 22 | +import net.minecraft.core.component.DataComponents; |
| 23 | +import net.minecraft.network.chat.Component; |
| 24 | +import net.minecraft.network.chat.MutableComponent; |
| 25 | +import net.minecraft.tags.BiomeTags; |
| 26 | +import net.minecraft.util.Mth; |
| 27 | +import net.minecraft.util.RandomSource; |
| 28 | +import net.minecraft.world.effect.MobEffectInstance; |
| 29 | +import net.minecraft.world.entity.Entity; |
| 30 | +import net.minecraft.world.item.alchemy.PotionContents; |
| 31 | +import net.minecraft.world.item.component.ItemLore; |
| 32 | +import net.minecraft.world.item.component.SuspiciousStewEffects; |
| 33 | +import net.minecraft.world.level.biome.Biome; |
| 34 | +import net.minecraft.world.level.biome.Biomes; |
| 35 | +import net.minecraft.world.level.block.Blocks; |
| 36 | +import net.minecraft.world.level.block.BrushableBlock; |
| 37 | +import net.minecraft.world.level.block.state.BlockState; |
| 38 | + |
| 39 | +import java.util.ArrayList; |
| 40 | +import java.util.List; |
| 41 | +import java.util.function.Supplier; |
| 42 | +import java.util.stream.Stream; |
| 43 | + |
| 44 | +import static net.earthcomputer.clientcommands.command.ClientCommandHelper.*; |
| 45 | +import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.*; |
| 46 | + |
| 47 | +public class PredictBrushablesCommand { |
| 48 | + public static final Flag<Boolean> FLAG_KEEP_SEARCHING = Flag.ofFlag("keep-searching").build(); |
| 49 | + |
| 50 | + public static void register(CommandDispatcher<FabricClientCommandSource> dispatcher) { |
| 51 | + var cpredictbrushables = dispatcher.register(literal("cpredictbrushables") |
| 52 | + .executes(PredictBrushablesCommand::predictBrushables)); |
| 53 | + FLAG_KEEP_SEARCHING.addToCommand(dispatcher, cpredictbrushables, ctx -> true); |
| 54 | + } |
| 55 | + |
| 56 | + private static int predictBrushables(CommandContext<FabricClientCommandSource> ctx) throws CommandSyntaxException { |
| 57 | + boolean keepSearching = getFlag(ctx, FLAG_KEEP_SEARCHING); |
| 58 | + String taskName = TaskManager.addTask("cpredictbrushables", new PredictBrushablesTask(keepSearching)); |
| 59 | + if (keepSearching) { |
| 60 | + sendFeedback(Component.translatable("commands.cpredictbrushables.starting.keepSearching", getCommandTextComponent("commands.client.cancel", "/ctask stop " + taskName))); |
| 61 | + } else { |
| 62 | + sendFeedback(Component.translatable("commands.cpredictbrushables.starting")); |
| 63 | + } |
| 64 | + |
| 65 | + return Command.SINGLE_SUCCESS; |
| 66 | + } |
| 67 | + |
| 68 | + private static final class PredictBrushablesTask extends RenderDistanceScanTask { |
| 69 | + private boolean found = false; |
| 70 | + |
| 71 | + PredictBrushablesTask(boolean keepSearching) { |
| 72 | + super(keepSearching); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + protected void scanBlock(Entity cameraEntity, BlockPos pos) { |
| 77 | + Minecraft minecraft = Minecraft.getInstance(); |
| 78 | + LocalPlayer player = minecraft.player; |
| 79 | + ClientLevel level = minecraft.level; |
| 80 | + assert level != null; |
| 81 | + BlockState blockState = level.getBlockState(pos); |
| 82 | + |
| 83 | + // best effort, may be inaccurate |
| 84 | + Supplier<LootTable> lootTableSupplier; |
| 85 | + long lootSeed; |
| 86 | + |
| 87 | + if (blockState.is(Blocks.SUSPICIOUS_SAND)) { |
| 88 | + Holder<Biome> biome = level.getBiome(pos); |
| 89 | + if (biome.is(Biomes.DESERT)) { |
| 90 | + // either desert well or desert pyramid, check for water |
| 91 | + boolean hasWater = Stream.of(pos.atY(pos.getY() + 1), pos.atY(pos.getY() + 2)) |
| 92 | + .map(level::getBlockState) |
| 93 | + .anyMatch(s -> s.is(Blocks.WATER)); |
| 94 | + lootTableSupplier = hasWater ? MCLootTables.DESERT_WELL_ARCHAEOLOGY : MCLootTables.DESERT_PYRAMID_ARCHAEOLOGY; |
| 95 | + lootSeed = pos.asLong(); |
| 96 | + } else if (biome.is(BiomeTags.HAS_OCEAN_RUIN_WARM)) { |
| 97 | + lootTableSupplier = MCLootTables.OCEAN_RUIN_WARM_ARCHAEOLOGY; |
| 98 | + RandomSource randomSource = RandomSource.create(Mth.getSeed(pos)); |
| 99 | + lootSeed = randomSource.nextLong(); |
| 100 | + } else { |
| 101 | + return; |
| 102 | + } |
| 103 | + } else if (blockState.is(Blocks.SUSPICIOUS_GRAVEL)) { |
| 104 | + Holder<Biome> biome = level.getBiome(pos); |
| 105 | + if (biome.is(BiomeTags.HAS_OCEAN_RUIN_COLD)) { |
| 106 | + lootTableSupplier = MCLootTables.OCEAN_RUIN_COLD_ARCHAEOLOGY; |
| 107 | + RandomSource randomSource = RandomSource.create(Mth.getSeed(pos)); |
| 108 | + lootSeed = randomSource.nextLong(); |
| 109 | + } else if (biome.is(BiomeTags.HAS_TRAIL_RUINS)) { |
| 110 | + // TODO: check for rare or common loot |
| 111 | + // Common loot is generated in TRAIL_RUINS_HOUSES_ARCHAEOLOGY, |
| 112 | + // TRAIL_RUINS_ROADS_ARCHAEOLOGY and TRAIL_RUINS_TOWER_TOP_ARCHAEOLOGY |
| 113 | + // Rare is generated in TRAIL_RUINS_HOUSES_ARCHAEOLOGY |
| 114 | + // Ignored for now |
| 115 | + return; |
| 116 | + } else { |
| 117 | + return; |
| 118 | + } |
| 119 | + } else { |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + LootContext context = new LootContext(lootSeed, SeedfindingUtil.getMCVersion()) |
| 124 | + .withLuck((int) player.getLuck()); |
| 125 | + |
| 126 | + List<ItemStack> items = lootTableSupplier.get().generate(context); |
| 127 | + for (ItemStack item : items) { |
| 128 | + var mcItemStack = SeedfindingUtil.fromSeedfindingItem(item, level.registryAccess()); |
| 129 | + mcItemStack.set(DataComponents.LORE, getItemLore(mcItemStack)); |
| 130 | + ClientCommandHelper.sendFeedback(Component.translatable("commands.cpredictbrushables.foundBrushableBlock", blockState.getBlock().getName(), ClientCommandHelper.getLookCoordsTextComponent(pos), mcItemStack.getDisplayName(), ClientCommandHelper.getGlowButtonTextComponent(pos))); |
| 131 | + } |
| 132 | + |
| 133 | + found = true; |
| 134 | + } |
| 135 | + |
| 136 | + // use lore to display effects |
| 137 | + private static ItemLore getItemLore(net.minecraft.world.item.ItemStack itemStack) { |
| 138 | + SuspiciousStewEffects effects = itemStack.get(DataComponents.SUSPICIOUS_STEW_EFFECTS); |
| 139 | + if (effects == null) { |
| 140 | + return ItemLore.EMPTY; |
| 141 | + } |
| 142 | + List<Component> components = new ArrayList<>(); |
| 143 | + for (SuspiciousStewEffects.Entry entry : effects.effects()) { |
| 144 | + MobEffectInstance mobEffectInstance = entry.createEffectInstance(); |
| 145 | + MutableComponent description = PotionContents.getPotionDescription(mobEffectInstance.getEffect(), mobEffectInstance.getAmplifier()); |
| 146 | + Component line = Component.translatable("commands.cpredictbrushables.stewEffect", description, entry.duration() / SharedConstants.TICKS_PER_SECOND); |
| 147 | + components.add(line); |
| 148 | + } |
| 149 | + return new ItemLore(components); |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + protected void onBlockStateUpdate(ClientLevel level, BlockPos pos, BlockState oldState, BlockState newState) { |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + protected boolean canScanChunkSection(Entity cameraEntity, SectionPos pos) { |
| 158 | + return hasBlockState(pos, s -> s.getBlock() instanceof BrushableBlock) && super.canScanChunkSection(cameraEntity, pos); |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public void onCompleted() { |
| 163 | + super.onCompleted(); |
| 164 | + if (!found) { |
| 165 | + sendError(Component.translatable("commands.cpredictbrushables.notFound")); |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | +} |
0 commit comments