| 1 | package net.minecraft.world.level.block.entity;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import java.util.Arrays;
|
| 5 | import java.util.Optional;
|
| 6 | import net.minecraft.core.BlockPos;
|
| 7 | import net.minecraft.core.Direction;
|
| 8 | import net.minecraft.core.HolderLookup;
|
| 9 | import net.minecraft.core.NonNullList;
|
| 10 | import net.minecraft.core.component.DataComponentGetter;
|
| 11 | import net.minecraft.core.component.DataComponentMap;
|
| 12 | import net.minecraft.core.component.DataComponents;
|
| 13 | import net.minecraft.core.particles.ParticleTypes;
|
| 14 | import net.minecraft.nbt.CompoundTag;
|
| 15 | import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
|
| 16 | import net.minecraft.server.level.ServerLevel;
|
| 17 | import net.minecraft.util.Mth;
|
| 18 | import net.minecraft.util.ProblemReporter;
|
| 19 | import net.minecraft.util.RandomSource;
|
| 20 | import net.minecraft.world.Clearable;
|
| 21 | import net.minecraft.world.ContainerHelper;
|
| 22 | import net.minecraft.world.Containers;
|
| 23 | import net.minecraft.world.entity.LivingEntity;
|
| 24 | import net.minecraft.world.item.ItemStack;
|
| 25 | import net.minecraft.world.item.component.ItemContainerContents;
|
| 26 | import net.minecraft.world.item.crafting.CampfireCookingRecipe;
|
| 27 | import net.minecraft.world.item.crafting.RecipeHolder;
|
| 28 | import net.minecraft.world.item.crafting.RecipeManager;
|
| 29 | import net.minecraft.world.item.crafting.RecipeType;
|
| 30 | import net.minecraft.world.item.crafting.SingleRecipeInput;
|
| 31 | import net.minecraft.world.level.Level;
|
| 32 | import net.minecraft.world.level.block.CampfireBlock;
|
| 33 | import net.minecraft.world.level.block.state.BlockState;
|
| 34 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 35 | import net.minecraft.world.level.storage.TagValueOutput;
|
| 36 | import net.minecraft.world.level.storage.ValueInput;
|
| 37 | import net.minecraft.world.level.storage.ValueOutput;
|
| 38 | import org.jspecify.annotations.Nullable;
|
| 39 | import org.slf4j.Logger;
|
| 40 |
|
| 41 | public class CampfireBlockEntity extends BlockEntity implements Clearable {
|
| 42 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 43 | private static final int BURN_COOL_SPEED = 2;
|
| 44 | private static final int NUM_SLOTS = 4;
|
| 45 | private final NonNullList<ItemStack> items = NonNullList.withSize(4, ItemStack.EMPTY);
|
| 46 | private final int[] cookingProgress = new int[4];
|
| 47 | private final int[] cookingTime = new int[4];
|
| 48 |
|
| 49 | public CampfireBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
|
| 50 | super(BlockEntityTypes.CAMPFIRE, worldPosition, blockState);
|
| 51 | }
|
| 52 |
|
| 53 | public static void cookTick(
|
| 54 | final ServerLevel level,
|
| 55 | final BlockPos pos,
|
| 56 | final BlockState state,
|
| 57 | final CampfireBlockEntity entity,
|
| 58 | final RecipeManager.CachedCheck<SingleRecipeInput, CampfireCookingRecipe> recipeCache
|
| 59 | ) {
|
| 60 | boolean changed = false;
|
| 61 |
|
| 62 | for (int slot = 0; slot < entity.items.size(); slot++) {
|
| 63 | ItemStack itemStack = entity.items.get(slot);
|
| 64 | if (!itemStack.isEmpty()) {
|
| 65 | changed = true;
|
| 66 | entity.cookingProgress[slot]++;
|
| 67 | if (entity.cookingProgress[slot] >= entity.cookingTime[slot]) {
|
| 68 | SingleRecipeInput input = new SingleRecipeInput(itemStack);
|
| 69 | ItemStack result = recipeCache.getRecipeFor(input, level).map(r -> r.value().assemble(input)).orElse(itemStack);
|
| 70 | if (result.isItemEnabled(level.enabledFeatures())) {
|
| 71 | Containers.dropItemStack(level, pos.getX(), pos.getY(), pos.getZ(), result);
|
| 72 | entity.items.set(slot, ItemStack.EMPTY);
|
| 73 | level.sendBlockUpdated(pos, state, state, 3);
|
| 74 | level.gameEvent(GameEvent.BLOCK_CHANGE, pos, GameEvent.Context.of(state));
|
| 75 | }
|
| 76 | }
|
| 77 | }
|
| 78 | }
|
| 79 |
|
| 80 | if (changed) {
|
| 81 | setChanged(level, pos, state);
|
| 82 | }
|
| 83 | }
|
| 84 |
|
| 85 | public static void cooldownTick(final Level level, final BlockPos pos, final BlockState state, final CampfireBlockEntity entity) {
|
| 86 | boolean changed = false;
|
| 87 |
|
| 88 | for (int slot = 0; slot < entity.items.size(); slot++) {
|
| 89 | if (entity.cookingProgress[slot] > 0) {
|
| 90 | changed = true;
|
| 91 | entity.cookingProgress[slot] = Mth.clamp(entity.cookingProgress[slot] - 2, 0, entity.cookingTime[slot]);
|
| 92 | }
|
| 93 | }
|
| 94 |
|
| 95 | if (changed) {
|
| 96 | setChanged(level, pos, state);
|
| 97 | }
|
| 98 | }
|
| 99 |
|
| 100 | public static void particleTick(final Level level, final BlockPos pos, final BlockState state, final CampfireBlockEntity entity) {
|
| 101 | RandomSource random = level.getRandom();
|
| 102 | if (random.nextFloat() < 0.11F) {
|
| 103 | for (int i = 0; i < random.nextInt(2) + 2; i++) {
|
| 104 | CampfireBlock.makeParticles(level, pos, state.getValue(CampfireBlock.SIGNAL_FIRE), false);
|
| 105 | }
|
| 106 | }
|
| 107 |
|
| 108 | int rotation = state.getValue(CampfireBlock.FACING).get2DDataValue();
|
| 109 |
|
| 110 | for (int slot = 0; slot < entity.items.size(); slot++) {
|
| 111 | if (!entity.items.get(slot).isEmpty() && random.nextFloat() < 0.2F) {
|
| 112 | Direction direction = Direction.from2DDataValue(Math.floorMod(slot + rotation, 4));
|
| 113 | float distanceFromCenter = 0.3125F;
|
| 114 | double x = pos.getX() + 0.5 - direction.getStepX() * 0.3125F + direction.getClockWise().getStepX() * 0.3125F;
|
| 115 | double y = pos.getY() + 0.5;
|
| 116 | double z = pos.getZ() + 0.5 - direction.getStepZ() * 0.3125F + direction.getClockWise().getStepZ() * 0.3125F;
|
| 117 |
|
| 118 | for (int i = 0; i < 4; i++) {
|
| 119 | level.addParticle(ParticleTypes.SMOKE, x, y, z, 0.0, 5.0E-4, 0.0);
|
| 120 | }
|
| 121 | }
|
| 122 | }
|
| 123 | }
|
| 124 |
|
| 125 | public NonNullList<ItemStack> getItems() {
|
| 126 | return this.items;
|
| 127 | }
|
| 128 |
|
| 129 | @Override
|
| 130 | protected void loadAdditional(final ValueInput input) {
|
| 131 | super.loadAdditional(input);
|
| 132 | this.items.clear();
|
| 133 | ContainerHelper.loadAllItems(input, this.items);
|
| 134 | input.getIntArray("CookingTimes")
|
| 135 | .ifPresentOrElse(
|
| 136 | cookingTimes -> System.arraycopy(cookingTimes, 0, this.cookingProgress, 0, Math.min(this.cookingTime.length, cookingTimes.length)),
|
| 137 | () -> Arrays.fill(this.cookingProgress, 0)
|
| 138 | );
|
| 139 | input.getIntArray("CookingTotalTimes")
|
| 140 | .ifPresentOrElse(
|
| 141 | cookingTimes -> System.arraycopy(cookingTimes, 0, this.cookingTime, 0, Math.min(this.cookingTime.length, cookingTimes.length)),
|
| 142 | () -> Arrays.fill(this.cookingTime, 0)
|
| 143 | );
|
| 144 | }
|
| 145 |
|
| 146 | @Override
|
| 147 | protected void saveAdditional(final ValueOutput output) {
|
| 148 | super.saveAdditional(output);
|
| 149 | ContainerHelper.saveAllItems(output, this.items, true);
|
| 150 | output.putIntArray("CookingTimes", this.cookingProgress);
|
| 151 | output.putIntArray("CookingTotalTimes", this.cookingTime);
|
| 152 | }
|
| 153 |
|
| 154 | public ClientboundBlockEntityDataPacket getUpdatePacket() {
|
| 155 | return ClientboundBlockEntityDataPacket.create(this);
|
| 156 | }
|
| 157 |
|
| 158 | @Override
|
| 159 | public CompoundTag getUpdateTag(final HolderLookup.Provider registries) {
|
| 160 | try (ProblemReporter.ScopedCollector reporter = new ProblemReporter.ScopedCollector(this.problemPath(), LOGGER)) {
|
| 161 | TagValueOutput output = TagValueOutput.createWithContext(reporter, registries);
|
| 162 | ContainerHelper.saveAllItems(output, this.items, true);
|
| 163 | return output.buildResult();
|
| 164 | }
|
| 165 | }
|
| 166 |
|
| 167 | public boolean placeFood(final ServerLevel serverLevel, final @Nullable LivingEntity sourceEntity, final ItemStack placeItem) {
|
| 168 | for (int slot = 0; slot < this.items.size(); slot++) {
|
| 169 | ItemStack item = this.items.get(slot);
|
| 170 | if (item.isEmpty()) {
|
| 171 | Optional<RecipeHolder<CampfireCookingRecipe>> recipe = serverLevel.recipeAccess()
|
| 172 | .getRecipeFor(RecipeType.CAMPFIRE_COOKING, new SingleRecipeInput(placeItem), serverLevel);
|
| 173 | if (recipe.isEmpty()) {
|
| 174 | return false;
|
| 175 | }
|
| 176 |
|
| 177 | this.cookingTime[slot] = recipe.get().value().cookingTime();
|
| 178 | this.cookingProgress[slot] = 0;
|
| 179 | this.items.set(slot, placeItem.consumeAndReturn(1, sourceEntity));
|
| 180 | serverLevel.gameEvent(GameEvent.BLOCK_CHANGE, this.getBlockPos(), GameEvent.Context.of(sourceEntity, this.getBlockState()));
|
| 181 | this.markUpdated();
|
| 182 | return true;
|
| 183 | }
|
| 184 | }
|
| 185 |
|
| 186 | return false;
|
| 187 | }
|
| 188 |
|
| 189 | private void markUpdated() {
|
| 190 | this.setChanged();
|
| 191 | this.getLevel().sendBlockUpdated(this.getBlockPos(), this.getBlockState(), this.getBlockState(), 3);
|
| 192 | }
|
| 193 |
|
| 194 | @Override
|
| 195 | public void clearContent() {
|
| 196 | this.items.clear();
|
| 197 | }
|
| 198 |
|
| 199 | @Override
|
| 200 | public void preRemoveSideEffects(final BlockPos pos, final BlockState state) {
|
| 201 | if (this.level != null) {
|
| 202 | Containers.dropContents(this.level, pos, this.getItems());
|
| 203 | }
|
| 204 | }
|
| 205 |
|
| 206 | @Override
|
| 207 | protected void applyImplicitComponents(final DataComponentGetter components) {
|
| 208 | super.applyImplicitComponents(components);
|
| 209 | components.getOrDefault(DataComponents.CONTAINER, ItemContainerContents.EMPTY).copyInto(this.getItems());
|
| 210 | }
|
| 211 |
|
| 212 | @Override
|
| 213 | protected void collectImplicitComponents(final DataComponentMap.Builder components) {
|
| 214 | super.collectImplicitComponents(components);
|
| 215 | components.set(DataComponents.CONTAINER, ItemContainerContents.fromItems(this.getItems()));
|
| 216 | }
|
| 217 |
|
| 218 | @Override
|
| 219 | public void removeComponentsFromTag(final ValueOutput output) {
|
| 220 | output.discard("Items");
|
| 221 | }
|
| 222 | }
|
| 223 |
|