Skip to content

Commit 02c67c8

Browse files
committed
Pick Block finds block providers that can place blocks as well
1 parent 5667cea commit 02c67c8

File tree

6 files changed

+73
-0
lines changed

6 files changed

+73
-0
lines changed

Xplat/src/generated/resources/.cache/5e449fe289648869bd1f4d4d99715c8b4e0c057d

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Xplat/src/generated/resources/data/botania/tags/item/pickable_block_providers.json

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Xplat/src/main/java/vazkii/botania/common/lib/BotaniaTags.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ public static class Items {
107107
public static final TagKey<Item> MANA_USING_ITEMS = tag("mana_using_items");
108108
public static final TagKey<Item> SEED_APOTHECARY_REAGENT = tag("seed_apothecary_reagent");
109109

110+
/**
111+
* Block provider items in this tag can be auto-selected via the vanilla "Pick Block" feature.
112+
* (Not every block provider makes sense here, e.g. the Hand of Ender cannot place blocks.)
113+
*/
114+
public static final TagKey<Item> PICKABLE_BLOCK_PROVIDER = tag("pickable_block_providers");
115+
110116
public static TagKey<Item> getPetalTag(DyeColor color) {
111117
return switch (color) {
112118
case WHITE -> PETALS_WHITE;

Xplat/src/main/java/vazkii/botania/data/ItemTagProvider.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,9 @@ protected void addTags(HolderLookup.Provider provider) {
240240
this.tag(BotaniaTags.Items.SEED_APOTHECARY_REAGENT)
241241
.add(Items.WHEAT_SEEDS, Items.BEETROOT_SEEDS, Items.MELON_SEEDS, Items.PUMPKIN_SEEDS)
242242
.addOptionalTag(ResourceLocation.fromNamespaceAndPath("forge", "seeds"));
243+
244+
this.tag(BotaniaTags.Items.PICKABLE_BLOCK_PROVIDER)
245+
.add(dirtRod, skyDirtRod, cobbleRod, blackHoleTalisman);
243246
}
244247

245248
private static Item[] getItems(Predicate<Item> predicate) {
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package vazkii.botania.mixin;
2+
3+
import net.minecraft.core.NonNullList;
4+
import net.minecraft.world.entity.player.Inventory;
5+
import net.minecraft.world.entity.player.Player;
6+
import net.minecraft.world.item.BlockItem;
7+
import net.minecraft.world.item.ItemStack;
8+
import net.minecraft.world.level.block.Block;
9+
10+
import org.spongepowered.asm.mixin.Final;
11+
import org.spongepowered.asm.mixin.Mixin;
12+
import org.spongepowered.asm.mixin.Shadow;
13+
import org.spongepowered.asm.mixin.injection.At;
14+
import org.spongepowered.asm.mixin.injection.Inject;
15+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
16+
17+
import vazkii.botania.common.lib.BotaniaTags;
18+
import vazkii.botania.xplat.XplatAbstractions;
19+
20+
@Mixin(Inventory.class)
21+
public class InventoryMixin {
22+
@Shadow
23+
@Final
24+
public Player player;
25+
26+
@Shadow
27+
@Final
28+
public NonNullList<ItemStack> items;
29+
30+
/**
31+
* Also look for block providers when picking a block, if the actual block item could not be found.
32+
*/
33+
@Inject(method = "findSlotMatchingItem", at = @At("RETURN"), cancellable = true)
34+
private void findBlockProviderItem(ItemStack searchStack, CallbackInfoReturnable<Integer> cir) {
35+
if (this.player.getAbilities().instabuild || cir.getReturnValue() != -1
36+
|| !(searchStack.getItem() instanceof BlockItem blockItem)) {
37+
// skip when in creative mode, when an exact match was already found, or when not looking for a block item
38+
return;
39+
}
40+
41+
Block block = blockItem.getBlock();
42+
for (int i = 0; i < this.items.size(); ++i) {
43+
ItemStack invStack = this.items.get(i);
44+
if (!invStack.is(BotaniaTags.Items.PICKABLE_BLOCK_PROVIDER)) {
45+
continue;
46+
}
47+
var provider = XplatAbstractions.INSTANCE.findBlockProvider(invStack);
48+
if (provider != null && provider.provideBlock(this.player, searchStack, block, false)) {
49+
cir.setReturnValue(i);
50+
return;
51+
}
52+
}
53+
}
54+
}

Xplat/src/main/resources/botania.xplat.mixins.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"HurtByTargetGoalAccessor",
3131
"HurtByTargetGoalMixin",
3232
"InventoryAccessor",
33+
"InventoryMixin",
3334
"ItemEntityAccessor",
3435
"ItemEntityMixin",
3536
"ItemMixin",

0 commit comments

Comments
 (0)