PaperMC/patches/server/0262-Implement-furnace-cook-speed-multiplier-API.patch
2022-03-30 13:28:38 -07:00

115 lines
7.3 KiB
Diff

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
Signed-off-by: Tassu <git@tassu.me>
Fixed an issue where a furnace's cook-speed multiplier rounds down
to the nearest Integer when updating its current cook time.
Modified by: Eric Su <ericsu@alumni.usc.edu>
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
index c3c53a34f645a834256c31951e465ae33aa1a9ff..635d47d4bea679e96736ef891c40f57488cbc6a1 100644
--- a/src/main/java/net/minecraft/world/level/block/entity/AbstractFurnaceBlockEntity.java
+++ b/src/main/java/net/minecraft/world/level/block/entity/AbstractFurnaceBlockEntity.java
@@ -75,6 +75,7 @@ public abstract class AbstractFurnaceBlockEntity extends BaseContainerBlockEntit
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;
@@ -281,6 +282,11 @@ public abstract class AbstractFurnaceBlockEntity extends BaseContainerBlockEntit
this.recipesUsed.put(new ResourceLocation(s), nbttagcompound1.getInt(s));
}
+ // Paper start - cook speed API
+ if (nbt.contains("Paper.CookSpeedMultiplier")) {
+ this.cookSpeedMultiplier = nbt.getDouble("Paper.CookSpeedMultiplier");
+ }
+ // Paper end
}
@Override
@@ -289,6 +295,7 @@ public abstract class AbstractFurnaceBlockEntity extends BaseContainerBlockEntit
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();
@@ -351,7 +358,7 @@ public abstract class AbstractFurnaceBlockEntity extends BaseContainerBlockEntit
CraftItemStack source = CraftItemStack.asCraftMirror(blockEntity.items.get(0));
CookingRecipe<?> recipe = (CookingRecipe<?>) irecipe.toBukkitRecipe();
- 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();
@@ -359,9 +366,9 @@ public abstract class AbstractFurnaceBlockEntity extends BaseContainerBlockEntit
// CraftBukkit end
++blockEntity.cookingProgress;
- if (blockEntity.cookingProgress == blockEntity.cookingTotalTime) {
+ if (blockEntity.cookingProgress >= blockEntity.cookingTotalTime) { // Paper - cook speed multiplier API
blockEntity.cookingProgress = 0;
- blockEntity.cookingTotalTime = AbstractFurnaceBlockEntity.getTotalCookTime(world, blockEntity.recipeType, blockEntity);
+ blockEntity.cookingTotalTime = AbstractFurnaceBlockEntity.getTotalCookTime(world, blockEntity.recipeType, blockEntity, blockEntity.cookSpeedMultiplier);
if (AbstractFurnaceBlockEntity.burn(blockEntity.level, blockEntity.worldPosition, irecipe, blockEntity.items, i)) { // CraftBukkit
blockEntity.setRecipeUsed(irecipe);
}
@@ -461,9 +468,13 @@ public abstract class AbstractFurnaceBlockEntity extends BaseContainerBlockEntit
}
}
- private static int getTotalCookTime(Level world, RecipeType<? extends AbstractCookingRecipe> recipeType, Container inventory) {
- return (world != null) ? (Integer) world.getRecipeManager().getRecipeFor((RecipeType<AbstractCookingRecipe>) recipeType, inventory, world).map(AbstractCookingRecipe::getCookingTime).orElse(200) : 200; // CraftBukkit - SPIGOT-4302 // Eclipse fail
+ // Paper begin - Expose this function so CraftFurnace can correctly scale the total cooking time to a new multiplier
+ public static int getTotalCookTime(@Nullable Level world, RecipeType<? extends AbstractCookingRecipe> recipeType, Container inventory, final double cookSpeedMultiplier) {
+ /* Scale the recipe's cooking time to the current cookSpeedMultiplier */
+ int cookTime = (world != null ? world.getRecipeManager() : net.minecraft.server.MinecraftServer.getServer().getRecipeManager()).getRecipeFor(recipeType, inventory, world /* passing a null level here is safe. world is only used for map extending recipes which won't happen here */).map(AbstractCookingRecipe::getCookingTime).orElse(200); // CraftBukkit - SPIGOT-4302 // Eclipse fail
+ return (int) Math.ceil (cookTime / cookSpeedMultiplier);
}
+ // Paper end
public static boolean isFuel(ItemStack stack) {
return AbstractFurnaceBlockEntity.getFuel().containsKey(stack.getItem());
@@ -532,7 +543,7 @@ public abstract class AbstractFurnaceBlockEntity extends BaseContainerBlockEntit
}
if (slot == 0 && !flag) {
- this.cookingTotalTime = AbstractFurnaceBlockEntity.getTotalCookTime(this.level, this.recipeType, this);
+ this.cookingTotalTime = AbstractFurnaceBlockEntity.getTotalCookTime(this.level, this.recipeType, this, this.cookSpeedMultiplier);
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
index facf95a44b5d3a63fda156c6afc8cabe50b21d32..3da4616c904d47bbecae0d4cb6970496fbec9a8b 100644
--- a/src/main/java/org/bukkit/craftbukkit/block/CraftFurnace.java
+++ b/src/main/java/org/bukkit/craftbukkit/block/CraftFurnace.java
@@ -78,4 +78,20 @@ public abstract class CraftFurnace<T extends AbstractFurnaceBlockEntity> extends
return recipesUsed.build();
}
+
+ // 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;
+ 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
+ }
+ // Paper end
}