| 1 | package net.minecraft.world.item;
|
| 2 |
|
| 3 | import com.google.common.base.Suppliers;
|
| 4 | import com.google.common.collect.BiMap;
|
| 5 | import com.google.common.collect.ImmutableBiMap;
|
| 6 | import com.google.common.collect.ImmutableMap;
|
| 7 | import com.google.common.collect.ImmutableBiMap.Builder;
|
| 8 | import com.mojang.datafixers.util.Pair;
|
| 9 | import java.util.List;
|
| 10 | import java.util.Optional;
|
| 11 | import java.util.function.Function;
|
| 12 | import java.util.function.Supplier;
|
| 13 | import java.util.stream.Stream;
|
| 14 | import net.minecraft.advancements.triggers.CriteriaTriggers;
|
| 15 | import net.minecraft.core.BlockPos;
|
| 16 | import net.minecraft.data.recipes.RecipeCategory;
|
| 17 | import net.minecraft.server.level.ServerPlayer;
|
| 18 | import net.minecraft.sounds.SoundEvents;
|
| 19 | import net.minecraft.sounds.SoundSource;
|
| 20 | import net.minecraft.world.InteractionResult;
|
| 21 | import net.minecraft.world.entity.player.Player;
|
| 22 | import net.minecraft.world.item.context.UseOnContext;
|
| 23 | import net.minecraft.world.level.Level;
|
| 24 | import net.minecraft.world.level.block.Block;
|
| 25 | import net.minecraft.world.level.block.Blocks;
|
| 26 | import net.minecraft.world.level.block.ChestBlock;
|
| 27 | import net.minecraft.world.level.block.WeatheringCopperCollection;
|
| 28 | import net.minecraft.world.level.block.entity.SignBlockEntity;
|
| 29 | import net.minecraft.world.level.block.entity.SignText;
|
| 30 | import net.minecraft.world.level.block.entity.SignTextSlot;
|
| 31 | import net.minecraft.world.level.block.state.BlockState;
|
| 32 | import net.minecraft.world.level.block.state.properties.ChestType;
|
| 33 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 34 |
|
| 35 | public class HoneycombItem extends Item implements SignApplicator {
|
| 36 | public static final Supplier<BiMap<Block, Block>> WAXABLES = Suppliers.memoize(
|
| 37 | () -> {
|
| 38 | Builder<Block, Block> builderx = ImmutableBiMap.builder();
|
| 39 | Stream.of(
|
| 40 | Blocks.COPPER_BLOCK,
|
| 41 | Blocks.CUT_COPPER,
|
| 42 | Blocks.CUT_COPPER_SLAB,
|
| 43 | Blocks.CUT_COPPER_STAIRS,
|
| 44 | Blocks.CHISELED_COPPER,
|
| 45 | Blocks.COPPER_DOOR,
|
| 46 | Blocks.COPPER_TRAPDOOR,
|
| 47 | Blocks.COPPER_BARS,
|
| 48 | Blocks.COPPER_GRATE,
|
| 49 | Blocks.COPPER_BULB,
|
| 50 | Blocks.COPPER_CHEST,
|
| 51 | Blocks.COPPER_GOLEM_STATUE,
|
| 52 | Blocks.LIGHTNING_ROD,
|
| 53 | Blocks.COPPER_LANTERN,
|
| 54 | Blocks.COPPER_CHAIN
|
| 55 | )
|
| 56 | .forEach(collection -> collection.zipUnwaxedWaxed(builderx::put));
|
| 57 | return builderx.build();
|
| 58 | }
|
| 59 | );
|
| 60 | public static final Supplier<BiMap<Block, Block>> WAX_OFF_BY_BLOCK = Suppliers.memoize(() -> WAXABLES.get().inverse());
|
| 61 | public static final ImmutableMap<Block, Pair<RecipeCategory, String>> WAXED_RECIPES;
|
| 62 |
|
| 63 | public HoneycombItem(final Item.Properties properties) {
|
| 64 | super(properties);
|
| 65 | }
|
| 66 |
|
| 67 | @Override
|
| 68 | public InteractionResult useOn(final UseOnContext context) {
|
| 69 | Level level = context.getLevel();
|
| 70 | BlockPos pos = context.getClickedPos();
|
| 71 | BlockState oldState = level.getBlockState(pos);
|
| 72 | return getWaxed(oldState).map(waxedState -> {
|
| 73 | Player player = context.getPlayer();
|
| 74 | ItemStack itemInHand = context.getItemInHand();
|
| 75 | if (player instanceof ServerPlayer serverPlayer) {
|
| 76 | CriteriaTriggers.ITEM_USED_ON_BLOCK.trigger(serverPlayer, pos, itemInHand);
|
| 77 | }
|
| 78 |
|
| 79 | itemInHand.shrink(1);
|
| 80 | level.setBlock(pos, waxedState, 11);
|
| 81 | level.gameEvent(GameEvent.BLOCK_CHANGE, pos, GameEvent.Context.of(player, waxedState));
|
| 82 | level.levelEvent(player, 3003, pos, 0);
|
| 83 | level.playSound(player, pos, SoundEvents.HONEYCOMB_WAX_ON, SoundSource.BLOCKS, 1.0F, 1.0F);
|
| 84 | if (oldState.getBlock() instanceof ChestBlock && oldState.getValue(ChestBlock.TYPE) != ChestType.SINGLE) {
|
| 85 | BlockPos neighborPos = ChestBlock.getConnectedBlockPos(pos, oldState);
|
| 86 | level.gameEvent(GameEvent.BLOCK_CHANGE, neighborPos, GameEvent.Context.of(player, level.getBlockState(neighborPos)));
|
| 87 | level.levelEvent(player, 3003, neighborPos, 0);
|
| 88 | level.playSound(player, pos, SoundEvents.HONEYCOMB_WAX_ON, SoundSource.BLOCKS, 1.0F, 1.0F);
|
| 89 | }
|
| 90 |
|
| 91 | return InteractionResult.SUCCESS;
|
| 92 | }).orElse(InteractionResult.PASS);
|
| 93 | }
|
| 94 |
|
| 95 | public static Optional<BlockState> getWaxed(final BlockState oldState) {
|
| 96 | return Optional.ofNullable(WAXABLES.get().get(oldState.getBlock())).map(b -> b.withPropertiesOf(oldState));
|
| 97 | }
|
| 98 |
|
| 99 | @Override
|
| 100 | public boolean tryApplyToSign(final Level level, final SignBlockEntity sign, final SignTextSlot slot, final ItemStack item, final Player player) {
|
| 101 | if (sign.setWaxed(true)) {
|
| 102 | BlockPos blockPos = sign.getBlockPos();
|
| 103 | level.levelEvent(null, 3003, blockPos, 0);
|
| 104 | level.playSound(null, blockPos, SoundEvents.HONEYCOMB_WAX_ON, SoundSource.BLOCKS, 1.0F, 1.0F);
|
| 105 | return true;
|
| 106 | } else {
|
| 107 | return false;
|
| 108 | }
|
| 109 | }
|
| 110 |
|
| 111 | @Override
|
| 112 | public boolean canApplyToSign(final SignText text, final ItemStack item, final Player player) {
|
| 113 | return true;
|
| 114 | }
|
| 115 |
|
| 116 | static {
|
| 117 | com.google.common.collect.ImmutableMap.Builder<Block, Pair<RecipeCategory, String>> builder = ImmutableMap.builder();
|
| 118 |
|
| 119 | for (HoneycombItem.WaxedRecipeGroup data : List.of(
|
| 120 | new HoneycombItem.WaxedRecipeGroup(Blocks.COPPER_BULB, block -> Pair.of(RecipeCategory.REDSTONE, block.builtInRegistryHolder().key().identifier().getPath())),
|
| 121 | new HoneycombItem.WaxedRecipeGroup(Blocks.COPPER_DOOR, var0x -> Pair.of(RecipeCategory.REDSTONE, "waxed_copper_door")),
|
| 122 | new HoneycombItem.WaxedRecipeGroup(Blocks.COPPER_TRAPDOOR, var0x -> Pair.of(RecipeCategory.REDSTONE, "waxed_copper_trapdoor")),
|
| 123 | new HoneycombItem.WaxedRecipeGroup(Blocks.COPPER_GOLEM_STATUE, var0x -> Pair.of(RecipeCategory.BUILDING_BLOCKS, "waxed_copper_golem_statue")),
|
| 124 | new HoneycombItem.WaxedRecipeGroup(Blocks.COPPER_CHEST, var0x -> Pair.of(RecipeCategory.BUILDING_BLOCKS, "waxed_copper_chest")),
|
| 125 | new HoneycombItem.WaxedRecipeGroup(Blocks.LIGHTNING_ROD, var0x -> Pair.of(RecipeCategory.BUILDING_BLOCKS, "waxed_lightning_rod")),
|
| 126 | new HoneycombItem.WaxedRecipeGroup(Blocks.COPPER_BARS, var0x -> Pair.of(RecipeCategory.BUILDING_BLOCKS, "waxed_copper_bar")),
|
| 127 | new HoneycombItem.WaxedRecipeGroup(Blocks.COPPER_CHAIN, var0x -> Pair.of(RecipeCategory.BUILDING_BLOCKS, "waxed_copper_chain")),
|
| 128 | new HoneycombItem.WaxedRecipeGroup(Blocks.COPPER_LANTERN, var0x -> Pair.of(RecipeCategory.BUILDING_BLOCKS, "waxed_copper_lantern")),
|
| 129 | new HoneycombItem.WaxedRecipeGroup(Blocks.COPPER_BLOCK, var0x -> Pair.of(RecipeCategory.BUILDING_BLOCKS, "waxed_copper_block"))
|
| 130 | )) {
|
| 131 | data.block.waxed().forEach(block -> builder.put(block, data.recipeIdProvider.apply(block)));
|
| 132 | }
|
| 133 |
|
| 134 | WAXED_RECIPES = builder.build();
|
| 135 | }
|
| 136 |
|
| 137 | private record WaxedRecipeGroup(WeatheringCopperCollection<Block> block, Function<Block, Pair<RecipeCategory, String>> recipeIdProvider) {
|
| 138 | }
|
| 139 | }
|
| 140 |
|