Skip to content

Commit

Permalink
fragment code BrewingKegBlockEntity.java
Browse files Browse the repository at this point in the history
  • Loading branch information
MEGATREX4 committed Jul 6, 2024
1 parent 890c63c commit b2d52df
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import com.megatrex4.ukrainian_dlight.screen.BrewingKegScreenHandler;
import com.megatrex4.ukrainian_dlight.util.FluidStack;
import io.netty.buffer.Unpooled;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectIterator;
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.fabricmc.fabric.api.networking.v1.PlayerLookup;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
Expand All @@ -18,9 +21,11 @@
import net.fabricmc.fabric.api.transfer.v1.storage.base.SingleVariantStorage;
import net.fabricmc.fabric.api.transfer.v1.transaction.Transaction;
import net.fabricmc.fabric.api.transfer.v1.transaction.TransactionContext;
import net.fabricmc.fabric.api.util.NbtType;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.entity.ExperienceOrbEntity;
import net.minecraft.entity.ItemEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
Expand All @@ -33,14 +38,18 @@
import net.minecraft.nbt.NbtCompound;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.recipe.Ingredient;
import net.minecraft.recipe.Recipe;
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.Identifier;
import net.minecraft.util.collection.DefaultedList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;

Expand All @@ -51,6 +60,7 @@
public class BrewingKegBlockEntity extends BlockEntity implements ExtendedScreenHandlerFactory, ImplementedInventory {
private final DefaultedList<ItemStack> inventory = DefaultedList.ofSize(10, ItemStack.EMPTY);


//adds 6 input ingredients slot
public static final int INGREDIENT_SLOT_1 = 0;
public static final int INGREDIENT_SLOT_2 = 1;
Expand All @@ -70,6 +80,7 @@ public class BrewingKegBlockEntity extends BlockEntity implements ExtendedScreen
private int progress;
private int maxProgress = 200; // Adjusted to match the brewing time in the JSON
private ItemStack drinkContainer = ItemStack.EMPTY;
private float totalExperience = 0;

public final SingleVariantStorage<FluidVariant> fluidStorage = new SingleVariantStorage<FluidVariant>() {
@Override
Expand Down Expand Up @@ -140,8 +151,12 @@ protected void writeNbt(NbtCompound nbt) {
NbtCompound containerTag = new NbtCompound();
drinkContainer.writeNbt(containerTag);
nbt.put("Container", containerTag);

// Save totalExperience
nbt.putFloat("TotalExperience", totalExperience);
}


@Override
public void readNbt(NbtCompound nbt) {
super.readNbt(nbt);
Expand All @@ -153,6 +168,11 @@ public void readNbt(NbtCompound nbt) {
// Load drinkContainer
NbtCompound containerTag = nbt.getCompound("Container");
drinkContainer = ItemStack.fromNbt(containerTag);

// Load totalExperience
if (nbt.contains("TotalExperience", NbtType.FLOAT)) {
totalExperience = nbt.getFloat("TotalExperience");
}
}


Expand Down Expand Up @@ -186,7 +206,11 @@ public void tick(World world, BlockPos pos, BlockState state) {
}

handleWaterBucket();
processItemTransfer(world, pos, state);
processCrafting();
}

private void processItemTransfer(World world, BlockPos pos, BlockState state) {
ItemStack displayStack = getStack(DRINKS_DISPLAY_SLOT);
ItemStack containerStack = getStack(CONTAINER_SLOT);
ItemStack outputStack = getStack(OUTPUT_SLOT);
Expand All @@ -211,10 +235,28 @@ public void tick(World world, BlockPos pos, BlockState state) {
drinkContainer = ItemStack.EMPTY;
}

markDirty(world, pos, state);
markDirty(); // Mark the block entity as dirty

// Give experience to the closest player
giveExperience(world, pos);
}
}
}

private void giveExperience(World world, BlockPos pos) {
int xpToGive = MathHelper.floor(totalExperience);
if (xpToGive > 0) {
PlayerEntity player = world.getClosestPlayer(pos.getX(), pos.getY(), pos.getZ(), -1.0, false);
if (player != null) {
player.addExperience(xpToGive);
totalExperience -= xpToGive;
}
}
}



private void processCrafting() {
Optional<BrewingRecipe> match = getCurrentRecipe();
if (match.isPresent()) {
BrewingRecipe recipe = match.get();
Expand All @@ -225,16 +267,15 @@ public void tick(World world, BlockPos pos, BlockState state) {
boolean enoughFluid = hasEnoughFluid();

if (validIngredients && displaySlotReceivable && hasRecipe && enoughFluid) {
// System.out.println("All conditions met, attempting to craft item.");
if (this.craftItem(recipe)) {
// Calculate crafted amount, assuming 1 for now
int craftedAmount = 1;
if (this.craftItem(recipe, craftedAmount)) { // Provide craftedAmount here
this.resetProgress();
}
} else {
// System.out.println("Conditions not met, resetting progress.");
this.resetProgress();
}
} else {
// System.out.println("No matching recipe found, resetting progress.");
this.resetProgress();
}
}
Expand All @@ -243,7 +284,6 @@ public void tick(World world, BlockPos pos, BlockState state) {




private boolean hasValidIngredients(BrewingRecipe recipe) {
List<ItemStack> ingredients = new ArrayList<>();
for (int i = INGREDIENT_SLOT_1; i <= INGREDIENT_SLOT_6; i++) {
Expand Down Expand Up @@ -284,22 +324,12 @@ private boolean hasValidIngredients(BrewingRecipe recipe) {
}








private boolean isOutputSlotEmptyOrReceivable(ItemStack output) {
ItemStack outputStack = this.getStack(OUTPUT_SLOT);
return outputStack.isEmpty() || (ItemStack.canCombine(outputStack, output) && outputStack.getCount() < outputStack.getMaxCount());
}






private void handleWaterBucket() {
ItemStack waterBucketStack = this.getStack(WATER_SLOT);

Expand Down Expand Up @@ -351,23 +381,28 @@ private void sendFluidPacket() {
}
}


public void setFluidLevel(FluidVariant fluidVariant, long fluidLevel) {
this.fluidStorage.variant = fluidVariant;
this.fluidStorage.amount = fluidLevel;
}


private void resetProgress() {
this.progress = 0;
}


private boolean hasCraftingFinished() {
return progress >= maxProgress;
}


private void increaseCraftProgress() {
progress++;
}


private Optional<BrewingRecipe> getCurrentRecipe() {
if (world == null) return Optional.empty();
return world.getRecipeManager().getFirstMatch(ModRecipes.BREWING, this, world);
Expand All @@ -384,7 +419,7 @@ private boolean hasRecipe() {
}


private boolean craftItem(BrewingRecipe recipe) {
private boolean craftItem(BrewingRecipe recipe, int craftedAmount) {
if (this.world != null && recipe != null) {
++this.progress;
this.maxProgress = recipe.getBrewingTime();
Expand Down Expand Up @@ -424,7 +459,12 @@ private boolean craftItem(BrewingRecipe recipe) {
}

// Decrement liquid amount here using extractFluid
extractFluid(recipe.getWaterAmount());
this.extractFluid(recipe.getWaterAmount());

// Calculate total experience and track it
float recipeExperience = recipe.getExperience();
float totalExperience = recipeExperience * craftedAmount;
this.trackRecipeExperience(totalExperience);

markDirty();
return true;
Expand All @@ -438,18 +478,6 @@ private boolean craftItem(BrewingRecipe recipe) {















private void extractFluid(int amount) {
try (Transaction transaction = Transaction.openOuter()) {
this.fluidStorage.extract(FluidVariant.of(Fluids.WATER), amount, transaction);
Expand Down Expand Up @@ -489,4 +517,8 @@ public void markDirty() {
syncFluidToClient();
}


public void trackRecipeExperience(float totalExperience) {
this.totalExperience += totalExperience;
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"parent": "tutorialmod:block/brewing_keg"
"parent": "item/generated",
"textures": {
"layer0": "ukrainian_delight:item/borscht"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"item": "minecraft:glass_bottle"
},
"brewingtime": 200,
"experience": 1.0,
"experience": 100.0,
"water": 100,
"ingredients": [
{
Expand Down

0 comments on commit b2d52df

Please sign in to comment.