| 1 | package net.minecraft.world.level.block.entity;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import it.unimi.dsi.fastutil.objects.Reference2IntOpenHashMap;
|
| 6 | import it.unimi.dsi.fastutil.objects.Reference2IntMap.Entry;
|
| 7 | import java.util.List;
|
| 8 | import java.util.Map;
|
| 9 | import net.minecraft.core.BlockPos;
|
| 10 | import net.minecraft.core.Direction;
|
| 11 | import net.minecraft.core.NonNullList;
|
| 12 | import net.minecraft.core.component.DataComponents;
|
| 13 | import net.minecraft.resources.ResourceKey;
|
| 14 | import net.minecraft.server.level.ServerLevel;
|
| 15 | import net.minecraft.server.level.ServerPlayer;
|
| 16 | import net.minecraft.tags.ItemTags;
|
| 17 | import net.minecraft.util.Mth;
|
| 18 | import net.minecraft.world.ContainerHelper;
|
| 19 | import net.minecraft.world.Containers;
|
| 20 | import net.minecraft.world.WorldlyContainer;
|
| 21 | import net.minecraft.world.entity.ExperienceOrb;
|
| 22 | import net.minecraft.world.entity.player.Player;
|
| 23 | import net.minecraft.world.entity.player.StackedItemContents;
|
| 24 | import net.minecraft.world.inventory.ContainerData;
|
| 25 | import net.minecraft.world.inventory.RecipeCraftingHolder;
|
| 26 | import net.minecraft.world.inventory.StackedContentsCompatible;
|
| 27 | import net.minecraft.world.item.Item;
|
| 28 | import net.minecraft.world.item.ItemStack;
|
| 29 | import net.minecraft.world.item.ItemStackTemplate;
|
| 30 | import net.minecraft.world.item.Items;
|
| 31 | import net.minecraft.world.item.component.CookingFuel;
|
| 32 | import net.minecraft.world.item.crafting.AbstractCookingRecipe;
|
| 33 | import net.minecraft.world.item.crafting.Recipe;
|
| 34 | import net.minecraft.world.item.crafting.RecipeHolder;
|
| 35 | import net.minecraft.world.item.crafting.RecipeManager;
|
| 36 | import net.minecraft.world.item.crafting.RecipeType;
|
| 37 | import net.minecraft.world.item.crafting.SingleRecipeInput;
|
| 38 | import net.minecraft.world.level.block.AbstractFurnaceBlock;
|
| 39 | import net.minecraft.world.level.block.state.BlockState;
|
| 40 | import net.minecraft.world.level.storage.ValueInput;
|
| 41 | import net.minecraft.world.level.storage.ValueOutput;
|
| 42 | import net.minecraft.world.level.storage.loot.providers.number.floats.ResolvableFloat;
|
| 43 | import net.minecraft.world.level.storage.loot.providers.number.ints.ResolvableInt;
|
| 44 | import net.minecraft.world.phys.Vec3;
|
| 45 | import org.jspecify.annotations.Nullable;
|
| 46 |
|
| 47 | public abstract class AbstractFurnaceBlockEntity extends BaseContainerBlockEntity implements WorldlyContainer, StackedContentsCompatible, RecipeCraftingHolder {
|
| 48 | protected static final int SLOT_INPUT = 0;
|
| 49 | protected static final int SLOT_FUEL = 1;
|
| 50 | protected static final int SLOT_RESULT = 2;
|
| 51 | public static final int DATA_LIT_TIME = 0;
|
| 52 | private static final int[] SLOTS_FOR_UP = new int[]{0};
|
| 53 | private static final int[] SLOTS_FOR_DOWN = new int[]{2, 1};
|
| 54 | private static final int[] SLOTS_FOR_SIDES = new int[]{1};
|
| 55 | public static final int DATA_LIT_DURATION = 1;
|
| 56 | public static final int DATA_COOKING_PROGRESS = 2;
|
| 57 | public static final int DATA_COOKING_TOTAL_TIME = 3;
|
| 58 | public static final int NUM_DATA_VALUES = 4;
|
| 59 | public static final int BURN_TIME_STANDARD = 200;
|
| 60 | public static final int BURN_COOL_SPEED = 2;
|
| 61 | private static final Codec<Map<ResourceKey<Recipe<?>>, Integer>> RECIPES_USED_CODEC = Codec.unboundedMap(Recipe.KEY_CODEC, Codec.INT);
|
| 62 | private static final int DEFAULT_COOKING_TIMER = 0;
|
| 63 | private static final int DEFAULT_COOKING_TOTAL_TIME = 0;
|
| 64 | private static final int DEFAULT_LIT_TIME_REMAINING = 0;
|
| 65 | private static final int DEFAULT_LIT_TOTAL_TIME = 0;
|
| 66 | private static final float DEFAULT_SPEED_MULTIPLIER = 1.0F;
|
| 67 | protected NonNullList<ItemStack> items = NonNullList.withSize(3, ItemStack.EMPTY);
|
| 68 | private int litTimeRemaining;
|
| 69 | private int litTotalTime;
|
| 70 | private int cookingTimer;
|
| 71 | private int cookingTotalTime;
|
| 72 | private float speedMultiplier = 1.0F;
|
| 73 | protected final ContainerData dataAccess = new ContainerData() {
|
| 74 | @Override
|
| 75 | public int get(final int dataId) {
|
| 76 | switch (dataId) {
|
| 77 | case 0:
|
| 78 | return AbstractFurnaceBlockEntity.this.litTimeRemaining;
|
| 79 | case 1:
|
| 80 | return AbstractFurnaceBlockEntity.this.litTotalTime;
|
| 81 | case 2:
|
| 82 | return AbstractFurnaceBlockEntity.this.cookingTimer;
|
| 83 | case 3:
|
| 84 | return AbstractFurnaceBlockEntity.this.cookingTotalTime;
|
| 85 | default:
|
| 86 | return 0;
|
| 87 | }
|
| 88 | }
|
| 89 |
|
| 90 | @Override
|
| 91 | public void set(final int dataId, final int value) {
|
| 92 | switch (dataId) {
|
| 93 | case 0:
|
| 94 | AbstractFurnaceBlockEntity.this.litTimeRemaining = value;
|
| 95 | break;
|
| 96 | case 1:
|
| 97 | AbstractFurnaceBlockEntity.this.litTotalTime = value;
|
| 98 | break;
|
| 99 | case 2:
|
| 100 | AbstractFurnaceBlockEntity.this.cookingTimer = value;
|
| 101 | break;
|
| 102 | case 3:
|
| 103 | AbstractFurnaceBlockEntity.this.cookingTotalTime = value;
|
| 104 | }
|
| 105 | }
|
| 106 |
|
| 107 | @Override
|
| 108 | public int getCount() {
|
| 109 | return 4;
|
| 110 | }
|
| 111 | };
|
| 112 | private final Reference2IntOpenHashMap<ResourceKey<Recipe<?>>> recipesUsed = new Reference2IntOpenHashMap<>();
|
| 113 | private final RecipeManager.CachedCheck<SingleRecipeInput, ? extends AbstractCookingRecipe> quickCheck;
|
| 114 |
|
| 115 | protected AbstractFurnaceBlockEntity(
|
| 116 | final BlockEntityType<?> type, final BlockPos worldPosition, final BlockState blockState, final RecipeType<? extends AbstractCookingRecipe> recipeType
|
| 117 | ) {
|
| 118 | super(type, worldPosition, blockState);
|
| 119 | this.quickCheck = RecipeManager.createCheck(recipeType);
|
| 120 | }
|
| 121 |
|
| 122 | @Override
|
| 123 | protected void loadAdditional(final ValueInput input) {
|
| 124 | super.loadAdditional(input);
|
| 125 | this.items = NonNullList.withSize(this.getContainerSize(), ItemStack.EMPTY);
|
| 126 | ContainerHelper.loadAllItems(input, this.items);
|
| 127 | this.cookingTimer = input.getIntOr("cooking_time_spent", 0);
|
| 128 | this.cookingTotalTime = input.getIntOr("cooking_total_time", 0);
|
| 129 | this.litTimeRemaining = input.getIntOr("lit_time_remaining", 0);
|
| 130 | this.litTotalTime = input.getIntOr("lit_total_time", 0);
|
| 131 | this.speedMultiplier = input.getFloatOr("speed_multiplier", 1.0F);
|
| 132 | this.recipesUsed.clear();
|
| 133 | this.recipesUsed.putAll(input.read("RecipesUsed", RECIPES_USED_CODEC).orElse(Map.of()));
|
| 134 | }
|
| 135 |
|
| 136 | @Override
|
| 137 | protected void saveAdditional(final ValueOutput output) {
|
| 138 | super.saveAdditional(output);
|
| 139 | output.putInt("cooking_time_spent", this.cookingTimer);
|
| 140 | output.putInt("cooking_total_time", this.cookingTotalTime);
|
| 141 | output.putInt("lit_time_remaining", this.litTimeRemaining);
|
| 142 | output.putInt("lit_total_time", this.litTotalTime);
|
| 143 | output.putFloat("speed_multiplier", this.speedMultiplier);
|
| 144 | ContainerHelper.saveAllItems(output, this.items);
|
| 145 | output.store("RecipesUsed", RECIPES_USED_CODEC, this.recipesUsed);
|
| 146 | }
|
| 147 |
|
| 148 | public static void serverTick(final ServerLevel level, final BlockPos pos, BlockState state, final AbstractFurnaceBlockEntity entity) {
|
| 149 | boolean changed = false;
|
| 150 | boolean isLit;
|
| 151 | boolean wasLit;
|
| 152 | if (entity.litTimeRemaining > 0) {
|
| 153 | wasLit = true;
|
| 154 | entity.litTimeRemaining--;
|
| 155 | isLit = entity.litTimeRemaining > 0;
|
| 156 | } else {
|
| 157 | wasLit = false;
|
| 158 | isLit = false;
|
| 159 | }
|
| 160 |
|
| 161 | ItemStack fuel = entity.items.get(1);
|
| 162 | ItemStack ingredient = entity.items.get(0);
|
| 163 | boolean hasIngredient = !ingredient.isEmpty();
|
| 164 | boolean hasFuel = !fuel.isEmpty();
|
| 165 | if (isLit || hasFuel && hasIngredient) {
|
| 166 | if (hasIngredient) {
|
| 167 | SingleRecipeInput input = new SingleRecipeInput(ingredient);
|
| 168 | RecipeHolder<? extends AbstractCookingRecipe> recipe = entity.quickCheck.getRecipeFor(input, level).orElse(null);
|
| 169 | if (recipe != null) {
|
| 170 | int maxStackSize = entity.getMaxStackSize();
|
| 171 | ItemStack burnResult = recipe.value().assemble(input);
|
| 172 | if (!burnResult.isEmpty() && canBurn(entity.items, maxStackSize, burnResult)) {
|
| 173 | if (!isLit) {
|
| 174 | int newLitTime = entity.getBurnDuration(level, fuel);
|
| 175 | float newSpeedMultiplier = entity.getSpeedMultiplier(level, fuel);
|
| 176 | entity.litTimeRemaining = newLitTime;
|
| 177 | entity.litTotalTime = newLitTime;
|
| 178 | entity.speedMultiplier = newSpeedMultiplier;
|
| 179 | if (entity.cookingTotalTime > 0 && entity.cookingTimer < entity.cookingTotalTime) {
|
| 180 | float completionRatio = (float)entity.cookingTimer / entity.cookingTotalTime;
|
| 181 | entity.cookingTotalTime = getTotalCookTime(recipe, entity);
|
| 182 | entity.cookingTimer = (int)Math.ceil(completionRatio * entity.cookingTotalTime);
|
| 183 | }
|
| 184 |
|
| 185 | if (newLitTime > 0) {
|
| 186 | consumeFuel(level, pos, entity.items, fuel);
|
| 187 | isLit = true;
|
| 188 | changed = true;
|
| 189 | }
|
| 190 | }
|
| 191 |
|
| 192 | if (isLit) {
|
| 193 | entity.cookingTimer++;
|
| 194 | if (entity.cookingTimer >= entity.cookingTotalTime) {
|
| 195 | entity.cookingTimer = 0;
|
| 196 | entity.cookingTotalTime = getTotalCookTime(recipe, entity);
|
| 197 | burn(entity.items, ingredient, burnResult);
|
| 198 | entity.setRecipeUsed(recipe);
|
| 199 | changed = true;
|
| 200 | }
|
| 201 | } else {
|
| 202 | entity.cookingTimer = 0;
|
| 203 | }
|
| 204 | } else {
|
| 205 | entity.cookingTimer = 0;
|
| 206 | }
|
| 207 | }
|
| 208 | } else {
|
| 209 | entity.cookingTimer = 0;
|
| 210 | }
|
| 211 | } else if (entity.cookingTimer > 0) {
|
| 212 | entity.cookingTimer = Mth.clamp(entity.cookingTimer - 2, 0, entity.cookingTotalTime);
|
| 213 | }
|
| 214 |
|
| 215 | if (wasLit != isLit) {
|
| 216 | changed = true;
|
| 217 | state = state.setValue(AbstractFurnaceBlock.LIT, isLit);
|
| 218 | level.setBlockAndUpdate(pos, state);
|
| 219 | }
|
| 220 |
|
| 221 | if (changed) {
|
| 222 | setChanged(level, pos, state);
|
| 223 | }
|
| 224 | }
|
| 225 |
|
| 226 | private static void consumeFuel(final ServerLevel level, final BlockPos pos, final NonNullList<ItemStack> items, final ItemStack fuel) {
|
| 227 | Item fuelItem = fuel.getItem();
|
| 228 | ItemStackTemplate remainder = fuelItem.getCraftingRemainder();
|
| 229 | ItemStack newFuel = fuel;
|
| 230 | fuel.shrink(1);
|
| 231 | if (remainder != null) {
|
| 232 | if (fuel.isEmpty()) {
|
| 233 | newFuel = remainder.create();
|
| 234 | } else {
|
| 235 | Containers.dropItemStack(level, pos.getX(), pos.getY(), pos.getZ(), remainder.create());
|
| 236 | }
|
| 237 | }
|
| 238 |
|
| 239 | items.set(1, newFuel);
|
| 240 | }
|
| 241 |
|
| 242 | private static boolean canBurn(final NonNullList<ItemStack> items, final int maxStackSize, final ItemStack burnResult) {
|
| 243 | ItemStack resultItemStack = items.get(2);
|
| 244 | if (resultItemStack.isEmpty()) {
|
| 245 | return true;
|
| 246 | }
|
| 247 |
|
| 248 | if (!ItemStack.isSameItemSameComponents(resultItemStack, burnResult)) {
|
| 249 | return false;
|
| 250 | }
|
| 251 |
|
| 252 | int resultCount = resultItemStack.getCount() + burnResult.count();
|
| 253 | int maxResultCount = Math.min(maxStackSize, burnResult.getMaxStackSize());
|
| 254 | return resultCount <= maxResultCount;
|
| 255 | }
|
| 256 |
|
| 257 | private static void burn(final NonNullList<ItemStack> items, final ItemStack inputItemStack, final ItemStack result) {
|
| 258 | ItemStack resultItemStack = items.get(2);
|
| 259 | if (resultItemStack.isEmpty()) {
|
| 260 | items.set(2, result.copy());
|
| 261 | } else {
|
| 262 | resultItemStack.grow(result.getCount());
|
| 263 | }
|
| 264 |
|
| 265 | if (inputItemStack.is(Items.WET_SPONGE) && !items.get(1).isEmpty() && items.get(1).is(Items.BUCKET)) {
|
| 266 | items.set(1, new ItemStack(Items.WATER_BUCKET));
|
| 267 | }
|
| 268 |
|
| 269 | inputItemStack.shrink(1);
|
| 270 | }
|
| 271 |
|
| 272 | protected int getBurnDuration(final ServerLevel level, final ItemStack fuelItem) {
|
| 273 | return ResolvableInt.getFromItem(fuelItem, DataComponents.COOKING_FUEL, CookingFuel::burnTime, this.getLootContext(level), 0);
|
| 274 | }
|
| 275 |
|
| 276 | protected float getSpeedMultiplier(final ServerLevel level, final ItemStack fuelItem) {
|
| 277 | return ResolvableFloat.getFromItem(fuelItem, DataComponents.COOKING_FUEL, CookingFuel::speedMultiplier, this.getLootContext(level), 1.0F);
|
| 278 | }
|
| 279 |
|
| 280 | private static int getTotalCookTime(final RecipeHolder<? extends AbstractCookingRecipe> recipe, final AbstractFurnaceBlockEntity entity) {
|
| 281 | int cookingTotalTime = recipe.value().cookingTime();
|
| 282 | return entity.speedMultiplier > 0.0F ? (int)Math.ceil(cookingTotalTime / entity.speedMultiplier) : cookingTotalTime;
|
| 283 | }
|
| 284 |
|
| 285 | private static int getTotalCookTime(final ServerLevel level, final AbstractFurnaceBlockEntity entity) {
|
| 286 | SingleRecipeInput input = new SingleRecipeInput(entity.getItem(0));
|
| 287 | return entity.quickCheck
|
| 288 | .getRecipeFor(input, level)
|
| 289 | .map(recipeHolder -> getTotalCookTime((RecipeHolder<? extends AbstractCookingRecipe>)recipeHolder, entity))
|
| 290 | .orElse(200);
|
| 291 | }
|
| 292 |
|
| 293 | @Override
|
| 294 | public int[] getSlotsForFace(final Direction direction) {
|
| 295 | if (direction == Direction.DOWN) {
|
| 296 | return SLOTS_FOR_DOWN;
|
| 297 | } else {
|
| 298 | return direction == Direction.UP ? SLOTS_FOR_UP : SLOTS_FOR_SIDES;
|
| 299 | }
|
| 300 | }
|
| 301 |
|
| 302 | @Override
|
| 303 | public boolean canPlaceItemThroughFace(final int slot, final ItemStack itemStack, final @Nullable Direction direction) {
|
| 304 | return this.canPlaceItem(slot, itemStack);
|
| 305 | }
|
| 306 |
|
| 307 | @Override
|
| 308 | public boolean canTakeItemThroughFace(final int slot, final ItemStack itemStack, final Direction direction) {
|
| 309 | return direction == Direction.DOWN && slot == 1 ? itemStack.is(ItemTags.FURNACE_FUEL_BOTTOM_TAKEABLE) : true;
|
| 310 | }
|
| 311 |
|
| 312 | @Override
|
| 313 | public int getContainerSize() {
|
| 314 | return this.items.size();
|
| 315 | }
|
| 316 |
|
| 317 | @Override
|
| 318 | protected NonNullList<ItemStack> getItems() {
|
| 319 | return this.items;
|
| 320 | }
|
| 321 |
|
| 322 | @Override
|
| 323 | protected void setItems(final NonNullList<ItemStack> items) {
|
| 324 | this.items = items;
|
| 325 | }
|
| 326 |
|
| 327 | @Override
|
| 328 | public void setItem(final int slot, final ItemStack itemStack) {
|
| 329 | ItemStack oldStack = this.items.get(slot);
|
| 330 | boolean same = !itemStack.isEmpty() && ItemStack.isSameItemSameComponents(oldStack, itemStack);
|
| 331 | this.items.set(slot, itemStack);
|
| 332 | itemStack.limitSize(this.getMaxStackSize(itemStack));
|
| 333 | if (slot == 0 && !same && this.level instanceof ServerLevel serverLevel) {
|
| 334 | this.cookingTotalTime = getTotalCookTime(serverLevel, this);
|
| 335 | this.cookingTimer = 0;
|
| 336 | this.setChanged();
|
| 337 | }
|
| 338 | }
|
| 339 |
|
| 340 | @Override
|
| 341 | public boolean canPlaceItem(final int slot, final ItemStack itemStack) {
|
| 342 | if (slot == 2) {
|
| 343 | return false;
|
| 344 | }
|
| 345 |
|
| 346 | if (slot != 1) {
|
| 347 | return true;
|
| 348 | }
|
| 349 |
|
| 350 | ItemStack fuelSlot = this.items.get(1);
|
| 351 | return itemStack.has(DataComponents.COOKING_FUEL) || itemStack.is(Items.BUCKET) && !fuelSlot.is(Items.BUCKET);
|
| 352 | }
|
| 353 |
|
| 354 | @Override
|
| 355 | public void setRecipeUsed(final @Nullable RecipeHolder<?> recipeUsed) {
|
| 356 | if (recipeUsed != null) {
|
| 357 | ResourceKey<Recipe<?>> id = recipeUsed.id();
|
| 358 | this.recipesUsed.addTo(id, 1);
|
| 359 | }
|
| 360 | }
|
| 361 |
|
| 362 | @Override
|
| 363 | public @Nullable RecipeHolder<?> getRecipeUsed() {
|
| 364 | return null;
|
| 365 | }
|
| 366 |
|
| 367 | @Override
|
| 368 | public void awardUsedRecipes(final Player player, final List<ItemStack> itemStacks) {
|
| 369 | }
|
| 370 |
|
| 371 | public void awardUsedRecipesAndPopExperience(final ServerPlayer player) {
|
| 372 | List<RecipeHolder<?>> recipesToAward = this.getRecipesToAwardAndPopExperience(player.level(), player.position());
|
| 373 | player.awardRecipes(recipesToAward);
|
| 374 |
|
| 375 | for (RecipeHolder<?> recipe : recipesToAward) {
|
| 376 | player.triggerRecipeCrafted(recipe, this.items);
|
| 377 | }
|
| 378 |
|
| 379 | this.recipesUsed.clear();
|
| 380 | }
|
| 381 |
|
| 382 | public List<RecipeHolder<?>> getRecipesToAwardAndPopExperience(final ServerLevel level, final Vec3 position) {
|
| 383 | List<RecipeHolder<?>> recipesToAward = Lists.newArrayList();
|
| 384 |
|
| 385 | for (Entry<ResourceKey<Recipe<?>>> entry : this.recipesUsed.reference2IntEntrySet()) {
|
| 386 | level.recipeAccess().byKey(entry.getKey()).ifPresent(recipe -> {
|
| 387 | recipesToAward.add((RecipeHolder<?>)recipe);
|
| 388 | createExperience(level, position, entry.getIntValue(), ((AbstractCookingRecipe)recipe.value()).experience());
|
| 389 | });
|
| 390 | }
|
| 391 |
|
| 392 | return recipesToAward;
|
| 393 | }
|
| 394 |
|
| 395 | private static void createExperience(final ServerLevel level, final Vec3 position, final int amount, final float value) {
|
| 396 | int xpReward = Mth.floor(amount * value);
|
| 397 | float xpFraction = Mth.frac(amount * value);
|
| 398 | if (xpFraction != 0.0F && level.getRandom().nextFloat() < xpFraction) {
|
| 399 | xpReward++;
|
| 400 | }
|
| 401 |
|
| 402 | ExperienceOrb.award(level, position, xpReward);
|
| 403 | }
|
| 404 |
|
| 405 | @Override
|
| 406 | public void fillStackedContents(final StackedItemContents contents) {
|
| 407 | for (ItemStack itemStack : this.items) {
|
| 408 | contents.accountStack(itemStack);
|
| 409 | }
|
| 410 | }
|
| 411 |
|
| 412 | @Override
|
| 413 | public void preRemoveSideEffects(final BlockPos pos, final BlockState state) {
|
| 414 | super.preRemoveSideEffects(pos, state);
|
| 415 | if (this.level instanceof ServerLevel serverLevel) {
|
| 416 | this.getRecipesToAwardAndPopExperience(serverLevel, Vec3.atCenterOf(pos));
|
| 417 | }
|
| 418 | }
|
| 419 | }
|
| 420 |
|