PaperMC/patches/server/0245-Implement-furnace-cook-speed-multiplier-API.patch

135 lines
8.7 KiB
Diff
Raw Permalink Normal View History

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-10-25 17:08:48 +02:00
== AT ==
public net.minecraft.world.level.block.entity.AbstractFurnaceBlockEntity getTotalCookTime(Lnet/minecraft/server/level/ServerLevel;Lnet/minecraft/world/level/block/entity/AbstractFurnaceBlockEntity;)I
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-12-03 19:05:31 +01:00
index 9f44f4ed56bf3cabd3d4a5409f7f6068273d7ff7..b2c0de191fdc84d4a007373309a5df81cacd5466 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-12-03 19:05:31 +01:00
@@ -78,6 +78,8 @@ public abstract class AbstractFurnaceBlockEntity extends BaseContainerBlockEntit
2021-06-13 07:09:48 +02:00
protected final ContainerData dataAccess;
2024-10-23 13:13:00 +02:00
public final Reference2IntOpenHashMap<ResourceKey<Recipe<?>>> recipesUsed;
2024-06-13 21:04:27 +02:00
private final RecipeManager.CachedCheck<SingleRecipeInput, ? extends AbstractCookingRecipe> quickCheck;
+ public final RecipeType<? extends AbstractCookingRecipe> recipeType; // Paper - cook speed multiplier API
2024-12-03 19:05:31 +01:00
+ public double cookSpeedMultiplier = 1.0; // 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-12-03 19:05:31 +01:00
@@ -124,6 +126,7 @@ public abstract class AbstractFurnaceBlockEntity extends BaseContainerBlockEntit
2022-06-08 02:15:06 +02:00
};
2024-10-23 13:13:00 +02:00
this.recipesUsed = new Reference2IntOpenHashMap();
2022-06-08 02:15:06 +02:00
this.quickCheck = RecipeManager.createCheck((RecipeType<AbstractCookingRecipe>) recipeType); // CraftBukkit - decompile error // Eclipse fail
+ this.recipeType = recipeType; // Paper - cook speed multiplier API
2022-06-08 02:15:06 +02:00
}
2024-10-23 13:13:00 +02:00
// CraftBukkit start - add fields and methods
2024-12-03 19:05:31 +01:00
@@ -178,6 +181,11 @@ public abstract class AbstractFurnaceBlockEntity extends BaseContainerBlockEntit
2024-10-23 13:13:00 +02:00
this.recipesUsed.put(ResourceKey.create(Registries.RECIPE, ResourceLocation.parse(s)), nbttagcompound1.getInt(s));
2021-06-13 07:09:48 +02: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");
+ }
+ // Paper end - cook speed multiplier API
2021-06-13 07:09:48 +02:00
}
@Override
2024-12-03 19:05:31 +01:00
@@ -187,6 +195,7 @@ public abstract class AbstractFurnaceBlockEntity extends BaseContainerBlockEntit
nbt.putShort("cooking_total_time", (short) this.cookingTotalTime);
nbt.putShort("lit_time_remaining", (short) this.litTimeRemaining);
nbt.putShort("lit_total_time", (short) this.litTotalTime);
2021-06-13 07:09:48 +02:00
+ nbt.putDouble("Paper.CookSpeedMultiplier", this.cookSpeedMultiplier); // Paper - cook speed multiplier API
2024-10-23 13:13:00 +02:00
ContainerHelper.saveAllItems(nbt, this.items, registries);
2021-06-13 07:09:48 +02:00
CompoundTag nbttagcompound1 = new CompoundTag();
2024-12-03 19:05:31 +01:00
@@ -258,7 +267,7 @@ public abstract class AbstractFurnaceBlockEntity extends BaseContainerBlockEntit
CraftItemStack source = CraftItemStack.asCraftMirror(blockEntity.items.get(0));
2023-09-22 00:26:51 +02:00
CookingRecipe<?> recipe = (CookingRecipe<?>) recipeholder.toBukkitRecipe();
- FurnaceStartSmeltEvent event = new FurnaceStartSmeltEvent(CraftBlock.at(world, pos), source, recipe);
2024-10-23 13:13:00 +02:00
+ FurnaceStartSmeltEvent event = new FurnaceStartSmeltEvent(CraftBlock.at(world, pos), source, recipe, AbstractFurnaceBlockEntity.getTotalCookTime(world, blockEntity, blockEntity.recipeType, blockEntity.cookSpeedMultiplier)); // Paper - cook speed multiplier API
world.getCraftServer().getPluginManager().callEvent(event);
blockEntity.cookingTotalTime = event.getTotalCookTime();
2024-12-03 19:05:31 +01:00
@@ -266,9 +275,9 @@ public abstract class AbstractFurnaceBlockEntity extends BaseContainerBlockEntit
// CraftBukkit end
2021-06-13 07:09:48 +02:00
2024-12-03 19:05:31 +01:00
++blockEntity.cookingTimer;
- if (blockEntity.cookingTimer == blockEntity.cookingTotalTime) {
+ if (blockEntity.cookingTimer >= blockEntity.cookingTotalTime) { // Paper - cook speed multiplier API
blockEntity.cookingTimer = 0;
2022-06-08 02:15:06 +02:00
- blockEntity.cookingTotalTime = AbstractFurnaceBlockEntity.getTotalCookTime(world, blockEntity);
2024-10-23 13:13:00 +02:00
+ blockEntity.cookingTotalTime = AbstractFurnaceBlockEntity.getTotalCookTime(world, blockEntity, blockEntity.recipeType, blockEntity.cookSpeedMultiplier); // Paper - cook speed multiplier API
if (AbstractFurnaceBlockEntity.burn(blockEntity.level, blockEntity.worldPosition, world.registryAccess(), recipeholder, singlerecipeinput, blockEntity.items, i)) { // CraftBukkit
2023-09-22 00:26:51 +02:00
blockEntity.setRecipeUsed(recipeholder);
2021-06-13 07:09:48 +02:00
}
2024-12-03 19:05:31 +01:00
@@ -362,13 +371,14 @@ public abstract class AbstractFurnaceBlockEntity extends BaseContainerBlockEntit
2024-10-23 13:13:00 +02:00
return fuelRegistry.burnDuration(stack);
2021-06-13 07:09:48 +02:00
}
2024-10-25 17:08:48 +02:00
- public static int getTotalCookTime(ServerLevel world, AbstractFurnaceBlockEntity furnace) {
2023-09-22 00:26:51 +02:00
- if (world == null) return 200; // CraftBukkit - SPIGOT-4302
2024-10-25 17:08:48 +02:00
+ public static int getTotalCookTime(@Nullable ServerLevel world, AbstractFurnaceBlockEntity furnace, RecipeType<? extends AbstractCookingRecipe> recipeType, double cookSpeedMultiplier) { // Paper - cook speed multiplier API
2024-06-13 21:04:27 +02:00
SingleRecipeInput singlerecipeinput = new SingleRecipeInput(furnace.getItem(0));
- return (Integer) furnace.quickCheck.getRecipeFor(singlerecipeinput, world).map((recipeholder) -> {
2024-10-23 13:13:00 +02:00
- return ((AbstractCookingRecipe) recipeholder.value()).cookingTime();
2023-09-22 00:26:51 +02:00
- }).orElse(200);
2024-06-13 21:04:27 +02:00
+ // Paper start - cook speed multiplier API
2021-06-13 07:09:48 +02:00
+ /* Scale the recipe's cooking time to the current cookSpeedMultiplier */
2024-10-23 13:13:00 +02:00
+ int cookTime = world != null ? furnace.quickCheck.getRecipeFor(singlerecipeinput, world).map(holder -> holder.value().cookingTime()).orElse(200) : (net.minecraft.server.MinecraftServer.getServer().getRecipeManager().getRecipeFor(recipeType, singlerecipeinput, 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().cookingTime()).orElse(200));
2021-06-13 07:09:48 +02:00
+ return (int) Math.ceil (cookTime / cookSpeedMultiplier);
+ // Paper end - cook speed multiplier API
2021-06-13 07:09:48 +02:00
}
2024-10-23 13:13:00 +02:00
@Override
2024-12-03 19:05:31 +01:00
@@ -414,12 +424,11 @@ public abstract class AbstractFurnaceBlockEntity extends BaseContainerBlockEntit
2024-10-23 13:13:00 +02:00
if (world instanceof ServerLevel) {
ServerLevel worldserver = (ServerLevel) world;
- this.cookingTotalTime = AbstractFurnaceBlockEntity.getTotalCookTime(worldserver, this);
+ this.cookingTotalTime = AbstractFurnaceBlockEntity.getTotalCookTime(worldserver, this, this.recipeType, this.cookSpeedMultiplier); // Paper - cook speed multiplier API
2024-12-03 19:05:31 +01:00
this.cookingTimer = 0;
2024-10-23 13:13:00 +02:00
this.setChanged();
}
2021-06-13 07:09:48 +02:00
}
2024-10-23 13:13:00 +02:00
-
}
@Override
2021-06-13 07:09:48 +02:00
diff --git a/src/main/java/org/bukkit/craftbukkit/block/CraftFurnace.java b/src/main/java/org/bukkit/craftbukkit/block/CraftFurnace.java
2024-12-03 19:05:31 +01:00
index 7a642b1b95b36b91698455d50ae85b846d520611..fd2f28ee785283bcc979c7a146308827f2ce2ba9 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
2024-10-23 13:13:00 +02:00
@@ -88,4 +88,20 @@ public abstract class CraftFurnace<T extends AbstractFurnaceBlockEntity> extends
@Override
2024-04-24 07:43:09 +02:00
public abstract CraftFurnace<T> copy(Location location);
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;
2024-10-25 17:08:48 +02:00
+ snapshot.cookingTotalTime = AbstractFurnaceBlockEntity.getTotalCookTime(this.isPlaced() ? this.world.getHandle() : null, snapshot, snapshot.recipeType, 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
}