| 1 | package net.minecraft.world.level.block.entity;
|
| 2 |
|
| 3 | import net.minecraft.SharedConstants;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.core.HolderLookup;
|
| 6 | import net.minecraft.nbt.CompoundTag;
|
| 7 | import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
|
| 8 | import net.minecraft.util.RandomSource;
|
| 9 | import net.minecraft.util.Util;
|
| 10 | import net.minecraft.world.entity.EntityType;
|
| 11 | import net.minecraft.world.item.component.TypedEntityData;
|
| 12 | import net.minecraft.world.level.Level;
|
| 13 | import net.minecraft.world.level.Spawner;
|
| 14 | import net.minecraft.world.level.block.TrialSpawnerBlock;
|
| 15 | import net.minecraft.world.level.block.entity.trialspawner.PlayerDetector;
|
| 16 | import net.minecraft.world.level.block.entity.trialspawner.TrialSpawner;
|
| 17 | import net.minecraft.world.level.block.entity.trialspawner.TrialSpawnerState;
|
| 18 | import net.minecraft.world.level.block.state.BlockState;
|
| 19 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 20 | import net.minecraft.world.level.storage.ValueInput;
|
| 21 | import net.minecraft.world.level.storage.ValueOutput;
|
| 22 |
|
| 23 | public class TrialSpawnerBlockEntity extends BlockEntity implements TrialSpawner.StateAccessor, Spawner {
|
| 24 | private final TrialSpawner trialSpawner = this.createDefaultSpawner();
|
| 25 |
|
| 26 | public TrialSpawnerBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
|
| 27 | super(BlockEntityTypes.TRIAL_SPAWNER, worldPosition, blockState);
|
| 28 | }
|
| 29 |
|
| 30 | private TrialSpawner createDefaultSpawner() {
|
| 31 | PlayerDetector playerDetector = SharedConstants.DEBUG_TRIAL_SPAWNER_DETECTS_SHEEP_AS_PLAYERS ? PlayerDetector.SHEEP : PlayerDetector.NO_CREATIVE_PLAYERS;
|
| 32 | PlayerDetector.EntitySelector entitySelector = PlayerDetector.EntitySelector.SELECT_FROM_LEVEL;
|
| 33 | return new TrialSpawner(TrialSpawner.FullConfig.DEFAULT, this, playerDetector, entitySelector);
|
| 34 | }
|
| 35 |
|
| 36 | @Override
|
| 37 | protected void loadAdditional(final ValueInput input) {
|
| 38 | super.loadAdditional(input);
|
| 39 | this.trialSpawner.load(input);
|
| 40 | if (this.level != null) {
|
| 41 | this.markUpdated();
|
| 42 | }
|
| 43 | }
|
| 44 |
|
| 45 | @Override
|
| 46 | protected void saveAdditional(final ValueOutput output) {
|
| 47 | super.saveAdditional(output);
|
| 48 | this.trialSpawner.store(output);
|
| 49 | }
|
| 50 |
|
| 51 | public ClientboundBlockEntityDataPacket getUpdatePacket() {
|
| 52 | return ClientboundBlockEntityDataPacket.create(this);
|
| 53 | }
|
| 54 |
|
| 55 | @Override
|
| 56 | public CompoundTag getUpdateTag(final HolderLookup.Provider registries) {
|
| 57 | return this.trialSpawner.getStateData().getUpdateTag(this.getBlockState().getValue(TrialSpawnerBlock.STATE));
|
| 58 | }
|
| 59 |
|
| 60 | @Override
|
| 61 | public void setEntityId(final EntityType<?> type, final RandomSource random) {
|
| 62 | if (this.level == null) {
|
| 63 | Util.logAndPauseIfInIde("Expected non-null level");
|
| 64 | } else {
|
| 65 | this.trialSpawner.overrideEntityToSpawn(type, this.level);
|
| 66 | this.setChanged();
|
| 67 | }
|
| 68 | }
|
| 69 |
|
| 70 | @Override
|
| 71 | public void setEntityData(final TypedEntityData<EntityType<?>> entityData, final RandomSource random) {
|
| 72 | if (this.level == null) {
|
| 73 | Util.logAndPauseIfInIde("Expected non-null level");
|
| 74 | } else {
|
| 75 | this.trialSpawner.overrideEntityToSpawn(entityData, this.level);
|
| 76 | this.setChanged();
|
| 77 | }
|
| 78 | }
|
| 79 |
|
| 80 | public TrialSpawner getTrialSpawner() {
|
| 81 | return this.trialSpawner;
|
| 82 | }
|
| 83 |
|
| 84 | @Override
|
| 85 | public TrialSpawnerState getState() {
|
| 86 | return !this.getBlockState().hasProperty(BlockStateProperties.TRIAL_SPAWNER_STATE)
|
| 87 | ? TrialSpawnerState.INACTIVE
|
| 88 | : this.getBlockState().getValue(BlockStateProperties.TRIAL_SPAWNER_STATE);
|
| 89 | }
|
| 90 |
|
| 91 | @Override
|
| 92 | public void setState(final Level level, final TrialSpawnerState state) {
|
| 93 | this.setChanged();
|
| 94 | level.setBlockAndUpdate(this.worldPosition, this.getBlockState().setValue(BlockStateProperties.TRIAL_SPAWNER_STATE, state));
|
| 95 | }
|
| 96 |
|
| 97 | @Override
|
| 98 | public void markUpdated() {
|
| 99 | this.setChanged();
|
| 100 | if (this.level != null) {
|
| 101 | this.level.sendBlockUpdated(this.worldPosition, this.getBlockState(), this.getBlockState(), 3);
|
| 102 | }
|
| 103 | }
|
| 104 | }
|
| 105 |
|