Skip to content

Commit

Permalink
in word brewing_keg interaction
Browse files Browse the repository at this point in the history
  • Loading branch information
MEGATREX4 committed Jul 14, 2024
1 parent 8b4b9f9 commit 6c2cbe9
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 34 deletions.
Binary file added projectfiles/brewing_keg_gui_v2.psd
Binary file not shown.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@
import net.minecraft.inventory.Inventories;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.registry.Registries;
import net.minecraft.screen.NamedScreenHandlerFactory;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.DirectionProperty;
import net.minecraft.state.property.EnumProperty;
Expand Down Expand Up @@ -233,14 +236,90 @@ public static void spawnParticles(World world, BlockPos pos, BlockState state) {
@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
if (!world.isClient) {
NamedScreenHandlerFactory screenHandlerFactory = state.createScreenHandlerFactory(world, pos);
if (screenHandlerFactory != null) {
player.openHandledScreen(screenHandlerFactory);
BrewingKegBlockEntity blockEntity = (BrewingKegBlockEntity) world.getBlockEntity(pos);
if (blockEntity == null) {
return ActionResult.PASS;
}

if (hand == Hand.MAIN_HAND) {
ItemStack heldItem = player.getStackInHand(hand);
if (heldItem.isOf(Items.WATER_BUCKET)) {
return handleWaterBucket(world, pos, player, hand, blockEntity);
} else {
return handleContainerFill(world, pos, player, hand, blockEntity);
}
}

openScreenForPlayer(state, world, pos, player);
}
return ActionResult.SUCCESS;
}

private ActionResult handleWaterBucket(World world, BlockPos pos, PlayerEntity player, Hand hand, BrewingKegBlockEntity blockEntity) {
ItemStack heldItem = player.getStackInHand(hand);
long waterAmount = blockEntity.getWaterAmount();
long waterCapacity = blockEntity.getWaterCapacity();

if (waterAmount + 1000 <= waterCapacity) {
blockEntity.addWater(1000);
if (!world.isClient) {
world.playSound(null, pos, SoundEvents.ITEM_BUCKET_EMPTY, SoundCategory.BLOCKS, 1.0F, 1.0F);
}
if (!player.isCreative()) {
player.setStackInHand(hand, new ItemStack(Items.BUCKET));
}
return ActionResult.SUCCESS;
}
return ActionResult.FAIL;
}

private ActionResult handleContainerFill(World world, BlockPos pos, PlayerEntity player, Hand hand, BrewingKegBlockEntity blockEntity) {
ItemStack heldItem = player.getStackInHand(hand);
ItemStack requiredContainer = blockEntity.getStack(REQUIRE_CONTAINER);

if (heldItem.isOf(requiredContainer.getItem())) {
ItemStack displayItem = blockEntity.getStack(DRINKS_DISPLAY_SLOT);

if (!displayItem.isEmpty() && displayItem.getCount() > 0) {
ItemStack filledContainer = displayItem.copy();
filledContainer.setCount(1);

displayItem.decrement(1);
heldItem.decrement(1);

if (!giveItemToPlayerOrDrop(world, player, filledContainer)) {
return ActionResult.FAIL;
}

blockEntity.markDirty();
blockEntity.sendFluidPacket();
return ActionResult.SUCCESS;
}
}
return ActionResult.FAIL;
}

private boolean giveItemToPlayerOrDrop(World world, PlayerEntity player, ItemStack itemStack) {
if (!player.getInventory().insertStack(itemStack)) {
ItemEntity itemEntity = new ItemEntity(world, player.getX(), player.getY(), player.getZ(), itemStack);
world.spawnEntity(itemEntity);
return false;
}
return true;
}

private void openScreenForPlayer(BlockState state, World world, BlockPos pos, PlayerEntity player) {
NamedScreenHandlerFactory screenHandlerFactory = state.createScreenHandlerFactory(world, pos);
if (screenHandlerFactory != null) {
player.openHandledScreen(screenHandlerFactory);
}
}






@Nullable
@Override
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(World world, BlockState state, BlockEntityType<T> type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
import com.megatrex4.ukrainian_dlight.recipe.BrewingRecipe;
import com.megatrex4.ukrainian_dlight.recipe.ModRecipes;
import com.megatrex4.ukrainian_dlight.screen.BrewingKegScreenHandler;
import com.megatrex4.ukrainian_dlight.util.CompoundTagUtils;
import com.megatrex4.ukrainian_dlight.util.FluidStack;
import com.nhoryzon.mc.farmersdelight.FarmersDelightMod;
import com.nhoryzon.mc.farmersdelight.entity.block.CookingPotBlockEntity;
import com.nhoryzon.mc.farmersdelight.registry.ParticleTypesRegistry;
import com.nhoryzon.mc.farmersdelight.util.CompoundTagUtils;

import io.netty.buffer.Unpooled;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
Expand Down Expand Up @@ -495,26 +493,35 @@ private void handleWaterBucket() {
ItemStack waterBucketStack = this.getStack(WATER_SLOT);

if (waterBucketStack.getItem() == Items.WATER_BUCKET) {
try (Transaction transaction = Transaction.openOuter()) {
long amountToAdd = FluidStack.convertDropletsToMb(FluidConstants.BUCKET);
long insertedAmount = this.fluidStorage.insert(FluidVariant.of(Fluids.WATER), amountToAdd, transaction);

if (insertedAmount == amountToAdd) {
// Successfully added water, replace the bucket with an empty one
this.setStack(WATER_SLOT, new ItemStack(Items.BUCKET));
transaction.commit();
markDirty();
sendFluidPacket();

// Play water pouring sound effect
if (!this.world.isClient) {
this.world.playSound(null, this.pos, SoundEvents.ITEM_BUCKET_EMPTY, SoundCategory.BLOCKS, 1.0F, 1.0F);
}
boolean success = this.addWater(1000); // 1000 mb equals one bucket

if (success) {
// Successfully added water, replace the bucket with an empty one
this.setStack(WATER_SLOT, new ItemStack(Items.BUCKET));
markDirty();
sendFluidPacket();

// Play water pouring sound effect
if (!this.world.isClient) {
this.world.playSound(null, this.pos, SoundEvents.ITEM_BUCKET_EMPTY, SoundCategory.BLOCKS, 1.0F, 1.0F);
}
}
}
}

public boolean addWater(long WaterAmountAdd) {
try (Transaction transaction = Transaction.openOuter()) {
long insertedAmount = this.fluidStorage.insert(FluidVariant.of(Fluids.WATER), WaterAmountAdd, transaction);

if (insertedAmount == WaterAmountAdd) {
transaction.commit();
return true; // Successfully added water
}
}
return false; // Failed to add water
}



private static void transferFluidToFluidStorage(BrewingKegBlockEntity entity) {
try(Transaction transaction = Transaction.openOuter()) {
Expand All @@ -535,8 +542,17 @@ private boolean hasEnoughFluid() {
return false;
}

public long getWaterAmount() {
return fluidStorage.amount;
}

public long getWaterCapacity() {
return fluidStorage.getCapacity();
}



private void sendFluidPacket() {
public void sendFluidPacket() {
PacketByteBuf data = PacketByteBufs.create();
fluidStorage.variant.toPacket(data);
data.writeLong(fluidStorage.amount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public void onEndTick(MinecraftServer server) {
activeHandlers.remove(this);

// Optionally, you can unregister from the event here if it supports it
// ServerTickEvents.END_SERVER_TICK.unregister(this);
ServerTickEvents.END_SERVER_TICK.equals(this);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,22 @@ public static void applySecondaryEffects(PlayerEntity player, StatusEffectInstan
int amplifier = mainEffect.getAmplifier() + 1;

if (player.getInventory().contains(ModTags.LIGHT_DRINK)) {
applyWeakness(player, duration / 2, (int) (amplifier * 1.3)); // Convert to int after multiplication
applyNausea(player, duration / 2, (int) (amplifier * 1.3)); // Convert to int after multiplication
applyWeakness(player, duration / 2, (int) (amplifier * 1.3));
applyNausea(player, duration / 2, (int) (amplifier * 1.3));
if (RANDOM.nextFloat() < 0.2) {
applyBlindness(player, (int) (duration * 0.7), (int) (amplifier * 1.3));
}
} else if (player.getInventory().contains(ModTags.MID_DRINK)) {
applyWeakness(player, (int) (duration * 0.7), (int) (amplifier * 1.7)); // Convert to int after multiplication
applyNausea(player, (int) (duration * 0.7), (int) (amplifier * 1.7)); // Convert to int after multiplication
if (RANDOM.nextFloat() < 0.5) {
applyBlindness(player, (int) (duration * 0.7), (int) (amplifier * 1.7)); // Convert to int after multiplication
applyWeakness(player, (int) (duration * 0.7), (int) (amplifier * 1.5));
applyNausea(player, (int) (duration * 0.7), (int) (amplifier * 1.5));
if (RANDOM.nextFloat() < 0.7) {
applyBlindness(player, (int) (duration * 0.7), (int) (amplifier * 1.5));
}
} else if (player.getInventory().contains(ModTags.STRONG_DRINK)) {
applyWeakness(player, (int) (duration * 0.9), (int) (amplifier * 2.2)); // Convert to int after multiplication
applyNausea(player, (int) (duration * 0.9), (int) (amplifier * 2.2)); // Convert to int after multiplication
if (RANDOM.nextFloat() < 0.7) {
applyBlindness(player, (int) (duration * 0.9), (int) (amplifier * 2.2)); // Convert to int after multiplication
applyWeakness(player, (int) (duration * 0.9), (int) (amplifier * 1.7));
applyNausea(player, (int) (duration * 0.9), (int) (amplifier * 1.7));
if (RANDOM.nextFloat() < 0.8) {
applyBlindness(player, (int) (duration * 0.9), (int) (amplifier * 2.2));
}
}
}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 6c2cbe9

Please sign in to comment.