Skip to content

Commit

Permalink
update BrewingKegBlock.java
Browse files Browse the repository at this point in the history
update BrewingKegBlockEntity.java
  • Loading branch information
MEGATREX4 committed Jul 6, 2024
1 parent 41ae1a1 commit 890c63c
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 61 deletions.
Original file line number Diff line number Diff line change
@@ -1,30 +1,53 @@
package com.megatrex4.ukrainian_dlight.block.custom;


import com.megatrex4.ukrainian_dlight.block.entity.BrewingKegBlockEntity;
import com.megatrex4.ukrainian_dlight.block.entity.ModBlockEntities;
import net.minecraft.block.*;
import net.minecraft.block.BlockWithEntity;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityTicker;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.screen.NamedScreenHandlerFactory;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.ItemScatterer;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.DirectionProperty;
import net.minecraft.state.property.Properties;
import net.minecraft.util.*;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.shape.VoxelShape;
import net.minecraft.world.BlockView;
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;

public class BrewingKegBlock extends BlockWithEntity implements BlockEntityProvider {
public static final DirectionProperty FACING = Properties.HORIZONTAL_FACING;
private static final VoxelShape SHAPE = Block.createCuboidShape(0, 0, 0, 16, 12, 16);

public BrewingKegBlock (Settings settings) {
public BrewingKegBlock(Settings settings) {
super(settings);
this.setDefaultState(this.stateManager.getDefaultState().with(FACING, Direction.NORTH));
}

@Override
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
builder.add(FACING);
}

@Override
public BlockState rotate(BlockState state, BlockRotation rotation) {
return state.with(FACING, rotation.rotate(state.get(FACING)));
}

@Override
public BlockState mirror(BlockState state, BlockMirror mirror) {
return state.rotate(mirror.getRotation(state.get(FACING)));
}

@Override
public BlockState getPlacementState(ItemPlacementContext context) {
return this.getDefaultState().with(FACING, context.getPlayerLookDirection().getOpposite());
}

@Override
Expand Down Expand Up @@ -58,13 +81,11 @@ public void onStateReplaced(BlockState state, World world, BlockPos pos, BlockSt
@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
if (!world.isClient) {
NamedScreenHandlerFactory screenHandlerFactory = ((BrewingKegBlockEntity) world.getBlockEntity(pos));

NamedScreenHandlerFactory screenHandlerFactory = (BrewingKegBlockEntity) world.getBlockEntity(pos);
if (screenHandlerFactory != null) {
player.openHandledScreen(screenHandlerFactory);
}
}

return ActionResult.SUCCESS;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.megatrex4.ukrainian_dlight.block.entity;

import com.megatrex4.ukrainian_dlight.block.DrinkBottleBlock;
import com.megatrex4.ukrainian_dlight.block.custom.BrewingKegBlock;
import com.megatrex4.ukrainian_dlight.networking.ModMessages;
import com.megatrex4.ukrainian_dlight.recipe.BrewingRecipe;
import com.megatrex4.ukrainian_dlight.recipe.ModRecipes;
Expand All @@ -20,6 +21,7 @@
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.entity.ItemEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.fluid.Fluids;
Expand All @@ -30,16 +32,20 @@
import net.minecraft.item.Items;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.recipe.Ingredient;
import net.minecraft.screen.PropertyDelegate;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.Text;
import net.minecraft.util.collection.DefaultedList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class BrewingKegBlockEntity extends BlockEntity implements ExtendedScreenHandlerFactory, ImplementedInventory {
Expand Down Expand Up @@ -179,17 +185,13 @@ public void tick(World world, BlockPos pos, BlockState state) {
return;
}

// Handle water bucket conversion
handleWaterBucket();

// Check if a crafted item is ready to be moved to the OUTPUT_SLOT
ItemStack displayStack = getStack(DRINKS_DISPLAY_SLOT);
ItemStack containerStack = getStack(CONTAINER_SLOT);
ItemStack outputStack = getStack(OUTPUT_SLOT);

// Check if the container matches the saved drinkContainer item
if (!displayStack.isEmpty() && !containerStack.isEmpty() && containerStack.getItem() == drinkContainer.getItem()) {
// Only decrement the container if the OUTPUT_SLOT can receive more items
if (isOutputSlotEmptyOrReceivable(outputStack)) {
containerStack.decrement(1);

Expand All @@ -204,7 +206,6 @@ public void tick(World world, BlockPos pos, BlockState state) {
}
}

// Clear the DRINKS_DISPLAY_SLOT and drinkContainer if empty
if (displayStack.isEmpty()) {
setStack(DRINKS_DISPLAY_SLOT, ItemStack.EMPTY);
drinkContainer = ItemStack.EMPTY;
Expand All @@ -217,28 +218,78 @@ public void tick(World world, BlockPos pos, BlockState state) {
Optional<BrewingRecipe> match = getCurrentRecipe();
if (match.isPresent()) {
BrewingRecipe recipe = match.get();
ItemStack output = recipe.craft(this, world.getRegistryManager());

if (isDisplaySlotEmptyOrReceivable(output)) {
if (this.hasRecipe() && hasEnoughFluid()) {
this.increaseCraftProgress();
markDirty(world, pos, state);
boolean validIngredients = hasValidIngredients(recipe);
boolean displaySlotReceivable = isDisplaySlotEmptyOrReceivable(recipe.craft(this, world.getRegistryManager()));
boolean hasRecipe = hasRecipe();
boolean enoughFluid = hasEnoughFluid();

if (hasCraftingFinished()) {
this.craftItem();
this.resetProgress();
}
} else {
if (validIngredients && displaySlotReceivable && hasRecipe && enoughFluid) {
// System.out.println("All conditions met, attempting to craft item.");
if (this.craftItem(recipe)) {
this.resetProgress();
}
} else {
// System.out.println("Conditions not met, resetting progress.");
this.resetProgress();
markDirty(world, pos, state);
}
} else {
// System.out.println("No matching recipe found, resetting progress.");
this.resetProgress();
}
}






private boolean hasValidIngredients(BrewingRecipe recipe) {
List<ItemStack> ingredients = new ArrayList<>();
for (int i = INGREDIENT_SLOT_1; i <= INGREDIENT_SLOT_6; i++) {
ingredients.add(getStack(i));
}

List<Ingredient> recipeIngredients = recipe.getIngredients();

// Check if the number of ingredients in the recipe is greater than the number of slots
if (recipeIngredients.size() > ingredients.size()) {
return false;
}

// Check that each ingredient in the recipe can be found in the ingredient slots
for (Ingredient ingredient : recipeIngredients) {
boolean foundMatch = false;
for (ItemStack stack : ingredients) {
if (ingredient.test(stack)) {
foundMatch = true;
// Remove the matched ingredient to prevent duplicate matching
ingredients.remove(stack);
break;
}
}
if (!foundMatch) {
return false;
}
}

// Ensure the remaining slots are empty
for (ItemStack stack : ingredients) {
if (!stack.isEmpty()) {
return false;
}
}

return true;
}








private boolean isOutputSlotEmptyOrReceivable(ItemStack output) {
ItemStack outputStack = this.getStack(OUTPUT_SLOT);
return outputStack.isEmpty() || (ItemStack.canCombine(outputStack, output) && outputStack.getCount() < outputStack.getMaxCount());
Expand Down Expand Up @@ -333,50 +384,53 @@ private boolean hasRecipe() {
}


private void craftItem() {
Optional<BrewingRecipe> match = getCurrentRecipe();
if (match.isPresent()) {
BrewingRecipe recipe = match.get();
ItemStack output = recipe.craft(this, world.getRegistryManager());

// Check if all ingredient slots have enough items
for (int i = 0; i < 6; i++) {
ItemStack ingredientStack = getStack(i);
if (ingredientStack.getCount() <= 0) {
return; // Stop crafting if any ingredient slot is empty
}
}

// Ensure the crafting only starts if the DRINKS_DISPLAY_SLOT is empty or contains the same item
if (!getStack(DRINKS_DISPLAY_SLOT).isEmpty() && !ItemStack.canCombine(getStack(DRINKS_DISPLAY_SLOT), output)) {
return;
}

// Check if there's enough fluid for the recipe
if (hasEnoughFluid()) {
extractFluid(recipe.getWaterAmount());
private boolean craftItem(BrewingRecipe recipe) {
if (this.world != null && recipe != null) {
++this.progress;
this.maxProgress = recipe.getBrewingTime();

// Decrement ingredients
for (int i = 0; i < 6; i++) {
getStack(i).decrement(1);
if (this.progress < this.maxProgress) {
return false;
} else {
this.progress = 0;
this.drinkContainer = recipe.getContainer();
ItemStack recipeOutput = recipe.craft(this, this.world.getRegistryManager());
ItemStack currentOutput = this.getStack(DRINKS_DISPLAY_SLOT);

if (currentOutput.isEmpty()) {
this.setStack(DRINKS_DISPLAY_SLOT, recipeOutput.copy());
} else if (currentOutput.getItem() == recipeOutput.getItem()) {
currentOutput.increment(recipeOutput.getCount());
}

// Move the crafted item to the DRINKS_DISPLAY_SLOT
if (!output.isEmpty()) {
if (getStack(DRINKS_DISPLAY_SLOT).isEmpty()) {
setStack(DRINKS_DISPLAY_SLOT, new ItemStack(output.getItem(), 1)); // Only one bottle
drinkContainer = recipe.getContainer(); // Save the container item from the recipe
} else if (ItemStack.canCombine(getStack(DRINKS_DISPLAY_SLOT), output)) {
getStack(DRINKS_DISPLAY_SLOT).increment(1); // Only increment by one
} else {
// Handle case where items cannot combine but DRINKS_DISPLAY_SLOT is not empty
return;
for (int i = 0; i < 6; ++i) {
ItemStack itemStack = this.getStack(i);
for (Ingredient ingredient : recipe.getIngredients()) {
if (ingredient.test(itemStack)) {
if (itemStack.getItem().hasRecipeRemainder() && this.world != null) {
Direction direction = this.getCachedState().get(BrewingKegBlock.FACING).rotateYCounterclockwise();
double dropX = this.pos.getX() + 0.5 + direction.getOffsetX() * 0.25;
double dropY = this.pos.getY() + 0.7;
double dropZ = this.pos.getZ() + 0.5 + direction.getOffsetZ() * 0.25;
ItemEntity entity = new ItemEntity(this.world, dropX, dropY, dropZ, new ItemStack(itemStack.getItem().getRecipeRemainder()));
entity.setVelocity(direction.getOffsetX() * 0.08F, 0.25, direction.getOffsetZ() * 0.08F);
this.world.spawnEntity(entity);
}

itemStack.decrement(1);
break;
}
}
}

// Mark the block entity as dirty after crafting
// Decrement liquid amount here using extractFluid
extractFluid(recipe.getWaterAmount());

markDirty();
return true;
}
} else {
return false;
}
}

Expand All @@ -392,6 +446,10 @@ private void craftItem() {







private void extractFluid(int amount) {
try (Transaction transaction = Transaction.openOuter()) {
this.fluidStorage.extract(FluidVariant.of(Fluids.WATER), amount, transaction);
Expand Down

0 comments on commit 890c63c

Please sign in to comment.