2021-06-13 07:09:48 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Tassu <git@tassu.me>
Date: Thu, 13 Sep 2018 08:45:21 +0300
Subject: [PATCH] Implement furnace cook speed multiplier API
Fixed an issue where a furnace's cook-speed multiplier rounds down
to the nearest Integer when updating its current cook time.
2024-01-21 13:56:22 +01:00
Co-authored-by: Eric Su <ericsu@alumni.usc.edu>
2021-06-13 07:09:48 +02:00
diff --git a/src/main/java/net/minecraft/world/level/block/entity/AbstractFurnaceBlockEntity.java b/src/main/java/net/minecraft/world/level/block/entity/AbstractFurnaceBlockEntity.java
2024-01-21 13:56:22 +01:00
index 7a13042631bea761952490cfd14dc20147405161..9801b777bc6ab7de91d82b29a142459292ee8605 100644
2021-06-13 07:09:48 +02:00
--- a/src/main/java/net/minecraft/world/level/block/entity/AbstractFurnaceBlockEntity.java
+++ b/src/main/java/net/minecraft/world/level/block/entity/AbstractFurnaceBlockEntity.java
2024-01-14 10:46:04 +01:00
@@ -78,11 +78,13 @@ public abstract class AbstractFurnaceBlockEntity extends BaseContainerBlockEntit
2021-06-13 07:09:48 +02:00
protected NonNullList<ItemStack> items;
public int litTime;
int litDuration;
+ public double cookSpeedMultiplier = 1.0; // Paper - cook speed multiplier API
public int cookingProgress;
public int cookingTotalTime;
protected final ContainerData dataAccess;
2022-06-08 02:15:06 +02:00
public final Object2IntOpenHashMap<ResourceLocation> recipesUsed;
private final RecipeManager.CachedCheck<Container, ? extends AbstractCookingRecipe> quickCheck;
2024-01-21 13:56:22 +01:00
+ public final RecipeType<? extends AbstractCookingRecipe> recipeType; // Paper - cook speed multiplier API
2022-06-08 02:15:06 +02:00
protected AbstractFurnaceBlockEntity(BlockEntityType<?> blockEntityType, BlockPos pos, BlockState state, RecipeType<? extends AbstractCookingRecipe> recipeType) {
super(blockEntityType, pos, state);
2024-01-14 10:46:04 +01:00
@@ -129,6 +131,7 @@ public abstract class AbstractFurnaceBlockEntity extends BaseContainerBlockEntit
2022-06-08 02:15:06 +02:00
};
this.recipesUsed = new Object2IntOpenHashMap();
this.quickCheck = RecipeManager.createCheck((RecipeType<AbstractCookingRecipe>) recipeType); // CraftBukkit - decompile error // Eclipse fail
2024-01-21 13:56:22 +01:00
+ this.recipeType = recipeType; // Paper - cook speed multiplier API
2022-06-08 02:15:06 +02:00
}
public static Map<Item, Integer> getFuel() {
2024-01-14 10:46:04 +01:00
@@ -281,6 +284,11 @@ public abstract class AbstractFurnaceBlockEntity extends BaseContainerBlockEntit
2021-06-13 07:09:48 +02:00
this.recipesUsed.put(new ResourceLocation(s), nbttagcompound1.getInt(s));
}
2024-01-21 13:56:22 +01:00
+ // Paper start - cook speed multiplier API
2021-06-13 07:09:48 +02:00
+ if (nbt.contains("Paper.CookSpeedMultiplier")) {
+ this.cookSpeedMultiplier = nbt.getDouble("Paper.CookSpeedMultiplier");
+ }
2024-01-21 13:56:22 +01:00
+ // Paper end - cook speed multiplier API
2021-06-13 07:09:48 +02:00
}
@Override
2024-01-14 10:46:04 +01:00
@@ -289,6 +297,7 @@ public abstract class AbstractFurnaceBlockEntity extends BaseContainerBlockEntit
2021-06-13 07:09:48 +02:00
nbt.putShort("BurnTime", (short) this.litTime);
nbt.putShort("CookTime", (short) this.cookingProgress);
nbt.putShort("CookTimeTotal", (short) this.cookingTotalTime);
+ nbt.putDouble("Paper.CookSpeedMultiplier", this.cookSpeedMultiplier); // Paper - cook speed multiplier API
ContainerHelper.saveAllItems(nbt, this.items);
CompoundTag nbttagcompound1 = new CompoundTag();
2024-01-14 10:46:04 +01:00
@@ -360,7 +369,7 @@ public abstract class AbstractFurnaceBlockEntity extends BaseContainerBlockEntit
2021-09-22 21:46:15 +02:00
CraftItemStack source = CraftItemStack.asCraftMirror(blockEntity.items.get(0));
2023-09-22 00:26:51 +02:00
CookingRecipe<?> recipe = (CookingRecipe<?>) recipeholder.toBukkitRecipe();
2021-09-22 21:46:15 +02:00
- FurnaceStartSmeltEvent event = new FurnaceStartSmeltEvent(CraftBlock.at(world, pos), source, recipe);
+ FurnaceStartSmeltEvent event = new FurnaceStartSmeltEvent(CraftBlock.at(world, pos), source, recipe, AbstractFurnaceBlockEntity.getTotalCookTime(world, blockEntity.recipeType, blockEntity, blockEntity.cookSpeedMultiplier)); // Paper - cook speed multiplier API
world.getCraftServer().getPluginManager().callEvent(event);
blockEntity.cookingTotalTime = event.getTotalCookTime();
2024-01-14 10:46:04 +01:00
@@ -368,9 +377,9 @@ public abstract class AbstractFurnaceBlockEntity extends BaseContainerBlockEntit
2021-07-17 03:48:27 +02:00
// CraftBukkit end
2021-06-13 07:09:48 +02:00
++blockEntity.cookingProgress;
- if (blockEntity.cookingProgress == blockEntity.cookingTotalTime) {
+ if (blockEntity.cookingProgress >= blockEntity.cookingTotalTime) { // Paper - cook speed multiplier API
blockEntity.cookingProgress = 0;
2022-06-08 02:15:06 +02:00
- blockEntity.cookingTotalTime = AbstractFurnaceBlockEntity.getTotalCookTime(world, blockEntity);
2024-01-21 13:56:22 +01:00
+ blockEntity.cookingTotalTime = AbstractFurnaceBlockEntity.getTotalCookTime(world, blockEntity.recipeType, blockEntity, blockEntity.cookSpeedMultiplier); // Paper - cook speed multiplier API
2023-09-22 00:26:51 +02:00
if (AbstractFurnaceBlockEntity.burn(blockEntity.level, blockEntity.worldPosition, world.registryAccess(), recipeholder, blockEntity.items, i)) { // CraftBukkit
blockEntity.setRecipeUsed(recipeholder);
2021-06-13 07:09:48 +02:00
}
2024-01-14 10:46:04 +01:00
@@ -470,11 +479,12 @@ public abstract class AbstractFurnaceBlockEntity extends BaseContainerBlockEntit
2021-06-13 07:09:48 +02:00
}
}
2022-06-08 02:15:06 +02:00
- private static int getTotalCookTime(Level world, AbstractFurnaceBlockEntity furnace) {
2023-09-22 00:26:51 +02:00
- if (world == null) return 200; // CraftBukkit - SPIGOT-4302
- return (Integer) furnace.quickCheck.getRecipeFor(furnace, world).map((recipeholder) -> {
- return ((AbstractCookingRecipe) recipeholder.value()).getCookingTime();
- }).orElse(200);
2024-01-21 13:56:22 +01:00
+ // Paper start - cook speed multiplier API
2022-06-08 03:19:35 +02:00
+ public static int getTotalCookTime(@Nullable Level world, RecipeType<? extends AbstractCookingRecipe> recipeType, AbstractFurnaceBlockEntity furnace, double cookSpeedMultiplier) {
2021-06-13 07:09:48 +02:00
+ /* Scale the recipe's cooking time to the current cookSpeedMultiplier */
2023-09-22 00:26:51 +02:00
+ int cookTime = world != null ? furnace.quickCheck.getRecipeFor(furnace, world).map(holder -> holder.value().getCookingTime()).orElse(200) : (net.minecraft.server.MinecraftServer.getServer().getRecipeManager().getRecipeFor(recipeType, furnace, world /* passing a null level here is safe. world is only used for map extending recipes which won't happen here */).map(holder -> holder.value().getCookingTime()).orElse(200));
2021-06-13 07:09:48 +02:00
+ return (int) Math.ceil (cookTime / cookSpeedMultiplier);
2024-01-21 13:56:22 +01:00
+ // Paper end - cook speed multiplier API
2021-06-13 07:09:48 +02:00
}
public static boolean isFuel(ItemStack stack) {
2024-01-14 10:46:04 +01:00
@@ -544,7 +554,7 @@ public abstract class AbstractFurnaceBlockEntity extends BaseContainerBlockEntit
2021-06-13 07:09:48 +02:00
}
if (slot == 0 && !flag) {
2022-06-08 02:15:06 +02:00
- this.cookingTotalTime = AbstractFurnaceBlockEntity.getTotalCookTime(this.level, this);
2024-01-21 13:56:22 +01:00
+ this.cookingTotalTime = AbstractFurnaceBlockEntity.getTotalCookTime(this.level, this.recipeType, this, this.cookSpeedMultiplier); // Paper - cook speed multiplier API
2021-06-13 07:09:48 +02:00
this.cookingProgress = 0;
this.setChanged();
}
diff --git a/src/main/java/org/bukkit/craftbukkit/block/CraftFurnace.java b/src/main/java/org/bukkit/craftbukkit/block/CraftFurnace.java
2023-09-29 02:28:26 +02:00
index 2f25a502c5c7223101f2d8c125977e2c614cd899..f98cf44e929d326037e1089dafe4f82c6f6d8647 100644
2021-06-13 07:09:48 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/block/CraftFurnace.java
+++ b/src/main/java/org/bukkit/craftbukkit/block/CraftFurnace.java
2023-09-29 02:28:26 +02:00
@@ -85,4 +85,20 @@ public abstract class CraftFurnace<T extends AbstractFurnaceBlockEntity> extends
2022-02-15 11:30:58 +01:00
2023-09-29 02:28:26 +02:00
@Override
public abstract CraftFurnace<T> copy();
2021-06-13 07:09:48 +02:00
+
+ // Paper start - cook speed multiplier API
+ @Override
+ public double getCookSpeedMultiplier() {
+ return this.getSnapshot().cookSpeedMultiplier;
+ }
+
+ @Override
+ public void setCookSpeedMultiplier(double multiplier) {
+ com.google.common.base.Preconditions.checkArgument(multiplier >= 0, "Furnace speed multiplier cannot be negative");
+ com.google.common.base.Preconditions.checkArgument(multiplier <= 200, "Furnace speed multiplier cannot more than 200");
+ T snapshot = this.getSnapshot();
+ snapshot.cookSpeedMultiplier = multiplier;
2022-01-18 20:33:37 +01:00
+ snapshot.cookingTotalTime = AbstractFurnaceBlockEntity.getTotalCookTime(this.isPlaced() ? this.world.getHandle() : null, snapshot.recipeType, snapshot, snapshot.cookSpeedMultiplier); // Update the snapshot's current total cook time to scale with the newly set multiplier
2021-06-13 07:09:48 +02:00
+ }
+ // Paper end
}