Skip to content

Commit

Permalink
update BlockItem for BrewingKegBlockEntity
Browse files Browse the repository at this point in the history
getItemBarStep
getItemBarColor
  • Loading branch information
MEGATREX4 committed Aug 8, 2024
1 parent e71313d commit 9157177
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 25 deletions.
14 changes: 11 additions & 3 deletions src/main/java/com/megatrex4/ukrainian_dlight/block/ModBlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.megatrex4.ukrainian_dlight.UkrainianDelight;
import com.megatrex4.ukrainian_dlight.block.custom.BrewingKegBlock;
import com.megatrex4.ukrainian_dlight.block.custom.BrewingKegBlockItem;
import com.megatrex4.ukrainian_dlight.block.custom.SaltBlock;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.block.Block;
Expand Down Expand Up @@ -32,9 +33,16 @@ private static Block registerBlock(String name, Block block){
return Registry.register(Registries.BLOCK, new Identifier(UkrainianDelight.MOD_ID, name), block);
}

private static Item registerBlockItem(String name, Block block){
return Registry.register(Registries.ITEM, new Identifier(UkrainianDelight.MOD_ID, name),
new BlockItem(block, new Item.Settings()));
private static void registerBlockItem(String name, Block block){
Item item;
if (block instanceof BrewingKegBlock) {
// Use custom BrewingKegBlockItem for BrewingKegBlock
item = new BrewingKegBlockItem(block, new Item.Settings().maxCount(1));
} else {
// Default BlockItem for other blocks
item = new BlockItem(block, new Item.Settings());
}
Registry.register(Registries.ITEM, new Identifier(UkrainianDelight.MOD_ID, name), item);
}

public static void registerModBlocks() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.megatrex4.ukrainian_dlight.block.custom;

import net.minecraft.block.Block;
import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.util.math.MathHelper;

public class BrewingKegBlockItem extends BlockItem {

public BrewingKegBlockItem(Block block, Item.Settings settings) {
super(block, settings);
}

@Override
public boolean isItemBarVisible(ItemStack stack) {
return getDrinkCount(stack) > 0;
}

@Override
public int getItemBarStep(ItemStack stack) {
int drinkCount = getDrinkCount(stack);
int maxCapacity = getMaxCapacity(stack); // Get max capacity from item data
return Math.round(13.0F * drinkCount / (float) maxCapacity);
}

@Override
public int getItemBarColor(ItemStack stack) {
// Convert HEX #6666ff to HSV
float hue = 240.0F / 360.0F; // Hue for #6666ff is approximately 240 degrees
float saturation = 0.6F; // Saturation of #6666ff
float value = 1.0F; // Full brightness

return MathHelper.hsvToRgb(hue, saturation, value);
}





private int getDrinkCount(ItemStack stack) {
if (stack.getNbt() != null && stack.getNbt().contains("BlockEntityTag")) {
NbtCompound tag = stack.getNbt().getCompound("BlockEntityTag");
if (tag.contains("DisplaySlot", NbtElement.COMPOUND_TYPE)) {
ItemStack drink = ItemStack.fromNbt(tag.getCompound("DisplaySlot"));
return drink.getCount();
}
}
return 0;
}

public static int getMaxCapacity(ItemStack stack) {
if (stack.getNbt() != null && stack.getNbt().contains("BlockEntityTag")) {
NbtCompound tag = stack.getNbt().getCompound("BlockEntityTag");
if (tag.contains("DisplaySlot", NbtElement.COMPOUND_TYPE)) {
ItemStack drink = ItemStack.fromNbt(tag.getCompound("DisplaySlot"));
// Return the max stack size of the item in DisplaySlot
return drink.getMaxCount();
}
} // Default capacity if no valid DisplaySlot item is found
return 0;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -312,13 +312,14 @@ public void tick(World world, BlockPos pos, BlockState state, BrewingKegBlockEnt
handleWaterBucket();
boolean enoughFluid = hasEnoughFluid();

// Check if all required slots are filled correctly
if (blockEntity.hasInput() && enoughFluid) {
Optional<BrewingRecipe> match = getCurrentRecipe();
if (match.isPresent()) {
BrewingRecipe recipe = match.get();
if (blockEntity.canCook(recipe)) {
// Process brewing only if recipe is correct
dirty = blockEntity.processBrewing(recipe);

} else {
blockEntity.progress = 0;
}
Expand All @@ -343,6 +344,7 @@ public void tick(World world, BlockPos pos, BlockState state, BrewingKegBlockEnt
}
}


private boolean hasInput() {
for (int i = 0; i < DRINKS_DISPLAY_SLOT; ++i) {
if (i != WATER_SLOT && i != CONTAINER_SLOT && !getStack(i).isEmpty()) {
Expand All @@ -358,24 +360,34 @@ protected boolean canCook(Recipe<?> recipeIn) {
ItemStack recipeOutput = recipeIn.getOutput(world.getRegistryManager());
if (recipeOutput.isEmpty()) {
return false;
} else {
ItemStack currentOutput = getStack(DRINKS_DISPLAY_SLOT);
if (currentOutput.isEmpty()) {
return true;
} else if (!ItemStack.areItemsEqual(currentOutput, recipeOutput)) {
}

// Check that all required ingredients are present and no extra items are there
for (int i = 0; i < DRINKS_DISPLAY_SLOT; ++i) {
if (i == WATER_SLOT || i == CONTAINER_SLOT) continue;
ItemStack stack = getStack(i);
if (!stack.isEmpty() && !recipeIn.getIngredients().stream().anyMatch(ingredient -> ingredient.test(stack))) {
return false;
} else if (currentOutput.getCount() + recipeOutput.getCount() <= getMaxCountPerStack()) {
return true;
} else {
return currentOutput.getCount() + recipeOutput.getCount() <= recipeOutput.getMaxCount();
}
}

ItemStack currentOutput = getStack(DRINKS_DISPLAY_SLOT);
if (currentOutput.isEmpty()) {
return true;
} else if (!ItemStack.areItemsEqual(currentOutput, recipeOutput)) {
return false;
} else if (currentOutput.getCount() + recipeOutput.getCount() <= getMaxCountPerStack()) {
return true;
} else {
return currentOutput.getCount() + recipeOutput.getCount() <= recipeOutput.getMaxCount();
}
} else {
return false;
}
}



private boolean processBrewing(BrewingRecipe recipe) {
if (world == null || recipe == null) return false;

Expand All @@ -385,6 +397,7 @@ private boolean processBrewing(BrewingRecipe recipe) {
return false;
}

// Complete the brewing process
progress = 0;
drinkContainer = recipe.getContainer();
ItemStack recipeOutput = recipe.getOutput(world.getRegistryManager());
Expand All @@ -397,29 +410,31 @@ private boolean processBrewing(BrewingRecipe recipe) {
trackRecipeExperience(recipe);
this.extractFluid(recipe.getWaterAmount());

// Consume ingredients and handle recipe remainder
for (int i = 0; i < DRINKS_DISPLAY_SLOT; ++i) {
if (i == WATER_SLOT || i == CONTAINER_SLOT) continue;

ItemStack itemStack = getStack(i);
if (itemStack.getItem().hasRecipeRemainder() && world != null) {
Direction direction = getCachedState().get(BrewingKegBlock.FACING).rotateYCounterclockwise();
double dropX = pos.getX() + .5d + (direction.getOffsetX() * .25d);
double dropY = pos.getY() + .7d;
double dropZ = pos.getZ() + .5d + (direction.getOffsetZ() * .25d);
ItemEntity entity = new ItemEntity(world, dropX, dropY, dropZ, new ItemStack(getStack(i).getItem()
.getRecipeRemainder()));
entity.setVelocity(direction.getOffsetX() * .08f, .25f, direction.getOffsetZ() * .08f);
world.spawnEntity(entity);
}
if (!itemStack.isEmpty()) {
if (itemStack.getItem().hasRecipeRemainder()) {
Direction direction = getCachedState().get(BrewingKegBlock.FACING).rotateYCounterclockwise();
double dropX = pos.getX() + .5d + (direction.getOffsetX() * .25d);
double dropY = pos.getY() + .7d;
double dropZ = pos.getZ() + .5d + (direction.getOffsetZ() * .25d);
ItemEntity entity = new ItemEntity(world, dropX, dropY, dropZ, new ItemStack(itemStack.getItem().getRecipeRemainder()));
entity.setVelocity(direction.getOffsetX() * .08f, .25f, direction.getOffsetZ() * .08f);
world.spawnEntity(entity);
}

if (!getStack(i).isEmpty()) {
getStack(i).decrement(1);
itemStack.decrement(1); // Consume the item
}
}

return true;
}



private void useStoredContainersOnDrink() {
ItemStack drinkDisplay = getStack(DRINKS_DISPLAY_SLOT);
ItemStack containerInput = getStack(CONTAINER_SLOT);
Expand Down

0 comments on commit 9157177

Please sign in to comment.