Skip to content

Commit

Permalink
Waterbottle fill. Fixes #5 and #1
Browse files Browse the repository at this point in the history
  • Loading branch information
oierbravo committed Mar 31, 2024
1 parent 0c91d77 commit a554660
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 36 deletions.
24 changes: 23 additions & 1 deletion src/main/java/com/oierbravo/watercondenser/WaterCondenser.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@
import com.oierbravo.watercondenser.entity.WatercondenserBlockEntity;
import com.oierbravo.watercondenser.item.ModItems;
import com.oierbravo.watercondenser.network.ModMessages;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.CreativeModeTabs;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.alchemy.Potion;
import net.minecraft.world.item.alchemy.Potions;
import net.minecraft.world.level.Level;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.BuildCreativeModeTabContentsEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.event.server.ServerAboutToStartEvent;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.eventbus.api.SubscribeEvent;
Expand All @@ -19,7 +26,7 @@
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import org.slf4j.Logger;


import net.minecraft.world.item.alchemy.PotionUtils;
@Mod(WaterCondenser.MODID)
public class WaterCondenser
{
Expand Down Expand Up @@ -53,5 +60,20 @@ private void addCreative(BuildCreativeModeTabContentsEvent event) {
public void onServerAboutToStart(final ServerAboutToStartEvent event) {
WatercondenserBlockEntity.verifyConfig(LOGGER);
}
//@SubscribeEvent
//public void onBottleClick(PlayerInteractEvent.RightClickItem e) {
// Player player = e.getEntity();
// Level world = e.getLevel();
// InteractionHand hand = e.getHand();

// ItemStack stack = player.getItemInHand(hand);
// Potion waterPotion = Potion.byName(Potions.WATER.toString());
//player.getInventory().placeItemBackInInventory(new ItemStack(waterPotion));
//player.getInventory().placeItemBackInInventory(meshInv.getStackInSlot(0));


//if (BottleEvent.onBottleClick(e.getEntity(), e.getLevel(), e.getHand()).getResult().equals(InteractionResult.FAIL)) {
// e.setCanceled(true);
//}
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.alchemy.PotionUtils;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.*;
Expand All @@ -18,15 +21,16 @@
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.DirectionProperty;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraftforge.fluids.FluidUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import net.minecraft.world.item.alchemy.Potions;

public class WatercondenserBlock extends BaseEntityBlock{
public static final DirectionProperty FACING = BlockStateProperties.HORIZONTAL_FACING;


public WatercondenserBlock(Properties properties) {
super(properties);
this.registerDefaultState(this.getStateDefinition().any().setValue(FACING, Direction.NORTH));
Expand Down Expand Up @@ -58,18 +62,6 @@ public RenderShape getRenderShape(BlockState pState) {
return RenderShape.MODEL;
}

@Override
public void onRemove(BlockState pState, Level pLevel, BlockPos pPos, BlockState pNewState, boolean pIsMoving) {
if (pState.getBlock() != pNewState.getBlock()) {
BlockEntity blockEntity = pLevel.getBlockEntity(pPos);
if (blockEntity instanceof WatercondenserBlockEntity) {
//((WatercondenserBlockEntity) blockEntity).drops();
}
}
super.onRemove(pState, pLevel, pPos, pNewState, pIsMoving);
}


@Override
public @NotNull InteractionResult use(BlockState pState, Level pLevel, BlockPos pPos,
Player pPlayer, InteractionHand pHand, BlockHitResult pHit) {
Expand All @@ -82,6 +74,20 @@ public void onRemove(BlockState pState, Level pLevel, BlockPos pPos, BlockState
if(success){
watercondenser.setChanged();
}

ItemStack held = pPlayer.getItemInHand(pHand);

if (!pLevel.isClientSide() && held.getItem() == Items.GLASS_BOTTLE){
boolean waterConsumed = watercondenser.consumeWaterBottle();
if(waterConsumed) {
held.shrink(1);
ItemStack waterPotion = PotionUtils.setPotion(new ItemStack(Items.POTION), Potions.WATER);
pPlayer.getInventory().placeItemBackInInventory(waterPotion);
return InteractionResult.CONSUME;
}
return InteractionResult.FAIL;
}

} else {
throw new IllegalStateException("Our Container provider is missing!");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class ModConfigCommon {
public static final ForgeConfigSpec.ConfigValue<Integer> CONDENSER_MB_PER_CYCLE;
public static final ForgeConfigSpec.ConfigValue<Float> CONDENSER_MB_MULTI_MIN;
public static final ForgeConfigSpec.ConfigValue<Float> CONDENSER_MB_MULTI_MAX;
public static final ForgeConfigSpec.ConfigValue<Integer> CONDENSER_BOTTLE_MB_CONSUMPTION;

static {
BUILDER.push("Configs for WaterCondenser");
Expand All @@ -30,6 +31,9 @@ public class ModConfigCommon {
CONDENSER_MB_MULTI_MAX = BUILDER.comment("For random variance, the maximum multiplier for each fill cycle")
.define("Fluid multiplier chance max", 1.0f);

CONDENSER_BOTTLE_MB_CONSUMPTION = BUILDER.comment("Bottle consumption per bottle, in mB")
.define("Fluid amount in mB", 250);


BUILDER.pop();
SPEC = BUILDER.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,6 @@ public IFluidHandler getFluidHandler() {
return this.fluidTankHandler;
}

/*@Override
public CompoundTag getUpdateTag() {
this.saveAdditional(updateTag);
return updateTag;
}*/
@Nullable
@Override
public ClientboundBlockEntityDataPacket getUpdatePacket() {
Expand All @@ -185,24 +180,16 @@ public void handleUpdateTag(CompoundTag tag) {
this.load(tag);
}



/*public void clientSync() {
if (Objects.requireNonNull(this.getLevel()).isClientSide) {
return;
}
ServerLevel world = (ServerLevel) this.getLevel();
Stream<ServerPlayer> entities = world.getChunkSource().chunkMap.getPlayers(new ChunkPos(this.worldPosition), false).stream();
Packet<ClientGamePacketListener> updatePacket = this.getUpdatePacket();
entities.forEach(e -> {
if (updatePacket != null) {
e.connection.send(updatePacket);
}
});
}*/


public void setFluid(FluidStack fluidStack) {
this.fluidTankHandler.setFluid(fluidStack);
}

public boolean consumeWaterBottle() {
int consumption = ModConfigCommon.CONDENSER_BOTTLE_MB_CONSUMPTION.get();
if( consumption > fluidTankHandler.getFluidAmount()){
return false;
}
fluidTankHandler.drain(consumption,FluidAction.EXECUTE);
return true;
}
}

0 comments on commit a554660

Please sign in to comment.