CopperChestBlock .java

net.minecraft.world.level.block.CopperChestBlock (class, 128 lines): members: COPPER_TO_COPPER_CHEST_MAPPING, getFromCopperBlock, getHingeSound, isWaxed.

net.minecraft.world.level.blockclass128 linescommonextends ChestBlock5.4 KB

API Surface (8)

SoundEvent getHingeSound(final WeatheringCopper.WeatherState state, final boolean open)
boolean chestCanConnectTo(final BlockState blockState)
BlockState getStateForPlacement(final BlockPlaceContext context)
BlockState updateShape(final BlockState state, final LevelReader level, final ScheduledTickAccess ticks, final BlockPos pos, final Direction directionToNeighbour, final BlockPos neighbourPos, final BlockState neighbourState, final RandomSource random)
WeatheringCopper.WeatherState getState()
BlockState getFromCopperBlock(final Block copperBlock, final Direction facing, final Level level, final BlockPos pos)
boolean isWaxed()
boolean shouldChangedStateKeepBlockEntity(final BlockState oldState)

Fields & Constants (10)

COPPER_TO_COPPER_CHEST_MAPPING
weatherState
closeSound
state
level
ticks
pos
directionToNeighbour
neighbourPos
neighbourState

Full Source

CopperChestBlock.java128 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.block;
2
3
import com.google.common.base.Suppliers;
4
import com.google.common.collect.ImmutableMap;
5
import com.google.common.collect.ImmutableMap.Builder;
6
import java.util.Map;
7
import java.util.Optional;
8
import java.util.function.Supplier;
9
import net.minecraft.core.BlockPos;
10
import net.minecraft.core.Direction;
11
import net.minecraft.sounds.SoundEvent;
12
import net.minecraft.sounds.SoundEvents;
13
import net.minecraft.tags.BlockTags;
14
import net.minecraft.util.RandomSource;
15
import net.minecraft.world.item.HoneycombItem;
16
import net.minecraft.world.item.context.BlockPlaceContext;
17
import net.minecraft.world.level.Level;
18
import net.minecraft.world.level.LevelReader;
19
import net.minecraft.world.level.ScheduledTickAccess;
20
import net.minecraft.world.level.block.entity.BlockEntityTypes;
21
import net.minecraft.world.level.block.state.BlockBehaviour;
22
import net.minecraft.world.level.block.state.BlockState;
23
import net.minecraft.world.level.block.state.properties.ChestType;
24
25
public class CopperChestBlock extends ChestBlock {
26
	private static final Supplier<Map<Block, Block>> COPPER_TO_COPPER_CHEST_MAPPING = Suppliers.memoize(() -> {
27
		Builder<Block, Block> result = ImmutableMap.builder();
28
		WeatheringCopperCollection.zipApply(Blocks.COPPER_BLOCK, Blocks.COPPER_CHEST, result::put);
29
		return result.buildOrThrow();
30
	});
31
	private final WeatheringCopper.WeatherState weatherState;
32
33
	public CopperChestBlock(
34
		final WeatheringCopper.WeatherState weatherState, final SoundEvent openSound, final SoundEvent closeSound, final BlockBehaviour.Properties properties
35
	) {
36
		super(() -> BlockEntityTypes.CHEST, openSound, closeSound, properties);
37
		this.weatherState = weatherState;
38
	}
39
40
	public static SoundEvent getHingeSound(final WeatheringCopper.WeatherState state, final boolean open) {
41
		return switch (state) {
42
			case WEATHERED -> open ? SoundEvents.COPPER_CHEST_WEATHERED_OPEN : SoundEvents.COPPER_CHEST_WEATHERED_CLOSE;
43
			case OXIDIZED -> open ? SoundEvents.COPPER_CHEST_OXIDIZED_OPEN : SoundEvents.COPPER_CHEST_OXIDIZED_CLOSE;
44
			default -> open ? SoundEvents.COPPER_CHEST_OPEN : SoundEvents.COPPER_CHEST_CLOSE;
45
		};
46
	}
47
48
	@Override
49
	public boolean chestCanConnectTo(final BlockState blockState) {
50
		return blockState.is(BlockTags.COPPER_CHESTS) && blockState.hasProperty(ChestBlock.TYPE);
51
	}
52
53
	@Override
54
	public BlockState getStateForPlacement(final BlockPlaceContext context) {
55
		BlockState state = super.getStateForPlacement(context);
56
		return getLeastOxidizedChestOfConnectedBlocks(state, context.getLevel(), context.getClickedPos());
57
	}
58
59
	private static BlockState getLeastOxidizedChestOfConnectedBlocks(final BlockState state, final Level level, final BlockPos pos) {
60
		BlockState connectedState = level.getBlockState(pos.relative(getConnectedDirection(state)));
61
		if (!state.getValue(ChestBlock.TYPE).equals(ChestType.SINGLE)
62
			&& state.getBlock() instanceof CopperChestBlock copperChestBlock
63
			&& connectedState.getBlock() instanceof CopperChestBlock connectedCopperChestBlock) {
64
			BlockState updatedBlockState = state;
65
			BlockState connectedPredictedBlockState = connectedState;
66
			if (copperChestBlock.isWaxed() != connectedCopperChestBlock.isWaxed()) {
67
				updatedBlockState = unwaxBlock(copperChestBlock, state).orElse(updatedBlockState);
68
				connectedPredictedBlockState = unwaxBlock(connectedCopperChestBlock, connectedState).orElse(connectedPredictedBlockState);
69
			}
70
71
			Block leastOxidizedBlock = copperChestBlock.weatherState.ordinal() <= connectedCopperChestBlock.weatherState.ordinal()
72
				? updatedBlockState.getBlock()
73
				: connectedPredictedBlockState.getBlock();
74
			return leastOxidizedBlock.withPropertiesOf(updatedBlockState);
75
		} else {
76
			return state;
77
		}
78
	}
79
80
	@Override
81
	protected BlockState updateShape(
82
		final BlockState state,
83
		final LevelReader level,
84
		final ScheduledTickAccess ticks,
85
		final BlockPos pos,
86
		final Direction directionToNeighbour,
87
		final BlockPos neighbourPos,
88
		final BlockState neighbourState,
89
		final RandomSource random
90
	) {
91
		BlockState blockState = super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
92
		if (this.chestCanConnectTo(neighbourState)) {
93
			ChestType chestType = blockState.getValue(ChestBlock.TYPE);
94
			if (!chestType.equals(ChestType.SINGLE) && getConnectedDirection(blockState) == directionToNeighbour) {
95
				return neighbourState.getBlock().withPropertiesOf(blockState);
96
			}
97
		}
98
99
		return blockState;
100
	}
101
102
	private static Optional<BlockState> unwaxBlock(final CopperChestBlock copperChestBlock, final BlockState state) {
103
		return !copperChestBlock.isWaxed()
104
			? Optional.of(state)
105
			: Optional.ofNullable(HoneycombItem.WAX_OFF_BY_BLOCK.get().get(state.getBlock())).map(b -> b.withPropertiesOf(state));
106
	}
107
108
	public WeatheringCopper.WeatherState getState() {
109
		return this.weatherState;
110
	}
111
112
	public static BlockState getFromCopperBlock(final Block copperBlock, final Direction facing, final Level level, final BlockPos pos) {
113
		CopperChestBlock block = (CopperChestBlock)COPPER_TO_COPPER_CHEST_MAPPING.get().getOrDefault(copperBlock, Blocks.COPPER_CHEST.weathering().unaffected());
114
		ChestType chestType = block.getChestType(level, pos, facing);
115
		BlockState state = block.defaultBlockState().setValue(FACING, facing).setValue(TYPE, chestType);
116
		return getLeastOxidizedChestOfConnectedBlocks(state, level, pos);
117
	}
118
119
	public boolean isWaxed() {
120
		return true;
121
	}
122
123
	@Override
124
	public boolean shouldChangedStateKeepBlockEntity(final BlockState oldState) {
125
		return oldState.is(BlockTags.COPPER_CHESTS);
126
	}
127
}
128

Decompiled Minecraft 26.3 source of CopperChestBlock (net/minecraft/world/level/block/CopperChestBlock.java) with Mojang official mappings, stored verbatim on the site.