Skip to content

Commit

Permalink
feat: change the behavior of furnace block to match vanilla
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd committed Jan 13, 2025
1 parent fcf16ce commit af543b0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ Unless otherwise specified, any version comparison below is the comparison of se
- (API) `EntityBaseComponent#getBlockStateStandingOn` now return `BlockStateWithPos` instead of `BlockState`.
- (API) Removed `BlockFace#horizontalIndex` which is useless.
- (API) Removed `ScoreboardService#ServerEventListener` as it is not supposed to be touched by plugin.
- (API) Methods `BlockEntityFurnaceBaseComponent#getStoredXP` and `BlockEntityFurnaceBaseComponent#setStoredXP` now accept
`int` instead of `float`.
- Removed useless class `PackageClassLoaderUtils`, dependency `org.reflections.reflections` is also removed.
- Added `-dev` suffix to api version in development build.
- Changed `ContainerActionProcessorHolder` to a final class instead of an interface, because this abstraction is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ public interface BlockEntityFurnaceBaseComponent extends BlockEntityBaseComponen
*
* @return the stored XP
*/
float getStoredXP();
int getStoredXP();

/**
* Sets the stored XP.
*
* @param storedXP the stored XP to set
*/
void setStoredXP(float storedXP);
void setStoredXP(int storedXP);

/**
* Gets the unlit block type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,19 @@
import org.cloudburstmc.protocol.bedrock.packet.ContainerSetDataPacket;
import org.joml.Vector3f;

import java.util.concurrent.ThreadLocalRandom;

/**
* @author daoge_cmd
*/
@Slf4j
public class BlockEntityFurnaceBaseComponentImpl extends BlockEntityBaseComponentImpl implements BlockEntityFurnaceBaseComponent {

public static final int MAX_COOK_TIME = 200;
public static final String TAG_BURN_TIME = "BurnTime";
public static final String TAG_COOK_TIME = "CookTime";
public static final String TAG_BURN_DURATION = "BurnDuration";
public static final String TAG_STORED_XP_INT = "StoredXPInt";

@Dependency
protected BlockEntityContainerHolderComponent containerHolderComponent;
Expand All @@ -49,7 +55,7 @@ public class BlockEntityFurnaceBaseComponentImpl extends BlockEntityBaseComponen
protected int burnDuration; // unit: gt
@Getter
@Setter
protected float storedXP;
protected int storedXP;

protected int currentIngredientStackNetworkId = Integer.MAX_VALUE;
protected FurnaceRecipe currentFurnaceRecipe = null;
Expand Down Expand Up @@ -103,21 +109,21 @@ public float getCurrentSpeed() {
@Override
public NbtMap saveNBT() {
return super.saveNBT().toBuilder()
.putShort("BurnTime", (short) burnTime)
.putShort("CookTime", (short) cookTime)
.putShort("BurnDuration", (short) burnDuration)
.putFloat("StoredXP", storedXP)
.putShort(TAG_BURN_TIME, (short) burnTime)
.putShort(TAG_COOK_TIME, (short) cookTime)
.putShort(TAG_BURN_DURATION, (short) burnDuration)
.putInt(TAG_STORED_XP_INT, storedXP)
.build();
}

@Override
public void loadNBT(NbtMap nbt) {
super.loadNBT(nbt);

nbt.listenForShort("BurnTime", value -> burnTime = value);
nbt.listenForShort("CookTime", value -> cookTime = value);
nbt.listenForShort("BurnDuration", value -> burnDuration = value);
nbt.listenForFloat("StoredXP", value -> storedXP = value);
nbt.listenForShort(TAG_BURN_TIME, value -> burnTime = value);
nbt.listenForShort(TAG_COOK_TIME, value -> cookTime = value);
nbt.listenForShort(TAG_BURN_DURATION, value -> burnDuration = value);
nbt.listenForInt(TAG_STORED_XP_INT, value -> storedXP = value);
}

@Override
Expand Down Expand Up @@ -198,7 +204,16 @@ protected void tickFurnace() {
container.notifySlotChange(FurnaceContainer.RESULT_SLOT);
}

storedXP += output.getItemData().furnaceXPMultiplier();
// Calculate the amount of experience to grant
// Round the experience down to the nearest integer, and the remaining
// XP is a chance to be granted an additional experience point
var xp = output.getItemData().furnaceXPMultiplier();
int earned = (int) Math.floor(xp);
var chance = xp - earned;
if (chance > 0 && ThreadLocalRandom.current().nextFloat() < chance) {
earned++;
}
storedXP += earned;
}

protected boolean checkIngredient(ItemStack ingredient) {
Expand Down

0 comments on commit af543b0

Please sign in to comment.